aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
commit314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch)
tree594b7e873f2c29113d95c75147ab10865cdd772c
parent0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff)
downloadlua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.gz
lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.bz2
lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.zip
Avoid excessive name pollution in test files
Test files are more polite regarding the use of globals when locals would do, and when globals are necessary deleting them after use.
-rw-r--r--testes/all.lua1
-rw-r--r--testes/api.lua112
-rw-r--r--testes/attrib.lua24
-rw-r--r--testes/big.lua2
-rw-r--r--testes/calls.lua44
-rw-r--r--testes/closure.lua6
-rw-r--r--testes/code.lua6
-rw-r--r--testes/constructs.lua36
-rw-r--r--testes/coroutine.lua56
-rw-r--r--testes/db.lua27
-rw-r--r--testes/errors.lua83
-rw-r--r--testes/events.lua3
-rw-r--r--testes/files.lua6
-rw-r--r--testes/gc.lua42
-rw-r--r--testes/literals.lua33
-rw-r--r--testes/locals.lua8
-rw-r--r--testes/main.lua4
-rw-r--r--testes/math.lua6
-rw-r--r--testes/nextvar.lua2
-rw-r--r--testes/pm.lua32
-rw-r--r--testes/sort.lua23
-rw-r--r--testes/strings.lua6
-rw-r--r--testes/tpack.lua2
-rw-r--r--testes/utf8.lua2
-rw-r--r--testes/vararg.lua18
-rw-r--r--testes/verybig.lua12
26 files changed, 335 insertions, 261 deletions
diff --git a/testes/all.lua b/testes/all.lua
index a8e44024..5df0ff9b 100644
--- a/testes/all.lua
+++ b/testes/all.lua
@@ -163,6 +163,7 @@ f()
163 163
164dofile('db.lua') 164dofile('db.lua')
165assert(dofile('calls.lua') == deep and deep) 165assert(dofile('calls.lua') == deep and deep)
166_G.deep = nil
166olddofile('strings.lua') 167olddofile('strings.lua')
167olddofile('literals.lua') 168olddofile('literals.lua')
168dofile('tpack.lua') 169dofile('tpack.lua')
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
diff --git a/testes/attrib.lua b/testes/attrib.lua
index 83821c06..458488a8 100644
--- a/testes/attrib.lua
+++ b/testes/attrib.lua
@@ -85,7 +85,7 @@ local DIR = "libs" .. dirsep
85 85
86-- prepend DIR to a name and correct directory separators 86-- prepend DIR to a name and correct directory separators
87local function D (x) 87local function D (x)
88 x = string.gsub(x, "/", dirsep) 88 local x = string.gsub(x, "/", dirsep)
89 return DIR .. x 89 return DIR .. x
90end 90end
91 91
@@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras)
106 end 106 end
107end 107end
108 108
109function removefiles (files) 109local function removefiles (files)
110 for n in pairs(files) do 110 for n in pairs(files) do
111 os.remove(D(n)) 111 os.remove(D(n))
112 end 112 end
@@ -154,10 +154,9 @@ local try = function (p, n, r, ext)
154 assert(ext == x) 154 assert(ext == x)
155end 155end
156 156
157a = require"names" 157local a = require"names"
158assert(a[1] == "names" and a[2] == D"names.lua") 158assert(a[1] == "names" and a[2] == D"names.lua")
159 159
160_G.a = nil
161local st, msg = pcall(require, "err") 160local st, msg = pcall(require, "err")
162assert(not st and string.find(msg, "arithmetic") and B == 15) 161assert(not st and string.find(msg, "arithmetic") and B == 15)
163st, msg = pcall(require, "synerr") 162st, msg = pcall(require, "synerr")
@@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX")
191 190
192 191
193removefiles(files) 192removefiles(files)
193NAME, REQUIRED, AA, B = nil
194 194
195 195
196-- testing require of sub-packages 196-- testing require of sub-packages
@@ -223,7 +223,7 @@ assert(require"P1" == m and m.AA == 10)
223 223
224 224
225removefiles(files) 225removefiles(files)
226 226AA = nil
227 227
228package.path = "" 228package.path = ""
229assert(not pcall(require, "file_does_not_exist")) 229assert(not pcall(require, "file_does_not_exist"))
@@ -305,6 +305,7 @@ else
305 assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") 305 assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
306 assert(string.find(ext, "libs/lib1", 1, true)) 306 assert(string.find(ext, "libs/lib1", 1, true))
307 assert(fs.id(45) == 45) 307 assert(fs.id(45) == 45)
308 _ENV.x, _ENV.y = nil
308end 309end
309 310
310_ENV = _G 311_ENV = _G
@@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors")
338 339
339local res, res2 = 27 340local res, res2 = 27
340 341
341a, b = 1, 2+3 342local a, b = 1, 2+3
342assert(a==1 and b==5) 343assert(a==1 and b==5)
343a={} 344a={}
344function f() return 10, 11, 12 end 345local function f() return 10, 11, 12 end
345a.x, b, a[1] = 1, 2, f() 346a.x, b, a[1] = 1, 2, f()
346assert(a.x==1 and b==2 and a[1]==10) 347assert(a.x==1 and b==2 and a[1]==10)
347a[f()], b, a[f()+3] = f(), a, 'x' 348a[f()], b, a[f()+3] = f(), a, 'x'
@@ -353,15 +354,15 @@ do
353 local a,b,c 354 local a,b,c
354 a,b = 0, f(1) 355 a,b = 0, f(1)
355 assert(a == 0 and b == 1) 356 assert(a == 0 and b == 1)
356 A,b = 0, f(1) 357 a,b = 0, f(1)
357 assert(A == 0 and b == 1) 358 assert(a == 0 and b == 1)
358 a,b,c = 0,5,f(4) 359 a,b,c = 0,5,f(4)
359 assert(a==0 and b==5 and c==1) 360 assert(a==0 and b==5 and c==1)
360 a,b,c = 0,5,f(0) 361 a,b,c = 0,5,f(0)
361 assert(a==0 and b==5 and c==nil) 362 assert(a==0 and b==5 and c==nil)
362end 363end
363 364
364a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 365local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
365assert(not a and b and c and d==6) 366assert(not a and b and c and d==6)
366 367
367d = 20 368d = 20
@@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end))
419assert(a[nil] == undef) 420assert(a[nil] == undef)
420a = nil 421a = nil
421 422
423local a, b, c
422a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} 424a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
423a, a.x, a.y = a, a[-3] 425a, a.x, a.y = a, a[-3]
424assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) 426assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
@@ -455,7 +457,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do
455 maxint = maxint // 2 457 maxint = maxint // 2
456end 458end
457 459
458maxintF = maxint + 0.0 -- float version 460local maxintF = maxint + 0.0 -- float version
459 461
460assert(maxintF == maxint and math.type(maxintF) == "float" and 462assert(maxintF == maxint and math.type(maxintF) == "float" and
461 maxintF >= 2.0^14) 463 maxintF >= 2.0^14)
diff --git a/testes/big.lua b/testes/big.lua
index 39e293ef..46fd8466 100644
--- a/testes/big.lua
+++ b/testes/big.lua
@@ -32,7 +32,7 @@ setmetatable(env, {
32}) 32})
33 33
34X = nil 34X = nil
35co = coroutine.wrap(f) 35local co = coroutine.wrap(f)
36assert(co() == 's') 36assert(co() == 's')
37assert(co() == 'g') 37assert(co() == 'g')
38assert(co() == 'g') 38assert(co() == 'g')
diff --git a/testes/calls.lua b/testes/calls.lua
index ee8cce73..a1938584 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -16,7 +16,7 @@ assert(type(nil) == 'nil'
16 and type(type) == 'function') 16 and type(type) == 'function')
17 17
18assert(type(assert) == type(print)) 18assert(type(assert) == type(print))
19function f (x) return a:x (x) end 19local function f (x) return a:x (x) end
20assert(type(f) == 'function') 20assert(type(f) == 'function')
21assert(not pcall(type)) 21assert(not pcall(type))
22 22
@@ -33,10 +33,11 @@ do
33 assert(fact(5) == 120) 33 assert(fact(5) == 120)
34end 34end
35assert(fact == false) 35assert(fact == false)
36fact = nil
36 37
37-- testing declarations 38-- testing declarations
38a = {i = 10} 39local a = {i = 10}
39self = 20 40local self = 20
40function a:x (x) return x+self.i end 41function a:x (x) return x+self.i end
41function a.y (x) return x+self end 42function a.y (x) return x+self end
42 43
@@ -72,6 +73,8 @@ f(1,2, -- this one too
72 3,4) 73 3,4)
73assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') 74assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
74 75
76t = nil -- delete 't'
77
75function fat(x) 78function fat(x)
76 if x <= 1 then return 1 79 if x <= 1 then return 1
77 else return x*load("return fat(" .. x-1 .. ")", "")() 80 else return x*load("return fat(" .. x-1 .. ")", "")()
@@ -80,26 +83,29 @@ end
80 83
81assert(load "load 'assert(fat(6)==720)' () ")() 84assert(load "load 'assert(fat(6)==720)' () ")()
82a = load('return fat(5), 3') 85a = load('return fat(5), 3')
83a,b = a() 86local a,b = a()
84assert(a == 120 and b == 3) 87assert(a == 120 and b == 3)
88fat = nil
85print('+') 89print('+')
86 90
87function err_on_n (n) 91local function err_on_n (n)
88 if n==0 then error(); exit(1); 92 if n==0 then error(); exit(1);
89 else err_on_n (n-1); exit(1); 93 else err_on_n (n-1); exit(1);
90 end 94 end
91end 95end
92 96
93do 97do
94 function dummy (n) 98 local function dummy (n)
95 if n > 0 then 99 if n > 0 then
96 assert(not pcall(err_on_n, n)) 100 assert(not pcall(err_on_n, n))
97 dummy(n-1) 101 dummy(n-1)
98 end 102 end
99 end 103 end
104
105 dummy(10)
100end 106end
101 107
102dummy(10) 108_G.deep = nil -- "declaration" (used by 'all.lua')
103 109
104function deep (n) 110function deep (n)
105 if n>0 then deep(n-1) end 111 if n>0 then deep(n-1) end
@@ -209,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40)
209-- testing closures 215-- testing closures
210 216
211-- fixed-point operator 217-- fixed-point operator
212Z = function (le) 218local Z = function (le)
213 local function a (f) 219 local function a (f)
214 return le(function (x) return f(f)(x) end) 220 return le(function (x) return f(f)(x) end)
215 end 221 end
@@ -219,14 +225,14 @@ Z = function (le)
219 225
220-- non-recursive factorial 226-- non-recursive factorial
221 227
222F = function (f) 228local F = function (f)
223 return function (n) 229 return function (n)
224 if n == 0 then return 1 230 if n == 0 then return 1
225 else return n*f(n-1) end 231 else return n*f(n-1) end
226 end 232 end
227 end 233 end
228 234
229fat = Z(F) 235local fat = Z(F)
230 236
231assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) 237assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
232 238
@@ -237,22 +243,21 @@ local function g (z)
237 return f(z,z+1,z+2,z+3) 243 return f(z,z+1,z+2,z+3)
238end 244end
239 245
240f = g(10) 246local f = g(10)
241assert(f(9, 16) == 10+11+12+13+10+9+16+10) 247assert(f(9, 16) == 10+11+12+13+10+9+16+10)
242 248
243Z, F, f = nil
244print('+') 249print('+')
245 250
246-- testing multiple returns 251-- testing multiple returns
247 252
248function unlpack (t, i) 253local function unlpack (t, i)
249 i = i or 1 254 i = i or 1
250 if (i <= #t) then 255 if (i <= #t) then
251 return t[i], unlpack(t, i+1) 256 return t[i], unlpack(t, i+1)
252 end 257 end
253end 258end
254 259
255function equaltab (t1, t2) 260local function equaltab (t1, t2)
256 assert(#t1 == #t2) 261 assert(#t1 == #t2)
257 for i = 1, #t1 do 262 for i = 1, #t1 do
258 assert(t1[i] == t2[i]) 263 assert(t1[i] == t2[i])
@@ -261,8 +266,8 @@ end
261 266
262local pack = function (...) return (table.pack(...)) end 267local pack = function (...) return (table.pack(...)) end
263 268
264function f() return 1,2,30,4 end 269local function f() return 1,2,30,4 end
265function ret2 (a,b) return a,b end 270local function ret2 (a,b) return a,b end
266 271
267local a,b,c,d = unlpack{1,2,3} 272local a,b,c,d = unlpack{1,2,3}
268assert(a==1 and b==2 and c==3 and d==nil) 273assert(a==1 and b==2 and c==3 and d==nil)
@@ -291,7 +296,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
291local x = "-- a comment\0\0\0\n x = 10 + \n23; \ 296local x = "-- a comment\0\0\0\n x = 10 + \n23; \
292 local a = function () x = 'hi' end; \ 297 local a = function () x = 'hi' end; \
293 return '\0'" 298 return '\0'"
294function read1 (x) 299local function read1 (x)
295 local i = 0 300 local i = 0
296 return function () 301 return function ()
297 collectgarbage() 302 collectgarbage()
@@ -300,7 +305,7 @@ function read1 (x)
300 end 305 end
301end 306end
302 307
303function cannotload (msg, a,b) 308local function cannotload (msg, a,b)
304 assert(not a and string.find(b, msg)) 309 assert(not a and string.find(b, msg))
305end 310end
306 311
@@ -342,6 +347,7 @@ a = assert(load(read1(x), nil, "b"))
342assert(a() == 1 and _G.x == 1) 347assert(a() == 1 and _G.x == 1)
343cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) 348cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
344cannotload("attempt to load a binary chunk", load(x, nil, "t")) 349cannotload("attempt to load a binary chunk", load(x, nil, "t"))
350_G.x = nil
345 351
346assert(not pcall(string.dump, print)) -- no dump of C functions 352assert(not pcall(string.dump, print)) -- no dump of C functions
347 353
@@ -366,7 +372,7 @@ debug.setupvalue(x, 2, _G)
366assert(x() == 123) 372assert(x() == 123)
367 373
368assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) 374assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
369 375XX = nil
370 376
371-- test generic load with nested functions 377-- test generic load with nested functions
372x = [[ 378x = [[
diff --git a/testes/closure.lua b/testes/closure.lua
index c2453677..ea038e82 100644
--- a/testes/closure.lua
+++ b/testes/closure.lua
@@ -4,7 +4,7 @@
4print "testing closures" 4print "testing closures"
5 5
6local A,B = 0,{g=10} 6local A,B = 0,{g=10}
7function f(x) 7local function f(x)
8 local a = {} 8 local a = {}
9 for i=1,1000 do 9 for i=1,1000 do
10 local y = 0 10 local y = 0
@@ -89,6 +89,7 @@ assert(r == "a" and s == "b")
89 89
90 90
91-- testing closures with 'for' control variable x break 91-- testing closures with 'for' control variable x break
92local f
92for i=1,3 do 93for i=1,3 do
93 f = function () return i end 94 f = function () return i end
94 break 95 break
@@ -139,7 +140,7 @@ assert(b('get') == 'xuxu')
139b('set', 10); assert(b('get') == 14) 140b('set', 10); assert(b('get') == 14)
140 141
141 142
142local w 143local y, w
143-- testing multi-level closure 144-- testing multi-level closure
144function f(x) 145function f(x)
145 return function (y) 146 return function (y)
@@ -230,6 +231,7 @@ t()
230-- test for debug manipulation of upvalues 231-- test for debug manipulation of upvalues
231local debug = require'debug' 232local debug = require'debug'
232 233
234local foo1, foo2, foo3
233do 235do
234 local a , b, c = 3, 5, 7 236 local a , b, c = 3, 5, 7
235 foo1 = function () return a+b end; 237 foo1 = function () return a+b end;
diff --git a/testes/code.lua b/testes/code.lua
index 543743fc..bd4b10d0 100644
--- a/testes/code.lua
+++ b/testes/code.lua
@@ -86,7 +86,7 @@ checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0})
86-- testing opcodes 86-- testing opcodes
87 87
88-- check that 'f' opcodes match '...' 88-- check that 'f' opcodes match '...'
89function check (f, ...) 89local function check (f, ...)
90 local arg = {...} 90 local arg = {...}
91 local c = T.listcode(f) 91 local c = T.listcode(f)
92 for i=1, #arg do 92 for i=1, #arg do
@@ -99,7 +99,7 @@ end
99 99
100 100
101-- check that 'f' opcodes match '...' and that 'f(p) == r'. 101-- check that 'f' opcodes match '...' and that 'f(p) == r'.
102function checkR (f, p, r, ...) 102local function checkR (f, p, r, ...)
103 local r1 = f(p) 103 local r1 = f(p)
104 assert(r == r1 and math.type(r) == math.type(r1)) 104 assert(r == r1 and math.type(r) == math.type(r1))
105 check(f, ...) 105 check(f, ...)
@@ -107,7 +107,7 @@ end
107 107
108 108
109-- check that 'a' and 'b' has the same opcodes 109-- check that 'a' and 'b' has the same opcodes
110function checkequal (a, b) 110local function checkequal (a, b)
111 a = T.listcode(a) 111 a = T.listcode(a)
112 b = T.listcode(b) 112 b = T.listcode(b)
113 assert(#a == #b) 113 assert(#a == #b)
diff --git a/testes/constructs.lua b/testes/constructs.lua
index 0d9ec92d..6ac68166 100644
--- a/testes/constructs.lua
+++ b/testes/constructs.lua
@@ -11,6 +11,7 @@ local function checkload (s, msg)
11end 11end
12 12
13-- testing semicollons 13-- testing semicollons
14local a
14do ;;; end 15do ;;; end
15; do ; a = 3; assert(a == 3) end; 16; do ; a = 3; assert(a == 3) end;
16; 17;
@@ -49,10 +50,10 @@ assert((((nil and true) or false) and true) == false)
49 50
50local a,b = 1,nil; 51local a,b = 1,nil;
51assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); 52assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
52x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); 53local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
53x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); 54x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
54 55
55x,y=1,2; 56local x, y = 1, 2;
56assert((x>y) and x or y == 2); 57assert((x>y) and x or y == 2);
57x,y=2,1; 58x,y=2,1;
58assert((x>y) and x or y == 2); 59assert((x>y) and x or y == 2);
@@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants
77 local gab = f(o1, o2) 78 local gab = f(o1, o2)
78 79
79 _ENV.XX = o1 80 _ENV.XX = o1
80 code = string.format("return XX %s %s", op, o2) 81 local code = string.format("return XX %s %s", op, o2)
81 res = assert(load(code))() 82 local res = assert(load(code))()
82 assert(res == gab) 83 assert(res == gab)
83 84
84 _ENV.XX = o2 85 _ENV.XX = o2
85 local code = string.format("return (%s) %s XX", o1, op) 86 code = string.format("return (%s) %s XX", o1, op)
86 local res = assert(load(code))() 87 res = assert(load(code))()
87 assert(res == gab) 88 assert(res == gab)
88 89
89 code = string.format("return (%s) %s %s", o1, op, o2) 90 code = string.format("return (%s) %s %s", o1, op, o2)
@@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants
92 end 93 end
93 end 94 end
94 end 95 end
96 _ENV.XX = nil
95end 97end
96 98
97 99
@@ -100,7 +102,7 @@ repeat until 1; repeat until true;
100while false do end; while nil do end; 102while false do end; while nil do end;
101 103
102do -- test old bug (first name could not be an `upvalue') 104do -- test old bug (first name could not be an `upvalue')
103 local a; function f(x) x={a=1}; x={x=1}; x={G=1} end 105 local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end
104end 106end
105 107
106 108
@@ -128,7 +130,7 @@ do -- bug since 5.4.0
128end 130end
129 131
130 132
131function f (i) 133local function f (i)
132 if type(i) ~= 'number' then return i,'jojo'; end; 134 if type(i) ~= 'number' then return i,'jojo'; end;
133 if i > 0 then return i, f(i-1); end; 135 if i > 0 then return i, f(i-1); end;
134end 136end
@@ -154,10 +156,10 @@ end
154assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) 156assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
155 157
156for i=1,1000 do break; end; 158for i=1,1000 do break; end;
157n=100; 159local n=100;
158i=3; 160local i=3;
159t = {}; 161local t = {};
160a=nil 162local a=nil
161while not a do 163while not a do
162 a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; 164 a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
163end 165end
@@ -200,14 +202,14 @@ a={y=1}
200x = {a.y} 202x = {a.y}
201assert(x[1] == 1) 203assert(x[1] == 1)
202 204
203function f(i) 205local function f (i)
204 while 1 do 206 while 1 do
205 if i>0 then i=i-1; 207 if i>0 then i=i-1;
206 else return; end; 208 else return; end;
207 end; 209 end;
208end; 210end;
209 211
210function g(i) 212local function g(i)
211 while 1 do 213 while 1 do
212 if i>0 then i=i-1 214 if i>0 then i=i-1
213 else return end 215 else return end
@@ -272,7 +274,7 @@ function g (a,b,c,d,e)
272 if not (a>=b or c or d and e or nil) then return 0; else return 1; end; 274 if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
273end 275end
274 276
275function h (a,b,c,d,e) 277local function h (a,b,c,d,e)
276 while (a>=b or c or (d and e) or nil) do return 1; end; 278 while (a>=b or c or (d and e) or nil) do return 1; end;
277 return 0; 279 return 0;
278end; 280end;
@@ -300,7 +302,7 @@ do
300 assert(a==2) 302 assert(a==2)
301end 303end
302 304
303function F(a) 305local function F (a)
304 assert(debug.getinfo(1, "n").name == 'F') 306 assert(debug.getinfo(1, "n").name == 'F')
305 return a,2,3 307 return a,2,3
306end 308end
@@ -393,6 +395,8 @@ for n = 1, level do
393 if i % 60000 == 0 then print('+') end 395 if i % 60000 == 0 then print('+') end
394 end 396 end
395end 397end
398IX = nil
399_G.GLOB1 = nil
396------------------------------------------------------------------ 400------------------------------------------------------------------
397 401
398-- testing some syntax errors (chosen through 'gcov') 402-- testing some syntax errors (chosen through 'gcov')
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index 15fccc30..de7e46fb 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -30,7 +30,8 @@ local function eqtab (t1, t2)
30end 30end
31 31
32_G.x = nil -- declare x 32_G.x = nil -- declare x
33function foo (a, ...) 33_G.f = nil -- declare f
34local function foo (a, ...)
34 local x, y = coroutine.running() 35 local x, y = coroutine.running()
35 assert(x == f and y == false) 36 assert(x == f and y == false)
36 -- next call should not corrupt coroutine (but must fail, 37 -- next call should not corrupt coroutine (but must fail,
@@ -67,10 +68,11 @@ assert(coroutine.status(f) == "dead")
67s, a = coroutine.resume(f, "xuxu") 68s, a = coroutine.resume(f, "xuxu")
68assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") 69assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
69 70
71_G.f = nil
70 72
71-- yields in tail calls 73-- yields in tail calls
72local function foo (i) return coroutine.yield(i) end 74local function foo (i) return coroutine.yield(i) end
73f = coroutine.wrap(function () 75local f = coroutine.wrap(function ()
74 for i=1,10 do 76 for i=1,10 do
75 assert(foo(i) == _G.x) 77 assert(foo(i) == _G.x)
76 end 78 end
@@ -79,8 +81,10 @@ end)
79for i=1,10 do _G.x = i; assert(f(i) == i) end 81for i=1,10 do _G.x = i; assert(f(i) == i) end
80_G.x = 'xuxu'; assert(f('xuxu') == 'a') 82_G.x = 'xuxu'; assert(f('xuxu') == 'a')
81 83
84_G.x = nil
85
82-- recursive 86-- recursive
83function pf (n, i) 87local function pf (n, i)
84 coroutine.yield(n) 88 coroutine.yield(n)
85 pf(n*i, i+1) 89 pf(n*i, i+1)
86end 90end
@@ -93,14 +97,14 @@ for i=1,10 do
93end 97end
94 98
95-- sieve 99-- sieve
96function gen (n) 100local function gen (n)
97 return coroutine.wrap(function () 101 return coroutine.wrap(function ()
98 for i=2,n do coroutine.yield(i) end 102 for i=2,n do coroutine.yield(i) end
99 end) 103 end)
100end 104end
101 105
102 106
103function filter (p, g) 107local function filter (p, g)
104 return coroutine.wrap(function () 108 return coroutine.wrap(function ()
105 while 1 do 109 while 1 do
106 local n = g() 110 local n = g()
@@ -221,14 +225,14 @@ do
221 -- <close> versus pcall in coroutines 225 -- <close> versus pcall in coroutines
222 local X = false 226 local X = false
223 local Y = false 227 local Y = false
224 function foo () 228 local function foo ()
225 local x <close> = func2close(function (self, err) 229 local x <close> = func2close(function (self, err)
226 Y = debug.getinfo(2) 230 Y = debug.getinfo(2)
227 X = err 231 X = err
228 end) 232 end)
229 error(43) 233 error(43)
230 end 234 end
231 co = coroutine.create(function () return pcall(foo) end) 235 local co = coroutine.create(function () return pcall(foo) end)
232 local st1, st2, err = coroutine.resume(co) 236 local st1, st2, err = coroutine.resume(co)
233 assert(st1 and not st2 and err == 43) 237 assert(st1 and not st2 and err == 43)
234 assert(X == 43 and Y.what == "C") 238 assert(X == 43 and Y.what == "C")
@@ -275,7 +279,7 @@ end
275 279
276-- yielding across C boundaries 280-- yielding across C boundaries
277 281
278co = coroutine.wrap(function() 282local co = coroutine.wrap(function()
279 assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) 283 assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
280 assert(coroutine.isyieldable()) 284 assert(coroutine.isyieldable())
281 coroutine.yield(20) 285 coroutine.yield(20)
@@ -303,15 +307,15 @@ local r1, r2, v = f1(nil)
303assert(r1 and not r2 and v[1] == (10 + 1)*10/2) 307assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
304 308
305 309
306function f (a, b) a = coroutine.yield(a); error{a + b} end 310local function f (a, b) a = coroutine.yield(a); error{a + b} end
307function g(x) return x[1]*2 end 311local function g(x) return x[1]*2 end
308 312
309co = coroutine.wrap(function () 313co = coroutine.wrap(function ()
310 coroutine.yield(xpcall(f, g, 10, 20)) 314 coroutine.yield(xpcall(f, g, 10, 20))
311 end) 315 end)
312 316
313assert(co() == 10) 317assert(co() == 10)
314r, msg = co(100) 318local r, msg = co(100)
315assert(not r and msg == 240) 319assert(not r and msg == 240)
316 320
317 321
@@ -373,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead")
373a,b = coroutine.resume(x) 377a,b = coroutine.resume(x)
374assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") 378assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
375 379
380goo = nil
376 381
377-- co-routines x for loop 382-- co-routines x for loop
378function all (a, n, k) 383local function all (a, n, k)
379 if k == 0 then coroutine.yield(a) 384 if k == 0 then coroutine.yield(a)
380 else 385 else
381 for i=1,n do 386 for i=1,n do
@@ -415,7 +420,7 @@ assert(f() == 43 and f() == 53)
415 420
416-- old bug: attempt to resume itself 421-- old bug: attempt to resume itself
417 422
418function co_func (current_co) 423local function co_func (current_co)
419 assert(coroutine.running() == current_co) 424 assert(coroutine.running() == current_co)
420 assert(coroutine.resume(current_co) == false) 425 assert(coroutine.resume(current_co) == false)
421 coroutine.yield(10, 20) 426 coroutine.yield(10, 20)
@@ -491,15 +496,16 @@ a = nil
491-- access to locals of erroneous coroutines 496-- access to locals of erroneous coroutines
492local x = coroutine.create (function () 497local x = coroutine.create (function ()
493 local a = 10 498 local a = 10
494 _G.f = function () a=a+1; return a end 499 _G.F = function () a=a+1; return a end
495 error('x') 500 error('x')
496 end) 501 end)
497 502
498assert(not coroutine.resume(x)) 503assert(not coroutine.resume(x))
499-- overwrite previous position of local `a' 504-- overwrite previous position of local `a'
500assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) 505assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
501assert(_G.f() == 11) 506assert(_G.F() == 11)
502assert(_G.f() == 12) 507assert(_G.F() == 12)
508_G.F = nil
503 509
504 510
505if not T then 511if not T then
@@ -510,7 +516,7 @@ else
510 516
511 local turn 517 local turn
512 518
513 function fact (t, x) 519 local function fact (t, x)
514 assert(turn == t) 520 assert(turn == t)
515 if x == 0 then return 1 521 if x == 0 then return 1
516 else return x*fact(t, x-1) 522 else return x*fact(t, x-1)
@@ -579,6 +585,7 @@ else
579 _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) 585 _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
580 _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) 586 _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
581 assert(co() == 10) 587 assert(co() == 10)
588 _G.X = nil
582 589
583 -- testing yields in count hook 590 -- testing yields in count hook
584 co = coroutine.wrap(function () 591 co = coroutine.wrap(function ()
@@ -656,6 +663,8 @@ else
656 663
657 assert(X == 'a a a' and Y == 'OK') 664 assert(X == 'a a a' and Y == 'OK')
658 665
666 X, Y = nil
667
659 668
660 -- resuming running coroutine 669 -- resuming running coroutine
661 C = coroutine.create(function () 670 C = coroutine.create(function ()
@@ -701,7 +710,7 @@ else
701 X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; 710 X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
702 return 'ok']])) 711 return 'ok']]))
703 712
704 t = table.pack(T.testC(state, [[ 713 local t = table.pack(T.testC(state, [[
705 rawgeti R 1 # get main thread 714 rawgeti R 1 # get main thread
706 pushstring 'XX' 715 pushstring 'XX'
707 getglobal X # get function for body 716 getglobal X # get function for body
@@ -730,13 +739,13 @@ end
730 739
731 740
732-- leaving a pending coroutine open 741-- leaving a pending coroutine open
733_X = coroutine.wrap(function () 742_G.TO_SURVIVE = coroutine.wrap(function ()
734 local a = 10 743 local a = 10
735 local x = function () a = a+1 end 744 local x = function () a = a+1 end
736 coroutine.yield() 745 coroutine.yield()
737 end) 746 end)
738 747
739_X() 748_G.TO_SURVIVE()
740 749
741 750
742if not _soft then 751if not _soft then
@@ -935,7 +944,7 @@ assert(run(function ()
935do local _ENV = _ENV 944do local _ENV = _ENV
936 f = function () AAA = BBB + 1; return AAA end 945 f = function () AAA = BBB + 1; return AAA end
937end 946end
938g = new(10); g.k.BBB = 10; 947local g = new(10); g.k.BBB = 10;
939debug.setupvalue(f, 1, g) 948debug.setupvalue(f, 1, g)
940assert(run(f, {"idx", "nidx", "idx"}) == 11) 949assert(run(f, {"idx", "nidx", "idx"}) == 11)
941assert(g.k.AAA == 11) 950assert(g.k.AAA == 11)
@@ -1075,6 +1084,8 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34)
1075 1084
1076-- testing yields with continuations 1085-- testing yields with continuations
1077 1086
1087local y
1088
1078co = coroutine.wrap(function (...) return 1089co = coroutine.wrap(function (...) return
1079 T.testC([[ # initial function 1090 T.testC([[ # initial function
1080 yieldk 1 2 1091 yieldk 1 2
@@ -1127,6 +1138,9 @@ assert(x == "YIELD" and y == 4)
1127 1138
1128assert(not pcall(co)) -- coroutine should be dead 1139assert(not pcall(co)) -- coroutine should be dead
1129 1140
1141_G.ctx = nil
1142_G.status = nil
1143
1130 1144
1131-- bug in nCcalls 1145-- bug in nCcalls
1132local co = coroutine.wrap(function () 1146local co = coroutine.wrap(function ()
diff --git a/testes/db.lua b/testes/db.lua
index f891e9b8..02b96aca 100644
--- a/testes/db.lua
+++ b/testes/db.lua
@@ -16,7 +16,7 @@ end
16assert(not debug.gethook()) 16assert(not debug.gethook())
17 17
18local testline = 19 -- line where 'test' is defined 18local testline = 19 -- line where 'test' is defined
19function test (s, l, p) -- this must be line 19 19local function test (s, l, p) -- this must be line 19
20 collectgarbage() -- avoid gc during trace 20 collectgarbage() -- avoid gc during trace
21 local function f (event, line) 21 local function f (event, line)
22 assert(event == 'line') 22 assert(event == 'line')
@@ -50,7 +50,7 @@ end
50 50
51 51
52-- test file and string names truncation 52-- test file and string names truncation
53a = "function f () end" 53local a = "function f () end"
54local function dostring (s, x) return load(s, x)() end 54local function dostring (s, x) return load(s, x)() end
55dostring(a) 55dostring(a)
56assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) 56assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
@@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500)))
72assert(string.find(debug.getinfo(f).short_src, "^x*$")) 72assert(string.find(debug.getinfo(f).short_src, "^x*$"))
73dostring(a, "=") 73dostring(a, "=")
74assert(debug.getinfo(f).short_src == "") 74assert(debug.getinfo(f).short_src == "")
75a = nil; f = nil; 75_G.a = nil; _G.f = nil;
76_G[string.rep("p", 400)] = nil
76 77
77 78
78repeat 79repeat
@@ -120,6 +121,7 @@ else
120end 121end
121]], {2,3,4,7}) 122]], {2,3,4,7})
122 123
124
123test([[ 125test([[
124local function foo() 126local function foo()
125end 127end
@@ -128,6 +130,7 @@ A = 1
128A = 2 130A = 2
129A = 3 131A = 3
130]], {2, 3, 2, 4, 5, 6}) 132]], {2, 3, 2, 4, 5, 6})
133_G.A = nil
131 134
132 135
133test([[-- 136test([[--
@@ -175,6 +178,8 @@ end
175 178
176test([[for i=1,4 do a=1 end]], {1,1,1,1}) 179test([[for i=1,4 do a=1 end]], {1,1,1,1})
177 180
181_G.a = nil
182
178 183
179do -- testing line info/trace with large gaps in source 184do -- testing line info/trace with large gaps in source
180 185
@@ -194,6 +199,7 @@ do -- testing line info/trace with large gaps in source
194 end 199 end
195 end 200 end
196end 201end
202_G.a = nil
197 203
198 204
199do -- testing active lines 205do -- testing active lines
@@ -287,7 +293,6 @@ foo(200, 3, 4)
287local a = {} 293local a = {}
288for i = 1, (_soft and 100 or 1000) do a[i] = i end 294for i = 1, (_soft and 100 or 1000) do a[i] = i end
289foo(table.unpack(a)) 295foo(table.unpack(a))
290a = nil
291 296
292 297
293 298
@@ -307,13 +312,14 @@ do -- test hook presence in debug info
307 debug.sethook() 312 debug.sethook()
308 assert(count == 4) 313 assert(count == 4)
309end 314end
315_ENV.a = nil
310 316
311 317
312-- hook table has weak keys 318-- hook table has weak keys
313assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') 319assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
314 320
315 321
316a = {}; L = nil 322a = {}; local L = nil
317local glob = 1 323local glob = 1
318local oldglob = glob 324local oldglob = glob
319debug.sethook(function (e,l) 325debug.sethook(function (e,l)
@@ -354,7 +360,7 @@ function foo()
354end; foo() -- set L 360end; foo() -- set L
355-- check line counting inside strings and empty lines 361-- check line counting inside strings and empty lines
356 362
357_ = 'alo\ 363local _ = 'alo\
358alo' .. [[ 364alo' .. [[
359 365
360]] 366]]
@@ -403,6 +409,7 @@ function g(a,b) return (a+1) + f() end
403 409
404assert(g(0,0) == 30) 410assert(g(0,0) == 30)
405 411
412_G.f, _G.g = nil
406 413
407debug.sethook(nil); 414debug.sethook(nil);
408assert(not debug.gethook()) 415assert(not debug.gethook())
@@ -446,7 +453,7 @@ local function collectlocals (level)
446end 453end
447 454
448 455
449X = nil 456local X = nil
450a = {} 457a = {}
451function a:f (a, b, ...) local arg = {...}; local c = 13 end 458function a:f (a, b, ...) local arg = {...}; local c = 13 end
452debug.sethook(function (e) 459debug.sethook(function (e)
@@ -469,6 +476,7 @@ a:f(1,2,3,4,5)
469assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) 476assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
470assert(XX == 12) 477assert(XX == 12)
471assert(not debug.gethook()) 478assert(not debug.gethook())
479_G.XX = nil
472 480
473 481
474-- testing access to local variables in return hook (bug in 5.2) 482-- testing access to local variables in return hook (bug in 5.2)
@@ -593,6 +601,7 @@ end
593 601
594debug.sethook() 602debug.sethook()
595 603
604local g, g1
596 605
597-- tests for tail calls 606-- tests for tail calls
598local function f (x) 607local function f (x)
@@ -638,7 +647,7 @@ h(false)
638debug.sethook() 647debug.sethook()
639assert(b == 2) -- two tail calls 648assert(b == 2) -- two tail calls
640 649
641lim = _soft and 3000 or 30000 650local lim = _soft and 3000 or 30000
642local function foo (x) 651local function foo (x)
643 if x==0 then 652 if x==0 then
644 assert(debug.getinfo(2).what == "main") 653 assert(debug.getinfo(2).what == "main")
@@ -940,7 +949,7 @@ end
940 949
941 950
942print("testing debug functions on chunk without debug info") 951print("testing debug functions on chunk without debug info")
943prog = [[-- program to be loaded without debug information (strip) 952local prog = [[-- program to be loaded without debug information (strip)
944local debug = require'debug' 953local debug = require'debug'
945local a = 12 -- a local variable 954local a = 12 -- a local variable
946 955
diff --git a/testes/errors.lua b/testes/errors.lua
index 55bdab82..cf0ab526 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -114,12 +114,14 @@ checkmessage("a = {} | 1", "bitwise operation")
114checkmessage("a = {} < 1", "attempt to compare") 114checkmessage("a = {} < 1", "attempt to compare")
115checkmessage("a = {} <= 1", "attempt to compare") 115checkmessage("a = {} <= 1", "attempt to compare")
116 116
117checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") 117checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
118checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") 118checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
119checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") 119checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
120assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) 120assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
121checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") 121checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
122checkmessage("a=(1)..{}", "a table value") 122checkmessage("aaa=(1)..{}", "a table value")
123
124_G.aaa, _G.bbbb = nil
123 125
124-- calls 126-- calls
125checkmessage("local a; a(13)", "local 'a'") 127checkmessage("local a; a(13)", "local 'a'")
@@ -134,12 +136,13 @@ checkmessage([[
134 136
135-- tail calls 137-- tail calls
136checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") 138checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
137checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") 139checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
140
141checkmessage("aaa = #print", "length of a function value")
142checkmessage("aaa = #3", "length of a number value")
138 143
139checkmessage("a = #print", "length of a function value") 144_G.aaa = nil
140checkmessage("a = #3", "length of a number value")
141 145
142aaa = nil
143checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") 146checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
144checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") 147checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
145checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") 148checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
@@ -152,15 +155,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
152 155
153checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") 156checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
154 157
155checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") 158checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
156checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") 159checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
157checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") 160checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
158checkmessage("aaa={}; x=-aaa", "global 'aaa'") 161checkmessage("aaa={}; x=-aaa", "global 'aaa'")
159 162
160-- short circuit 163-- short circuit
161checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", 164checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
162 "local 'bbbb'") 165 "local 'bbbb'")
163checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") 166checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
167 "local 'bbbb'")
164checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") 168checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
165checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") 169checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
166assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) 170assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
@@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation")
187checkmessage("return ~-3.009", "has no integer representation") 191checkmessage("return ~-3.009", "has no integer representation")
188checkmessage("return 3.009 & 1", "has no integer representation") 192checkmessage("return 3.009 & 1", "has no integer representation")
189checkmessage("return 34 >> {}", "table value") 193checkmessage("return 34 >> {}", "table value")
190checkmessage("a = 24 // 0", "divide by zero") 194checkmessage("aaa = 24 // 0", "divide by zero")
191checkmessage("a = 1 % 0", "'n%0'") 195checkmessage("aaa = 1 % 0", "'n%0'")
192 196
193 197
194-- type error for an object which is neither in an upvalue nor a register. 198-- type error for an object which is neither in an upvalue nor a register.
@@ -269,13 +273,13 @@ end
269-- tests for field accesses after RK limit 273-- tests for field accesses after RK limit
270local t = {} 274local t = {}
271for i = 1, 1000 do 275for i = 1, 1000 do
272 t[i] = "a = x" .. i 276 t[i] = "aaa = x" .. i
273end 277end
274local s = table.concat(t, "; ") 278local s = table.concat(t, "; ")
275t = nil 279t = nil
276checkmessage(s.."; a = bbb + 1", "global 'bbb'") 280checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
277checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") 281checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
278checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") 282checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
279checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") 283checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
280 284
281checkmessage([[aaa=9 285checkmessage([[aaa=9
@@ -324,14 +328,17 @@ main()
324]], "global 'NoSuchName'") 328]], "global 'NoSuchName'")
325print'+' 329print'+'
326 330
327a = {}; setmetatable(a, {__index = string}) 331aaa = {}; setmetatable(aaa, {__index = string})
328checkmessage("a:sub()", "bad self") 332checkmessage("aaa:sub()", "bad self")
329checkmessage("string.sub('a', {})", "#2") 333checkmessage("string.sub('a', {})", "#2")
330checkmessage("('a'):sub{}", "#1") 334checkmessage("('a'):sub{}", "#1")
331 335
332checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") 336checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
333checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") 337checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
334 338
339_G.aaa = nil
340
341
335-- tests for errors in coroutines 342-- tests for errors in coroutines
336 343
337local function f (n) 344local function f (n)
@@ -349,7 +356,7 @@ checkerr("yield across", f)
349 356
350-- testing size of 'source' info; size of buffer for that info is 357-- testing size of 'source' info; size of buffer for that info is
351-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. 358-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
352idsize = 60 - 1 359local idsize = 60 - 1
353local function checksize (source) 360local function checksize (source)
354 -- syntax error 361 -- syntax error
355 local _, msg = load("x", source) 362 local _, msg = load("x", source)
@@ -411,13 +418,14 @@ x
411 418
412local p = [[ 419local p = [[
413 function g() f() end 420 function g() f() end
414 function f(x) error('a', X) end 421 function f(x) error('a', XX) end
415g() 422g()
416]] 423]]
417X=3;lineerror((p), 3) 424XX=3;lineerror((p), 3)
418X=0;lineerror((p), false) 425XX=0;lineerror((p), false)
419X=1;lineerror((p), 2) 426XX=1;lineerror((p), 2)
420X=2;lineerror((p), 1) 427XX=2;lineerror((p), 1)
428_G.XX, _G.g, _G.f = nil
421 429
422 430
423lineerror([[ 431lineerror([[
@@ -449,11 +457,11 @@ if not _soft then
449 -- several tests that exaust the Lua stack 457 -- several tests that exaust the Lua stack
450 collectgarbage() 458 collectgarbage()
451 print"testing stack overflow" 459 print"testing stack overflow"
452 C = 0 460 local C = 0
453 -- get line where stack overflow will happen 461 -- get line where stack overflow will happen
454 local l = debug.getinfo(1, "l").currentline + 1 462 local l = debug.getinfo(1, "l").currentline + 1
455 local function auxy () C=C+1; auxy() end -- produce a stack overflow 463 local function auxy () C=C+1; auxy() end -- produce a stack overflow
456 function y () 464 function YY ()
457 collectgarbage("stop") -- avoid running finalizers without stack space 465 collectgarbage("stop") -- avoid running finalizers without stack space
458 auxy() 466 auxy()
459 collectgarbage("restart") 467 collectgarbage("restart")
@@ -465,9 +473,11 @@ if not _soft then
465 return (string.find(m, "stack overflow")) 473 return (string.find(m, "stack overflow"))
466 end 474 end
467 -- repeated stack overflows (to check stack recovery) 475 -- repeated stack overflows (to check stack recovery)
468 assert(checkstackmessage(doit('y()'))) 476 assert(checkstackmessage(doit('YY()')))
469 assert(checkstackmessage(doit('y()'))) 477 assert(checkstackmessage(doit('YY()')))
470 assert(checkstackmessage(doit('y()'))) 478 assert(checkstackmessage(doit('YY()')))
479
480 _G.YY = nil
471 481
472 482
473 -- error lines in stack overflow 483 -- error lines in stack overflow
@@ -561,7 +571,7 @@ do
561end 571end
562 572
563-- xpcall with arguments 573-- xpcall with arguments
564a, b, c = xpcall(string.find, error, "alo", "al") 574local a, b, c = xpcall(string.find, error, "alo", "al")
565assert(a and b == 1 and c == 2) 575assert(a and b == 1 and c == 2)
566a, b, c = xpcall(string.find, function (x) return {} end, true, "al") 576a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
567assert(not a and type(b) == "table" and c == nil) 577assert(not a and type(b) == "table" and c == nil)
@@ -581,11 +591,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
581-- test 255 as first char in a chunk 591-- test 255 as first char in a chunk
582checksyntax("\255a = 1", "", "<\\255>", 1) 592checksyntax("\255a = 1", "", "<\\255>", 1)
583 593
584doit('I = load("a=9+"); a=3') 594doit('I = load("a=9+"); aaa=3')
585assert(a==3 and not I) 595assert(_G.aaa==3 and not _G.I)
596_G.I,_G.aaa = nil
586print('+') 597print('+')
587 598
588lim = 1000 599local lim = 1000
589if _soft then lim = 100 end 600if _soft then lim = 100 end
590for i=1,lim do 601for i=1,lim do
591 doit('a = ') 602 doit('a = ')
diff --git a/testes/events.lua b/testes/events.lua
index 17a73664..8d8563b9 100644
--- a/testes/events.lua
+++ b/testes/events.lua
@@ -420,6 +420,9 @@ assert(i == 3 and x[1] == 3 and x[3] == 5)
420 420
421assert(_G.X == 20) 421assert(_G.X == 20)
422 422
423_G.X, _G.B = nil
424
425
423print'+' 426print'+'
424 427
425local _g = _G 428local _g = _G
diff --git a/testes/files.lua b/testes/files.lua
index 78f962e5..be00bf3f 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -507,15 +507,17 @@ load((io.lines(file, 1)))()
507assert(_G.X == 4) 507assert(_G.X == 4)
508load((io.lines(file, 3)))() 508load((io.lines(file, 3)))()
509assert(_G.X == 8) 509assert(_G.X == 8)
510_G.X = nil
510 511
511print('+') 512print('+')
512 513
513local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" 514local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
514io.output(file) 515io.output(file)
515assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) 516assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1)))
516io.close() 517io.close()
517assert(loadfile(file))() 518assert(loadfile(file))()
518assert(x1 == x2) 519assert(x1 == _G.X2)
520_G.X2 = nil
519print('+') 521print('+')
520assert(os.remove(file)) 522assert(os.remove(file))
521assert(not os.remove(file)) 523assert(not os.remove(file))
diff --git a/testes/gc.lua b/testes/gc.lua
index 381c5548..03093e34 100644
--- a/testes/gc.lua
+++ b/testes/gc.lua
@@ -125,7 +125,7 @@ do
125 end 125 end
126 126
127 a:test() 127 a:test()
128 128 _G.temp = nil
129end 129end
130 130
131 131
@@ -134,7 +134,7 @@ do local f = function () end end
134 134
135 135
136print("functions with errors") 136print("functions with errors")
137prog = [[ 137local prog = [[
138do 138do
139 a = 10; 139 a = 10;
140 function foo(x,y) 140 function foo(x,y)
@@ -153,22 +153,25 @@ do
153 end 153 end
154 end 154 end
155end 155end
156rawset(_G, "a", nil)
157_G.x = nil
156 158
157foo = nil 159do
158print('long strings') 160 foo = nil
159x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" 161 print('long strings')
160assert(string.len(x)==80) 162 local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
161s = '' 163 assert(string.len(x)==80)
162k = math.min(300, (math.maxinteger // 80) // 2) 164 local s = ''
163for n = 1, k do s = s..x; j=tostring(n) end 165 local k = math.min(300, (math.maxinteger // 80) // 2)
164assert(string.len(s) == k*80) 166 for n = 1, k do s = s..x; local j=tostring(n) end
165s = string.sub(s, 1, 10000) 167 assert(string.len(s) == k*80)
166s, i = string.gsub(s, '(%d%d%d%d)', '') 168 s = string.sub(s, 1, 10000)
167assert(i==10000 // 4) 169 local s, i = string.gsub(s, '(%d%d%d%d)', '')
168s = nil 170 assert(i==10000 // 4)
169x = nil 171
170 172 assert(_G["while"] == 234)
171assert(_G["while"] == 234) 173 _G["while"] = nil
174end
172 175
173 176
174-- 177--
@@ -227,8 +230,8 @@ end
227 230
228 231
229print("clearing tables") 232print("clearing tables")
230lim = 15 233local lim = 15
231a = {} 234local a = {}
232-- fill a with `collectable' indices 235-- fill a with `collectable' indices
233for i=1,lim do a[{}] = i end 236for i=1,lim do a[{}] = i end
234b = {} 237b = {}
@@ -552,6 +555,7 @@ do
552 for i=1,1000 do _ENV.a = {} end -- no collection during the loop 555 for i=1,1000 do _ENV.a = {} end -- no collection during the loop
553 until gcinfo() > 2 * x 556 until gcinfo() > 2 * x
554 collectgarbage"restart" 557 collectgarbage"restart"
558 _ENV.a = nil
555end 559end
556 560
557 561
diff --git a/testes/literals.lua b/testes/literals.lua
index d5a769ed..30ab9ab1 100644
--- a/testes/literals.lua
+++ b/testes/literals.lua
@@ -10,6 +10,7 @@ local function dostring (x) return assert(load(x), "")() end
10 10
11dostring("x \v\f = \t\r 'a\0a' \v\f\f") 11dostring("x \v\f = \t\r 'a\0a' \v\f\f")
12assert(x == 'a\0a' and string.len(x) == 3) 12assert(x == 'a\0a' and string.len(x) == 3)
13_G.x = nil
13 14
14-- escape sequences 15-- escape sequences
15assert('\n\"\'\\' == [[ 16assert('\n\"\'\\' == [[
@@ -129,16 +130,16 @@ end
129 130
130-- long variable names 131-- long variable names
131 132
132var1 = string.rep('a', 15000) .. '1' 133local var1 = string.rep('a', 15000) .. '1'
133var2 = string.rep('a', 15000) .. '2' 134local var2 = string.rep('a', 15000) .. '2'
134prog = string.format([[ 135local prog = string.format([[
135 %s = 5 136 %s = 5
136 %s = %s + 1 137 %s = %s + 1
137 return function () return %s - %s end 138 return function () return %s - %s end
138]], var1, var2, var1, var1, var2) 139]], var1, var2, var1, var1, var2)
139local f = dostring(prog) 140local f = dostring(prog)
140assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) 141assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
141var1, var2, f = nil 142_G[var1], _G[var2] = nil
142print('+') 143print('+')
143 144
144-- escapes -- 145-- escapes --
@@ -150,13 +151,13 @@ assert([[
150 $debug]] == "\n $debug") 151 $debug]] == "\n $debug")
151assert([[ [ ]] ~= [[ ] ]]) 152assert([[ [ ]] ~= [[ ] ]])
152-- long strings -- 153-- long strings --
153b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" 154local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
154assert(string.len(b) == 960) 155assert(string.len(b) == 960)
155prog = [=[ 156prog = [=[
156print('+') 157print('+')
157 158
158a1 = [["this is a 'string' with several 'quotes'"]] 159local a1 = [["this is a 'string' with several 'quotes'"]]
159a2 = "'quotes'" 160local a2 = "'quotes'"
160 161
161assert(string.find(a1, a2) == 34) 162assert(string.find(a1, a2) == 34)
162print('+') 163print('+')
@@ -164,12 +165,13 @@ print('+')
164a1 = [==[temp = [[an arbitrary value]]; ]==] 165a1 = [==[temp = [[an arbitrary value]]; ]==]
165assert(load(a1))() 166assert(load(a1))()
166assert(temp == 'an arbitrary value') 167assert(temp == 'an arbitrary value')
168_G.temp = nil
167-- long strings -- 169-- long strings --
168b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" 170local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
169assert(string.len(b) == 960) 171assert(string.len(b) == 960)
170print('+') 172print('+')
171 173
172a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 174local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
17300123456789012345678901234567890123456789123456789012345678901234567890123456789 17500123456789012345678901234567890123456789123456789012345678901234567890123456789
17400123456789012345678901234567890123456789123456789012345678901234567890123456789 17600123456789012345678901234567890123456789123456789012345678901234567890123456789
17500123456789012345678901234567890123456789123456789012345678901234567890123456789 17700123456789012345678901234567890123456789123456789012345678901234567890123456789
@@ -199,13 +201,11 @@ x = 1
199]=] 201]=]
200 202
201print('+') 203print('+')
202x = nil 204_G.x = nil
203dostring(prog) 205dostring(prog)
204assert(x) 206assert(x)
207_G.x = nil
205 208
206prog = nil
207a = nil
208b = nil
209 209
210 210
211do -- reuse of long strings 211do -- reuse of long strings
@@ -234,8 +234,8 @@ end
234 234
235-- testing line ends 235-- testing line ends
236prog = [[ 236prog = [[
237a = 1 -- a comment 237local a = 1 -- a comment
238b = 2 238local b = 2
239 239
240 240
241x = [=[ 241x = [=[
@@ -252,10 +252,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
252 assert(dostring(prog) == nn) 252 assert(dostring(prog) == nn)
253 assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") 253 assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
254end 254end
255_G.x, _G.y = nil
255 256
256 257
257-- testing comments and strings with long brackets 258-- testing comments and strings with long brackets
258a = [==[]=]==] 259local a = [==[]=]==]
259assert(a == "]=") 260assert(a == "]=")
260 261
261a = [==[[===[[=[]]=][====[]]===]===]==] 262a = [==[[===[[=[]]=][====[]]===]===]==]
diff --git a/testes/locals.lua b/testes/locals.lua
index d50beaa5..2c48546d 100644
--- a/testes/locals.lua
+++ b/testes/locals.lua
@@ -37,7 +37,7 @@ end
37f = nil 37f = nil
38 38
39local f 39local f
40x = 1 40local x = 1
41 41
42a = nil 42a = nil
43load('local a = {}')() 43load('local a = {}')()
@@ -152,7 +152,7 @@ local dummy
152local _ENV = (function (...) return ... end)(_G, dummy) -- { 152local _ENV = (function (...) return ... end)(_G, dummy) -- {
153 153
154do local _ENV = {assert=assert}; assert(true) end 154do local _ENV = {assert=assert}; assert(true) end
155mt = {_G = _G} 155local mt = {_G = _G}
156local foo,x 156local foo,x
157A = false -- "declare" A 157A = false -- "declare" A
158do local _ENV = mt 158do local _ENV = mt
@@ -174,6 +174,8 @@ do local _ENV = {assert=assert, A=10};
174end 174end
175assert(x==20) 175assert(x==20)
176 176
177A = nil
178
177 179
178do -- constants 180do -- constants
179 local a<const>, b, c<const> = 10, 20, 30 181 local a<const>, b, c<const> = 10, 20, 30
@@ -711,7 +713,7 @@ if rawget(_G, "T") then
711 713
712 collectgarbage(); collectgarbage() 714 collectgarbage(); collectgarbage()
713 715
714 m = T.totalmem() 716 local m = T.totalmem()
715 collectgarbage("stop") 717 collectgarbage("stop")
716 718
717 -- error in the first buffer allocation 719 -- error in the first buffer allocation
diff --git a/testes/main.lua b/testes/main.lua
index 9187420e..f59badcf 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -339,7 +339,7 @@ prepfile("a = [[b\nc\nd\ne]]\n=a")
339RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) 339RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
340checkprogout("b\nc\nd\ne\n\n") 340checkprogout("b\nc\nd\ne\n\n")
341 341
342prompt = "alo" 342local prompt = "alo"
343prepfile[[ -- 343prepfile[[ --
344a = 2 344a = 2
345]] 345]]
@@ -390,7 +390,7 @@ NoRun("error object is a table value", [[lua %s]], prog)
390 390
391 391
392-- chunk broken in many lines 392-- chunk broken in many lines
393s = [=[ -- 393local s = [=[ --
394function f ( x ) 394function f ( x )
395 local a = [[ 395 local a = [[
396xuxu 396xuxu
diff --git a/testes/math.lua b/testes/math.lua
index 48c1efe1..0191f7dd 100644
--- a/testes/math.lua
+++ b/testes/math.lua
@@ -50,7 +50,7 @@ end
50local msgf2i = "number.* has no integer representation" 50local msgf2i = "number.* has no integer representation"
51 51
52-- float equality 52-- float equality
53function eq (a,b,limit) 53local function eq (a,b,limit)
54 if not limit then 54 if not limit then
55 if floatbits >= 50 then limit = 1E-11 55 if floatbits >= 50 then limit = 1E-11
56 else limit = 1E-5 56 else limit = 1E-5
@@ -62,7 +62,7 @@ end
62 62
63 63
64-- equality with types 64-- equality with types
65function eqT (a,b) 65local function eqT (a,b)
66 return a == b and math.type(a) == math.type(b) 66 return a == b and math.type(a) == math.type(b)
67end 67end
68 68
@@ -83,7 +83,7 @@ end
83do 83do
84 local x = -1 84 local x = -1
85 local mz = 0/x -- minus zero 85 local mz = 0/x -- minus zero
86 t = {[0] = 10, 20, 30, 40, 50} 86 local t = {[0] = 10, 20, 30, 40, 50}
87 assert(t[mz] == t[0] and t[-0] == t[0]) 87 assert(t[mz] == t[0] and t[-0] == t[0])
88end 88end
89 89
diff --git a/testes/nextvar.lua b/testes/nextvar.lua
index 0874e5bb..02b7dea2 100644
--- a/testes/nextvar.lua
+++ b/testes/nextvar.lua
@@ -189,7 +189,7 @@ end
189 189
190-- size tests for vararg 190-- size tests for vararg
191lim = 35 191lim = 35
192function foo (n, ...) 192local function foo (n, ...)
193 local arg = {...} 193 local arg = {...}
194 check(arg, n, 0) 194 check(arg, n, 0)
195 assert(select('#', ...) == n) 195 assert(select('#', ...) == n)
diff --git a/testes/pm.lua b/testes/pm.lua
index 94bb63ca..795596d4 100644
--- a/testes/pm.lua
+++ b/testes/pm.lua
@@ -9,12 +9,12 @@ local function checkerror (msg, f, ...)
9end 9end
10 10
11 11
12function f(s, p) 12local function f (s, p)
13 local i,e = string.find(s, p) 13 local i,e = string.find(s, p)
14 if i then return string.sub(s, i, e) end 14 if i then return string.sub(s, i, e) end
15end 15end
16 16
17a,b = string.find('', '') -- empty patterns are tricky 17local a,b = string.find('', '') -- empty patterns are tricky
18assert(a == 1 and b == 0); 18assert(a == 1 and b == 0);
19a,b = string.find('alo', '') 19a,b = string.find('alo', '')
20assert(a == 1 and b == 0) 20assert(a == 1 and b == 0)
@@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo")
88print('+') 88print('+')
89 89
90 90
91function f1(s, p) 91local function f1 (s, p)
92 p = string.gsub(p, "%%([0-9])", function (s) 92 p = string.gsub(p, "%%([0-9])", function (s)
93 return "%" .. (tonumber(s)+1) 93 return "%" .. (tonumber(s)+1)
94 end) 94 end)
@@ -113,7 +113,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255));
113 113
114assert(string.len(abc) == 256) 114assert(string.len(abc) == 256)
115 115
116function strset (p) 116local function strset (p)
117 local res = {s=''} 117 local res = {s=''}
118 string.gsub(abc, p, function (c) res.s = res.s .. c end) 118 string.gsub(abc, p, function (c) res.s = res.s .. c end)
119 return res.s 119 return res.s
@@ -147,7 +147,7 @@ assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
147assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim 147assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
148assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim 148assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
149assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') 149assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
150t = "abç d" 150local t = "abç d"
151a, b = string.gsub(t, '(.)', '%1@') 151a, b = string.gsub(t, '(.)', '%1@')
152assert('@'..a == string.gsub(t, '', '@') and b == 5) 152assert('@'..a == string.gsub(t, '', '@') and b == 5)
153a, b = string.gsub('abçd', '(.)', '%0@', 2) 153a, b = string.gsub('abçd', '(.)', '%0@', 2)
@@ -184,6 +184,7 @@ do
184 local function setglobal (n,v) rawset(_G, n, v) end 184 local function setglobal (n,v) rawset(_G, n, v) end
185 string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) 185 string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
186 assert(_G.a=="roberto" and _G.roberto=="a") 186 assert(_G.a=="roberto" and _G.roberto=="a")
187 _G.a = nil; _G.roberto = nil
187end 188end
188 189
189function f(a,b) return string.gsub(a,'.',b) end 190function f(a,b) return string.gsub(a,'.',b) end
@@ -195,20 +196,21 @@ assert(string.gsub("alo $a='x'$ novamente $return a$",
195 "$([^$]*)%$", 196 "$([^$]*)%$",
196 dostring) == "alo novamente x") 197 dostring) == "alo novamente x")
197 198
198x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", 199local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
199 "$([^$]*)%$", dostring) 200 "$([^$]*)%$", dostring)
200assert(x == ' assim vai para ALO') 201assert(x == ' assim vai para ALO')
201 202_G.a, _G.x = nil
202t = {} 203
203s = 'a alo jose joao' 204local t = {}
204r = string.gsub(s, '()(%w+)()', function (a,w,b) 205local s = 'a alo jose joao'
205 assert(string.len(w) == b-a); 206local r = string.gsub(s, '()(%w+)()', function (a,w,b)
206 t[a] = b-a; 207 assert(string.len(w) == b-a);
207 end) 208 t[a] = b-a;
209 end)
208assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) 210assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
209 211
210 212
211function isbalanced (s) 213local function isbalanced (s)
212 return not string.find(string.gsub(s, "%b()", ""), "[()]") 214 return not string.find(string.gsub(s, "%b()", ""), "[()]")
213end 215end
214 216
@@ -251,7 +253,7 @@ if not _soft then
251end 253end
252 254
253-- recursive nest of gsubs 255-- recursive nest of gsubs
254function rev (s) 256local function rev (s)
255 return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) 257 return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
256end 258end
257 259
diff --git a/testes/sort.lua b/testes/sort.lua
index ef405d92..52919b8c 100644
--- a/testes/sort.lua
+++ b/testes/sort.lua
@@ -20,7 +20,7 @@ end
20checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) 20checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
21 21
22local x,y,z,a,n 22local x,y,z,a,n
23a = {}; lim = _soft and 200 or 2000 23a = {}; local lim = _soft and 200 or 2000
24for i=1, lim do a[i]=i end 24for i=1, lim do a[i]=i end
25assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) 25assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
26x = unpack(a) 26x = unpack(a)
@@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
222table.sort(a) 222table.sort(a)
223check(a) 223check(a)
224 224
225function perm (s, n) 225local function perm (s, n)
226 n = n or #s 226 n = n or #s
227 if n == 1 then 227 if n == 1 then
228 local t = {unpack(s)} 228 local t = {unpack(s)}
@@ -248,7 +248,7 @@ perm{1,2,3,3,5}
248perm{1,2,3,4,5,6} 248perm{1,2,3,4,5,6}
249perm{2,2,3,3,5,6} 249perm{2,2,3,3,5,6}
250 250
251function timesort (a, n, func, msg, pre) 251local function timesort (a, n, func, msg, pre)
252 local x = os.clock() 252 local x = os.clock()
253 table.sort(a, func) 253 table.sort(a, func)
254 x = (os.clock() - x) * 1000 254 x = (os.clock() - x) * 1000
@@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre)
257 check(a, func) 257 check(a, func)
258end 258end
259 259
260limit = 50000 260local limit = 50000
261if _soft then limit = 5000 end 261if _soft then limit = 5000 end
262 262
263a = {} 263a = {}
@@ -274,7 +274,7 @@ for i=1,limit do
274 a[i] = math.random() 274 a[i] = math.random()
275end 275end
276 276
277x = os.clock(); i=0 277local x = os.clock(); local i = 0
278table.sort(a, function(x,y) i=i+1; return y<x end) 278table.sort(a, function(x,y) i=i+1; return y<x end)
279x = (os.clock() - x) * 1000 279x = (os.clock() - x) * 1000
280print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", 280print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
@@ -289,18 +289,19 @@ timesort(a, limit, function(x,y) return nil end, "equal")
289 289
290for i,v in pairs(a) do assert(v == false) end 290for i,v in pairs(a) do assert(v == false) end
291 291
292A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} 292AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
293table.sort(A) 293table.sort(AA)
294check(A) 294check(AA)
295 295
296table.sort(A, function (x, y) 296table.sort(AA, function (x, y)
297 load(string.format("A[%q] = ''", x), "")() 297 load(string.format("AA[%q] = ''", x), "")()
298 collectgarbage() 298 collectgarbage()
299 return x<y 299 return x<y
300 end) 300 end)
301 301
302_G.AA = nil
302 303
303tt = {__lt = function (a,b) return a.val < b.val end} 304local tt = {__lt = function (a,b) return a.val < b.val end}
304a = {} 305a = {}
305for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end 306for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
306table.sort(a) 307table.sort(a)
diff --git a/testes/strings.lua b/testes/strings.lua
index 337c2937..b033c6ab 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -52,7 +52,7 @@ assert(("\000123456789"):sub(8) == "789")
52 52
53-- testing string.find 53-- testing string.find
54assert(string.find("123456789", "345") == 3) 54assert(string.find("123456789", "345") == 3)
55a,b = string.find("123456789", "345") 55local a,b = string.find("123456789", "345")
56assert(string.sub("123456789", a, b) == "345") 56assert(string.sub("123456789", a, b) == "345")
57assert(string.find("1234567890123456789", "345", 3) == 3) 57assert(string.find("1234567890123456789", "345", 3) == 3)
58assert(string.find("1234567890123456789", "345", 4) == 13) 58assert(string.find("1234567890123456789", "345", 4) == 13)
@@ -192,7 +192,7 @@ do -- tests for '%p' format
192 end 192 end
193end 193end
194 194
195x = '"ílo"\n\\' 195local x = '"ílo"\n\\'
196assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') 196assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
197assert(string.format('%q', "\0") == [["\0"]]) 197assert(string.format('%q', "\0") == [["\0"]])
198assert(load(string.format('return %q', x))() == x) 198assert(load(string.format('return %q', x))() == x)
@@ -452,7 +452,7 @@ end
452do 452do
453 local f = string.gmatch("1 2 3 4 5", "%d+") 453 local f = string.gmatch("1 2 3 4 5", "%d+")
454 assert(f() == "1") 454 assert(f() == "1")
455 co = coroutine.wrap(f) 455 local co = coroutine.wrap(f)
456 assert(co() == "2") 456 assert(co() == "2")
457end 457end
458 458
diff --git a/testes/tpack.lua b/testes/tpack.lua
index 2b9953f8..bfa63fc4 100644
--- a/testes/tpack.lua
+++ b/testes/tpack.lua
@@ -35,7 +35,7 @@ print("\talignment: " .. align)
35 35
36 36
37-- check errors in arguments 37-- check errors in arguments
38function checkerror (msg, f, ...) 38local function checkerror (msg, f, ...)
39 local status, err = pcall(f, ...) 39 local status, err = pcall(f, ...)
40 -- print(status, err, msg) 40 -- print(status, err, msg)
41 assert(not status and string.find(err, msg)) 41 assert(not status and string.find(err, msg))
diff --git a/testes/utf8.lua b/testes/utf8.lua
index 7472cfd0..c5a9dd3f 100644
--- a/testes/utf8.lua
+++ b/testes/utf8.lua
@@ -230,7 +230,7 @@ do
230 check(s, {0x10000, 0x1FFFFF}, true) 230 check(s, {0x10000, 0x1FFFFF}, true)
231end 231end
232 232
233x = "日本語a-4\0éó" 233local x = "日本語a-4\0éó"
234check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) 234check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
235 235
236 236
diff --git a/testes/vararg.lua b/testes/vararg.lua
index 44848d25..1b025102 100644
--- a/testes/vararg.lua
+++ b/testes/vararg.lua
@@ -3,13 +3,13 @@
3 3
4print('testing vararg') 4print('testing vararg')
5 5
6function f(a, ...) 6local function f (a, ...)
7 local x = {n = select('#', ...), ...} 7 local x = {n = select('#', ...), ...}
8 for i = 1, x.n do assert(a[i] == x[i]) end 8 for i = 1, x.n do assert(a[i] == x[i]) end
9 return x.n 9 return x.n
10end 10end
11 11
12function c12 (...) 12local function c12 (...)
13 assert(arg == _G.arg) -- no local 'arg' 13 assert(arg == _G.arg) -- no local 'arg'
14 local x = {...}; x.n = #x 14 local x = {...}; x.n = #x
15 local res = (x.n==2 and x[1] == 1 and x[2] == 2) 15 local res = (x.n==2 and x[1] == 1 and x[2] == 2)
@@ -17,7 +17,7 @@ function c12 (...)
17 return res, 2 17 return res, 2
18end 18end
19 19
20function vararg (...) return {n = select('#', ...), ...} end 20local function vararg (...) return {n = select('#', ...), ...} end
21 21
22local call = function (f, args) return f(table.unpack(args, 1, args.n)) end 22local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
23 23
@@ -29,7 +29,7 @@ assert(vararg().n == 0)
29assert(vararg(nil, nil).n == 2) 29assert(vararg(nil, nil).n == 2)
30 30
31assert(c12(1,2)==55) 31assert(c12(1,2)==55)
32a,b = assert(call(c12, {1,2})) 32local a,b = assert(call(c12, {1,2}))
33assert(a == 55 and b == 2) 33assert(a == 55 and b == 2)
34a = call(c12, {1,2;n=2}) 34a = call(c12, {1,2;n=2})
35assert(a == 55 and b == 2) 35assert(a == 55 and b == 2)
@@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end
49assert(t:f(1,4) == 3 and t:f(2) == 11) 49assert(t:f(1,4) == 3 and t:f(2) == 11)
50print('+') 50print('+')
51 51
52lim = 20 52local lim = 20
53local i, a = 1, {} 53local i, a = 1, {}
54while i <= lim do a[i] = i+0.3; i=i+1 end 54while i <= lim do a[i] = i+0.3; i=i+1 end
55 55
@@ -59,7 +59,7 @@ function f(a, b, c, d, ...)
59 more[lim-4] == lim+0.3 and not more[lim-3]) 59 more[lim-4] == lim+0.3 and not more[lim-3])
60end 60end
61 61
62function g(a,b,c) 62local function g (a,b,c)
63 assert(a == 1.3 and b == 2.3 and c == 3.3) 63 assert(a == 1.3 and b == 2.3 and c == 3.3)
64end 64end
65 65
@@ -76,7 +76,7 @@ print("+")
76 76
77-- new-style varargs 77-- new-style varargs
78 78
79function oneless (a, ...) return ... end 79local function oneless (a, ...) return ... end
80 80
81function f (n, a, ...) 81function f (n, a, ...)
82 local b 82 local b
@@ -99,8 +99,8 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil)
99 99
100 100
101-- varargs for main chunks 101-- varargs for main chunks
102f = load[[ return {...} ]] 102local f = load[[ return {...} ]]
103x = f(2,3) 103local x = f(2,3)
104assert(x[1] == 2 and x[2] == 3 and x[3] == undef) 104assert(x[1] == 2 and x[2] == 3 and x[3] == undef)
105 105
106 106
diff --git a/testes/verybig.lua b/testes/verybig.lua
index 8fb7b13e..250ea795 100644
--- a/testes/verybig.lua
+++ b/testes/verybig.lua
@@ -52,7 +52,7 @@ if _soft then return 10 end
52print "testing large programs (>64k)" 52print "testing large programs (>64k)"
53 53
54-- template to create a very big test file 54-- template to create a very big test file
55prog = [[$ 55local prog = [[$
56 56
57local a,b 57local a,b
58 58
@@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end
85assert(b:xxx(10, 12) == 22) -- pushself with non-constant index 85assert(b:xxx(10, 12) == 22) -- pushself with non-constant index
86b["xxx"] = undef 86b["xxx"] = undef
87 87
88s = 0; n=0 88local s = 0; local n=0
89for a,b in pairs(b) do s=s+b; n=n+1 end 89for a,b in pairs(b) do s=s+b; n=n+1 end
90-- with 32-bit floats, exact value of 's' depends on summation order 90-- with 32-bit floats, exact value of 's' depends on summation order
91assert(81800000.0 < s and s < 81860000 and n == 70001) 91assert(81800000.0 < s and s < 81860000 and n == 70001)
@@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001)
93a = nil; b = nil 93a = nil; b = nil
94print'+' 94print'+'
95 95
96function f(x) b=x end 96local function f(x) b=x end
97 97
98a = f{$3$} or 10 98a = f{$3$} or 10
99 99
@@ -118,7 +118,7 @@ local function sig (x)
118 return (x % 2 == 0) and '' or '-' 118 return (x % 2 == 0) and '' or '-'
119end 119end
120 120
121F = { 121local F = {
122function () -- $1$ 122function () -- $1$
123 for i=10,50009 do 123 for i=10,50009 do
124 io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') 124 io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n')
@@ -138,14 +138,14 @@ function () -- $3$
138end, 138end,
139} 139}
140 140
141file = os.tmpname() 141local file = os.tmpname()
142io.output(file) 142io.output(file)
143for s in string.gmatch(prog, "$([^$]+)") do 143for s in string.gmatch(prog, "$([^$]+)") do
144 local n = tonumber(s) 144 local n = tonumber(s)
145 if not n then io.write(s) else F[n]() end 145 if not n then io.write(s) else F[n]() end
146end 146end
147io.close() 147io.close()
148result = dofile(file) 148local result = dofile(file)
149assert(os.remove(file)) 149assert(os.remove(file))
150print'OK' 150print'OK'
151return result 151return result