Refactor math support to provide more consistent and complete math library coverage

This commit is contained in:
James Roseborough
2008-07-21 22:12:06 +00:00
parent 467923b86e
commit 6642b38f91
9 changed files with 352 additions and 169 deletions

View File

@@ -5,7 +5,6 @@ local fail = 'fail '
local needcheck = 'needcheck '
local badmsg = 'badmsg '
akey = 'aa'
astring = 'abc'
astrnum = '789'
@@ -62,7 +61,7 @@ end
local function ellipses(v)
local s = tostring(v)
return #s <= 8 and s or string.sub(s,8)..'...'
return #s <= 8 and s or (string.sub(s,1,8)..'...')
end
local pretty = {
@@ -84,6 +83,15 @@ local function values(list)
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
@@ -145,14 +153,18 @@ local function subbanner(name)
end
-- check that all combinations of arguments pass
function checkallpass( name, typesets )
function checkallpass( name, typesets, typesonly )
subbanner('checkallpass')
for i,v in arglists(typesets) do
local sig = signature(name,v)
local r = { invoke( name, v ) }
local s = table.remove( r, 1 )
if s then
print( ok, sig, values(r) )
if typesonly then
print( ok, sig, types(r) )
else
print( ok, sig, values(r) )
end
else
print( fail, sig, values(r) )
end

View File

@@ -2,17 +2,26 @@ package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for math library functions
local somenumber = {23,45.67,'-12','-345.678'}
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', 'ceil', 'cos', 'cosh', 'deg', 'exp', 'floor', 'frexp',
'log', 'log10', 'rad', 'randomseed', 'sin', 'sinh', 'sqrt', 'tan', 'tanh',
}
'abs', 'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'deg', 'exp', 'floor',
'rad', 'randomseed', 'sin', 'sinh', 'tan', 'tanh', 'frexp',
}
local singleargposdomain = {
'log', 'log10', 'sqrt',
}
local twoargfunctions = {
'atan2', 'fmod', 'pow',
'atan2', 'fmod',
}
local twoargsposdomain = {
'pow',
}
-- single argument tests
@@ -23,6 +32,14 @@ for i,v in ipairs(singleargfunctions) do
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
@@ -35,6 +52,18 @@ for i,v in ipairs(twoargfunctions) do
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 #1')
checkallerrors(funcname,{notanumber},'bad argument #1')
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})
@@ -56,11 +85,10 @@ 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',{})
checkallpass('math.random',{somem})
checkallpass('math.random',{somem,somen})
checkallpass('math.random',{{8},{7.8}})
checkallpass('math.random',{{-4,-5.6,'-7','-8.9'},{-1,100,23.45,'-1.23'}})
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')
@@ -71,8 +99,8 @@ 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 #2')
checkallerrors('math.ldexp',{notanumber},'bad argument #2')
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')