aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-02-09 02:49:44 -0500
committerEvan Huus <eapache@gmail.com>2014-02-09 16:04:36 +0000
commit4e8832bb0a59aa40b85bac72a54ba00784e4229d (patch)
tree5b413088379cda4869b1e39532e60a4f9ef3d298 /test/lua
parent0353c9a8e357f688cd8d9523ab10a95c7bc8473d (diff)
Fix bug 9736 'Lua: new Int64/UInt64 functions don't prevent division by zero'
OK, in all fairness this does a bit more than just fix that bug. It also adds a 'Int64()' and 'UInt64()' __call metamethods. I generally dislike using __call metamethods, because they're often unintuitive (for example the wireshark Field and FielInfo use this in a bizarre fashion). But this happens to be a perfect use for it, and very natural, imho. Another change is to make the metatables of classes visible to scripts. There was never really a good reason not to make them visible, and they have to be visible to do things like use __call on a plain table... not to mention I need them to be visible to run test scripts verifying evrything is kosher. I also updated the test suite to test for the div/mod by zero. Change-Id: Ia4c594c8f59d4e799090716bd032ba2815df032f Reviewed-on: https://code.wireshark.org/review/149 Reviewed-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'test/lua')
-rwxr-xr-xtest/lua/int64.lua66
1 files changed, 51 insertions, 15 deletions
diff --git a/test/lua/int64.lua b/test/lua/int64.lua
index c0c8f9dd45..8582485b31 100755
--- a/test/lua/int64.lua
+++ b/test/lua/int64.lua
@@ -93,13 +93,14 @@ for i,t in ipairs(testtbl) do
test("new1",checkeq(my64a,val))
test("new2",checkeq(my64b,val))
test("new3",checkeq(my64a,obj.new(my64b)))
+ test("new3b",checkeq(my64a,obj(my64b)))
test("new4",checkeq(valc,my64c))
test("new5",checkeq(0,my64z))
test("new6",obj.new(0,1):tonumber() == (2^32))
if t.name == "Int64" then
- test("new7",obj.new(-1):tonumber() == -1)
+ test("new7",obj(-1):tonumber() == -1)
test("new8",obj.new(0,-1):tonumber() == -4294967296)
- test("new9",obj.new(obj.new(-1)):tonumber() == -1)
+ test("new9",obj(obj.new(-1)):tonumber() == -1)
end
test("tonumber1",val == my64a:tonumber())
@@ -126,7 +127,7 @@ for i,t in ipairs(testtbl) do
test("ge2", my64c >= my64z)
test("neq1",not(my64a ~= my64b))
- test("neq2",my64a ~= obj.new(0))
+ test("neq2",my64a ~= obj(0))
test("neq2",my64a ~= my64c)
test("gt1",my64a > my64z)
@@ -166,7 +167,7 @@ for i,t in ipairs(testtbl) do
test("pow3",checkeq(my64a ^ obj.new(3), val ^ 3))
test("pow4",checkeq(my64c ^ 1, valc ^ 1))
- test("mul1",checkeq(my64a * obj.new(1), my64b))
+ test("mul1",checkeq(my64a * obj(1), my64b))
test("mul2",checkeq(my64a * my64b, my64b * my64a))
test("mul3",checkeq(my64a * 1, my64b))
test("mul4",checkeq(2 * my64c, 2 * valc))
@@ -268,23 +269,23 @@ for i,t in ipairs(testtbl) do
testing("bit operations")
- test("band1",checkeq(obj.new(1):band(1), 1))
- test("band2",checkeq(obj.new(1):band(0), 0))
- test("band3",checkeq(obj.new(4294967295,100):band(4294967295), 4294967295))
- test("band4",obj.new(4294967295,100):band(obj.new(0,100),obj.new(0,100),obj.new(0,100)) == obj.new(0,100))
- test("band5",checkeq(obj.new(4294967295,100):band(obj.new(0,100),obj.new(0)), 0))
+ test("band1",checkeq(obj(1):band(1), 1))
+ test("band2",checkeq(obj(1):band(0), 0))
+ test("band3",checkeq(obj(4294967295,100):band(4294967295), 4294967295))
+ test("band4",obj.new(4294967295,100):band(obj(0,100),obj(0,100),obj(0,100)) == obj(0,100))
+ test("band5",checkeq(obj.new(4294967295,100):band(obj.new(0,100),obj(0)), 0))
- test("bor1",checkeq(obj.new(1):bor(1), 1))
- test("bor2",checkeq(obj.new(1):bor(0), 1))
- test("bor3",checkeq(obj.new(0):bor(0), 0))
+ test("bor1",checkeq(obj(1):bor(1), 1))
+ test("bor2",checkeq(obj(1):bor(0), 1))
+ test("bor3",checkeq(obj(0):bor(0), 0))
test("bor4",obj.new(0,100):bor(4294967295) == obj.new(4294967295,100))
- test("bor5",obj.new(1):bor(obj.new(2),obj.new(4),obj.new(8),16,32,64,128) == obj.new(255))
+ test("bor5",obj.new(1):bor(obj(2),obj.new(4),obj(8),16,32,64,128) == obj(255))
test("bxor1",checkeq(obj.new(1):bxor(1), 0))
test("bxor2",checkeq(obj.new(1):bxor(0), 1))
test("bxor3",checkeq(obj.new(0):bxor(0), 0))
- test("bxor4",obj.new(4294967295,100):bxor(obj.new(0,100)) == obj.new(4294967295))
- test("bxor5",obj.new(1):bxor(obj.new(2),obj.new(4),obj.new(8),16,32,64,128) == obj.new(255))
+ test("bxor4",obj.new(4294967295,100):bxor(obj(0,100)) == obj.new(4294967295))
+ test("bxor5",obj.new(1):bxor(obj(2),obj(4),obj(8),16,32,64,128) == obj(255))
test("bnot1",checkeq(obj.new(4294967295,4294967295):bnot(), 0))
test("bnot2",obj.new(0):bnot() == obj.new(4294967295,4294967295))
@@ -336,6 +337,41 @@ test("min3",z==Int64.min())
test("minmax",Int64.min()== - Int64.max() - 1)
+testing("error conditions")
+
+local function divtest(f,s)
+ local r = (f / s)
+ if r == 5 then
+ io.stdout:write("ok...")
+ else
+ error("test failed!")
+ end
+end
+
+local function modtest(f,s)
+ local r = (f % s)
+ if r == 5 then
+ io.stdout:write("ok...")
+ else
+ error("test failed!")
+ end
+end
+
+test("error1", pcall(divtest, 10, 2)) -- not an error, but checking the div function works above
+test("error2", not pcall(divtest, Int64(10), 0))
+test("error3", not pcall(divtest, Int64(10), Int64(0)))
+test("error4", not pcall(divtest, Int64(10), UInt64(0)))
+test("error5", not pcall(divtest, UInt64(10), 0))
+test("error6", not pcall(divtest, UInt64(10), Int64(0)))
+test("error7", not pcall(divtest, UInt64(10), UInt64(0)))
+test("error8", pcall(modtest, 17, 6)) -- not an error, but checking the mod function works above
+test("error9", not pcall(modtest, Int64(10), 0))
+test("error10", not pcall(modtest, Int64(10), Int64(0)))
+test("error11", not pcall(modtest, Int64(10), UInt64(0)))
+test("error12", not pcall(modtest, UInt64(10), 0))
+test("error13", not pcall(modtest, UInt64(10), Int64(0)))
+test("error14", not pcall(modtest, UInt64(10), UInt64(0)))
+
print("\n-----------------------------\n")
print("All tests passed!\n\n")