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 testlib
= require("testlib")
11 testlib
.init( { [OTHER
] = 23 } )
13 -- you can't compare (use the '==') userdata objects with numbers, so this function does it instead.
14 function checkeq(arg1
,arg2
)
17 elseif type(arg1
) == 'userdata' and arg1
.tonumber then
18 if type(arg2
) == 'userdata' and arg2
.tonumber then
19 return arg1
:tonumber() == arg2
:tonumber()
21 return arg1
:tonumber() == arg2
23 elseif type(arg2
) == 'userdata' and arg2
.tonumber then
24 return arg1
== arg2
:tonumber()
30 -----------------------------
32 testlib
.testing("Int64/UInt64 library")
35 { ["type"]=Int64
, ["name"]="Int64" } ,
36 { ["type"]=UInt64
, ["name"]="UInt64" },
39 for i
,t
in ipairs(testtbl
) do
40 testlib
.init( { [t
.name
] = 125+(t
.name
== "Int64" and 3 or 0) } )
42 testlib
.testing(t
.name
, "class")
45 for name
, val
in pairs(obj
) do
46 print("\t"..name
.." = "..type(val
))
49 testlib
.test(t
.name
,"class1",type(obj
) == 'table')
50 testlib
.test(t
.name
,"class2",type(obj
.new
) == 'function')
51 testlib
.test(t
.name
,"class3",type(obj
.max) == 'function')
52 testlib
.test(t
.name
,"class4",type(obj
.min) == 'function')
53 testlib
.test(t
.name
,"class5",type(obj
.tonumber) == 'function')
54 testlib
.test(t
.name
,"class6",type(obj
.fromhex
) == 'function')
55 testlib
.test(t
.name
,"class7",type(obj
.tohex
) == 'function')
56 testlib
.test(t
.name
,"class8",type(obj
.higher
) == 'function')
57 testlib
.test(t
.name
,"class9",type(obj
.lower
) == 'function')
60 testlib
.testing(t
.name
, "new, tonumber, tostring")
62 local my64a
= obj
.new(val
)
63 local my64b
= obj
.new(tostring(val
))
64 local zero
= obj
.new(0)
65 -- remember in Lua it's a double, so only precise up to 9,007,199,254,740,992
66 local my64c
= obj
.new(val
,100)
67 local valc
= (100 * 4294967296) + val
68 print(tostring(my64c
))
69 local my64z
= obj
.new(0,0)
70 local my64d
= obj
.new(0,100)
71 local vald
= (100 * 4294967296)
73 testlib
.test(t
.name
,"new1",checkeq(my64a
,val
))
74 testlib
.test(t
.name
,"new2",checkeq(my64b
,val
))
75 testlib
.test(t
.name
,"new3",checkeq(my64a
,obj
.new(my64b
)))
76 testlib
.test(t
.name
,"new3b",checkeq(my64a
,obj(my64b
)))
77 testlib
.test(t
.name
,"new4",checkeq(valc
,my64c
))
78 testlib
.test(t
.name
,"new5",checkeq(0,my64z
))
79 testlib
.test(t
.name
,"new6",obj
.new(0,1):tonumber() == (2^
32))
80 if t
.name
== "Int64" then
81 testlib
.test(t
.name
,"new7",obj(-1):tonumber() == -1)
82 testlib
.test(t
.name
,"new8",obj
.new(0,-1):tonumber() == -4294967296)
83 testlib
.test(t
.name
,"new9",obj(obj
.new(-1)):tonumber() == -1)
86 testlib
.test(t
.name
,"tonumber1",val
== my64a
:tonumber())
87 testlib
.test(t
.name
,"tonumber2",valc
== my64c
:tonumber())
88 testlib
.test(t
.name
,"tonumber3",vald
== my64d
:tonumber())
89 testlib
.test(t
.name
,"tonumber4",0 == my64z
:tonumber())
91 testlib
.test(t
.name
,"tostring1", tostring(my64a
)==tostring(val
))
92 testlib
.test(t
.name
,"tostring2",tostring(my64b
)==tostring(val
))
93 testlib
.test(t
.name
,"tostring3",tostring(my64c
)==tostring(valc
))
94 testlib
.test(t
.name
,"tostring4",tostring(my64d
)==tostring(vald
))
97 testlib
.testing(t
.name
, "compare ops")
99 testlib
.test(t
.name
,"eq", my64a
== my64b
)
101 testlib
.test(t
.name
,"le1", my64a
<= my64b
)
102 testlib
.test(t
.name
,"le2", my64a
<= my64c
)
103 testlib
.test(t
.name
,"le3", my64z
<= my64c
)
105 testlib
.test(t
.name
,"ge1", my64a
>= my64b
)
106 testlib
.test(t
.name
,"ge2", my64c
>= my64b
)
107 testlib
.test(t
.name
,"ge2", my64c
>= my64z
)
109 testlib
.test(t
.name
,"neq1",not(my64a
~= my64b
))
110 testlib
.test(t
.name
,"neq2",my64a
~= obj(0))
111 testlib
.test(t
.name
,"neq2",my64a
~= my64c
)
113 testlib
.test(t
.name
,"gt1",my64a
> my64z
)
114 testlib
.test(t
.name
,"gt2",my64c
> my64a
)
116 testlib
.test(t
.name
,"lt1",not(my64a
< my64b
))
117 testlib
.test(t
.name
,"lt2",my64a
< my64c
)
120 testlib
.testing(t
.name
, "math ops")
122 testlib
.test(t
.name
,"add1",checkeq(my64a
+ my64b
, val
+ val
))
123 testlib
.test(t
.name
,"add2",my64a
+ my64z
== my64b
)
124 testlib
.test(t
.name
,"add3",my64a
+ my64b
== my64b
+ my64a
)
125 testlib
.test(t
.name
,"add4",my64d
+ my64a
== my64c
)
126 testlib
.test(t
.name
,"add5",checkeq(my64a
+ vald
, valc
))
127 testlib
.test(t
.name
,"add6",checkeq(vald
+ my64a
, valc
))
129 testlib
.test(t
.name
,"sub1",checkeq(my64a
- my64b
, 0))
130 testlib
.test(t
.name
,"sub2",my64a
- my64b
== my64z
)
131 testlib
.test(t
.name
,"sub3",my64a
- my64b
== my64b
- my64a
)
132 testlib
.test(t
.name
,"sub4",my64c
- my64a
== my64d
)
133 testlib
.test(t
.name
,"sub5",checkeq(my64a
- val
, 0))
135 testlib
.test(t
.name
,"mod1",checkeq(my64a
% my64b
, 0))
136 testlib
.test(t
.name
,"mod2",checkeq(my64c
% my64b
, valc
% val
))
137 testlib
.test(t
.name
,"mod3",checkeq(my64c
% val
, valc
% val
))
138 testlib
.test(t
.name
,"mod4",checkeq(val
% my64c
, val
% valc
))
140 testlib
.test(t
.name
,"div1",checkeq(my64a
/ my64b
, 1))
141 testlib
.test(t
.name
,"div2",checkeq(my64a
/ val
, 1))
142 testlib
.test(t
.name
,"div3",checkeq(val
/ my64a
, 1))
143 testlib
.test(t
.name
,"div4",my64c
/ my64d
== obj
.new(1))
145 testlib
.test(t
.name
,"pow1",checkeq(my64a ^
1, val
))
146 testlib
.test(t
.name
,"pow2",checkeq(my64a ^ obj
.new(2), val ^
2))
147 testlib
.test(t
.name
,"pow3",checkeq(my64a ^ obj
.new(3), val ^
3))
148 testlib
.test(t
.name
,"pow4",checkeq(my64c ^
1, valc ^
1))
150 testlib
.test(t
.name
,"mul1",checkeq(my64a
* obj(1), my64b
))
151 testlib
.test(t
.name
,"mul2",checkeq(my64a
* my64b
, my64b
* my64a
))
152 testlib
.test(t
.name
,"mul3",checkeq(my64a
* 1, my64b
))
153 testlib
.test(t
.name
,"mul4",checkeq(2 * my64c
, 2 * valc
))
155 if t
.name
== "Int64" then
156 -- unary minus on UInt64 is illogical, but oh well
157 testlib
.test(t
.name
,"unm1",checkeq(-my64a
,-val
))
158 testlib
.test(t
.name
,"unm2",checkeq(string.sub(tostring(-my64a
),1,1), "-"))
159 testlib
.test(t
.name
,"unm3",checkeq(-my64c
,-valc
))
161 testlib
.test(t
.name
,"unm1",checkeq(-my64a
,val
))
162 testlib
.test(t
.name
,"unm2",checkeq(string.sub(tostring(-my64a
),1,1), "1"))
163 testlib
.test(t
.name
,"unm3",checkeq(-my64c
,valc
))
165 testlib
.test(t
.name
,"unm4",checkeq(-my64z
,0))
167 testlib
.testing(t
.name
, "methods")
169 testlib
.test(t
.name
,"higher1",my64a
:higher() == 0)
170 testlib
.test(t
.name
,"higher2",my64c
:higher() == 100)
172 testlib
.test(t
.name
,"lower1",my64a
:lower() == val
)
173 testlib
.test(t
.name
,"lower2",my64c
:lower() == val
)
174 testlib
.test(t
.name
,"lower3",my64d
:lower() == 0)
176 local vale1
= 3735928559 -- yields hex of deadbeef
177 local vale2
= 5045997 -- yields 4cfeed
178 local my64e
= obj
.new(vale1
, vale2
)
179 testlib
.test(t
.name
,"fromhex1",obj
.fromhex("0000000000003039") == my64a
);
180 testlib
.test(t
.name
,"fromhex2",obj
.fromhex("3039") == my64a
);
181 testlib
.test(t
.name
,"fromhex3",obj
.fromhex("0000006400003039") == my64c
);
182 testlib
.test(t
.name
,"fromhex4",obj
.fromhex("0000000000000000") == my64z
);
183 testlib
.test(t
.name
,"fromhex5",obj
.fromhex("004cfeeddeadbeef") == my64e
);
184 testlib
.test(t
.name
,"fromhex6",obj
.fromhex("4cFEEDDEADBEEF") == my64e
);
186 testlib
.test(t
.name
,"tohex1",my64a
:tohex() == "0000000000003039")
187 testlib
.test(t
.name
,"tohex2",my64c
:tohex(16) == "0000006400003039")
188 testlib
.test(t
.name
,"tohex3",my64z
:tohex() == "0000000000000000")
189 testlib
.test(t
.name
,"tohex4",my64e
:tohex() == "004cfeeddeadbeef")
190 testlib
.test(t
.name
,"tohex5",my64e
:tohex(8) == "deadbeef")
191 testlib
.test(t
.name
,"tohex6",my64e
:tohex(-8) == "DEADBEEF")
193 testlib
.test(t
.name
,"encode1",my64a
:encode(true) == "\57\48\00\00\00\00\00\00")
194 testlib
.test(t
.name
,"encode2",my64a
:encode(false) == "\00\00\00\00\00\00\48\57")
195 testlib
.test(t
.name
,"encode3",my64c
:encode(false) == "\00\00\00\100\00\00\48\57")
197 testlib
.test(t
.name
,"decode1",obj
.decode("\57\48\00\00\00\00\00\00", true) == my64a
)
198 testlib
.test(t
.name
,"decode2",obj
.decode("\00\00\00\00\00\00\48\57", false) == my64a
)
199 testlib
.test(t
.name
,"decode3",obj
.decode("\00\00\00\100\00\00\48\57", false) == my64c
)
202 local function testpower(b
)
203 testlib
.testing(t
.name
, "powers of "..b
)
216 testlib
.testing(t
.name
, "factorials")
233 [15]="1307674368000",
234 [16]="20922789888000",
235 [17]="355687428096000",
236 [18]="6402373705728000",
237 [19]="121645100408832000",
238 [20]="2432902008176640000",
246 print(i
,z
,f
,f
==obj
.tonumber(z
),tostring(z
)==F
[i
])
247 --print(i,int64.new(F[i]))
250 testlib
.testing(t
.name
, "bit operations")
252 testlib
.test(t
.name
,"band1",checkeq(obj(1):band(1), 1))
253 testlib
.test(t
.name
,"band2",checkeq(obj(1):band(0), 0))
254 testlib
.test(t
.name
,"band3",checkeq(obj(4294967295,100):band(4294967295), 4294967295))
255 testlib
.test(t
.name
,"band4",obj
.new(4294967295,100):band(obj(0,100),obj(0,100),obj(0,100)) == obj(0,100))
256 testlib
.test(t
.name
,"band5",checkeq(obj
.new(4294967295,100):band(obj
.new(0,100),obj(0)), 0))
258 testlib
.test(t
.name
,"bor1",checkeq(obj(1):bor(1), 1))
259 testlib
.test(t
.name
,"bor2",checkeq(obj(1):bor(0), 1))
260 testlib
.test(t
.name
,"bor3",checkeq(obj(0):bor(0), 0))
261 testlib
.test(t
.name
,"bor4",obj
.new(0,100):bor(4294967295) == obj
.new(4294967295,100))
262 testlib
.test(t
.name
,"bor5",obj
.new(1):bor(obj(2),obj
.new(4),obj(8),16,32,64,128) == obj(255))
264 testlib
.test(t
.name
,"bxor1",checkeq(obj
.new(1):bxor(1), 0))
265 testlib
.test(t
.name
,"bxor2",checkeq(obj
.new(1):bxor(0), 1))
266 testlib
.test(t
.name
,"bxor3",checkeq(obj
.new(0):bxor(0), 0))
267 testlib
.test(t
.name
,"bxor4",obj
.new(4294967295,100):bxor(obj(0,100)) == obj
.new(4294967295))
268 testlib
.test(t
.name
,"bxor5",obj
.new(1):bxor(obj(2),obj(4),obj(8),16,32,64,128) == obj(255))
270 testlib
.test(t
.name
,"bnot1",checkeq(obj
.new(4294967295,4294967295):bnot(), 0))
271 testlib
.test(t
.name
,"bnot2",obj
.new(0):bnot() == obj
.new(4294967295,4294967295))
272 testlib
.test(t
.name
,"bnot3",obj
.new(0xaaaaaaaa,0xaaaaaaaa):bnot() == obj
.new( 0x55555555, 0x55555555))
274 testlib
.test(t
.name
,"bsawp1",obj
.new( 0x01020304, 0x05060708 ):bswap() == obj
.new( 0x08070605, 0x04030201 ))
275 testlib
.test(t
.name
,"bsawp2",obj
.new( 0xFF020304, 0xFF060708 ):bswap() == obj
.new( 0x080706FF, 0x040302FF ))
277 testlib
.test(t
.name
,"lshift1",obj
.new( 0x01020304, 0x0506070F ):lshift(4) == obj
.new( 0x10203040, 0x506070f0 ))
278 testlib
.test(t
.name
,"lshift2",obj
.new( 0x0102030F, 0x05060708 ):lshift(63) == obj
.new( 0, 0x80000000 ))
279 if t
.name
== "Int64" then
280 testlib
.test(t
.name
,"lshift3",checkeq(obj
.new( 0x0102030F, 0x05060708 ):lshift(63), -9223372036854775808))
282 testlib
.test(t
.name
,"lshift3",obj
.new( 0x0102030F, 0x05060708 ):lshift(63) == obj
.new( 0, 0x80000000 ))
285 testlib
.test(t
.name
,"rshift1",obj
.new( 0x01020304, 0xF5060708 ):rshift(4) == obj
.new( 0x80102030, 0x0F506070 ))
286 testlib
.test(t
.name
,"rshift2",checkeq(obj
.new( 0x01020304, 0xF5060708 ):rshift(63), 1))
288 if t
.name
== "Int64" then
289 testlib
.test(t
.name
,"arshift1",obj
.new( 0x01020304, 0xF5060708 ):arshift(4) == obj
.new( 0x80102030, 0xFF506070 ))
290 testlib
.test(t
.name
,"arshift2",obj
.new( 0x01020304, 0xF5060708 ):arshift(63) == obj
.new( 0xFFFFFFFF, 0xFFFFFFFF ))
292 testlib
.test(t
.name
,"arshift1",obj
.new( 0x01020304, 0xF5060708 ):arshift(4) == obj
.new( 0x80102030, 0x0F506070 ))
293 testlib
.test(t
.name
,"arshift2",checkeq(obj
.new( 0x01020304, 0xF5060708 ):arshift(63),1))
295 testlib
.test(t
.name
,"arshift3",obj
.new( 0x01020304, 0x05060708 ):arshift(4) == obj
.new( 0x80102030, 0x00506070 ))
296 testlib
.test(t
.name
,"arshift4",checkeq(obj
.new( 0x01020304, 0x05060708 ):arshift(63), 0))
298 testlib
.test(t
.name
,"rol1",obj
.new( 0x01020304, 0xF5060708 ):rol(4) == obj
.new( 0x1020304F, 0x50607080 ))
299 testlib
.test(t
.name
,"rol2",obj
.new( 0x01020304, 0xF5060708 ):rol(32):rol(32) == obj
.new( 0x01020304, 0xF5060708 ))
301 testlib
.test(t
.name
,"ror1",obj
.new( 0x01020304, 0xF5060708 ):ror(4) == obj
.new( 0x80102030, 0x4F506070 ))
302 testlib
.test(t
.name
,"ror2",obj
.new( 0x01020304, 0xF5060708 ):ror(32):ror(32) == obj
.new( 0x01020304, 0xF5060708 ))
306 testlib
.testing("min and max values")
309 testlib
.test(OTHER
,"max1",tostring(Int64
.max()) == "9223372036854775807")
310 testlib
.test(OTHER
,"max2",Int64
.max() == Int64
.new(4294967295, 2147483647))
311 testlib
.test(OTHER
,"max3",z
==Int64
.max())
312 testlib
.test(OTHER
,"min1",tostring(Int64
.min()) == "-9223372036854775808")
313 testlib
.test(OTHER
,"min2",Int64
.min() == Int64
.new(0,2147483648))
316 testlib
.test(OTHER
,"min3",z
==Int64
.min())
318 testlib
.test(OTHER
,"minmax",Int64
.min()== - Int64
.max() - 1)
320 --Because of g_ascii_strtoll() usage without errno check, "invalid" strings are converted to 0
321 testlib
.testing("invalid string values")
322 testlib
.test(OTHER
,"invalid",Int64
.new("invalid")== Int64
.new(0,0))
323 testlib
.test(OTHER
,"invalid2",UInt64
.new("invalid")== UInt64
.new(0,0))
325 testlib
.testing("error conditions")
327 local function divtest(f
,s
)
330 io
.stdout
:write("ok...")
332 error("test failed!")
336 local function modtest(f
,s
)
339 io
.stdout
:write("ok...")
341 error("test failed!")
345 testlib
.test(OTHER
,"error1", pcall(divtest
, 10, 2)) -- not an error, but checking the div function works above
346 testlib
.test(OTHER
,"error2", not pcall(divtest
, Int64(10), 0))
347 testlib
.test(OTHER
,"error3", not pcall(divtest
, Int64(10), Int64(0)))
348 testlib
.test(OTHER
,"error4", not pcall(divtest
, Int64(10), UInt64(0)))
349 testlib
.test(OTHER
,"error5", not pcall(divtest
, UInt64(10), 0))
350 testlib
.test(OTHER
,"error6", not pcall(divtest
, UInt64(10), Int64(0)))
351 testlib
.test(OTHER
,"error7", not pcall(divtest
, UInt64(10), UInt64(0)))
352 testlib
.test(OTHER
,"error8", pcall(modtest
, 17, 6)) -- not an error, but checking the mod function works above
353 testlib
.test(OTHER
,"error9", not pcall(modtest
, Int64(10), 0))
354 testlib
.test(OTHER
,"error10", not pcall(modtest
, Int64(10), Int64(0)))
355 testlib
.test(OTHER
,"error11", not pcall(modtest
, Int64(10), UInt64(0)))
356 testlib
.test(OTHER
,"error12", not pcall(modtest
, UInt64(10), 0))
357 testlib
.test(OTHER
,"error13", not pcall(modtest
, UInt64(10), Int64(0)))
358 testlib
.test(OTHER
,"error14", not pcall(modtest
, UInt64(10), UInt64(0)))