diff options
Diffstat (limited to 'testes/api.lua')
-rw-r--r-- | testes/api.lua | 112 |
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 |
15 | local MEMERRMSG = "not enough memory" | 15 | local MEMERRMSG = "not enough memory" |
16 | 16 | ||
17 | function tcheck (t1, t2) | 17 | local 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 |
20 | end | 20 | end |
@@ -28,7 +28,7 @@ end | |||
28 | 28 | ||
29 | print('testing C API') | 29 | print('testing C API') |
30 | 30 | ||
31 | a = T.testC("pushvalue R; return 1") | 31 | local a = T.testC("pushvalue R; return 1") |
32 | assert(a == debug.getregistry()) | 32 | assert(a == debug.getregistry()) |
33 | 33 | ||
34 | 34 | ||
@@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0) | |||
43 | assert(a == string.pack("d", 12458954321123.0)) | 43 | assert(a == string.pack("d", 12458954321123.0)) |
44 | assert(T.s2d(a) == 12458954321123.0) | 44 | assert(T.s2d(a) == 12458954321123.0) |
45 | 45 | ||
46 | a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") | 46 | local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") |
47 | assert(a == 2 and b == 3 and not c) | 47 | assert(a == 2 and b == 3 and not c) |
48 | 48 | ||
49 | f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") | 49 | local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") |
50 | a,b,c = f() | 50 | a,b,c = f() |
51 | assert(a == 2 and b == 3 and not c) | 51 | assert(a == 2 and b == 3 and not c) |
52 | 52 | ||
@@ -61,7 +61,7 @@ assert(a==false and b==true and c==false) | |||
61 | a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) | 61 | a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) |
62 | assert(a == 40 and b == 5 and not c) | 62 | assert(a == 40 and b == 5 and not c) |
63 | 63 | ||
64 | t = pack(T.testC("settop 5; return *", 2, 3)) | 64 | local t = pack(T.testC("settop 5; return *", 2, 3)) |
65 | tcheck(t, {n=4,2,3}) | 65 | tcheck(t, {n=4,2,3}) |
66 | 66 | ||
67 | t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) | 67 | t = 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" |
170 | local a = {T.testC[[ | 170 | local 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 | ]]} |
177 | assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") | 177 | assert(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 |
181 | assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) | 182 | assert(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]]) |
201 | assert(a == 1 and b == -10 and c == "5") | 202 | assert(a == 1 and b == -10 and c == "5") |
202 | mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, | 203 | local 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} |
205 | a,b,c = setmetatable({4}, mt), | 207 | a,b,c = setmetatable({4}, mt), |
206 | setmetatable({8}, mt), | 208 | setmetatable({8}, mt), |
207 | setmetatable({-3}, mt) | 209 | setmetatable({-3}, mt) |
208 | x,y,z = T.testC("arith +; return 2", 10, a, b) | 210 | local x,y,z = T.testC("arith +; return 2", 10, a, b) |
209 | assert(x == 10 and y[1] == 12 and z == nil) | 211 | assert(x == 10 and y[1] == 12 and z == nil) |
210 | assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) | 212 | assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) |
211 | assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == | 213 | assert(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 | ||
315 | function B(x) return x and 1 or 0 end | 317 | local function B (x) return x and 1 or 0 end |
316 | 318 | ||
317 | function count (x, n) | 319 | local 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 | ||
348 | function to (s, x, n) | 350 | local 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) |
351 | end | 353 | end |
@@ -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 | ||
491 | assert(type(a) == 'string' and x == 150) | 493 | assert(type(a) == 'string' and XX == 150) |
494 | _G.XX = nil | ||
492 | 495 | ||
493 | function check3(p, ...) | 496 | local 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 *", ".")) | |||
500 | check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) | 503 | check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) |
501 | 504 | ||
502 | -- test errors in non protected threads | 505 | -- test errors in non protected threads |
503 | function checkerrnopro (code, msg) | 506 | local 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") |
516 | end | 520 | end |
517 | print"+" | 521 | print"+" |
@@ -588,7 +592,7 @@ assert(a[a] == "x") | |||
588 | 592 | ||
589 | b = setmetatable({p = a}, {}) | 593 | b = setmetatable({p = a}, {}) |
590 | getmetatable(b).__index = function (t, i) return t.p[i] end | 594 | getmetatable(b).__index = function (t, i) return t.p[i] end |
591 | k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") | 595 | local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") |
592 | assert(x == 15 and k == 35) | 596 | assert(x == 15 and k == 35) |
593 | k = T.testC("getfield 2 y, return 1", b) | 597 | k = T.testC("getfield 2 y, return 1", b) |
594 | assert(k == 12) | 598 | assert(k == 12) |
@@ -748,8 +752,8 @@ local i = T.ref{} | |||
748 | T.unref(i) | 752 | T.unref(i) |
749 | assert(T.ref{} == i) | 753 | assert(T.ref{} == i) |
750 | 754 | ||
751 | Arr = {} | 755 | local Arr = {} |
752 | Lim = 100 | 756 | local Lim = 100 |
753 | for i=1,Lim do -- lock many objects | 757 | for i=1,Lim do -- lock many objects |
754 | Arr[i] = T.ref({}) | 758 | Arr[i] = T.ref({}) |
755 | end | 759 | end |
@@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them | |||
761 | T.unref(Arr[i]) | 765 | T.unref(Arr[i]) |
762 | end | 766 | end |
763 | 767 | ||
764 | function printlocks () | 768 | local 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 |
796 | tt = {} | 800 | local tt = {} |
797 | cl = {n=0} | 801 | local cl = {n=0} |
798 | A = nil; B = nil | 802 | A = nil; B = nil |
799 | local F | 803 | local F |
800 | F = function (x) | 804 | F = function (x) |
@@ -817,6 +821,7 @@ F = function (x) | |||
817 | end | 821 | end |
818 | tt.__gc = F | 822 | tt.__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 |
821 | do | 826 | do |
822 | collectgarbage(); | 827 | collectgarbage(); |
@@ -853,9 +858,9 @@ end | |||
853 | collectgarbage("stop") | 858 | collectgarbage("stop") |
854 | 859 | ||
855 | -- create 3 userdatas with tag `tt' | 860 | -- create 3 userdatas with tag `tt' |
856 | a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) | 861 | a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a) |
857 | b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) | 862 | b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b) |
858 | c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) | 863 | c = 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 |
861 | x = T.newuserdata(4) | 866 | x = T.newuserdata(4) |
@@ -866,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x) | |||
866 | 871 | ||
867 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) | 872 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) |
868 | 873 | ||
869 | d=T.ref(a); | 874 | local d = T.ref(a); |
870 | e=T.ref(b); | 875 | local e = T.ref(b); |
871 | f=T.ref(c); | 876 | local f = T.ref(c); |
872 | t = {T.getref(d), T.getref(e), T.getref(f)} | 877 | t = {T.getref(d), T.getref(e), T.getref(f)} |
873 | assert(t[1] == a and t[2] == b and t[3] == c) | 878 | assert(t[1] == a and t[2] == b and t[3] == c) |
874 | 879 | ||
@@ -888,7 +893,7 @@ tt=nil -- frees tt for GC | |||
888 | A = nil | 893 | A = nil |
889 | b = nil | 894 | b = nil |
890 | T.unref(d); | 895 | T.unref(d); |
891 | n5 = T.newuserdata(0) | 896 | local n5 = T.newuserdata(0) |
892 | debug.setmetatable(n5, {__gc=F}) | 897 | debug.setmetatable(n5, {__gc=F}) |
893 | n5 = T.udataval(n5) | 898 | n5 = T.udataval(n5) |
894 | collectgarbage() | 899 | collectgarbage() |
@@ -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 = {} |
963 | T.sethook([[ | 968 | T.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) | |||
973 | a = 1 -- line hook | 978 | a = 1 -- line hook |
974 | a = 1 -- line hook | 979 | a = 1 -- line hook |
975 | debug.sethook() | 980 | debug.sethook() |
976 | t = _G.t | 981 | local t = _G.TT |
977 | assert(t[1] == "line") | 982 | assert(t[1] == "line") |
978 | line = t[2] | 983 | local line = t[2] |
979 | assert(t[3] == "line" and t[4] == line + 1) | 984 | assert(t[3] == "line" and t[4] == line + 1) |
980 | assert(t[5] == "line" and t[6] == line + 2) | 985 | assert(t[5] == "line" and t[6] == line + 2) |
981 | assert(t[7] == nil) | 986 | assert(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") |
1005 | end | 1011 | end |
1012 | _G.A = nil | ||
1006 | ------------------------------------------------------------------------- | 1013 | ------------------------------------------------------------------------- |
1007 | -- test for userdata vals | 1014 | -- test for userdata vals |
1008 | do | 1015 | do |
@@ -1032,8 +1039,8 @@ assert(a == 'alo' and b == '3') | |||
1032 | 1039 | ||
1033 | T.doremote(L1, "_ERRORMESSAGE = nil") | 1040 | T.doremote(L1, "_ERRORMESSAGE = nil") |
1034 | -- error: `sin' is not defined | 1041 | -- error: `sin' is not defined |
1035 | a, _, b = T.doremote(L1, "return sin(1)") | 1042 | a, b, c = T.doremote(L1, "return sin(1)") |
1036 | assert(a == nil and b == 2) -- 2 == run-time error | 1043 | assert(a == nil and c == 2) -- 2 == run-time error |
1037 | 1044 | ||
1038 | -- error: syntax error | 1045 | -- error: syntax error |
1039 | a, b, c = T.doremote(L1, "return a+") | 1046 | a, 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. |
1207 | function testbytes (s, f) | 1214 | local 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 | ||
1232 | function testalloc (s, f) | 1239 | local 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) |
1299 | mt = T.testC("rawgeti R 1; return 1") | 1306 | local mt = T.testC("rawgeti R 1; return 1") |
1300 | assert(type(mt) == "thread" and coroutine.running() == mt) | 1307 | assert(type(mt) == "thread" and coroutine.running() == mt) |
1301 | 1308 | ||
1302 | 1309 | ||
1303 | 1310 | ||
1304 | function expand (n,s) | 1311 | local 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 | |||
1311 | G=0; collectgarbage(); a =collectgarbage("count") | 1318 | G=0; collectgarbage(); a =collectgarbage("count") |
1312 | load(expand(20,"G=G+1"))() | 1319 | load(expand(20,"G=G+1"))() |
1313 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) | 1320 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) |
1321 | G = nil | ||
1314 | 1322 | ||
1315 | testamem("running code on new thread", function () | 1323 | testamem("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 |
1317 | end) | 1325 | end) |
1318 | 1326 | ||
1319 | 1327 | ||
@@ -1327,13 +1335,13 @@ end) | |||
1327 | local testprog = [[ | 1335 | local testprog = [[ |
1328 | local function foo () return end | 1336 | local function foo () return end |
1329 | local t = {"x"} | 1337 | local t = {"x"} |
1330 | a = "aaa" | 1338 | AA = "aaa" |
1331 | for i = 1, #t do a=a..t[i] end | 1339 | for i = 1, #t do AA = AA .. t[i] end |
1332 | return true | 1340 | return true |
1333 | ]] | 1341 | ]] |
1334 | 1342 | ||
1335 | -- testing memory x dofile | 1343 | -- testing memory x dofile |
1336 | _G.a = nil | 1344 | _G.AA = nil |
1337 | local t =os.tmpname() | 1345 | local t =os.tmpname() |
1338 | local f = assert(io.open(t, "w")) | 1346 | local f = assert(io.open(t, "w")) |
1339 | f:write(testprog) | 1347 | f:write(testprog) |
@@ -1343,7 +1351,7 @@ testamem("dofile", function () | |||
1343 | return a and a() | 1351 | return a and a() |
1344 | end) | 1352 | end) |
1345 | assert(os.remove(t)) | 1353 | assert(os.remove(t)) |
1346 | assert(_G.a == "aaax") | 1354 | assert(_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() |
1361 | end) | 1369 | end) |
1362 | 1370 | ||
1371 | _G.AA = nil | ||
1372 | |||
1363 | local t = os.tmpname() | 1373 | local t = os.tmpname() |
1364 | testamem("file creation", function () | 1374 | testamem("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 () | |||
1381 | end) | 1391 | end) |
1382 | 1392 | ||
1383 | local a = 1 | 1393 | local a = 1 |
1384 | close = nil | 1394 | local close = nil |
1385 | testamem("closure creation", function () | 1395 | testamem("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 |