aboutsummaryrefslogtreecommitdiff
path: root/testes/api.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/api.lua')
-rw-r--r--testes/api.lua112
1 files changed, 61 insertions, 51 deletions
diff --git a/testes/api.lua b/testes/api.lua
index bd85a923..752ff18f 100644
--- a/testes/api.lua
+++ b/testes/api.lua
@@ -14,7 +14,7 @@ local pack = table.pack
14-- standard error message for memory errors 14-- standard error message for memory errors
15local MEMERRMSG = "not enough memory" 15local MEMERRMSG = "not enough memory"
16 16
17function tcheck (t1, t2) 17local function tcheck (t1, t2)
18 assert(t1.n == (t2.n or #t2) + 1) 18 assert(t1.n == (t2.n or #t2) + 1)
19 for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end 19 for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
20end 20end
@@ -28,7 +28,7 @@ end
28 28
29print('testing C API') 29print('testing C API')
30 30
31a = T.testC("pushvalue R; return 1") 31local a = T.testC("pushvalue R; return 1")
32assert(a == debug.getregistry()) 32assert(a == debug.getregistry())
33 33
34 34
@@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0)
43assert(a == string.pack("d", 12458954321123.0)) 43assert(a == string.pack("d", 12458954321123.0))
44assert(T.s2d(a) == 12458954321123.0) 44assert(T.s2d(a) == 12458954321123.0)
45 45
46a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") 46local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
47assert(a == 2 and b == 3 and not c) 47assert(a == 2 and b == 3 and not c)
48 48
49f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") 49local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
50a,b,c = f() 50a,b,c = f()
51assert(a == 2 and b == 3 and not c) 51assert(a == 2 and b == 3 and not c)
52 52
@@ -61,7 +61,7 @@ assert(a==false and b==true and c==false)
61a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) 61a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
62assert(a == 40 and b == 5 and not c) 62assert(a == 40 and b == 5 and not c)
63 63
64t = pack(T.testC("settop 5; return *", 2, 3)) 64local t = pack(T.testC("settop 5; return *", 2, 3))
65tcheck(t, {n=4,2,3}) 65tcheck(t, {n=4,2,3})
66 66
67t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) 67t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
@@ -166,16 +166,17 @@ end
166 166
167 167
168-- testing globals 168-- testing globals
169_G.a = 14; _G.b = "a31" 169_G.AA = 14; _G.BB = "a31"
170local a = {T.testC[[ 170local a = {T.testC[[
171 getglobal a; 171 getglobal AA;
172 getglobal b; 172 getglobal BB;
173 getglobal b; 173 getglobal BB;
174 setglobal a; 174 setglobal AA;
175 return * 175 return *
176]]} 176]]}
177assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") 177assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
178 178
179_G.AA, _G.BB = nil
179 180
180-- testing arith 181-- testing arith
181assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) 182assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
@@ -199,13 +200,14 @@ a,b,c = T.testC([[pushnum 1;
199 pushstring 10; arith _; 200 pushstring 10; arith _;
200 pushstring 5; return 3]]) 201 pushstring 5; return 3]])
201assert(a == 1 and b == -10 and c == "5") 202assert(a == 1 and b == -10 and c == "5")
202mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, 203local mt = {
204 __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
203 __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, 205 __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
204 __unm = function (a) return setmetatable({a[1]* 2}, mt) end} 206 __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
205a,b,c = setmetatable({4}, mt), 207a,b,c = setmetatable({4}, mt),
206 setmetatable({8}, mt), 208 setmetatable({8}, mt),
207 setmetatable({-3}, mt) 209 setmetatable({-3}, mt)
208x,y,z = T.testC("arith +; return 2", 10, a, b) 210local x,y,z = T.testC("arith +; return 2", 10, a, b)
209assert(x == 10 and y[1] == 12 and z == nil) 211assert(x == 10 and y[1] == 12 and z == nil)
210assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) 212assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
211assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == 213assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
@@ -312,9 +314,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
312 314
313-- testing lua_is 315-- testing lua_is
314 316
315function B(x) return x and 1 or 0 end 317local function B (x) return x and 1 or 0 end
316 318
317function count (x, n) 319local function count (x, n)
318 n = n or 2 320 n = n or 2
319 local prog = [[ 321 local prog = [[
320 isnumber %d; 322 isnumber %d;
@@ -345,7 +347,7 @@ assert(count(nil, 15) == 100)
345 347
346-- testing lua_to... 348-- testing lua_to...
347 349
348function to (s, x, n) 350local function to (s, x, n)
349 n = n or 2 351 n = n or 2
350 return T.testC(string.format("%s %d; return 1", s, n), x) 352 return T.testC(string.format("%s %d; return 1", s, n), x)
351end 353end
@@ -486,11 +488,12 @@ a = T.testC([[
486 pushvalue 3; insert -2; pcall 1 1 0; 488 pushvalue 3; insert -2; pcall 1 1 0;
487 pcall 0 0 0; 489 pcall 0 0 0;
488 return 1 490 return 1
489]], "x=150", function (a) assert(a==nil); return 3 end) 491]], "XX=150", function (a) assert(a==nil); return 3 end)
490 492
491assert(type(a) == 'string' and x == 150) 493assert(type(a) == 'string' and XX == 150)
494_G.XX = nil
492 495
493function check3(p, ...) 496local function check3(p, ...)
494 local arg = {...} 497 local arg = {...}
495 assert(#arg == 3) 498 assert(#arg == 3)
496 assert(string.find(arg[3], p)) 499 assert(string.find(arg[3], p))
@@ -500,7 +503,7 @@ check3("%.", T.testC("loadfile 2; return *", "."))
500check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) 503check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
501 504
502-- test errors in non protected threads 505-- test errors in non protected threads
503function checkerrnopro (code, msg) 506local function checkerrnopro (code, msg)
504 local th = coroutine.create(function () end) -- create new thread 507 local th = coroutine.create(function () end) -- create new thread
505 local stt, err = pcall(T.testC, th, code) -- run code there 508 local stt, err = pcall(T.testC, th, code) -- run code there
506 assert(not stt and string.find(err, msg)) 509 assert(not stt and string.find(err, msg))
@@ -510,8 +513,9 @@ if not _soft then
510 collectgarbage("stop") -- avoid __gc with full stack 513 collectgarbage("stop") -- avoid __gc with full stack
511 checkerrnopro("pushnum 3; call 0 0", "attempt to call") 514 checkerrnopro("pushnum 3; call 0 0", "attempt to call")
512 print"testing stack overflow in unprotected thread" 515 print"testing stack overflow in unprotected thread"
513 function f () f() end 516 function F () F() end
514 checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") 517 checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
518 F = nil
515 collectgarbage("restart") 519 collectgarbage("restart")
516end 520end
517print"+" 521print"+"
@@ -588,7 +592,7 @@ assert(a[a] == "x")
588 592
589b = setmetatable({p = a}, {}) 593b = setmetatable({p = a}, {})
590getmetatable(b).__index = function (t, i) return t.p[i] end 594getmetatable(b).__index = function (t, i) return t.p[i] end
591k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") 595local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
592assert(x == 15 and k == 35) 596assert(x == 15 and k == 35)
593k = T.testC("getfield 2 y, return 1", b) 597k = T.testC("getfield 2 y, return 1", b)
594assert(k == 12) 598assert(k == 12)
@@ -748,8 +752,8 @@ local i = T.ref{}
748T.unref(i) 752T.unref(i)
749assert(T.ref{} == i) 753assert(T.ref{} == i)
750 754
751Arr = {} 755local Arr = {}
752Lim = 100 756local Lim = 100
753for i=1,Lim do -- lock many objects 757for i=1,Lim do -- lock many objects
754 Arr[i] = T.ref({}) 758 Arr[i] = T.ref({})
755end 759end
@@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them
761 T.unref(Arr[i]) 765 T.unref(Arr[i])
762end 766end
763 767
764function printlocks () 768local function printlocks ()
765 local f = T.makeCfunc("gettable R; return 1") 769 local f = T.makeCfunc("gettable R; return 1")
766 local n = f("n") 770 local n = f("n")
767 print("n", n) 771 print("n", n)
@@ -793,8 +797,8 @@ assert(type(T.getref(a)) == 'table')
793 797
794 798
795-- colect in cl the `val' of all collected userdata 799-- colect in cl the `val' of all collected userdata
796tt = {} 800local tt = {}
797cl = {n=0} 801local cl = {n=0}
798A = nil; B = nil 802A = nil; B = nil
799local F 803local F
800F = function (x) 804F = function (x)
@@ -817,6 +821,7 @@ F = function (x)
817end 821end
818tt.__gc = F 822tt.__gc = F
819 823
824
820-- test whether udate collection frees memory in the right time 825-- test whether udate collection frees memory in the right time
821do 826do
822 collectgarbage(); 827 collectgarbage();
@@ -853,9 +858,9 @@ end
853collectgarbage("stop") 858collectgarbage("stop")
854 859
855-- create 3 userdatas with tag `tt' 860-- create 3 userdatas with tag `tt'
856a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) 861a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
857b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) 862b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
858c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) 863c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
859 864
860-- create userdata without meta table 865-- create userdata without meta table
861x = T.newuserdata(4) 866x = T.newuserdata(4)
@@ -866,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x)
866 871
867assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) 872assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
868 873
869d=T.ref(a); 874local d = T.ref(a);
870e=T.ref(b); 875local e = T.ref(b);
871f=T.ref(c); 876local f = T.ref(c);
872t = {T.getref(d), T.getref(e), T.getref(f)} 877t = {T.getref(d), T.getref(e), T.getref(f)}
873assert(t[1] == a and t[2] == b and t[3] == c) 878assert(t[1] == a and t[2] == b and t[3] == c)
874 879
@@ -888,7 +893,7 @@ tt=nil -- frees tt for GC
888A = nil 893A = nil
889b = nil 894b = nil
890T.unref(d); 895T.unref(d);
891n5 = T.newuserdata(0) 896local n5 = T.newuserdata(0)
892debug.setmetatable(n5, {__gc=F}) 897debug.setmetatable(n5, {__gc=F})
893n5 = T.udataval(n5) 898n5 = T.udataval(n5)
894collectgarbage() 899collectgarbage()
@@ -959,11 +964,11 @@ print'+'
959 964
960 965
961-- testing changing hooks during hooks 966-- testing changing hooks during hooks
962_G.t = {} 967_G.TT = {}
963T.sethook([[ 968T.sethook([[
964 # set a line hook after 3 count hooks 969 # set a line hook after 3 count hooks
965 sethook 4 0 ' 970 sethook 4 0 '
966 getglobal t; 971 getglobal TT;
967 pushvalue -3; append -2 972 pushvalue -3; append -2
968 pushvalue -2; append -2 973 pushvalue -2; append -2
969 ']], "c", 3) 974 ']], "c", 3)
@@ -973,12 +978,13 @@ a = 1 -- count hook (set line hook)
973a = 1 -- line hook 978a = 1 -- line hook
974a = 1 -- line hook 979a = 1 -- line hook
975debug.sethook() 980debug.sethook()
976t = _G.t 981local t = _G.TT
977assert(t[1] == "line") 982assert(t[1] == "line")
978line = t[2] 983local line = t[2]
979assert(t[3] == "line" and t[4] == line + 1) 984assert(t[3] == "line" and t[4] == line + 1)
980assert(t[5] == "line" and t[6] == line + 2) 985assert(t[5] == "line" and t[6] == line + 2)
981assert(t[7] == nil) 986assert(t[7] == nil)
987_G.TT = nil
982 988
983 989
984------------------------------------------------------------------------- 990-------------------------------------------------------------------------
@@ -1003,6 +1009,7 @@ do -- testing errors during GC
1003 collectgarbage("restart") 1009 collectgarbage("restart")
1004 warn("@on") 1010 warn("@on")
1005end 1011end
1012_G.A = nil
1006------------------------------------------------------------------------- 1013-------------------------------------------------------------------------
1007-- test for userdata vals 1014-- test for userdata vals
1008do 1015do
@@ -1032,8 +1039,8 @@ assert(a == 'alo' and b == '3')
1032 1039
1033T.doremote(L1, "_ERRORMESSAGE = nil") 1040T.doremote(L1, "_ERRORMESSAGE = nil")
1034-- error: `sin' is not defined 1041-- error: `sin' is not defined
1035a, _, b = T.doremote(L1, "return sin(1)") 1042a, b, c = T.doremote(L1, "return sin(1)")
1036assert(a == nil and b == 2) -- 2 == run-time error 1043assert(a == nil and c == 2) -- 2 == run-time error
1037 1044
1038-- error: syntax error 1045-- error: syntax error
1039a, b, c = T.doremote(L1, "return a+") 1046a, b, c = T.doremote(L1, "return a+")
@@ -1204,7 +1211,7 @@ T.alloccount() -- remove limit
1204-- o that we get memory errors in all allocations of a given 1211-- o that we get memory errors in all allocations of a given
1205-- task, until there is enough memory to complete the task without 1212-- task, until there is enough memory to complete the task without
1206-- errors. 1213-- errors.
1207function testbytes (s, f) 1214local function testbytes (s, f)
1208 collectgarbage() 1215 collectgarbage()
1209 local M = T.totalmem() 1216 local M = T.totalmem()
1210 local oldM = M 1217 local oldM = M
@@ -1229,7 +1236,7 @@ end
1229-- task, until there is enough allocations to complete the task without 1236-- task, until there is enough allocations to complete the task without
1230-- errors. 1237-- errors.
1231 1238
1232function testalloc (s, f) 1239local function testalloc (s, f)
1233 collectgarbage() 1240 collectgarbage()
1234 local M = 0 1241 local M = 0
1235 local a,b = nil 1242 local a,b = nil
@@ -1296,12 +1303,12 @@ end)
1296-- testing threads 1303-- testing threads
1297 1304
1298-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) 1305-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
1299mt = T.testC("rawgeti R 1; return 1") 1306local mt = T.testC("rawgeti R 1; return 1")
1300assert(type(mt) == "thread" and coroutine.running() == mt) 1307assert(type(mt) == "thread" and coroutine.running() == mt)
1301 1308
1302 1309
1303 1310
1304function expand (n,s) 1311local function expand (n,s)
1305 if n==0 then return "" end 1312 if n==0 then return "" end
1306 local e = string.rep("=", n) 1313 local e = string.rep("=", n)
1307 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", 1314 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
@@ -1311,9 +1318,10 @@ end
1311G=0; collectgarbage(); a =collectgarbage("count") 1318G=0; collectgarbage(); a =collectgarbage("count")
1312load(expand(20,"G=G+1"))() 1319load(expand(20,"G=G+1"))()
1313assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) 1320assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1321G = nil
1314 1322
1315testamem("running code on new thread", function () 1323testamem("running code on new thread", function ()
1316 return T.doonnewstack("x=1") == 0 -- try to create thread 1324 return T.doonnewstack("local x=1") == 0 -- try to create thread
1317end) 1325end)
1318 1326
1319 1327
@@ -1327,13 +1335,13 @@ end)
1327local testprog = [[ 1335local testprog = [[
1328local function foo () return end 1336local function foo () return end
1329local t = {"x"} 1337local t = {"x"}
1330a = "aaa" 1338AA = "aaa"
1331for i = 1, #t do a=a..t[i] end 1339for i = 1, #t do AA = AA .. t[i] end
1332return true 1340return true
1333]] 1341]]
1334 1342
1335-- testing memory x dofile 1343-- testing memory x dofile
1336_G.a = nil 1344_G.AA = nil
1337local t =os.tmpname() 1345local t =os.tmpname()
1338local f = assert(io.open(t, "w")) 1346local f = assert(io.open(t, "w"))
1339f:write(testprog) 1347f:write(testprog)
@@ -1343,7 +1351,7 @@ testamem("dofile", function ()
1343 return a and a() 1351 return a and a()
1344end) 1352end)
1345assert(os.remove(t)) 1353assert(os.remove(t))
1346assert(_G.a == "aaax") 1354assert(_G.AA == "aaax")
1347 1355
1348 1356
1349-- other generic tests 1357-- other generic tests
@@ -1360,6 +1368,8 @@ testamem("dump/undump", function ()
1360 return a and a() 1368 return a and a()
1361end) 1369end)
1362 1370
1371_G.AA = nil
1372
1363local t = os.tmpname() 1373local t = os.tmpname()
1364testamem("file creation", function () 1374testamem("file creation", function ()
1365 local f = assert(io.open(t, 'w')) 1375 local f = assert(io.open(t, 'w'))
@@ -1381,7 +1391,7 @@ testamem("constructors", function ()
1381end) 1391end)
1382 1392
1383local a = 1 1393local a = 1
1384close = nil 1394local close = nil
1385testamem("closure creation", function () 1395testamem("closure creation", function ()
1386 function close (b) 1396 function close (b)
1387 return function (x) return b + x end 1397 return function (x) return b + x end