aboutsummaryrefslogtreecommitdiff
path: root/testes/api.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:38:20 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:38:20 -0300
commitb5ab31a475ccf5f96c2ffb34fccc1d6592913794 (patch)
treeef6d2b1844ca672dc81ab4411e37abbef2b7c48f /testes/api.lua
parent140cdcced5e28bde7a89e88fcae428f318565f1d (diff)
parent314745ed8438d1276c6c928d5f9d4be018dfadb6 (diff)
downloadlua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.gz
lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.bz2
lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.zip
Merge branch 'master' into nextversion
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 f8e36ae3..dece98f5 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+")
@@ -1206,7 +1213,7 @@ T.alloccount() -- remove limit
1206-- o that we get memory errors in all allocations of a given 1213-- o that we get memory errors in all allocations of a given
1207-- task, until there is enough memory to complete the task without 1214-- task, until there is enough memory to complete the task without
1208-- errors. 1215-- errors.
1209function testbytes (s, f) 1216local function testbytes (s, f)
1210 collectgarbage() 1217 collectgarbage()
1211 local M = T.totalmem() 1218 local M = T.totalmem()
1212 local oldM = M 1219 local oldM = M
@@ -1231,7 +1238,7 @@ end
1231-- task, until there is enough allocations to complete the task without 1238-- task, until there is enough allocations to complete the task without
1232-- errors. 1239-- errors.
1233 1240
1234function testalloc (s, f) 1241local function testalloc (s, f)
1235 collectgarbage() 1242 collectgarbage()
1236 local M = 0 1243 local M = 0
1237 local a,b = nil 1244 local a,b = nil
@@ -1298,12 +1305,12 @@ end)
1298-- testing threads 1305-- testing threads
1299 1306
1300-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) 1307-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
1301mt = T.testC("rawgeti R 1; return 1") 1308local mt = T.testC("rawgeti R 1; return 1")
1302assert(type(mt) == "thread" and coroutine.running() == mt) 1309assert(type(mt) == "thread" and coroutine.running() == mt)
1303 1310
1304 1311
1305 1312
1306function expand (n,s) 1313local function expand (n,s)
1307 if n==0 then return "" end 1314 if n==0 then return "" end
1308 local e = string.rep("=", n) 1315 local e = string.rep("=", n)
1309 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", 1316 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
@@ -1313,9 +1320,10 @@ end
1313G=0; collectgarbage(); a =collectgarbage("count") 1320G=0; collectgarbage(); a =collectgarbage("count")
1314load(expand(20,"G=G+1"))() 1321load(expand(20,"G=G+1"))()
1315assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) 1322assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1323G = nil
1316 1324
1317testamem("running code on new thread", function () 1325testamem("running code on new thread", function ()
1318 return T.doonnewstack("x=1") == 0 -- try to create thread 1326 return T.doonnewstack("local x=1") == 0 -- try to create thread
1319end) 1327end)
1320 1328
1321 1329
@@ -1329,13 +1337,13 @@ end)
1329local testprog = [[ 1337local testprog = [[
1330local function foo () return end 1338local function foo () return end
1331local t = {"x"} 1339local t = {"x"}
1332a = "aaa" 1340AA = "aaa"
1333for i = 1, #t do a=a..t[i] end 1341for i = 1, #t do AA = AA .. t[i] end
1334return true 1342return true
1335]] 1343]]
1336 1344
1337-- testing memory x dofile 1345-- testing memory x dofile
1338_G.a = nil 1346_G.AA = nil
1339local t =os.tmpname() 1347local t =os.tmpname()
1340local f = assert(io.open(t, "w")) 1348local f = assert(io.open(t, "w"))
1341f:write(testprog) 1349f:write(testprog)
@@ -1345,7 +1353,7 @@ testamem("dofile", function ()
1345 return a and a() 1353 return a and a()
1346end) 1354end)
1347assert(os.remove(t)) 1355assert(os.remove(t))
1348assert(_G.a == "aaax") 1356assert(_G.AA == "aaax")
1349 1357
1350 1358
1351-- other generic tests 1359-- other generic tests
@@ -1362,6 +1370,8 @@ testamem("dump/undump", function ()
1362 return a and a() 1370 return a and a()
1363end) 1371end)
1364 1372
1373_G.AA = nil
1374
1365local t = os.tmpname() 1375local t = os.tmpname()
1366testamem("file creation", function () 1376testamem("file creation", function ()
1367 local f = assert(io.open(t, 'w')) 1377 local f = assert(io.open(t, 'w'))
@@ -1383,7 +1393,7 @@ testamem("constructors", function ()
1383end) 1393end)
1384 1394
1385local a = 1 1395local a = 1
1386close = nil 1396local close = nil
1387testamem("closure creation", function () 1397testamem("closure creation", function ()
1388 function close (b) 1398 function close (b)
1389 return function (x) return b + x end 1399 return function (x) return b + x end