Improve compatibility tests.

This commit is contained in:
James Roseborough
2008-07-22 23:52:02 +00:00
parent ef48f8cd12
commit 81223754f2
8 changed files with 92 additions and 122 deletions

View File

@@ -48,10 +48,6 @@ public class CompatibiltyTest extends ScriptDrivenTest {
runTest("test8"); runTest("test8");
} }
public void testArgtypes() throws IOException, InterruptedException {
runTest("argtypes");
}
public void testAutoload() throws IOException, InterruptedException { public void testAutoload() throws IOException, InterruptedException {
runTest("autoload"); runTest("autoload");
} }

View File

@@ -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

View File

@@ -2,12 +2,20 @@
package.path = "?.lua;src/test/res/?.lua" package.path = "?.lua;src/test/res/?.lua"
require 'ids' 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() print()
print(11) print(11)
print("abc",123,nil,"pqr") print("abc",123,nil,"pqr")
-- assert -- assert
print( 'assert(true)', assert(true) ) print( 'assert(true)', assert(true) )
print( 'pcall(assert,true)', pcall(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") ) print( 'pcall(assert,false,"msg","msg2")', pcall(assert,false,"msg","msg2") )
-- collectgarbage (not supported) -- collectgarbage (not supported)
print( 'collectgarbage("count")', id(collectgarbage("count"))) print( 'collectgarbage("count")', type(collectgarbage("count")))
print( 'collectgarbage("collect")', collectgarbage("collect")) print( 'collectgarbage("collect")', type(collectgarbage("collect")))
print( 'collectgarbage("count")', id(collectgarbage("count"))) print( 'collectgarbage("count")', type(collectgarbage("count")))
-- dofile (not supported) -- dofile (not supported)
-- ipairs -- ipairs
@@ -39,9 +47,9 @@ for k,v in ipairs({[30]='30',[20]='20'}) do print('ipairs5',k,v)end
-- loadfile -- loadfile
-- loadstring -- loadstring
local lst = "print(3+4); return 8" local lst = "print(3+4); return 8"
local lss,lsv = pcall( loadstring, lst ) local chunk, err = loadstring( lst )
print( 'loadstring("'..lst..'")', lss, id(lsv) ) print( 'loadstring("'..lst..'")', id(chunk), err )
print( 'loadstring("'..lst..'")()', pcall( lsv ) ) print( 'loadstring("'..lst..'")()', chunk() )
-- pairs -- pairs
print( 'pcall(pairs)', pcall(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({'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'}) 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({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 -- _G
print( '_G["abc"] (before)', _G["abc"] ) 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,123)', pcall(tostring,123) )
print( 'pcall(tostring,true)', pcall(tostring,true) ) print( 'pcall(tostring,true)', pcall(tostring,true) )
print( 'pcall(tostring,false)', pcall(tostring,false) ) print( 'pcall(tostring,false)', pcall(tostring,false) )
print( 'tostring(tostring)', id(tostring(tostring)) ) print( 'tostring(tostring)', type(tostring(tostring)) )
print( 'tostring(function() end)', id(tostring(function() end)) ) print( 'tostring(function() end)', type(tostring(function() end)) )
print( 'tostring({"one","two",a="aa",b="bb"})', id(tostring({"one","two",a="aa",b="bb"})) ) print( 'tostring({"one","two",a="aa",b="bb"})', type(tostring({"one","two",a="aa",b="bb"})) )
-- unpack -- unpack
print( 'pcall(unpack)', pcall(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) ); print( 'pcall(unpack,t,function() end)', pcall(unpack,t,function() end) );
-- _VERSION -- _VERSION
print( '_VERSION', _VERSION ) print( '_VERSION', type(_VERSION) )

View File

@@ -19,6 +19,10 @@ local result1 = factorial(5)
print(result1) print(result1)
print(factorial(5)) 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) local result2 = f(math.pi)
print(result2) print(truncate(result2))
print(f(math.pi)) print(truncate(f(math.pi)))

View File

@@ -5,7 +5,7 @@ require 'ids'
-- test of common types of errors -- test of common types of errors
local function c(f,...) return f(...) end local function c(f,...) return f(...) end
local function b(...) return c(...) end local function b(...) return c(...) end
local function a(...) return pcall(b,...) end local function a(...) return (pcall(b,...)) end
s = 'some string' s = 'some string'
local t = {} local t = {}
-- error message tests -- error message tests
@@ -78,11 +78,11 @@ local function concatsuite(comparefunc)
print( '3.5.."b"', comparefunc(3.5,"b") ) print( '3.5.."b"', comparefunc(3.5,"b") )
end end
local function strconcat(a,b) local function strconcat(a,b)
return pcall( function() return a..b end ) return (pcall( function() return a..b end) )
end end
local function tblconcat(a,b) local function tblconcat(a,b)
local t={a,b} local t={a,b}
return pcall( function() return table.concat(t,'-') end ) return (pcall( function() return table.concat(t,'-') end ))
end end
-- concatsuite(strconcat) -- concatsuite(strconcat)
concatsuite(tblconcat) concatsuite(tblconcat)

View File

@@ -3,17 +3,31 @@ print( math.cos( math.pi ) )
print( math.sqrt( 9.0 ) ) print( math.sqrt( 9.0 ) )
print( math.modf( 5.25 ) ) print( math.modf( 5.25 ) )
local aliases = {
['nan']='<nan>',
['inf']='<pos-inf>',
['-inf']='<neg-inf>',
['1.#INF']='<pos-inf>',
['-1.#INF']='<neg-inf>',
['1.#IND']='<nan>',
['-1.#IND']='<nan>',
}
local function normalized(x)
local s = tostring(x)
return aliases[s] or s
end
-- binary ops -- binary ops
function binops( a, b ) function binops( a, b )
local sa = tostring(a) local sa = tostring(a)
local sb = tostring(b) local sb = tostring(b)
print( sa..'+'..sb..'='..tostring(a+b) ) print( sa..'+'..sb..'='..normalized(a+b) )
print( sa..'-'..sb..'='..tostring(a-b) ) print( sa..'-'..sb..'='..normalized(a-b) )
print( sa..'*'..sb..'='..tostring(a*b) ) print( sa..'*'..sb..'='..normalized(a*b) )
print( sa..'^'..sb..'='..tostring(a^b) ) print( sa..'^'..sb..'='..normalized(a^b) )
print( sa..'/'..sb..'=',pcall( function() return a / b end ) ) print( sa..'/'..sb..'='..normalized(a/b) )
print( sa..'%'..sb..'=',pcall( function() return a % b end ) ) print( sa..'%'..sb..'='..normalized(a%b) )
return '--' return '--'
end end
print( pcall( binops, 2, 0 ) ) print( pcall( binops, 2, 0 ) )
@@ -24,45 +38,48 @@ print( pcall( binops, 5, 2 ) )
print( pcall( binops, -5, 2 ) ) print( pcall( binops, -5, 2 ) )
print( pcall( binops, 16, -2 ) ) print( pcall( binops, 16, -2 ) )
print( pcall( binops, -16, -2 ) ) print( pcall( binops, -16, -2 ) )
print( pcall( binops, 256, .5 ) ) print( pcall( binops, 256, 0.5 ) )
print( pcall( binops, 256, .25 ) ) print( pcall( binops, 256, 0.25 ) )
print( pcall( binops, 256, .625 ) ) print( pcall( binops, 256, 0.625 ) )
print( pcall( binops, 256, -.5 ) ) print( pcall( binops, 256, -0.5 ) )
print( pcall( binops, 256, -.25 ) ) print( pcall( binops, 256, -0.25 ) )
print( pcall( binops, 256, -.625 ) ) print( pcall( binops, 256, -0.625 ) )
print( pcall( binops, -256, .5 ) ) print( pcall( binops, -256, 0.5 ) )
print( pcall( binops, .5, 0 ) ) print( pcall( binops, 0.5, 0 ) )
print( pcall( binops, .5, 1 ) ) print( pcall( binops, 0.5, 1 ) )
print( pcall( binops, .5, 2 ) ) print( pcall( binops, 0.5, 2 ) )
print( pcall( binops, .5, -1 ) ) print( pcall( binops, 0.5, -1 ) )
print( pcall( binops, .5, -2 ) ) print( pcall( binops, 0.5, -2 ) )
print( pcall( binops, 2.25, 0 ) ) print( pcall( binops, 2.25, 0 ) )
print( pcall( binops, 2.25, 2 ) ) 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.25, 2.5 ) )
print( pcall( binops, -2, 0 ) ) print( pcall( binops, -2, 0 ) )
-- random tests -- random tests
print("Random tests") print("Random tests")
print( math.random(5,10) ) local function testrandom(string,lo,hi)
print( math.random(5,10) ) local c,e = loadstring('return '..string)
print( math.random(5,10) ) for i=1,5 do
print( math.random(5,10) ) local s,e = pcall(c)
print( math.random() ) print( string, s and type(e) or e, (e>=lo) and (e<=hi) )
print( math.random() ) end
print( math.random() ) end
print( math.random() ) testrandom('math.random()',0,1)
print( math.randomseed(20), math.random(), math.random(), math.random() ) testrandom('math.random(5,10)',5,10)
print( math.randomseed(20), math.random() ) testrandom('math.random(30)',0,30)
print( math.randomseed(20), math.random() ) testrandom('math.random(-4,-2)',-4,-2)
print( math.randomseed(30), math.random() ) local t = {}
print( math.randomseed(30), math.random() ) print( math.randomseed(20) )
print( math.random(30) ) for i=1,20 do
print( math.random(30) ) t[i] = math.random()
print( math.random(30) ) end
print( math.random(30) ) print( '-- comparing new numbers')
print( math.random(-4,-2) ) for i=1,20 do
print( math.random(-4,-2) ) print( t[i] == math.random(), t[i] == t[0] )
print( math.random(-4,-2) ) end
print( math.random(-4,-2) ) print( '-- resetting seed')
print( math.random(-4,-2) ) print( math.randomseed(20) )
for i=1,20 do
print( t[i] == math.random() )
end

View File

@@ -9,8 +9,8 @@ print(s:sub(2,4))
local t = {} local t = {}
function op(name,...) function op(name,...)
a,b,c,d = pcall( setmetatable, t, ... ) local a,b = pcall( setmetatable, t, ... )
print( name, id(t), id(getmetatable(t)), id(a), id(b), id(c), id(d) ) print( name, id(t), id(getmetatable(t)), id(a), a and id(b) or type(b) )
end end
op('set{} ',{}) op('set{} ',{})
op('set-nil',nil) op('set-nil',nil)

View File

@@ -86,7 +86,8 @@ package.loaders = { function(...)
end } end }
pcall( g, 'require-sample-chunk-error' ) 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 package.loaders = pl
function g(name) function g(name)
print( name, pcall(f,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.RequireSampleLoadRuntimeExcep')
pcall( g, 'org.luaj.vm.require.RequireSampleSuccess') pcall( g, 'org.luaj.vm.require.RequireSampleSuccess')
pcall( g, 'org.luaj.vm.require.RequireSampleSuccess') pcall( g, 'org.luaj.vm.require.RequireSampleSuccess')
--]]