Commit 034e0577 authored by Phil Hagelberg's avatar Phil Hagelberg

Test our own bencode lib.

parent 1251b157
local encode, decode
local function decode_list(str, t, total_len)
-- print("list", str, lume.serialize(t))
if(str:sub(1,1) == "e") then return t, total_len + 1 end
......@@ -18,7 +20,7 @@ local function decode_table(str, t, total_len)
return decode_table(str:sub(end_pos), t, total_len)
end
local function decode(str)
function decode(str)
-- print("decoding", str)
if(str:sub(1,1) == "l") then
return decode_list(str:sub(2), {}, 1)
......@@ -37,8 +39,6 @@ local function decode(str)
end
end
local encode
local function encode_str(s) return #s .. ":" .. s end
local function encode_int(n) return "i" .. tostring(n) .. "e" end
......@@ -58,7 +58,7 @@ function encode(x)
if(type(x) == "table" and select("#", unpack(x)) == lume.count(x)) then
return encode_list(x)
elseif(type(x) == "table") then
return encode_table(t)
return encode_table(x)
elseif(type(x) == "number" and math.floor(x) == x) then
return encode_int(x)
elseif(type(x) == "string") then
......
......@@ -2,6 +2,12 @@
BUILD_DATE=$(shell dpkg-parsechangelog --show-field=Date)
# TODO:
# * use fonts from apt
# * patch makefile to remove "check" target
# * package embedded libs? (lume, globtopattern, serpent, utf8)
# * replace md5 lib with an FFI call
%:
dh $@
......
local t = require("lunatest")
local bencode = require("bencode")
local assert_kv_equal = function(t1, t2)
t.assert_equal(lume.count(t1), lume.count(t2))
for k in pairs(t1) do t.assert_equal(t1[k], t2[k]) end
end
function test_decoding_scalars()
t.assert_equal(bencode.decode("i42e"), 42)
t.assert_equal(bencode.decode("4:junk"), "junk")
end
function test_decoding_tables()
assert_kv_equal(bencode.decode("l4:hurgi42ee"), {"hurg", 42})
assert_kv_equal(bencode.decode("d3:bar4:spam3:fooi42ee"),
{bar="spam",foo=42})
end
function test_roundtrip()
local tbl = {a=1,b=2}
assert_kv_equal(tbl, bencode.decode(bencode.encode(tbl)))
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment