Add unit tests used in vm1

This commit is contained in:
James Roseborough
2010-05-09 18:10:55 +00:00
parent 7c72a9feaf
commit 40b7cb19c9
36 changed files with 487 additions and 1444 deletions

203
test/lua/errors/args.lua Normal file
View File

@@ -0,0 +1,203 @@
-- utilities to check that args of various types pass or fail
-- argument type checking
local ok = '-\t'
local fail = 'fail '
local needcheck = 'needcheck '
local badmsg = 'badmsg '
akey = 'aa'
astring = 'abc'
astrnum = '789'
anumber = 1.23
ainteger = 345
adouble = 123.456
aboolean = true
atable = {[akey]=456}
afunction = function() end
anil = nil
athread = coroutine.create(afunction)
anylua = { nil, astring, anumber, aboolean, atable, afunction, athread }
somestring = { astring, anumber }
somenumber = { anumber, astrnum }
someboolean = { aboolean }
sometable = { atable }
somefunction = { afunction }
somenil = { anil }
somekey = { akey }
notakey = { astring, anumber, aboolean, atable, afunction }
notastring = { nil, aboolean, atable, afunction, athread }
notanumber = { nil, astring, aboolean, atable, afunction, athread }
notaboolean = { nil, astring, anumber, atable, afunction, athread }
notatable = { nil, astring, anumber, aboolean, afunction, athread }
notafunction = { nil, astring, anumber, aboolean, atable, athread }
notathread = { nil, astring, anumber, aboolean, atable, afunction }
notanil = { astring, anumber, aboolean, atable, afunction, athread }
nonstring = { aboolean, atable, afunction, athread }
nonnumber = { astring, aboolean, atable, afunction, athread }
nonboolean = { astring, anumber, atable, afunction, athread }
nontable = { astring, anumber, aboolean, afunction, athread }
nonfunction = { astring, anumber, aboolean, atable, athread }
nonthread = { astring, anumber, aboolean, atable, afunction }
nonkey = { astring, anumber, aboolean, atable, afunction }
local structtypes = {
['table']='<table>',
['function']='<function>',
['thread']='<thread>',
['userdata']='<userdata>',
}
local function bracket(v)
return "<"..type(v)..">"
end
local function quote(v)
return "'"..v.."'"
end
local function ellipses(v)
local s = tostring(v)
return #s <= 8 and s or (string.sub(s,1,8)..'...')
end
local pretty = {
['table']=bracket,
['function']=bracket,
['thread']=bracket,
['userdata']=bracket,
['string']= quote,
['number']= ellipses,
}
local function values(list)
local t = {}
for i=1,#list do
local ai = list[i]
local fi = pretty[type(ai)]
t[i] = fi and fi(ai) or tostring(ai)
end
return table.concat(t,',')
end
local function types(list)
local t = {}
for i=1,#list do
local ai = list[i]
t[i] = type(ai)
end
return table.concat(t,',')
end
local function signature(name,arglist)
return name..'('..values(arglist)..')'
end
local function dup(t)
local s = {}
for i=1,#t do
s[i] = t[i]
end
return s
end
local function split(t)
local s = {}
local n = #t
for i=1,n-1 do
s[i] = t[i]
end
return s,t[n]
end
local function expand(argsets, typesets, ...)
local n = typesets and #typesets or 0
if n <= 0 then
table.insert(argsets,{...})
return argsets
end
local s,v = split(typesets)
for i=1,#v do
expand(argsets, s, v[i], ...)
end
return argsets
end
local function arglists(typesets)
local argsets = expand({},typesets)
return ipairs(argsets)
end
local function lookup( name )
return loadstring('return '..name)()
end
local function invoke( name, arglist )
local s,c = pcall(lookup, name)
if not s then return s,c end
local f = function(...)
local u = unpack
local t = { c(...) }
return u(t)
end
return pcall(f, unpack(arglist))
end
-- messages, banners
local _print = print
local _tostring = tostring
local _find = string.find
function banner(name)
_print( '====== '.._tostring(name)..' ======' )
end
local function subbanner(name)
_print( '--- '.._tostring(name) )
end
local function pack(s,...)
return s,{...}
end
-- check that all combinations of arguments pass
function checkallpass( name, typesets, typesonly )
subbanner('checkallpass')
for i,v in arglists(typesets) do
local sig = signature(name,v)
local s,r = pack( invoke( name, v ) )
if s then
if typesonly then
_print( ok, sig, types(r) )
else
_print( ok, sig, values(r) )
end
else
_print( fail, sig, values(r) )
end
end
end
-- check that all combinations of arguments fail in some way,
-- ignore error messages
function checkallerrors( name, typesets, template )
subbanner('checkallerrors')
template = _tostring(template)
for i,v in arglists(typesets) do
local sig = signature(name,v)
local s,e = invoke( name, v )
if not s then
if _find(e, template, 1, true) then
_print( ok, sig, '...'..template..'...' )
else
_print( badmsg, sig, "template='"..template.."' actual='"..e.."'" )
end
else
_print( needcheck, sig, e )
end
end
end

View File

@@ -0,0 +1,171 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg types for basic library functions
-- assert
banner('assert')
checkallpass('assert',{{true,123},anylua})
checkallerrors('assert',{{nil,false},{nil}},'assertion failed')
checkallerrors('assert',{{nil,false},{'message'}},'message')
-- collectgarbage
banner('collectgarbage')
checkallpass('collectgarbage',{{'collect','count'}},true)
checkallerrors('collectgarbage',{notanil},'bad argument #1')
-- dofile
banner('dofile')
checkallpass('dofile', {})
checkallpass('dofile', {{'src/test/errors/args.lua'}})
checkallerrors('dofile', {{'args.lua'}}, 'cannot open args.lua')
checkallerrors('dofile', {nonstring}, 'bad argument #1')
-- error
banner('error')
checkallerrors('error', {{'message'},{nil,0,1,2}}, 'message')
checkallerrors('error', {{123},{nil,1,2}}, 123)
-- getfenv
banner('getfenv')
checkallpass('getfenv', {{nil,print,function()end,0,1,2}})
checkallerrors('getfenv', {{true,{},'abc'}}, 'bad argument #1')
-- getmetatable
banner('getmetatable')
checkallpass('getmetatable', {notanil})
checkallerrors('getmetatable',{},'bad argument #1')
-- ipairs
banner('ipairs')
checkallpass('ipairs', {sometable})
checkallerrors('ipairs', {notatable}, 'bad argument #1')
-- load
banner('load')
checkallpass('load', {somefunction,{nil,astring}})
checkallerrors('load', {notafunction,{nil,astring,anumber}}, 'bad argument #1')
checkallerrors('load', {somefunction,{afunction,atable}}, 'bad argument #2')
-- loadfile
banner('loadfile')
checkallpass('loadfile', {})
checkallpass('loadfile', {{'bogus'}})
checkallpass('loadfile', {{'src/test/errors/args.lua'}})
checkallpass('loadfile', {{'args.lua'}})
checkallerrors('loadfile', {nonstring}, 'bad argument #1')
-- loadstring
banner('loadstring')
checkallpass('loadstring', {{'return'}})
checkallpass('loadstring', {{'return'},{'mychunk'}})
checkallpass('loadstring', {{'return a ... b'},{'mychunk'}},true)
checkallerrors('loadstring', {{'return a ... b'},{'mychunk'}},'hello')
checkallerrors('loadstring', {notastring,{nil,astring,anumber}}, 'bad argument #1')
checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument #2')
-- next
banner('next')
checkallpass('next', {sometable,somekey})
checkallerrors('next', {notatable,{nil,1}}, 'bad argument #1')
checkallerrors('next', {sometable,nonkey}, 'invalid key')
-- pairs
banner('pairs')
checkallpass('pairs', {sometable})
checkallerrors('pairs', {notatable}, 'bad argument #1')
-- pcall
banner('pcall')
checkallpass('pcall', {notanil,anylua}, true)
checkallerrors('pcall',{},'bad argument #1')
-- print
banner('print')
checkallpass('print', {})
checkallpass('print', {{nil,astring,anumber,aboolean}})
-- rawequal
banner('rawequal')
checkallpass('rawequal', {notanil,notanil})
checkallerrors('rawequal', {}, 'bad argument #1')
checkallerrors('rawequal', {notanil}, 'bad argument #2')
-- rawget
banner('rawget')
checkallpass('rawget', {sometable,somekey})
checkallpass('rawget', {sometable,nonkey})
checkallerrors('rawget', {sometable,somenil},'bad argument #2')
checkallerrors('rawget', {notatable,notakey}, 'bad argument #1')
checkallerrors('rawget', {}, 'bad argument #1')
-- rawset
banner('rawset')
checkallpass('rawset', {sometable,somekey,notanil})
checkallpass('rawset', {sometable,nonkey,notanil})
checkallerrors('rawset', {sometable,somenil},'table index is nil')
checkallerrors('rawset', {}, 'bad argument #1')
checkallerrors('rawset', {notatable,somestring,somestring}, 'bad argument #1')
checkallerrors('rawset', {sometable,somekey}, 'bad argument #3')
-- select
banner('select')
checkallpass('select', {{anumber,'#'},anylua})
checkallerrors('select', {notanumber}, 'bad argument #1')
-- setfenv
banner('setfenv')
local g = _G
checkallpass('setfenv', {{function()end},sometable})
checkallerrors('setfenv', {{-1, '-2'},{g}}, 'level must be non-negative')
checkallerrors('setfenv', {{10, '11'},{g}}, 'invalid level')
checkallerrors('setfenv', {{rawset},{g}}, 'cannot change environment of given object')
checkallerrors('setfenv', {{atable,athread,aboolean,astring},{g}}, 'bad argument #1')
checkallerrors('setfenv', {notafunction}, 'bad argument #2')
checkallerrors('setfenv', {anylua}, 'bad argument #2')
checkallerrors('setfenv', {{function()end},notatable}, 'bad argument #2')
-- setmetatable
banner('setmetatable')
checkallpass('setmetatable', {sometable,sometable})
checkallpass('setmetatable', {sometable,{}})
checkallerrors('setmetatable',{notatable,sometable},'bad argument #1')
checkallerrors('setmetatable',{sometable,notatable},'bad argument #2')
-- tonumber
banner('tonumber')
checkallpass('tonumber',{somenumber,{nil,2,10,36}})
checkallpass('tonumber',{notanil,{nil,10}})
checkallerrors('tonumber',{{nil,afunction,atable},{2,9,11,36}},'bad argument #1')
checkallerrors('tonumber',{somenumber,{1,37,atable,afunction,aboolean}},'bad argument #2')
-- tostring
banner('tostring')
checkallpass('tostring',{{astring,anumber,aboolean}})
checkallpass('tostring',{{atable,afunction,athread}},true)
checkallpass('tostring',{{astring,anumber,aboolean},{'anchor'}})
checkallpass('tostring',{{atable,afunction,athread},{'anchor'}},true)
checkallerrors('tostring',{},'bad argument #1')
-- type
banner('type')
checkallpass('type',{notanil})
checkallpass('type',{anylua,{'anchor'}})
checkallerrors('type',{},'bad argument')
-- unpack
banner('unpack')
checkallpass('unpack',{sometable})
checkallpass('unpack',{sometable,somenumber})
checkallpass('unpack',{sometable,somenumber,somenumber})
checkallerrors('unpack',{notatable,somenumber,somenumber},'bad argument #1')
checkallerrors('unpack',{sometable,nonnumber,somenumber},'bad argument #2')
checkallerrors('unpack',{sometable,somenumber,nonnumber},'bad argument #3')
-- xpcall
banner('xpcall')
checkallpass('xpcall', {notanil,nonfunction})
checkallpass('xpcall', {notanil,{function(...)return 'aaa', 'bbb', #{...} end}})
checkallerrors('xpcall',{anylua},'bad argument #2')

View File

@@ -0,0 +1,47 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for coroutine library functions
-- coroutine.create
banner('coroutine.create')
checkallpass('coroutine.create',{somefunction})
checkallerrors('coroutine.create',{notafunction},'bad argument')
-- coroutine.resume
banner('coroutine.resume')
local co = coroutine.create(function() while true do coroutine.yield() end end)
checkallpass('coroutine.resume',{{co},anylua})
checkallerrors('coroutine.resume',{notathread},'bad argument #1')
-- coroutine.running
banner('coroutine.running')
checkallpass('coroutine.running',{anylua})
-- coroutine.status
banner('coroutine.status')
checkallpass('coroutine.status',{{co}})
checkallerrors('coroutine.status',{notathread},'bad argument #1')
-- coroutine.wrap
banner('coroutine.wrap')
checkallpass('coroutine.wrap',{somefunction})
checkallerrors('coroutine.wrap',{notafunction},'bad argument #1')
-- coroutine.yield
banner('coroutine.yield')
local function f()
print( 'yield', coroutine.yield() )
print( 'yield', coroutine.yield(astring,anumber,aboolean) )
error('error within coroutine thread')
end
local co = coroutine.create( f )
print( 'status', coroutine.status(co) )
print( coroutine.resume(co,astring,anumber) )
print( 'status', coroutine.status(co) )
print( coroutine.resume(co,astring,anumber) )
print( 'status', coroutine.status(co) )
local s,e = coroutine.resume(co,astring,anumber)
print( s, string.match(e,'error within coroutine thread') or 'bad message: '..tostring(e) )
print( 'status', coroutine.status(co) )

View File

@@ -0,0 +1,76 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for io library functions
local f
-- io.close ([file])
banner('io.close')
f = io.open("abc.txt","w")
checkallpass('io.close',{{f}})
checkallerrors('io.close',{notanil},'bad argument #1')
-- io.input ([file])
f = io.open("abc.txt","r")
checkallpass('io.input',{{nil,f,"abc.txt"}})
checkallerrors('io.input',{nonstring},'bad argument #1')
-- io.lines ([filename])
io.input("abc.txt")
checkallpass('io.lines',{{nil,"abc.txt"}})
checkallerrors('io.lines',{{f}},'bad argument #1')
checkallerrors('io.lines',{nonstring},'bad argument #1')
-- io.open (filename [, mode])
checkallpass('io.open',{{"abc.txt"},{nil,"r","w","a","r+","w+","a+"}})
checkallerrors('io.open',{notastring},'bad argument #1')
checkallerrors('io.open',{{"abc.txt"},{nonstring}},'bad argument #2')
-- io.output ([file])
checkallpass('io.output',{{nil,f,"abc.txt"}})
checkallerrors('io.output',{nonstring},'bad argument #1')
-- io.popen (prog [, mode])
checkallpass('io.popen',{{"hostname"},{nil,"r","w","a","r+","w+","a+"}})
checkallerrors('io.popen',{notastring},'bad argument #1')
checkallerrors('io.popen',{{"hostname"},{nonstring}},'bad argument #2')
-- io.read (···)
local areadfmt = {2,"*n","*a","*l","3"}
checkallpass('io.read',{})
checkallpass('io.read',{areadfmt})
checkallpass('io.read',{areadfmt,areadfmt})
checkallerrors('io.read',{{aboolean,afunction,atable}},'bad argument #1')
-- io.read (···)
checkallpass('io.write',{})
checkallpass('io.write',{somestring})
checkallpass('io.write',{somestring,somestring})
checkallerrors('io.write',{nonstring},'bad argument #1')
checkallerrors('io.write',{somestring,nonstring},'bad argument #2')
-- file:write ()
file = io.open("seektest.txt","w")
checkallpass('file.write',{{file},somestring})
checkallpass('file.write',{{file},somestring,somestring})
checkallerrors('file.write',{},'bad argument #1')
checkallerrors('file.write',{{file},nonstring},'bad argument #1')
checkallerrors('file.write',{{file},somestring,nonstring},'bad argument #2')
pcall( file.close, file )
-- file:seek ([whence] [, offset])
file = io.open("seektest.txt","r")
checkallpass('file.seek',{{file}})
checkallpass('file.seek',{{file},{"set","cur","end"}})
checkallpass('file.seek',{{file},{"set","cur","end"},{2,"3"}})
checkallerrors('file.seek',{},'bad argument #1')
checkallerrors('file.seek',{{file},nonstring},'bad argument #1')
checkallerrors('file.seek',{{file},{"set","cur","end"},nonnumber},'bad argument #2')
-- file:setvbuf (mode [, size])
checkallpass('file.setvbuf',{{file},{"no","full","line"}})
checkallpass('file.setvbuf',{{file},{"full"},{1024,"512"}})
checkallerrors('file.setvbuf',{},'bad argument #1')
checkallerrors('file.setvbuf',{{file},notastring},'bad argument #1')
checkallerrors('file.setvbuf',{{file},{"full"},nonnumber},'bad argument #2')

View File

@@ -0,0 +1,106 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for math library functions
local somenumber = {1,0.75,'-1','-0.25'}
local somepositive = {1,0.75,'2', '2.5'}
local notanumber = {nil,astring,aboolean,afunction,atable,athread}
local nonnumber = {astring,aboolean,afunction,atable}
local singleargfunctions = {
'abs', 'acos', 'asin', 'atan', 'cos', 'cosh', 'deg', 'exp', 'floor',
'rad', 'randomseed', 'sin', 'sinh', 'tan', 'tanh', 'frexp',
}
local singleargposdomain = {
'log', 'log10', 'sqrt', 'ceil',
}
local twoargfunctions = {
'atan2',
}
local twoargsposdomain = {
'pow', 'fmod',
}
-- single argument tests
for i,v in ipairs(singleargfunctions) do
local funcname = 'math.'..v
banner(funcname)
checkallpass(funcname,{somenumber})
checkallerrors(funcname,{notanumber},'bad argument #1')
end
-- single argument, positive domain tests
for i,v in ipairs(singleargposdomain) do
local funcname = 'math.'..v
banner(funcname)
checkallpass(funcname,{somepositive})
checkallerrors(funcname,{notanumber},'bad argument #1')
end
-- two-argument tests
for i,v in ipairs(twoargfunctions) do
local funcname = 'math.'..v
banner(funcname)
checkallpass(funcname,{somenumber,somenumber})
checkallerrors(funcname,{},'bad argument #')
checkallerrors(funcname,{notanumber},'bad argument #')
checkallerrors(funcname,{notanumber,somenumber},'bad argument #1')
checkallerrors(funcname,{somenumber},'bad argument #2')
checkallerrors(funcname,{somenumber,notanumber},'bad argument #2')
end
-- two-argument, positive domain tests
for i,v in ipairs(twoargsposdomain) do
local funcname = 'math.'..v
banner(funcname)
checkallpass(funcname,{somepositive,somenumber})
checkallerrors(funcname,{},'bad argument #')
checkallerrors(funcname,{notanumber},'bad argument #')
checkallerrors(funcname,{notanumber,somenumber},'bad argument #1')
checkallerrors(funcname,{somenumber},'bad argument #2')
checkallerrors(funcname,{somenumber,notanumber},'bad argument #2')
end
-- math.max
banner('math.max')
checkallpass('math.max',{somenumber})
checkallpass('math.max',{somenumber,somenumber})
checkallerrors('math.max',{},'bad argument #1')
checkallerrors('math.max',{nonnumber},'bad argument #1')
checkallerrors('math.max',{somenumber,nonnumber},'bad argument #2')
-- math.min
banner('math.min')
checkallpass('math.min',{somenumber})
checkallpass('math.min',{somenumber,somenumber})
checkallerrors('math.min',{},'bad argument #1')
checkallerrors('math.min',{nonnumber},'bad argument #1')
checkallerrors('math.min',{somenumber,nonnumber},'bad argument #2')
-- math.random
local somem = {3,4.5,'6.7'}
local somen = {8,9.10,'12.34'}
local notamn = {astring,aboolean,atable,afunction}
banner('math.random')
checkallpass('math.random',{},true)
checkallpass('math.random',{somem},true)
checkallpass('math.random',{somem,somen},true)
checkallpass('math.random',{{-4,-5.6,'-7','-8.9'},{-1,100,23.45,'-1.23'}},true)
checkallerrors('math.random',{{-4,-5.6,'-7','-8.9'}},'interval is empty')
checkallerrors('math.random',{somen,somem},'interval is empty')
checkallerrors('math.random',{notamn,somen},'bad argument #1')
checkallerrors('math.random',{somem,notamn},'bad argument #2')
-- math.ldexp
local somee = {-3,0,3,9.10,'12.34'}
local notae = {nil,astring,aboolean,atable,afunction}
banner('math.ldexp')
checkallpass('math.ldexp',{somenumber,somee})
checkallerrors('math.ldexp',{},'bad argument')
checkallerrors('math.ldexp',{notanumber},'bad argument')
checkallerrors('math.ldexp',{notanumber,somee},'bad argument #1')
checkallerrors('math.ldexp',{somenumber},'bad argument #2')
checkallerrors('math.ldexp',{somenumber,notae},'bad argument #2')

View File

@@ -0,0 +1,49 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for module library functions
-- require
banner('require')
checkallpass('require',{{'math','coroutine','package','string','table'}},true)
checkallerrors('require',{{anumber}},'not found')
checkallerrors('require',{{anil,aboolean,afunction,atable}},'bad argument')
-- package.loadlib
banner('package.loadlib')
checkallpass('package.loadlib',{{'foo'},{'bar'}},true)
checkallerrors('package.loadlib',{notastring},'bad argument')
-- package.seeall
banner('package.seeall')
checkallpass('package.seeall',{sometable})
checkallerrors('package.seeall',{notatable},'bad argument')
-- module tests - require special rigging
banner('module')
print( pcall( function()
checkallpass('module',{{20001}})
end ) )
print( pcall( function()
checkallpass('module',{{20002},{package.seeall}})
end ) )
print( pcall( function()
checkallpass('module',{{20003},{package.seeall},{function() end}})
end ) )
print( pcall( function()
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule1'},{'pqrs'}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule2'},{aboolean}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule3'},{athread}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule4'},{atable}},'attempt to call')
end ) )

View File

@@ -0,0 +1,157 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg types for language operator
-- ========= unary operators: - # not
-- unary minus -
banner('unary -')
negative = function(a) return - a end
checkallpass('negative',{somenumber})
checkallerrors('negative',{notanumber},'attempt to perform arithmetic on')
-- length
banner('#')
lengthop = function(a) return #a end
checkallpass('lengthop',{sometable})
checkallerrors('lengthop',{notatable},'attempt to get length of')
-- length
banner('not')
notop = function(a) return not a end
checkallpass('notop',{somenumber})
checkallerrors('notop',{notanumber},'attempt to perform arithmetic on')
-- function call
banner( '()' )
funcop = function(a) return a() end
checkallpass('funcop',{somefunction})
checkallerrors('funcop',{notafunction},'attempt to call')
-- ========= binary ops: .. + - * / % ^ == ~= <= >= < > [] . and or
banner( '..' )
concatop = function(a,b) return a..b end
checkallpass('concatop',{somestring,somestring})
checkallerrors('concatop',{notastring,somestring},'attempt to concatenate')
checkallerrors('concatop',{somestring,notastring},'attempt to concatenate')
banner( '+' )
plusop = function(a,b) return a+b end
checkallpass('plusop',{somenumber,somenumber})
checkallerrors('plusop',{notanumber,somenumber},'attempt to perform arithmetic on')
checkallerrors('plusop',{somenumber,notanumber},'attempt to perform arithmetic on')
banner( '-' )
minusop = function(a,b) return a-b end
checkallpass('minusop',{somenumber,somenumber})
checkallerrors('minusop',{notanumber,somenumber},'attempt to perform arithmetic on')
checkallerrors('minusop',{somenumber,notanumber},'attempt to perform arithmetic on')
banner( '*' )
timesop = function(a,b) return a*b end
checkallpass('timesop',{somenumber,somenumber})
checkallerrors('timesop',{notanumber,somenumber},'attempt to perform arithmetic on')
checkallerrors('timesop',{somenumber,notanumber},'attempt to perform arithmetic on')
banner( '/' )
divideop = function(a,b) return a/b end
checkallpass('divideop',{somenumber,somenumber})
checkallerrors('divideop',{notanumber,somenumber},'attempt to perform arithmetic on')
checkallerrors('divideop',{somenumber,notanumber},'attempt to perform arithmetic on')
banner( '%' )
modop = function(a,b) return a%b end
checkallpass('modop',{somenumber,somenumber})
checkallerrors('modop',{notanumber,somenumber},'attempt to perform arithmetic on')
checkallerrors('modop',{somenumber,notanumber},'attempt to perform arithmetic on')
banner( '^' )
powerop = function(a,b) return a^b end
checkallpass('powerop',{{2,'2.5'},{3,'3.5'}})
checkallerrors('powerop',{notanumber,{3,'3.1'}},'attempt to perform arithmetic on')
checkallerrors('powerop',{{2,'2.1'},notanumber},'attempt to perform arithmetic on')
banner( '==' )
equalsop = function(a,b) return a==b end
checkallpass('equalsop',{anylua,anylua})
banner( '~=' )
noteqop = function(a,b) return a~=b end
checkallpass('noteqop',{anylua,anylua})
banner( '<=' )
leop = function(a,b) return a<=b end
checkallpass('leop',{{anumber},{anumber}})
checkallpass('leop',{{astring,astrnum},{astring,astrnum}})
checkallerrors('leop',{notanumber,{anumber}},'attempt to compare')
checkallerrors('leop',{{astrnum},{anumber}},'attempt to compare')
checkallerrors('leop',{notastring,{astring,astrnum}},'attempt to compare')
checkallerrors('leop',{{anumber},notanumber},'attempt to compare')
checkallerrors('leop',{{anumber},{astrnum}},'attempt to compare')
checkallerrors('leop',{{astring,astrnum},notastring},'attempt to compare')
banner( '>=' )
geop = function(a,b) return a>=b end
checkallpass('geop',{{anumber},{anumber}})
checkallpass('geop',{{astring,astrnum},{astring,astrnum}})
checkallerrors('geop',{notanumber,{anumber}},'attempt to compare')
checkallerrors('geop',{{astrnum},{anumber}},'attempt to compare')
checkallerrors('geop',{notastring,{astring,astrnum}},'attempt to compare')
checkallerrors('geop',{{anumber},notanumber},'attempt to compare')
checkallerrors('geop',{{anumber},{astrnum}},'attempt to compare')
checkallerrors('geop',{{astring,astrnum},notastring},'attempt to compare')
banner( '<' )
ltop = function(a,b) return a<b end
checkallpass('ltop',{{anumber},{anumber}})
checkallpass('ltop',{{astring,astrnum},{astring,astrnum}})
checkallerrors('ltop',{notanumber,{anumber}},'attempt to compare')
checkallerrors('ltop',{{astrnum},{anumber}},'attempt to compare')
checkallerrors('ltop',{notastring,{astring,astrnum}},'attempt to compare')
checkallerrors('ltop',{{anumber},notanumber},'attempt to compare')
checkallerrors('ltop',{{anumber},{astrnum}},'attempt to compare')
checkallerrors('ltop',{{astring,astrnum},notastring},'attempt to compare')
banner( '>' )
gtop = function(a,b) return a>b end
checkallpass('gtop',{{anumber},{anumber}})
checkallpass('gtop',{{astring,astrnum},{astring,astrnum}})
checkallerrors('gtop',{notanumber,{anumber}},'attempt to compare')
checkallerrors('gtop',{{astrnum},{anumber}},'attempt to compare')
checkallerrors('gtop',{notastring,{astring,astrnum}},'attempt to compare')
checkallerrors('gtop',{{anumber},notanumber},'attempt to compare')
checkallerrors('gtop',{{anumber},{astrnum}},'attempt to compare')
checkallerrors('gtop',{{astring,astrnum},notastring},'attempt to compare')
banner( '[]' )
bracketop = function(a,b) return a[b] end
checkallpass('bracketop',{sometable,notanil})
checkallerrors('bracketop',{notatable,notanil},'attempt to index')
checkallerrors('bracketop',{sometable},'attempt to index')
banner( '.' )
dotop = function(a,b) return a.b end
checkallpass('dotop',{sometable,notanil})
checkallerrors('dotop',{notatable,notanil},'attempt to index')
checkallerrors('dotop',{sometable},'attempt to index')
banner( 'and' )
types = {['table']='table',['function']='function',['thread']='thread'}
clean = function(x) return types[type(x)] or x end
andop = function(a,b) return clean(a and b) end
checkallpass('andop',{anylua,anylua})
banner( 'or' )
orop = function(a,b) return clean(a or b) end
checkallpass('orop',{anylua,anylua})
-- ========= for x in y
banner( 'for x=a,b,c' )
forop = function(a,b,c) for x=a,b,c do end end
checkallpass('forop',{{1,'1.1'},{10,'10.1'},{2,'2.1'}})
checkallerrors('forop',{notanumber,{10,'10.1'},{2,'2.1'}},"'for' initial value must be a number")
checkallerrors('forop',{{1,'1.1'},notanumber,{2,'2.1'}},"'for' limit must be a number")
checkallerrors('forop',{{1,'1.1'},{10,'10.1'},notanumber},"'for' step must be a number")

View File

@@ -0,0 +1,121 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for string library functions
-- string.byte
banner('string.byte')
checkallpass('string.byte',{somestring})
checkallpass('string.byte',{somestring,somenumber})
checkallpass('string.byte',{somestring,somenumber,somenumber})
checkallerrors('string.byte',{somestring,{astring,afunction,atable}},'bad argument')
checkallerrors('string.byte',{notastring,{nil,111}},'bad argument')
-- string.char
function string_char(...)
return string.byte( string.char( ... ) )
end
banner('string_char')
checkallpass('string.char',{{60}})
checkallpass('string.char',{{60},{70}})
checkallpass('string.char',{{60},{70},{80}})
checkallpass('string_char',{{nil,0,9,40,127,128,255,'0','9','255','9.2',9.2}})
checkallpass('string_char',{{0,127,255},{0,127,255}})
checkallpass('string_char',{})
checkallerrors('string_char',{},'bad argument #1')
checkallerrors('string_char',{{-1,256}},'bad argument #1')
checkallerrors('string_char',{notanumber,{23,'45',6.7}},'bad argument #1')
checkallerrors('string_char',{{23,'45',6.7},nonnumber},'bad argument #2')
-- string.dump
banner('string.dump')
local someupval = 435
local function funcwithupvals() return someupval end
checkallpass('string.dump',{{function() return 123 end}})
checkallpass('string.dump',{{funcwithupvals}})
checkallerrors('string.dump',{notafunction},'bad argument')
-- string.find
banner('string.find')
checkallpass('string.find',{somestring,somestring})
checkallpass('string.find',{somestring,somestring,{nil,-3,3}})
checkallpass('string.find',{somestring,somestring,somenumber,anylua})
checkallerrors('string.find',{notastring,somestring},'bad argument #1')
checkallerrors('string.find',{somestring,notastring},'bad argument #2')
checkallerrors('string.find',{somestring,somestring,nonnumber},'bad argument #3')
-- string.format
--local numfmts = {'%c','%d','%E','%e','%f','%g','%G','%i','%o','%u','%X','%x'}
local numfmts = {'%c','%d','%i','%o','%u','%X','%x'}
local strfmts = {'%q','%s'}
local badfmts = {'%w'}
banner('string.format')
checkallpass('string.format',{somestring,anylua})
checkallpass('string.format',{numfmts,somenumber})
checkallpass('string.format',{strfmts,somestring})
checkallerrors('string.format',{numfmts,notanumber},'bad argument #2')
checkallerrors('string.format',{strfmts,notastring},'bad argument #2')
checkallerrors('string.format',{badfmts,somestring},"invalid option '%w'")
-- string.gmatch
banner('string.gmatch')
checkallpass('string.gmatch',{somestring,somestring})
checkallerrors('string.gmatch',{notastring,somestring},'bad argument #1')
checkallerrors('string.gmatch',{somestring,notastring},'bad argument #2')
-- string.gsub
local somerepl = {astring,atable,afunction}
local notarepl = {nil,aboolean}
banner('string.gsub')
checkallpass('string.gsub',{somestring,somestring,somerepl,{nil,-1}})
checkallerrors('string.gsub',{nonstring,somestring,somerepl},'bad argument #1')
checkallerrors('string.gsub',{somestring,nonstring,somerepl},'bad argument #2')
checkallerrors('string.gsub',{{astring},{astring},notarepl},'bad argument')
checkallerrors('string.gsub',{{astring},{astring},somerepl,nonnumber},'bad argument #4')
-- string.len
banner('string.len')
checkallpass('string.len',{somestring})
checkallerrors('string.len',{notastring},'bad argument #1')
-- string.lower
banner('string.lower')
checkallpass('string.lower',{somestring})
checkallerrors('string.lower',{notastring},'bad argument #1')
-- string.match
banner('string.match')
checkallpass('string.match',{somestring,somestring})
checkallpass('string.match',{somestring,somestring,{nil,-3,3}})
checkallerrors('string.match',{},'bad argument #1')
checkallerrors('string.match',{nonstring,somestring},'bad argument #1')
checkallerrors('string.match',{somestring},'bad argument #2')
checkallerrors('string.match',{somestring,nonstring},'bad argument #2')
checkallerrors('string.match',{somestring,somestring,notanumber},'bad argument #3')
-- string.reverse
banner('string.reverse')
checkallpass('string.reverse',{somestring})
checkallerrors('string.reverse',{notastring},'bad argument #1')
-- string.rep
banner('string.rep')
checkallpass('string.rep',{somestring,somenumber})
checkallerrors('string.rep',{notastring,somenumber},'bad argument #1')
checkallerrors('string.rep',{somestring,notanumber},'bad argument #2')
-- string.sub
banner('string.sub')
checkallpass('string.sub',{somestring,somenumber})
checkallpass('string.sub',{somestring,somenumber,somenumber})
checkallerrors('string.sub',{},'bad argument #1')
checkallerrors('string.sub',{nonstring,somenumber,somenumber},'bad argument #1')
checkallerrors('string.sub',{somestring},'bad argument #2')
checkallerrors('string.sub',{somestring,nonnumber,somenumber},'bad argument #2')
checkallerrors('string.sub',{somestring,somenumber,nonnumber},'bad argument #3')
-- string.upper
banner('string.upper')
checkallpass('string.upper',{somestring})
checkallerrors('string.upper',{notastring},'bad argument #1')

View File

@@ -0,0 +1,66 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for table library functions
-- table.concat
local somestringstable = {{8,7,6,5,4,3,2,1,}}
local somenonstringtable = {{true,true,true,true,true,true,true,true,}}
local somesep = {',',1.23}
local notasep = {aboolean,atable,afunction}
local somei = {2,'2','2.2'}
local somej = {4,'4','4.4'}
local notij = {astring,aboolean,atable,afunction}
banner('table.concat')
checkallpass('table.concat',{somestringstable})
checkallpass('table.concat',{somestringstable,somesep})
checkallpass('table.concat',{somestringstable,{'-'},somei})
checkallpass('table.concat',{somestringstable,{'-'},{2},somej})
checkallerrors('table.concat',{notatable},'bad argument #1')
checkallerrors('table.concat',{somenonstringtable},'table contains non-strings')
checkallerrors('table.concat',{somestringstable,notasep},'bad argument #2')
checkallerrors('table.concat',{somestringstable,{'-'},notij},'bad argument #3')
checkallerrors('table.concat',{somestringstable,{'-'},{2},notij},'bad argument #4')
-- table.insert
banner('table.insert')
checkallpass('table.insert',{sometable,notanil})
checkallpass('table.insert',{sometable,somei,notanil})
checkallerrors('table.insert',{notatable,somestring},'bad argument #1')
checkallerrors('table.insert',{sometable,notij,notanil},'bad argument #2')
-- table.maxn
banner('table.maxn')
checkallpass('table.maxn',{sometable})
checkallerrors('table.maxn',{notatable},'bad argument #1')
-- table.remove
banner('table.remove')
checkallpass('table.remove',{sometable})
checkallpass('table.remove',{sometable,somei})
checkallerrors('table.remove',{notatable},'bad argument #1')
checkallerrors('table.remove',{notatable,somei},'bad argument #1')
checkallerrors('table.remove',{sometable,notij},'bad argument #2')
-- table.sort
local somecomp = {nil,afunction}
local notacomp = {astring,anumber,aboolean,atable}
banner('table.sort')
checkallpass('table.sort',{somestringstable,somecomp})
checkallerrors('table.sort',{sometable},'attempt to compare')
checkallerrors('table.sort',{notatable,somecomp},'bad argument #1')
checkallerrors('table.sort',{sometable,notacomp},'bad argument #2')
-- table get
banner('table_get - tbl[key]')
function table_get(tbl,key) return tbl[key] end
checkallpass('table_get',{sometable,anylua})
-- table set
banner('table_set - tbl[key]=val')
function table_set(tbl,key,val) tbl[key]=val end
function table_set_nil_key(tbl,val) tbl[nil]=val end
checkallpass('table_set',{sometable,notanil,anylua})
checkallerrors('table_set_nil_key',{sometable,anylua},'table index is nil')