Skip to content

Commit

Permalink
Modernise more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
corsix committed Apr 9, 2016
1 parent 1bee68f commit bcd286b
Show file tree
Hide file tree
Showing 19 changed files with 309 additions and 300 deletions.
1 change: 1 addition & 0 deletions test/lang/constant/index
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
number.lua
table.lua
31 changes: 15 additions & 16 deletions test/misc/tnew_tdup.lua → test/lang/constant/table.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

do
local a = nil
local b = {}
local t = {[true] = a, [false] = b or 1}
assert(t[true] == nil)
assert(t[false] == b)
end

do
local b = {}
local t = {[true] = nil, [false] = b or 1}
assert(t[true] == nil)
assert(t[false] == b)
end


do --- tnew
local a = nil
local b = {}
local t = {[true] = a, [false] = b or 1}
assert(t[true] == nil)
assert(t[false] == b)
end

do --- tdup
local b = {}
local t = {[true] = nil, [false] = b or 1}
assert(t[true] == nil)
assert(t[false] == b)
end
1 change: 1 addition & 0 deletions test/lang/index
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ assignment.lua
compare.lua
constant
for.lua
length.lua
modulo.lua
concat.lua
self.lua
Expand Down
46 changes: 23 additions & 23 deletions test/misc/tlen_loop.lua → test/lang/length.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

do
local t = {}
for i=1,100 do t[#t+1] = i end
assert(#t == 100)
for i=1,100 do t[#t] = nil end
end

do
local t = {}
t[90] = 999
for i=1,100 do t[#t+1] = i end
assert(#t > 100 and t[#t] == 100)
end

do
local t = {}
for i=1,100 do t[i] = i end
t[10] = nil
for i=1,99 do t[#t] = nil end
assert(#t == 0)
end


do --- length increasing and decreasing in loop
local t = {}
for i=1,100 do t[#t+1] = i end
assert(#t == 100)
for i=1,100 do t[#t] = nil end
assert(#t == 0)
end

do --- length increasing in loop with existing element
local t = {}
t[90] = 999
for i=1,100 do t[#t+1] = i end
assert(#t > 100 and t[#t] == 100)
end

do --- length decreasing in loop with erased element
local t = {}
for i=1,100 do t[i] = i end
t[10] = nil
for i=1,99 do t[#t] = nil end
assert(#t == 0)
end
3 changes: 3 additions & 0 deletions test/lib/base/index
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ assert.lua
error.lua
getfenv.lua +lua<5.2
getsetmetatable.lua
ipairs.lua
next.lua
pairs.lua
select.lua
tonumber_tostring.lua
xpcall_jit.lua +compat5.2
41 changes: 41 additions & 0 deletions test/lib/base/ipairs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
do --- small integer values
local t = { 4,5,6,7,8,9,10 }
local n = 0
for i,v in ipairs(t) do
assert(v == i+3)
n = n + 1
end
assert(n == 7)
end

do --- jit key=value
local t = {}
for i=1,100 do t[i]=i end
local n = 0
for i,v in ipairs(t) do
assert(i == v)
n = n + 1
end
assert(n == 100)
end

do --- untitled
local t = {}
local o = {{}, {}}
for i=1,100 do
local c = i..""
t[i] = c
o[1][c] = i
o[2][c] = i
end
o[1]["90"] = nil

local n = 0
for _, c in ipairs(t) do
for i = 1, 2 do
o[i][c] = o[i][c] or 1
n = n + 1
end
end
assert(n == 200)
end
17 changes: 17 additions & 0 deletions test/lib/base/next.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
do --- _G 1
local ok, err = pcall(next, _G, 1)
assert(not ok)
local ok, err = pcall(function() next(_G, 1) end)
assert(not ok)
end

do --- as iterator
local t = { foo = 9, bar = 10, 4, 5, 6 }
local r = {}
local function dummy() end
local function f(next)
for k,v in next,t,nil do r[#r+1] = k; if v == 5 then f(dummy) end end
end
f(next)
assert(#r == 5)
end
26 changes: 26 additions & 0 deletions test/lib/base/pairs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@ do --- metamethods +compat5.2
assert(b == 107500)
end

do --- _G
local n = 0
for k,v in pairs(_G) do
assert(_G[k] == v)
n = n + 1
end
assert(n >= 35)
end

do --- count
local function count(t)
local n = 0
for i,v in pairs(t) do
n = n + 1
end
return n;
end
assert(count({ 4,5,6,nil,8,nil,10}) == 5)
assert(count({ [0] = 3, 4,5,6,nil,8,nil,10}) == 6)
assert(count({ foo=1, bar=2, baz=3 }) == 3)
assert(count({ foo=1, bar=2, baz=3, boo=4 }) == 4)
assert(count({ 4,5,6,nil,8,nil,10, foo=1, bar=2, baz=3 }) == 8)
local t = { foo=1, bar=2, baz=3, boo=4 }
t.bar = nil; t.boo = nil
assert(count(t) == 2)
end
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@

do
do --- tonumber int
local x = 0
for i=1,100 do x = x + tonumber(i) end
assert(x == 5050)
end

do
do --- tonumber float
local x = 0
for i=1.5,100.5 do x = x + tonumber(i) end
assert(x == 5100)
end

do
do --- tostring int / tonumber
local t = {}
for i=1,100 do t[i] = tostring(i) end
local x = 0
for i=1,100 do assert(type(t[i]) == "string"); x = x + tonumber(t[i]) end
assert(x == 5050)
end

do
do --- tostring float / tonumber
local t = {}
for i=1,100 do t[i] = tostring(i+0.5) end
local x = 0
for i=1,100 do assert(type(t[i]) == "string"); x = x + tonumber(t[i]) end
assert(x == 5100)
end

do
do --- tonumber table
for i=1,100 do assert(tonumber({}) == nil) end
end

do
do --- tostring int / tostring
local t = {}
for i=1,100 do t[i] = tostring(i) end
for i=1,100 do t[i] = tostring(t[i]) end
Expand All @@ -40,7 +40,7 @@ do
assert(x == 5050)
end

do
do --- tostring table __tostring
local mt = { __tostring = function(t) return tostring(t[1]) end }
local t = {}
for i=1,100 do t[i] = setmetatable({i}, mt) end
Expand All @@ -50,7 +50,7 @@ do
assert(x == 5050)
end

do
do --- tostring table __tostring __call
local r = setmetatable({},
{ __call = function(x, t) return tostring(t[1]) end })
local mt = { __tostring = r }
Expand All @@ -62,7 +62,7 @@ do
assert(x == 5050)
end

do
do --- print calls overridden tostring +lua<5.2
local x = false
local co = coroutine.create(function() print(1) end)
debug.setfenv(co, setmetatable({}, { __index = {
Expand All @@ -71,12 +71,11 @@ do
assert(x == true)
end

do
do --- tonumber base 2
assert(tonumber(111, 2) == 7)
end

do
do --- __tostring must be callable
local t = setmetatable({}, { __tostring = "" })
assert(pcall(function() tostring(t) end) == false)
end

85 changes: 0 additions & 85 deletions test/misc/iter.lua

This file was deleted.

Loading

0 comments on commit bcd286b

Please sign in to comment.