Skip to content

Commit

Permalink
Modernise misc/meta_* tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
corsix committed Apr 9, 2016
1 parent e002a08 commit 6a7e342
Show file tree
Hide file tree
Showing 30 changed files with 670 additions and 627 deletions.
74 changes: 74 additions & 0 deletions test/lang/compare.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,77 @@ do --- string 1 255
assert(a >= b)
assert(b <= a)
end

do --- String comparisons:
local function str_cmp(a, b, lt, gt, le, ge)
assert(a<b == lt)
assert(a>b == gt)
assert(a<=b == le)
assert(a>=b == ge)
assert((not (a<b)) == (not lt))
assert((not (a>b)) == (not gt))
assert((not (a<=b)) == (not le))
assert((not (a>=b)) == (not ge))
end

local function str_lo(a, b)
str_cmp(a, b, true, false, true, false)
end

local function str_eq(a, b)
str_cmp(a, b, false, false, true, true)
end

local function str_hi(a, b)
str_cmp(a, b, false, true, false, true)
end

str_lo("a", "b")
str_eq("a", "a")
str_hi("b", "a")

str_lo("a", "aa")
str_hi("aa", "a")

str_lo("a", "a\0")
str_hi("a\0", "a")
end

do --- obj_eq/ne
local function obj_eq(a, b)
assert(a==b == true)
assert(a~=b == false)
end

local function obj_ne(a, b)
assert(a==b == false)
assert(a~=b == true)
end

obj_eq(nil, nil)
obj_ne(nil, false)
obj_ne(nil, true)

obj_ne(false, nil)
obj_eq(false, false)
obj_ne(false, true)

obj_ne(true, nil)
obj_ne(true, false)
obj_eq(true, true)

obj_eq(1, 1)
obj_ne(1, 2)
obj_ne(2, 1)

obj_eq("a", "a")
obj_ne("a", "b")
obj_ne("a", 1)
obj_ne(1, "a")

local t, t2 = {}, {}
obj_eq(t, t)
obj_ne(t, t2)
obj_ne(t, 1)
obj_ne(t, "")
end
1 change: 1 addition & 0 deletions test/lang/index
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ coroutine.lua
tail_recursion.lua
gc.lua
goto.lua +goto
meta
118 changes: 118 additions & 0 deletions test/lang/meta/arith.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
local function create(arith, v1, v2)
local meta = {
__add=function(a,b) return arith("add", a, b) end,
__sub=function(a,b) return arith("sub", a, b) end,
__mul=function(a,b) return arith("mul", a, b) end,
__div=function(a,b) return arith("div", a, b) end,
__mod=function(a,b) return arith("mod", a, b) end,
__pow=function(a,b) return arith("pow", a, b) end,
__unm=function(a,b) return arith("unm", a, b) end,
}
return setmetatable({v1}, meta), setmetatable({v2}, meta)
end

do --- op
local a, b = create(function(op,a,b) return op end)
assert(a+b == "add")
assert(a-b == "sub")
assert(a*b == "mul")
assert(a/b == "div")
assert(a%b == "mod")
assert(a^b == "pow")
assert(-a == "unm")
end

do --- lhs
local a, b = create(function(op,a,b) return a[1] end, "foo", 42)
assert(a+b == "foo")
assert(a-b == "foo")
assert(a*b == "foo")
assert(a/b == "foo")
assert(a%b == "foo")
assert(a^b == "foo")
assert(-a == "foo")
end

do --- rhs
local a, b = create(function(op,a,b) return b[1] end, 42, "foo")
assert(a+b == "foo")
assert(a-b == "foo")
assert(a*b == "foo")
assert(a/b == "foo")
assert(a%b == "foo")
assert(a^b == "foo")
assert(-a == 42)
end

do --- meta only lhs
local a, b = create(function(op,a,b) return a[1]+b end, 39), 3
assert(a+b == 42)
assert(a-b == 42)
assert(a*b == 42)
assert(a/b == 42)
assert(a%b == 42)
assert(a^b == 42)
end

do --- meta only rhs
local a, b = 39, create(function(op,a,b) return a+b[1] end, 3)
assert(a+b == 42)
assert(a-b == 42)
assert(a*b == 42)
assert(a/b == 42)
assert(a%b == 42)
assert(a^b == 42)
end

do --- defaults string, int
local a, b = "39", 3
assert(a+b == 42)
assert(a-b == 36)
assert(a*b == 117)
assert(a/b == 13)
assert(a%b == 0)
assert(a^b == 59319)
assert(-a == -39)
end

do --- defaults int, string
local a, b = 39, "3"
assert(a+b == 42)
assert(a-b == 36)
assert(a*b == 117)
assert(a/b == 13)
assert(a%b == 0)
assert(a^b == 59319)
assert(-a == -39)
end

do --- defaults string, string
local a, b = "39", "3"
assert(a+b == 42)
assert(a-b == 36)
assert(a*b == 117)
assert(a/b == 13)
assert(a%b == 0)
assert(a^b == 59319)
assert(-a == -39)
end

do --- defaults string, kint
local a = "39"
assert(a+3 == 42)
assert(a-3 == 36)
assert(a*3 == 117)
assert(a/3 == 13)
assert(a%3 == 0)
assert(a^3 == 59319)
end

do --- defaults kint, string
local b = "3"
assert(39+b == 42)
assert(39-b == 36)
assert(39*b == 117)
assert(39/b == 13)
assert(39%b == 0)
assert(39^b == 59319)
end
8 changes: 4 additions & 4 deletions test/misc/meta_arith_jit.lua → test/lang/meta/arith_jit.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

do
do --- assert rhs
local t = {}
local mt = {
__add = function(a, b) assert(b == t); return a+11 end,
Expand All @@ -20,7 +20,7 @@ do
do local x = 0; for i=1,100 do x = x + (-t) end; assert(x == 1700); end
end

do
do --- assert lhs
local t = {}
local mt = {
__add = function(a, b) assert(a == t); return b+11 end,
Expand All @@ -39,7 +39,7 @@ do
do local x = 0; for i=1,100 do x = t ^ x end; assert(x == 1600); end
end

do
do --- assert both sides
local t = {}
local mt = {
__add = function(a, b) assert(a == t and b == t); return 11 end,
Expand All @@ -58,7 +58,7 @@ do
do local x = 0; for i=1,100 do x = t ^ t end; assert(x == 16); end
end

do
do --- adjust no result to one result
local t = {}
local mt = { __add = function(a, b) end }
t = setmetatable(t, mt)
Expand Down
41 changes: 22 additions & 19 deletions test/misc/meta_call.lua → test/lang/meta/call.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ end

local meta = { __call = callmeta }

do
do --- table
local t = setmetatable({}, meta)
local o,a,b = t()
assert(o == t and a == nil and b == nil)
Expand All @@ -15,7 +15,7 @@ do
assert(o == t and a == "foo" and b == "bar")
end

do
do --- userdata +lua<5.2
local u = newproxy(true)
getmetatable(u).__call = callmeta

Expand All @@ -27,16 +27,18 @@ do
assert(o == u and a == "foo" and b == "bar")
end

debug.setmetatable(0, meta)
local o,a,b = (42)()
assert(o == 42 and a == nil and b == nil)
local o,a,b = (42)("foo")
assert(o == 42 and a == "foo" and b == nil)
local o,a,b = (42)("foo", "bar")
assert(o == 42 and a == "foo" and b == "bar")
debug.setmetatable(0, nil)
do --- number
debug.setmetatable(0, meta)
local o,a,b = (42)()
assert(o == 42 and a == nil and b == nil)
local o,a,b = (42)("foo")
assert(o == 42 and a == "foo" and b == nil)
local o,a,b = (42)("foo", "bar")
assert(o == 42 and a == "foo" and b == "bar")
debug.setmetatable(0, nil)
end

do
do --- table with changing metamethod
local tc = setmetatable({}, { __call = function(o,a,b) return o end})
local ta = setmetatable({}, { __add = tc})
local o,a = ta + ta
Expand All @@ -47,32 +49,33 @@ do
assert(o == ta and a == nil)
end

do
do --- jit table
local t = setmetatable({}, { __call = function(t, a) return 100-a end })
for i=1,100 do assert(t(i) == 100-i) end
end

do
do --- jit table rawget as metamethod
local t = setmetatable({}, { __call = rawget })
for i=1,100 do t[i] = 100-i end
for i=1,100 do assert(t(i) == 100-i) end
end

debug.setmetatable(0, { __call = function(n) return 100-n end })
for i=1,100 do assert((i)() == 100-i) end
debug.setmetatable(0, nil)
do --- jit number
debug.setmetatable(0, { __call = function(n) return 100-n end })
for i=1,100 do assert((i)() == 100-i) end
debug.setmetatable(0, nil)
end

do
do --- jit newindex pcall
local t = setmetatable({}, { __newindex = pcall, __call = rawset })
for i=1,100 do t[i] = 100-i end
for i=1,100 do assert(t[i] == 100-i) end
end

do
do --- jit index pcall
local t = setmetatable({}, {
__index = pcall, __newindex = rawset,
__call = function(t, i) t[i] = 100-i end,
})
for i=1,100 do assert(t[i] == true and rawget(t, i) == 100-i) end
end

19 changes: 9 additions & 10 deletions test/misc/meta_cat.lua → test/lang/meta/cat.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
local function create(cat, v1, v2)
local meta = { __concat = cat }
return setmetatable({v1}, meta), setmetatable({v2}, meta)
end

do
do --- default
local a, b, c = "foo", "bar", "baz"
assert(a..b == "foobar")
assert(a..b..c == "foobarbaz")
end

local function create(cat, v1, v2)
local meta = { __concat = cat }
return setmetatable({v1}, meta), setmetatable({v2}, meta)
end

do
do --- lhs
local a, b = create(function(a, b) return a end)
assert(a..b == a)
assert(b..a == b)
Expand All @@ -20,7 +19,7 @@ do
assert(a..b..b..b..b..b..b..b == a)
end

do
do --- rhs
local a, b = create(function(a, b) return b end)
assert(a..b == b)
assert(b..a == a)
Expand All @@ -30,7 +29,7 @@ do
assert(a..a..a..a..a..a..a..b == b)
end

do
do --- mixed types
local a, b = create(function(a, b)
return (type(a) == "string" and a or a[1])..
(type(b) == "string" and b or b[1])
Expand All @@ -45,7 +44,7 @@ do
assert(a..b..a..b..a.."x".."x".."x" == "ababaxxx")
end

do
do --- jit mixed types
local a, b = create(function(a, b)
if a ~= b then local x = gg end
return (type(a) == "string" and a or a[1])..
Expand Down
Loading

0 comments on commit 6a7e342

Please sign in to comment.