2011-09-20 19:12:24 +00:00
|
|
|
-- test script for ELua/Lunatic Interpreter
|
|
|
|
|
2012-05-06 20:22:56 +00:00
|
|
|
--do return end
|
2012-04-22 19:10:29 +00:00
|
|
|
|
2012-07-13 18:20:38 +00:00
|
|
|
-- error=nil -- must not affect "require"
|
|
|
|
local string = require("string")
|
|
|
|
local bit = require("bit")
|
|
|
|
local math = require("math")
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
print('---=== ELua Test script ===---')
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-06-10 18:56:10 +00:00
|
|
|
local function printf(fmt, ...)
|
|
|
|
print(string.format(fmt, ...))
|
|
|
|
end
|
|
|
|
|
2012-07-13 18:20:40 +00:00
|
|
|
local function checkfail(funcstr, expectedmsg)
|
|
|
|
local status, errmsg = pcall(DBG_.loadstring(funcstr))
|
2011-09-25 15:11:47 +00:00
|
|
|
if (status) then
|
|
|
|
print('ERROR: '..funcstr.." DIDN'T fail")
|
|
|
|
else
|
2012-07-13 18:20:40 +00:00
|
|
|
if (expectedmsg==nil or string.find(errmsg, expectedmsg, 1, true)) then
|
|
|
|
print('SUCCESS: '..funcstr.." failed: "..errmsg)
|
|
|
|
else
|
|
|
|
print('ERROR*: '..funcstr.." failed: "..errmsg..
|
|
|
|
", but expected error message was: "..expectedmsg)
|
|
|
|
end
|
2011-09-25 15:11:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
|
|
|
|
---- check serialization ----
|
|
|
|
gamevar("ourvar")
|
|
|
|
gamevar("ourvar2")
|
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
-- test nans, infs, precision, subnorms, booleans
|
|
|
|
ourvar2 = { "asd"; 0/0, 1/0, -1/0, 0.12345678901234567, 1e-314, true }
|
|
|
|
ourvar = { ourvar2; 1, 2, 3, "qwe"; [true]=0, [false]=1 }
|
2012-04-22 19:10:29 +00:00
|
|
|
ourvar[#ourvar+1] = ourvar;
|
|
|
|
|
|
|
|
local gvstr = DBG_.serializeGamevars()
|
|
|
|
ourvar = -1
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
print("========== attempting to load string: ==========")
|
2012-04-22 19:10:29 +00:00
|
|
|
print(gvstr)
|
|
|
|
print("---------- (end string to load) ----------")
|
|
|
|
|
2012-06-17 19:45:33 +00:00
|
|
|
-- XXX: need to think about fully restoring state
|
2012-04-22 19:10:29 +00:00
|
|
|
DBG_.loadGamevarsString(gvstr)
|
|
|
|
print("ourvar[4]="..ourvar[4])
|
|
|
|
|
|
|
|
|
2011-09-20 19:12:24 +00:00
|
|
|
local i
|
|
|
|
|
|
|
|
print('tweaking sector pals')
|
2012-02-08 11:00:48 +00:00
|
|
|
print('numsectors: ' .. gv.numsectors .. ' of ' .. gv.MAXSECTORS)
|
2012-01-28 14:37:24 +00:00
|
|
|
|
2011-10-11 16:53:15 +00:00
|
|
|
---[[
|
2011-09-20 19:12:24 +00:00
|
|
|
for i = 0, gv.numsectors/2 do
|
|
|
|
sector[i].floorpal = 1;
|
|
|
|
sector[i].ceilingpal = 2;
|
|
|
|
end
|
|
|
|
|
2012-01-28 14:38:41 +00:00
|
|
|
local vol, lev
|
|
|
|
vol, lev = TEMP_getvollev()
|
|
|
|
print('volume='..vol..', level='..lev)
|
|
|
|
|
|
|
|
if (vol==1 and lev==1) then -- E1L1
|
|
|
|
print('tweaking some sprites 2')
|
|
|
|
i = 562
|
|
|
|
spriteext[i].alpha = 0.5;
|
|
|
|
sprite[i].cstat = bit.bor(sprite[i].cstat, 2+512);
|
|
|
|
spriteext[i].pitch = 128;
|
|
|
|
spriteext[i].roll = 256;
|
|
|
|
|
|
|
|
for spr in spritesofsect(307) do -- some fence sprites in E1L1
|
|
|
|
print('spr', spr)
|
|
|
|
sprite[spr].pal = 6
|
|
|
|
end
|
|
|
|
for spr in spritesofsect(236) do
|
|
|
|
print('#spr', spr)
|
|
|
|
end
|
2012-01-28 14:37:24 +00:00
|
|
|
|
2012-01-28 14:38:41 +00:00
|
|
|
--this is a problem
|
|
|
|
--actor = {}
|
|
|
|
actor[562].flags = bit.bor(actor[562].flags, 2); -- pal 6 with goggles on front SEENINE
|
|
|
|
end
|
|
|
|
|
|
|
|
if (vol==1 and lev==8) then
|
2012-02-04 14:29:59 +00:00
|
|
|
print('tweaking bunch 1');
|
2012-01-28 14:38:41 +00:00
|
|
|
-- trueror1.map
|
|
|
|
for i in sectorsofbunch(1, gv.CEILING) do
|
|
|
|
sector[i].ceilingz = sector[i].ceilingz - 3*1024;
|
|
|
|
end
|
|
|
|
for i in sectorsofbunch(1, gv.FLOOR) do
|
|
|
|
sector[i].floorz = sector[i].floorz - 3*1024;
|
|
|
|
end
|
|
|
|
end
|
2011-12-21 18:43:08 +00:00
|
|
|
|
2011-10-11 16:53:15 +00:00
|
|
|
--]]
|
2012-07-13 18:20:35 +00:00
|
|
|
--tostring = nil -- REMEMBER
|
2012-05-29 20:01:58 +00:00
|
|
|
--DBG_.printkv('_G in test.elua', _G)
|
2011-09-25 15:11:47 +00:00
|
|
|
|
2012-07-13 18:20:40 +00:00
|
|
|
-- direct gv array access forbidden
|
|
|
|
checkfail('gv.sprite[0].yrepeat = 100', "dummy variable: read access forbidden")
|
|
|
|
|
|
|
|
-- indexing struct array with non-numeric type
|
|
|
|
checkfail('local i = sprite["qwe"]') -- this will produce an error inside the sprite metatable
|
|
|
|
|
|
|
|
checkfail('print(sprite[100000].ceilingpal)', "out-of-bounds sprite[] struct read access")
|
|
|
|
|
|
|
|
-- NOTE: gv.sprite doesn't fail, but we can't use it
|
|
|
|
checkfail('print(gv.sprite[0])', "dummy variable: read access forbidden")
|
|
|
|
|
|
|
|
-- set metatable forbidden
|
|
|
|
checkfail('setmetatable(sprite, {})', "attempt to read undeclared variable 'setmetatable'")
|
|
|
|
|
|
|
|
-- oob write access
|
|
|
|
checkfail('sector[-1].ceilingpal = 4', "out-of-bounds sector[] read access") -- note the strange err msg
|
|
|
|
|
|
|
|
-- wallnum member is read-only
|
|
|
|
checkfail('sector[0].wallnum = 0', "attempt to write to constant location") -- this comes from LJ/FFI
|
|
|
|
|
|
|
|
-- gv.numsectors is read-only
|
|
|
|
checkfail('gv.numsectors = 4', "cannot create new or write into existing fields of 'gv'")
|
|
|
|
|
|
|
|
-- cannot create new fields in 'gv'
|
|
|
|
checkfail('gv.QWE = 4', "cannot create new or write into existing fields of 'gv'")
|
|
|
|
|
|
|
|
-- direct sector write access forbidden
|
|
|
|
checkfail('sector[4] = sector[6]', "cannot write directly to sector[] struct")
|
|
|
|
|
|
|
|
-- that would be horrible...
|
|
|
|
checkfail('nextspritesect[4] = -666', "cannot write directly to nextspritesect[]")
|
|
|
|
|
|
|
|
-- we're indexing a plain array!
|
|
|
|
checkfail('print(nextspritesect[4].whatfield)', "attempt to index a number value")
|
|
|
|
|
|
|
|
-- creating new keys forbidden... handled by LuaJit
|
|
|
|
checkfail('wall[4].QWE = 123', "has no member named 'QWE'")
|
|
|
|
|
|
|
|
-- our 'require' disallows importing such dangerous stuff
|
|
|
|
checkfail("require('os')")
|
|
|
|
|
|
|
|
-- we must declare globals with 'gamevar'
|
|
|
|
checkfail("new_global = 345", "attempt to write to undeclared variable 'new_global'")
|
|
|
|
|
|
|
|
-- can't redefine constants in 'gv'
|
|
|
|
checkfail('gv.CEILING = 3', "cannot create new or write into existing fields of 'gv'")
|
|
|
|
|
|
|
|
-- string.dump is unavailable
|
|
|
|
checkfail('local s=require[[string]]; local tmp=s.dump(gameevent)',
|
|
|
|
"attempt to call field 'dump' (a nil value)")
|
|
|
|
|
|
|
|
-- disallow changing base module tables
|
|
|
|
checkfail('local s=require[[string]]; s.format=nil', "modifying base module table forbidden")
|
|
|
|
|
2012-07-13 18:20:38 +00:00
|
|
|
print('')
|
2012-05-29 20:01:58 +00:00
|
|
|
-- This is problematic, even though pretty much every access will yield a
|
|
|
|
-- "missing declaration" error.
|
|
|
|
-- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains.
|
2012-07-13 18:20:40 +00:00
|
|
|
checkfail('gv.luaJIT_setmode(nil, 0, 0)', "missing declaration for symbol 'luaJIT_setmode'")
|
|
|
|
|
|
|
|
checkfail('gv.luaJIT_BC_con_lang', "attempt to call a nil value")
|
|
|
|
checkfail('gv.yax_getbunch(0,0)', "attempt to call field 'yax_getbunch' (a table value)")
|
|
|
|
checkfail('gv.gethitickms = nil', "cannot create new or write into existing fields of 'gv'")
|
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
-- we don't have arrays in Lua-accessible structs now
|
2012-07-13 18:20:40 +00:00
|
|
|
checkfail('local i = actor[0].t_data[15]', "has no member named 't_data'")
|
|
|
|
|
|
|
|
-- no pointer arithmetic!
|
|
|
|
checkfail('local spr = sprite[0]; local x=spr+1',
|
|
|
|
"attempt to perform arithmetic on")
|
|
|
|
|
|
|
|
checkfail('gameactor(1680, 0)', "bad argument #3 to 'gameactor' (function expected, got number)")
|
|
|
|
|
2012-06-22 21:40:01 +00:00
|
|
|
|
|
|
|
printf('ceilingbunch of sector 0: %d', getbunch(0, gv.CEILING))
|
2012-05-29 20:01:58 +00:00
|
|
|
|
2012-06-10 18:56:15 +00:00
|
|
|
gameevent(gv.EVENT_JUMP,
|
|
|
|
function(actori, playeri, dist)
|
|
|
|
printf("jump i=%d p=%d d=%d", actori, playeri, dist)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2012-06-22 21:40:01 +00:00
|
|
|
gameevent(gv.EVENT_ENTERLEVEL,
|
|
|
|
function()
|
2012-07-01 22:11:07 +00:00
|
|
|
-- NOTE: times are for helixhorned (Core2Duo 3GHz)
|
2012-06-22 21:40:01 +00:00
|
|
|
local i
|
|
|
|
local N = 1e6
|
|
|
|
local t = gv.gethitickms()
|
|
|
|
|
|
|
|
for i=3,N do
|
|
|
|
gv.gethitickms()
|
|
|
|
end
|
|
|
|
|
|
|
|
t = gv.gethitickms()-t
|
|
|
|
|
2012-07-13 18:20:35 +00:00
|
|
|
-- x86_64: 40ns/call, x86: 290 ns/call
|
2012-06-22 21:40:01 +00:00
|
|
|
printf("%d gethitickms() calls took %.03f ms (%.03f us/call)",
|
|
|
|
N, t, (t*1000)/N)
|
2012-07-01 22:11:07 +00:00
|
|
|
|
|
|
|
local sum=0
|
|
|
|
t = gv.gethitickms()
|
|
|
|
for i=1,N do sum = sum+gv.ksqrt(i) end
|
|
|
|
t = gv.gethitickms()-t
|
|
|
|
-- x86_64: 14ns/call
|
|
|
|
printf("%d ksqrt() calls took %.03f ms (%.03f us/call) [sum=%f]",
|
|
|
|
N, t, (t*1000)/N, sum)
|
|
|
|
|
|
|
|
sum=0
|
|
|
|
t = gv.gethitickms()
|
|
|
|
for i=1,N do sum = sum+math.sqrt(i) end
|
|
|
|
t = gv.gethitickms()-t
|
|
|
|
-- x86_64: 7ns/call
|
|
|
|
printf("%d math.sqrt() calls took %.03f ms (%.03f us/call) [sum=%f]",
|
|
|
|
N, t, (t*1000)/N, sum)
|
|
|
|
|
|
|
|
printf("sqrt(0xffffffff) = %f(ksqrt) %f(math.sqrt)",
|
2012-07-01 22:11:14 +00:00
|
|
|
gv.ksqrt(0xffffffff), math.sqrt(0xffffffff))
|
2012-06-22 21:40:01 +00:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2012-06-10 18:56:15 +00:00
|
|
|
gameactor(1680, -- LIZTROOP
|
|
|
|
function(i, playeri, dist)
|
|
|
|
sprite[i].pal = math.random(32)
|
|
|
|
-- sprite[i].ang = bit.band(sprite[i].ang-20, 2047)
|
2012-06-22 21:40:01 +00:00
|
|
|
|
|
|
|
local spr = sprite[i]
|
|
|
|
|
|
|
|
local x,y,z = spr.x, spr.y, spr.z
|
|
|
|
local t = gv.gethitickms()
|
|
|
|
local hit = hitscan(x,y,z, spr.sectnum, 10, 10, 0, gv.CLIPMASK0)
|
|
|
|
printf("hitscan took %.03f us, sec=%d, wal=%d, spr=%d", 1000*(gv.gethitickms()-t),
|
|
|
|
hit.hitsect, hit.hitwall, hit.hitsprite)
|
2012-06-10 18:56:15 +00:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2012-06-17 19:45:33 +00:00
|
|
|
printf("EVENT_INIT = %d", gv.EVENT_INIT) -- tests default defines
|
2012-05-29 20:01:58 +00:00
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
local bittest = require "bittest"
|
|
|
|
bittest.sieve()
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
print('---=== END TEST SCRIPT ===---')
|
2012-07-20 21:57:31 +00:00
|
|
|
|
|
|
|
-- This will complain about wrong usage of 'error'. In particular,
|
|
|
|
-- the nil must not propagate to C!
|
|
|
|
error(nil)
|