Extract luaj3.0-tests.zip to luaj-test/src/test/resources and delete it

This commit is contained in:
Enrico Horn
2021-07-11 22:59:08 +02:00
parent e7e6190f9c
commit 1833c57d4d
138 changed files with 20460 additions and 39 deletions

View File

@@ -0,0 +1,277 @@
-- tostring replacement that assigns ids
local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'}
tostring = function(x)
if not x or not types[type(x)] then return ts(x) end
if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end
return id[x]
end
-- wrap pcall to return one result
-- error message are tested elsewhere
local pc = pcall
local pcall = function(...)
local s,e = pc(...)
if s then return e end
return false, type(e)
end
-- print
print()
print(11)
print("abc",123,nil,"pqr")
print( nil and 'T' or 'F' )
print( false and 'T' or 'F' )
print( 0 and 'T' or 'F' )
-- assert
print( 'assert(true)', assert(true) )
print( 'pcall(assert,true)', pcall(assert,true) )
print( 'pcall(assert,false)', pcall(assert,false) )
print( 'pcall(assert,nil)', pcall(assert,nil) )
print( 'pcall(assert,true,"msg")', pcall(assert,true,"msg") )
print( 'pcall(assert,false,"msg")', pcall(assert,false,"msg") )
print( 'pcall(assert,nil,"msg")', pcall(assert,nil,"msg") )
print( 'pcall(assert,false,"msg","msg2")', pcall(assert,false,"msg","msg2") )
-- collectgarbage (not supported)
print( 'collectgarbage("count")', type(collectgarbage("count")))
print( 'collectgarbage("collect")', type(collectgarbage("collect")))
print( 'collectgarbage("count")', type(collectgarbage("count")))
-- dofile (not supported)
-- ipairs
print( 'pcall(ipairs)', pcall(ipairs) )
print( 'pcall(ipairs,nil)', pcall(ipairs,nil) )
print( 'pcall(ipairs,"a")', pcall(ipairs,"a") )
print( 'pcall(ipairs,1)', pcall(ipairs,1) )
for k,v in ipairs({}) do print('ipairs1',k,v)end
for k,v in ipairs({'one','two'}) do print('ipairs2',k,v)end
for k,v in ipairs({aa='aaa',bb='bbb'}) do print('ipairs3',k,v)end
for k,v in ipairs({aa='aaa',bb='bbb','one','two'}) do print('ipairs4',k,v)end
for k,v in ipairs({[30]='30',[20]='20'}) do print('ipairs5',k,v)end
-- load
t = { "print ", "'table ", "loaded'", "", " print'after empty string'" }
i = 0
f = function() i = i + 1; return t[i]; end
c,e = load(f)
if c then print('load: ', pcall(c)) else print('load failed:', e) end
-- loadfile
-- load
local lst = "print(3+4); return 8"
local chunk, err = load( lst )
print( 'load("'..lst..'")', chunk, err )
print( 'load("'..lst..'")()', chunk() )
-- pairs
print( 'pcall(pairs)', pcall(pairs) )
print( 'pcall(pairs,nil)', pcall(pairs,nil) )
print( 'pcall(pairs,"a")', pcall(pairs,"a") )
print( 'pcall(pairs,1)', pcall(pairs,1) )
for k,v in pairs({}) do print('pairs1',k,v)end
for k,v in pairs({'one','two'}) do print('pairs2',k,v)end
for k,v in pairs({aa='aaa'}) do print('pairs3',k,v)end
for k,v in pairs({aa='aaa','one','two'}) do print('pairs4',k,v)end
for k,v in pairs({[20]='30',[30]='20'}) do print('pairs5',k,v)end
-- _G
print( '_G["abc"] (before)', _G["abc"] )
abc='def'
print( '_G["abc"] (after)', _G["abc"] )
-- type
print( 'type(nil)', type(nil) )
print( 'type("a")', type("a") )
print( 'type(1)', type(1) )
print( 'type(1.5)', type(1.5) )
print( 'type(function() end)', type(function() end) )
print( 'type({})', type({}) )
print( 'type(true)', type(true) )
print( 'type(false)', type(false) )
print( 'pcall(type,type)', pcall(type,type) )
print( 'pcall(type)', pcall(type) )
print( '(function() return pcall(type) end)()', (function() return pcall(type) end)() )
local function la() return pcall(type) end
print( 'la()', la() )
function ga() return pcall(type) end
print( 'ga()', ga() )
-- getmetatable, setmetatable
ta = { aa1="aaa1", aa2="aaa2" }
tb = { bb1="bbb1", bb2="bbb2" }
print( 'getmetatable(ta)', getmetatable(ta) )
print( 'getmetatable(tb)', getmetatable(tb) )
print( 'setmetatable(ta),{cc1="ccc1"}', type( setmetatable(ta,{cc1="ccc1"}) ) )
print( 'setmetatable(tb),{dd1="ddd1"}', type( setmetatable(tb,{dd1="ddd1"}) ) )
print( 'getmetatable(ta)["cc1"]', getmetatable(ta)["cc1"] )
print( 'getmetatable(tb)["dd1"]', getmetatable(tb)["dd1"] )
print( 'getmetatable(1)', getmetatable(1) )
print( 'pcall(setmetatable,1)', pcall(setmetatable,1) )
print( 'pcall(setmetatable,nil)', pcall(setmetatable,nil) )
print( 'pcall(setmetatable,"ABC")', pcall(setmetatable,"ABC") )
print( 'pcall(setmetatable,function() end)', pcall(setmetatable,function() end) )
-- rawget,rawset
local mt = {aa="aaa", bb="bbb"}
mt.__index = mt
mt.__newindex = mt
local s = {cc="ccc", dd="ddd", }
local t = {cc="ccc", dd="ddd"}
setmetatable(t,mt)
print( 'pcall(rawget)', pcall(rawget))
print( 'pcall(rawget,"a")', pcall(rawget,"a"))
print( 'pcall(rawget,s)', pcall(rawget,s))
print( 'pcall(rawget,t)', pcall(rawget,t))
function printtables()
function printtable(name,t)
print( ' '..name, t["aa"], t["bb"], t["cc"], t["dd"], t["ee"], t["ff"], t["gg"] )
print( ' '..name,
rawget(t,"aa"),
rawget(t,"bb"),
rawget(t,"cc"),
rawget(t,"dd"),
rawget(t,"ee"),
rawget(t,"ff"),
rawget(t,"gg") )
end
printtable( 's', s )
printtable( 't', t )
printtable( 'mt', mt )
end
printtables()
print( 'pcall(rawset,s,"aa","www")', rawset(s,"aa","www"))
printtables()
print( 'pcall(rawset,s,"cc","xxx")', rawset(s,"cc","xxx"))
printtables()
print( 'pcall(rawset,t,"aa","yyy")', rawset(t,"aa","yyy"))
printtables()
print( 'pcall(rawset,t,"dd","zzz")', rawset(t,"dd","zzz"))
printtables()
-- rawlen
print( 'pcall(rawlen, {})', pcall(rawlen, {}))
print( 'pcall(rawlen, {"a"})', pcall(rawlen, {'a'}))
print( 'pcall(rawlen, {"a","b"})', pcall(rawlen, {'a','b'}))
print( 'pcall(rawlen, "")', pcall(rawlen, ""))
print( 'pcall(rawlen, "a")', pcall(rawlen, 'a'))
print( 'pcall(rawlen, "ab")', pcall(rawlen, 'ab'))
print( 'pcall(rawlen, 1)', pcall(rawlen, 1))
print( 'pcall(rawlen, nil)', pcall(rawlen, nil))
print( 'pcall(rawlen)', pcall(rawlen))
printtables()
print( 's["ee"]="ppp"' ); s["ee"]="ppp"
printtables()
print( 's["cc"]="qqq"' ); s["cc"]="qqq"
printtables()
print( 't["ff"]="rrr"' ); t["ff"]="rrr"
printtables()
print( 't["dd"]="sss"' ); t["dd"]="sss"
printtables()
print( 'mt["gg"]="ttt"' ); mt["gg"]="ttt"
printtables()
-- select
print( 'pcall(select)', pcall(select) )
print( 'select(1,11,22,33,44,55)', select(1,11,22,33,44,55) )
print( 'select(2,11,22,33,44,55)', select(2,11,22,33,44,55) )
print( 'select(3,11,22,33,44,55)', select(3,11,22,33,44,55) )
print( 'select(4,11,22,33,44,55)', select(4,11,22,33,44,55) )
print( 'pcall(select,5,11,22,33,44,55)', pcall(select,5,11,22,33,44,55) )
print( 'pcall(select,6,11,22,33,44,55)', pcall(select,6,11,22,33,44,55) )
print( 'pcall(select,7,11,22,33,44,55)', pcall(select,7,11,22,33,44,55) )
print( 'pcall(select,0,11,22,33,44,55)', pcall(select,0,11,22,33,44,55) )
print( 'pcall(select,-1,11,22,33,44,55)', pcall(select,-1,11,22,33,44,55) )
print( 'pcall(select,-2,11,22,33,44,55)', pcall(select,-2,11,22,33,44,55) )
print( 'pcall(select,-4,11,22,33,44,55)', pcall(select,-4,11,22,33,44,55) )
print( 'pcall(select,-5,11,22,33,44,55)', pcall(select,-5,11,22,33,44,55) )
print( 'pcall(select,-6,11,22,33,44,55)', pcall(select,-6,11,22,33,44,55) )
print( 'pcall(select,1)', pcall(select,1) )
print( 'pcall(select,select)', pcall(select,select) )
print( 'pcall(select,{})', pcall(select,{}) )
print( 'pcall(select,"2",11,22,33)', pcall(select,"2",11,22,33) )
print( 'pcall(select,"abc",11,22,33)', pcall(select,"abc",11,22,33) )
-- tonumber
print( 'pcall(tonumber)', pcall(tostring) )
print( 'pcall(tonumber,nil)', pcall(tonumber,nil) )
print( 'pcall(tonumber,"abc")', pcall(tonumber,"abc") )
print( 'pcall(tonumber,"123")', pcall(tonumber,"123") )
print( 'pcall(tonumber,"123",10)', pcall(tonumber,"123", 10) )
print( 'pcall(tonumber,"123",8)', pcall(tonumber,"123", 8) )
print( 'pcall(tonumber,"123",6)', pcall(tonumber,"123", 6) )
print( 'pcall(tonumber,"10101",4)', pcall(tonumber,"10101", 4) )
print( 'pcall(tonumber,"10101",3)', pcall(tonumber,"10101", 3) )
print( 'pcall(tonumber,"10101",2)', pcall(tonumber,"10101", 2) )
print( 'pcall(tonumber,"1a1",16)', pcall(tonumber,"1a1", 16) )
print( 'pcall(tonumber,"1a1",32)', pcall(tonumber,"1a1", 32) )
print( 'pcall(tonumber,"1a1",54)', pcall(tonumber,"1a1", 54) )
print( 'pcall(tonumber,"1a1",1)', pcall(tonumber,"1a1", 1) )
print( 'pcall(tonumber,"1a1",0)', pcall(tonumber,"1a1", 0) )
print( 'pcall(tonumber,"1a1",-1)', pcall(tonumber,"1a1", -1) )
print( 'pcall(tonumber,"1a1","32")', pcall(tonumber,"1a1", "32") )
print( 'pcall(tonumber,"123","456")', pcall(tonumber,"123","456") )
print( 'pcall(tonumber,"1a1",10)', pcall(tonumber,"1a1", 10) )
print( 'pcall(tonumber,"151",4)', pcall(tonumber,"151", 4) )
print( 'pcall(tonumber,"151",3)', pcall(tonumber,"151", 3) )
print( 'pcall(tonumber,"151",2)', pcall(tonumber,"151", 2) )
print( 'pcall(tonumber,"123",8,8)', pcall(tonumber,"123", 8, 8) )
print( 'pcall(tonumber,123)', pcall(tonumber,123) )
print( 'pcall(tonumber,true)', pcall(tonumber,true) )
print( 'pcall(tonumber,false)', pcall(tonumber,false) )
print( 'pcall(tonumber,tonumber)', pcall(tonumber,tonumber) )
print( 'pcall(tonumber,function() end)', pcall(tonumber,function() end) )
print( 'pcall(tonumber,{"one","two",a="aa",b="bb"})', pcall(tonumber,{"one","two",a="aa",b="bb"}) )
print( 'pcall(tonumber,"123.456")', pcall(tonumber,"123.456") )
print( 'pcall(tonumber," 123.456")', pcall(tonumber," 123.456") )
print( 'pcall(tonumber," 234qwer")', pcall(tonumber," 234qwer") )
print( 'pcall(tonumber,"0x20")', pcall(tonumber,"0x20") )
print( 'pcall(tonumber," 0x20")', pcall(tonumber," 0x20") )
print( 'pcall(tonumber,"0x20 ")', pcall(tonumber,"0x20 ") )
print( 'pcall(tonumber," 0x20 ")', pcall(tonumber," 0x20 ") )
print( 'pcall(tonumber,"0X20")', pcall(tonumber,"0X20") )
print( 'pcall(tonumber," 0X20")', pcall(tonumber," 0X20") )
print( 'pcall(tonumber,"0X20 ")', pcall(tonumber,"0X20 ") )
print( 'pcall(tonumber," 0X20 ")', pcall(tonumber," 0X20 ") )
print( 'pcall(tonumber,"0x20",10)', pcall(tonumber,"0x20",10) )
print( 'pcall(tonumber,"0x20",16)', pcall(tonumber,"0x20",16) )
print( 'pcall(tonumber,"0x20",8)', pcall(tonumber,"0x20",8) )
-- tostring
print( 'pcall(tostring)', pcall(tostring) )
print( 'pcall(tostring,nil)', pcall(tostring,nil) )
print( 'pcall(tostring,"abc")', pcall(tostring,"abc") )
print( 'pcall(tostring,"abc","def")', pcall(tostring,"abc","def") )
print( 'pcall(tostring,123)', pcall(tostring,123) )
print( 'pcall(tostring,true)', pcall(tostring,true) )
print( 'pcall(tostring,false)', pcall(tostring,false) )
print( 'tostring(tostring)', type(tostring(tostring)) )
print( 'tostring(function() end)', type(tostring(function() end)) )
print( 'tostring({"one","two",a="aa",b="bb"})', type(tostring({"one","two",a="aa",b="bb"})) )
-- _VERSION
print( '_VERSION', type(_VERSION) )
-- xpcall
local errfunc = function( detail )
print( ' in errfunc', type(detail) )
return 'response-from-xpcall'
end
local badfunc = function() error( 'error-from-badfunc' ) end
local wrappedbad = function() pcall( badfunc ) end
print( 'pcall(badfunc)', pcall(badfunc) )
print( 'pcall(badfunc,errfunc)', pcall(badfunc,errfunc) )
print( 'pcall(badfunc,badfunc)', pcall(badfunc,badfunc) )
print( 'pcall(wrappedbad)', pcall(wrappedbad) )
print( 'pcall(wrappedbad,errfunc)', pcall(wrappedbad,errfunc) )
print( 'pcall(xpcall(badfunc))', pcall(xpcall,badfunc) )
print( 'pcall(xpcall(badfunc,errfunc))', pcall(xpcall,badfunc,errfunc) )
print( 'pcall(xpcall(badfunc,badfunc))', pcall(xpcall,badfunc,badfunc) )
print( 'pcall(xpcall(wrappedbad))', pcall(xpcall,wrappedbad) )
print( 'xpcall(wrappedbad,errfunc)', xpcall(wrappedbad,errfunc) )

View File

@@ -0,0 +1,240 @@
11
abc 123 nil pqr
F
F
T
assert(true) true
pcall(assert,true) true
pcall(assert,false) false string
pcall(assert,nil) false string
pcall(assert,true,"msg") true
pcall(assert,false,"msg") false string
pcall(assert,nil,"msg") false string
pcall(assert,false,"msg","msg2") false string
collectgarbage("count") number
collectgarbage("collect") number
collectgarbage("count") number
pcall(ipairs) false string
pcall(ipairs,nil) false string
pcall(ipairs,"a") false string
pcall(ipairs,1) false string
ipairs2 1 one
ipairs2 2 two
ipairs4 1 one
ipairs4 2 two
table loaded
load: nil
load("print(3+4); return 8") func.1 nil
7
load("print(3+4); return 8")() 8
pcall(pairs) false string
pcall(pairs,nil) false string
pcall(pairs,"a") false string
pcall(pairs,1) false string
pairs2 1 one
pairs2 2 two
pairs3 aa aaa
pairs4 1 one
pairs4 2 two
pairs4 aa aaa
pairs5 20 30
pairs5 30 20
_G["abc"] (before) nil
_G["abc"] (after) def
type(nil) nil
type("a") string
type(1) number
type(1.5) number
type(function() end) function
type({}) table
type(true) boolean
type(false) boolean
pcall(type,type) function
pcall(type) false string
(function() return pcall(type) end)() false string
la() false string
ga() false string
getmetatable(ta) nil
getmetatable(tb) nil
setmetatable(ta),{cc1="ccc1"} table
setmetatable(tb),{dd1="ddd1"} table
getmetatable(ta)["cc1"] ccc1
getmetatable(tb)["dd1"] ddd1
getmetatable(1) nil
pcall(setmetatable,1) false string
pcall(setmetatable,nil) false string
pcall(setmetatable,"ABC") false string
pcall(setmetatable,function() end) false string
pcall(rawget) false string
pcall(rawget,"a") false string
pcall(rawget,s) false string
pcall(rawget,t) false string
s nil nil ccc ddd nil nil nil
s nil nil ccc ddd nil nil nil
t aaa bbb ccc ddd nil nil nil
t nil nil ccc ddd nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
pcall(rawset,s,"aa","www") tbl.2
s www nil ccc ddd nil nil nil
s www nil ccc ddd nil nil nil
t aaa bbb ccc ddd nil nil nil
t nil nil ccc ddd nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
pcall(rawset,s,"cc","xxx") tbl.2
s www nil xxx ddd nil nil nil
s www nil xxx ddd nil nil nil
t aaa bbb ccc ddd nil nil nil
t nil nil ccc ddd nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
pcall(rawset,t,"aa","yyy") tbl.3
s www nil xxx ddd nil nil nil
s www nil xxx ddd nil nil nil
t yyy bbb ccc ddd nil nil nil
t yyy nil ccc ddd nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
pcall(rawset,t,"dd","zzz") tbl.3
s www nil xxx ddd nil nil nil
s www nil xxx ddd nil nil nil
t yyy bbb ccc zzz nil nil nil
t yyy nil ccc zzz nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
pcall(rawlen, {}) 0
pcall(rawlen, {"a"}) 1
pcall(rawlen, {"a","b"}) 2
pcall(rawlen, "") 0
pcall(rawlen, "a") 1
pcall(rawlen, "ab") 2
pcall(rawlen, 1) false string
pcall(rawlen, nil) false string
pcall(rawlen) false string
s www nil xxx ddd nil nil nil
s www nil xxx ddd nil nil nil
t yyy bbb ccc zzz nil nil nil
t yyy nil ccc zzz nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
s["ee"]="ppp"
s www nil xxx ddd ppp nil nil
s www nil xxx ddd ppp nil nil
t yyy bbb ccc zzz nil nil nil
t yyy nil ccc zzz nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
s["cc"]="qqq"
s www nil qqq ddd ppp nil nil
s www nil qqq ddd ppp nil nil
t yyy bbb ccc zzz nil nil nil
t yyy nil ccc zzz nil nil nil
mt aaa bbb nil nil nil nil nil
mt aaa bbb nil nil nil nil nil
t["ff"]="rrr"
s www nil qqq ddd ppp nil nil
s www nil qqq ddd ppp nil nil
t yyy bbb ccc zzz nil rrr nil
t yyy nil ccc zzz nil nil nil
mt aaa bbb nil nil nil rrr nil
mt aaa bbb nil nil nil rrr nil
t["dd"]="sss"
s www nil qqq ddd ppp nil nil
s www nil qqq ddd ppp nil nil
t yyy bbb ccc sss nil rrr nil
t yyy nil ccc sss nil nil nil
mt aaa bbb nil nil nil rrr nil
mt aaa bbb nil nil nil rrr nil
mt["gg"]="ttt"
s www nil qqq ddd ppp nil nil
s www nil qqq ddd ppp nil nil
t yyy bbb ccc sss nil rrr ttt
t yyy nil ccc sss nil nil nil
mt aaa bbb nil nil nil rrr ttt
mt aaa bbb nil nil nil rrr ttt
pcall(select) false string
select(1,11,22,33,44,55) 11 22 33 44 55
select(2,11,22,33,44,55) 22 33 44 55
select(3,11,22,33,44,55) 33 44 55
select(4,11,22,33,44,55) 44 55
pcall(select,5,11,22,33,44,55) 55
pcall(select,6,11,22,33,44,55) nil
pcall(select,7,11,22,33,44,55) nil
pcall(select,0,11,22,33,44,55) false string
pcall(select,-1,11,22,33,44,55) 55
pcall(select,-2,11,22,33,44,55) 44
pcall(select,-4,11,22,33,44,55) 22
pcall(select,-5,11,22,33,44,55) 11
pcall(select,-6,11,22,33,44,55) false string
pcall(select,1) nil
pcall(select,select) false string
pcall(select,{}) false string
pcall(select,"2",11,22,33) 22
pcall(select,"abc",11,22,33) false string
pcall(tonumber) nil
pcall(tonumber,nil) nil
pcall(tonumber,"abc") nil
pcall(tonumber,"123") 123
pcall(tonumber,"123",10) 123
pcall(tonumber,"123",8) 83
pcall(tonumber,"123",6) 51
pcall(tonumber,"10101",4) 273
pcall(tonumber,"10101",3) 91
pcall(tonumber,"10101",2) 21
pcall(tonumber,"1a1",16) 417
pcall(tonumber,"1a1",32) 1345
pcall(tonumber,"1a1",54) false string
pcall(tonumber,"1a1",1) false string
pcall(tonumber,"1a1",0) false string
pcall(tonumber,"1a1",-1) false string
pcall(tonumber,"1a1","32") 1345
pcall(tonumber,"123","456") false string
pcall(tonumber,"1a1",10) nil
pcall(tonumber,"151",4) nil
pcall(tonumber,"151",3) nil
pcall(tonumber,"151",2) nil
pcall(tonumber,"123",8,8) 83
pcall(tonumber,123) 123
pcall(tonumber,true) nil
pcall(tonumber,false) nil
pcall(tonumber,tonumber) nil
pcall(tonumber,function() end) nil
pcall(tonumber,{"one","two",a="aa",b="bb"}) nil
pcall(tonumber,"123.456") 123.456
pcall(tonumber," 123.456") 123.456
pcall(tonumber," 234qwer") nil
pcall(tonumber,"0x20") 32
pcall(tonumber," 0x20") 32
pcall(tonumber,"0x20 ") 32
pcall(tonumber," 0x20 ") 32
pcall(tonumber,"0X20") 32
pcall(tonumber," 0X20") 32
pcall(tonumber,"0X20 ") 32
pcall(tonumber," 0X20 ") 32
pcall(tonumber,"0x20",10) nil
pcall(tonumber,"0x20",16) nil
pcall(tonumber,"0x20",8) nil
pcall(tostring) nil
pcall(tostring,nil) nil
pcall(tostring,"abc") abc
pcall(tostring,"abc","def") abc
pcall(tostring,123) 123
pcall(tostring,true) true
pcall(tostring,false) false
tostring(tostring) string
tostring(function() end) string
tostring({"one","two",a="aa",b="bb"}) string
_VERSION string
pcall(badfunc) false string
pcall(badfunc,errfunc) false string
pcall(badfunc,badfunc) false string
pcall(wrappedbad) nil
pcall(wrappedbad,errfunc) nil
pcall(xpcall(badfunc)) false string
in errfunc string
pcall(xpcall(badfunc,errfunc)) false
pcall(xpcall(badfunc,badfunc)) false
pcall(xpcall(wrappedbad)) false string
xpcall(wrappedbad,errfunc) true

View File

@@ -0,0 +1,126 @@
function printrunning()
if coroutine.running() == nil then
print("running is nil");
else
print("running is not nil")
end
end
function foo (a)
print("foo", a)
return coroutine.yield(2*a)
end
co = coroutine.create(function (a,b)
print("co-body", a, b)
local r = foo(a+1)
print("co-body", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
printrunning()
print("co.status.inside",coroutine.status(co));
local co2 = coroutine.create(function()
print("co.status.inside2",coroutine.status(co));
end)
print("co.status.inside",coroutine.status(co));
coroutine.resume(co2);
return b, "end"
end)
function exercise()
printrunning()
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, 1, 10))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "r"))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "x", "y"))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "x", "y"))
print("co.status",coroutine.status(co));
end
exercise();
co = coroutine.create(function (a,b)
print("co-body", a, b)
-- TODO: make java and C behave the same for yielding in pcalls
-- local status,r = pcall( foo, a+1 )
foo(a+1)
print("co-body", status,r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
return b, "end"
end)
exercise();
-- wrap test
local g = coroutine.wrap(function (a,b)
print("co-body", a, b)
local r = foo(a+1)
print("co-body", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
return b, "end"
end )
print("g", g(1, 10))
print("g", g("r"))
print("g", g("x", "y"))
local s,e = pcall( g, "x", "y" )
print("g", string.match(e,'cannot resume dead coroutine') or 'badmessage: '..tostring(e))
-- varargs passing
local echo = function(msg,...)
print( msg, ...)
return ...
end
local echocr = function(...)
local arg = table.pack(...)
echo('(echocr) first args', table.unpack(arg,1,arg.n))
local a = arg
while true do
a = { echo( '(echoch) yield returns', coroutine.yield( table.unpack(a) ) ) }
end
end
local c = coroutine.create( echocr )
local step = function(...)
echo( '(main) resume returns',
coroutine.resume(c, echo('(main) sending args', ...)) )
end
step(111,222,333)
step()
step(111)
step(111,222,333)
-- test loops in resume calls
b = coroutine.create( function( arg )
while ( true ) do
print( ' b-resumed', arg, b == coroutine.running() )
print( ' b-b', coroutine.status(b) )
print( ' b-c', coroutine.status(c) )
print( ' b-resume-b',coroutine.resume( b, 'b-arg-for-b' ) )
print( ' b-resume-c',coroutine.resume( c, 'b-arg-for-c' ) )
arg = coroutine.yield( 'b-rslt' )
end
end )
c = coroutine.create( function( arg )
for i=1,3 do
print( ' c-resumed', arg, c == coroutine.running() )
print( ' c-b', coroutine.status(b) )
print( ' c-c', coroutine.status(c) )
print( ' c-resume-b',coroutine.resume( b, 'b-arg-for-b' ) )
print( ' c-resume-c',coroutine.resume( c, 'b-arg-for-c' ) )
arg = coroutine.yield( 'c-rslt' )
end
end )
for i=1,3 do
print( 'main-b', coroutine.status(b) )
print( 'main-c', coroutine.status(c) )
print( 'main-resume-b',coroutine.resume( b, 'main-arg-for-b' ) )
print( 'main-resume-c',coroutine.resume( c, 'main-arg-for-c' ) )
end

View File

@@ -0,0 +1,74 @@
running is not nil
co.status suspended
co-body 1 10
foo 2
main true 4
co.status suspended
co-body r
main true 11 -9
co.status suspended
co-body x y
running is not nil
co.status.inside running
co.status.inside running
co.status.inside2 normal
main true 10 end
co.status dead
main false cannot resume dead coroutine
co.status dead
running is not nil
co.status suspended
co-body 1 10
foo 2
main true 4
co.status suspended
co-body nil nil
main true 11 -9
co.status suspended
co-body x y
main true 10 end
co.status dead
main false cannot resume dead coroutine
co.status dead
co-body 1 10
foo 2
g 4
co-body r
g 11 -9
co-body x y
g 10 end
g cannot resume dead coroutine
(main) sending args 111 222 333
(echocr) first args 111 222 333
(main) resume returns true 111 222 333
(main) sending args
(echoch) yield returns
(main) resume returns true
(main) sending args 111
(echoch) yield returns 111
(main) resume returns true 111
(main) sending args 111 222 333
(echoch) yield returns 111 222 333
(main) resume returns true 111 222 333
main-b suspended
main-c suspended
b-resumed main-arg-for-b true
b-b running
b-c suspended
b-resume-b false cannot resume non-suspended coroutine
c-resumed b-arg-for-c true
c-b normal
c-c running
c-resume-b false cannot resume non-suspended coroutine
c-resume-c false cannot resume non-suspended coroutine
b-resume-c false attempt to yield across metamethod/C-call boundary
main-resume-b false attempt to yield across metamethod/C-call boundary
main-resume-c false cannot resume dead coroutine
main-b dead
main-c dead
main-resume-b false cannot resume dead coroutine
main-resume-c false cannot resume dead coroutine
main-b dead
main-c dead
main-resume-b false cannot resume dead coroutine
main-resume-c false cannot resume dead coroutine

View File

@@ -0,0 +1,280 @@
local print,tostring,_G,pcall,ipairs,isnumber = print,tostring,_G,pcall,ipairs,isnumber
local e,f,g,h,s
print( 'has debug', debug~=nil )
if not debug then error( 'no debug' ) end
print( '----- debug.getlocal, debug.setlocal' )
h = function(v,i,n)
s = 'h-'..v..'-'..i
local x1,y1 = debug.getlocal(v,i)
local x2,y2 = debug.setlocal(v,i,n)
local x3,y3 = debug.getlocal(v,i)
return s..' -> '..v..'-'..i..' '..
'get='..tostring(x1)..','..tostring(y1)..' '..
'set='..tostring(x2)..','..tostring(y2)..' '..
'get='..tostring(x3)..','..tostring(y3)..' '
end
g = function(...)
local p,q,r=7,8,9
local t = h(...)
local b = table.concat({...},',')
return t..'\tg locals='..p..','..q..','..r..' tbl={'..b..'}'
end
f = function(a,b,c)
local d,e,f = 4,5,6
local t = g(a,b,c)
return t..'\tf locals='..','..a..','..b..','..c..','..d..','..e..','..f
end
for lvl=3,2,-1 do
for lcl=0,7 do
print( pcall( f, lvl, lcl, '#' ) )
end
end
for lvl=1,1 do
for lcl=3,7 do
print( pcall( f, lvl, lcl, '#' ) )
end
end
print( '----- debug.getupvalue, debug.setupvalue' )
local m,n,o = 101,102,103
f = function(p,q,r)
local p,q,r = 104,105,106
local g = function(s,t,u)
local v,w,x = 107,108,109
return function()
return m,n,o,p,q,r,v,w,x
end
end
return g
end
g = f()
h = g()
local callh = function()
local t = {}
for i,v in ipairs( { pcall(h) } ) do
t[i] = tostring(v)
end
return table.concat(t,',')
end
print( 'h', h() )
local funs = { f, g, h }
local names = { 'f', 'g', 'h' }
for i=1,3 do
local fun,name = funs[i],names[i]
for index=0,10 do
local s1,x1,y1 = pcall( debug.getupvalue, fun, index )
local s2,x2,y2 = pcall( debug.setupvalue, fun, index, 666000+i*111000+index )
local s3,x3,y3 = pcall( debug.getupvalue, fun, index )
print( name..' -> '..i..'-'..index..' '..
'get='..tostring(s1)..','..tostring(x1)..','..tostring(y1)..' '..
'set='..tostring(s2)..','..tostring(x2)..','..tostring(y2)..' '..
'get='..tostring(s3)..','..tostring(x3)..','..tostring(y3)..' '..
'tbl='..callh() )
end
end
print( '----- debug.setmetatable, debug.getmetatable' )
local a = {a='bbb'}
local b = {}
local mt = {__index={b='ccc'}}
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s1,x1,y1 = pcall( debug.getmetatable, a )
local s2,x2,y2 = pcall( debug.setmetatable, a, mt )
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s3,x3,y3 = pcall( debug.getmetatable, a ) print(type(s3), type(x3), type(y3), type(getmetatable(a)))
local s4,x4,y4 = pcall( debug.getmetatable, b ) print(type(s4), type(x4), type(y4), type(getmetatable(b)))
local s5,x5,y5 = pcall( debug.setmetatable, a, nil ) print(type(s5), type(x5), type(y5), type(getmetatable(a)))
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s6,x6,y6 = pcall( debug.getmetatable, a ) print(type(s6), type(x6), type(y6), type(getmetatable(a)))
if not s1 then print( 's1 error', x1 ) end
if not s2 then print( 's2 error', x2 ) end
if not s3 then print( 's3 error', x3 ) end
if not s4 then print( 's4 error', x4 ) end
if not s5 then print( 's5 error', x5 ) end
if not s6 then print( 's6 error', x6 ) end
print( 'get='..tostring(s1)..','..tostring(x1==nil)..','..tostring(y1) )
print( 'set='..tostring(s2)..','..tostring(x2==a)..','..tostring(y2) )
print( 'get='..tostring(s3)..','..tostring(x3==mt)..','..tostring(y3) )
print( 'get='..tostring(s4)..','..tostring(x4==nil)..','..tostring(y4) )
print( 'set='..tostring(s5)..','..tostring(x5==a)..','..tostring(y5) )
print( 'get='..tostring(s6)..','..tostring(x6==nil)..','..tostring(y6) )
print( pcall( debug.getmetatable, 1 ) )
print( pcall( debug.setmetatable, 1, {} ) )
print( pcall( debug.setmetatable, 1, nil ) )
print( '----- debug.getinfo' )
local printfield = function(tbl, field)
local x = tbl[field]
if x == nil then return end
local typ = type(x)
if typ=='table' then
x = '{'..table.concat(x,',')..'}'
elseif typ=='function' then
x = typ
end
print( ' '..field..': '..tostring(x) )
end
local fields = { 'source', 'short_src', 'what',
'currentline', 'linedefined', 'lastlinedefined',
'nups', 'func', 'activelines' }
local printinfo = function(...)
for i,a in ipairs({...}) do
if type(a) == 'table' then
for j,field in ipairs(fields) do
printfield( a, field)
end
else
print( tostring(a) )
end
end
end
function test()
local x = 5
function f()
x = x + 1
return x
end
function g()
x = x - 1
print( '---' )
printinfo( 'debug.getinfo(1)', debug.getinfo(1) )
printinfo( 'debug.getinfo(1,"")', debug.getinfo(1, "") )
printinfo( 'debug.getinfo(1,"l")', debug.getinfo(1, "l") )
printinfo( 'debug.getinfo(1,"fL")', debug.getinfo(1, "fL") )
printinfo( 'debug.getinfo(2)', debug.getinfo(2) )
printinfo( 'debug.getinfo(2,"l")', debug.getinfo(2, "l") )
printinfo( 'debug.getinfo(2,"fL")', debug.getinfo(2, "fL") )
printinfo( 'debug.getinfo(10,"")', pcall( debug.getinfo, 10, "" ) )
printinfo( 'debug.getinfo(-10,"")', pcall( debug.getinfo, -10, "" ) )
print( '---' )
return x
end
print(f())
print(g())
return f, g
end
local options = "nSlufL"
local e,f,g = pcall( test )
print( 'e,f,g', e, type(f), type(g) )
printinfo( 'debug.getinfo(f)', pcall(debug.getinfo, f) )
printinfo( 'debug.getinfo(f,"'..options..'")', pcall(debug.getinfo, f, options) )
for j=1,6 do
local opts = options:sub(j,j)
printinfo( 'debug.getinfo(f,"'..opts..'")', pcall(debug.getinfo, f, opts) )
end
printinfo( 'debug.getinfo(g)', pcall(debug.getinfo, g) )
printinfo( 'debug.getinfo(test)', pcall(debug.getinfo, test) )
print( '----- debug.sethook, debug.gethook' )
f = function(x)
g = function(y)
return math.min(x,h)
end
local a = g(x)
return a + a
end
local hook = function(...)
print( ' ... in hook', ... )
local info = debug.getinfo(2,"Sl")
if info then
print( ' info[2]='..tostring(info.short_src)..','..tostring(info.currentline) )
end
end
local tryfunc = function(hook,mask,func,arg)
local x,f,h,m
pcall( function()
debug.sethook(hook,mask)
x = func(arg)
f,h,m = debug.gethook()
end )
debug.sethook()
return x,f,h,m
end
local tryhooks = function(mask)
local s1,a1,b1,c1,d1 = pcall( tryfunc, hook, mask, f, 333 )
print( 'hook = '..mask..' -> '..
'result='..tostring(s1)..','..tostring(a1)..','..
type(b1)..','..type(c1)..','..
tostring(b1==f)..','..tostring(c1==hook)..','..
tostring(d1)..' ' )
end
tryhooks("c")
tryhooks("r")
tryhooks("l")
tryhooks("crl")
print( '----- debug.traceback' )
function test()
function a(msg)
print((string.gsub(debug.traceback(msg), "%[Java]", "[C]")))
end
local function b(msg)
pcall(a,msg)
end
c = function(i)
if i <= 0 then b('hi') return end
return c(i-1)
end
d = setmetatable({},{__index=function(t,k) v = c(k) return v end})
local e = function()
return d[0]
end
local f = {
g = function()
e()
end
}
h = function()
f.g()
end
local i = h
i()
end
pcall(test)
print( '----- debug.upvalueid' )
local x,y = 100,200
function a(b,c)
local z,w = b,c
return function()
x,y,z,w = x+1,y+1,z+1,w+1
return x,y,z,w
end
end
a1 = a(300,400)
a2 = a(500,600)
print('debug.getupvalue(a1,1)', debug.getupvalue(a1,1))
print('debug.getupvalue(a1,2)', debug.getupvalue(a1,2))
print('debug.getupvalue(a2,1)', debug.getupvalue(a2,1))
print('debug.getupvalue(a2,2)', debug.getupvalue(a2,2))
print('debug.upvalueid(a1,1) == debug.upvalueid(a1,1)', debug.upvalueid(a1,1) == debug.upvalueid(a1,1))
print('debug.upvalueid(a1,1) == debug.upvalueid(a2,1)', debug.upvalueid(a1,1) == debug.upvalueid(a2,1))
print('debug.upvalueid(a1,1) == debug.upvalueid(a1,2)', debug.upvalueid(a1,1) == debug.upvalueid(a1,2))
print( '----- debug.upvaluejoin' )
print('a1',a1())
print('a2',a2())
print('debug.upvaluejoin(a1,1,a2,2)', debug.upvaluejoin(a1,1,a2,2))
print('debug.upvaluejoin(a1,3,a2,4)', debug.upvaluejoin(a1,3,a2,4))
print('a1',a1())
print('a2',a2())
print('a1',a1())
print('a2',a2())
for i = 1,4 do
print('debug.getupvalue(a1,'..i..')', debug.getupvalue(a1,i))
print('debug.getupvalue(a2,'..i..')', debug.getupvalue(a2,i))
for j = 1,4 do
print('debug.upvalueid(a1,'..i..') == debug.upvalueid(a1,'..j..')', debug.upvalueid(a1,i) == debug.upvalueid(a1,j))
print('debug.upvalueid(a1,'..i..') == debug.upvalueid(a2,'..j..')', debug.upvalueid(a1,i) == debug.upvalueid(a2,j))
print('debug.upvalueid(a2,'..i..') == debug.upvalueid(a1,'..j..')', debug.upvalueid(a2,i) == debug.upvalueid(a1,j))
print('debug.upvalueid(a2,'..i..') == debug.upvalueid(a2,'..j..')', debug.upvalueid(a2,i) == debug.upvalueid(a2,j))
end
end

View File

@@ -0,0 +1,329 @@
has debug true
----- debug.getlocal, debug.setlocal
true h-3-0 -> 3-0 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={3,0,#} f locals=,3,0,#,4,5,6
true h-3-1 -> 3-1 get=a,3 set=a,nil get=a,# g locals=7,8,9 tbl={3,1,#} f locals=,#,1,#,4,5,6
true h-3-2 -> 3-2 get=b,2 set=b,nil get=b,# g locals=7,8,9 tbl={3,2,#} f locals=,3,#,#,4,5,6
true h-3-3 -> 3-3 get=c,# set=c,nil get=c,# g locals=7,8,9 tbl={3,3,#} f locals=,3,3,#,4,5,6
true h-3-4 -> 3-4 get=d,4 set=d,nil get=d,# g locals=7,8,9 tbl={3,4,#} f locals=,3,4,#,#,5,6
true h-3-5 -> 3-5 get=e,5 set=e,nil get=e,# g locals=7,8,9 tbl={3,5,#} f locals=,3,5,#,4,#,6
true h-3-6 -> 3-6 get=f,6 set=f,nil get=f,# g locals=7,8,9 tbl={3,6,#} f locals=,3,6,#,4,5,#
true h-3-7 -> 3-7 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={3,7,#} f locals=,3,7,#,4,5,6
true h-2-0 -> 2-0 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,0,#} f locals=,2,0,#,4,5,6
true h-2-1 -> 2-1 get=p,7 set=p,nil get=p,# g locals=#,8,9 tbl={2,1,#} f locals=,2,1,#,4,5,6
true h-2-2 -> 2-2 get=q,8 set=q,nil get=q,# g locals=7,#,9 tbl={2,2,#} f locals=,2,2,#,4,5,6
true h-2-3 -> 2-3 get=r,9 set=r,nil get=r,# g locals=7,8,# tbl={2,3,#} f locals=,2,3,#,4,5,6
true h-2-4 -> 2-4 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,4,#} f locals=,2,4,#,4,5,6
true h-2-5 -> 2-5 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,5,#} f locals=,2,5,#,4,5,6
true h-2-6 -> 2-6 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,6,#} f locals=,2,6,#,4,5,6
true h-2-7 -> 2-7 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,7,#} f locals=,2,7,#,4,5,6
true h-1-3 -> 1-3 get=n,# set=n,nil get=n,# g locals=7,8,9 tbl={1,3,#} f locals=,1,3,#,4,5,6
true h-1-4 -> 1-4 get=#,nil set=x1,nil get=x1,# g locals=7,8,9 tbl={1,4,#} f locals=,1,4,#,4,5,6
true h-1-5 -> 1-5 get=nil,# set=y1,nil get=y1,# g locals=7,8,9 tbl={1,5,#} f locals=,1,5,#,4,5,6
true h-1-6 -> 1-6 get=nil,nil set=nil,nil get=x2,nil g locals=7,8,9 tbl={1,6,#} f locals=,1,6,#,4,5,6
true h-1-7 -> 1-7 get=nil,nil set=nil,nil get=y2,nil g locals=7,8,9 tbl={1,7,#} f locals=,1,7,#,4,5,6
----- debug.getupvalue, debug.setupvalue
h 101 102 103 104 105 106 107 108 109
f -> 1-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,101,102,103,104,105,106,107,108,109
f -> 1-1 get=true,m,101 set=true,m,nil get=true,m,777001 tbl=true,777001,102,103,104,105,106,107,108,109
f -> 1-2 get=true,n,102 set=true,n,nil get=true,n,777002 tbl=true,777001,777002,103,104,105,106,107,108,109
f -> 1-3 get=true,o,103 set=true,o,nil get=true,o,777003 tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-4 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-5 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-6 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-7 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-8 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-9 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
g -> 2-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
g -> 2-1 get=true,m,777001 set=true,m,nil get=true,m,888001 tbl=true,888001,777002,777003,104,105,106,107,108,109
g -> 2-2 get=true,n,777002 set=true,n,nil get=true,n,888002 tbl=true,888001,888002,777003,104,105,106,107,108,109
g -> 2-3 get=true,o,777003 set=true,o,nil get=true,o,888003 tbl=true,888001,888002,888003,104,105,106,107,108,109
g -> 2-4 get=true,p,104 set=true,p,nil get=true,p,888004 tbl=true,888001,888002,888003,888004,105,106,107,108,109
g -> 2-5 get=true,q,105 set=true,q,nil get=true,q,888005 tbl=true,888001,888002,888003,888004,888005,106,107,108,109
g -> 2-6 get=true,r,106 set=true,r,nil get=true,r,888006 tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-7 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-8 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-9 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
h -> 3-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
h -> 3-1 get=true,m,888001 set=true,m,nil get=true,m,999001 tbl=true,999001,888002,888003,888004,888005,888006,107,108,109
h -> 3-2 get=true,n,888002 set=true,n,nil get=true,n,999002 tbl=true,999001,999002,888003,888004,888005,888006,107,108,109
h -> 3-3 get=true,o,888003 set=true,o,nil get=true,o,999003 tbl=true,999001,999002,999003,888004,888005,888006,107,108,109
h -> 3-4 get=true,p,888004 set=true,p,nil get=true,p,999004 tbl=true,999001,999002,999003,999004,888005,888006,107,108,109
h -> 3-5 get=true,q,888005 set=true,q,nil get=true,q,999005 tbl=true,999001,999002,999003,999004,999005,888006,107,108,109
h -> 3-6 get=true,r,888006 set=true,r,nil get=true,r,999006 tbl=true,999001,999002,999003,999004,999005,999006,107,108,109
h -> 3-7 get=true,v,107 set=true,v,nil get=true,v,999007 tbl=true,999001,999002,999003,999004,999005,999006,999007,108,109
h -> 3-8 get=true,w,108 set=true,w,nil get=true,w,999008 tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,109
h -> 3-9 get=true,x,109 set=true,x,nil get=true,x,999009 tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,999009
h -> 3-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,999009
----- debug.setmetatable, debug.getmetatable
a.a=bbb a.b=nil b.a=nil b.b=nil
a.a=bbb a.b=ccc b.a=nil b.b=nil
boolean table nil table
boolean nil nil nil
boolean table nil nil
a.a=bbb a.b=nil b.a=nil b.b=nil
boolean nil nil nil
get=true,true,nil
set=true,true,nil
get=true,true,nil
get=true,true,nil
set=true,true,nil
get=true,true,nil
true nil
true 1
true 1
----- debug.getinfo
6
---
debug.getinfo(1)
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: 144
linedefined: 141
lastlinedefined: 155
nups: 5
func: function
debug.getinfo(1,"")
debug.getinfo(1,"l")
currentline: 146
debug.getinfo(1,"fL")
func: function
activelines: {}
debug.getinfo(2)
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: 157
linedefined: 135
lastlinedefined: 159
nups: 6
func: function
debug.getinfo(2,"l")
currentline: 157
debug.getinfo(2,"fL")
func: function
activelines: {}
debug.getinfo(10,"")
true
debug.getinfo(-10,"")
true
---
5
e,f,g true function function
debug.getinfo(f)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 137
lastlinedefined: 140
nups: 1
func: function
debug.getinfo(f,"nSlufL")
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 137
lastlinedefined: 140
nups: 1
func: function
activelines: {}
debug.getinfo(f,"n")
true
debug.getinfo(f,"S")
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
linedefined: 137
lastlinedefined: 140
debug.getinfo(f,"l")
true
currentline: -1
debug.getinfo(f,"u")
true
nups: 1
debug.getinfo(f,"f")
true
func: function
debug.getinfo(f,"L")
true
activelines: {}
debug.getinfo(g)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 141
lastlinedefined: 155
nups: 5
func: function
debug.getinfo(test)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 135
lastlinedefined: 159
nups: 6
func: function
----- debug.sethook, debug.gethook
... in hook call nil
info[2]=debuglib.lua,177
... in hook call nil
info[2]=debuglib.lua,176
... in hook call nil
info[2]=[C],-1
... in hook call nil
info[2]=[C],-1
hook = c -> result=true,nil,nil,nil,false,false,nil
... in hook return nil
info[2]=[C],-1
... in hook return nil
info[2]=[C],-1
hook = r -> result=true,nil,nil,nil,false,false,nil
... in hook line 192
info[2]=debuglib.lua,192
... in hook line 177
info[2]=debuglib.lua,177
... in hook line 178
info[2]=debuglib.lua,178
... in hook line 176
info[2]=debuglib.lua,176
... in hook line 195
info[2]=debuglib.lua,195
hook = l -> result=true,nil,nil,nil,false,false,nil
... in hook return nil
info[2]=[C],-1
... in hook line 192
info[2]=debuglib.lua,192
... in hook call nil
info[2]=debuglib.lua,177
... in hook line 177
info[2]=debuglib.lua,177
... in hook line 178
info[2]=debuglib.lua,178
... in hook call nil
info[2]=debuglib.lua,176
... in hook line 176
info[2]=debuglib.lua,176
... in hook call nil
info[2]=[C],-1
... in hook return nil
info[2]=[C],-1
... in hook line 195
info[2]=debuglib.lua,195
... in hook call nil
info[2]=[C],-1
hook = crl -> result=true,nil,nil,nil,false,false,nil
----- debug.traceback
hi
stack traceback:
debuglib.lua:216: in function <debuglib.lua:215>
[C]: in function 'pcall'
debuglib.lua:219: in function 'b'
debuglib.lua:222: in function 'c'
debuglib.lua:225: in function '__index'
debuglib.lua:227: in function 'e'
debuglib.lua:231: in function 'g'
debuglib.lua:235: in function 'i'
debuglib.lua:238: in function <debuglib.lua:214>
[C]: in function 'pcall'
debuglib.lua:240: in main chunk
[C]: in ?
----- debug.upvalueid
debug.getupvalue(a1,1) x 100
debug.getupvalue(a1,2) y 200
debug.getupvalue(a2,1) x 100
debug.getupvalue(a2,2) y 200
debug.upvalueid(a1,1) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a1,2) false
----- debug.upvaluejoin
a1 101 201 301 401
a2 102 202 501 601
debug.upvaluejoin(a1,1,a2,2)
debug.upvaluejoin(a1,3,a2,4)
a1 203 203 602 402
a2 103 204 502 603
a1 205 205 604 403
a2 104 206 503 605
debug.getupvalue(a1,1) x 206
debug.getupvalue(a2,1) x 104
debug.upvalueid(a1,1) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a1,2) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,2) true
debug.upvalueid(a2,1) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,1) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,1) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,1) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,1) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,2) y 206
debug.getupvalue(a2,2) y 206
debug.upvalueid(a1,2) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,2) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,1) true
debug.upvalueid(a2,2) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,2) == debug.upvalueid(a1,2) true
debug.upvalueid(a1,2) == debug.upvalueid(a2,2) true
debug.upvalueid(a2,2) == debug.upvalueid(a1,2) true
debug.upvalueid(a2,2) == debug.upvalueid(a2,2) true
debug.upvalueid(a1,2) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,2) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,2) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,2) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,2) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,2) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,3) z 605
debug.getupvalue(a2,3) z 503
debug.upvalueid(a1,3) == debug.upvalueid(a1,1) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,3) == debug.upvalueid(a1,2) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,2) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,3) == debug.upvalueid(a1,3) true
debug.upvalueid(a1,3) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,3) true
debug.upvalueid(a1,3) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,4) true
debug.upvalueid(a2,3) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,4) w 403
debug.getupvalue(a2,4) w 605
debug.upvalueid(a1,4) == debug.upvalueid(a1,1) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,2) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,2) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,3) true
debug.upvalueid(a2,4) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,4) true
debug.upvalueid(a1,4) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,4) true

View File

@@ -0,0 +1,137 @@
-- tostring replacement that assigns ids
local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'}
tostring = function(x)
if not x or not types[type(x)] then return ts(x) end
if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end
return id[x]
end
-- test of common types of errors
-- local function c(f,...) return f(...) end
-- local function b(...) return c(...) end
--local function a(...) return (pcall(b,...)) end
local function a(...) local s,e=pcall(...) if s then return s,e else return false,type(e) end end
s = 'some string'
local t = {}
-- error message tests
print( 'a(error)', a(error) )
print( 'a(error,"msg")', a(error,"msg") )
print( 'a(error,"msg",0)', a(error,"msg",0) )
print( 'a(error,"msg",1)', a(error,"msg",1) )
print( 'a(error,"msg",2)', a(error,"msg",2) )
print( 'a(error,"msg",3)', a(error,"msg",3) )
print( 'a(error,"msg",4)', a(error,"msg",4) )
print( 'a(error,"msg",5)', a(error,"msg",5) )
print( 'a(error,"msg",6)', a(error,"msg",6) )
-- call errors
print( 'a(nil())', a(function() return n() end) )
print( 'a(t()) ', a(function() return t() end) )
print( 'a(s()) ', a(function() return s() end) )
print( 'a(true())', a(function() local b = true; return b() end) )
-- arithmetic errors
print( 'a(nil+1)', a(function() return nil+1 end) )
print( 'a(a+1) ', a(function() return a+1 end) )
print( 'a(s+1) ', a(function() return s+1 end) )
print( 'a(true+1)', a(function() local b = true; return b+1 end) )
-- table errors
print( 'a(nil.x)', a(function() return n.x end) )
print( 'a(a.x) ', a(function() return a.x end) )
print( 'a(s.x) ', a(function() return s.x end) )
print( 'a(true.x)', a(function() local b = true; return b.x end) )
print( 'a(nil.x=5)', a(function() n.x=5 end) )
print( 'a(a.x=5) ', a(function() a.x=5 end) )
print( 'a(s.x=5) ', a(function() s.x=5 end) )
print( 'a(true.x=5)', a(function() local b = true; b.x=5 end) )
-- len operator
print( 'a(#nil) ', a(function() return #n end) )
print( 'a(#t) ', a(function() return #t end) )
print( 'a(#s) ', a(function() return #s end) )
print( 'a(#a) ', a(function() return #a end) )
print( 'a(#true)', a(function() local b = true; return #b end) )
-- comparison errors
print( 'a(nil>1)', a(function() return nil>1 end) )
print( 'a(a>1) ', a(function() return a>1 end) )
print( 'a(s>1) ', a(function() return s>1 end) )
print( 'a(true>1)', a(function() local b = true; return b>1 end) )
-- unary minus errors
print( 'a(-nil)', a(function() return -n end) )
print( 'a(-a) ', a(function() return -a end) )
print( 'a(-s) ', a(function() return -s end) )
print( 'a(-true)', a(function() local b = true; return -b end) )
-- string concatenation
local function concatsuite(comparefunc)
print( '"a".."b"', comparefunc("a","b") )
print( '"a"..nil', comparefunc("a",nil) )
print( 'nil.."b"', comparefunc(nil,"b") )
print( '"a"..{}', comparefunc("a",{}) )
print( '{}.."b"', comparefunc({},"b") )
print( '"a"..2', comparefunc("a",2) )
print( '2.."b"', comparefunc(2,"b") )
print( '"a"..print', comparefunc("a",print) )
print( 'print.."b"', comparefunc(print,"b") )
print( '"a"..true', comparefunc("a",true) )
print( 'true.."b"', comparefunc(true,"b") )
print( 'nil..true', comparefunc(nil,true) )
print( '"a"..3.5', comparefunc("a",3.5) )
print( '3.5.."b"', comparefunc(3.5,"b") )
end
local function strconcat(a,b)
return (pcall( function() return a..b end) )
end
local function tblconcat(a,b)
local t={a,b}
return (pcall( function() return table.concat(t,'-',1,2) end ))
end
print( '-------- string concatenation' )
concatsuite(strconcat)
print( '-------- table concatenation' )
concatsuite(tblconcat)
-- pairs
print( '-------- pairs tests' )
print( 'a(pairs(nil))', a(function() return pairs(nil,{}) end) )
print( 'a(pairs(a)) ', a(function() return pairs(a,{}) end) )
print( 'a(pairs(s)) ', a(function() return pairs(s,{}) end) )
print( 'a(pairs(t)) ', a(function() return pairs(t,{}) end) )
print( 'a(pairs(true))', a(function() local b = true; return pairs(b,{}) end) )
-- setmetatable
print( '-------- setmetatable tests' )
function sm(...)
return tostring(setmetatable(...))
end
print( 'a(setmetatable(nil))', a(function() return sm(nil,{}) end) )
print( 'a(setmetatable(a)) ', a(function() return sm(a,{}) end) )
print( 'a(setmetatable(s)) ', a(function() return sm(s,{}) end) )
print( 'a(setmetatable(true))', a(function() local b = true; return sm(b,{}) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
print( 'a(setmetatable(t*)) ', a(function() return sm(t,{__metatable={}}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
t = {}
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
print( 'a(setmetatable(t*)) ', a(function() return sm(t,{__metatable='some string'}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(getmetatable(t)) ', a(function() return getmetatable(t),type(getmetatable(t)) end) )
print( 'a(setmetatable(t,nil)) ', a(function() return sm(t,nil) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t) end) )
print( 'a(setmetatable({},"abc")) ', a(function() return sm({},'abc') end) )
-- bad args to error!
print( 'error("msg","arg")', a(function() error('some message', 'some bad arg') end) )
-- loadfile, dofile on missing files
print( 'loadfile("bogus.txt")', a(function() return loadfile("bogus.txt") end) )
print( 'dofile("bogus.txt")', a(function() return dofile("bogus.txt") end) )

View File

@@ -0,0 +1,97 @@
a(error) false nil
a(error,"msg") false string
a(error,"msg",0) false string
a(error,"msg",1) false string
a(error,"msg",2) false string
a(error,"msg",3) false string
a(error,"msg",4) false string
a(error,"msg",5) false string
a(error,"msg",6) false string
a(nil()) false string
a(t()) false string
a(s()) false string
a(true()) false string
a(nil+1) false string
a(a+1) false string
a(s+1) false string
a(true+1) false string
a(nil.x) false string
a(a.x) false string
a(s.x) true nil
a(true.x) false string
a(nil.x=5) false string
a(a.x=5) false string
a(s.x=5) false string
a(true.x=5) false string
a(#nil) false string
a(#t) true 0
a(#s) true 11
a(#a) false string
a(#true) false string
a(nil>1) false string
a(a>1) false string
a(s>1) false string
a(true>1) false string
a(-nil) false string
a(-a) false string
a(-s) false string
a(-true) false string
-------- string concatenation
"a".."b" true
"a"..nil false
nil.."b" false
"a"..{} false
{}.."b" false
"a"..2 true
2.."b" true
"a"..print false
print.."b" false
"a"..true false
true.."b" false
nil..true false
"a"..3.5 true
3.5.."b" true
-------- table concatenation
"a".."b" true
"a"..nil false
nil.."b" false
"a"..{} false
{}.."b" false
"a"..2 true
2.."b" true
"a"..print false
print.."b" false
"a"..true false
true.."b" false
nil..true false
"a"..3.5 true
3.5.."b" true
-------- pairs tests
a(pairs(nil)) false string
a(pairs(a)) false string
a(pairs(s)) false string
a(pairs(t)) true func.1
a(pairs(true)) false string
-------- setmetatable tests
a(setmetatable(nil)) false string
a(setmetatable(a)) false string
a(setmetatable(s)) false string
a(setmetatable(true)) false string
a(setmetatable(t)) true tbl.2
a(getmetatable(t)) true tbl.3
a(setmetatable(t*)) true tbl.2
a(getmetatable(t)) true tbl.4
a(setmetatable(t)) false string
a(getmetatable(t)) true tbl.4
a(setmetatable(t)) true tbl.5
a(getmetatable(t)) true tbl.6
a(setmetatable(t*)) true tbl.5
a(getmetatable(t)) true some string
a(setmetatable(t)) false string
a(getmetatable(t)) true some string
a(setmetatable(t,nil)) false string
a(setmetatable(t)) false string
a(setmetatable({},"abc")) false string
error("msg","arg") false string
loadfile("bogus.txt") true nil
dofile("bogus.txt") false string

View File

@@ -0,0 +1,74 @@
function f0() print( "f0:" ) end
function f1(a) print( "f1:", a ) end
function f2(a,b) print( "f2:", a, b ) end
function f3(a,b,c) print( "f3:", a, b, c ) end
function f4(a,b,c,d) print( "f4:", a, b, c, d ) end
f0() f0( "a1/1" ) f0( "a1/2", "a2/2" ) f0( "a1/3", "a2/3", "a3/3" ) f0( "a1/4", "a2/4", "a3/4", "a4/4" )
f1() f1( "a1/1" ) f1( "a1/2", "a2/2" ) f1( "a1/3", "a2/3", "a3/3" ) f1( "a1/4", "a2/4", "a3/4", "a4/4" )
f2() f2( "a1/1" ) f2( "a1/2", "a2/2" ) f2( "a1/3", "a2/3", "a3/3" ) f2( "a1/4", "a2/4", "a3/4", "a4/4" )
f3() f3( "a1/1" ) f3( "a1/2", "a2/2" ) f3( "a1/3", "a2/3", "a3/3" ) f3( "a1/4", "a2/4", "a3/4", "a4/4" )
f4() f4( "a1/1" ) f4( "a1/2", "a2/2" ) f4( "a1/3", "a2/3", "a3/3" ) f4( "a1/4", "a2/4", "a3/4", "a4/4" )
function g0(a,b,c,d) return end
function g1(a,b,c,d) return d end
function g2(a,b,c,d) return c, d end
function g3(a,b,c,d) return b, c, d end
function g4(a,b,c,d) return a, b, c, d end
z = g0("c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4")
print( "z0:", z )
z = g2("c2.1/4", "c2.2/4", "c2.3/4", "c2.4/4")
print( "z2:", z )
z = g4("c4.1/4", "c4.2/4", "c4.3/4", "c4.4/4")
print( "z4:", z )
a,b,c,d = g0( "c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4" )
print( "g0:", a, b, c, d, "(eol)" )
a,b,c,d = g2( "b2.1/4", "b2.2/4", "b2.3/4", "b2.4/4" )
print( "g2:", a, b, c, d, "(eol)" )
a,b,c,d = g4( "b4.1/4", "b4.2/4", "b4.3/4", "b4.4/4" )
print( "g4:", a, b, c, d, "(eol)" )
function func(a,b,c)
return a, b, c
end
print( func(11, 12, 13) )
print( func(23, 22, 21) )
print( func(func(32,33,34), func(45,46,47), func(58,59,50)) )
function p(a,...)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
p()
p("q")
p("q","r")
p("q","r","s")
-- tail call tests
function first(...)
return 'abc', ..., '|', ...
end
function second(a,...)
return 'def', ..., '|', a, ...
end
function third( a, b, c )
print( 'third', first( a, b, c ) )
print( 'third', second( a, b, c ) )
return second( a, b, c )
end
print( 'third', third() )
print( 'third', third('p') )
print( 'third', third('p','q') )
print( 'third', third('p','q','r') )
print( 'third', third('p','q','r','s') )
print( 'third', third() )

View File

@@ -0,0 +1,68 @@
f0:
f0:
f0:
f0:
f0:
f1: nil
f1: a1/1
f1: a1/2
f1: a1/3
f1: a1/4
f2: nil nil
f2: a1/1 nil
f2: a1/2 a2/2
f2: a1/3 a2/3
f2: a1/4 a2/4
f3: nil nil nil
f3: a1/1 nil nil
f3: a1/2 a2/2 nil
f3: a1/3 a2/3 a3/3
f3: a1/4 a2/4 a3/4
f4: nil nil nil nil
f4: a1/1 nil nil nil
f4: a1/2 a2/2 nil nil
f4: a1/3 a2/3 a3/3 nil
f4: a1/4 a2/4 a3/4 a4/4
z0: nil
z2: c2.3/4
z4: c4.1/4
g0: nil nil nil nil (eol)
g2: b2.3/4 b2.4/4 nil nil (eol)
g4: b4.1/4 b4.2/4 b4.3/4 b4.4/4 (eol)
11 12 13
23 22 21
32 45 58
a nil
...
...,a nil nil
a,... nil
a q
...
...,a nil q
a,... q
a q
... r
...,a r q
a,... q r
a q
... r s
...,a r q
a,... q r s
third abc nil | nil nil nil
third def nil | nil nil nil
third def nil | nil nil nil
third abc p | p nil nil
third def nil | p nil nil
third def nil | p nil nil
third abc p | p q nil
third def q | p q nil
third def q | p q nil
third abc p | p q r
third def q | p q r
third def q | p q r
third abc p | p q r
third def q | p q r
third def q | p q r
third abc nil | nil nil nil
third def nil | nil nil nil
third def nil | nil nil nil

View File

@@ -0,0 +1,153 @@
local platform = ...
--print( 'platform', platform )
-- simple io-library tests
--
-- C version on Windows will add change \n into \r\n for text files at least
--
local tostr,files,nfiles = tostring,{},0
tostring = function(x)
local s = tostr(x)
if s:sub(1,4) ~= 'file' then return s end
if files[s] then return files[s] end
files[s] = 'file.'..nfiles
nfiles = nfiles + 1
return files[s]
end
print( io ~= nil )
print( io.open ~= nil )
print( io.stdin ~= nil )
print( io.stdout ~= nil )
print( io.stderr ~= nil )
print( 'write', io.write() )
print( 'write', io.write("This") )
print( 'write', io.write(" is a pen.") )
print( 'flush', io.flush() )
local f = io.open("abc.txt","w")
print( 'f', type(f) )
print( io.type(f) )
print( 'write', f:write("abcdef 12345 \t\t 678910 more\aaaaaaa\bbbbthe rest") )
print( 'type(f)', io.type(f) )
print( 'close', f:close() )
print( 'type(f)', io.type(f) )
print( 'type("f")', io.type("f") )
local g = io.open("abc.txt","r")
local t = { g:read(3, 3, "*n", "*n", "*l", "*l", "*a") }
for i,v in ipairs(t) do
print( string.format("%q",tostring(v)), type(v))
print( '----- ', i )
end
local h,s = io.open("abc.txt", "a")
print( 'h', io.type(h), string.sub(tostring(h),1,6), s )
print( 'write', h:write('and more and more and more text.') )
print( 'close', h:close() )
if platform ~= 'JME' then
local j = io.open( "abc.txt", "r" )
print( 'j', io.type(j) )
print( 'seek', j:seek("set", 3) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("set", 2) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("cur", -8 ) )
print( 'read', j:read(4), j:read(3) )
print( 'seek(cur,0)', j:seek("cur",0) )
print( 'seek(cur,20)', j:seek("cur",20) )
print( 'seek(end,-5)', j:seek("end", -5) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'close', j:close() )
end
-- write a few lines, including a non-terminating one
files = {}
f = io.open("abc.txt","w")
print( 'f.type', io.type(f) )
print( 'f', f )
print( 'write', f:write("line one\nline two\n\nafter blank line\nunterminated line") )
print( 'type(f)', io.type(f) )
print( 'close', f:close() )
files = {}
-- read using io.lines()
for l in io.lines("abc.txt") do
print( string.format('%q',l) )
end
io.input("abc.txt")
for l in io.lines() do
print( string.format('%q',l) )
end
io.input(io.open("abc.txt","r"))
for l in io.lines() do
print( string.format('%q',l) )
end
io.input("abc.txt")
io.input(io.input())
for l in io.lines() do
print( string.format('%q',l) )
end
local count = 0
io.tmpfile = function()
count = count + 1
return io.open("tmp"..count..".out","w")
end
local a = io.tmpfile()
local b = io.tmpfile()
print( io.type(a) )
print( io.type(b) )
print( "a:write", a:write('aaaaaaa') )
print( "b:write", b:write('bbbbbbb') )
print( "a:setvbuf", a:setvbuf("no") )
print( "a:setvbuf", a:setvbuf("full",1024) )
print( "a:setvbuf", a:setvbuf("line") )
print( "a:write", a:write('ccccc') )
print( "b:write", b:write('ddddd') )
print( "a:flush", a:flush() )
print( "b:flush", b:flush() )
--[[
print( "a:read", a:read(7) )
print( "b:read", b:read(7) )
print( "a:seek", a:seek("cur",-4) )
print( "b:seek", b:seek("cur",-4) )
print( "a:read", ( a:read(7) ) )
print( "b:read", ( b:read(7) ) )
print( "a:seek", a:seek("cur",-8) )
print( "b:seek", b:seek("cur",-8) )
print( "a:read", ( a:read(7) ) )
print( "b:read", ( b:read(7) ) )
--]]
local pcall = function(...)
local s,e = pcall(...)
if s then return s end
return s,e:match("closed")
end
print( 'a:close', pcall( a.close, a ) )
print( 'a:write', pcall( a.write, a, 'eee') )
print( 'a:flush', pcall( a.flush, a) )
print( 'a:read', pcall( a.read, a, 5) )
print( 'a:lines', pcall( a.lines, a) )
print( 'a:seek', pcall( a.seek, a, "cur", -2) )
print( 'a:setvbuf', pcall( a.setvbuf, a, "no") )
print( 'a:close', pcall( a.close, a ) )
print( 'io.type(a)', pcall( io.type, a ) )
print( 'io.close()', pcall( io.close ) )
print( 'io.close(io.output())', pcall( io.close, io.output() ) )
io.output('abc.txt')
print( 'io.close()', pcall( io.close ) )
print( 'io.write', pcall( io.write, 'eee') )
print( 'io.flush', pcall( io.flush) )
print( 'io.close', pcall( io.close ) )
io.input('abc.txt'):close()
print( 'io.read', pcall( io.read, 5) )
print( 'io.lines', pcall( io.lines) )

View File

@@ -0,0 +1,96 @@
true
true
true
true
true
write file.0
Thiswrite file.0
is a pen.write file.0
flush true
f userdata
file.1
write file.2
type(f) file.1
close true
type(f) closed file
type("f") nil
"abc" string
----- 1
"def" string
----- 2
"12345" number
----- 3
"678910" number
----- 4
" more\7aaaaaa\8bbbthe rest" string
----- 5
h file.1 file.4 nil
write file.3
close true
j file.1
seek 3
read def 123
seek 2
read cdef 12
seek 1
read bcde f 1
seek(cur,0) 8
seek(cur,20) 28
seek(end,-5) 73
read(4) "text"
read(4) "."
read(4) "nil"
close true
f.type file.5
f file.6
write file.6
type(f) file.5
close true
"line one"
"line two"
""
"after blank line"
"unterminated line"
"line one"
"line two"
""
"after blank line"
"unterminated line"
"line one"
"line two"
""
"after blank line"
"unterminated line"
"line one"
"line two"
""
"after blank line"
"unterminated line"
file.7
file.7
a:write file.8
b:write file.9
a:setvbuf true
a:setvbuf true
a:setvbuf true
a:write file.8
b:write file.9
a:flush true
b:flush true
a:close true
a:write false closed
a:flush false closed
a:read false closed
a:lines false closed
a:seek false closed
a:setvbuf false closed
a:close false closed
io.type(a) true
io.close() true
io.close(io.output()) true
io.close() true
io.write false closed
io.flush false closed
io.close false closed
io.read false closed
io.lines false closed

View File

@@ -0,0 +1,321 @@
has debug true
----- debug.getlocal, debug.setlocal
true h-3-0 -> 3-0 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={3,0,#} f locals=,3,0,#,4,5,6
true h-3-1 -> 3-1 get=a,3 set=a,nil get=a,# g locals=7,8,9 tbl={3,1,#} f locals=,#,1,#,4,5,6
true h-3-2 -> 3-2 get=b,2 set=b,nil get=b,# g locals=7,8,9 tbl={3,2,#} f locals=,3,#,#,4,5,6
true h-3-3 -> 3-3 get=c,# set=c,nil get=c,# g locals=7,8,9 tbl={3,3,#} f locals=,3,3,#,4,5,6
true h-3-4 -> 3-4 get=d,4 set=d,nil get=d,# g locals=7,8,9 tbl={3,4,#} f locals=,3,4,#,#,5,6
true h-3-5 -> 3-5 get=e,5 set=e,nil get=e,# g locals=7,8,9 tbl={3,5,#} f locals=,3,5,#,4,#,6
true h-3-6 -> 3-6 get=f,6 set=f,nil get=f,# g locals=7,8,9 tbl={3,6,#} f locals=,3,6,#,4,5,#
true h-3-7 -> 3-7 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={3,7,#} f locals=,3,7,#,4,5,6
true h-2-0 -> 2-0 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,0,#} f locals=,2,0,#,4,5,6
true h-2-1 -> 2-1 get=p,7 set=p,nil get=p,# g locals=#,8,9 tbl={2,1,#} f locals=,2,1,#,4,5,6
true h-2-2 -> 2-2 get=q,8 set=q,nil get=q,# g locals=7,#,9 tbl={2,2,#} f locals=,2,2,#,4,5,6
true h-2-3 -> 2-3 get=r,9 set=r,nil get=r,# g locals=7,8,# tbl={2,3,#} f locals=,2,3,#,4,5,6
true h-2-4 -> 2-4 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,4,#} f locals=,2,4,#,4,5,6
true h-2-5 -> 2-5 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,5,#} f locals=,2,5,#,4,5,6
true h-2-6 -> 2-6 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,6,#} f locals=,2,6,#,4,5,6
true h-2-7 -> 2-7 get=nil,nil set=nil,nil get=nil,nil g locals=7,8,9 tbl={2,7,#} f locals=,2,7,#,4,5,6
true h-1-3 -> 1-3 get=n,# set=n,nil get=n,# g locals=7,8,9 tbl={1,3,#} f locals=,1,3,#,4,5,6
true h-1-4 -> 1-4 get=#,nil set=x1,nil get=x1,# g locals=7,8,9 tbl={1,4,#} f locals=,1,4,#,4,5,6
true h-1-5 -> 1-5 get=nil,# set=y1,nil get=y1,# g locals=7,8,9 tbl={1,5,#} f locals=,1,5,#,4,5,6
true h-1-6 -> 1-6 get=nil,nil set=nil,nil get=x2,nil g locals=7,8,9 tbl={1,6,#} f locals=,1,6,#,4,5,6
true h-1-7 -> 1-7 get=nil,nil set=nil,nil get=y2,nil g locals=7,8,9 tbl={1,7,#} f locals=,1,7,#,4,5,6
----- debug.getupvalue, debug.setupvalue
h 101 102 103 104 105 106 107 108 109
f -> 1-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,101,102,103,104,105,106,107,108,109
f -> 1-1 get=true,m,101 set=true,m,nil get=true,m,777001 tbl=true,777001,102,103,104,105,106,107,108,109
f -> 1-2 get=true,n,102 set=true,n,nil get=true,n,777002 tbl=true,777001,777002,103,104,105,106,107,108,109
f -> 1-3 get=true,o,103 set=true,o,nil get=true,o,777003 tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-4 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-5 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-6 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-7 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-8 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-9 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
f -> 1-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
g -> 2-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,777001,777002,777003,104,105,106,107,108,109
g -> 2-1 get=true,m,777001 set=true,m,nil get=true,m,888001 tbl=true,888001,777002,777003,104,105,106,107,108,109
g -> 2-2 get=true,n,777002 set=true,n,nil get=true,n,888002 tbl=true,888001,888002,777003,104,105,106,107,108,109
g -> 2-3 get=true,o,777003 set=true,o,nil get=true,o,888003 tbl=true,888001,888002,888003,104,105,106,107,108,109
g -> 2-4 get=true,p,104 set=true,p,nil get=true,p,888004 tbl=true,888001,888002,888003,888004,105,106,107,108,109
g -> 2-5 get=true,q,105 set=true,q,nil get=true,q,888005 tbl=true,888001,888002,888003,888004,888005,106,107,108,109
g -> 2-6 get=true,r,106 set=true,r,nil get=true,r,888006 tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-7 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-8 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-9 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
g -> 2-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
h -> 3-0 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,888001,888002,888003,888004,888005,888006,107,108,109
h -> 3-1 get=true,m,888001 set=true,m,nil get=true,m,999001 tbl=true,999001,888002,888003,888004,888005,888006,107,108,109
h -> 3-2 get=true,n,888002 set=true,n,nil get=true,n,999002 tbl=true,999001,999002,888003,888004,888005,888006,107,108,109
h -> 3-3 get=true,o,888003 set=true,o,nil get=true,o,999003 tbl=true,999001,999002,999003,888004,888005,888006,107,108,109
h -> 3-4 get=true,p,888004 set=true,p,nil get=true,p,999004 tbl=true,999001,999002,999003,999004,888005,888006,107,108,109
h -> 3-5 get=true,q,888005 set=true,q,nil get=true,q,999005 tbl=true,999001,999002,999003,999004,999005,888006,107,108,109
h -> 3-6 get=true,r,888006 set=true,r,nil get=true,r,999006 tbl=true,999001,999002,999003,999004,999005,999006,107,108,109
h -> 3-7 get=true,v,107 set=true,v,nil get=true,v,999007 tbl=true,999001,999002,999003,999004,999005,999006,999007,108,109
h -> 3-8 get=true,w,108 set=true,w,nil get=true,w,999008 tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,109
h -> 3-9 get=true,x,109 set=true,x,nil get=true,x,999009 tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,999009
h -> 3-10 get=true,nil,nil set=true,nil,nil get=true,nil,nil tbl=true,999001,999002,999003,999004,999005,999006,999007,999008,999009
----- debug.setmetatable, debug.getmetatable
a.a=bbb a.b=nil b.a=nil b.b=nil
a.a=bbb a.b=ccc b.a=nil b.b=nil
boolean table nil table
boolean nil nil nil
boolean boolean nil nil
a.a=bbb a.b=nil b.a=nil b.b=nil
boolean nil nil nil
get=true,true,nil
set=true,false,nil
get=true,true,nil
get=true,true,nil
set=true,false,nil
get=true,true,nil
true nil
true true
true true
----- debug.getinfo
6
---
debug.getinfo(1)
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: 144
linedefined: 141
lastlinedefined: 155
nups: 4
func: function
debug.getinfo(1,"")
debug.getinfo(1,"l")
currentline: 146
debug.getinfo(1,"fL")
func: function
activelines: {}
debug.getinfo(2)
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: 157
linedefined: 135
lastlinedefined: 159
nups: 5
func: function
debug.getinfo(2,"l")
currentline: 157
debug.getinfo(2,"fL")
func: function
activelines: {}
debug.getinfo(10,"")
true
debug.getinfo(-10,"")
true
---
5
e,f,g true function function
debug.getinfo(f)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 137
lastlinedefined: 140
nups: 1
func: function
debug.getinfo(f,"nSlufL")
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 137
lastlinedefined: 140
nups: 1
func: function
activelines: {}
debug.getinfo(f,"n")
true
debug.getinfo(f,"S")
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
linedefined: 137
lastlinedefined: 140
debug.getinfo(f,"l")
true
currentline: -1
debug.getinfo(f,"u")
true
nups: 1
debug.getinfo(f,"f")
true
func: function
debug.getinfo(f,"L")
true
activelines: {}
debug.getinfo(g)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 141
lastlinedefined: 155
nups: 4
func: function
debug.getinfo(test)
true
source: @debuglib.lua
short_src: debuglib.lua
what: Lua
currentline: -1
linedefined: 135
lastlinedefined: 159
nups: 5
func: function
----- debug.sethook, debug.gethook
... in hook call nil
info[2]=debuglib.lua,174
... in hook call nil
info[2]=debuglib.lua,175
... in hook call nil
info[2]=[C],-1
... in hook call nil
info[2]=[C],-1
hook = c -> result=true,nil,nil,nil,false,false,nil
hook = r -> result=true,nil,nil,nil,false,false,nil
... in hook line 192
info[2]=debuglib.lua,192
... in hook line 177
info[2]=debuglib.lua,177
... in hook line 178
info[2]=debuglib.lua,178
... in hook line 176
info[2]=debuglib.lua,176
... in hook line 195
info[2]=debuglib.lua,195
hook = l -> result=true,nil,nil,nil,false,false,nil
... in hook line 192
info[2]=debuglib.lua,192
... in hook call nil
info[2]=debuglib.lua,174
... in hook line 177
info[2]=debuglib.lua,177
... in hook line 178
info[2]=debuglib.lua,178
... in hook call nil
info[2]=debuglib.lua,175
... in hook line 176
info[2]=debuglib.lua,176
... in hook call nil
info[2]=[C],-1
... in hook line 195
info[2]=debuglib.lua,195
... in hook call nil
info[2]=[C],-1
hook = crl -> result=true,nil,nil,nil,false,false,nil
----- debug.traceback
hi
stack traceback:
debuglib.lua:216: in function <debuglib.lua:215>
[C]: in function 'pcall'
debuglib.lua:219: in function 'b'
debuglib.lua:222: in function 'c'
debuglib.lua:225: in function '__index'
debuglib.lua:227: in function 'e'
debuglib.lua:231: in function 'g'
debuglib.lua:235: in function 'i'
debuglib.lua:238: in function <debuglib.lua:214>
[C]: in function 'pcall'
debuglib.lua:240: in main chunk
[C]: at 0x55eaaf4e2f20
----- debug.upvalueid
debug.getupvalue(a1,1) x 100
debug.getupvalue(a1,2) y 200
debug.getupvalue(a2,1) x 100
debug.getupvalue(a2,2) y 200
debug.upvalueid(a1,1) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a1,2) false
----- debug.upvaluejoin
a1 101 201 301 401
a2 102 202 501 601
debug.upvaluejoin(a1,1,a2,2)
debug.upvaluejoin(a1,3,a2,4)
a1 203 203 602 402
a2 103 204 502 603
a1 205 205 604 403
a2 104 206 503 605
debug.getupvalue(a1,1) x 206
debug.getupvalue(a2,1) x 104
debug.upvalueid(a1,1) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,1) true
debug.upvalueid(a1,1) == debug.upvalueid(a1,2) true
debug.upvalueid(a1,1) == debug.upvalueid(a2,2) true
debug.upvalueid(a2,1) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,1) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,1) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,1) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,1) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,1) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,1) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,2) y 206
debug.getupvalue(a2,2) y 206
debug.upvalueid(a1,2) == debug.upvalueid(a1,1) true
debug.upvalueid(a1,2) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,1) true
debug.upvalueid(a2,2) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,2) == debug.upvalueid(a1,2) true
debug.upvalueid(a1,2) == debug.upvalueid(a2,2) true
debug.upvalueid(a2,2) == debug.upvalueid(a1,2) true
debug.upvalueid(a2,2) == debug.upvalueid(a2,2) true
debug.upvalueid(a1,2) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,2) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,2) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,2) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,2) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,2) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,2) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,3) z 605
debug.getupvalue(a2,3) z 503
debug.upvalueid(a1,3) == debug.upvalueid(a1,1) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,3) == debug.upvalueid(a1,2) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,2) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,3) == debug.upvalueid(a1,3) true
debug.upvalueid(a1,3) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,3) == debug.upvalueid(a1,3) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,3) true
debug.upvalueid(a1,3) == debug.upvalueid(a1,4) false
debug.upvalueid(a1,3) == debug.upvalueid(a2,4) true
debug.upvalueid(a2,3) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,3) == debug.upvalueid(a2,4) false
debug.getupvalue(a1,4) w 403
debug.getupvalue(a2,4) w 605
debug.upvalueid(a1,4) == debug.upvalueid(a1,1) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,1) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,1) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,1) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,2) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,2) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,2) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,2) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,3) false
debug.upvalueid(a1,4) == debug.upvalueid(a2,3) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,3) true
debug.upvalueid(a2,4) == debug.upvalueid(a2,3) false
debug.upvalueid(a1,4) == debug.upvalueid(a1,4) true
debug.upvalueid(a1,4) == debug.upvalueid(a2,4) false
debug.upvalueid(a2,4) == debug.upvalueid(a1,4) false
debug.upvalueid(a2,4) == debug.upvalueid(a2,4) true

View File

@@ -0,0 +1,37 @@
local t = {}
local template = [[
local f<i>
do
local result
function f<i>()
if not result then
result = f<i-2>() + f<i-1>()
end
return result
end
end
]]
t[1] = [[
local f1
f1 = function() return 1 end
]]
t[2] = [[
local f2
f2 = function() return 1 end
]]
for i = 3, 199 do
t[i] = template:gsub("<([^>]+)>", function(s)
local c = assert(load('return '..s, 'f'..i, 'bt', { i = i }), 'could not compile: '..s)
return c()
end)
end
t[200] = [[
print("5th fibonacci number is", f5())
print("10th fibonacci number is", f10())
print("199th fibonacci number is", f199())
]]
local s = table.concat(t)
print(s)
f = load(s)
f()

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,239 @@
local platform = ...
--print( 'platform', platform )
local aliases = {
['0']='<zero>',
['-0']='<zero>',
['nan']='<nan>',
['inf']='<pos-inf>',
['-inf']='<neg-inf>',
['1.#INF']='<pos-inf>',
['-1.#INF']='<neg-inf>',
['1.#IND']='<nan>',
['-1.#IND']='<nan>',
}
local UNOPVALUES = { -2.5, -2, 0, 2, 2.5, "'-2.5'", "'-2'", "'0'", "'2'", "'2.5'" }
local NUMBERPAIRS = {
{ 2, 0 }, { -2.5, 0 }, { 2, 1 },
{ 5, 2 }, {-5, 2 }, {16, 2}, {-16, -2},
{ .5, 0}, {.5, 1}, {.5, 2}, {.5, -1}, {.5, 2},
{2.25, 0}, {2.25, 2}, {-2, 0},
{ 3, 3 },
}
local STRINGPAIRS = {
{ "'2'", "'0'" }, { "'2.5'","'3'" }, { "'-2'", "'1.5'" }, { "'-2.5'", "'-1.5'" },
{ "'3.0'", "'3.0'" }, { 2.75, 2.75 }, { "'2.75'", "'2.75'" },
}
local MIXEDPAIRS = {
{ 3, "'3'" }, { "'3'", 3 }, { 2.75, "'2.75'" }, { "'2.75'", 2.75 },
{ -3, "'-4'" }, { "'-3'", 4 }, { -3, "'4'" }, { "'-3'", -4 },
{ -4.75, "'2.75'" }, { "'-2.75'", 1.75 }, { 4.75, "'-2.75'" }, { "'2.75'", -1.75 },
}
local BINOPVALUES = {}
local RELATIONALOPVALUES = {}
local function addall( t, s )
for i,v in ipairs(s) do
t[#t+1] = v
end
end
addall( BINOPVALUES, NUMBERPAIRS )
addall( BINOPVALUES, STRINGPAIRS )
addall( BINOPVALUES, MIXEDPAIRS )
addall( RELATIONALOPVALUES, NUMBERPAIRS )
addall( RELATIONALOPVALUES, STRINGPAIRS )
local VARARGSVALUES = {
{ 4, }, { -4.5 }, { "'5.5'" }, { "'-5'" },
{ 4, "'8'" }, { -4.5, "'-8'" }, { "'5.5'", 2.2 }, { "'-5'", -2.2 },
{ 111,222,333 }, { -222,-333,-111 }, { 444,-111,-222 },
}
local CONSTANTS = {
"huge", "pi",
}
local UNARYOPS = {
"-", "not ",
}
local BINARYOPS = {
"+", "-", "*", "^", "/", "%",
}
local RELATIONALS = {
"==", "~=", ">", "<", ">=", "<=",
}
local ONEARG_JME = {
"abs", "ceil", "cos", "deg",
"exp", "floor", "frexp", "modf",
"rad", "sin", "sqrt", "tan",
}
local ONEARG_JSE = {
"acos", "asin", "atan", "cosh",
"log", "sinh", "tanh",
}
local TWOARGS_JME = {
"fmod", "ldexp", "pow",
}
local TWOARGS_JSE = {
"atan2",
}
local VARARGSFUNCS = {
"max", "min",
}
local ts = tostring
tostring = function(x)
local s = ts(x)
if type(x)~='number' then return s end
if aliases[s] then return aliases[s] end
if #s < 7 then return s end
local a,b = string.match(s,'([%-0-9%.]*)([eE]?[%-0-9]*)')
return a and (string.sub(a,1,6)..(b or '')) or s
end
local function eval( expr, script )
script = script or ('return '..expr)
local s,a,b = load( script, 'expr' )
if s then print( expr, pcall( s ) )
else print( expr, 'load:', a ) end
end
-- misc tests
print( '---------- miscellaneous tests ----------' )
eval( 'math.sin( 0.0 )' )
eval( 'math.cos( math.pi )' )
eval( 'math.sqrt( 9.0 )' )
eval( 'math.modf( 5.25 )')
eval( 'math.frexp(0.00625)' )
eval( '-5 ^ 2' )
eval( '-5 / 2' )
eval( '-5 % 2' )
-- constants
print( '---------- constants ----------' )
for i,v in ipairs(CONSTANTS) do
eval( 'math.'..v )
end
-- unary operators
for i,v in ipairs(UNARYOPS) do
print( '---------- unary operator '..v..' ----------' )
for j,a in ipairs(UNOPVALUES) do
eval( v..a, 'return '..v..a )
end
end
-- binary operators
for i,v in ipairs(BINARYOPS) do
print( '---------- binary operator '..v..' ----------' )
for j,xy in ipairs(BINOPVALUES) do
eval( xy[1]..v..xy[2],
'local x,y='..xy[1]..','..xy[2]..'; return x'..v..'y' )
end
end
-- relational operators
for i,v in ipairs(RELATIONALS) do
print( '---------- binary operator '..v..' ----------' )
for j,xy in ipairs(RELATIONALOPVALUES) do
eval( xy[1]..v..xy[2],
'local x,y='..xy[1]..','..xy[2]..'; return x'..v..'y' )
end
end
-- one-argument math functions
for i,v in ipairs(ONEARG_JME) do
print( '---------- math.'..v..' ----------' )
for j,x in ipairs(UNOPVALUES) do
eval( 'math.'..v..'('..x..')' )
end
end
if platform ~= 'JME' then
for i,v in ipairs(ONEARG_JSE) do
print( '---------- math.'..v..' (jse only) ----------' )
for j,x in ipairs(UNOPVALUES) do
eval( 'math.'..v..'('..x..')' )
end
end
end
-- two-argument math functions
for i,v in ipairs(TWOARGS_JME) do
print( '---------- math.'..v..' ----------' )
for j,x in ipairs(BINOPVALUES) do
eval( 'math.'..v..'('..x[1]..','..x[2]..')' )
end
end
if platform ~= 'JME' then
for i,v in ipairs(TWOARGS_JSE) do
print( '---------- math.'..v..' (jse only) ----------' )
for j,x in ipairs(BINOPVALUES) do
eval( 'math.'..v..'('..x[1]..','..x[2]..')' )
end
end
end
-- var-arg math functions
for i,v in ipairs(VARARGSFUNCS) do
print( '---------- math.'..v..' ----------' )
for j,x in ipairs(VARARGSVALUES) do
eval( 'math.'..v..'('..table.concat(x,',')..')' )
end
end
-- random tests
print("----------- Random number tests")
local function testrandom(string,lo,hi)
local c,e = load('return '..string)
for i=1,5 do
local s,e = pcall(c)
if s then
print( string, s and type(e) or e, (e>=lo) and (e<=hi) )
else
print( string, 'error', e )
end
end
end
testrandom('math.random()',0,1)
testrandom('math.random(5,10)',5,10)
testrandom('math.random(30)',0,30)
testrandom('math.random(-4,-2)',-4,-2)
local t = {}
print( math.randomseed(20) )
for i=1,20 do
t[i] = math.random()
end
print( '-- comparing new numbers')
for i=1,20 do
print( t[i] == math.random(), t[i] == t[0] )
end
print( '-- resetting seed')
print( math.randomseed(20) )
for i=1,20 do
print( t[i] == math.random() )
end
-- tests involving -0, which is folded into 0 for luaj, but not for plain lua
print("----------- Tests involving -0 and NaN")
print('0 == -0', 0 == -0)
t = {[0] = 10, 20, 30, 40, 50}
print('t[-0] == t[0]',t[-0] == t[0])
local x = -1
local mz, z = 0/x, 0 -- minus zero, zero
print('mz, z', mz, z)
print('mz == z', mz == z)
local a = {[mz] = 1}
print('a[z] == 1 and a[mz] == 1', a[z] == 1 and a[mz] == 1)
-- string with same binary representation as 0.0 (may create problems
-- for constant manipulation in the pre-compiler)
local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0"
assert(a1 == a2 and a2 == a4 and a1 ~= a3)
assert(a3 == a5)
--]]

View File

@@ -0,0 +1,842 @@
---------- miscellaneous tests ----------
math.sin( 0.0 ) true <zero>
math.cos( math.pi ) true -1
math.sqrt( 9.0 ) true 3
math.modf( 5.25 ) true 5 0.25
math.frexp(0.00625) true 0.8 -7
-5 ^ 2 true -25
-5 / 2 true -2.5
-5 % 2 true 1
---------- constants ----------
math.huge true <pos-inf>
math.pi true 3.1415
---------- unary operator - ----------
--2.5 true
--2 true
-0 true <zero>
-2 true -2
-2.5 true -2.5
-'-2.5' true 2.5
-'-2' true 2
-'0' true <zero>
-'2' true -2
-'2.5' true -2.5
---------- unary operator not ----------
not -2.5 true false
not -2 true false
not 0 true false
not 2 true false
not 2.5 true false
not '-2.5' true false
not '-2' true false
not '0' true false
not '2' true false
not '2.5' true false
---------- binary operator + ----------
2+0 true 2
-2.5+0 true -2.5
2+1 true 3
5+2 true 7
-5+2 true -3
16+2 true 18
-16+-2 true -18
0.5+0 true 0.5
0.5+1 true 1.5
0.5+2 true 2.5
0.5+-1 true -0.5
0.5+2 true 2.5
2.25+0 true 2.25
2.25+2 true 4.25
-2+0 true -2
3+3 true 6
'2'+'0' true 2
'2.5'+'3' true 5.5
'-2'+'1.5' true -0.5
'-2.5'+'-1.5' true -4
'3.0'+'3.0' true 6
2.75+2.75 true 5.5
'2.75'+'2.75' true 5.5
3+'3' true 6
'3'+3 true 6
2.75+'2.75' true 5.5
'2.75'+2.75 true 5.5
-3+'-4' true -7
'-3'+4 true 1
-3+'4' true 1
'-3'+-4 true -7
-4.75+'2.75' true -2
'-2.75'+1.75 true -1
4.75+'-2.75' true 2
'2.75'+-1.75 true 1
---------- binary operator - ----------
2-0 true 2
-2.5-0 true -2.5
2-1 true 1
5-2 true 3
-5-2 true -7
16-2 true 14
-16--2 true -14
0.5-0 true 0.5
0.5-1 true -0.5
0.5-2 true -1.5
0.5--1 true 1.5
0.5-2 true -1.5
2.25-0 true 2.25
2.25-2 true 0.25
-2-0 true -2
3-3 true <zero>
'2'-'0' true 2
'2.5'-'3' true -0.5
'-2'-'1.5' true -3.5
'-2.5'-'-1.5' true -1
'3.0'-'3.0' true <zero>
2.75-2.75 true <zero>
'2.75'-'2.75' true <zero>
3-'3' true <zero>
'3'-3 true <zero>
2.75-'2.75' true <zero>
'2.75'-2.75 true <zero>
-3-'-4' true 1
'-3'-4 true -7
-3-'4' true -7
'-3'--4 true 1
-4.75-'2.75' true -7.5
'-2.75'-1.75 true -4.5
4.75-'-2.75' true 7.5
'2.75'--1.75 true 4.5
---------- binary operator * ----------
2*0 true <zero>
-2.5*0 true <zero>
2*1 true 2
5*2 true 10
-5*2 true -10
16*2 true 32
-16*-2 true 32
0.5*0 true <zero>
0.5*1 true 0.5
0.5*2 true 1
0.5*-1 true -0.5
0.5*2 true 1
2.25*0 true <zero>
2.25*2 true 4.5
-2*0 true <zero>
3*3 true 9
'2'*'0' true <zero>
'2.5'*'3' true 7.5
'-2'*'1.5' true -3
'-2.5'*'-1.5' true 3.75
'3.0'*'3.0' true 9
2.75*2.75 true 7.5625
'2.75'*'2.75' true 7.5625
3*'3' true 9
'3'*3 true 9
2.75*'2.75' true 7.5625
'2.75'*2.75 true 7.5625
-3*'-4' true 12
'-3'*4 true -12
-3*'4' true -12
'-3'*-4 true 12
-4.75*'2.75' true -13.06
'-2.75'*1.75 true -4.812
4.75*'-2.75' true -13.06
'2.75'*-1.75 true -4.812
---------- binary operator ^ ----------
2^0 true 1
-2.5^0 true 1
2^1 true 2
5^2 true 25
-5^2 true 25
16^2 true 256
-16^-2 true 0.0039
0.5^0 true 1
0.5^1 true 0.5
0.5^2 true 0.25
0.5^-1 true 2
0.5^2 true 0.25
2.25^0 true 1
2.25^2 true 5.0625
-2^0 true 1
3^3 true 27
'2'^'0' true 1
'2.5'^'3' true 15.625
'-2'^'1.5' true <nan>
'-2.5'^'-1.5' true <nan>
'3.0'^'3.0' true 27
2.75^2.75 true 16.149
'2.75'^'2.75' true 16.149
3^'3' true 27
'3'^3 true 27
2.75^'2.75' true 16.149
'2.75'^2.75 true 16.149
-3^'-4' true 0.0123
'-3'^4 true 81
-3^'4' true 81
'-3'^-4 true 0.0123
-4.75^'2.75' true <nan>
'-2.75'^1.75 true <nan>
4.75^'-2.75' true 0.0137
'2.75'^-1.75 true 0.1702
---------- binary operator / ----------
2/0 true <pos-inf>
-2.5/0 true <neg-inf>
2/1 true 2
5/2 true 2.5
-5/2 true -2.5
16/2 true 8
-16/-2 true 8
0.5/0 true <pos-inf>
0.5/1 true 0.5
0.5/2 true 0.25
0.5/-1 true -0.5
0.5/2 true 0.25
2.25/0 true <pos-inf>
2.25/2 true 1.125
-2/0 true <neg-inf>
3/3 true 1
'2'/'0' true <pos-inf>
'2.5'/'3' true 0.8333
'-2'/'1.5' true -1.333
'-2.5'/'-1.5' true 1.6666
'3.0'/'3.0' true 1
2.75/2.75 true 1
'2.75'/'2.75' true 1
3/'3' true 1
'3'/3 true 1
2.75/'2.75' true 1
'2.75'/2.75 true 1
-3/'-4' true 0.75
'-3'/4 true -0.75
-3/'4' true -0.75
'-3'/-4 true 0.75
-4.75/'2.75' true -1.727
'-2.75'/1.75 true -1.571
4.75/'-2.75' true -1.727
'2.75'/-1.75 true -1.571
---------- binary operator % ----------
2%0 true <nan>
-2.5%0 true <nan>
2%1 true <zero>
5%2 true 1
-5%2 true 1
16%2 true <zero>
-16%-2 true <zero>
0.5%0 true <nan>
0.5%1 true 0.5
0.5%2 true 0.5
0.5%-1 true -0.5
0.5%2 true 0.5
2.25%0 true <nan>
2.25%2 true 0.25
-2%0 true <nan>
3%3 true <zero>
'2'%'0' true <nan>
'2.5'%'3' true 2.5
'-2'%'1.5' true 1
'-2.5'%'-1.5' true -1
'3.0'%'3.0' true <zero>
2.75%2.75 true <zero>
'2.75'%'2.75' true <zero>
3%'3' true <zero>
'3'%3 true <zero>
2.75%'2.75' true <zero>
'2.75'%2.75 true <zero>
-3%'-4' true -3
'-3'%4 true 1
-3%'4' true 1
'-3'%-4 true -3
-4.75%'2.75' true 0.75
'-2.75'%1.75 true 0.75
4.75%'-2.75' true -0.75
'2.75'%-1.75 true -0.75
---------- binary operator == ----------
2==0 true false
-2.5==0 true false
2==1 true false
5==2 true false
-5==2 true false
16==2 true false
-16==-2 true false
0.5==0 true false
0.5==1 true false
0.5==2 true false
0.5==-1 true false
0.5==2 true false
2.25==0 true false
2.25==2 true false
-2==0 true false
3==3 true true
'2'=='0' true false
'2.5'=='3' true false
'-2'=='1.5' true false
'-2.5'=='-1.5' true false
'3.0'=='3.0' true true
2.75==2.75 true true
'2.75'=='2.75' true true
---------- binary operator ~= ----------
2~=0 true true
-2.5~=0 true true
2~=1 true true
5~=2 true true
-5~=2 true true
16~=2 true true
-16~=-2 true true
0.5~=0 true true
0.5~=1 true true
0.5~=2 true true
0.5~=-1 true true
0.5~=2 true true
2.25~=0 true true
2.25~=2 true true
-2~=0 true true
3~=3 true false
'2'~='0' true true
'2.5'~='3' true true
'-2'~='1.5' true true
'-2.5'~='-1.5' true true
'3.0'~='3.0' true false
2.75~=2.75 true false
'2.75'~='2.75' true false
---------- binary operator > ----------
2>0 true true
-2.5>0 true false
2>1 true true
5>2 true true
-5>2 true false
16>2 true true
-16>-2 true false
0.5>0 true true
0.5>1 true false
0.5>2 true false
0.5>-1 true true
0.5>2 true false
2.25>0 true true
2.25>2 true true
-2>0 true false
3>3 true false
'2'>'0' true true
'2.5'>'3' true false
'-2'>'1.5' true false
'-2.5'>'-1.5' true true
'3.0'>'3.0' true false
2.75>2.75 true false
'2.75'>'2.75' true false
---------- binary operator < ----------
2<0 true false
-2.5<0 true true
2<1 true false
5<2 true false
-5<2 true true
16<2 true false
-16<-2 true true
0.5<0 true false
0.5<1 true true
0.5<2 true true
0.5<-1 true false
0.5<2 true true
2.25<0 true false
2.25<2 true false
-2<0 true true
3<3 true false
'2'<'0' true false
'2.5'<'3' true true
'-2'<'1.5' true true
'-2.5'<'-1.5' true false
'3.0'<'3.0' true false
2.75<2.75 true false
'2.75'<'2.75' true false
---------- binary operator >= ----------
2>=0 true true
-2.5>=0 true false
2>=1 true true
5>=2 true true
-5>=2 true false
16>=2 true true
-16>=-2 true false
0.5>=0 true true
0.5>=1 true false
0.5>=2 true false
0.5>=-1 true true
0.5>=2 true false
2.25>=0 true true
2.25>=2 true true
-2>=0 true false
3>=3 true true
'2'>='0' true true
'2.5'>='3' true false
'-2'>='1.5' true false
'-2.5'>='-1.5' true true
'3.0'>='3.0' true true
2.75>=2.75 true true
'2.75'>='2.75' true true
---------- binary operator <= ----------
2<=0 true false
-2.5<=0 true true
2<=1 true false
5<=2 true false
-5<=2 true true
16<=2 true false
-16<=-2 true true
0.5<=0 true false
0.5<=1 true true
0.5<=2 true true
0.5<=-1 true false
0.5<=2 true true
2.25<=0 true false
2.25<=2 true false
-2<=0 true true
3<=3 true true
'2'<='0' true false
'2.5'<='3' true true
'-2'<='1.5' true true
'-2.5'<='-1.5' true false
'3.0'<='3.0' true true
2.75<=2.75 true true
'2.75'<='2.75' true true
---------- math.abs ----------
math.abs(-2.5) true 2.5
math.abs(-2) true 2
math.abs(0) true <zero>
math.abs(2) true 2
math.abs(2.5) true 2.5
math.abs('-2.5') true 2.5
math.abs('-2') true 2
math.abs('0') true <zero>
math.abs('2') true 2
math.abs('2.5') true 2.5
---------- math.ceil ----------
math.ceil(-2.5) true -2
math.ceil(-2) true -2
math.ceil(0) true <zero>
math.ceil(2) true 2
math.ceil(2.5) true 3
math.ceil('-2.5') true -2
math.ceil('-2') true -2
math.ceil('0') true <zero>
math.ceil('2') true 2
math.ceil('2.5') true 3
---------- math.cos ----------
math.cos(-2.5) true -0.801
math.cos(-2) true -0.416
math.cos(0) true 1
math.cos(2) true -0.416
math.cos(2.5) true -0.801
math.cos('-2.5') true -0.801
math.cos('-2') true -0.416
math.cos('0') true 1
math.cos('2') true -0.416
math.cos('2.5') true -0.801
---------- math.deg ----------
math.deg(-2.5) true -143.2
math.deg(-2) true -114.5
math.deg(0) true <zero>
math.deg(2) true 114.59
math.deg(2.5) true 143.23
math.deg('-2.5') true -143.2
math.deg('-2') true -114.5
math.deg('0') true <zero>
math.deg('2') true 114.59
math.deg('2.5') true 143.23
---------- math.exp ----------
math.exp(-2.5) true 0.0820
math.exp(-2) true 0.1353
math.exp(0) true 1
math.exp(2) true 7.3890
math.exp(2.5) true 12.182
math.exp('-2.5') true 0.0820
math.exp('-2') true 0.1353
math.exp('0') true 1
math.exp('2') true 7.3890
math.exp('2.5') true 12.182
---------- math.floor ----------
math.floor(-2.5) true -3
math.floor(-2) true -2
math.floor(0) true <zero>
math.floor(2) true 2
math.floor(2.5) true 2
math.floor('-2.5') true -3
math.floor('-2') true -2
math.floor('0') true <zero>
math.floor('2') true 2
math.floor('2.5') true 2
---------- math.frexp ----------
math.frexp(-2.5) true -0.625 2
math.frexp(-2) true -0.5 2
math.frexp(0) true <zero> <zero>
math.frexp(2) true 0.5 2
math.frexp(2.5) true 0.625 2
math.frexp('-2.5') true -0.625 2
math.frexp('-2') true -0.5 2
math.frexp('0') true <zero> <zero>
math.frexp('2') true 0.5 2
math.frexp('2.5') true 0.625 2
---------- math.modf ----------
math.modf(-2.5) true -2 -0.5
math.modf(-2) true -2 <zero>
math.modf(0) true <zero> <zero>
math.modf(2) true 2 <zero>
math.modf(2.5) true 2 0.5
math.modf('-2.5') true -2 -0.5
math.modf('-2') true -2 <zero>
math.modf('0') true <zero> <zero>
math.modf('2') true 2 <zero>
math.modf('2.5') true 2 0.5
---------- math.rad ----------
math.rad(-2.5) true -0.043
math.rad(-2) true -0.034
math.rad(0) true <zero>
math.rad(2) true 0.0349
math.rad(2.5) true 0.0436
math.rad('-2.5') true -0.043
math.rad('-2') true -0.034
math.rad('0') true <zero>
math.rad('2') true 0.0349
math.rad('2.5') true 0.0436
---------- math.sin ----------
math.sin(-2.5) true -0.598
math.sin(-2) true -0.909
math.sin(0) true <zero>
math.sin(2) true 0.9092
math.sin(2.5) true 0.5984
math.sin('-2.5') true -0.598
math.sin('-2') true -0.909
math.sin('0') true <zero>
math.sin('2') true 0.9092
math.sin('2.5') true 0.5984
---------- math.sqrt ----------
math.sqrt(-2.5) true <nan>
math.sqrt(-2) true <nan>
math.sqrt(0) true <zero>
math.sqrt(2) true 1.4142
math.sqrt(2.5) true 1.5811
math.sqrt('-2.5') true <nan>
math.sqrt('-2') true <nan>
math.sqrt('0') true <zero>
math.sqrt('2') true 1.4142
math.sqrt('2.5') true 1.5811
---------- math.tan ----------
math.tan(-2.5) true 0.7470
math.tan(-2) true 2.1850
math.tan(0) true <zero>
math.tan(2) true -2.185
math.tan(2.5) true -0.747
math.tan('-2.5') true 0.7470
math.tan('-2') true 2.1850
math.tan('0') true <zero>
math.tan('2') true -2.185
math.tan('2.5') true -0.747
---------- math.acos (jse only) ----------
math.acos(-2.5) true <nan>
math.acos(-2) true <nan>
math.acos(0) true 1.5707
math.acos(2) true <nan>
math.acos(2.5) true <nan>
math.acos('-2.5') true <nan>
math.acos('-2') true <nan>
math.acos('0') true 1.5707
math.acos('2') true <nan>
math.acos('2.5') true <nan>
---------- math.asin (jse only) ----------
math.asin(-2.5) true <nan>
math.asin(-2) true <nan>
math.asin(0) true <zero>
math.asin(2) true <nan>
math.asin(2.5) true <nan>
math.asin('-2.5') true <nan>
math.asin('-2') true <nan>
math.asin('0') true <zero>
math.asin('2') true <nan>
math.asin('2.5') true <nan>
---------- math.atan (jse only) ----------
math.atan(-2.5) true -1.190
math.atan(-2) true -1.107
math.atan(0) true <zero>
math.atan(2) true 1.1071
math.atan(2.5) true 1.1902
math.atan('-2.5') true -1.190
math.atan('-2') true -1.107
math.atan('0') true <zero>
math.atan('2') true 1.1071
math.atan('2.5') true 1.1902
---------- math.cosh (jse only) ----------
math.cosh(-2.5) true 6.1322
math.cosh(-2) true 3.7621
math.cosh(0) true 1
math.cosh(2) true 3.7621
math.cosh(2.5) true 6.1322
math.cosh('-2.5') true 6.1322
math.cosh('-2') true 3.7621
math.cosh('0') true 1
math.cosh('2') true 3.7621
math.cosh('2.5') true 6.1322
---------- math.log (jse only) ----------
math.log(-2.5) true <nan>
math.log(-2) true <nan>
math.log(0) true <neg-inf>
math.log(2) true 0.6931
math.log(2.5) true 0.9162
math.log('-2.5') true <nan>
math.log('-2') true <nan>
math.log('0') true <neg-inf>
math.log('2') true 0.6931
math.log('2.5') true 0.9162
---------- math.sinh (jse only) ----------
math.sinh(-2.5) true -6.050
math.sinh(-2) true -3.626
math.sinh(0) true <zero>
math.sinh(2) true 3.6268
math.sinh(2.5) true 6.0502
math.sinh('-2.5') true -6.050
math.sinh('-2') true -3.626
math.sinh('0') true <zero>
math.sinh('2') true 3.6268
math.sinh('2.5') true 6.0502
---------- math.tanh (jse only) ----------
math.tanh(-2.5) true -0.986
math.tanh(-2) true -0.964
math.tanh(0) true <zero>
math.tanh(2) true 0.9640
math.tanh(2.5) true 0.9866
math.tanh('-2.5') true -0.986
math.tanh('-2') true -0.964
math.tanh('0') true <zero>
math.tanh('2') true 0.9640
math.tanh('2.5') true 0.9866
---------- math.fmod ----------
math.fmod(2,0) true <nan>
math.fmod(-2.5,0) true <nan>
math.fmod(2,1) true <zero>
math.fmod(5,2) true 1
math.fmod(-5,2) true -1
math.fmod(16,2) true <zero>
math.fmod(-16,-2) true <zero>
math.fmod(0.5,0) true <nan>
math.fmod(0.5,1) true 0.5
math.fmod(0.5,2) true 0.5
math.fmod(0.5,-1) true 0.5
math.fmod(0.5,2) true 0.5
math.fmod(2.25,0) true <nan>
math.fmod(2.25,2) true 0.25
math.fmod(-2,0) true <nan>
math.fmod(3,3) true <zero>
math.fmod('2','0') true <nan>
math.fmod('2.5','3') true 2.5
math.fmod('-2','1.5') true -0.5
math.fmod('-2.5','-1.5') true -1
math.fmod('3.0','3.0') true <zero>
math.fmod(2.75,2.75) true <zero>
math.fmod('2.75','2.75') true <zero>
math.fmod(3,'3') true <zero>
math.fmod('3',3) true <zero>
math.fmod(2.75,'2.75') true <zero>
math.fmod('2.75',2.75) true <zero>
math.fmod(-3,'-4') true -3
math.fmod('-3',4) true -3
math.fmod(-3,'4') true -3
math.fmod('-3',-4) true -3
math.fmod(-4.75,'2.75') true -2
math.fmod('-2.75',1.75) true -1
math.fmod(4.75,'-2.75') true 2
math.fmod('2.75',-1.75) true 1
---------- math.ldexp ----------
math.ldexp(2,0) true 2
math.ldexp(-2.5,0) true -2.5
math.ldexp(2,1) true 4
math.ldexp(5,2) true 20
math.ldexp(-5,2) true -20
math.ldexp(16,2) true 64
math.ldexp(-16,-2) true -4
math.ldexp(0.5,0) true 0.5
math.ldexp(0.5,1) true 1
math.ldexp(0.5,2) true 2
math.ldexp(0.5,-1) true 0.25
math.ldexp(0.5,2) true 2
math.ldexp(2.25,0) true 2.25
math.ldexp(2.25,2) true 9
math.ldexp(-2,0) true -2
math.ldexp(3,3) true 24
math.ldexp('2','0') true 2
math.ldexp('2.5','3') true 20
math.ldexp('-2','1.5') true -4
math.ldexp('-2.5','-1.5') true -1.25
math.ldexp('3.0','3.0') true 24
math.ldexp(2.75,2.75) true 11
math.ldexp('2.75','2.75') true 11
math.ldexp(3,'3') true 24
math.ldexp('3',3) true 24
math.ldexp(2.75,'2.75') true 11
math.ldexp('2.75',2.75) true 11
math.ldexp(-3,'-4') true -0.187
math.ldexp('-3',4) true -48
math.ldexp(-3,'4') true -48
math.ldexp('-3',-4) true -0.187
math.ldexp(-4.75,'2.75') true -19
math.ldexp('-2.75',1.75) true -5.5
math.ldexp(4.75,'-2.75') true 1.1875
math.ldexp('2.75',-1.75) true 1.375
---------- math.pow ----------
math.pow(2,0) true 1
math.pow(-2.5,0) true 1
math.pow(2,1) true 2
math.pow(5,2) true 25
math.pow(-5,2) true 25
math.pow(16,2) true 256
math.pow(-16,-2) true 0.0039
math.pow(0.5,0) true 1
math.pow(0.5,1) true 0.5
math.pow(0.5,2) true 0.25
math.pow(0.5,-1) true 2
math.pow(0.5,2) true 0.25
math.pow(2.25,0) true 1
math.pow(2.25,2) true 5.0625
math.pow(-2,0) true 1
math.pow(3,3) true 27
math.pow('2','0') true 1
math.pow('2.5','3') true 15.625
math.pow('-2','1.5') true <nan>
math.pow('-2.5','-1.5') true <nan>
math.pow('3.0','3.0') true 27
math.pow(2.75,2.75) true 16.149
math.pow('2.75','2.75') true 16.149
math.pow(3,'3') true 27
math.pow('3',3) true 27
math.pow(2.75,'2.75') true 16.149
math.pow('2.75',2.75) true 16.149
math.pow(-3,'-4') true 0.0123
math.pow('-3',4) true 81
math.pow(-3,'4') true 81
math.pow('-3',-4) true 0.0123
math.pow(-4.75,'2.75') true <nan>
math.pow('-2.75',1.75) true <nan>
math.pow(4.75,'-2.75') true 0.0137
math.pow('2.75',-1.75) true 0.1702
---------- math.atan2 (jse only) ----------
math.atan2(2,0) true 1.5707
math.atan2(-2.5,0) true -1.570
math.atan2(2,1) true 1.1071
math.atan2(5,2) true 1.1902
math.atan2(-5,2) true -1.190
math.atan2(16,2) true 1.4464
math.atan2(-16,-2) true -1.695
math.atan2(0.5,0) true 1.5707
math.atan2(0.5,1) true 0.4636
math.atan2(0.5,2) true 0.2449
math.atan2(0.5,-1) true 2.6779
math.atan2(0.5,2) true 0.2449
math.atan2(2.25,0) true 1.5707
math.atan2(2.25,2) true 0.8441
math.atan2(-2,0) true -1.570
math.atan2(3,3) true 0.7853
math.atan2('2','0') true 1.5707
math.atan2('2.5','3') true 0.6947
math.atan2('-2','1.5') true -0.927
math.atan2('-2.5','-1.5') true -2.111
math.atan2('3.0','3.0') true 0.7853
math.atan2(2.75,2.75) true 0.7853
math.atan2('2.75','2.75') true 0.7853
math.atan2(3,'3') true 0.7853
math.atan2('3',3) true 0.7853
math.atan2(2.75,'2.75') true 0.7853
math.atan2('2.75',2.75) true 0.7853
math.atan2(-3,'-4') true -2.498
math.atan2('-3',4) true -0.643
math.atan2(-3,'4') true -0.643
math.atan2('-3',-4) true -2.498
math.atan2(-4.75,'2.75') true -1.046
math.atan2('-2.75',1.75) true -1.004
math.atan2(4.75,'-2.75') true 2.0955
math.atan2('2.75',-1.75) true 2.1375
---------- math.max ----------
math.max(4) true 4
math.max(-4.5) true -4.5
math.max('5.5') true 5.5
math.max('-5') true -5
math.max(4,'8') true 8
math.max(-4.5,'-8') true -4.5
math.max('5.5',2.2) true 5.5
math.max('-5',-2.2) true -2.2
math.max(111,222,333) true 333
math.max(-222,-333,-111) true -111
math.max(444,-111,-222) true 444
---------- math.min ----------
math.min(4) true 4
math.min(-4.5) true -4.5
math.min('5.5') true 5.5
math.min('-5') true -5
math.min(4,'8') true 4
math.min(-4.5,'-8') true -8
math.min('5.5',2.2) true 2.2
math.min('-5',-2.2) true -5
math.min(111,222,333) true 111
math.min(-222,-333,-111) true -333
math.min(444,-111,-222) true -222
----------- Random number tests
math.random() number true
math.random() number true
math.random() number true
math.random() number true
math.random() number true
math.random(5,10) number true
math.random(5,10) number true
math.random(5,10) number true
math.random(5,10) number true
math.random(5,10) number true
math.random(30) number true
math.random(30) number true
math.random(30) number true
math.random(30) number true
math.random(30) number true
math.random(-4,-2) number true
math.random(-4,-2) number true
math.random(-4,-2) number true
math.random(-4,-2) number true
math.random(-4,-2) number true
-- comparing new numbers
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
false false
-- resetting seed
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
----------- Tests involving -0 and NaN
0 == -0 true
t[-0] == t[0] true
mz, z <zero> <zero>
mz == z true
a[z] == 1 and a[mz] == 1 true

View File

@@ -0,0 +1,286 @@
local anumber,bnumber = 111,23.45
local astring,bstring = "abc","def"
local anumstr,bnumstr = tostring(anumber),tostring(bnumber)
local aboolean,bboolean = false,true
local afunction,bfunction = function() end, function() end
local athread,bthead = coroutine.create(afunction),coroutine.create(bfunction)
local atable,btable = {},{}
local values = { anumber, aboolean, afunction, athread, atable }
local groups
local ts = tostring
local tb,count = {},0
tostring = function(o)
local t = type(o)
if t~='thread' and t~='function' and t~='table' then return ts(o) end
if not tb[o] then
count = count + 1
tb[o] = t..'.'..count
end
return tb[o]
end
local buildop = function(name)
return function(a,b)
print( 'mt.__'..name..'()', a, b )
return '__'..name..'-result'
end
end
local buildop3 = function(name)
return function(a,b,c)
print( 'mt.__'..name..'()', a, b, c )
return '__'..name..'-result'
end
end
local buildop1 = function(name)
return function(a)
print( 'mt.__'..name..'()', a )
return '__'..name..'-result'
end
end
local mt = {
__call=buildop('call'),
__add=buildop('add'),
__sub=buildop('sub'),
__mul=buildop('mul'),
__div=buildop('div'),
__pow=buildop('pow'),
__mod=buildop('mod'),
__unm=buildop1('unm'),
__len=buildop1('len'),
__lt=buildop('lt'),
__le=buildop('le'),
__index=buildop('index'),
__newindex=buildop3('newindex'),
__concat=buildop('concat'),
}
-- pcall a function and check for a pattern in the error string
ecall = function(pattern, ...)
local s,e = pcall(...)
if not s then e = string.match(e,pattern) or e end
return s,e
end
print( '---- __eq same types' )
local eqmt = { __eq=buildop('eq'), }
groups = { {nil,nil}, {true,false}, {123,456}, {11,5.5}, {afunction,bfunction}, {athread,bthread}, {astring,bstring}, {anumber,anumstr} }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', pcall( function() return a==b end ) )
print( type(a), type(b), 'before', pcall( function() return a~=b end ) )
print( debug.setmetatable( a, eqmt ) )
print( debug.setmetatable( b, eqmt ) )
print( type(a), type(b), 'after', pcall( function() return a==b end ) )
print( type(a), type(b), 'after', pcall( function() return a~=b end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( '---- __eq, tables - should invoke metatag comparison' )
groups = { {atable,btable} }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', pcall( function() return a==b end ) )
print( type(a), type(b), 'before', pcall( function() return a~=b end ) )
print( debug.setmetatable( a, eqmt ) )
print( debug.setmetatable( b, eqmt ) )
print( type(a), type(b), 'after-a', pcall( function() return a==b end ) )
print( type(a), type(b), 'after-a', pcall( function() return a~=b end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( 'nilmt', debug.getmetatable(nil) )
print( 'boolmt', debug.getmetatable(true) )
print( 'number', debug.getmetatable(1) )
print( 'function', debug.getmetatable(afunction) )
print( 'thread', debug.getmetatable(athread) )
print( '---- __call' )
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( type(a), 'before', ecall( 'attempt to call', function() return a('a','b') end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), 'after', pcall( function() return a() end ) )
print( type(a), 'after', pcall( function() return a('a') end ) )
print( type(a), 'after', pcall( function() return a('a','b') end ) )
print( type(a), 'after', pcall( function() return a('a','b','c') end ) )
print( type(a), 'after', pcall( function() return a('a','b','c','d') end ) )
print( debug.setmetatable( a, amt ) )
end
print( '---- __add, __sub, __mul, __div, __pow, __mod' )
local groups = { {aboolean, aboolean}, {aboolean, athread}, {aboolean, afunction}, {aboolean, "abc"}, {aboolean, atable} }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a+b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return b+a end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a-b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return b-a end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a*b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return b*a end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a^b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return b^a end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a%b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return b%a end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), type(b), 'after', pcall( function() return a+b end ) )
print( type(a), type(b), 'after', pcall( function() return b+a end ) )
print( type(a), type(b), 'after', pcall( function() return a-b end ) )
print( type(a), type(b), 'after', pcall( function() return b-a end ) )
print( type(a), type(b), 'after', pcall( function() return a*b end ) )
print( type(a), type(b), 'after', pcall( function() return b*a end ) )
print( type(a), type(b), 'after', pcall( function() return a^b end ) )
print( type(a), type(b), 'after', pcall( function() return b^a end ) )
print( type(a), type(b), 'after', pcall( function() return a%b end ) )
print( type(a), type(b), 'after', pcall( function() return b%a end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( '---- __len' )
values = { aboolean, afunction, athread, anumber }
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( type(a), 'before', ecall( 'attempt to get length of ', function() return #a end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), 'after', pcall( function() return #a end ) )
print( debug.setmetatable( a, amt ) )
end
--
print( '---- __neg' )
values = { aboolean, afunction, athread, "abcd", atable, anumber }
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( type(v), 'before', ecall( 'attempt to perform arithmetic ', function() return -a end ) )
print( debug.setmetatable( a, mt ) )
print( type(v), 'after', pcall( function() return -a end ) )
print( debug.setmetatable( a, amt ) )
end
print( '---- __lt, __le, same types' )
local bfunction = function() end
local bthread = coroutine.create( bfunction )
local btable = {}
local groups
groups = { {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, {atable, atable}, {atable, btable} }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a<b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a>b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, mt ) )
print( debug.setmetatable( b, mt ) )
print( type(a), type(b), 'after', pcall( function() return a<b end ) )
print( type(a), type(b), 'after', pcall( function() return a<=b end ) )
print( type(a), type(b), 'after', pcall( function() return a>b end ) )
print( type(a), type(b), 'after', pcall( function() return a>=b end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( '---- __lt, __le, different types' )
groups = { {aboolean, athread}, }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a<b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a>b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, mt ) )
print( debug.setmetatable( b, mt ) )
print( type(a), type(b), 'after-a', ecall( 'attempt to compare', function() return a<b end ) )
print( type(a), type(b), 'after-a', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(a), type(b), 'after-a', ecall( 'attempt to compare', function() return a>b end ) )
print( type(a), type(b), 'after-a', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( '---- __tostring' )
values = { aboolean, afunction, athread, atable, "abc" }
local strmt = { __tostring=function(a)
return 'mt.__tostring('..type(a)..')'
end,
}
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( debug.setmetatable( a, strmt ) )
print( type(a), 'after', pcall( function() return ts(a) end ) )
print( debug.setmetatable( a, amt ) )
end
print( '---- __index, __newindex' )
values = { aboolean, anumber, afunction, athread }
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( type(a), 'before', ecall( 'attempt to index', function() return a.foo end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a[123] end ) )
print( type(a), 'before', ecall( 'index', function() a.foo = 'bar' end ) )
print( type(a), 'before', ecall( 'index', function() a[123] = 'bar' end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a:foo() end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), 'after', pcall( function() return a.foo end ) )
print( type(a), 'after', pcall( function() return a[123] end ) )
print( type(a), 'after', pcall( function() a.foo = 'bar' end ) )
print( type(a), 'after', pcall( function() a[123] = 'bar' end ) )
print( type(a), 'after', ecall( 'attempt to call', function() return a:foo() end ) )
print( debug.setmetatable( a, amt ) )
end
print( '---- __concat' )
groups = { {atable, afunction}, {afunction, atable}, {123, nil}, {nil, 123} }
local s,t,u = 'sss',777
local concatresult = setmetatable( { '__concat-result' }, {
__tostring=function()
return 'concat-string-result'
end } )
local concatmt = {
__concat=function(a,b)
print( 'mt.__concat('..type(a)..','..type(b)..')', a, b )
return concatresult
end
}
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
local amt,bmt = debug.getmetatable(a),debug.getmetatable(b)
print( type(a), type(b), 'before', ecall( 'attempt to concatenate ', function() return a..b end ) )
print( type(a), type(b), 'before', ecall( 'attempt to concatenate ', function() return b..a end ) )
print( type(a), type(s), type(t), 'before', ecall( 'attempt to concatenate ', function() return a..s..t end ) )
print( type(s), type(a), type(t), 'before', ecall( 'attempt to concatenate ', function() return s..a..t end ) )
print( type(s), type(t), type(a), 'before', ecall( 'attempt to concatenate ', function() return s..t..a end ) )
print( debug.setmetatable( a, concatmt ) )
print( type(a), type(b), 'after', pcall( function() return a..b end ) )
print( type(a), type(b), 'after', pcall( function() return b..a end ) )
print( type(a), type(s), type(t), 'before', pcall( function() return a..s..t end ) )
print( type(s), type(a), type(t), 'before', ecall( 'attempt to concatenate ', function() return s..a..t end ) )
print( type(s), type(t), type(a), 'before', ecall( 'attempt to concatenate ', function() return s..t..a end ) )
print( debug.setmetatable( a, amt ) )
print( debug.setmetatable( b, bmt ) )
end
print( '---- __metatable' )
values = { aboolean, afunction, athread, atable, "abc" }
local mtmt = { __metatable={}, }
for i=1,#values do
local a = values[i]
local amt = debug.getmetatable(a)
print( type(a), 'before', pcall( function() return debug.getmetatable(a), getmetatable(a) end ) )
print( debug.setmetatable( a, mtmt ) )
print( type(a), 'after', pcall( function() return debug.getmetatable(a), getmetatable(a) end ) )
print( debug.setmetatable( a, amt ) )
end

View File

@@ -0,0 +1,649 @@
---- __eq same types
nil nil before true true
nil nil before true false
nil
nil
nil nil after true true
nil nil after true false
nil
nil
boolean boolean before true false
boolean boolean before true true
true
false
boolean boolean after true false
boolean boolean after true true
true
false
number number before true false
number number before true true
123
456
number number after true false
number number after true true
123
456
number number before true false
number number before true true
11
5.5
number number after true false
number number after true true
11
5.5
function function before true false
function function before true true
function.1
function.2
function function after true false
function function after true true
function.1
function.2
thread nil before true false
thread nil before true true
thread.3
nil
thread nil after true false
thread nil after true true
thread.3
nil
string string before true false
string string before true true
abc
def
string string after true false
string string after true true
abc
def
number string before true false
number string before true true
111
111
number string after true false
number string after true true
111
111
---- __eq, tables - should invoke metatag comparison
table table before true false
table table before true true
table.4
table.5
mt.__eq() table.4 table.5
table table after-a true true
mt.__eq() table.4 table.5
table table after-a true false
table.4
table.5
nilmt nil
boolmt nil
number nil
function nil
thread nil
---- __call
number before false attempt to call
111
mt.__call() 111 nil
number after true __call-result
mt.__call() 111 a
number after true __call-result
mt.__call() 111 a
number after true __call-result
mt.__call() 111 a
number after true __call-result
mt.__call() 111 a
number after true __call-result
111
boolean before false attempt to call
false
mt.__call() false nil
boolean after true __call-result
mt.__call() false a
boolean after true __call-result
mt.__call() false a
boolean after true __call-result
mt.__call() false a
boolean after true __call-result
mt.__call() false a
boolean after true __call-result
false
function before true nil
function.1
function after true
function after true
function after true
function after true
function after true
function.1
thread before false attempt to call
thread.3
mt.__call() thread.3 nil
thread after true __call-result
mt.__call() thread.3 a
thread after true __call-result
mt.__call() thread.3 a
thread after true __call-result
mt.__call() thread.3 a
thread after true __call-result
mt.__call() thread.3 a
thread after true __call-result
thread.3
table before false attempt to call
table.4
mt.__call() table.4 nil
table after true __call-result
mt.__call() table.4 a
table after true __call-result
mt.__call() table.4 a
table after true __call-result
mt.__call() table.4 a
table after true __call-result
mt.__call() table.4 a
table after true __call-result
table.4
---- __add, __sub, __mul, __div, __pow, __mod
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
boolean boolean before false attempt to perform arithmetic
false
mt.__add() false false
boolean boolean after true __add-result
mt.__add() false false
boolean boolean after true __add-result
mt.__sub() false false
boolean boolean after true __sub-result
mt.__sub() false false
boolean boolean after true __sub-result
mt.__mul() false false
boolean boolean after true __mul-result
mt.__mul() false false
boolean boolean after true __mul-result
mt.__pow() false false
boolean boolean after true __pow-result
mt.__pow() false false
boolean boolean after true __pow-result
mt.__mod() false false
boolean boolean after true __mod-result
mt.__mod() false false
boolean boolean after true __mod-result
false
false
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
boolean thread before false attempt to perform arithmetic
false
mt.__add() false thread.3
boolean thread after true __add-result
mt.__add() thread.3 false
boolean thread after true __add-result
mt.__sub() false thread.3
boolean thread after true __sub-result
mt.__sub() thread.3 false
boolean thread after true __sub-result
mt.__mul() false thread.3
boolean thread after true __mul-result
mt.__mul() thread.3 false
boolean thread after true __mul-result
mt.__pow() false thread.3
boolean thread after true __pow-result
mt.__pow() thread.3 false
boolean thread after true __pow-result
mt.__mod() false thread.3
boolean thread after true __mod-result
mt.__mod() thread.3 false
boolean thread after true __mod-result
false
thread.3
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
boolean function before false attempt to perform arithmetic
false
mt.__add() false function.1
boolean function after true __add-result
mt.__add() function.1 false
boolean function after true __add-result
mt.__sub() false function.1
boolean function after true __sub-result
mt.__sub() function.1 false
boolean function after true __sub-result
mt.__mul() false function.1
boolean function after true __mul-result
mt.__mul() function.1 false
boolean function after true __mul-result
mt.__pow() false function.1
boolean function after true __pow-result
mt.__pow() function.1 false
boolean function after true __pow-result
mt.__mod() false function.1
boolean function after true __mod-result
mt.__mod() function.1 false
boolean function after true __mod-result
false
function.1
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
boolean string before false attempt to perform arithmetic
false
mt.__add() false abc
boolean string after true __add-result
mt.__add() abc false
boolean string after true __add-result
mt.__sub() false abc
boolean string after true __sub-result
mt.__sub() abc false
boolean string after true __sub-result
mt.__mul() false abc
boolean string after true __mul-result
mt.__mul() abc false
boolean string after true __mul-result
mt.__pow() false abc
boolean string after true __pow-result
mt.__pow() abc false
boolean string after true __pow-result
mt.__mod() false abc
boolean string after true __mod-result
mt.__mod() abc false
boolean string after true __mod-result
false
abc
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
boolean table before false attempt to perform arithmetic
false
mt.__add() false table.4
boolean table after true __add-result
mt.__add() table.4 false
boolean table after true __add-result
mt.__sub() false table.4
boolean table after true __sub-result
mt.__sub() table.4 false
boolean table after true __sub-result
mt.__mul() false table.4
boolean table after true __mul-result
mt.__mul() table.4 false
boolean table after true __mul-result
mt.__pow() false table.4
boolean table after true __pow-result
mt.__pow() table.4 false
boolean table after true __pow-result
mt.__mod() false table.4
boolean table after true __mod-result
mt.__mod() table.4 false
boolean table after true __mod-result
false
table.4
---- __len
boolean before false attempt to get length of
false
mt.__len() false
boolean after true __len-result
false
function before false attempt to get length of
function.1
mt.__len() function.1
function after true __len-result
function.1
thread before false attempt to get length of
thread.3
mt.__len() thread.3
thread after true __len-result
thread.3
number before false attempt to get length of
111
mt.__len() 111
number after true __len-result
111
---- __neg
nil before false attempt to perform arithmetic
false
mt.__unm() false
nil after true __unm-result
false
nil before false attempt to perform arithmetic
function.1
mt.__unm() function.1
nil after true __unm-result
function.1
nil before false attempt to perform arithmetic
thread.3
mt.__unm() thread.3
nil after true __unm-result
thread.3
nil before false attempt to perform arithmetic
abcd
mt.__unm() abcd
nil after true __unm-result
abcd
nil before false attempt to perform arithmetic
table.4
mt.__unm() table.4
nil after true __unm-result
table.4
nil before true -111
111
nil after true -111
111
---- __lt, __le, same types
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
true
true
mt.__lt() true true
boolean boolean after true true
mt.__le() true true
boolean boolean after true true
mt.__lt() true true
boolean boolean after true true
mt.__le() true true
boolean boolean after true true
true
true
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
boolean boolean before false attempt to compare
true
false
mt.__lt() true false
boolean boolean after true true
mt.__le() true false
boolean boolean after true true
mt.__lt() false true
boolean boolean after true true
mt.__le() false true
boolean boolean after true true
true
false
function function before false attempt to compare
function function before false attempt to compare
function function before false attempt to compare
function function before false attempt to compare
function.1
function.6
mt.__lt() function.1 function.6
function function after true true
mt.__le() function.1 function.6
function function after true true
mt.__lt() function.6 function.1
function function after true true
mt.__le() function.6 function.1
function function after true true
function.1
function.6
thread thread before false attempt to compare
thread thread before false attempt to compare
thread thread before false attempt to compare
thread thread before false attempt to compare
thread.3
thread.7
mt.__lt() thread.3 thread.7
thread thread after true true
mt.__le() thread.3 thread.7
thread thread after true true
mt.__lt() thread.7 thread.3
thread thread after true true
mt.__le() thread.7 thread.3
thread thread after true true
thread.3
thread.7
table table before false attempt to compare
table table before false attempt to compare
table table before false attempt to compare
table table before false attempt to compare
table.4
table.4
mt.__lt() table.4 table.4
table table after true true
mt.__le() table.4 table.4
table table after true true
mt.__lt() table.4 table.4
table table after true true
mt.__le() table.4 table.4
table table after true true
table.4
table.4
table table before false attempt to compare
table table before false attempt to compare
table table before false attempt to compare
table table before false attempt to compare
table.4
table.8
mt.__lt() table.4 table.8
table table after true true
mt.__le() table.4 table.8
table table after true true
mt.__lt() table.8 table.4
table table after true true
mt.__le() table.8 table.4
table table after true true
table.4
table.8
---- __lt, __le, different types
boolean thread before false attempt to compare
boolean thread before false attempt to compare
boolean thread before false attempt to compare
boolean thread before false attempt to compare
false
thread.3
mt.__lt() false thread.3
boolean thread after-a true true
mt.__le() false thread.3
boolean thread after-a true true
mt.__lt() thread.3 false
boolean thread after-a true true
mt.__le() thread.3 false
boolean thread after-a true true
false
thread.3
---- __tostring
mt.__tostring(boolean)
boolean after mt.__tostring(boolean) mt.__tostring(boolean)
false
function.1
function after true mt.__tostring(function)
function.1
thread.3
thread after true mt.__tostring(thread)
thread.3
table.4
table after true mt.__tostring(table)
table.4
mt.__tostring(string)
mt.__tostring(string) mt.__tostring(string) true mt.__tostring(string)
abc
---- __index, __newindex
boolean before false attempt to index
boolean before false attempt to index
boolean before false index
boolean before false index
boolean before false attempt to index
false
mt.__index() false foo
boolean after true __index-result
mt.__index() false 123
boolean after true __index-result
mt.__newindex() false foo bar
boolean after true
mt.__newindex() false 123 bar
boolean after true
mt.__index() false foo
boolean after false attempt to call
false
number before false attempt to index
number before false attempt to index
number before false index
number before false index
number before false attempt to index
111
mt.__index() 111 foo
number after true __index-result
mt.__index() 111 123
number after true __index-result
mt.__newindex() 111 foo bar
number after true
mt.__newindex() 111 123 bar
number after true
mt.__index() 111 foo
number after false attempt to call
111
function before false attempt to index
function before false attempt to index
function before false index
function before false index
function before false attempt to index
function.1
mt.__index() function.1 foo
function after true __index-result
mt.__index() function.1 123
function after true __index-result
mt.__newindex() function.1 foo bar
function after true
mt.__newindex() function.1 123 bar
function after true
mt.__index() function.1 foo
function after false attempt to call
function.1
thread before false attempt to index
thread before false attempt to index
thread before false index
thread before false index
thread before false attempt to index
thread.3
mt.__index() thread.3 foo
thread after true __index-result
mt.__index() thread.3 123
thread after true __index-result
mt.__newindex() thread.3 foo bar
thread after true
mt.__newindex() thread.3 123 bar
thread after true
mt.__index() thread.3 foo
thread after false attempt to call
thread.3
---- __concat
table function before false attempt to concatenate
table function before false attempt to concatenate
table string number before false attempt to concatenate
string table number before false attempt to concatenate
string number table before false attempt to concatenate
table.4
mt.__concat(table,function) table.4 function.1
table function after true table.9
mt.__concat(function,table) function.1 table.4
table function after true table.9
mt.__concat(table,string) table.4 sss777
table string number before true table.9
mt.__concat(table,number) table.4 777
string table number before false attempt to concatenate
mt.__concat(number,table) 777 table.4
string number table before false attempt to concatenate
table.4
function.1
function table before false attempt to concatenate
function table before false attempt to concatenate
function string number before false attempt to concatenate
string function number before false attempt to concatenate
string number function before false attempt to concatenate
function.1
mt.__concat(function,table) function.1 table.4
function table after true table.9
mt.__concat(table,function) table.4 function.1
function table after true table.9
mt.__concat(function,string) function.1 sss777
function string number before true table.9
mt.__concat(function,number) function.1 777
string function number before false attempt to concatenate
mt.__concat(number,function) 777 function.1
string number function before false attempt to concatenate
function.1
table.4
number nil before false attempt to concatenate
number nil before false attempt to concatenate
number string number before true 123sss777
string number number before true sss123777
string number number before true sss777123
123
mt.__concat(number,nil) 123 nil
number nil after true table.9
mt.__concat(nil,number) nil 123
number nil after true table.9
number string number before true 123sss777
string number number before true sss123777
string number number before true sss777123
123
nil
nil number before false attempt to concatenate
nil number before false attempt to concatenate
nil string number before false attempt to concatenate
string nil number before false attempt to concatenate
string number nil before false attempt to concatenate
nil
mt.__concat(nil,number) nil 123
nil number after true table.9
mt.__concat(number,nil) 123 nil
nil number after true table.9
mt.__concat(nil,string) nil sss777
nil string number before true table.9
mt.__concat(nil,number) nil 777
string nil number before false attempt to concatenate
mt.__concat(number,nil) 777 nil
string number nil before false attempt to concatenate
nil
123
---- __metatable
boolean before true nil nil
false
boolean after true table.10 table.11
false
function before true nil nil
function.1
function after true table.10 table.11
function.1
thread before true nil nil
thread.3
thread after true table.10 table.11
thread.3
table before true nil nil
table.4
table after true table.10 table.11
table.4
string before true table.12 table.12
abc
string after true table.10 table.11
abc

View File

@@ -0,0 +1,60 @@
-- simple os-library tests
--
-- because the nature of the "os" library is to provide os-specific behavior,
-- the compatibility tests must be extremely loose, and can really only
-- compare things like return value type to be meaningful.
--
-- actual os behavior needs to go in an oslib function test
--
local pcall = function(...)
local s,e,f = pcall(...)
return s,type(e),type(f)
end
print( 'os', type(os) )
print( 'os.clock()', pcall( os.clock ) )
print( 'os.date()', pcall( os.date ) )
print( 'os.difftime(123000, 21500)', pcall( os.difftime, 123000, 21250 ) )
print( 'os.getenv()', pcall( os.getenv ) )
print( 'os.getenv("bogus.key")', pcall( os.getenv, 'bogus.key' ) )
local s,p = pcall( os.tmpname )
local s,q = pcall( os.tmpname )
print( 'os.tmpname()', s, p )
print( 'os.tmpname()', s, q )
-- permission denied on windows
--print( 'os.remove(p)', pcall( os.remove, p ) )
--print( 'os.rename(p,q)', pcall( os.rename, p, q ) )
local s,f = pcall( io.open, p,"w" )
print( 'io.open', s, f )
print( 'write', pcall( f.write, f, "abcdef 12345" ) )
print( 'close', pcall( f.close, f ) )
print( 'os.rename(p,q)', pcall( os.rename, p, q ) )
print( 'os.remove(q)', pcall( os.remove, q ) )
print( 'os.remove(q)', pcall( os.remove, q ) )
-- setlocale not supported on jse yet
-- print( 'os.setlocale()', pcall( os.setlocale ) )
-- print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) )
-- print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) )
-- print( 'os.setlocale(nil,"all")', pcall( os.setlocale, nil, "all" ) )
print( 'os.setlocale("C")', pcall( os.setlocale, "C" ) )
print( 'os.exit', type(os.exit) )
-- os.date() formatting
local t = 1281364496 -- Aug 9, 2010, 2:34:56 PM (Monday)
local function p(s)
if pcall(os.date, s, t) then
print( "os.date('"..s.."', "..t..")", pcall(os.date, s, t))
end
end
for i= 65, 90 do
p('%'..string.char(i + 97 - 65))
p('%'..string.char(i))
end
local tbl = os.date('*t', t)
for i,k in ipairs({'year', 'month', 'day', 'hour', 'min', 'sec', 'wday', 'yday', 'isdst'}) do
local v = tbl[k]
print('k', type(k), k, 'v', type(v), v)
end
print('type(os.time())', type(os.time()))
print('os.time({year=1971, month=2, day=25})', os.time({year=1971, month=2, day=25}))
print('os.time({year=1971, month=2, day=25, hour=11, min=22, sec=33})', os.time({year=1971, month=2, day=25, hour=11, min=22, sec=33}))

View File

@@ -0,0 +1,49 @@
os table
os.clock() true number nil
os.date() true string nil
os.difftime(123000, 21500) true number nil
os.getenv() false string nil
os.getenv("bogus.key") true nil nil
os.tmpname() true string
os.tmpname() true string
io.open true userdata
write false string nil
close false string nil
os.rename(p,q) true boolean nil
os.remove(q) true boolean nil
os.remove(q) true nil string
os.setlocale("C") true string nil
os.exit function
os.date('%a', 1281364496) true string nil
os.date('%A', 1281364496) true string nil
os.date('%b', 1281364496) true string nil
os.date('%B', 1281364496) true string nil
os.date('%c', 1281364496) true string nil
os.date('%d', 1281364496) true string nil
os.date('%H', 1281364496) true string nil
os.date('%I', 1281364496) true string nil
os.date('%j', 1281364496) true string nil
os.date('%m', 1281364496) true string nil
os.date('%M', 1281364496) true string nil
os.date('%p', 1281364496) true string nil
os.date('%S', 1281364496) true string nil
os.date('%U', 1281364496) true string nil
os.date('%w', 1281364496) true string nil
os.date('%W', 1281364496) true string nil
os.date('%x', 1281364496) true string nil
os.date('%X', 1281364496) true string nil
os.date('%y', 1281364496) true string nil
os.date('%Y', 1281364496) true string nil
os.date('%z', 1281364496) true string nil
k string year v number 2010
k string month v number 8
k string day v number 9
k string hour v number 7
k string min v number 34
k string sec v number 56
k string wday v number 2
k string yday v number 221
k string isdst v boolean true
type(os.time()) number
os.time({year=1971, month=2, day=25}) 36360000
os.time({year=1971, month=2, day=25, hour=11, min=22, sec=33}) 36357753

View File

@@ -0,0 +1,192 @@
print( string.find("1234567890", ".", 0, true) )
print( string.find( 'alo alx 123 b\0o b\0o', '(..*) %1' ) )
print( string.find( 'aloALO', '%l*' ) )
print( string.find( ' \n isto <20> assim', '%S%S*' ) )
print( string.find( "", "" ) )
print( string.find( "ababaabbaba", "abb" ) )
print( string.find( "ababaabbaba", "abb", 7 ) )
print( string.match( "aabaa", "a*" ) )
print( string.match( "aabaa", "a*", 3 ) )
print( string.match( "aabaa", "a*b" ) )
print( string.match( "aabaa", "a*b", 3 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)b" ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b" ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 3 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 8 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 12 ) )
print( string.byte("hi", -3) )
print( string.gsub("ABC", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("@123", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@def", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@qrs@def@tuv", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@qrs@def@tuv", "@(%x+)", function(s) return "@ab" end) )
print( tostring(1234567890123) )
print( tostring(1234567890124) )
print( tostring(1234567890125) )
function f1(s, p)
print(p)
p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (s+1) end)
print(p)
p = string.gsub(p, "^(^?)", "%1()", 1)
print(p)
p = string.gsub(p, "($?)$", "()%1", 1)
print(p)
local t = {string.match(s, p)}
return string.sub(s, t[1], t[#t] - 1)
end
print( pcall( f1, 'alo alx 123 b\0o b\0o', '(..*) %1' ) )
local function badpat()
print( string.gsub( "alo", "(.)", "%2" ) )
end
print( ( pcall( badpat ) ) )
for k, v in string.gmatch("w=200&h=150", "(%w+)=(%w+)") do
print(k, v)
end
-- string.sub
function t(str)
local i = { 0, 1, 2, 8, -1 }
for ki,vi in ipairs(i) do
local s,v = pcall( string.sub, str, vi )
print( 'string.sub("'..str..'",'..tostring(vi)..')='..tostring(s)..',"'..tostring(v)..'"' )
local j = { 0, 1, 2, 4, 8, -1 }
for kj,vj in ipairs(j) do
local s,v = pcall( string.sub, str, vi, vj )
print( 'string.sub("'..str..'",'..tostring(vi)..','..tostring(vj)..')='..tostring(s)..',"'..tostring(v)..'"' )
end
end
end
t( 'abcdefghijklmn' )
t( 'abcdefg' )
t( 'abcd' )
t( 'abc' )
t( 'ab' )
t( 'a' )
t( '' )
print(string.len("Hello, world"))
print(#"Hello, world")
print(string.len("\0\0\0"))
print(#"\0\0\0")
print(string.len("\0\1\2\3"))
print(#"\0\1\2\3")
local s = "My JaCk-O-lAnTeRn CaSe TeXt"
print(s, string.len(s), #s)
-- string.format
print(string.format("(%.0d) (%.0d) (%.0d)", 0, -5, 9))
print(string.format("(%.1d) (%.1d) (%.1d)", 0, -5, 9))
print(string.format("(%.2d) (%.2d) (%.2d)", 0, -5, 9))
print(string.format("(%+.0d) (%+.0d) (%+.0d)", 0, -5, 9))
print(string.format("(%+.1d) (%+.1d) (%+.1d)", 0, -5, 9))
print(string.format("(%+.2d) (%+.2d) (%+.2d)", 0, -5, 9))
print(string.format("(%+3d) (% 3d) (%+ 3d)", 55, 55, 55))
print(string.format("(%-1d) (%-1d) (%-1d)", 1, 12, -12))
print(string.format("(%-2d) (%-2d) (%-2d)", 1, 12, -12))
print(string.format("(%-3d) (%-3d) (%-3d)", 1, 12, -12))
print(string.format("(%8x) (%8d) (%8o)", 255, 255, 255))
print(string.format("(%08x) (%08d) (%08o)", 255, 255, 255))
print(string.format("simple%ssimple", " simple "))
local testformat = function(message,fmt,...)
local s,e = pcall( string.format, fmt, ... )
if s then
if string.find(fmt, 'q') then
print(message, e)
end
print( message, string.byte(e,1,#e) )
else
print( message, 'error', e )
end
end
testformat('plain %', "%%")
testformat("specials (%s)", "---%s---", " %% \000 \r \n ")
testformat("specials (%q)", "---%q---", " %% \000 \r \n ")
testformat("specials (%q)", "---%q---", "0%%0\0000\r0\n0")
testformat("controls (%q)", "---%q---", ' \a \b \f \t \v \\ ')
testformat("controls (%q)", "---%q---", '0\a0\b0\f0\t0\v0\\0')
testformat("extended (%q)", "---%q---", ' \222 \223 \224 ')
testformat("extended (%q)", "---%q---", '0\2220\2230\2240')
testformat("embedded newlines", "%s\r%s\n%s", '===', '===', '===')
-- format long string
print("this is a %s long string", string.rep("really, ", 30))
local function pc(...)
local s,e = pcall(...)
return s and e or 'false-'..type(e)
end
local function strtests(name,func,...)
print(name, 'good', pc( func, ... ) )
print(name, 'empty', pc( func ) )
print(name, 'table', pc( func, {} ) )
print(name, 'nil', pc( func, nil ) )
end
strtests('lower', string.lower, s )
strtests('upper', string.upper, s )
strtests('reverse', string.reverse, s )
strtests('char', string.char, 92, 60, 61, 93 )
stringdumptest = function()
return load(string.dump(function(x) return 'foo->'..x end),'bar')('bat')
end
print( 'string.dump test:', pcall(stringdumptest) )
-- floating point formats (not supported yet)
--[==[
local prefixes = {'','+','-'}
local lengths = {'7','2','0','1',''}
local letters = {'f','e','g'}
local fmt, spec, desc
for i,letter in ipairs(letters) do
for k,before in ipairs(lengths) do
for j,prefix in ipairs(prefixes) do
spec = '(%'..prefix..before..letter..')'
fmt = spec..'\t'..spec..'\t'..spec..'\t'..spec..'\t'..spec..'\t'..spec
print(spec, string.format(fmt, 12.34, -12.34, 1/11, -1/11, 300/11, -300/11) )
for l,after in ipairs(lengths) do
spec = '(%'..prefix..before..'.'..after..letter..')'
fmt = spec..' '..spec..' '..spec..' '..spec..' '..spec..' '..spec
print(spec, string.format(fmt, 12.34, -12.34, 1/11, -1/11, 300/11, -300/11) )
end
end
end
end
--]==]
local function fmterr(...)
local r, s = pcall(...)
if r then
return s
else
s = string.gsub(s, "stdin:%d+:%s*", "")
return s
end
end
print(fmterr(string.find, "ab%c)0(", "%"))
print(fmterr(string.find, "ab%c)0(", "("))
print(pcall(string.find, "ab%c)0(", ")"))

View File

@@ -0,0 +1,284 @@
local func = function(t,...)
return (...)
end
local tbl = setmetatable({},{__index=func})
print( tbl[2] )
-- tostring replacement that assigns ids
local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'}
tostring = function(x)
if not x or not types[type(x)] then return ts(x) end
if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end
return id[x]
end
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
table.insert(t,'six');
table.insert(t,1,'seven');
table.insert(t,4,'eight');
table.insert(t,7,'nine');
table.insert(t,10,'ten'); print( #t )
-- concat
print( '-- concat tests' )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
print( table.concat(t,',',2,2) )
print( table.concat(t,',',5,2) )
end
tryconcat( { "one", "two", "three", a='aaa', b='bbb', c='ccc' } )
tryconcat( { "one", "two", "three", "four", "five" } )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
end
tryconcat( { a='aaa', b='bbb', c='ccc', d='ddd', e='eee' } )
tryconcat( { [501]="one", [502]="two", [503]="three", [504]="four", [505]="five" } )
tryconcat( {} )
-- print the elements of a table in a platform-independent way
function eles(t,f)
f = f or pairs
all = {}
for k,v in f(t) do
table.insert( all, "["..tostring(k).."]="..tostring(v) )
end
table.sort( all )
return "{"..table.concat(all,',').."}"
end
-- insert, len
print( '-- insert, len tests' )
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
print( eles(t), #t )
table.insert(t,'six'); print( eles(t), #t )
table.insert(t,1,'seven'); print( eles(t), #t )
table.insert(t,4,'eight'); print( eles(t), #t )
table.insert(t,7,'nine'); print( eles(t), #t )
table.insert(t,10,'ten'); print( eles(t), #t )
print( '#{}', #{} )
print( '#{"a"}', #{"a"} )
print( '#{"a","b"}', #{"a","b"} )
print( '#{"a",nil}', #{"a",nil} )
print( '#{nil,nil}', #{nil,nil} )
print( '#{nil,"b"}', #{nil,"b"}==0 or #{nil,"b"}==2 )
print( '#{"a","b","c"}', #{"a","b","c"} )
print( '#{"a","b",nil}', #{"a","b",nil} )
print( '#{"a",nil,nil}', #{"a",nil,nil} )
print( '#{nil,nil,nil}', #{nil,nil,nil} )
print( '#{nil,nil,"c"}', #{nil,nil,"c"}==0 or #{nil,nil,"c"}==3 )
print( '#{nil,"b","c"}', #{nil,"b","c"}==0 or #{nil,"b","c"}==3 )
print( '#{nil,"b",nil}', #{nil,"b",nil}==0 or #{nil,"b",nil}==2 )
print( '#{"a",nil,"c"}', #{"a",nil,"c"}==1 or #{"a",nil,"c"}==3 )
-- remove
print( '-- remove tests' )
t = { "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' }
print( eles(t), #t )
print( 'table.remove(t)', table.remove(t) ); print( eles(t), #t )
print( 'table.remove(t,1)', table.remove(t,1) ); print( eles(t), #t )
print( 'table.remove(t,3)', table.remove(t,3) ); print( eles(t), #t )
print( 'table.remove(t,5)', table.remove(t,5) ); print( eles(t), #t )
print( 'table.remove(t,10)', table.remove(t,10) ); print( eles(t), #t )
print( 'table.remove(t,-1)', table.remove(t,-1) ); print( eles(t), #t )
print( 'table.remove(t,-1)', table.remove(t,-1) ) ; print( eles(t), #t )
-- sort
print( '-- sort tests' )
function sorttest(t,f)
t = (t)
print( table.concat(t,'-') )
if f then
table.sort(t,f)
else
table.sort(t)
end
print( table.concat(t,'-') )
end
sorttest{ "one", "two", "three" }
sorttest{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }
sorttest( { "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }, function(a,b) return b<a end)
-- pairs, ipairs
--[[
function testpairs(f, t, name)
print( name )
for a,b in f(t) do
print( ' ', a, b )
end
end
function testbothpairs(t)
testpairs( pairs, t, 'pairs( '..eles(t)..' )' )
testpairs( ipairs, t, 'ipairs( '..eles(t)..' )' )
end
for i,t in ipairs({t0,t1,t2,t3}) do
testbothpairs(t)
end
t = { 'one', 'two', 'three', 'four', 'five' }
testbothpairs(t)
t[6] = 'six'
testbothpairs(t)
t[4] = nil
testbothpairs(t)
--]]
-- tests of setlist table constructors
-- length is tested elsewhere
print('----- unpack tests -------')
local unpack = table.unpack
print( 'pcall(unpack)', (pcall(unpack)) );
print( 'pcall(unpack,nil)', (pcall(unpack,nil)) );
print( 'pcall(unpack,"abc")', (pcall(unpack,"abc")) );
print( 'pcall(unpack,1)', (pcall(unpack,1)) );
print( 'unpack({"aa"})', unpack({"aa"}) );
print( 'unpack({"aa","bb"})', unpack({"aa","bb"}) );
print( 'unpack({"aa","bb","cc"})', unpack({"aa","bb","cc"}) );
local function a(...) return ... end
print('unpack -',unpack({}))
print('unpack a',unpack({'a'}))
print('unpack .',unpack({nil},1,1))
print('unpack ab',unpack({'a', 'b'}))
print('unpack .b',unpack({nil, 'b'},1,2))
print('unpack a.',unpack({'a', nil},1,2))
print('unpack abc',unpack({'a', 'b', 'c'}))
print('unpack .ab',unpack({nil, 'a', 'b'},1,3))
print('unpack a.b',unpack({'a', nil, 'b'},1,3))
print('unpack ab.',unpack({'a', 'b', nil},1,3))
print('unpack ..b',unpack({nil, nil, 'b'},1,3))
print('unpack a..',unpack({'a', nil, nil},1,3))
print('unpack .b.',unpack({nil, 'b', nil},1,3))
print('unpack ...',unpack({nil, nil, nil},1,3))
print('unpack (-)',unpack({a()}))
print('unpack (a)',unpack({a('a')}))
print('unpack (.)',unpack({a(nil)},1,1))
print('unpack (ab)',unpack({a('a', 'b')}))
print('unpack (.b)',unpack({a(nil, 'b')},1,2))
print('unpack (a.)',unpack({a('a', nil)},1,2))
print('unpack (abc)',unpack({a('a', 'b', 'c')}))
print('unpack (.ab)',unpack({a(nil, 'a', 'b')},1,3))
print('unpack (a.b)',unpack({a('a', nil, 'b')},1,3))
print('unpack (ab.)',unpack({a('a', 'b', nil)},1,3))
print('unpack (..b)',unpack({a(nil, nil, 'b')},1,3))
print('unpack (a..)',unpack({a('a', nil, nil)},1,3))
print('unpack (.b.)',unpack({a(nil, 'b', nil)},1,3))
print('unpack (...)',unpack({a(nil, nil, nil)},1,3))
local t = {"aa","bb","cc","dd","ee","ff"}
print( 'pcall(unpack,t)', pcall(unpack,t) );
print( 'pcall(unpack,t,2)', pcall(unpack,t,2) );
print( 'pcall(unpack,t,2,5)', pcall(unpack,t,2,5) );
print( 'pcall(unpack,t,2,6)', pcall(unpack,t,2,6) );
print( 'pcall(unpack,t,2,7)', pcall(unpack,t,2,7) );
print( 'pcall(unpack,t,1)', pcall(unpack,t,1) );
print( 'pcall(unpack,t,1,5)', pcall(unpack,t,1,5) );
print( 'pcall(unpack,t,1,6)', pcall(unpack,t,1,6) );
print( 'pcall(unpack,t,1,7)', pcall(unpack,t,1,7) );
print( 'pcall(unpack,t,0)', pcall(unpack,t,0) );
print( 'pcall(unpack,t,0,5)', pcall(unpack,t,0,5) );
print( 'pcall(unpack,t,0,6)', pcall(unpack,t,0,6) );
print( 'pcall(unpack,t,0,7)', pcall(unpack,t,0,7) );
print( 'pcall(unpack,t,-1)', pcall(unpack,t,-1) );
print( 'pcall(unpack,t,-1,5)', pcall(unpack,t,-1,5) );
print( 'pcall(unpack,t,-1,6)', pcall(unpack,t,-1,6) );
print( 'pcall(unpack,t,-1,7)', pcall(unpack,t,-1,7) );
print( 'pcall(unpack,t,2,4)', pcall(unpack,t,2,4) );
print( 'pcall(unpack,t,2,5)', pcall(unpack,t,2,5) );
print( 'pcall(unpack,t,2,6)', pcall(unpack,t,2,6) );
print( 'pcall(unpack,t,2,7)', pcall(unpack,t,2,7) );
print( 'pcall(unpack,t,2,8)', pcall(unpack,t,2,8) );
print( 'pcall(unpack,t,2,2)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,1)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,0)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,-1)', pcall(unpack,t,2,-1) );
t[0] = 'zz'
t[-1] = 'yy'
t[-2] = 'xx'
print( 'pcall(unpack,t,0)', pcall(unpack,t,0) );
print( 'pcall(unpack,t,2,0)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,-1)', pcall(unpack,t,2,-1) );
print( 'pcall(unpack,t,"3")', pcall(unpack,t,"3") );
print( 'pcall(unpack,t,"a")', (pcall(unpack,t,"a")) );
print( 'pcall(unpack,t,function() end)', (pcall(unpack,t,function() end)) );
-- misc tests
print('----- misc table initializer tests -------')
print( # { 'abc', 'def', 'ghi', nil } ) -- should be 3 !
print( # { 'abc', 'def', 'ghi', false } ) -- should be 4 !
print( # { 'abc', 'def', 'ghi', 0 } ) -- should be 4 !
-- basic table operation tests
print('----- basic table operations -------')
local dummyfunc = function(t,...)
print( 'metatable call args', type(t), ...)
return 'dummy'
end
local makeloud = function(t)
return setmetatable(t,{
__index=function(t,k)
print( '__index', type(t), k )
return rawset(t,k)
end,
__newindex=function(t,k,v)
print( '__newindex', type(t), k, v )
rawset(t,k,v)
end})
end
local tests = {
{'basic table', {}},
{'function metatable on __index', setmetatable({},{__index=dummyfunc})},
{'function metatable on __newindex', setmetatable({},{__newindex=dummyfunc})},
{'plain metatable on __index', setmetatable({},makeloud({}))},
{'plain metatable on __newindex', setmetatable({},makeloud({}))},
}
local function shoulderr(s,e)
return s,type(e)
end
for i,test in ipairs(tests) do
local testname = test[1]
local testtable = test[2]
print( '------ basic table tests on '..testname..' '..type(testtable) )
print( 't[1]=2', pcall( function() testtable[1]=2 end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't[1]=nil', pcall( function() testtable[1]=nil end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't["a"]="b"', pcall( function() testtable["a"]="b" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a="c"', pcall( function() testtable.a="c" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a=nil', pcall( function() testtable.a=nil end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't[nil]="d"', shoulderr( pcall( function() testtable[nil]="d" end ) ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
print( 't[nil]=nil', shoulderr( pcall( function() testtable[nil]=nil end ) ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
end
print( '-- sort tests' )
local function tryall(cmp)
local function try(t)
print( table.concat(t,'-') )
if pcall( table.sort, t, cmp ) then
print( table.concat(t,'-') )
else
print( 'sort failed' )
end
end
try{ 2, 4, 6, 8, 1, 3, 5, 7 }
try{ 333, 222, 111 }
try{ "www", "xxx", "yyy", "aaa", "bbb", "ccc" }
try{ 21, 23, "25", 27, 22, "24", 26, 28 }
end
local function comparator(a,b)
return tonumber(a)<tonumber(b)
end
print ( 'default (lexical) comparator' )
tryall()
print ( 'custom (numerical) comparator' )
tryall(comparator)

View File

@@ -0,0 +1,245 @@
2
7
-- concat tests
onetwothree
one--two--three
two,three
two
onetwothreefourfive
one--two--three--four--five
two,three,four,five
two
-- insert, len tests
{[1]=one,[2]=two,[3]=three,[a]=aaa,[b]=bbb,[c]=ccc} 3
{[1]=one,[2]=two,[3]=three,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
{[1]=seven,[2]=one,[3]=two,[4]=three,[5]=six,[a]=aaa,[b]=bbb,[c]=ccc} 5
{[1]=seven,[2]=one,[3]=two,[4]=eight,[5]=three,[6]=six,[a]=aaa,[b]=bbb,[c]=ccc} 6
{[1]=seven,[2]=one,[3]=two,[4]=eight,[5]=three,[6]=six,[7]=nine,[a]=aaa,[b]=bbb,[c]=ccc} 7
{[10]=ten,[1]=seven,[2]=one,[3]=two,[4]=eight,[5]=three,[6]=six,[7]=nine,[a]=aaa,[b]=bbb,[c]=ccc} 7
#{} 0
#{"a"} 1
#{"a","b"} 2
#{"a",nil} 1
#{nil,nil} 0
#{nil,"b"} true
#{"a","b","c"} 3
#{"a","b",nil} 2
#{"a",nil,nil} 1
#{nil,nil,nil} 0
#{nil,nil,"c"} true
#{nil,"b","c"} true
#{nil,"b",nil} true
#{"a",nil,"c"} true
-- remove tests
{[10]=ten,[1]=one,[2]=two,[3]=three,[4]=four,[5]=five,[6]=six,[7]=seven,[a]=aaa,[b]=bbb,[c]=ccc} 7
table.remove(t) seven
{[10]=ten,[1]=one,[2]=two,[3]=three,[4]=four,[5]=five,[6]=six,[a]=aaa,[b]=bbb,[c]=ccc} 6
table.remove(t,1) one
{[10]=ten,[1]=two,[2]=three,[3]=four,[4]=five,[5]=six,[a]=aaa,[b]=bbb,[c]=ccc} 5
table.remove(t,3) four
{[10]=ten,[1]=two,[2]=three,[3]=five,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
table.remove(t,5)
{[10]=ten,[1]=two,[2]=three,[3]=five,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
table.remove(t,10)
{[10]=ten,[1]=two,[2]=three,[3]=five,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
table.remove(t,-1)
{[10]=ten,[1]=two,[2]=three,[3]=five,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
table.remove(t,-1)
{[10]=ten,[1]=two,[2]=three,[3]=five,[4]=six,[a]=aaa,[b]=bbb,[c]=ccc} 4
-- sort tests
one-two-three
one-three-two
www-vvv-uuu-ttt-sss-zzz-yyy-xxx
sss-ttt-uuu-vvv-www-xxx-yyy-zzz
www-vvv-uuu-ttt-sss-zzz-yyy-xxx
zzz-yyy-xxx-www-vvv-uuu-ttt-sss
----- unpack tests -------
pcall(unpack) false
pcall(unpack,nil) false
pcall(unpack,"abc") false
pcall(unpack,1) false
unpack({"aa"}) aa
unpack({"aa","bb"}) aa bb
unpack({"aa","bb","cc"}) aa bb cc
unpack -
unpack a a
unpack . nil
unpack ab a b
unpack .b nil b
unpack a. a nil
unpack abc a b c
unpack .ab nil a b
unpack a.b a nil b
unpack ab. a b nil
unpack ..b nil nil b
unpack a.. a nil nil
unpack .b. nil b nil
unpack ... nil nil nil
unpack (-)
unpack (a) a
unpack (.) nil
unpack (ab) a b
unpack (.b) nil b
unpack (a.) a nil
unpack (abc) a b c
unpack (.ab) nil a b
unpack (a.b) a nil b
unpack (ab.) a b nil
unpack (..b) nil nil b
unpack (a..) a nil nil
unpack (.b.) nil b nil
unpack (...) nil nil nil
pcall(unpack,t) true aa bb cc dd ee ff
pcall(unpack,t,2) true bb cc dd ee ff
pcall(unpack,t,2,5) true bb cc dd ee
pcall(unpack,t,2,6) true bb cc dd ee ff
pcall(unpack,t,2,7) true bb cc dd ee ff nil
pcall(unpack,t,1) true aa bb cc dd ee ff
pcall(unpack,t,1,5) true aa bb cc dd ee
pcall(unpack,t,1,6) true aa bb cc dd ee ff
pcall(unpack,t,1,7) true aa bb cc dd ee ff nil
pcall(unpack,t,0) true nil aa bb cc dd ee ff
pcall(unpack,t,0,5) true nil aa bb cc dd ee
pcall(unpack,t,0,6) true nil aa bb cc dd ee ff
pcall(unpack,t,0,7) true nil aa bb cc dd ee ff nil
pcall(unpack,t,-1) true nil nil aa bb cc dd ee ff
pcall(unpack,t,-1,5) true nil nil aa bb cc dd ee
pcall(unpack,t,-1,6) true nil nil aa bb cc dd ee ff
pcall(unpack,t,-1,7) true nil nil aa bb cc dd ee ff nil
pcall(unpack,t,2,4) true bb cc dd
pcall(unpack,t,2,5) true bb cc dd ee
pcall(unpack,t,2,6) true bb cc dd ee ff
pcall(unpack,t,2,7) true bb cc dd ee ff nil
pcall(unpack,t,2,8) true bb cc dd ee ff nil nil
pcall(unpack,t,2,2) true
pcall(unpack,t,2,1) true
pcall(unpack,t,2,0) true
pcall(unpack,t,2,-1) true
pcall(unpack,t,0) true zz aa bb cc dd ee ff
pcall(unpack,t,2,0) true
pcall(unpack,t,2,-1) true
pcall(unpack,t,"3") true cc dd ee ff
pcall(unpack,t,"a") false
pcall(unpack,t,function() end) false
----- misc table initializer tests -------
3
4
4
----- basic table operations -------
------ basic table tests on basic table table
t[1]=2 true
t[1] true 2
t[1]=nil true
t[1] true nil
t["a"]="b" true
t["a"],t.a true b b
t.a="c" true
t["a"],t.a true c c
t.a=nil true
t["a"],t.a true nil nil
t[nil]="d" false string
t[nil] true nil
t[nil]=nil false string
t[nil] true nil
------ basic table tests on function metatable on __index table
t[1]=2 true
t[1] true 2
t[1]=nil true
metatable call args table 1
t[1] true dummy
t["a"]="b" true
t["a"],t.a true b b
t.a="c" true
t["a"],t.a true c c
t.a=nil true
metatable call args table a
metatable call args table a
t["a"],t.a true dummy dummy
t[nil]="d" false string
metatable call args table nil
t[nil] true dummy
t[nil]=nil false string
metatable call args table nil
t[nil] true dummy
------ basic table tests on function metatable on __newindex table
metatable call args table 1 2
t[1]=2 true
t[1] true nil
metatable call args table 1 nil
t[1]=nil true
t[1] true nil
metatable call args table a b
t["a"]="b" true
t["a"],t.a true nil nil
metatable call args table a c
t.a="c" true
t["a"],t.a true nil nil
metatable call args table a nil
t.a=nil true
t["a"],t.a true nil nil
metatable call args table nil d
t[nil]="d" true nil
t[nil] true nil
metatable call args table nil nil
t[nil]=nil true nil
t[nil] true nil
------ basic table tests on plain metatable on __index table
t[1]=2 true
t[1] true 2
t[1]=nil true
t[1] true nil
t["a"]="b" true
t["a"],t.a true b b
t.a="c" true
t["a"],t.a true c c
t.a=nil true
t["a"],t.a true nil nil
t[nil]="d" false string
t[nil] true nil
t[nil]=nil false string
t[nil] true nil
------ basic table tests on plain metatable on __newindex table
t[1]=2 true
t[1] true 2
t[1]=nil true
t[1] true nil
t["a"]="b" true
t["a"],t.a true b b
t.a="c" true
t["a"],t.a true c c
t.a=nil true
t["a"],t.a true nil nil
t[nil]="d" false string
t[nil] true nil
t[nil]=nil false string
t[nil] true nil
-- sort tests
default (lexical) comparator
2-4-6-8-1-3-5-7
1-2-3-4-5-6-7-8
333-222-111
111-222-333
www-xxx-yyy-aaa-bbb-ccc
aaa-bbb-ccc-www-xxx-yyy
21-23-25-27-22-24-26-28
sort failed
custom (numerical) comparator
2-4-6-8-1-3-5-7
1-2-3-4-5-6-7-8
333-222-111
111-222-333
www-xxx-yyy-aaa-bbb-ccc
sort failed
21-23-25-27-22-24-26-28
21-22-23-24-25-26-27-28

View File

@@ -0,0 +1,153 @@
-- tostring replacement that assigns ids
local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'}
tostring = function(x)
if not x or not types[type(x)] then return ts(x) end
if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end
return id[x]
end
function a()
return pcall( function() end )
end
function b()
return pcall( function() print 'b' end )
end
function c()
return pcall( function() return 'c' end )
end
print( pcall( a ) )
print( pcall( b ) )
print( pcall( c ) )
local function sum(...)
local s = 0
for i,v in ipairs({...}) do
s = s + v
end
return s
end
local function f1(n,a,b,c)
local a = a or 0
local b = b or 0
local c = c or 0
if n <= 0 then
return a,a+b,a+b+c
end
return f1(n-1,a,a+b,a+b+c)
end
local function f2(n,...)
if n <= 0 then
print( " --f2, n<=0, returning sum(...)", ... )
return sum(...)
end
print( " --f2, n>0, returning f2(n-1,n,...)", n-1,n,... )
return f2(n-1,n,...)
end
local function f3(n,...)
if n <= 0 then
return sum(...)
end
print( " f3,n-1,n,...", f3,n-1,n,... )
return pcall(f3,n-1,n,...)
end
local function all(f)
for n=0,3 do
t = {}
for m=1,5 do
print( "--f, n, table.unpack(t)", f, n, table.unpack(t) )
print( pcall( f, n, table.unpack(t)) )
t[m] = m
end
end
end
all(f1)
all(f2)
all(f3)
local function f(x)
-- tailcall to a builtin
return math.abs(x)
end
local function factorial(i)
local function helper(product, n)
if n <= 0 then
return product
else
-- tail call to a nested Lua function
return helper(n * product, n - 1)
end
end
return helper(1, i)
end
local result1 = factorial(5)
print(result1)
print(factorial(5))
local result2 = f(-1234)
print( result2 )
local function fib_bad(n)
local function helper(i, a, b)
if i >= n then
return a
else
-- not recognized by luac as a tailcall!
local result = helper(i + 1, b, a + b)
return result
end
end
return helper(1, 1, 1)
end
local function fib_good(n)
local function helper(i, a, b)
if i >= n then
return a
else
-- must be a tail call!
return helper(i + 1, b, a + b)
end
end
return helper(1, 1, 1)
end
local aliases = {
['1.#INF'] = 'inf',
['-1.#INF'] = '-inf',
['1.#IND'] = 'nan',
['-1.#IND'] = 'nan',
}
local p = function( s,e )
print( s, e and aliases[tostring(e)] or e )
end
p(pcall(fib_bad, 30))
--p((pcall(fib_bad, 25000)))
p(pcall(fib_good, 30))
p(pcall(fib_good, 25000))
local function fib_all(n, i, a, b)
i = i or 1
a = a or 1
b = b or 1
if i >= n then
return
else
return a, fib_all(n, i+1, b, a+b)
end
end
print(fib_all(10))

View File

@@ -0,0 +1,211 @@
true true
b
true true
true true c
--f, n, table.unpack(t) func.1 0
true 0 0 0
--f, n, table.unpack(t) func.1 0 1
true 1 1 1
--f, n, table.unpack(t) func.1 0 1 2
true 1 3 3
--f, n, table.unpack(t) func.1 0 1 2 3
true 1 3 6
--f, n, table.unpack(t) func.1 0 1 2 3 4
true 1 3 6
--f, n, table.unpack(t) func.1 1
true 0 0 0
--f, n, table.unpack(t) func.1 1 1
true 1 2 3
--f, n, table.unpack(t) func.1 1 1 2
true 1 4 7
--f, n, table.unpack(t) func.1 1 1 2 3
true 1 4 10
--f, n, table.unpack(t) func.1 1 1 2 3 4
true 1 4 10
--f, n, table.unpack(t) func.1 2
true 0 0 0
--f, n, table.unpack(t) func.1 2 1
true 1 3 6
--f, n, table.unpack(t) func.1 2 1 2
true 1 5 12
--f, n, table.unpack(t) func.1 2 1 2 3
true 1 5 15
--f, n, table.unpack(t) func.1 2 1 2 3 4
true 1 5 15
--f, n, table.unpack(t) func.1 3
true 0 0 0
--f, n, table.unpack(t) func.1 3 1
true 1 4 10
--f, n, table.unpack(t) func.1 3 1 2
true 1 6 18
--f, n, table.unpack(t) func.1 3 1 2 3
true 1 6 21
--f, n, table.unpack(t) func.1 3 1 2 3 4
true 1 6 21
--f, n, table.unpack(t) func.2 0
--f2, n<=0, returning sum(...)
true 0
--f, n, table.unpack(t) func.2 0 1
--f2, n<=0, returning sum(...) 1
true 1
--f, n, table.unpack(t) func.2 0 1 2
--f2, n<=0, returning sum(...) 1 2
true 3
--f, n, table.unpack(t) func.2 0 1 2 3
--f2, n<=0, returning sum(...) 1 2 3
true 6
--f, n, table.unpack(t) func.2 0 1 2 3 4
--f2, n<=0, returning sum(...) 1 2 3 4
true 10
--f, n, table.unpack(t) func.2 1
--f2, n>0, returning f2(n-1,n,...) 0 1
--f2, n<=0, returning sum(...) 1
true 1
--f, n, table.unpack(t) func.2 1 1
--f2, n>0, returning f2(n-1,n,...) 0 1 1
--f2, n<=0, returning sum(...) 1 1
true 2
--f, n, table.unpack(t) func.2 1 1 2
--f2, n>0, returning f2(n-1,n,...) 0 1 1 2
--f2, n<=0, returning sum(...) 1 1 2
true 4
--f, n, table.unpack(t) func.2 1 1 2 3
--f2, n>0, returning f2(n-1,n,...) 0 1 1 2 3
--f2, n<=0, returning sum(...) 1 1 2 3
true 7
--f, n, table.unpack(t) func.2 1 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 0 1 1 2 3 4
--f2, n<=0, returning sum(...) 1 1 2 3 4
true 11
--f, n, table.unpack(t) func.2 2
--f2, n>0, returning f2(n-1,n,...) 1 2
--f2, n>0, returning f2(n-1,n,...) 0 1 2
--f2, n<=0, returning sum(...) 1 2
true 3
--f, n, table.unpack(t) func.2 2 1
--f2, n>0, returning f2(n-1,n,...) 1 2 1
--f2, n>0, returning f2(n-1,n,...) 0 1 2 1
--f2, n<=0, returning sum(...) 1 2 1
true 4
--f, n, table.unpack(t) func.2 2 1 2
--f2, n>0, returning f2(n-1,n,...) 1 2 1 2
--f2, n>0, returning f2(n-1,n,...) 0 1 2 1 2
--f2, n<=0, returning sum(...) 1 2 1 2
true 6
--f, n, table.unpack(t) func.2 2 1 2 3
--f2, n>0, returning f2(n-1,n,...) 1 2 1 2 3
--f2, n>0, returning f2(n-1,n,...) 0 1 2 1 2 3
--f2, n<=0, returning sum(...) 1 2 1 2 3
true 9
--f, n, table.unpack(t) func.2 2 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 1 2 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 0 1 2 1 2 3 4
--f2, n<=0, returning sum(...) 1 2 1 2 3 4
true 13
--f, n, table.unpack(t) func.2 3
--f2, n>0, returning f2(n-1,n,...) 2 3
--f2, n>0, returning f2(n-1,n,...) 1 2 3
--f2, n>0, returning f2(n-1,n,...) 0 1 2 3
--f2, n<=0, returning sum(...) 1 2 3
true 6
--f, n, table.unpack(t) func.2 3 1
--f2, n>0, returning f2(n-1,n,...) 2 3 1
--f2, n>0, returning f2(n-1,n,...) 1 2 3 1
--f2, n>0, returning f2(n-1,n,...) 0 1 2 3 1
--f2, n<=0, returning sum(...) 1 2 3 1
true 7
--f, n, table.unpack(t) func.2 3 1 2
--f2, n>0, returning f2(n-1,n,...) 2 3 1 2
--f2, n>0, returning f2(n-1,n,...) 1 2 3 1 2
--f2, n>0, returning f2(n-1,n,...) 0 1 2 3 1 2
--f2, n<=0, returning sum(...) 1 2 3 1 2
true 9
--f, n, table.unpack(t) func.2 3 1 2 3
--f2, n>0, returning f2(n-1,n,...) 2 3 1 2 3
--f2, n>0, returning f2(n-1,n,...) 1 2 3 1 2 3
--f2, n>0, returning f2(n-1,n,...) 0 1 2 3 1 2 3
--f2, n<=0, returning sum(...) 1 2 3 1 2 3
true 12
--f, n, table.unpack(t) func.2 3 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 2 3 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 1 2 3 1 2 3 4
--f2, n>0, returning f2(n-1,n,...) 0 1 2 3 1 2 3 4
--f2, n<=0, returning sum(...) 1 2 3 1 2 3 4
true 16
--f, n, table.unpack(t) func.3 0
true 0
--f, n, table.unpack(t) func.3 0 1
true 1
--f, n, table.unpack(t) func.3 0 1 2
true 3
--f, n, table.unpack(t) func.3 0 1 2 3
true 6
--f, n, table.unpack(t) func.3 0 1 2 3 4
true 10
--f, n, table.unpack(t) func.3 1
f3,n-1,n,... func.3 0 1
true true 1
--f, n, table.unpack(t) func.3 1 1
f3,n-1,n,... func.3 0 1 1
true true 2
--f, n, table.unpack(t) func.3 1 1 2
f3,n-1,n,... func.3 0 1 1 2
true true 4
--f, n, table.unpack(t) func.3 1 1 2 3
f3,n-1,n,... func.3 0 1 1 2 3
true true 7
--f, n, table.unpack(t) func.3 1 1 2 3 4
f3,n-1,n,... func.3 0 1 1 2 3 4
true true 11
--f, n, table.unpack(t) func.3 2
f3,n-1,n,... func.3 1 2
f3,n-1,n,... func.3 0 1 2
true true true 3
--f, n, table.unpack(t) func.3 2 1
f3,n-1,n,... func.3 1 2 1
f3,n-1,n,... func.3 0 1 2 1
true true true 4
--f, n, table.unpack(t) func.3 2 1 2
f3,n-1,n,... func.3 1 2 1 2
f3,n-1,n,... func.3 0 1 2 1 2
true true true 6
--f, n, table.unpack(t) func.3 2 1 2 3
f3,n-1,n,... func.3 1 2 1 2 3
f3,n-1,n,... func.3 0 1 2 1 2 3
true true true 9
--f, n, table.unpack(t) func.3 2 1 2 3 4
f3,n-1,n,... func.3 1 2 1 2 3 4
f3,n-1,n,... func.3 0 1 2 1 2 3 4
true true true 13
--f, n, table.unpack(t) func.3 3
f3,n-1,n,... func.3 2 3
f3,n-1,n,... func.3 1 2 3
f3,n-1,n,... func.3 0 1 2 3
true true true true 6
--f, n, table.unpack(t) func.3 3 1
f3,n-1,n,... func.3 2 3 1
f3,n-1,n,... func.3 1 2 3 1
f3,n-1,n,... func.3 0 1 2 3 1
true true true true 7
--f, n, table.unpack(t) func.3 3 1 2
f3,n-1,n,... func.3 2 3 1 2
f3,n-1,n,... func.3 1 2 3 1 2
f3,n-1,n,... func.3 0 1 2 3 1 2
true true true true 9
--f, n, table.unpack(t) func.3 3 1 2 3
f3,n-1,n,... func.3 2 3 1 2 3
f3,n-1,n,... func.3 1 2 3 1 2 3
f3,n-1,n,... func.3 0 1 2 3 1 2 3
true true true true 12
--f, n, table.unpack(t) func.3 3 1 2 3 4
f3,n-1,n,... func.3 2 3 1 2 3 4
f3,n-1,n,... func.3 1 2 3 1 2 3 4
f3,n-1,n,... func.3 0 1 2 3 1 2 3 4
true true true true 16
120
120
1234
true 832040
true 832040
true inf
1 1 2 3 5 8 13 21 34

View File

@@ -0,0 +1,97 @@
print( '-------- simple upvalues tests --------' )
local function simpleupvalues()
function test()
local x = 5
function f()
x = x + 1
return x
end
function g()
x = x - 1
return x
end
print(f())
print(g())
return f, g
end
f1, g1 = test()
print("f1()=", f1())
print("g1()=", g1())
f2, g2 = test()
print("f2()=", f2())
print("g2()=", g2())
print("g1()=", g1())
print("f1()=", f1())
end
print( 'simplevalues result:', pcall( simpleupvalues ) )
-- The point of this test is that when an upvalue is created, it may
-- need to be inserted in the middle of the list, rather than always
-- appended at the end. Otherwise, it may not be found when it is
-- needed by another closure.
print( '----------- upvalued in middle ------------' )
local function middleupvaluestest()
local function test()
local x = 3
local y = 5
local z = 7
local function f()
print("y=", y)
end
local function g()
print("z=", z)
end
local function h()
print("x=", x)
end
local function setter(x1, y1, z1)
x = x1
y = y1
z = z1
end
return f, g, h, setter
end
local f, g, h, setter = test()
h()
f()
g()
setter("x", "y", "z")
h()
f()
g()
end
print( pcall( middleupvaluestest ) )
print( '--------- nested upvalues ----------' )
local function nestedupvaluestest()
local f
do
local x = 10
function g()
print(x, f())
end
end
function f()
return 20
end
g()
end
print( 'nestedupvaluestest result:', pcall( nestedupvaluestest ) )

View File

@@ -0,0 +1,23 @@
-------- simple upvalues tests --------
6
5
f1()= 6
g1()= 5
6
5
f2()= 6
g2()= 5
g1()= 4
f1()= 5
simplevalues result: true
----------- upvalued in middle ------------
x= 3
y= 5
z= 7
x= x
y= y
z= z
true
--------- nested upvalues ----------
10 20
nestedupvaluestest result: true

View File

@@ -0,0 +1,250 @@
print( '-------- basic vm tests --------' )
print( '-- boolean tests' )
local function booleantests()
t = true
f = false
n = nil
s = "Hello"
z = 0
one = 1
print(t)
print(f)
print(not t)
print(not f)
print(not n)
print(not z)
print(not s)
print(not(not(t)))
print(not(not(z)))
print(not(not(n)))
print(t and f)
print(t or f)
print(f and t)
print(f or t)
print(f or one)
print(f or z)
print(f or n)
print(t and one)
print(t and z)
print(t and n)
end
print( 'booleantests result:', pcall( booleantests ) )
print( '------------- varargs' )
local function varargstest()
function p(a,...)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function q(a,...)
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
end
function r(a,...)
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function s(a)
local arg = { '1', '2', '3' }
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a",a)
end
function t(a,...)
local arg = { '1', '2', '3' }
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function u(arg)
print( 'arg', arg )
end
function v(arg,...)
print( 'arg', arg )
print("...",...)
print("arg,...",arg,...)
end
arg = { "global-1", "global-2", "global-3" }
function tryall(f,name)
print( '---- function '..name..'()' )
print( '--'..name..'():' )
print( ' ->', pcall( f ) )
print( '--'..name..'("q"):' )
print( ' ->', pcall( f, "q" ) )
print( '--'..name..'("q","r"):' )
print( ' ->', pcall( f, "q", "r" ) )
print( '--'..name..'("q","r","s"):' )
print( ' ->', pcall( f, "q", "r", "s" ) )
end
tryall(p,'p')
tryall(q,'q')
tryall(r,'r')
tryall(s,'s')
tryall(t,'t')
tryall(u,'u')
tryall(v,'v')
end
print( 'varargstest result:', pcall( varargstest ) )
-- The purpose of this test case is to demonstrate that
-- basic metatable operations on non-table types work.
-- i.e. that s.sub(s,...) could be used in place of string.sub(s,...)
print( '---------- metatable tests' )
local function metatabletests()
-- tostring replacement that assigns ids
local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'}
tostring = function(x)
if not x or not types[type(x)] then return ts(x) end
if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end
return id[x]
end
local results = function(s,e,...)
if s then return e,... end
return false,type(e)
end
local pcall = function(...)
return results( pcall(...) )
end
local s = "hello"
print(s:sub(2,4))
local t = {}
function op(name,...)
local a,b = pcall( setmetatable, t, ... )
print( name, t, getmetatable(t), a, b )
end
op('set{} ',{})
op('set-nil',nil)
op('set{} ',{})
op('set')
op('set{} ',{})
op('set{} ',{})
op('set{}{}',{},{})
op('set-nil',nil)
op('set{__}',{__metatable={}})
op('set{} ',{})
op('set-nil',nil)
t = {}
op('set{} ',{})
op('set-nil',nil)
op('set{__}',{__metatable='abc'})
op('set{} ',{})
op('set-nil',nil)
local i = 1234
local t = setmetatable( {}, {
__mode="v",
__index=function(t,k)
local v = i
i = i + 1
rawset(t,k,v)
return v
end,
} )
local l = { 'a', 'b', 'a', 'b', 'c', 'a', 'b', 'c', 'd' }
for i,key in ipairs(l) do
print( 't.'..key, t[key] )
end
end
print( 'metatabletests result:', pcall( metatabletests ) )
-- test tables with more than 50 elements
print( '------------ huge tables' )
local function hugetables()
local t = { 1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
}
print ("#t=",#t,'t[1,50,51,59]', t[1], t[50], t[51], t[59])
print (table.concat(t,','))
local t2= { 0,3,4,7,9,8,12,15,23,5,
10,13,14,17,19,18,112,115,123,15,
20,33,24,27,29,28,212,215,223,25,
40,43,44,47,49,48,412,415,423,45,
50,53,54,57,59,58,512,515,523,55,
60,63,64,67,69,68,612,615,623,65,
70,73,74,77,79,78,72,715,723,75,
}
print ("#t2=",#t2,'t[1,50,51,59]', t[1], t[50], t[51], t[59])
print (table.concat(t2,','))
local t = {
[2000]='a', [2001]='b', [2002]='c', [2003]='d', [2004]='e', [2005]='f', [2006]='g', [2007]='h', [2008]='i', [2009]='j',
[3000]='a', [3001]='b', [3002]='c', [3003]='d', [3004]='e', [3005]='f', [3006]='g', [3007]='h', [3008]='i', [3009]='j',
[4000]='a', [4001]='b', [4002]='c', [4003]='d', [4004]='e', [4005]='f', [4006]='g', [4007]='h', [4008]='i', [4009]='j',
[5000]='a', [5001]='b', [5002]='c', [5003]='d', [5004]='e', [5005]='f', [5006]='g', [5007]='h', [5008]='i', [5009]='j',
[6000]='a', [6001]='b', [6002]='c', [6003]='d', [6004]='e', [6005]='f', [6006]='g', [6007]='h', [6008]='i', [6009]='j',
[7000]='a', [7001]='b', [7002]='c', [7003]='d', [7004]='e', [7005]='f', [7006]='g', [7007]='h', [7008]='i', [7009]='j',
[8000]='a', [8001]='b', [8002]='c', [8003]='d', [8004]='e', [8005]='f', [8006]='g', [8007]='h', [8008]='i', [8009]='j',
}
for i=2000,8000,1000 do
for j=0,9,1 do
print( 't['..tostring(i+j)..']', t[i+j] )
end
end
end
print( 'hugetables result:', pcall( hugetables ) )
print( '--------- many locals' )
local function manylocals()
-- test program with more than 50 non-sequential integer elements
local t0000='a'; local t0001='b'; local t0002='c'; local t0003='d'; local t0004='e'; local t0005='f'; local t0006='g'; local t0007='h'; local t0008='i'; local t0009='j';
local t1000='a'; local t1001='b'; local t1002='c'; local t1003='d'; local t1004='e'; local t1005='f'; local t1006='g'; local t1007='h'; local t1008='i'; local t1009='j';
local t2000='a'; local t2001='b'; local t2002='c'; local t2003='d'; local t2004='e'; local t2005='f'; local t2006='g'; local t2007='h'; local t2008='i'; local t2009='j';
local t3000='a'; local t3001='b'; local t3002='c'; local t3003='d'; local t3004='e'; local t3005='f'; local t3006='g'; local t3007='h'; local t3008='i'; local t3009='j';
local t4000='a'; local t4001='b'; local t4002='c'; local t4003='d'; local t4004='e'; local t4005='f'; local t4006='g'; local t4007='h'; local t4008='i'; local t4009='j';
local t5000='a'; local t5001='b'; local t5002='c'; local t5003='d'; local t5004='e'; local t5005='f'; local t5006='g'; local t5007='h'; local t5008='i'; local t5009='j';
local t6000='a'; local t6001='b'; local t6002='c'; local t6003='d'; local t6004='e'; local t6005='f'; local t6006='g'; local t6007='h'; local t6008='i'; local t6009='j';
local t7000='a'; local t7001='b'; local t7002='c'; local t7003='d'; local t7004='e'; local t7005='f'; local t7006='g'; local t7007='h'; local t7008='i'; local t7009='j';
local t8000='a'; local t8001='b'; local t8002='c'; local t8003='d'; local t8004='e'; local t8005='f'; local t8006='g'; local t8007='h'; local t8008='i'; local t8009='j';
local t9000='a'; local t9001='b'; local t9002='c'; local t9003='d'; local t9004='e'; local t9005='f'; local t9006='g'; local t9007='h'; local t9008='i'; local t9009='j';
local t10000='a'; local t10001='b'; local t10002='c'; local t10003='d'; local t10004='e'; local t10005='f'; local t10006='g'; local t10007='h'; local t10008='i'; local t10009='j';
local t11000='a'; local t11001='b'; local t11002='c'; local t11003='d'; local t11004='e'; local t11005='f'; local t11006='g'; local t11007='h'; local t11008='i'; local t11009='j';
local t12000='a'; local t12001='b'; local t12002='c'; local t12003='d'; local t12004='e'; local t12005='f'; local t12006='g'; local t12007='h'; local t12008='i'; local t12009='j';
local t13000='a'; local t13001='b'; local t13002='c'; local t13003='d'; local t13004='e'; local t13005='f'; local t13006='g'; local t13007='h'; local t13008='i'; local t13009='j';
-- print the variables
print(t0000,'a'); print(t0001,'b'); print(t0002,'c'); print(t0003,'d'); print(t0004,'e'); print(t0005,'f'); print(t0006,'g'); print(t0007,'h'); print(t0008,'i'); print(t0009,'j');
print(t1000,'a'); print(t1001,'b'); print(t1002,'c'); print(t1003,'d'); print(t1004,'e'); print(t1005,'f'); print(t1006,'g'); print(t1007,'h'); print(t1008,'i'); print(t1009,'j');
print(t2000,'a'); print(t2001,'b'); print(t2002,'c'); print(t2003,'d'); print(t2004,'e'); print(t2005,'f'); print(t2006,'g'); print(t2007,'h'); print(t2008,'i'); print(t2009,'j');
print(t3000,'a'); print(t3001,'b'); print(t3002,'c'); print(t3003,'d'); print(t3004,'e'); print(t3005,'f'); print(t3006,'g'); print(t3007,'h'); print(t3008,'i'); print(t3009,'j');
print(t4000,'a'); print(t4001,'b'); print(t4002,'c'); print(t4003,'d'); print(t4004,'e'); print(t4005,'f'); print(t4006,'g'); print(t4007,'h'); print(t4008,'i'); print(t4009,'j');
print(t5000,'a'); print(t5001,'b'); print(t5002,'c'); print(t5003,'d'); print(t5004,'e'); print(t5005,'f'); print(t5006,'g'); print(t5007,'h'); print(t5008,'i'); print(t5009,'j');
print(t6000,'a'); print(t6001,'b'); print(t6002,'c'); print(t6003,'d'); print(t6004,'e'); print(t6005,'f'); print(t6006,'g'); print(t6007,'h'); print(t6008,'i'); print(t6009,'j');
print(t7000,'a'); print(t7001,'b'); print(t7002,'c'); print(t7003,'d'); print(t7004,'e'); print(t7005,'f'); print(t7006,'g'); print(t7007,'h'); print(t7008,'i'); print(t7009,'j');
print(t8000,'a'); print(t8001,'b'); print(t8002,'c'); print(t8003,'d'); print(t8004,'e'); print(t8005,'f'); print(t8006,'g'); print(t8007,'h'); print(t8008,'i'); print(t8009,'j');
print(t9000,'a'); print(t9001,'b'); print(t9002,'c'); print(t9003,'d'); print(t9004,'e'); print(t9005,'f'); print(t9006,'g'); print(t9007,'h'); print(t9008,'i'); print(t9009,'j');
print(t10000,'a'); print(t10001,'b'); print(t10002,'c'); print(t10003,'d'); print(t10004,'e'); print(t10005,'f'); print(t10006,'g'); print(t10007,'h'); print(t10008,'i'); print(t10009,'j');
print(t11000,'a'); print(t11001,'b'); print(t11002,'c'); print(t11003,'d'); print(t11004,'e'); print(t11005,'f'); print(t11006,'g'); print(t11007,'h'); print(t11008,'i'); print(t11009,'j');
print(t12000,'a'); print(t12001,'b'); print(t12002,'c'); print(t12003,'d'); print(t12004,'e'); print(t12005,'f'); print(t12006,'g'); print(t12007,'h'); print(t12008,'i'); print(t12009,'j');
print(t13000,'a'); print(t13001,'b'); print(t13002,'c'); print(t13003,'d'); print(t13004,'e'); print(t13005,'f'); print(t13006,'g'); print(t13007,'h'); print(t13008,'i'); print(t13009,'j');
end
print( 'manylocals result:', pcall( manylocals ) )

View File

@@ -0,0 +1,418 @@
-------- basic vm tests --------
-- boolean tests
true
false
false
true
true
false
false
true
true
false
false
true
false
true
1
0
nil
1
0
nil
booleantests result: true
------------- varargs
---- function p()
--p():
a nil
...
...,a nil nil
a,... nil
-> true
--p("q"):
a q
...
...,a nil q
a,... q
-> true
--p("q","r"):
a q
... r
...,a r q
a,... q r
-> true
--p("q","r","s"):
a q
... r s
...,a r q
a,... q r s
-> true
---- function q()
--q():
a,arg[1],arg[2],arg[3] nil nil global-1 global-2 global-3
-> true
--q("q"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
-> true
--q("q","r"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
-> true
--q("q","r","s"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
-> true
---- function r()
--r():
a,arg[1],arg[2],arg[3] nil nil global-1 global-2 global-3
a nil
...
...,a nil nil
a,... nil
-> true
--r("q"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
a q
...
...,a nil q
a,... q
-> true
--r("q","r"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
a q
... r
...,a r q
a,... q r
-> true
--r("q","r","s"):
a,arg[1],arg[2],arg[3] q nil global-1 global-2 global-3
a q
... r s
...,a r q
a,... q r s
-> true
---- function s()
--s():
a,arg[1],arg[2],arg[3] nil 1 2 3
a nil
-> true
--s("q"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
-> true
--s("q","r"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
-> true
--s("q","r","s"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
-> true
---- function t()
--t():
a,arg[1],arg[2],arg[3] nil 1 2 3
a nil
...
...,a nil nil
a,... nil
-> true
--t("q"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
...
...,a nil q
a,... q
-> true
--t("q","r"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
... r
...,a r q
a,... q r
-> true
--t("q","r","s"):
a,arg[1],arg[2],arg[3] q 1 2 3
a q
... r s
...,a r q
a,... q r s
-> true
---- function u()
--u():
arg nil
-> true
--u("q"):
arg q
-> true
--u("q","r"):
arg q
-> true
--u("q","r","s"):
arg q
-> true
---- function v()
--v():
arg nil
...
arg,... nil
-> true
--v("q"):
arg q
...
arg,... q
-> true
--v("q","r"):
arg q
... r
arg,... q r
-> true
--v("q","r","s"):
arg q
... r s
arg,... q r s
-> true
varargstest result: true
---------- metatable tests
ell
set{} tbl.1 tbl.2 tbl.1 nil
set-nil tbl.1 nil tbl.1 nil
set{} tbl.1 tbl.3 tbl.1 nil
set tbl.1 tbl.3 false string
set{} tbl.1 tbl.4 tbl.1 nil
set{} tbl.1 tbl.5 tbl.1 nil
set{}{} tbl.1 tbl.6 tbl.1 nil
set-nil tbl.1 nil tbl.1 nil
set{__} tbl.1 tbl.7 tbl.1 nil
set{} tbl.1 tbl.7 false string
set-nil tbl.1 tbl.7 false string
set{} tbl.8 tbl.9 tbl.8 nil
set-nil tbl.8 nil tbl.8 nil
set{__} tbl.8 abc tbl.8 nil
set{} tbl.8 abc false string
set-nil tbl.8 abc false string
t.a 1234
t.b 1235
t.a 1234
t.b 1235
t.c 1236
t.a 1234
t.b 1235
t.c 1236
t.d 1237
metatabletests result: true
------------ huge tables
#t= 100 t[1,50,51,59] 1 1 1 1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
#t2= 70 t[1,50,51,59] 1 1 1 1
0,3,4,7,9,8,12,15,23,5,10,13,14,17,19,18,112,115,123,15,20,33,24,27,29,28,212,215,223,25,40,43,44,47,49,48,412,415,423,45,50,53,54,57,59,58,512,515,523,55,60,63,64,67,69,68,612,615,623,65,70,73,74,77,79,78,72,715,723,75
t[2000] a
t[2001] b
t[2002] c
t[2003] d
t[2004] e
t[2005] f
t[2006] g
t[2007] h
t[2008] i
t[2009] j
t[3000] a
t[3001] b
t[3002] c
t[3003] d
t[3004] e
t[3005] f
t[3006] g
t[3007] h
t[3008] i
t[3009] j
t[4000] a
t[4001] b
t[4002] c
t[4003] d
t[4004] e
t[4005] f
t[4006] g
t[4007] h
t[4008] i
t[4009] j
t[5000] a
t[5001] b
t[5002] c
t[5003] d
t[5004] e
t[5005] f
t[5006] g
t[5007] h
t[5008] i
t[5009] j
t[6000] a
t[6001] b
t[6002] c
t[6003] d
t[6004] e
t[6005] f
t[6006] g
t[6007] h
t[6008] i
t[6009] j
t[7000] a
t[7001] b
t[7002] c
t[7003] d
t[7004] e
t[7005] f
t[7006] g
t[7007] h
t[7008] i
t[7009] j
t[8000] a
t[8001] b
t[8002] c
t[8003] d
t[8004] e
t[8005] f
t[8006] g
t[8007] h
t[8008] i
t[8009] j
hugetables result: true
--------- many locals
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
manylocals result: true