Skip to content

Commit 0f63b92

Browse files
committed
Improve the autotester to handle tests where pack(unpack(d)) ~= d by design (e.g. rounding)
1 parent f823dd2 commit 0f63b92

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

vstruct/test/common.lua

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,43 @@ function test.record(name, result, data, message)
4747
table.insert(test.current_group, { name=name, result=result, message=message, data=data })
4848
end
4949

50-
function test.autotest(name, format, buffer, data, output)
50+
-- automatically test packing and unpacking. obuf and oval are optional,
51+
-- and default to the same values as ibuf and ival.
52+
-- This tests the following operations:
53+
-- unpack(ibuf) == oval
54+
-- pack(unpack(ibuf)) == obuf
55+
-- pack(ival) == obuf
56+
-- unpack(pack(ival)) == oval
57+
function test.autotest(name, format, ibuf, ival, obuf, oval)
5158
local eq = test.eq
5259
local record = test.record
5360

54-
output = output or buffer
55-
56-
if type(data) ~= "table" then data = {data} end
61+
obuf = obuf or ibuf
62+
oval = oval or ival
63+
64+
assert(type(obuf) == type(ibuf))
65+
assert(type(obuf) == "string")
66+
67+
if type(ival) ~= "table" then ival = {ival} end
68+
if type(oval) ~= "table" then oval = {oval} end
5769

5870
local function tester()
59-
local unpacked = vstruct.unpack(format, buffer)
60-
local packed = vstruct.pack(format, unpacked)
61-
62-
record(name.." (U )", eq(unpacked, data), unpack(unpacked))
63-
record(name.." (UP)", eq(packed, output), test.od(packed))
71+
local f = vstruct.compile(format)
6472

65-
local packed = vstruct.pack(format, data)
66-
local unpacked = vstruct.unpack(format, packed)
67-
68-
record(name.." (P )", eq(packed, output), test.od(packed))
69-
record(name.." (PU)", eq(unpacked, data), unpack(unpacked))
73+
local U = f.unpack(ibuf)
74+
local UP = f.pack(U)
75+
local P = f.pack(ival)
76+
local PU = f.unpack(P)
77+
78+
record(name.." (U )", eq(U, oval), unpack(U))
79+
record(name.." (UP)", eq(UP, obuf), test.od(UP))
80+
81+
record(name.." (P )", eq(P, obuf), test.od(P))
82+
record(name.." (PU)", eq(PU, oval), unpack(PU))
7083
end
7184

7285
xpcall(tester, function(err)
73-
record(name.." !ERR", false, "Error executing test: "..err)
86+
record(name.." !ERR", false, debug.traceback("Error executing test: "..err))
7487
end)
7588
end
7689

0 commit comments

Comments
 (0)