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");
}
public void testArgtypes() throws IOException, InterruptedException {
runTest("argtypes");
}
public void testAutoload() throws IOException, InterruptedException {
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"
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) )

View File

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

View File

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

View File

@@ -3,17 +3,31 @@ print( math.cos( math.pi ) )
print( math.sqrt( 9.0 ) )
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
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

View File

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

View File

@@ -87,6 +87,7 @@ end }
pcall( g, 'require-sample-chunk-error' )
-- 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')
--]]