2 -- This is a test script for tshark/wireshark.
3 -- This script runs inside tshark/wireshark, so to run it do:
4 -- wireshark -X lua_script:<path_to_testdir>/lua/int64.lua
5 -- tshark -r bogus.cap -X lua_script:<path_to_testdir>/lua/int64.lua
7 -- Tests Int64/UInt64 functions
9 local function testing(...)
10 print("---- Testing "..tostring(...).." ----")
13 local function test(name, ...)
14 io.stdout:write("test "..name.."...")
16 io.stdout:write("passed\n")
18 io.stdout:write("failed!\n")
19 error(name.." test failed!")
23 -- you can't compare (use the '==') userdata objects with numbers, so this function does it instead.
24 function checkeq(arg1,arg2)
27 elseif type(arg1) == 'userdata' and arg1.tonumber then
28 if type(arg2) == 'userdata' and arg2.tonumber then
29 return arg1:tonumber() == arg2:tonumber()
31 return arg1:tonumber() == arg2
33 elseif type(arg2) == 'userdata' and arg2.tonumber then
34 return arg1 == arg2:tonumber()
40 -----------------------------
42 testing("Int64/UInt64 library")
44 local testtbl = { { ["type"]=Int64, ["name"]="Int64" } , { ["type"]=UInt64, ["name"]="UInt64" } }
46 for i,t in ipairs(testtbl) do
48 local function testing(...)
49 print("---- Testing "..t.name..": "..tostring(...).." ----")
52 local function test(name, ...)
53 io.stdout:write("test "..t.name.."-"..name.."...")
55 io.stdout:write("passed\n")
57 io.stdout:write("failed!\n")
58 error(name.." test failed!")
65 for name, val in pairs(obj) do
66 print("\t"..name.." = "..type(val))
69 test("class1",type(obj) == 'table')
70 test("class2",type(obj.new) == 'function')
71 test("class3",type(obj.max) == 'function')
72 test("class4",type(obj.min) == 'function')
73 test("class5",type(obj.tonumber) == 'function')
74 test("class6",type(obj.fromhex) == 'function')
75 test("class7",type(obj.tohex) == 'function')
76 test("class8",type(obj.higher) == 'function')
77 test("class9",type(obj.lower) == 'function')
80 testing("new, tonumber, tostring")
82 local my64a = obj.new(val)
83 local my64b = obj.new(tostring(val))
84 local zero = obj.new(0)
85 -- remember in Lua it's a double, so only precise up to 9,007,199,254,740,992
86 local my64c = obj.new(val,100)
87 local valc = (100 * 4294967296) + val
88 print(tostring(my64c))
89 local my64z = obj.new(0,0)
90 local my64d = obj.new(0,100)
91 local vald = (100 * 4294967296)
93 test("new1",checkeq(my64a,val))
94 test("new2",checkeq(my64b,val))
95 test("new3",checkeq(my64a,obj.new(my64b)))
96 test("new3b",checkeq(my64a,obj(my64b)))
97 test("new4",checkeq(valc,my64c))
98 test("new5",checkeq(0,my64z))
99 test("new6",obj.new(0,1):tonumber() == (2^32))
100 if t.name == "Int64" then
101 test("new7",obj(-1):tonumber() == -1)
102 test("new8",obj.new(0,-1):tonumber() == -4294967296)
103 test("new9",obj(obj.new(-1)):tonumber() == -1)
106 test("tonumber1",val == my64a:tonumber())
107 test("tonumber2",valc == my64c:tonumber())
108 test("tonumber3",vald == my64d:tonumber())
109 test("tonumber4",0 == my64z:tonumber())
111 test("tostring1", tostring(my64a)==tostring(val))
112 test("tostring2",tostring(my64b)==tostring(val))
113 test("tostring3",tostring(my64c)==tostring(valc))
114 test("tostring4",tostring(my64d)==tostring(vald))
117 testing("compare ops")
119 test("eq", my64a == my64b)
121 test("le1", my64a <= my64b)
122 test("le2", my64a <= my64c)
123 test("le3", my64z <= my64c)
125 test("ge1", my64a >= my64b)
126 test("ge2", my64c >= my64b)
127 test("ge2", my64c >= my64z)
129 test("neq1",not(my64a ~= my64b))
130 test("neq2",my64a ~= obj(0))
131 test("neq2",my64a ~= my64c)
133 test("gt1",my64a > my64z)
134 test("gt2",my64c > my64a)
136 test("lt1",not(my64a < my64b))
137 test("lt2",my64a < my64c)
142 test("add1",checkeq(my64a + my64b, val + val))
143 test("add2",my64a + my64z == my64b)
144 test("add3",my64a + my64b == my64b + my64a)
145 test("add4",my64d + my64a == my64c)
146 test("add5",checkeq(my64a + vald, valc))
147 test("add6",checkeq(vald + my64a, valc))
149 test("sub1",checkeq(my64a - my64b, 0))
150 test("sub2",my64a - my64b == my64z)
151 test("sub3",my64a - my64b == my64b - my64a)
152 test("sub4",my64c - my64a == my64d)
153 test("sub5",checkeq(my64a - val, 0))
155 test("mod1",checkeq(my64a % my64b, 0))
156 test("mod2",checkeq(my64c % my64b, valc % val))
157 test("mod3",checkeq(my64c % val, valc % val))
158 test("mod4",checkeq(val % my64c, val % valc))
160 test("div1",checkeq(my64a / my64b, 1))
161 test("div2",checkeq(my64a / val, 1))
162 test("div3",checkeq(val / my64a, 1))
163 test("div4",my64c / my64d == obj.new(1))
165 test("pow1",checkeq(my64a ^ 1, val))
166 test("pow2",checkeq(my64a ^ obj.new(2), val ^ 2))
167 test("pow3",checkeq(my64a ^ obj.new(3), val ^ 3))
168 test("pow4",checkeq(my64c ^ 1, valc ^ 1))
170 test("mul1",checkeq(my64a * obj(1), my64b))
171 test("mul2",checkeq(my64a * my64b, my64b * my64a))
172 test("mul3",checkeq(my64a * 1, my64b))
173 test("mul4",checkeq(2 * my64c, 2 * valc))
175 if t.name == "Int64" then
176 -- unary minus on UInt64 is illogical, but oh well
177 test("unm1",checkeq(-my64a,-val))
178 test("unm2",checkeq(string.sub(tostring(-my64a),1,1), "-"))
179 test("unm3",checkeq(-my64c,-valc))
181 test("unm1",checkeq(-my64a,val))
182 test("unm2",checkeq(string.sub(tostring(-my64a),1,1), "1"))
183 test("unm3",checkeq(-my64c,valc))
185 test("unm4",checkeq(-my64z,0))
189 test("higher1",my64a:higher() == 0)
190 test("higher2",my64c:higher() == 100)
192 test("lower1",my64a:lower() == val)
193 test("lower2",my64c:lower() == val)
194 test("lower3",my64d:lower() == 0)
196 local vale1 = 3735928559 -- yields hex of deadbeef
197 local vale2 = 5045997 -- yields 4cfeed
198 local my64e = obj.new(vale1, vale2)
199 test("fromhex1",obj.fromhex("0000000000003039") == my64a);
200 test("fromhex2",obj.fromhex("3039") == my64a);
201 test("fromhex3",obj.fromhex("0000006400003039") == my64c);
202 test("fromhex4",obj.fromhex("0000000000000000") == my64z);
203 test("fromhex5",obj.fromhex("004cfeeddeadbeef") == my64e);
204 test("fromhex6",obj.fromhex("4cFEEDDEADBEEF") == my64e);
206 test("tohex1",my64a:tohex() == "0000000000003039")
207 test("tohex2",my64c:tohex(16) == "0000006400003039")
208 test("tohex3",my64z:tohex() == "0000000000000000")
209 test("tohex4",my64e:tohex() == "004cfeeddeadbeef")
210 test("tohex5",my64e:tohex(8) == "deadbeef")
211 test("tohex6",my64e:tohex(-8) == "DEADBEEF")
213 test("encode1",my64a:encode(true) == "\57\48\00\00\00\00\00\00")
214 test("encode2",my64a:encode(false) == "\00\00\00\00\00\00\48\57")
215 test("encode3",my64c:encode(false) == "\00\00\00\100\00\00\48\57")
217 test("decode1",obj.decode("\57\48\00\00\00\00\00\00", true) == my64a)
218 test("decode2",obj.decode("\00\00\00\00\00\00\48\57", false) == my64a)
219 test("decode3",obj.decode("\00\00\00\100\00\00\48\57", false) == my64c)
222 local function testpower(b)
223 testing("powers of "..b)
253 [15]="1307674368000",
254 [16]="20922789888000",
255 [17]="355687428096000",
256 [18]="6402373705728000",
257 [19]="121645100408832000",
258 [20]="2432902008176640000",
266 print(i,z,f,f==obj.tonumber(z),tostring(z)==F[i])
267 --print(i,int64.new(F[i]))
270 testing("bit operations")
272 test("band1",checkeq(obj(1):band(1), 1))
273 test("band2",checkeq(obj(1):band(0), 0))
274 test("band3",checkeq(obj(4294967295,100):band(4294967295), 4294967295))
275 test("band4",obj.new(4294967295,100):band(obj(0,100),obj(0,100),obj(0,100)) == obj(0,100))
276 test("band5",checkeq(obj.new(4294967295,100):band(obj.new(0,100),obj(0)), 0))
278 test("bor1",checkeq(obj(1):bor(1), 1))
279 test("bor2",checkeq(obj(1):bor(0), 1))
280 test("bor3",checkeq(obj(0):bor(0), 0))
281 test("bor4",obj.new(0,100):bor(4294967295) == obj.new(4294967295,100))
282 test("bor5",obj.new(1):bor(obj(2),obj.new(4),obj(8),16,32,64,128) == obj(255))
284 test("bxor1",checkeq(obj.new(1):bxor(1), 0))
285 test("bxor2",checkeq(obj.new(1):bxor(0), 1))
286 test("bxor3",checkeq(obj.new(0):bxor(0), 0))
287 test("bxor4",obj.new(4294967295,100):bxor(obj(0,100)) == obj.new(4294967295))
288 test("bxor5",obj.new(1):bxor(obj(2),obj(4),obj(8),16,32,64,128) == obj(255))
290 test("bnot1",checkeq(obj.new(4294967295,4294967295):bnot(), 0))
291 test("bnot2",obj.new(0):bnot() == obj.new(4294967295,4294967295))
292 test("bnot3",obj.new(0xaaaaaaaa,0xaaaaaaaa):bnot() == obj.new( 0x55555555, 0x55555555))
294 test("bsawp1",obj.new( 0x01020304, 0x05060708 ):bswap() == obj.new( 0x08070605, 0x04030201 ))
295 test("bsawp2",obj.new( 0xFF020304, 0xFF060708 ):bswap() == obj.new( 0x080706FF, 0x040302FF ))
297 test("lshift1",obj.new( 0x01020304, 0x0506070F ):lshift(4) == obj.new( 0x10203040, 0x506070f0 ))
298 test("lshift2",obj.new( 0x0102030F, 0x05060708 ):lshift(63) == obj.new( 0, 0x80000000 ))
299 if t.name == "Int64" then
300 test("lshift3",checkeq(obj.new( 0x0102030F, 0x05060708 ):lshift(63), -9223372036854775808))
302 test("lshift3",obj.new( 0x0102030F, 0x05060708 ):lshift(63) == obj.new( 0, 0x80000000 ))
305 test("rshift1",obj.new( 0x01020304, 0xF5060708 ):rshift(4) == obj.new( 0x80102030, 0x0F506070 ))
306 test("rshift2",checkeq(obj.new( 0x01020304, 0xF5060708 ):rshift(63), 1))
308 if t.name == "Int64" then
309 test("arshift1",obj.new( 0x01020304, 0xF5060708 ):arshift(4) == obj.new( 0x80102030, 0xFF506070 ))
310 test("arshift2",obj.new( 0x01020304, 0xF5060708 ):arshift(63) == obj.new( 0xFFFFFFFF, 0xFFFFFFFF ))
312 test("arshift1",obj.new( 0x01020304, 0xF5060708 ):arshift(4) == obj.new( 0x80102030, 0x0F506070 ))
313 test("arshift2",checkeq(obj.new( 0x01020304, 0xF5060708 ):arshift(63),1))
315 test("arshift3",obj.new( 0x01020304, 0x05060708 ):arshift(4) == obj.new( 0x80102030, 0x00506070 ))
316 test("arshift4",checkeq(obj.new( 0x01020304, 0x05060708 ):arshift(63), 0))
318 test("rol1",obj.new( 0x01020304, 0xF5060708 ):rol(4) == obj.new( 0x1020304F, 0x50607080 ))
319 test("rol2",obj.new( 0x01020304, 0xF5060708 ):rol(32):rol(32) == obj.new( 0x01020304, 0xF5060708 ))
321 test("ror1",obj.new( 0x01020304, 0xF5060708 ):ror(4) == obj.new( 0x80102030, 0x4F506070 ))
322 test("ror2",obj.new( 0x01020304, 0xF5060708 ):ror(32):ror(32) == obj.new( 0x01020304, 0xF5060708 ))
326 testing("min and max values")
329 test("max1",tostring(Int64.max()) == "9223372036854775807")
330 test("max2",Int64.max() == Int64.new(4294967295, 2147483647))
331 test("max3",z==Int64.max())
332 test("min1",tostring(Int64.min()) == "-9223372036854775808")
333 test("min2",Int64.min() == Int64.new(0,2147483648))
336 test("min3",z==Int64.min())
338 test("minmax",Int64.min()== - Int64.max() - 1)
340 testing("error conditions")
342 local function divtest(f,s)
345 io.stdout:write("ok...")
347 error("test failed!")
351 local function modtest(f,s)
354 io.stdout:write("ok...")
356 error("test failed!")
360 test("error1", pcall(divtest, 10, 2)) -- not an error, but checking the div function works above
361 test("error2", not pcall(divtest, Int64(10), 0))
362 test("error3", not pcall(divtest, Int64(10), Int64(0)))
363 test("error4", not pcall(divtest, Int64(10), UInt64(0)))
364 test("error5", not pcall(divtest, UInt64(10), 0))
365 test("error6", not pcall(divtest, UInt64(10), Int64(0)))
366 test("error7", not pcall(divtest, UInt64(10), UInt64(0)))
367 test("error8", pcall(modtest, 17, 6)) -- not an error, but checking the mod function works above
368 test("error9", not pcall(modtest, Int64(10), 0))
369 test("error10", not pcall(modtest, Int64(10), Int64(0)))
370 test("error11", not pcall(modtest, Int64(10), UInt64(0)))
371 test("error12", not pcall(modtest, UInt64(10), 0))
372 test("error13", not pcall(modtest, UInt64(10), Int64(0)))
373 test("error14", not pcall(modtest, UInt64(10), UInt64(0)))
375 print("\n-----------------------------\n")
377 print("All tests passed!\n\n")