PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lua/int64.lua

https://gitlab.com/crondaemon/wireshark-legacy
Lua | 377 lines | 299 code | 68 blank | 10 comment | 102 complexity | d1cb384f01ba1cf6770aa9591a0f4eda MD5 | raw file
  1. -- This is a test script for tshark/wireshark.
  2. -- This script runs inside tshark/wireshark, so to run it do:
  3. -- wireshark -X lua_script:<path_to_testdir>/lua/int64.lua
  4. -- tshark -r bogus.cap -X lua_script:<path_to_testdir>/lua/int64.lua
  5. -- Tests Int64/UInt64 functions
  6. local function testing(...)
  7. print("---- Testing "..tostring(...).." ----")
  8. end
  9. local function test(name, ...)
  10. io.stdout:write("test "..name.."...")
  11. if (...) == true then
  12. io.stdout:write("passed\n")
  13. else
  14. io.stdout:write("failed!\n")
  15. error(name.." test failed!")
  16. end
  17. end
  18. -- you can't compare (use the '==') userdata objects with numbers, so this function does it instead.
  19. function checkeq(arg1,arg2)
  20. if arg1 == arg2 then
  21. return true
  22. elseif type(arg1) == 'userdata' and arg1.tonumber then
  23. if type(arg2) == 'userdata' and arg2.tonumber then
  24. return arg1:tonumber() == arg2:tonumber()
  25. else
  26. return arg1:tonumber() == arg2
  27. end
  28. elseif type(arg2) == 'userdata' and arg2.tonumber then
  29. return arg1 == arg2:tonumber()
  30. else
  31. return false
  32. end
  33. end
  34. -----------------------------
  35. testing("Int64/UInt64 library")
  36. local testtbl = { { ["type"]=Int64, ["name"]="Int64" } , { ["type"]=UInt64, ["name"]="UInt64" } }
  37. for i,t in ipairs(testtbl) do
  38. local function testing(...)
  39. print("---- Testing "..t.name..": "..tostring(...).." ----")
  40. end
  41. local function test(name, ...)
  42. io.stdout:write("test "..t.name.."-"..name.."...")
  43. if (...) == true then
  44. io.stdout:write("passed\n")
  45. else
  46. io.stdout:write("failed!\n")
  47. error(name.." test failed!")
  48. end
  49. end
  50. testing("class")
  51. local obj = t.type
  52. for name, val in pairs(obj) do
  53. print("\t"..name.." = "..type(val))
  54. end
  55. test("class1",type(obj) == 'table')
  56. test("class2",type(obj.new) == 'function')
  57. test("class3",type(obj.max) == 'function')
  58. test("class4",type(obj.min) == 'function')
  59. test("class5",type(obj.tonumber) == 'function')
  60. test("class6",type(obj.fromhex) == 'function')
  61. test("class7",type(obj.tohex) == 'function')
  62. test("class8",type(obj.higher) == 'function')
  63. test("class9",type(obj.lower) == 'function')
  64. testing("new, tonumber, tostring")
  65. local val = 12345
  66. local my64a = obj.new(val)
  67. local my64b = obj.new(tostring(val))
  68. local zero = obj.new(0)
  69. -- remember in Lua it's a double, so only precise up to 9,007,199,254,740,992
  70. local my64c = obj.new(val,100)
  71. local valc = (100 * 4294967296) + val
  72. print(tostring(my64c))
  73. local my64z = obj.new(0,0)
  74. local my64d = obj.new(0,100)
  75. local vald = (100 * 4294967296)
  76. test("new1",checkeq(my64a,val))
  77. test("new2",checkeq(my64b,val))
  78. test("new3",checkeq(my64a,obj.new(my64b)))
  79. test("new3b",checkeq(my64a,obj(my64b)))
  80. test("new4",checkeq(valc,my64c))
  81. test("new5",checkeq(0,my64z))
  82. test("new6",obj.new(0,1):tonumber() == (2^32))
  83. if t.name == "Int64" then
  84. test("new7",obj(-1):tonumber() == -1)
  85. test("new8",obj.new(0,-1):tonumber() == -4294967296)
  86. test("new9",obj(obj.new(-1)):tonumber() == -1)
  87. end
  88. test("tonumber1",val == my64a:tonumber())
  89. test("tonumber2",valc == my64c:tonumber())
  90. test("tonumber3",vald == my64d:tonumber())
  91. test("tonumber4",0 == my64z:tonumber())
  92. test("tostring1", tostring(my64a)==tostring(val))
  93. test("tostring2",tostring(my64b)==tostring(val))
  94. test("tostring3",tostring(my64c)==tostring(valc))
  95. test("tostring4",tostring(my64d)==tostring(vald))
  96. testing("compare ops")
  97. test("eq", my64a == my64b)
  98. test("le1", my64a <= my64b)
  99. test("le2", my64a <= my64c)
  100. test("le3", my64z <= my64c)
  101. test("ge1", my64a >= my64b)
  102. test("ge2", my64c >= my64b)
  103. test("ge2", my64c >= my64z)
  104. test("neq1",not(my64a ~= my64b))
  105. test("neq2",my64a ~= obj(0))
  106. test("neq2",my64a ~= my64c)
  107. test("gt1",my64a > my64z)
  108. test("gt2",my64c > my64a)
  109. test("lt1",not(my64a < my64b))
  110. test("lt2",my64a < my64c)
  111. testing("math ops")
  112. test("add1",checkeq(my64a + my64b, val + val))
  113. test("add2",my64a + my64z == my64b)
  114. test("add3",my64a + my64b == my64b + my64a)
  115. test("add4",my64d + my64a == my64c)
  116. test("add5",checkeq(my64a + vald, valc))
  117. test("add6",checkeq(vald + my64a, valc))
  118. test("sub1",checkeq(my64a - my64b, 0))
  119. test("sub2",my64a - my64b == my64z)
  120. test("sub3",my64a - my64b == my64b - my64a)
  121. test("sub4",my64c - my64a == my64d)
  122. test("sub5",checkeq(my64a - val, 0))
  123. test("mod1",checkeq(my64a % my64b, 0))
  124. test("mod2",checkeq(my64c % my64b, valc % val))
  125. test("mod3",checkeq(my64c % val, valc % val))
  126. test("mod4",checkeq(val % my64c, val % valc))
  127. test("div1",checkeq(my64a / my64b, 1))
  128. test("div2",checkeq(my64a / val, 1))
  129. test("div3",checkeq(val / my64a, 1))
  130. test("div4",my64c / my64d == obj.new(1))
  131. test("pow1",checkeq(my64a ^ 1, val))
  132. test("pow2",checkeq(my64a ^ obj.new(2), val ^ 2))
  133. test("pow3",checkeq(my64a ^ obj.new(3), val ^ 3))
  134. test("pow4",checkeq(my64c ^ 1, valc ^ 1))
  135. test("mul1",checkeq(my64a * obj(1), my64b))
  136. test("mul2",checkeq(my64a * my64b, my64b * my64a))
  137. test("mul3",checkeq(my64a * 1, my64b))
  138. test("mul4",checkeq(2 * my64c, 2 * valc))
  139. if t.name == "Int64" then
  140. -- unary minus on UInt64 is illogical, but oh well
  141. test("unm1",checkeq(-my64a,-val))
  142. test("unm2",checkeq(string.sub(tostring(-my64a),1,1), "-"))
  143. test("unm3",checkeq(-my64c,-valc))
  144. else
  145. test("unm1",checkeq(-my64a,val))
  146. test("unm2",checkeq(string.sub(tostring(-my64a),1,1), "1"))
  147. test("unm3",checkeq(-my64c,valc))
  148. end
  149. test("unm4",checkeq(-my64z,0))
  150. testing("methods")
  151. test("higher1",my64a:higher() == 0)
  152. test("higher2",my64c:higher() == 100)
  153. test("lower1",my64a:lower() == val)
  154. test("lower2",my64c:lower() == val)
  155. test("lower3",my64d:lower() == 0)
  156. local vale1 = 3735928559 -- yields hex of deadbeef
  157. local vale2 = 5045997 -- yields 4cfeed
  158. local my64e = obj.new(vale1, vale2)
  159. test("fromhex1",obj.fromhex("0000000000003039") == my64a);
  160. test("fromhex2",obj.fromhex("3039") == my64a);
  161. test("fromhex3",obj.fromhex("0000006400003039") == my64c);
  162. test("fromhex4",obj.fromhex("0000000000000000") == my64z);
  163. test("fromhex5",obj.fromhex("004cfeeddeadbeef") == my64e);
  164. test("fromhex6",obj.fromhex("4cFEEDDEADBEEF") == my64e);
  165. test("tohex1",my64a:tohex() == "0000000000003039")
  166. test("tohex2",my64c:tohex(16) == "0000006400003039")
  167. test("tohex3",my64z:tohex() == "0000000000000000")
  168. test("tohex4",my64e:tohex() == "004cfeeddeadbeef")
  169. test("tohex5",my64e:tohex(8) == "deadbeef")
  170. test("tohex6",my64e:tohex(-8) == "DEADBEEF")
  171. test("encode1",my64a:encode(true) == "\57\48\00\00\00\00\00\00")
  172. test("encode2",my64a:encode(false) == "\00\00\00\00\00\00\48\57")
  173. test("encode3",my64c:encode(false) == "\00\00\00\100\00\00\48\57")
  174. test("decode1",obj.decode("\57\48\00\00\00\00\00\00", true) == my64a)
  175. test("decode2",obj.decode("\00\00\00\00\00\00\48\57", false) == my64a)
  176. test("decode3",obj.decode("\00\00\00\100\00\00\48\57", false) == my64c)
  177. local function testpower(b)
  178. testing("powers of "..b)
  179. b=obj.new(b)
  180. local z=obj.new(1)
  181. for i=0,100 do
  182. print(i,z,b^i)
  183. assert(z==b^i)
  184. z=b*z
  185. end
  186. end
  187. testpower(2)
  188. testpower(3)
  189. testing"factorials"
  190. F={
  191. [1]="1",
  192. [2]="2",
  193. [3]="6",
  194. [4]="24",
  195. [5]="120",
  196. [6]="720",
  197. [7]="5040",
  198. [8]="40320",
  199. [9]="362880",
  200. [10]="3628800",
  201. [11]="39916800",
  202. [12]="479001600",
  203. [13]="6227020800",
  204. [14]="87178291200",
  205. [15]="1307674368000",
  206. [16]="20922789888000",
  207. [17]="355687428096000",
  208. [18]="6402373705728000",
  209. [19]="121645100408832000",
  210. [20]="2432902008176640000",
  211. }
  212. z=obj.new(1)
  213. f=1
  214. for i=1,20 do
  215. z=z*i
  216. f=f*i
  217. s=obj.tonumber(z)
  218. print(i,z,f,f==obj.tonumber(z),tostring(z)==F[i])
  219. --print(i,int64.new(F[i]))
  220. end
  221. testing("bit operations")
  222. test("band1",checkeq(obj(1):band(1), 1))
  223. test("band2",checkeq(obj(1):band(0), 0))
  224. test("band3",checkeq(obj(4294967295,100):band(4294967295), 4294967295))
  225. test("band4",obj.new(4294967295,100):band(obj(0,100),obj(0,100),obj(0,100)) == obj(0,100))
  226. test("band5",checkeq(obj.new(4294967295,100):band(obj.new(0,100),obj(0)), 0))
  227. test("bor1",checkeq(obj(1):bor(1), 1))
  228. test("bor2",checkeq(obj(1):bor(0), 1))
  229. test("bor3",checkeq(obj(0):bor(0), 0))
  230. test("bor4",obj.new(0,100):bor(4294967295) == obj.new(4294967295,100))
  231. test("bor5",obj.new(1):bor(obj(2),obj.new(4),obj(8),16,32,64,128) == obj(255))
  232. test("bxor1",checkeq(obj.new(1):bxor(1), 0))
  233. test("bxor2",checkeq(obj.new(1):bxor(0), 1))
  234. test("bxor3",checkeq(obj.new(0):bxor(0), 0))
  235. test("bxor4",obj.new(4294967295,100):bxor(obj(0,100)) == obj.new(4294967295))
  236. test("bxor5",obj.new(1):bxor(obj(2),obj(4),obj(8),16,32,64,128) == obj(255))
  237. test("bnot1",checkeq(obj.new(4294967295,4294967295):bnot(), 0))
  238. test("bnot2",obj.new(0):bnot() == obj.new(4294967295,4294967295))
  239. test("bnot3",obj.new(0xaaaaaaaa,0xaaaaaaaa):bnot() == obj.new( 0x55555555, 0x55555555))
  240. test("bsawp1",obj.new( 0x01020304, 0x05060708 ):bswap() == obj.new( 0x08070605, 0x04030201 ))
  241. test("bsawp2",obj.new( 0xFF020304, 0xFF060708 ):bswap() == obj.new( 0x080706FF, 0x040302FF ))
  242. test("lshift1",obj.new( 0x01020304, 0x0506070F ):lshift(4) == obj.new( 0x10203040, 0x506070f0 ))
  243. test("lshift2",obj.new( 0x0102030F, 0x05060708 ):lshift(63) == obj.new( 0, 0x80000000 ))
  244. if t.name == "Int64" then
  245. test("lshift3",checkeq(obj.new( 0x0102030F, 0x05060708 ):lshift(63), -9223372036854775808))
  246. else
  247. test("lshift3",obj.new( 0x0102030F, 0x05060708 ):lshift(63) == obj.new( 0, 0x80000000 ))
  248. end
  249. test("rshift1",obj.new( 0x01020304, 0xF5060708 ):rshift(4) == obj.new( 0x80102030, 0x0F506070 ))
  250. test("rshift2",checkeq(obj.new( 0x01020304, 0xF5060708 ):rshift(63), 1))
  251. if t.name == "Int64" then
  252. test("arshift1",obj.new( 0x01020304, 0xF5060708 ):arshift(4) == obj.new( 0x80102030, 0xFF506070 ))
  253. test("arshift2",obj.new( 0x01020304, 0xF5060708 ):arshift(63) == obj.new( 0xFFFFFFFF, 0xFFFFFFFF ))
  254. else
  255. test("arshift1",obj.new( 0x01020304, 0xF5060708 ):arshift(4) == obj.new( 0x80102030, 0x0F506070 ))
  256. test("arshift2",checkeq(obj.new( 0x01020304, 0xF5060708 ):arshift(63),1))
  257. end
  258. test("arshift3",obj.new( 0x01020304, 0x05060708 ):arshift(4) == obj.new( 0x80102030, 0x00506070 ))
  259. test("arshift4",checkeq(obj.new( 0x01020304, 0x05060708 ):arshift(63), 0))
  260. test("rol1",obj.new( 0x01020304, 0xF5060708 ):rol(4) == obj.new( 0x1020304F, 0x50607080 ))
  261. test("rol2",obj.new( 0x01020304, 0xF5060708 ):rol(32):rol(32) == obj.new( 0x01020304, 0xF5060708 ))
  262. test("ror1",obj.new( 0x01020304, 0xF5060708 ):ror(4) == obj.new( 0x80102030, 0x4F506070 ))
  263. test("ror2",obj.new( 0x01020304, 0xF5060708 ):ror(32):ror(32) == obj.new( 0x01020304, 0xF5060708 ))
  264. end
  265. testing("min and max values")
  266. z=Int64.new(2)
  267. z=z^63-1
  268. test("max1",tostring(Int64.max()) == "9223372036854775807")
  269. test("max2",Int64.max() == Int64.new(4294967295, 2147483647))
  270. test("max3",z==Int64.max())
  271. test("min1",tostring(Int64.min()) == "-9223372036854775808")
  272. test("min2",Int64.min() == Int64.new(0,2147483648))
  273. z=-z
  274. z=z-1
  275. test("min3",z==Int64.min())
  276. test("minmax",Int64.min()== - Int64.max() - 1)
  277. testing("error conditions")
  278. local function divtest(f,s)
  279. local r = (f / s)
  280. if r == 5 then
  281. io.stdout:write("ok...")
  282. else
  283. error("test failed!")
  284. end
  285. end
  286. local function modtest(f,s)
  287. local r = (f % s)
  288. if r == 5 then
  289. io.stdout:write("ok...")
  290. else
  291. error("test failed!")
  292. end
  293. end
  294. test("error1", pcall(divtest, 10, 2)) -- not an error, but checking the div function works above
  295. test("error2", not pcall(divtest, Int64(10), 0))
  296. test("error3", not pcall(divtest, Int64(10), Int64(0)))
  297. test("error4", not pcall(divtest, Int64(10), UInt64(0)))
  298. test("error5", not pcall(divtest, UInt64(10), 0))
  299. test("error6", not pcall(divtest, UInt64(10), Int64(0)))
  300. test("error7", not pcall(divtest, UInt64(10), UInt64(0)))
  301. test("error8", pcall(modtest, 17, 6)) -- not an error, but checking the mod function works above
  302. test("error9", not pcall(modtest, Int64(10), 0))
  303. test("error10", not pcall(modtest, Int64(10), Int64(0)))
  304. test("error11", not pcall(modtest, Int64(10), UInt64(0)))
  305. test("error12", not pcall(modtest, UInt64(10), 0))
  306. test("error13", not pcall(modtest, UInt64(10), Int64(0)))
  307. test("error14", not pcall(modtest, UInt64(10), UInt64(0)))
  308. print("\n-----------------------------\n")
  309. print("All tests passed!\n\n")