diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
commit | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch) | |
tree | 594b7e873f2c29113d95c75147ab10865cdd772c | |
parent | 0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff) | |
download | lua-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.lua | 1 | ||||
-rw-r--r-- | testes/api.lua | 112 | ||||
-rw-r--r-- | testes/attrib.lua | 24 | ||||
-rw-r--r-- | testes/big.lua | 2 | ||||
-rw-r--r-- | testes/calls.lua | 44 | ||||
-rw-r--r-- | testes/closure.lua | 6 | ||||
-rw-r--r-- | testes/code.lua | 6 | ||||
-rw-r--r-- | testes/constructs.lua | 36 | ||||
-rw-r--r-- | testes/coroutine.lua | 56 | ||||
-rw-r--r-- | testes/db.lua | 27 | ||||
-rw-r--r-- | testes/errors.lua | 83 | ||||
-rw-r--r-- | testes/events.lua | 3 | ||||
-rw-r--r-- | testes/files.lua | 6 | ||||
-rw-r--r-- | testes/gc.lua | 42 | ||||
-rw-r--r-- | testes/literals.lua | 33 | ||||
-rw-r--r-- | testes/locals.lua | 8 | ||||
-rw-r--r-- | testes/main.lua | 4 | ||||
-rw-r--r-- | testes/math.lua | 6 | ||||
-rw-r--r-- | testes/nextvar.lua | 2 | ||||
-rw-r--r-- | testes/pm.lua | 32 | ||||
-rw-r--r-- | testes/sort.lua | 23 | ||||
-rw-r--r-- | testes/strings.lua | 6 | ||||
-rw-r--r-- | testes/tpack.lua | 2 | ||||
-rw-r--r-- | testes/utf8.lua | 2 | ||||
-rw-r--r-- | testes/vararg.lua | 18 | ||||
-rw-r--r-- | testes/verybig.lua | 12 |
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 | ||
164 | dofile('db.lua') | 164 | dofile('db.lua') |
165 | assert(dofile('calls.lua') == deep and deep) | 165 | assert(dofile('calls.lua') == deep and deep) |
166 | _G.deep = nil | ||
166 | olddofile('strings.lua') | 167 | olddofile('strings.lua') |
167 | olddofile('literals.lua') | 168 | olddofile('literals.lua') |
168 | dofile('tpack.lua') | 169 | dofile('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 |
15 | local MEMERRMSG = "not enough memory" | 15 | local MEMERRMSG = "not enough memory" |
16 | 16 | ||
17 | function tcheck (t1, t2) | 17 | local function tcheck (t1, t2) |
18 | assert(t1.n == (t2.n or #t2) + 1) | 18 | assert(t1.n == (t2.n or #t2) + 1) |
19 | for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end | 19 | for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end |
20 | end | 20 | end |
@@ -28,7 +28,7 @@ end | |||
28 | 28 | ||
29 | print('testing C API') | 29 | print('testing C API') |
30 | 30 | ||
31 | a = T.testC("pushvalue R; return 1") | 31 | local a = T.testC("pushvalue R; return 1") |
32 | assert(a == debug.getregistry()) | 32 | assert(a == debug.getregistry()) |
33 | 33 | ||
34 | 34 | ||
@@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0) | |||
43 | assert(a == string.pack("d", 12458954321123.0)) | 43 | assert(a == string.pack("d", 12458954321123.0)) |
44 | assert(T.s2d(a) == 12458954321123.0) | 44 | assert(T.s2d(a) == 12458954321123.0) |
45 | 45 | ||
46 | a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") | 46 | local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") |
47 | assert(a == 2 and b == 3 and not c) | 47 | assert(a == 2 and b == 3 and not c) |
48 | 48 | ||
49 | f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") | 49 | local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") |
50 | a,b,c = f() | 50 | a,b,c = f() |
51 | assert(a == 2 and b == 3 and not c) | 51 | assert(a == 2 and b == 3 and not c) |
52 | 52 | ||
@@ -61,7 +61,7 @@ assert(a==false and b==true and c==false) | |||
61 | a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) | 61 | a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) |
62 | assert(a == 40 and b == 5 and not c) | 62 | assert(a == 40 and b == 5 and not c) |
63 | 63 | ||
64 | t = pack(T.testC("settop 5; return *", 2, 3)) | 64 | local t = pack(T.testC("settop 5; return *", 2, 3)) |
65 | tcheck(t, {n=4,2,3}) | 65 | tcheck(t, {n=4,2,3}) |
66 | 66 | ||
67 | t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) | 67 | t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) |
@@ -166,16 +166,17 @@ end | |||
166 | 166 | ||
167 | 167 | ||
168 | -- testing globals | 168 | -- testing globals |
169 | _G.a = 14; _G.b = "a31" | 169 | _G.AA = 14; _G.BB = "a31" |
170 | local a = {T.testC[[ | 170 | local a = {T.testC[[ |
171 | getglobal a; | 171 | getglobal AA; |
172 | getglobal b; | 172 | getglobal BB; |
173 | getglobal b; | 173 | getglobal BB; |
174 | setglobal a; | 174 | setglobal AA; |
175 | return * | 175 | return * |
176 | ]]} | 176 | ]]} |
177 | assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") | 177 | assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31") |
178 | 178 | ||
179 | _G.AA, _G.BB = nil | ||
179 | 180 | ||
180 | -- testing arith | 181 | -- testing arith |
181 | assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) | 182 | assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) |
@@ -199,13 +200,14 @@ a,b,c = T.testC([[pushnum 1; | |||
199 | pushstring 10; arith _; | 200 | pushstring 10; arith _; |
200 | pushstring 5; return 3]]) | 201 | pushstring 5; return 3]]) |
201 | assert(a == 1 and b == -10 and c == "5") | 202 | assert(a == 1 and b == -10 and c == "5") |
202 | mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, | 203 | local mt = { |
204 | __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, | ||
203 | __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, | 205 | __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, |
204 | __unm = function (a) return setmetatable({a[1]* 2}, mt) end} | 206 | __unm = function (a) return setmetatable({a[1]* 2}, mt) end} |
205 | a,b,c = setmetatable({4}, mt), | 207 | a,b,c = setmetatable({4}, mt), |
206 | setmetatable({8}, mt), | 208 | setmetatable({8}, mt), |
207 | setmetatable({-3}, mt) | 209 | setmetatable({-3}, mt) |
208 | x,y,z = T.testC("arith +; return 2", 10, a, b) | 210 | local x,y,z = T.testC("arith +; return 2", 10, a, b) |
209 | assert(x == 10 and y[1] == 12 and z == nil) | 211 | assert(x == 10 and y[1] == 12 and z == nil) |
210 | assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) | 212 | assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) |
211 | assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == | 213 | assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == |
@@ -312,9 +314,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") | |||
312 | 314 | ||
313 | -- testing lua_is | 315 | -- testing lua_is |
314 | 316 | ||
315 | function B(x) return x and 1 or 0 end | 317 | local function B (x) return x and 1 or 0 end |
316 | 318 | ||
317 | function count (x, n) | 319 | local function count (x, n) |
318 | n = n or 2 | 320 | n = n or 2 |
319 | local prog = [[ | 321 | local prog = [[ |
320 | isnumber %d; | 322 | isnumber %d; |
@@ -345,7 +347,7 @@ assert(count(nil, 15) == 100) | |||
345 | 347 | ||
346 | -- testing lua_to... | 348 | -- testing lua_to... |
347 | 349 | ||
348 | function to (s, x, n) | 350 | local function to (s, x, n) |
349 | n = n or 2 | 351 | n = n or 2 |
350 | return T.testC(string.format("%s %d; return 1", s, n), x) | 352 | return T.testC(string.format("%s %d; return 1", s, n), x) |
351 | end | 353 | end |
@@ -486,11 +488,12 @@ a = T.testC([[ | |||
486 | pushvalue 3; insert -2; pcall 1 1 0; | 488 | pushvalue 3; insert -2; pcall 1 1 0; |
487 | pcall 0 0 0; | 489 | pcall 0 0 0; |
488 | return 1 | 490 | return 1 |
489 | ]], "x=150", function (a) assert(a==nil); return 3 end) | 491 | ]], "XX=150", function (a) assert(a==nil); return 3 end) |
490 | 492 | ||
491 | assert(type(a) == 'string' and x == 150) | 493 | assert(type(a) == 'string' and XX == 150) |
494 | _G.XX = nil | ||
492 | 495 | ||
493 | function check3(p, ...) | 496 | local function check3(p, ...) |
494 | local arg = {...} | 497 | local arg = {...} |
495 | assert(#arg == 3) | 498 | assert(#arg == 3) |
496 | assert(string.find(arg[3], p)) | 499 | assert(string.find(arg[3], p)) |
@@ -500,7 +503,7 @@ check3("%.", T.testC("loadfile 2; return *", ".")) | |||
500 | check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) | 503 | check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) |
501 | 504 | ||
502 | -- test errors in non protected threads | 505 | -- test errors in non protected threads |
503 | function checkerrnopro (code, msg) | 506 | local function checkerrnopro (code, msg) |
504 | local th = coroutine.create(function () end) -- create new thread | 507 | local th = coroutine.create(function () end) -- create new thread |
505 | local stt, err = pcall(T.testC, th, code) -- run code there | 508 | local stt, err = pcall(T.testC, th, code) -- run code there |
506 | assert(not stt and string.find(err, msg)) | 509 | assert(not stt and string.find(err, msg)) |
@@ -510,8 +513,9 @@ if not _soft then | |||
510 | collectgarbage("stop") -- avoid __gc with full stack | 513 | collectgarbage("stop") -- avoid __gc with full stack |
511 | checkerrnopro("pushnum 3; call 0 0", "attempt to call") | 514 | checkerrnopro("pushnum 3; call 0 0", "attempt to call") |
512 | print"testing stack overflow in unprotected thread" | 515 | print"testing stack overflow in unprotected thread" |
513 | function f () f() end | 516 | function F () F() end |
514 | checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") | 517 | checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow") |
518 | F = nil | ||
515 | collectgarbage("restart") | 519 | collectgarbage("restart") |
516 | end | 520 | end |
517 | print"+" | 521 | print"+" |
@@ -588,7 +592,7 @@ assert(a[a] == "x") | |||
588 | 592 | ||
589 | b = setmetatable({p = a}, {}) | 593 | b = setmetatable({p = a}, {}) |
590 | getmetatable(b).__index = function (t, i) return t.p[i] end | 594 | getmetatable(b).__index = function (t, i) return t.p[i] end |
591 | k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") | 595 | local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") |
592 | assert(x == 15 and k == 35) | 596 | assert(x == 15 and k == 35) |
593 | k = T.testC("getfield 2 y, return 1", b) | 597 | k = T.testC("getfield 2 y, return 1", b) |
594 | assert(k == 12) | 598 | assert(k == 12) |
@@ -748,8 +752,8 @@ local i = T.ref{} | |||
748 | T.unref(i) | 752 | T.unref(i) |
749 | assert(T.ref{} == i) | 753 | assert(T.ref{} == i) |
750 | 754 | ||
751 | Arr = {} | 755 | local Arr = {} |
752 | Lim = 100 | 756 | local Lim = 100 |
753 | for i=1,Lim do -- lock many objects | 757 | for i=1,Lim do -- lock many objects |
754 | Arr[i] = T.ref({}) | 758 | Arr[i] = T.ref({}) |
755 | end | 759 | end |
@@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them | |||
761 | T.unref(Arr[i]) | 765 | T.unref(Arr[i]) |
762 | end | 766 | end |
763 | 767 | ||
764 | function printlocks () | 768 | local function printlocks () |
765 | local f = T.makeCfunc("gettable R; return 1") | 769 | local f = T.makeCfunc("gettable R; return 1") |
766 | local n = f("n") | 770 | local n = f("n") |
767 | print("n", n) | 771 | print("n", n) |
@@ -793,8 +797,8 @@ assert(type(T.getref(a)) == 'table') | |||
793 | 797 | ||
794 | 798 | ||
795 | -- colect in cl the `val' of all collected userdata | 799 | -- colect in cl the `val' of all collected userdata |
796 | tt = {} | 800 | local tt = {} |
797 | cl = {n=0} | 801 | local cl = {n=0} |
798 | A = nil; B = nil | 802 | A = nil; B = nil |
799 | local F | 803 | local F |
800 | F = function (x) | 804 | F = function (x) |
@@ -817,6 +821,7 @@ F = function (x) | |||
817 | end | 821 | end |
818 | tt.__gc = F | 822 | tt.__gc = F |
819 | 823 | ||
824 | |||
820 | -- test whether udate collection frees memory in the right time | 825 | -- test whether udate collection frees memory in the right time |
821 | do | 826 | do |
822 | collectgarbage(); | 827 | collectgarbage(); |
@@ -853,9 +858,9 @@ end | |||
853 | collectgarbage("stop") | 858 | collectgarbage("stop") |
854 | 859 | ||
855 | -- create 3 userdatas with tag `tt' | 860 | -- create 3 userdatas with tag `tt' |
856 | a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) | 861 | a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a) |
857 | b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) | 862 | b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b) |
858 | c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) | 863 | c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c) |
859 | 864 | ||
860 | -- create userdata without meta table | 865 | -- create userdata without meta table |
861 | x = T.newuserdata(4) | 866 | x = T.newuserdata(4) |
@@ -866,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x) | |||
866 | 871 | ||
867 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) | 872 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) |
868 | 873 | ||
869 | d=T.ref(a); | 874 | local d = T.ref(a); |
870 | e=T.ref(b); | 875 | local e = T.ref(b); |
871 | f=T.ref(c); | 876 | local f = T.ref(c); |
872 | t = {T.getref(d), T.getref(e), T.getref(f)} | 877 | t = {T.getref(d), T.getref(e), T.getref(f)} |
873 | assert(t[1] == a and t[2] == b and t[3] == c) | 878 | assert(t[1] == a and t[2] == b and t[3] == c) |
874 | 879 | ||
@@ -888,7 +893,7 @@ tt=nil -- frees tt for GC | |||
888 | A = nil | 893 | A = nil |
889 | b = nil | 894 | b = nil |
890 | T.unref(d); | 895 | T.unref(d); |
891 | n5 = T.newuserdata(0) | 896 | local n5 = T.newuserdata(0) |
892 | debug.setmetatable(n5, {__gc=F}) | 897 | debug.setmetatable(n5, {__gc=F}) |
893 | n5 = T.udataval(n5) | 898 | n5 = T.udataval(n5) |
894 | collectgarbage() | 899 | collectgarbage() |
@@ -959,11 +964,11 @@ print'+' | |||
959 | 964 | ||
960 | 965 | ||
961 | -- testing changing hooks during hooks | 966 | -- testing changing hooks during hooks |
962 | _G.t = {} | 967 | _G.TT = {} |
963 | T.sethook([[ | 968 | T.sethook([[ |
964 | # set a line hook after 3 count hooks | 969 | # set a line hook after 3 count hooks |
965 | sethook 4 0 ' | 970 | sethook 4 0 ' |
966 | getglobal t; | 971 | getglobal TT; |
967 | pushvalue -3; append -2 | 972 | pushvalue -3; append -2 |
968 | pushvalue -2; append -2 | 973 | pushvalue -2; append -2 |
969 | ']], "c", 3) | 974 | ']], "c", 3) |
@@ -973,12 +978,13 @@ a = 1 -- count hook (set line hook) | |||
973 | a = 1 -- line hook | 978 | a = 1 -- line hook |
974 | a = 1 -- line hook | 979 | a = 1 -- line hook |
975 | debug.sethook() | 980 | debug.sethook() |
976 | t = _G.t | 981 | local t = _G.TT |
977 | assert(t[1] == "line") | 982 | assert(t[1] == "line") |
978 | line = t[2] | 983 | local line = t[2] |
979 | assert(t[3] == "line" and t[4] == line + 1) | 984 | assert(t[3] == "line" and t[4] == line + 1) |
980 | assert(t[5] == "line" and t[6] == line + 2) | 985 | assert(t[5] == "line" and t[6] == line + 2) |
981 | assert(t[7] == nil) | 986 | assert(t[7] == nil) |
987 | _G.TT = nil | ||
982 | 988 | ||
983 | 989 | ||
984 | ------------------------------------------------------------------------- | 990 | ------------------------------------------------------------------------- |
@@ -1003,6 +1009,7 @@ do -- testing errors during GC | |||
1003 | collectgarbage("restart") | 1009 | collectgarbage("restart") |
1004 | warn("@on") | 1010 | warn("@on") |
1005 | end | 1011 | end |
1012 | _G.A = nil | ||
1006 | ------------------------------------------------------------------------- | 1013 | ------------------------------------------------------------------------- |
1007 | -- test for userdata vals | 1014 | -- test for userdata vals |
1008 | do | 1015 | do |
@@ -1032,8 +1039,8 @@ assert(a == 'alo' and b == '3') | |||
1032 | 1039 | ||
1033 | T.doremote(L1, "_ERRORMESSAGE = nil") | 1040 | T.doremote(L1, "_ERRORMESSAGE = nil") |
1034 | -- error: `sin' is not defined | 1041 | -- error: `sin' is not defined |
1035 | a, _, b = T.doremote(L1, "return sin(1)") | 1042 | a, b, c = T.doremote(L1, "return sin(1)") |
1036 | assert(a == nil and b == 2) -- 2 == run-time error | 1043 | assert(a == nil and c == 2) -- 2 == run-time error |
1037 | 1044 | ||
1038 | -- error: syntax error | 1045 | -- error: syntax error |
1039 | a, b, c = T.doremote(L1, "return a+") | 1046 | a, b, c = T.doremote(L1, "return a+") |
@@ -1204,7 +1211,7 @@ T.alloccount() -- remove limit | |||
1204 | -- o that we get memory errors in all allocations of a given | 1211 | -- o that we get memory errors in all allocations of a given |
1205 | -- task, until there is enough memory to complete the task without | 1212 | -- task, until there is enough memory to complete the task without |
1206 | -- errors. | 1213 | -- errors. |
1207 | function testbytes (s, f) | 1214 | local function testbytes (s, f) |
1208 | collectgarbage() | 1215 | collectgarbage() |
1209 | local M = T.totalmem() | 1216 | local M = T.totalmem() |
1210 | local oldM = M | 1217 | local oldM = M |
@@ -1229,7 +1236,7 @@ end | |||
1229 | -- task, until there is enough allocations to complete the task without | 1236 | -- task, until there is enough allocations to complete the task without |
1230 | -- errors. | 1237 | -- errors. |
1231 | 1238 | ||
1232 | function testalloc (s, f) | 1239 | local function testalloc (s, f) |
1233 | collectgarbage() | 1240 | collectgarbage() |
1234 | local M = 0 | 1241 | local M = 0 |
1235 | local a,b = nil | 1242 | local a,b = nil |
@@ -1296,12 +1303,12 @@ end) | |||
1296 | -- testing threads | 1303 | -- testing threads |
1297 | 1304 | ||
1298 | -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) | 1305 | -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) |
1299 | mt = T.testC("rawgeti R 1; return 1") | 1306 | local mt = T.testC("rawgeti R 1; return 1") |
1300 | assert(type(mt) == "thread" and coroutine.running() == mt) | 1307 | assert(type(mt) == "thread" and coroutine.running() == mt) |
1301 | 1308 | ||
1302 | 1309 | ||
1303 | 1310 | ||
1304 | function expand (n,s) | 1311 | local function expand (n,s) |
1305 | if n==0 then return "" end | 1312 | if n==0 then return "" end |
1306 | local e = string.rep("=", n) | 1313 | local e = string.rep("=", n) |
1307 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", | 1314 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", |
@@ -1311,9 +1318,10 @@ end | |||
1311 | G=0; collectgarbage(); a =collectgarbage("count") | 1318 | G=0; collectgarbage(); a =collectgarbage("count") |
1312 | load(expand(20,"G=G+1"))() | 1319 | load(expand(20,"G=G+1"))() |
1313 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) | 1320 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) |
1321 | G = nil | ||
1314 | 1322 | ||
1315 | testamem("running code on new thread", function () | 1323 | testamem("running code on new thread", function () |
1316 | return T.doonnewstack("x=1") == 0 -- try to create thread | 1324 | return T.doonnewstack("local x=1") == 0 -- try to create thread |
1317 | end) | 1325 | end) |
1318 | 1326 | ||
1319 | 1327 | ||
@@ -1327,13 +1335,13 @@ end) | |||
1327 | local testprog = [[ | 1335 | local testprog = [[ |
1328 | local function foo () return end | 1336 | local function foo () return end |
1329 | local t = {"x"} | 1337 | local t = {"x"} |
1330 | a = "aaa" | 1338 | AA = "aaa" |
1331 | for i = 1, #t do a=a..t[i] end | 1339 | for i = 1, #t do AA = AA .. t[i] end |
1332 | return true | 1340 | return true |
1333 | ]] | 1341 | ]] |
1334 | 1342 | ||
1335 | -- testing memory x dofile | 1343 | -- testing memory x dofile |
1336 | _G.a = nil | 1344 | _G.AA = nil |
1337 | local t =os.tmpname() | 1345 | local t =os.tmpname() |
1338 | local f = assert(io.open(t, "w")) | 1346 | local f = assert(io.open(t, "w")) |
1339 | f:write(testprog) | 1347 | f:write(testprog) |
@@ -1343,7 +1351,7 @@ testamem("dofile", function () | |||
1343 | return a and a() | 1351 | return a and a() |
1344 | end) | 1352 | end) |
1345 | assert(os.remove(t)) | 1353 | assert(os.remove(t)) |
1346 | assert(_G.a == "aaax") | 1354 | assert(_G.AA == "aaax") |
1347 | 1355 | ||
1348 | 1356 | ||
1349 | -- other generic tests | 1357 | -- other generic tests |
@@ -1360,6 +1368,8 @@ testamem("dump/undump", function () | |||
1360 | return a and a() | 1368 | return a and a() |
1361 | end) | 1369 | end) |
1362 | 1370 | ||
1371 | _G.AA = nil | ||
1372 | |||
1363 | local t = os.tmpname() | 1373 | local t = os.tmpname() |
1364 | testamem("file creation", function () | 1374 | testamem("file creation", function () |
1365 | local f = assert(io.open(t, 'w')) | 1375 | local f = assert(io.open(t, 'w')) |
@@ -1381,7 +1391,7 @@ testamem("constructors", function () | |||
1381 | end) | 1391 | end) |
1382 | 1392 | ||
1383 | local a = 1 | 1393 | local a = 1 |
1384 | close = nil | 1394 | local close = nil |
1385 | testamem("closure creation", function () | 1395 | testamem("closure creation", function () |
1386 | function close (b) | 1396 | function close (b) |
1387 | return function (x) return b + x end | 1397 | return function (x) return b + x end |
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 |
87 | local function D (x) | 87 | local function D (x) |
88 | x = string.gsub(x, "/", dirsep) | 88 | local x = string.gsub(x, "/", dirsep) |
89 | return DIR .. x | 89 | return DIR .. x |
90 | end | 90 | end |
91 | 91 | ||
@@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras) | |||
106 | end | 106 | end |
107 | end | 107 | end |
108 | 108 | ||
109 | function removefiles (files) | 109 | local 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) |
155 | end | 155 | end |
156 | 156 | ||
157 | a = require"names" | 157 | local a = require"names" |
158 | assert(a[1] == "names" and a[2] == D"names.lua") | 158 | assert(a[1] == "names" and a[2] == D"names.lua") |
159 | 159 | ||
160 | _G.a = nil | ||
161 | local st, msg = pcall(require, "err") | 160 | local st, msg = pcall(require, "err") |
162 | assert(not st and string.find(msg, "arithmetic") and B == 15) | 161 | assert(not st and string.find(msg, "arithmetic") and B == 15) |
163 | st, msg = pcall(require, "synerr") | 162 | st, msg = pcall(require, "synerr") |
@@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX") | |||
191 | 190 | ||
192 | 191 | ||
193 | removefiles(files) | 192 | removefiles(files) |
193 | NAME, 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 | ||
225 | removefiles(files) | 225 | removefiles(files) |
226 | 226 | AA = nil | |
227 | 227 | ||
228 | package.path = "" | 228 | package.path = "" |
229 | assert(not pcall(require, "file_does_not_exist")) | 229 | assert(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 | ||
308 | end | 309 | end |
309 | 310 | ||
310 | _ENV = _G | 311 | _ENV = _G |
@@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors") | |||
338 | 339 | ||
339 | local res, res2 = 27 | 340 | local res, res2 = 27 |
340 | 341 | ||
341 | a, b = 1, 2+3 | 342 | local a, b = 1, 2+3 |
342 | assert(a==1 and b==5) | 343 | assert(a==1 and b==5) |
343 | a={} | 344 | a={} |
344 | function f() return 10, 11, 12 end | 345 | local function f() return 10, 11, 12 end |
345 | a.x, b, a[1] = 1, 2, f() | 346 | a.x, b, a[1] = 1, 2, f() |
346 | assert(a.x==1 and b==2 and a[1]==10) | 347 | assert(a.x==1 and b==2 and a[1]==10) |
347 | a[f()], b, a[f()+3] = f(), a, 'x' | 348 | a[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) |
362 | end | 363 | end |
363 | 364 | ||
364 | a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 | 365 | local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 |
365 | assert(not a and b and c and d==6) | 366 | assert(not a and b and c and d==6) |
366 | 367 | ||
367 | d = 20 | 368 | d = 20 |
@@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end)) | |||
419 | assert(a[nil] == undef) | 420 | assert(a[nil] == undef) |
420 | a = nil | 421 | a = nil |
421 | 422 | ||
423 | local a, b, c | ||
422 | a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} | 424 | a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} |
423 | a, a.x, a.y = a, a[-3] | 425 | a, a.x, a.y = a, a[-3] |
424 | assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) | 426 | assert(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 |
456 | end | 458 | end |
457 | 459 | ||
458 | maxintF = maxint + 0.0 -- float version | 460 | local maxintF = maxint + 0.0 -- float version |
459 | 461 | ||
460 | assert(maxintF == maxint and math.type(maxintF) == "float" and | 462 | assert(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 | ||
34 | X = nil | 34 | X = nil |
35 | co = coroutine.wrap(f) | 35 | local co = coroutine.wrap(f) |
36 | assert(co() == 's') | 36 | assert(co() == 's') |
37 | assert(co() == 'g') | 37 | assert(co() == 'g') |
38 | assert(co() == 'g') | 38 | assert(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 | ||
18 | assert(type(assert) == type(print)) | 18 | assert(type(assert) == type(print)) |
19 | function f (x) return a:x (x) end | 19 | local function f (x) return a:x (x) end |
20 | assert(type(f) == 'function') | 20 | assert(type(f) == 'function') |
21 | assert(not pcall(type)) | 21 | assert(not pcall(type)) |
22 | 22 | ||
@@ -33,10 +33,11 @@ do | |||
33 | assert(fact(5) == 120) | 33 | assert(fact(5) == 120) |
34 | end | 34 | end |
35 | assert(fact == false) | 35 | assert(fact == false) |
36 | fact = nil | ||
36 | 37 | ||
37 | -- testing declarations | 38 | -- testing declarations |
38 | a = {i = 10} | 39 | local a = {i = 10} |
39 | self = 20 | 40 | local self = 20 |
40 | function a:x (x) return x+self.i end | 41 | function a:x (x) return x+self.i end |
41 | function a.y (x) return x+self end | 42 | function 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) |
73 | assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') | 74 | assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') |
74 | 75 | ||
76 | t = nil -- delete 't' | ||
77 | |||
75 | function fat(x) | 78 | function 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 | ||
81 | assert(load "load 'assert(fat(6)==720)' () ")() | 84 | assert(load "load 'assert(fat(6)==720)' () ")() |
82 | a = load('return fat(5), 3') | 85 | a = load('return fat(5), 3') |
83 | a,b = a() | 86 | local a,b = a() |
84 | assert(a == 120 and b == 3) | 87 | assert(a == 120 and b == 3) |
88 | fat = nil | ||
85 | print('+') | 89 | print('+') |
86 | 90 | ||
87 | function err_on_n (n) | 91 | local 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 |
91 | end | 95 | end |
92 | 96 | ||
93 | do | 97 | do |
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) | ||
100 | end | 106 | end |
101 | 107 | ||
102 | dummy(10) | 108 | _G.deep = nil -- "declaration" (used by 'all.lua') |
103 | 109 | ||
104 | function deep (n) | 110 | function 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 |
212 | Z = function (le) | 218 | local 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 | ||
222 | F = function (f) | 228 | local 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 | ||
229 | fat = Z(F) | 235 | local fat = Z(F) |
230 | 236 | ||
231 | assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) | 237 | assert(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) |
238 | end | 244 | end |
239 | 245 | ||
240 | f = g(10) | 246 | local f = g(10) |
241 | assert(f(9, 16) == 10+11+12+13+10+9+16+10) | 247 | assert(f(9, 16) == 10+11+12+13+10+9+16+10) |
242 | 248 | ||
243 | Z, F, f = nil | ||
244 | print('+') | 249 | print('+') |
245 | 250 | ||
246 | -- testing multiple returns | 251 | -- testing multiple returns |
247 | 252 | ||
248 | function unlpack (t, i) | 253 | local 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 |
253 | end | 258 | end |
254 | 259 | ||
255 | function equaltab (t1, t2) | 260 | local 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 | ||
262 | local pack = function (...) return (table.pack(...)) end | 267 | local pack = function (...) return (table.pack(...)) end |
263 | 268 | ||
264 | function f() return 1,2,30,4 end | 269 | local function f() return 1,2,30,4 end |
265 | function ret2 (a,b) return a,b end | 270 | local function ret2 (a,b) return a,b end |
266 | 271 | ||
267 | local a,b,c,d = unlpack{1,2,3} | 272 | local a,b,c,d = unlpack{1,2,3} |
268 | assert(a==1 and b==2 and c==3 and d==nil) | 273 | assert(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") | |||
291 | local x = "-- a comment\0\0\0\n x = 10 + \n23; \ | 296 | local 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'" |
294 | function read1 (x) | 299 | local 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 |
301 | end | 306 | end |
302 | 307 | ||
303 | function cannotload (msg, a,b) | 308 | local function cannotload (msg, a,b) |
304 | assert(not a and string.find(b, msg)) | 309 | assert(not a and string.find(b, msg)) |
305 | end | 310 | end |
306 | 311 | ||
@@ -342,6 +347,7 @@ a = assert(load(read1(x), nil, "b")) | |||
342 | assert(a() == 1 and _G.x == 1) | 347 | assert(a() == 1 and _G.x == 1) |
343 | cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) | 348 | cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) |
344 | cannotload("attempt to load a binary chunk", load(x, nil, "t")) | 349 | cannotload("attempt to load a binary chunk", load(x, nil, "t")) |
350 | _G.x = nil | ||
345 | 351 | ||
346 | assert(not pcall(string.dump, print)) -- no dump of C functions | 352 | assert(not pcall(string.dump, print)) -- no dump of C functions |
347 | 353 | ||
@@ -366,7 +372,7 @@ debug.setupvalue(x, 2, _G) | |||
366 | assert(x() == 123) | 372 | assert(x() == 123) |
367 | 373 | ||
368 | assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) | 374 | assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) |
369 | 375 | XX = nil | |
370 | 376 | ||
371 | -- test generic load with nested functions | 377 | -- test generic load with nested functions |
372 | x = [[ | 378 | x = [[ |
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 @@ | |||
4 | print "testing closures" | 4 | print "testing closures" |
5 | 5 | ||
6 | local A,B = 0,{g=10} | 6 | local A,B = 0,{g=10} |
7 | function f(x) | 7 | local 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 |
92 | local f | ||
92 | for i=1,3 do | 93 | for 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') | |||
139 | b('set', 10); assert(b('get') == 14) | 140 | b('set', 10); assert(b('get') == 14) |
140 | 141 | ||
141 | 142 | ||
142 | local w | 143 | local y, w |
143 | -- testing multi-level closure | 144 | -- testing multi-level closure |
144 | function f(x) | 145 | function 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 |
231 | local debug = require'debug' | 232 | local debug = require'debug' |
232 | 233 | ||
234 | local foo1, foo2, foo3 | ||
233 | do | 235 | do |
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 '...' |
89 | function check (f, ...) | 89 | local 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'. |
102 | function checkR (f, p, r, ...) | 102 | local 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 |
110 | function checkequal (a, b) | 110 | local 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) | |||
11 | end | 11 | end |
12 | 12 | ||
13 | -- testing semicollons | 13 | -- testing semicollons |
14 | local a | ||
14 | do ;;; end | 15 | do ;;; 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 | ||
50 | local a,b = 1,nil; | 51 | local a,b = 1,nil; |
51 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); | 52 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); |
52 | x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); | 53 | local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); |
53 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); | 54 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); |
54 | 55 | ||
55 | x,y=1,2; | 56 | local x, y = 1, 2; |
56 | assert((x>y) and x or y == 2); | 57 | assert((x>y) and x or y == 2); |
57 | x,y=2,1; | 58 | x,y=2,1; |
58 | assert((x>y) and x or y == 2); | 59 | assert((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 | ||
95 | end | 97 | end |
96 | 98 | ||
97 | 99 | ||
@@ -100,7 +102,7 @@ repeat until 1; repeat until true; | |||
100 | while false do end; while nil do end; | 102 | while false do end; while nil do end; |
101 | 103 | ||
102 | do -- test old bug (first name could not be an `upvalue') | 104 | do -- 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 |
104 | end | 106 | end |
105 | 107 | ||
106 | 108 | ||
@@ -128,7 +130,7 @@ do -- bug since 5.4.0 | |||
128 | end | 130 | end |
129 | 131 | ||
130 | 132 | ||
131 | function f (i) | 133 | local 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; |
134 | end | 136 | end |
@@ -154,10 +156,10 @@ end | |||
154 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) | 156 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) |
155 | 157 | ||
156 | for i=1,1000 do break; end; | 158 | for i=1,1000 do break; end; |
157 | n=100; | 159 | local n=100; |
158 | i=3; | 160 | local i=3; |
159 | t = {}; | 161 | local t = {}; |
160 | a=nil | 162 | local a=nil |
161 | while not a do | 163 | while 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; |
163 | end | 165 | end |
@@ -200,14 +202,14 @@ a={y=1} | |||
200 | x = {a.y} | 202 | x = {a.y} |
201 | assert(x[1] == 1) | 203 | assert(x[1] == 1) |
202 | 204 | ||
203 | function f(i) | 205 | local 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; |
208 | end; | 210 | end; |
209 | 211 | ||
210 | function g(i) | 212 | local 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; |
273 | end | 275 | end |
274 | 276 | ||
275 | function h (a,b,c,d,e) | 277 | local 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; |
278 | end; | 280 | end; |
@@ -300,7 +302,7 @@ do | |||
300 | assert(a==2) | 302 | assert(a==2) |
301 | end | 303 | end |
302 | 304 | ||
303 | function F(a) | 305 | local 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 |
306 | end | 308 | end |
@@ -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 |
395 | end | 397 | end |
398 | IX = 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) | |||
30 | end | 30 | end |
31 | 31 | ||
32 | _G.x = nil -- declare x | 32 | _G.x = nil -- declare x |
33 | function foo (a, ...) | 33 | _G.f = nil -- declare f |
34 | local 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") | |||
67 | s, a = coroutine.resume(f, "xuxu") | 68 | s, a = coroutine.resume(f, "xuxu") |
68 | assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") | 69 | assert(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 |
72 | local function foo (i) return coroutine.yield(i) end | 74 | local function foo (i) return coroutine.yield(i) end |
73 | f = coroutine.wrap(function () | 75 | local 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) | |||
79 | for i=1,10 do _G.x = i; assert(f(i) == i) end | 81 | for 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 |
83 | function pf (n, i) | 87 | local function pf (n, i) |
84 | coroutine.yield(n) | 88 | coroutine.yield(n) |
85 | pf(n*i, i+1) | 89 | pf(n*i, i+1) |
86 | end | 90 | end |
@@ -93,14 +97,14 @@ for i=1,10 do | |||
93 | end | 97 | end |
94 | 98 | ||
95 | -- sieve | 99 | -- sieve |
96 | function gen (n) | 100 | local 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) |
100 | end | 104 | end |
101 | 105 | ||
102 | 106 | ||
103 | function filter (p, g) | 107 | local 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 | ||
278 | co = coroutine.wrap(function() | 282 | local 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) | |||
303 | assert(r1 and not r2 and v[1] == (10 + 1)*10/2) | 307 | assert(r1 and not r2 and v[1] == (10 + 1)*10/2) |
304 | 308 | ||
305 | 309 | ||
306 | function f (a, b) a = coroutine.yield(a); error{a + b} end | 310 | local function f (a, b) a = coroutine.yield(a); error{a + b} end |
307 | function g(x) return x[1]*2 end | 311 | local function g(x) return x[1]*2 end |
308 | 312 | ||
309 | co = coroutine.wrap(function () | 313 | co = 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 | ||
313 | assert(co() == 10) | 317 | assert(co() == 10) |
314 | r, msg = co(100) | 318 | local r, msg = co(100) |
315 | assert(not r and msg == 240) | 319 | assert(not r and msg == 240) |
316 | 320 | ||
317 | 321 | ||
@@ -373,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead") | |||
373 | a,b = coroutine.resume(x) | 377 | a,b = coroutine.resume(x) |
374 | assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") | 378 | assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") |
375 | 379 | ||
380 | goo = nil | ||
376 | 381 | ||
377 | -- co-routines x for loop | 382 | -- co-routines x for loop |
378 | function all (a, n, k) | 383 | local 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 | ||
418 | function co_func (current_co) | 423 | local 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 |
492 | local x = coroutine.create (function () | 497 | local 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 | ||
498 | assert(not coroutine.resume(x)) | 503 | assert(not coroutine.resume(x)) |
499 | -- overwrite previous position of local `a' | 504 | -- overwrite previous position of local `a' |
500 | assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) | 505 | assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) |
501 | assert(_G.f() == 11) | 506 | assert(_G.F() == 11) |
502 | assert(_G.f() == 12) | 507 | assert(_G.F() == 12) |
508 | _G.F = nil | ||
503 | 509 | ||
504 | 510 | ||
505 | if not T then | 511 | if 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 | ||
742 | if not _soft then | 751 | if not _soft then |
@@ -935,7 +944,7 @@ assert(run(function () | |||
935 | do local _ENV = _ENV | 944 | do local _ENV = _ENV |
936 | f = function () AAA = BBB + 1; return AAA end | 945 | f = function () AAA = BBB + 1; return AAA end |
937 | end | 946 | end |
938 | g = new(10); g.k.BBB = 10; | 947 | local g = new(10); g.k.BBB = 10; |
939 | debug.setupvalue(f, 1, g) | 948 | debug.setupvalue(f, 1, g) |
940 | assert(run(f, {"idx", "nidx", "idx"}) == 11) | 949 | assert(run(f, {"idx", "nidx", "idx"}) == 11) |
941 | assert(g.k.AAA == 11) | 950 | assert(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 | ||
1087 | local y | ||
1088 | |||
1078 | co = coroutine.wrap(function (...) return | 1089 | co = 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 | ||
1128 | assert(not pcall(co)) -- coroutine should be dead | 1139 | assert(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 |
1132 | local co = coroutine.wrap(function () | 1146 | local 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 | |||
16 | assert(not debug.gethook()) | 16 | assert(not debug.gethook()) |
17 | 17 | ||
18 | local testline = 19 -- line where 'test' is defined | 18 | local testline = 19 -- line where 'test' is defined |
19 | function test (s, l, p) -- this must be line 19 | 19 | local 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 |
53 | a = "function f () end" | 53 | local a = "function f () end" |
54 | local function dostring (s, x) return load(s, x)() end | 54 | local function dostring (s, x) return load(s, x)() end |
55 | dostring(a) | 55 | dostring(a) |
56 | assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) | 56 | assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) |
@@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500))) | |||
72 | assert(string.find(debug.getinfo(f).short_src, "^x*$")) | 72 | assert(string.find(debug.getinfo(f).short_src, "^x*$")) |
73 | dostring(a, "=") | 73 | dostring(a, "=") |
74 | assert(debug.getinfo(f).short_src == "") | 74 | assert(debug.getinfo(f).short_src == "") |
75 | a = nil; f = nil; | 75 | _G.a = nil; _G.f = nil; |
76 | _G[string.rep("p", 400)] = nil | ||
76 | 77 | ||
77 | 78 | ||
78 | repeat | 79 | repeat |
@@ -120,6 +121,7 @@ else | |||
120 | end | 121 | end |
121 | ]], {2,3,4,7}) | 122 | ]], {2,3,4,7}) |
122 | 123 | ||
124 | |||
123 | test([[ | 125 | test([[ |
124 | local function foo() | 126 | local function foo() |
125 | end | 127 | end |
@@ -128,6 +130,7 @@ A = 1 | |||
128 | A = 2 | 130 | A = 2 |
129 | A = 3 | 131 | A = 3 |
130 | ]], {2, 3, 2, 4, 5, 6}) | 132 | ]], {2, 3, 2, 4, 5, 6}) |
133 | _G.A = nil | ||
131 | 134 | ||
132 | 135 | ||
133 | test([[-- | 136 | test([[-- |
@@ -175,6 +178,8 @@ end | |||
175 | 178 | ||
176 | test([[for i=1,4 do a=1 end]], {1,1,1,1}) | 179 | test([[for i=1,4 do a=1 end]], {1,1,1,1}) |
177 | 180 | ||
181 | _G.a = nil | ||
182 | |||
178 | 183 | ||
179 | do -- testing line info/trace with large gaps in source | 184 | do -- 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 |
196 | end | 201 | end |
202 | _G.a = nil | ||
197 | 203 | ||
198 | 204 | ||
199 | do -- testing active lines | 205 | do -- testing active lines |
@@ -287,7 +293,6 @@ foo(200, 3, 4) | |||
287 | local a = {} | 293 | local a = {} |
288 | for i = 1, (_soft and 100 or 1000) do a[i] = i end | 294 | for i = 1, (_soft and 100 or 1000) do a[i] = i end |
289 | foo(table.unpack(a)) | 295 | foo(table.unpack(a)) |
290 | a = 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) |
309 | end | 314 | end |
315 | _ENV.a = nil | ||
310 | 316 | ||
311 | 317 | ||
312 | -- hook table has weak keys | 318 | -- hook table has weak keys |
313 | assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') | 319 | assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') |
314 | 320 | ||
315 | 321 | ||
316 | a = {}; L = nil | 322 | a = {}; local L = nil |
317 | local glob = 1 | 323 | local glob = 1 |
318 | local oldglob = glob | 324 | local oldglob = glob |
319 | debug.sethook(function (e,l) | 325 | debug.sethook(function (e,l) |
@@ -354,7 +360,7 @@ function foo() | |||
354 | end; foo() -- set L | 360 | end; 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\ | 363 | local _ = 'alo\ |
358 | alo' .. [[ | 364 | alo' .. [[ |
359 | 365 | ||
360 | ]] | 366 | ]] |
@@ -403,6 +409,7 @@ function g(a,b) return (a+1) + f() end | |||
403 | 409 | ||
404 | assert(g(0,0) == 30) | 410 | assert(g(0,0) == 30) |
405 | 411 | ||
412 | _G.f, _G.g = nil | ||
406 | 413 | ||
407 | debug.sethook(nil); | 414 | debug.sethook(nil); |
408 | assert(not debug.gethook()) | 415 | assert(not debug.gethook()) |
@@ -446,7 +453,7 @@ local function collectlocals (level) | |||
446 | end | 453 | end |
447 | 454 | ||
448 | 455 | ||
449 | X = nil | 456 | local X = nil |
450 | a = {} | 457 | a = {} |
451 | function a:f (a, b, ...) local arg = {...}; local c = 13 end | 458 | function a:f (a, b, ...) local arg = {...}; local c = 13 end |
452 | debug.sethook(function (e) | 459 | debug.sethook(function (e) |
@@ -469,6 +476,7 @@ a:f(1,2,3,4,5) | |||
469 | assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) | 476 | assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) |
470 | assert(XX == 12) | 477 | assert(XX == 12) |
471 | assert(not debug.gethook()) | 478 | assert(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 | ||
594 | debug.sethook() | 602 | debug.sethook() |
595 | 603 | ||
604 | local g, g1 | ||
596 | 605 | ||
597 | -- tests for tail calls | 606 | -- tests for tail calls |
598 | local function f (x) | 607 | local function f (x) |
@@ -638,7 +647,7 @@ h(false) | |||
638 | debug.sethook() | 647 | debug.sethook() |
639 | assert(b == 2) -- two tail calls | 648 | assert(b == 2) -- two tail calls |
640 | 649 | ||
641 | lim = _soft and 3000 or 30000 | 650 | local lim = _soft and 3000 or 30000 |
642 | local function foo (x) | 651 | local 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 | ||
942 | print("testing debug functions on chunk without debug info") | 951 | print("testing debug functions on chunk without debug info") |
943 | prog = [[-- program to be loaded without debug information (strip) | 952 | local prog = [[-- program to be loaded without debug information (strip) |
944 | local debug = require'debug' | 953 | local debug = require'debug' |
945 | local a = 12 -- a local variable | 954 | local 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") | |||
114 | checkmessage("a = {} < 1", "attempt to compare") | 114 | checkmessage("a = {} < 1", "attempt to compare") |
115 | checkmessage("a = {} <= 1", "attempt to compare") | 115 | checkmessage("a = {} <= 1", "attempt to compare") |
116 | 116 | ||
117 | checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") | 117 | checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") |
118 | checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") | 118 | checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") |
119 | checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") | 119 | checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") |
120 | assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) | 120 | assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) |
121 | checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") | 121 | checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") |
122 | checkmessage("a=(1)..{}", "a table value") | 122 | checkmessage("aaa=(1)..{}", "a table value") |
123 | |||
124 | _G.aaa, _G.bbbb = nil | ||
123 | 125 | ||
124 | -- calls | 126 | -- calls |
125 | checkmessage("local a; a(13)", "local 'a'") | 127 | checkmessage("local a; a(13)", "local 'a'") |
@@ -134,12 +136,13 @@ checkmessage([[ | |||
134 | 136 | ||
135 | -- tail calls | 137 | -- tail calls |
136 | checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") | 138 | checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") |
137 | checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") | 139 | checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") |
140 | |||
141 | checkmessage("aaa = #print", "length of a function value") | ||
142 | checkmessage("aaa = #3", "length of a number value") | ||
138 | 143 | ||
139 | checkmessage("a = #print", "length of a function value") | 144 | _G.aaa = nil |
140 | checkmessage("a = #3", "length of a number value") | ||
141 | 145 | ||
142 | aaa = nil | ||
143 | checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") | 146 | checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") |
144 | checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") | 147 | checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") |
145 | checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") | 148 | checkmessage("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 | ||
153 | checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") | 156 | checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") |
154 | 157 | ||
155 | checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") | 158 | checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") |
156 | checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") | 159 | checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") |
157 | checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") | 160 | checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") |
158 | checkmessage("aaa={}; x=-aaa", "global 'aaa'") | 161 | checkmessage("aaa={}; x=-aaa", "global 'aaa'") |
159 | 162 | ||
160 | -- short circuit | 163 | -- short circuit |
161 | checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", | 164 | checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", |
162 | "local 'bbbb'") | 165 | "local 'bbbb'") |
163 | checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") | 166 | checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", |
167 | "local 'bbbb'") | ||
164 | checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") | 168 | checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") |
165 | checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") | 169 | checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") |
166 | assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) | 170 | assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) |
@@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation") | |||
187 | checkmessage("return ~-3.009", "has no integer representation") | 191 | checkmessage("return ~-3.009", "has no integer representation") |
188 | checkmessage("return 3.009 & 1", "has no integer representation") | 192 | checkmessage("return 3.009 & 1", "has no integer representation") |
189 | checkmessage("return 34 >> {}", "table value") | 193 | checkmessage("return 34 >> {}", "table value") |
190 | checkmessage("a = 24 // 0", "divide by zero") | 194 | checkmessage("aaa = 24 // 0", "divide by zero") |
191 | checkmessage("a = 1 % 0", "'n%0'") | 195 | checkmessage("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 |
270 | local t = {} | 274 | local t = {} |
271 | for i = 1, 1000 do | 275 | for i = 1, 1000 do |
272 | t[i] = "a = x" .. i | 276 | t[i] = "aaa = x" .. i |
273 | end | 277 | end |
274 | local s = table.concat(t, "; ") | 278 | local s = table.concat(t, "; ") |
275 | t = nil | 279 | t = nil |
276 | checkmessage(s.."; a = bbb + 1", "global 'bbb'") | 280 | checkmessage(s.."; aaa = bbb + 1", "global 'bbb'") |
277 | checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") | 281 | checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'") |
278 | checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") | 282 | checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'") |
279 | checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") | 283 | checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") |
280 | 284 | ||
281 | checkmessage([[aaa=9 | 285 | checkmessage([[aaa=9 |
@@ -324,14 +328,17 @@ main() | |||
324 | ]], "global 'NoSuchName'") | 328 | ]], "global 'NoSuchName'") |
325 | print'+' | 329 | print'+' |
326 | 330 | ||
327 | a = {}; setmetatable(a, {__index = string}) | 331 | aaa = {}; setmetatable(aaa, {__index = string}) |
328 | checkmessage("a:sub()", "bad self") | 332 | checkmessage("aaa:sub()", "bad self") |
329 | checkmessage("string.sub('a', {})", "#2") | 333 | checkmessage("string.sub('a', {})", "#2") |
330 | checkmessage("('a'):sub{}", "#1") | 334 | checkmessage("('a'):sub{}", "#1") |
331 | 335 | ||
332 | checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") | 336 | checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") |
333 | checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") | 337 | checkmessage("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 | ||
337 | local function f (n) | 344 | local 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'. |
352 | idsize = 60 - 1 | 359 | local idsize = 60 - 1 |
353 | local function checksize (source) | 360 | local 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 | ||
412 | local p = [[ | 419 | local 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 |
415 | g() | 422 | g() |
416 | ]] | 423 | ]] |
417 | X=3;lineerror((p), 3) | 424 | XX=3;lineerror((p), 3) |
418 | X=0;lineerror((p), false) | 425 | XX=0;lineerror((p), false) |
419 | X=1;lineerror((p), 2) | 426 | XX=1;lineerror((p), 2) |
420 | X=2;lineerror((p), 1) | 427 | XX=2;lineerror((p), 1) |
428 | _G.XX, _G.g, _G.f = nil | ||
421 | 429 | ||
422 | 430 | ||
423 | lineerror([[ | 431 | lineerror([[ |
@@ -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 | |||
561 | end | 571 | end |
562 | 572 | ||
563 | -- xpcall with arguments | 573 | -- xpcall with arguments |
564 | a, b, c = xpcall(string.find, error, "alo", "al") | 574 | local a, b, c = xpcall(string.find, error, "alo", "al") |
565 | assert(a and b == 1 and c == 2) | 575 | assert(a and b == 1 and c == 2) |
566 | a, b, c = xpcall(string.find, function (x) return {} end, true, "al") | 576 | a, b, c = xpcall(string.find, function (x) return {} end, true, "al") |
567 | assert(not a and type(b) == "table" and c == nil) | 577 | assert(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 |
582 | checksyntax("\255a = 1", "", "<\\255>", 1) | 592 | checksyntax("\255a = 1", "", "<\\255>", 1) |
583 | 593 | ||
584 | doit('I = load("a=9+"); a=3') | 594 | doit('I = load("a=9+"); aaa=3') |
585 | assert(a==3 and not I) | 595 | assert(_G.aaa==3 and not _G.I) |
596 | _G.I,_G.aaa = nil | ||
586 | print('+') | 597 | print('+') |
587 | 598 | ||
588 | lim = 1000 | 599 | local lim = 1000 |
589 | if _soft then lim = 100 end | 600 | if _soft then lim = 100 end |
590 | for i=1,lim do | 601 | for 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 | ||
421 | assert(_G.X == 20) | 421 | assert(_G.X == 20) |
422 | 422 | ||
423 | _G.X, _G.B = nil | ||
424 | |||
425 | |||
423 | print'+' | 426 | print'+' |
424 | 427 | ||
425 | local _g = _G | 428 | local _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)))() | |||
507 | assert(_G.X == 4) | 507 | assert(_G.X == 4) |
508 | load((io.lines(file, 3)))() | 508 | load((io.lines(file, 3)))() |
509 | assert(_G.X == 8) | 509 | assert(_G.X == 8) |
510 | _G.X = nil | ||
510 | 511 | ||
511 | print('+') | 512 | print('+') |
512 | 513 | ||
513 | local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" | 514 | local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" |
514 | io.output(file) | 515 | io.output(file) |
515 | assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) | 516 | assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1))) |
516 | io.close() | 517 | io.close() |
517 | assert(loadfile(file))() | 518 | assert(loadfile(file))() |
518 | assert(x1 == x2) | 519 | assert(x1 == _G.X2) |
520 | _G.X2 = nil | ||
519 | print('+') | 521 | print('+') |
520 | assert(os.remove(file)) | 522 | assert(os.remove(file)) |
521 | assert(not os.remove(file)) | 523 | assert(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 | |
129 | end | 129 | end |
130 | 130 | ||
131 | 131 | ||
@@ -134,7 +134,7 @@ do local f = function () end end | |||
134 | 134 | ||
135 | 135 | ||
136 | print("functions with errors") | 136 | print("functions with errors") |
137 | prog = [[ | 137 | local prog = [[ |
138 | do | 138 | do |
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 |
155 | end | 155 | end |
156 | rawset(_G, "a", nil) | ||
157 | _G.x = nil | ||
156 | 158 | ||
157 | foo = nil | 159 | do |
158 | print('long strings') | 160 | foo = nil |
159 | x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" | 161 | print('long strings') |
160 | assert(string.len(x)==80) | 162 | local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" |
161 | s = '' | 163 | assert(string.len(x)==80) |
162 | k = math.min(300, (math.maxinteger // 80) // 2) | 164 | local s = '' |
163 | for n = 1, k do s = s..x; j=tostring(n) end | 165 | local k = math.min(300, (math.maxinteger // 80) // 2) |
164 | assert(string.len(s) == k*80) | 166 | for n = 1, k do s = s..x; local j=tostring(n) end |
165 | s = string.sub(s, 1, 10000) | 167 | assert(string.len(s) == k*80) |
166 | s, i = string.gsub(s, '(%d%d%d%d)', '') | 168 | s = string.sub(s, 1, 10000) |
167 | assert(i==10000 // 4) | 169 | local s, i = string.gsub(s, '(%d%d%d%d)', '') |
168 | s = nil | 170 | assert(i==10000 // 4) |
169 | x = nil | 171 | |
170 | 172 | assert(_G["while"] == 234) | |
171 | assert(_G["while"] == 234) | 173 | _G["while"] = nil |
174 | end | ||
172 | 175 | ||
173 | 176 | ||
174 | -- | 177 | -- |
@@ -227,8 +230,8 @@ end | |||
227 | 230 | ||
228 | 231 | ||
229 | print("clearing tables") | 232 | print("clearing tables") |
230 | lim = 15 | 233 | local lim = 15 |
231 | a = {} | 234 | local a = {} |
232 | -- fill a with `collectable' indices | 235 | -- fill a with `collectable' indices |
233 | for i=1,lim do a[{}] = i end | 236 | for i=1,lim do a[{}] = i end |
234 | b = {} | 237 | b = {} |
@@ -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 | ||
555 | end | 559 | end |
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 | ||
11 | dostring("x \v\f = \t\r 'a\0a' \v\f\f") | 11 | dostring("x \v\f = \t\r 'a\0a' \v\f\f") |
12 | assert(x == 'a\0a' and string.len(x) == 3) | 12 | assert(x == 'a\0a' and string.len(x) == 3) |
13 | _G.x = nil | ||
13 | 14 | ||
14 | -- escape sequences | 15 | -- escape sequences |
15 | assert('\n\"\'\\' == [[ | 16 | assert('\n\"\'\\' == [[ |
@@ -129,16 +130,16 @@ end | |||
129 | 130 | ||
130 | -- long variable names | 131 | -- long variable names |
131 | 132 | ||
132 | var1 = string.rep('a', 15000) .. '1' | 133 | local var1 = string.rep('a', 15000) .. '1' |
133 | var2 = string.rep('a', 15000) .. '2' | 134 | local var2 = string.rep('a', 15000) .. '2' |
134 | prog = string.format([[ | 135 | local 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) |
139 | local f = dostring(prog) | 140 | local f = dostring(prog) |
140 | assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) | 141 | assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) |
141 | var1, var2, f = nil | 142 | _G[var1], _G[var2] = nil |
142 | print('+') | 143 | print('+') |
143 | 144 | ||
144 | -- escapes -- | 145 | -- escapes -- |
@@ -150,13 +151,13 @@ assert([[ | |||
150 | $debug]] == "\n $debug") | 151 | $debug]] == "\n $debug") |
151 | assert([[ [ ]] ~= [[ ] ]]) | 152 | assert([[ [ ]] ~= [[ ] ]]) |
152 | -- long strings -- | 153 | -- long strings -- |
153 | b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" | 154 | local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
154 | assert(string.len(b) == 960) | 155 | assert(string.len(b) == 960) |
155 | prog = [=[ | 156 | prog = [=[ |
156 | print('+') | 157 | print('+') |
157 | 158 | ||
158 | a1 = [["this is a 'string' with several 'quotes'"]] | 159 | local a1 = [["this is a 'string' with several 'quotes'"]] |
159 | a2 = "'quotes'" | 160 | local a2 = "'quotes'" |
160 | 161 | ||
161 | assert(string.find(a1, a2) == 34) | 162 | assert(string.find(a1, a2) == 34) |
162 | print('+') | 163 | print('+') |
@@ -164,12 +165,13 @@ print('+') | |||
164 | a1 = [==[temp = [[an arbitrary value]]; ]==] | 165 | a1 = [==[temp = [[an arbitrary value]]; ]==] |
165 | assert(load(a1))() | 166 | assert(load(a1))() |
166 | assert(temp == 'an arbitrary value') | 167 | assert(temp == 'an arbitrary value') |
168 | _G.temp = nil | ||
167 | -- long strings -- | 169 | -- long strings -- |
168 | b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" | 170 | local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
169 | assert(string.len(b) == 960) | 171 | assert(string.len(b) == 960) |
170 | print('+') | 172 | print('+') |
171 | 173 | ||
172 | a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 | 174 | local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
173 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | 175 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
174 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | 176 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
175 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | 177 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
@@ -199,13 +201,11 @@ x = 1 | |||
199 | ]=] | 201 | ]=] |
200 | 202 | ||
201 | print('+') | 203 | print('+') |
202 | x = nil | 204 | _G.x = nil |
203 | dostring(prog) | 205 | dostring(prog) |
204 | assert(x) | 206 | assert(x) |
207 | _G.x = nil | ||
205 | 208 | ||
206 | prog = nil | ||
207 | a = nil | ||
208 | b = nil | ||
209 | 209 | ||
210 | 210 | ||
211 | do -- reuse of long strings | 211 | do -- reuse of long strings |
@@ -234,8 +234,8 @@ end | |||
234 | 234 | ||
235 | -- testing line ends | 235 | -- testing line ends |
236 | prog = [[ | 236 | prog = [[ |
237 | a = 1 -- a comment | 237 | local a = 1 -- a comment |
238 | b = 2 | 238 | local b = 2 |
239 | 239 | ||
240 | 240 | ||
241 | x = [=[ | 241 | x = [=[ |
@@ -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") |
254 | end | 254 | end |
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 |
258 | a = [==[]=]==] | 259 | local a = [==[]=]==] |
259 | assert(a == "]=") | 260 | assert(a == "]=") |
260 | 261 | ||
261 | a = [==[[===[[=[]]=][====[]]===]===]==] | 262 | a = [==[[===[[=[]]=][====[]]===]===]==] |
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 | |||
37 | f = nil | 37 | f = nil |
38 | 38 | ||
39 | local f | 39 | local f |
40 | x = 1 | 40 | local x = 1 |
41 | 41 | ||
42 | a = nil | 42 | a = nil |
43 | load('local a = {}')() | 43 | load('local a = {}')() |
@@ -152,7 +152,7 @@ local dummy | |||
152 | local _ENV = (function (...) return ... end)(_G, dummy) -- { | 152 | local _ENV = (function (...) return ... end)(_G, dummy) -- { |
153 | 153 | ||
154 | do local _ENV = {assert=assert}; assert(true) end | 154 | do local _ENV = {assert=assert}; assert(true) end |
155 | mt = {_G = _G} | 155 | local mt = {_G = _G} |
156 | local foo,x | 156 | local foo,x |
157 | A = false -- "declare" A | 157 | A = false -- "declare" A |
158 | do local _ENV = mt | 158 | do local _ENV = mt |
@@ -174,6 +174,8 @@ do local _ENV = {assert=assert, A=10}; | |||
174 | end | 174 | end |
175 | assert(x==20) | 175 | assert(x==20) |
176 | 176 | ||
177 | A = nil | ||
178 | |||
177 | 179 | ||
178 | do -- constants | 180 | do -- 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") | |||
339 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) | 339 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) |
340 | checkprogout("b\nc\nd\ne\n\n") | 340 | checkprogout("b\nc\nd\ne\n\n") |
341 | 341 | ||
342 | prompt = "alo" | 342 | local prompt = "alo" |
343 | prepfile[[ -- | 343 | prepfile[[ -- |
344 | a = 2 | 344 | a = 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 |
393 | s = [=[ -- | 393 | local s = [=[ -- |
394 | function f ( x ) | 394 | function f ( x ) |
395 | local a = [[ | 395 | local a = [[ |
396 | xuxu | 396 | xuxu |
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 | |||
50 | local msgf2i = "number.* has no integer representation" | 50 | local msgf2i = "number.* has no integer representation" |
51 | 51 | ||
52 | -- float equality | 52 | -- float equality |
53 | function eq (a,b,limit) | 53 | local 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 |
65 | function eqT (a,b) | 65 | local 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) |
67 | end | 67 | end |
68 | 68 | ||
@@ -83,7 +83,7 @@ end | |||
83 | do | 83 | do |
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]) |
88 | end | 88 | end |
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 |
191 | lim = 35 | 191 | lim = 35 |
192 | function foo (n, ...) | 192 | local 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, ...) | |||
9 | end | 9 | end |
10 | 10 | ||
11 | 11 | ||
12 | function f(s, p) | 12 | local 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 |
15 | end | 15 | end |
16 | 16 | ||
17 | a,b = string.find('', '') -- empty patterns are tricky | 17 | local a,b = string.find('', '') -- empty patterns are tricky |
18 | assert(a == 1 and b == 0); | 18 | assert(a == 1 and b == 0); |
19 | a,b = string.find('alo', '') | 19 | a,b = string.find('alo', '') |
20 | assert(a == 1 and b == 0) | 20 | assert(a == 1 and b == 0) |
@@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo") | |||
88 | print('+') | 88 | print('+') |
89 | 89 | ||
90 | 90 | ||
91 | function f1(s, p) | 91 | local 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 | ||
114 | assert(string.len(abc) == 256) | 114 | assert(string.len(abc) == 256) |
115 | 115 | ||
116 | function strset (p) | 116 | local 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') | |||
147 | assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim | 147 | assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim |
148 | assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim | 148 | assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim |
149 | assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') | 149 | assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') |
150 | t = "abç d" | 150 | local t = "abç d" |
151 | a, b = string.gsub(t, '(.)', '%1@') | 151 | a, b = string.gsub(t, '(.)', '%1@') |
152 | assert('@'..a == string.gsub(t, '', '@') and b == 5) | 152 | assert('@'..a == string.gsub(t, '', '@') and b == 5) |
153 | a, b = string.gsub('abçd', '(.)', '%0@', 2) | 153 | a, 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 | ||
187 | end | 188 | end |
188 | 189 | ||
189 | function f(a,b) return string.gsub(a,'.',b) end | 190 | function 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 | ||
198 | x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", | 199 | local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", |
199 | "$([^$]*)%$", dostring) | 200 | "$([^$]*)%$", dostring) |
200 | assert(x == ' assim vai para ALO') | 201 | assert(x == ' assim vai para ALO') |
201 | 202 | _G.a, _G.x = nil | |
202 | t = {} | 203 | |
203 | s = 'a alo jose joao' | 204 | local t = {} |
204 | r = string.gsub(s, '()(%w+)()', function (a,w,b) | 205 | local s = 'a alo jose joao' |
205 | assert(string.len(w) == b-a); | 206 | local 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) | ||
208 | assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) | 210 | assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) |
209 | 211 | ||
210 | 212 | ||
211 | function isbalanced (s) | 213 | local function isbalanced (s) |
212 | return not string.find(string.gsub(s, "%b()", ""), "[()]") | 214 | return not string.find(string.gsub(s, "%b()", ""), "[()]") |
213 | end | 215 | end |
214 | 216 | ||
@@ -251,7 +253,7 @@ if not _soft then | |||
251 | end | 253 | end |
252 | 254 | ||
253 | -- recursive nest of gsubs | 255 | -- recursive nest of gsubs |
254 | function rev (s) | 256 | local 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) |
256 | end | 258 | end |
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 | |||
20 | checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) | 20 | checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) |
21 | 21 | ||
22 | local x,y,z,a,n | 22 | local x,y,z,a,n |
23 | a = {}; lim = _soft and 200 or 2000 | 23 | a = {}; local lim = _soft and 200 or 2000 |
24 | for i=1, lim do a[i]=i end | 24 | for i=1, lim do a[i]=i end |
25 | assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) | 25 | assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) |
26 | x = unpack(a) | 26 | x = unpack(a) |
@@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", | |||
222 | table.sort(a) | 222 | table.sort(a) |
223 | check(a) | 223 | check(a) |
224 | 224 | ||
225 | function perm (s, n) | 225 | local 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} | |||
248 | perm{1,2,3,4,5,6} | 248 | perm{1,2,3,4,5,6} |
249 | perm{2,2,3,3,5,6} | 249 | perm{2,2,3,3,5,6} |
250 | 250 | ||
251 | function timesort (a, n, func, msg, pre) | 251 | local 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) |
258 | end | 258 | end |
259 | 259 | ||
260 | limit = 50000 | 260 | local limit = 50000 |
261 | if _soft then limit = 5000 end | 261 | if _soft then limit = 5000 end |
262 | 262 | ||
263 | a = {} | 263 | a = {} |
@@ -274,7 +274,7 @@ for i=1,limit do | |||
274 | a[i] = math.random() | 274 | a[i] = math.random() |
275 | end | 275 | end |
276 | 276 | ||
277 | x = os.clock(); i=0 | 277 | local x = os.clock(); local i = 0 |
278 | table.sort(a, function(x,y) i=i+1; return y<x end) | 278 | table.sort(a, function(x,y) i=i+1; return y<x end) |
279 | x = (os.clock() - x) * 1000 | 279 | x = (os.clock() - x) * 1000 |
280 | print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", | 280 | print(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 | ||
290 | for i,v in pairs(a) do assert(v == false) end | 290 | for i,v in pairs(a) do assert(v == false) end |
291 | 291 | ||
292 | A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} | 292 | AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} |
293 | table.sort(A) | 293 | table.sort(AA) |
294 | check(A) | 294 | check(AA) |
295 | 295 | ||
296 | table.sort(A, function (x, y) | 296 | table.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 | ||
303 | tt = {__lt = function (a,b) return a.val < b.val end} | 304 | local tt = {__lt = function (a,b) return a.val < b.val end} |
304 | a = {} | 305 | a = {} |
305 | for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end | 306 | for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end |
306 | table.sort(a) | 307 | table.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 |
54 | assert(string.find("123456789", "345") == 3) | 54 | assert(string.find("123456789", "345") == 3) |
55 | a,b = string.find("123456789", "345") | 55 | local a,b = string.find("123456789", "345") |
56 | assert(string.sub("123456789", a, b) == "345") | 56 | assert(string.sub("123456789", a, b) == "345") |
57 | assert(string.find("1234567890123456789", "345", 3) == 3) | 57 | assert(string.find("1234567890123456789", "345", 3) == 3) |
58 | assert(string.find("1234567890123456789", "345", 4) == 13) | 58 | assert(string.find("1234567890123456789", "345", 4) == 13) |
@@ -192,7 +192,7 @@ do -- tests for '%p' format | |||
192 | end | 192 | end |
193 | end | 193 | end |
194 | 194 | ||
195 | x = '"ílo"\n\\' | 195 | local x = '"ílo"\n\\' |
196 | assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') | 196 | assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') |
197 | assert(string.format('%q', "\0") == [["\0"]]) | 197 | assert(string.format('%q', "\0") == [["\0"]]) |
198 | assert(load(string.format('return %q', x))() == x) | 198 | assert(load(string.format('return %q', x))() == x) |
@@ -452,7 +452,7 @@ end | |||
452 | do | 452 | do |
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") |
457 | end | 457 | end |
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 |
38 | function checkerror (msg, f, ...) | 38 | local 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) |
231 | end | 231 | end |
232 | 232 | ||
233 | x = "日本語a-4\0éó" | 233 | local x = "日本語a-4\0éó" |
234 | check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) | 234 | check(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 | ||
4 | print('testing vararg') | 4 | print('testing vararg') |
5 | 5 | ||
6 | function f(a, ...) | 6 | local 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 |
10 | end | 10 | end |
11 | 11 | ||
12 | function c12 (...) | 12 | local 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 |
18 | end | 18 | end |
19 | 19 | ||
20 | function vararg (...) return {n = select('#', ...), ...} end | 20 | local function vararg (...) return {n = select('#', ...), ...} end |
21 | 21 | ||
22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end | 22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
23 | 23 | ||
@@ -29,7 +29,7 @@ assert(vararg().n == 0) | |||
29 | assert(vararg(nil, nil).n == 2) | 29 | assert(vararg(nil, nil).n == 2) |
30 | 30 | ||
31 | assert(c12(1,2)==55) | 31 | assert(c12(1,2)==55) |
32 | a,b = assert(call(c12, {1,2})) | 32 | local a,b = assert(call(c12, {1,2})) |
33 | assert(a == 55 and b == 2) | 33 | assert(a == 55 and b == 2) |
34 | a = call(c12, {1,2;n=2}) | 34 | a = call(c12, {1,2;n=2}) |
35 | assert(a == 55 and b == 2) | 35 | assert(a == 55 and b == 2) |
@@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end | |||
49 | assert(t:f(1,4) == 3 and t:f(2) == 11) | 49 | assert(t:f(1,4) == 3 and t:f(2) == 11) |
50 | print('+') | 50 | print('+') |
51 | 51 | ||
52 | lim = 20 | 52 | local lim = 20 |
53 | local i, a = 1, {} | 53 | local i, a = 1, {} |
54 | while i <= lim do a[i] = i+0.3; i=i+1 end | 54 | while 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]) |
60 | end | 60 | end |
61 | 61 | ||
62 | function g(a,b,c) | 62 | local 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) |
64 | end | 64 | end |
65 | 65 | ||
@@ -76,7 +76,7 @@ print("+") | |||
76 | 76 | ||
77 | -- new-style varargs | 77 | -- new-style varargs |
78 | 78 | ||
79 | function oneless (a, ...) return ... end | 79 | local function oneless (a, ...) return ... end |
80 | 80 | ||
81 | function f (n, a, ...) | 81 | function 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 |
102 | f = load[[ return {...} ]] | 102 | local f = load[[ return {...} ]] |
103 | x = f(2,3) | 103 | local x = f(2,3) |
104 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) | 104 | assert(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 | |||
52 | print "testing large programs (>64k)" | 52 | print "testing large programs (>64k)" |
53 | 53 | ||
54 | -- template to create a very big test file | 54 | -- template to create a very big test file |
55 | prog = [[$ | 55 | local prog = [[$ |
56 | 56 | ||
57 | local a,b | 57 | local a,b |
58 | 58 | ||
@@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end | |||
85 | assert(b:xxx(10, 12) == 22) -- pushself with non-constant index | 85 | assert(b:xxx(10, 12) == 22) -- pushself with non-constant index |
86 | b["xxx"] = undef | 86 | b["xxx"] = undef |
87 | 87 | ||
88 | s = 0; n=0 | 88 | local s = 0; local n=0 |
89 | for a,b in pairs(b) do s=s+b; n=n+1 end | 89 | for 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 |
91 | assert(81800000.0 < s and s < 81860000 and n == 70001) | 91 | assert(81800000.0 < s and s < 81860000 and n == 70001) |
@@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001) | |||
93 | a = nil; b = nil | 93 | a = nil; b = nil |
94 | print'+' | 94 | print'+' |
95 | 95 | ||
96 | function f(x) b=x end | 96 | local function f(x) b=x end |
97 | 97 | ||
98 | a = f{$3$} or 10 | 98 | a = 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 '-' |
119 | end | 119 | end |
120 | 120 | ||
121 | F = { | 121 | local F = { |
122 | function () -- $1$ | 122 | function () -- $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$ | |||
138 | end, | 138 | end, |
139 | } | 139 | } |
140 | 140 | ||
141 | file = os.tmpname() | 141 | local file = os.tmpname() |
142 | io.output(file) | 142 | io.output(file) |
143 | for s in string.gmatch(prog, "$([^$]+)") do | 143 | for 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 |
146 | end | 146 | end |
147 | io.close() | 147 | io.close() |
148 | result = dofile(file) | 148 | local result = dofile(file) |
149 | assert(os.remove(file)) | 149 | assert(os.remove(file)) |
150 | print'OK' | 150 | print'OK' |
151 | return result | 151 | return result |