diff --git a/src/test/java/org/luaj/vm/CompatibiltyTest.java b/src/test/java/org/luaj/vm/CompatibiltyTest.java index 69f633f5..4d15c8e1 100644 --- a/src/test/java/org/luaj/vm/CompatibiltyTest.java +++ b/src/test/java/org/luaj/vm/CompatibiltyTest.java @@ -48,10 +48,6 @@ public class CompatibiltyTest extends ScriptDrivenTest { runTest("test8"); } - public void testArgtypes() throws IOException, InterruptedException { - runTest("argtypes"); - } - public void testAutoload() throws IOException, InterruptedException { runTest("autoload"); } diff --git a/src/test/res/argtypes.lua b/src/test/res/argtypes.lua deleted file mode 100644 index 4117819d..00000000 --- a/src/test/res/argtypes.lua +++ /dev/null @@ -1,57 +0,0 @@ --- object ids -package.path = "?.lua;src/test/res/?.lua" -require 'ids' - -local names = { - string= { sub=2, }, - math= { ceil=1, floor=1 }, - table={ insert=2, remove=2 }, -} - -local args = { 'str', 123, {}, function() end, print, nil } - -local globals = _G -local ipairs = ipairs -local pairs = pairs - -local function f( pkg, name, count ) - print( '-----'..pkg..'.'..name..'-----' ) - if not globals[pkg] then - print( 'package not found: '..pkg ) - return - end - if not globals[pkg][name] then - print( 'function not found: '..pkg..'.'..name ) - return - end - local function g( ... ) - return globals[pkg][name](...) - end - print( pcall( g ) ) - if count > 0 then - for i,arg1 in ipairs(args) do - print( pcall( g, arg1 ) ) - if count > 1 then - for j,arg2 in ipairs(args) do - print( pcall( g, arg1, arg2 ) ) - end - end - end - end -end - -function sortedkeys(t) - local list = {} - for k,v in pairs(t) do - table.insert(list,k) - end - table.sort(list) - return list -end -for j,pkg in ipairs(sortedkeys(names)) do - local t = names[pkg] - for i,name in ipairs(sortedkeys(t)) do - local nargs = t[name] - f( pkg, name, nargs ) - end -end \ No newline at end of file diff --git a/src/test/res/baselib.lua b/src/test/res/baselib.lua index be905f08..a2e3da83 100644 --- a/src/test/res/baselib.lua +++ b/src/test/res/baselib.lua @@ -2,12 +2,20 @@ package.path = "?.lua;src/test/res/?.lua" require 'ids' +-- 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") - -- assert print( 'assert(true)', assert(true) ) print( 'pcall(assert,true)', pcall(assert,true) ) @@ -19,9 +27,9 @@ 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")', id(collectgarbage("count"))) -print( 'collectgarbage("collect")', collectgarbage("collect")) -print( 'collectgarbage("count")', id(collectgarbage("count"))) +print( 'collectgarbage("count")', type(collectgarbage("count"))) +print( 'collectgarbage("collect")', type(collectgarbage("collect"))) +print( 'collectgarbage("count")', type(collectgarbage("count"))) -- dofile (not supported) -- ipairs @@ -39,9 +47,9 @@ for k,v in ipairs({[30]='30',[20]='20'}) do print('ipairs5',k,v)end -- loadfile -- loadstring local lst = "print(3+4); return 8" -local lss,lsv = pcall( loadstring, lst ) -print( 'loadstring("'..lst..'")', lss, id(lsv) ) -print( 'loadstring("'..lst..'")()', pcall( lsv ) ) +local chunk, err = loadstring( lst ) +print( 'loadstring("'..lst..'")', id(chunk), err ) +print( 'loadstring("'..lst..'")()', chunk() ) -- pairs print( 'pcall(pairs)', pcall(pairs) ) @@ -52,7 +60,7 @@ 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',bb='bbb'}) do print('pairs3',k,v)end for k,v in pairs({aa='aaa',bb='bbb','one','two'}) do print('pairs4',k,v)end -for k,v in pairs({[30]='30',[20]='20'}) do print('pairs5',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"] ) @@ -208,9 +216,9 @@ 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)', id(tostring(tostring)) ) -print( 'tostring(function() end)', id(tostring(function() end)) ) -print( 'tostring({"one","two",a="aa",b="bb"})', id(tostring({"one","two",a="aa",b="bb"})) ) +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"})) ) -- unpack print( 'pcall(unpack)', pcall(unpack) ); @@ -258,4 +266,4 @@ print( 'pcall(unpack,t,"a")', pcall(unpack,t,"a") ); print( 'pcall(unpack,t,function() end)', pcall(unpack,t,function() end) ); -- _VERSION -print( '_VERSION', _VERSION ) +print( '_VERSION', type(_VERSION) ) diff --git a/src/test/res/calls.lua b/src/test/res/calls.lua index 236ad84a..a7a988de 100644 --- a/src/test/res/calls.lua +++ b/src/test/res/calls.lua @@ -19,6 +19,10 @@ local result1 = factorial(5) print(result1) print(factorial(5)) +local function truncate(x) + local s = tostring(x) + return (#s<6 and s) or string.sub(s,1,6)..'...' +end local result2 = f(math.pi) -print(result2) -print(f(math.pi)) +print(truncate(result2)) +print(truncate(f(math.pi))) diff --git a/src/test/res/errors.lua b/src/test/res/errors.lua index 4f285b33..05092851 100644 --- a/src/test/res/errors.lua +++ b/src/test/res/errors.lua @@ -5,7 +5,7 @@ require 'ids' -- 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(...) return (pcall(b,...)) end s = 'some string' local t = {} -- error message tests @@ -78,11 +78,11 @@ local function concatsuite(comparefunc) print( '3.5.."b"', comparefunc(3.5,"b") ) end local function strconcat(a,b) - return pcall( function() return a..b end ) + return (pcall( function() return a..b end) ) end local function tblconcat(a,b) local t={a,b} - return pcall( function() return table.concat(t,'-') end ) + return (pcall( function() return table.concat(t,'-') end )) end -- concatsuite(strconcat) concatsuite(tblconcat) diff --git a/src/test/res/mathlib.lua b/src/test/res/mathlib.lua index 2a38da17..3b4dedb9 100644 --- a/src/test/res/mathlib.lua +++ b/src/test/res/mathlib.lua @@ -3,17 +3,31 @@ print( math.cos( math.pi ) ) print( math.sqrt( 9.0 ) ) print( math.modf( 5.25 ) ) +local aliases = { + ['nan']='', + ['inf']='', + ['-inf']='', + ['1.#INF']='', + ['-1.#INF']='', + ['1.#IND']='', + ['-1.#IND']='', +} + +local function normalized(x) + local s = tostring(x) + return aliases[s] or s +end -- binary ops function binops( a, b ) local sa = tostring(a) local sb = tostring(b) - print( sa..'+'..sb..'='..tostring(a+b) ) - print( sa..'-'..sb..'='..tostring(a-b) ) - print( sa..'*'..sb..'='..tostring(a*b) ) - print( sa..'^'..sb..'='..tostring(a^b) ) - print( sa..'/'..sb..'=',pcall( function() return a / b end ) ) - print( sa..'%'..sb..'=',pcall( function() return a % b end ) ) + print( sa..'+'..sb..'='..normalized(a+b) ) + print( sa..'-'..sb..'='..normalized(a-b) ) + print( sa..'*'..sb..'='..normalized(a*b) ) + print( sa..'^'..sb..'='..normalized(a^b) ) + print( sa..'/'..sb..'='..normalized(a/b) ) + print( sa..'%'..sb..'='..normalized(a%b) ) return '--' end print( pcall( binops, 2, 0 ) ) @@ -24,45 +38,48 @@ print( pcall( binops, 5, 2 ) ) print( pcall( binops, -5, 2 ) ) print( pcall( binops, 16, -2 ) ) print( pcall( binops, -16, -2 ) ) -print( pcall( binops, 256, .5 ) ) -print( pcall( binops, 256, .25 ) ) -print( pcall( binops, 256, .625 ) ) -print( pcall( binops, 256, -.5 ) ) -print( pcall( binops, 256, -.25 ) ) -print( pcall( binops, 256, -.625 ) ) -print( pcall( binops, -256, .5 ) ) -print( pcall( binops, .5, 0 ) ) -print( pcall( binops, .5, 1 ) ) -print( pcall( binops, .5, 2 ) ) -print( pcall( binops, .5, -1 ) ) -print( pcall( binops, .5, -2 ) ) +print( pcall( binops, 256, 0.5 ) ) +print( pcall( binops, 256, 0.25 ) ) +print( pcall( binops, 256, 0.625 ) ) +print( pcall( binops, 256, -0.5 ) ) +print( pcall( binops, 256, -0.25 ) ) +print( pcall( binops, 256, -0.625 ) ) +print( pcall( binops, -256, 0.5 ) ) +print( pcall( binops, 0.5, 0 ) ) +print( pcall( binops, 0.5, 1 ) ) +print( pcall( binops, 0.5, 2 ) ) +print( pcall( binops, 0.5, -1 ) ) +print( pcall( binops, 0.5, -2 ) ) print( pcall( binops, 2.25, 0 ) ) print( pcall( binops, 2.25, 2 ) ) -print( pcall( binops, 2.25, .5 ) ) +print( pcall( binops, 2.25, 0.5 ) ) print( pcall( binops, 2.25, 2.5 ) ) print( pcall( binops, -2, 0 ) ) -- random tests print("Random tests") -print( math.random(5,10) ) -print( math.random(5,10) ) -print( math.random(5,10) ) -print( math.random(5,10) ) -print( math.random() ) -print( math.random() ) -print( math.random() ) -print( math.random() ) -print( math.randomseed(20), math.random(), math.random(), math.random() ) -print( math.randomseed(20), math.random() ) -print( math.randomseed(20), math.random() ) -print( math.randomseed(30), math.random() ) -print( math.randomseed(30), math.random() ) -print( math.random(30) ) -print( math.random(30) ) -print( math.random(30) ) -print( math.random(30) ) -print( math.random(-4,-2) ) -print( math.random(-4,-2) ) -print( math.random(-4,-2) ) -print( math.random(-4,-2) ) -print( math.random(-4,-2) ) +local function testrandom(string,lo,hi) + local c,e = loadstring('return '..string) + for i=1,5 do + local s,e = pcall(c) + print( string, s and type(e) or e, (e>=lo) and (e<=hi) ) + 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 diff --git a/src/test/res/metatables.lua b/src/test/res/metatables.lua index 9593b40c..8ff6fdf7 100644 --- a/src/test/res/metatables.lua +++ b/src/test/res/metatables.lua @@ -9,8 +9,8 @@ print(s:sub(2,4)) local t = {} function op(name,...) - a,b,c,d = pcall( setmetatable, t, ... ) - print( name, id(t), id(getmetatable(t)), id(a), id(b), id(c), id(d) ) + local a,b = pcall( setmetatable, t, ... ) + print( name, id(t), id(getmetatable(t)), id(a), a and id(b) or type(b) ) end op('set{} ',{}) op('set-nil',nil) diff --git a/src/test/res/require.lua b/src/test/res/require.lua index 73e376b6..f7fd6dd0 100644 --- a/src/test/res/require.lua +++ b/src/test/res/require.lua @@ -86,7 +86,8 @@ package.loaders = { function(...) end } pcall( g, 'require-sample-chunk-error' ) --- good, and bad java samples +-- good, and bad java samples +--[[ (needs to be moved to different test suite) package.loaders = pl function g(name) print( name, pcall(f,name) ) @@ -97,3 +98,4 @@ pcall( g, 'org.luaj.vm.require.RequireSampleLoadLuaError') pcall( g, 'org.luaj.vm.require.RequireSampleLoadRuntimeExcep') pcall( g, 'org.luaj.vm.require.RequireSampleSuccess') pcall( g, 'org.luaj.vm.require.RequireSampleSuccess') +--]] \ No newline at end of file