From 314745ed8438d1276c6c928d5f9d4be018dfadb6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 28 Dec 2022 18:34:11 -0300 Subject: [PATCH] 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. --- testes/all.lua | 1 + testes/api.lua | 112 +++++++++++++++++++++++------------------- testes/attrib.lua | 24 ++++----- testes/big.lua | 2 +- testes/calls.lua | 44 ++++++++++------- testes/closure.lua | 6 ++- testes/code.lua | 6 +-- testes/constructs.lua | 36 ++++++++------ testes/coroutine.lua | 56 +++++++++++++-------- testes/db.lua | 27 ++++++---- testes/errors.lua | 83 +++++++++++++++++-------------- testes/events.lua | 3 ++ testes/files.lua | 6 ++- testes/gc.lua | 42 +++++++++------- testes/literals.lua | 33 +++++++------ testes/locals.lua | 8 +-- testes/main.lua | 4 +- testes/math.lua | 6 +-- testes/nextvar.lua | 2 +- testes/pm.lua | 32 ++++++------ testes/sort.lua | 23 ++++----- testes/strings.lua | 6 +-- testes/tpack.lua | 2 +- testes/utf8.lua | 2 +- testes/vararg.lua | 18 +++---- 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() dofile('db.lua') assert(dofile('calls.lua') == deep and deep) +_G.deep = nil olddofile('strings.lua') olddofile('literals.lua') 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 -- standard error message for memory errors local MEMERRMSG = "not enough memory" -function tcheck (t1, t2) +local function tcheck (t1, t2) assert(t1.n == (t2.n or #t2) + 1) for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end end @@ -28,7 +28,7 @@ end print('testing C API') -a = T.testC("pushvalue R; return 1") +local a = T.testC("pushvalue R; return 1") assert(a == debug.getregistry()) @@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0) assert(a == string.pack("d", 12458954321123.0)) assert(T.s2d(a) == 12458954321123.0) -a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") +local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") assert(a == 2 and b == 3 and not c) -f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") +local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") a,b,c = f() assert(a == 2 and b == 3 and not c) @@ -61,7 +61,7 @@ assert(a==false and b==true and c==false) a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) assert(a == 40 and b == 5 and not c) -t = pack(T.testC("settop 5; return *", 2, 3)) +local t = pack(T.testC("settop 5; return *", 2, 3)) tcheck(t, {n=4,2,3}) t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) @@ -166,16 +166,17 @@ end -- testing globals -_G.a = 14; _G.b = "a31" +_G.AA = 14; _G.BB = "a31" local a = {T.testC[[ - getglobal a; - getglobal b; - getglobal b; - setglobal a; + getglobal AA; + getglobal BB; + getglobal BB; + setglobal AA; return * ]]} -assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") +assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31") +_G.AA, _G.BB = nil -- testing arith assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) @@ -199,13 +200,14 @@ a,b,c = T.testC([[pushnum 1; pushstring 10; arith _; pushstring 5; return 3]]) assert(a == 1 and b == -10 and c == "5") -mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, +local mt = { + __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, __unm = function (a) return setmetatable({a[1]* 2}, mt) end} a,b,c = setmetatable({4}, mt), setmetatable({8}, mt), setmetatable({-3}, mt) -x,y,z = T.testC("arith +; return 2", 10, a, b) +local x,y,z = T.testC("arith +; return 2", 10, a, b) assert(x == 10 and y[1] == 12 and z == nil) assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) 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") -- testing lua_is -function B(x) return x and 1 or 0 end +local function B (x) return x and 1 or 0 end -function count (x, n) +local function count (x, n) n = n or 2 local prog = [[ isnumber %d; @@ -345,7 +347,7 @@ assert(count(nil, 15) == 100) -- testing lua_to... -function to (s, x, n) +local function to (s, x, n) n = n or 2 return T.testC(string.format("%s %d; return 1", s, n), x) end @@ -486,11 +488,12 @@ a = T.testC([[ pushvalue 3; insert -2; pcall 1 1 0; pcall 0 0 0; return 1 -]], "x=150", function (a) assert(a==nil); return 3 end) +]], "XX=150", function (a) assert(a==nil); return 3 end) -assert(type(a) == 'string' and x == 150) +assert(type(a) == 'string' and XX == 150) +_G.XX = nil -function check3(p, ...) +local function check3(p, ...) local arg = {...} assert(#arg == 3) assert(string.find(arg[3], p)) @@ -500,7 +503,7 @@ check3("%.", T.testC("loadfile 2; return *", ".")) check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) -- test errors in non protected threads -function checkerrnopro (code, msg) +local function checkerrnopro (code, msg) local th = coroutine.create(function () end) -- create new thread local stt, err = pcall(T.testC, th, code) -- run code there assert(not stt and string.find(err, msg)) @@ -510,8 +513,9 @@ if not _soft then collectgarbage("stop") -- avoid __gc with full stack checkerrnopro("pushnum 3; call 0 0", "attempt to call") print"testing stack overflow in unprotected thread" - function f () f() end - checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") + function F () F() end + checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow") + F = nil collectgarbage("restart") end print"+" @@ -588,7 +592,7 @@ assert(a[a] == "x") b = setmetatable({p = a}, {}) getmetatable(b).__index = function (t, i) return t.p[i] end -k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") +local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") assert(x == 15 and k == 35) k = T.testC("getfield 2 y, return 1", b) assert(k == 12) @@ -748,8 +752,8 @@ local i = T.ref{} T.unref(i) assert(T.ref{} == i) -Arr = {} -Lim = 100 +local Arr = {} +local Lim = 100 for i=1,Lim do -- lock many objects Arr[i] = T.ref({}) end @@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them T.unref(Arr[i]) end -function printlocks () +local function printlocks () local f = T.makeCfunc("gettable R; return 1") local n = f("n") print("n", n) @@ -793,8 +797,8 @@ assert(type(T.getref(a)) == 'table') -- colect in cl the `val' of all collected userdata -tt = {} -cl = {n=0} +local tt = {} +local cl = {n=0} A = nil; B = nil local F F = function (x) @@ -817,6 +821,7 @@ F = function (x) end tt.__gc = F + -- test whether udate collection frees memory in the right time do collectgarbage(); @@ -853,9 +858,9 @@ end collectgarbage("stop") -- create 3 userdatas with tag `tt' -a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) -b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) -c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) +a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a) +b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b) +c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c) -- create userdata without meta table x = T.newuserdata(4) @@ -866,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x) assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) -d=T.ref(a); -e=T.ref(b); -f=T.ref(c); +local d = T.ref(a); +local e = T.ref(b); +local f = T.ref(c); t = {T.getref(d), T.getref(e), T.getref(f)} assert(t[1] == a and t[2] == b and t[3] == c) @@ -888,7 +893,7 @@ tt=nil -- frees tt for GC A = nil b = nil T.unref(d); -n5 = T.newuserdata(0) +local n5 = T.newuserdata(0) debug.setmetatable(n5, {__gc=F}) n5 = T.udataval(n5) collectgarbage() @@ -959,11 +964,11 @@ print'+' -- testing changing hooks during hooks -_G.t = {} +_G.TT = {} T.sethook([[ # set a line hook after 3 count hooks sethook 4 0 ' - getglobal t; + getglobal TT; pushvalue -3; append -2 pushvalue -2; append -2 ']], "c", 3) @@ -973,12 +978,13 @@ a = 1 -- count hook (set line hook) a = 1 -- line hook a = 1 -- line hook debug.sethook() -t = _G.t +local t = _G.TT assert(t[1] == "line") -line = t[2] +local line = t[2] assert(t[3] == "line" and t[4] == line + 1) assert(t[5] == "line" and t[6] == line + 2) assert(t[7] == nil) +_G.TT = nil ------------------------------------------------------------------------- @@ -1003,6 +1009,7 @@ do -- testing errors during GC collectgarbage("restart") warn("@on") end +_G.A = nil ------------------------------------------------------------------------- -- test for userdata vals do @@ -1032,8 +1039,8 @@ assert(a == 'alo' and b == '3') T.doremote(L1, "_ERRORMESSAGE = nil") -- error: `sin' is not defined -a, _, b = T.doremote(L1, "return sin(1)") -assert(a == nil and b == 2) -- 2 == run-time error +a, b, c = T.doremote(L1, "return sin(1)") +assert(a == nil and c == 2) -- 2 == run-time error -- error: syntax error a, b, c = T.doremote(L1, "return a+") @@ -1204,7 +1211,7 @@ T.alloccount() -- remove limit -- o that we get memory errors in all allocations of a given -- task, until there is enough memory to complete the task without -- errors. -function testbytes (s, f) +local function testbytes (s, f) collectgarbage() local M = T.totalmem() local oldM = M @@ -1229,7 +1236,7 @@ end -- task, until there is enough allocations to complete the task without -- errors. -function testalloc (s, f) +local function testalloc (s, f) collectgarbage() local M = 0 local a,b = nil @@ -1296,12 +1303,12 @@ end) -- testing threads -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) -mt = T.testC("rawgeti R 1; return 1") +local mt = T.testC("rawgeti R 1; return 1") assert(type(mt) == "thread" and coroutine.running() == mt) -function expand (n,s) +local function expand (n,s) if n==0 then return "" end local e = string.rep("=", n) return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", @@ -1311,9 +1318,10 @@ end G=0; collectgarbage(); a =collectgarbage("count") load(expand(20,"G=G+1"))() assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) +G = nil testamem("running code on new thread", function () - return T.doonnewstack("x=1") == 0 -- try to create thread + return T.doonnewstack("local x=1") == 0 -- try to create thread end) @@ -1327,13 +1335,13 @@ end) local testprog = [[ local function foo () return end local t = {"x"} -a = "aaa" -for i = 1, #t do a=a..t[i] end +AA = "aaa" +for i = 1, #t do AA = AA .. t[i] end return true ]] -- testing memory x dofile -_G.a = nil +_G.AA = nil local t =os.tmpname() local f = assert(io.open(t, "w")) f:write(testprog) @@ -1343,7 +1351,7 @@ testamem("dofile", function () return a and a() end) assert(os.remove(t)) -assert(_G.a == "aaax") +assert(_G.AA == "aaax") -- other generic tests @@ -1360,6 +1368,8 @@ testamem("dump/undump", function () return a and a() end) +_G.AA = nil + local t = os.tmpname() testamem("file creation", function () local f = assert(io.open(t, 'w')) @@ -1381,7 +1391,7 @@ testamem("constructors", function () end) local a = 1 -close = nil +local close = nil testamem("closure creation", function () function close (b) 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 -- prepend DIR to a name and correct directory separators local function D (x) - x = string.gsub(x, "/", dirsep) + local x = string.gsub(x, "/", dirsep) return DIR .. x end @@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras) end end -function removefiles (files) +local function removefiles (files) for n in pairs(files) do os.remove(D(n)) end @@ -154,10 +154,9 @@ local try = function (p, n, r, ext) assert(ext == x) end -a = require"names" +local a = require"names" assert(a[1] == "names" and a[2] == D"names.lua") -_G.a = nil local st, msg = pcall(require, "err") assert(not st and string.find(msg, "arithmetic") and B == 15) st, msg = pcall(require, "synerr") @@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX") removefiles(files) +NAME, REQUIRED, AA, B = nil -- testing require of sub-packages @@ -223,7 +223,7 @@ assert(require"P1" == m and m.AA == 10) removefiles(files) - +AA = nil package.path = "" assert(not pcall(require, "file_does_not_exist")) @@ -305,6 +305,7 @@ else assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") assert(string.find(ext, "libs/lib1", 1, true)) assert(fs.id(45) == 45) + _ENV.x, _ENV.y = nil end _ENV = _G @@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors") local res, res2 = 27 -a, b = 1, 2+3 +local a, b = 1, 2+3 assert(a==1 and b==5) a={} -function f() return 10, 11, 12 end +local function f() return 10, 11, 12 end a.x, b, a[1] = 1, 2, f() assert(a.x==1 and b==2 and a[1]==10) a[f()], b, a[f()+3] = f(), a, 'x' @@ -353,15 +354,15 @@ do local a,b,c a,b = 0, f(1) assert(a == 0 and b == 1) - A,b = 0, f(1) - assert(A == 0 and b == 1) + a,b = 0, f(1) + assert(a == 0 and b == 1) a,b,c = 0,5,f(4) assert(a==0 and b==5 and c==1) a,b,c = 0,5,f(0) assert(a==0 and b==5 and c==nil) end -a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 +local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 assert(not a and b and c and d==6) d = 20 @@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end)) assert(a[nil] == undef) a = nil +local a, b, c a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} a, a.x, a.y = a, a[-3] 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 maxint = maxint // 2 end -maxintF = maxint + 0.0 -- float version +local maxintF = maxint + 0.0 -- float version assert(maxintF == maxint and math.type(maxintF) == "float" and 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, { }) X = nil -co = coroutine.wrap(f) +local co = coroutine.wrap(f) assert(co() == 's') assert(co() == 'g') 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' and type(type) == 'function') assert(type(assert) == type(print)) -function f (x) return a:x (x) end +local function f (x) return a:x (x) end assert(type(f) == 'function') assert(not pcall(type)) @@ -33,10 +33,11 @@ do assert(fact(5) == 120) end assert(fact == false) +fact = nil -- testing declarations -a = {i = 10} -self = 20 +local a = {i = 10} +local self = 20 function a:x (x) return x+self.i end function a.y (x) return x+self end @@ -72,6 +73,8 @@ f(1,2, -- this one too 3,4) assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') +t = nil -- delete 't' + function fat(x) if x <= 1 then return 1 else return x*load("return fat(" .. x-1 .. ")", "")() @@ -80,26 +83,29 @@ end assert(load "load 'assert(fat(6)==720)' () ")() a = load('return fat(5), 3') -a,b = a() +local a,b = a() assert(a == 120 and b == 3) +fat = nil print('+') -function err_on_n (n) +local function err_on_n (n) if n==0 then error(); exit(1); else err_on_n (n-1); exit(1); end end do - function dummy (n) + local function dummy (n) if n > 0 then assert(not pcall(err_on_n, n)) dummy(n-1) end end + + dummy(10) end -dummy(10) +_G.deep = nil -- "declaration" (used by 'all.lua') function deep (n) if n>0 then deep(n-1) end @@ -209,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40) -- testing closures -- fixed-point operator -Z = function (le) +local Z = function (le) local function a (f) return le(function (x) return f(f)(x) end) end @@ -219,14 +225,14 @@ Z = function (le) -- non-recursive factorial -F = function (f) +local F = function (f) return function (n) if n == 0 then return 1 else return n*f(n-1) end end end -fat = Z(F) +local fat = Z(F) assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) @@ -237,22 +243,21 @@ local function g (z) return f(z,z+1,z+2,z+3) end -f = g(10) +local f = g(10) assert(f(9, 16) == 10+11+12+13+10+9+16+10) -Z, F, f = nil print('+') -- testing multiple returns -function unlpack (t, i) +local function unlpack (t, i) i = i or 1 if (i <= #t) then return t[i], unlpack(t, i+1) end end -function equaltab (t1, t2) +local function equaltab (t1, t2) assert(#t1 == #t2) for i = 1, #t1 do assert(t1[i] == t2[i]) @@ -261,8 +266,8 @@ end local pack = function (...) return (table.pack(...)) end -function f() return 1,2,30,4 end -function ret2 (a,b) return a,b end +local function f() return 1,2,30,4 end +local function ret2 (a,b) return a,b end local a,b,c,d = unlpack{1,2,3} 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 ay) and x or y == 2); x,y=2,1; assert((x>y) and x or y == 2); @@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants local gab = f(o1, o2) _ENV.XX = o1 - code = string.format("return XX %s %s", op, o2) - res = assert(load(code))() + local code = string.format("return XX %s %s", op, o2) + local res = assert(load(code))() assert(res == gab) _ENV.XX = o2 - local code = string.format("return (%s) %s XX", o1, op) - local res = assert(load(code))() + code = string.format("return (%s) %s XX", o1, op) + res = assert(load(code))() assert(res == gab) code = string.format("return (%s) %s %s", o1, op, o2) @@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants end end end + _ENV.XX = nil end @@ -100,7 +102,7 @@ repeat until 1; repeat until true; while false do end; while nil do end; do -- test old bug (first name could not be an `upvalue') - local a; function f(x) x={a=1}; x={x=1}; x={G=1} end + local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end end @@ -128,7 +130,7 @@ do -- bug since 5.4.0 end -function f (i) +local function f (i) if type(i) ~= 'number' then return i,'jojo'; end; if i > 0 then return i, f(i-1); end; end @@ -154,10 +156,10 @@ end assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) for i=1,1000 do break; end; -n=100; -i=3; -t = {}; -a=nil +local n=100; +local i=3; +local t = {}; +local a=nil while not a do a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; end @@ -200,14 +202,14 @@ a={y=1} x = {a.y} assert(x[1] == 1) -function f(i) +local function f (i) while 1 do if i>0 then i=i-1; else return; end; end; end; -function g(i) +local function g(i) while 1 do if i>0 then i=i-1 else return end @@ -272,7 +274,7 @@ function g (a,b,c,d,e) if not (a>=b or c or d and e or nil) then return 0; else return 1; end; end -function h (a,b,c,d,e) +local function h (a,b,c,d,e) while (a>=b or c or (d and e) or nil) do return 1; end; return 0; end; @@ -300,7 +302,7 @@ do assert(a==2) end -function F(a) +local function F (a) assert(debug.getinfo(1, "n").name == 'F') return a,2,3 end @@ -393,6 +395,8 @@ for n = 1, level do if i % 60000 == 0 then print('+') end end end +IX = nil +_G.GLOB1 = nil ------------------------------------------------------------------ -- 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) end _G.x = nil -- declare x -function foo (a, ...) +_G.f = nil -- declare f +local function foo (a, ...) local x, y = coroutine.running() assert(x == f and y == false) -- next call should not corrupt coroutine (but must fail, @@ -67,10 +68,11 @@ assert(coroutine.status(f) == "dead") s, a = coroutine.resume(f, "xuxu") assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") +_G.f = nil -- yields in tail calls local function foo (i) return coroutine.yield(i) end -f = coroutine.wrap(function () +local f = coroutine.wrap(function () for i=1,10 do assert(foo(i) == _G.x) end @@ -79,8 +81,10 @@ end) for i=1,10 do _G.x = i; assert(f(i) == i) end _G.x = 'xuxu'; assert(f('xuxu') == 'a') +_G.x = nil + -- recursive -function pf (n, i) +local function pf (n, i) coroutine.yield(n) pf(n*i, i+1) end @@ -93,14 +97,14 @@ for i=1,10 do end -- sieve -function gen (n) +local function gen (n) return coroutine.wrap(function () for i=2,n do coroutine.yield(i) end end) end -function filter (p, g) +local function filter (p, g) return coroutine.wrap(function () while 1 do local n = g() @@ -221,14 +225,14 @@ do -- versus pcall in coroutines local X = false local Y = false - function foo () + local function foo () local x = func2close(function (self, err) Y = debug.getinfo(2) X = err end) error(43) end - co = coroutine.create(function () return pcall(foo) end) + local co = coroutine.create(function () return pcall(foo) end) local st1, st2, err = coroutine.resume(co) assert(st1 and not st2 and err == 43) assert(X == 43 and Y.what == "C") @@ -275,7 +279,7 @@ end -- yielding across C boundaries -co = coroutine.wrap(function() +local co = coroutine.wrap(function() assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) assert(coroutine.isyieldable()) coroutine.yield(20) @@ -303,15 +307,15 @@ local r1, r2, v = f1(nil) assert(r1 and not r2 and v[1] == (10 + 1)*10/2) -function f (a, b) a = coroutine.yield(a); error{a + b} end -function g(x) return x[1]*2 end +local function f (a, b) a = coroutine.yield(a); error{a + b} end +local function g(x) return x[1]*2 end co = coroutine.wrap(function () coroutine.yield(xpcall(f, g, 10, 20)) end) assert(co() == 10) -r, msg = co(100) +local r, msg = co(100) assert(not r and msg == 240) @@ -373,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead") a,b = coroutine.resume(x) assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") +goo = nil -- co-routines x for loop -function all (a, n, k) +local function all (a, n, k) if k == 0 then coroutine.yield(a) else for i=1,n do @@ -415,7 +420,7 @@ assert(f() == 43 and f() == 53) -- old bug: attempt to resume itself -function co_func (current_co) +local function co_func (current_co) assert(coroutine.running() == current_co) assert(coroutine.resume(current_co) == false) coroutine.yield(10, 20) @@ -491,15 +496,16 @@ a = nil -- access to locals of erroneous coroutines local x = coroutine.create (function () local a = 10 - _G.f = function () a=a+1; return a end + _G.F = function () a=a+1; return a end error('x') end) assert(not coroutine.resume(x)) -- overwrite previous position of local `a' assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) -assert(_G.f() == 11) -assert(_G.f() == 12) +assert(_G.F() == 11) +assert(_G.F() == 12) +_G.F = nil if not T then @@ -510,7 +516,7 @@ else local turn - function fact (t, x) + local function fact (t, x) assert(turn == t) if x == 0 then return 1 else return x*fact(t, x-1) @@ -579,6 +585,7 @@ else _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) assert(co() == 10) + _G.X = nil -- testing yields in count hook co = coroutine.wrap(function () @@ -656,6 +663,8 @@ else assert(X == 'a a a' and Y == 'OK') + X, Y = nil + -- resuming running coroutine C = coroutine.create(function () @@ -701,7 +710,7 @@ else X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; return 'ok']])) - t = table.pack(T.testC(state, [[ + local t = table.pack(T.testC(state, [[ rawgeti R 1 # get main thread pushstring 'XX' getglobal X # get function for body @@ -730,13 +739,13 @@ end -- leaving a pending coroutine open -_X = coroutine.wrap(function () +_G.TO_SURVIVE = coroutine.wrap(function () local a = 10 local x = function () a = a+1 end coroutine.yield() end) -_X() +_G.TO_SURVIVE() if not _soft then @@ -935,7 +944,7 @@ assert(run(function () do local _ENV = _ENV f = function () AAA = BBB + 1; return AAA end end -g = new(10); g.k.BBB = 10; +local g = new(10); g.k.BBB = 10; debug.setupvalue(f, 1, g) assert(run(f, {"idx", "nidx", "idx"}) == 11) 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) -- testing yields with continuations +local y + co = coroutine.wrap(function (...) return T.testC([[ # initial function yieldk 1 2 @@ -1127,6 +1138,9 @@ assert(x == "YIELD" and y == 4) assert(not pcall(co)) -- coroutine should be dead +_G.ctx = nil +_G.status = nil + -- bug in nCcalls 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 assert(not debug.gethook()) local testline = 19 -- line where 'test' is defined -function test (s, l, p) -- this must be line 19 +local function test (s, l, p) -- this must be line 19 collectgarbage() -- avoid gc during trace local function f (event, line) assert(event == 'line') @@ -50,7 +50,7 @@ end -- test file and string names truncation -a = "function f () end" +local a = "function f () end" local function dostring (s, x) return load(s, x)() end dostring(a) assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) @@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500))) assert(string.find(debug.getinfo(f).short_src, "^x*$")) dostring(a, "=") assert(debug.getinfo(f).short_src == "") -a = nil; f = nil; +_G.a = nil; _G.f = nil; +_G[string.rep("p", 400)] = nil repeat @@ -120,6 +121,7 @@ else end ]], {2,3,4,7}) + test([[ local function foo() end @@ -128,6 +130,7 @@ A = 1 A = 2 A = 3 ]], {2, 3, 2, 4, 5, 6}) +_G.A = nil test([[-- @@ -175,6 +178,8 @@ end test([[for i=1,4 do a=1 end]], {1,1,1,1}) +_G.a = nil + do -- testing line info/trace with large gaps in source @@ -194,6 +199,7 @@ do -- testing line info/trace with large gaps in source end end end +_G.a = nil do -- testing active lines @@ -287,7 +293,6 @@ foo(200, 3, 4) local a = {} for i = 1, (_soft and 100 or 1000) do a[i] = i end foo(table.unpack(a)) -a = nil @@ -307,13 +312,14 @@ do -- test hook presence in debug info debug.sethook() assert(count == 4) end +_ENV.a = nil -- hook table has weak keys assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') -a = {}; L = nil +a = {}; local L = nil local glob = 1 local oldglob = glob debug.sethook(function (e,l) @@ -354,7 +360,7 @@ function foo() end; foo() -- set L -- check line counting inside strings and empty lines -_ = 'alo\ +local _ = 'alo\ alo' .. [[ ]] @@ -403,6 +409,7 @@ function g(a,b) return (a+1) + f() end assert(g(0,0) == 30) +_G.f, _G.g = nil debug.sethook(nil); assert(not debug.gethook()) @@ -446,7 +453,7 @@ local function collectlocals (level) end -X = nil +local X = nil a = {} function a:f (a, b, ...) local arg = {...}; local c = 13 end debug.sethook(function (e) @@ -469,6 +476,7 @@ a:f(1,2,3,4,5) assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) assert(XX == 12) assert(not debug.gethook()) +_G.XX = nil -- testing access to local variables in return hook (bug in 5.2) @@ -593,6 +601,7 @@ end debug.sethook() +local g, g1 -- tests for tail calls local function f (x) @@ -638,7 +647,7 @@ h(false) debug.sethook() assert(b == 2) -- two tail calls -lim = _soft and 3000 or 30000 +local lim = _soft and 3000 or 30000 local function foo (x) if x==0 then assert(debug.getinfo(2).what == "main") @@ -940,7 +949,7 @@ end print("testing debug functions on chunk without debug info") -prog = [[-- program to be loaded without debug information (strip) +local prog = [[-- program to be loaded without debug information (strip) local debug = require'debug' local a = 12 -- a local variable 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") checkmessage("a = {} < 1", "attempt to compare") checkmessage("a = {} <= 1", "attempt to compare") -checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") -checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") +checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") -assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) -checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") -checkmessage("a=(1)..{}", "a table value") +assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) +checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") +checkmessage("aaa=(1)..{}", "a table value") + +_G.aaa, _G.bbbb = nil -- calls checkmessage("local a; a(13)", "local 'a'") @@ -134,12 +136,13 @@ checkmessage([[ -- tail calls checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") -checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") + +checkmessage("aaa = #print", "length of a function value") +checkmessage("aaa = #3", "length of a number value") -checkmessage("a = #print", "length of a function value") -checkmessage("a = #3", "length of a number value") +_G.aaa = nil -aaa = nil checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") 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'") checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") -checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") +checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") -checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") +checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") checkmessage("aaa={}; x=-aaa", "global 'aaa'") -- short circuit -checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", "local 'bbbb'") -checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", + "local 'bbbb'") checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") 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") checkmessage("return ~-3.009", "has no integer representation") checkmessage("return 3.009 & 1", "has no integer representation") checkmessage("return 34 >> {}", "table value") -checkmessage("a = 24 // 0", "divide by zero") -checkmessage("a = 1 % 0", "'n%0'") +checkmessage("aaa = 24 // 0", "divide by zero") +checkmessage("aaa = 1 % 0", "'n%0'") -- type error for an object which is neither in an upvalue nor a register. @@ -269,13 +273,13 @@ end -- tests for field accesses after RK limit local t = {} for i = 1, 1000 do - t[i] = "a = x" .. i + t[i] = "aaa = x" .. i end local s = table.concat(t, "; ") t = nil -checkmessage(s.."; a = bbb + 1", "global 'bbb'") -checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") -checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") +checkmessage(s.."; aaa = bbb + 1", "global 'bbb'") +checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'") +checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'") checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") checkmessage([[aaa=9 @@ -324,14 +328,17 @@ main() ]], "global 'NoSuchName'") print'+' -a = {}; setmetatable(a, {__index = string}) -checkmessage("a:sub()", "bad self") +aaa = {}; setmetatable(aaa, {__index = string}) +checkmessage("aaa:sub()", "bad self") checkmessage("string.sub('a', {})", "#2") checkmessage("('a'):sub{}", "#1") checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") +_G.aaa = nil + + -- tests for errors in coroutines local function f (n) @@ -349,7 +356,7 @@ checkerr("yield across", f) -- testing size of 'source' info; size of buffer for that info is -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. -idsize = 60 - 1 +local idsize = 60 - 1 local function checksize (source) -- syntax error local _, msg = load("x", source) @@ -411,13 +418,14 @@ x local p = [[ function g() f() end - function f(x) error('a', X) end + function f(x) error('a', XX) end g() ]] -X=3;lineerror((p), 3) -X=0;lineerror((p), false) -X=1;lineerror((p), 2) -X=2;lineerror((p), 1) +XX=3;lineerror((p), 3) +XX=0;lineerror((p), false) +XX=1;lineerror((p), 2) +XX=2;lineerror((p), 1) +_G.XX, _G.g, _G.f = nil lineerror([[ @@ -449,11 +457,11 @@ if not _soft then -- several tests that exaust the Lua stack collectgarbage() print"testing stack overflow" - C = 0 + local C = 0 -- get line where stack overflow will happen local l = debug.getinfo(1, "l").currentline + 1 local function auxy () C=C+1; auxy() end -- produce a stack overflow - function y () + function YY () collectgarbage("stop") -- avoid running finalizers without stack space auxy() collectgarbage("restart") @@ -465,9 +473,11 @@ if not _soft then return (string.find(m, "stack overflow")) end -- repeated stack overflows (to check stack recovery) - assert(checkstackmessage(doit('y()'))) - assert(checkstackmessage(doit('y()'))) - assert(checkstackmessage(doit('y()'))) + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + + _G.YY = nil -- error lines in stack overflow @@ -561,7 +571,7 @@ do end -- xpcall with arguments -a, b, c = xpcall(string.find, error, "alo", "al") +local a, b, c = xpcall(string.find, error, "alo", "al") assert(a and b == 1 and c == 2) a, b, c = xpcall(string.find, function (x) return {} end, true, "al") assert(not a and type(b) == "table" and c == nil) @@ -581,11 +591,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1) -- test 255 as first char in a chunk checksyntax("\255a = 1", "", "<\\255>", 1) -doit('I = load("a=9+"); a=3') -assert(a==3 and not I) +doit('I = load("a=9+"); aaa=3') +assert(_G.aaa==3 and not _G.I) +_G.I,_G.aaa = nil print('+') -lim = 1000 +local lim = 1000 if _soft then lim = 100 end for i=1,lim do 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) assert(_G.X == 20) +_G.X, _G.B = nil + + print'+' 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)))() assert(_G.X == 4) load((io.lines(file, 3)))() assert(_G.X == 8) +_G.X = nil print('+') local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" io.output(file) -assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) +assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1))) io.close() assert(loadfile(file))() -assert(x1 == x2) +assert(x1 == _G.X2) +_G.X2 = nil print('+') assert(os.remove(file)) 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 end a:test() - + _G.temp = nil end @@ -134,7 +134,7 @@ do local f = function () end end print("functions with errors") -prog = [[ +local prog = [[ do a = 10; function foo(x,y) @@ -153,22 +153,25 @@ do end end end +rawset(_G, "a", nil) +_G.x = nil -foo = nil -print('long strings') -x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" -assert(string.len(x)==80) -s = '' -k = math.min(300, (math.maxinteger // 80) // 2) -for n = 1, k do s = s..x; j=tostring(n) end -assert(string.len(s) == k*80) -s = string.sub(s, 1, 10000) -s, i = string.gsub(s, '(%d%d%d%d)', '') -assert(i==10000 // 4) -s = nil -x = nil - -assert(_G["while"] == 234) +do + foo = nil + print('long strings') + local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + assert(string.len(x)==80) + local s = '' + local k = math.min(300, (math.maxinteger // 80) // 2) + for n = 1, k do s = s..x; local j=tostring(n) end + assert(string.len(s) == k*80) + s = string.sub(s, 1, 10000) + local s, i = string.gsub(s, '(%d%d%d%d)', '') + assert(i==10000 // 4) + + assert(_G["while"] == 234) + _G["while"] = nil +end -- @@ -227,8 +230,8 @@ end print("clearing tables") -lim = 15 -a = {} +local lim = 15 +local a = {} -- fill a with `collectable' indices for i=1,lim do a[{}] = i end b = {} @@ -552,6 +555,7 @@ do for i=1,1000 do _ENV.a = {} end -- no collection during the loop until gcinfo() > 2 * x collectgarbage"restart" + _ENV.a = nil end 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 dostring("x \v\f = \t\r 'a\0a' \v\f\f") assert(x == 'a\0a' and string.len(x) == 3) +_G.x = nil -- escape sequences assert('\n\"\'\\' == [[ @@ -129,16 +130,16 @@ end -- long variable names -var1 = string.rep('a', 15000) .. '1' -var2 = string.rep('a', 15000) .. '2' -prog = string.format([[ +local var1 = string.rep('a', 15000) .. '1' +local var2 = string.rep('a', 15000) .. '2' +local prog = string.format([[ %s = 5 %s = %s + 1 return function () return %s - %s end ]], var1, var2, var1, var1, var2) local f = dostring(prog) assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) -var1, var2, f = nil +_G[var1], _G[var2] = nil print('+') -- escapes -- @@ -150,13 +151,13 @@ assert([[ $debug]] == "\n $debug") assert([[ [ ]] ~= [[ ] ]]) -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) prog = [=[ print('+') -a1 = [["this is a 'string' with several 'quotes'"]] -a2 = "'quotes'" +local a1 = [["this is a 'string' with several 'quotes'"]] +local a2 = "'quotes'" assert(string.find(a1, a2) == 34) print('+') @@ -164,12 +165,13 @@ print('+') a1 = [==[temp = [[an arbitrary value]]; ]==] assert(load(a1))() assert(temp == 'an arbitrary value') +_G.temp = nil -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) print('+') -a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 +local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 @@ -199,13 +201,11 @@ x = 1 ]=] print('+') -x = nil +_G.x = nil dostring(prog) assert(x) +_G.x = nil -prog = nil -a = nil -b = nil do -- reuse of long strings @@ -234,8 +234,8 @@ end -- testing line ends prog = [[ -a = 1 -- a comment -b = 2 +local a = 1 -- a comment +local b = 2 x = [=[ @@ -252,10 +252,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do assert(dostring(prog) == nn) assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") end +_G.x, _G.y = nil -- testing comments and strings with long brackets -a = [==[]=]==] +local a = [==[]=]==] assert(a == "]=") 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 f = nil local f -x = 1 +local x = 1 a = nil load('local a = {}')() @@ -152,7 +152,7 @@ local dummy local _ENV = (function (...) return ... end)(_G, dummy) -- { do local _ENV = {assert=assert}; assert(true) end -mt = {_G = _G} +local mt = {_G = _G} local foo,x A = false -- "declare" A do local _ENV = mt @@ -174,6 +174,8 @@ do local _ENV = {assert=assert, A=10}; end assert(x==20) +A = nil + do -- constants local a, b, c = 10, 20, 30 @@ -711,7 +713,7 @@ if rawget(_G, "T") then collectgarbage(); collectgarbage() - m = T.totalmem() + local m = T.totalmem() collectgarbage("stop") -- 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") RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") -prompt = "alo" +local prompt = "alo" prepfile[[ -- a = 2 ]] @@ -390,7 +390,7 @@ NoRun("error object is a table value", [[lua %s]], prog) -- chunk broken in many lines -s = [=[ -- +local s = [=[ -- function f ( x ) local a = [[ 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 local msgf2i = "number.* has no integer representation" -- float equality -function eq (a,b,limit) +local function eq (a,b,limit) if not limit then if floatbits >= 50 then limit = 1E-11 else limit = 1E-5 @@ -62,7 +62,7 @@ end -- equality with types -function eqT (a,b) +local function eqT (a,b) return a == b and math.type(a) == math.type(b) end @@ -83,7 +83,7 @@ end do local x = -1 local mz = 0/x -- minus zero - t = {[0] = 10, 20, 30, 40, 50} + local t = {[0] = 10, 20, 30, 40, 50} assert(t[mz] == t[0] and t[-0] == t[0]) end 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 -- size tests for vararg lim = 35 -function foo (n, ...) +local function foo (n, ...) local arg = {...} check(arg, n, 0) 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, ...) end -function f(s, p) +local function f (s, p) local i,e = string.find(s, p) if i then return string.sub(s, i, e) end end -a,b = string.find('', '') -- empty patterns are tricky +local a,b = string.find('', '') -- empty patterns are tricky assert(a == 1 and b == 0); a,b = string.find('alo', '') assert(a == 1 and b == 0) @@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo") print('+') -function f1(s, p) +local function f1 (s, p) p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (tonumber(s)+1) end) @@ -113,7 +113,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); assert(string.len(abc) == 256) -function strset (p) +local function strset (p) local res = {s=''} string.gsub(abc, p, function (c) res.s = res.s .. c end) return res.s @@ -147,7 +147,7 @@ assert(string.gsub(' assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') -t = "abç d" +local t = "abç d" a, b = string.gsub(t, '(.)', '%1@') assert('@'..a == string.gsub(t, '', '@') and b == 5) a, b = string.gsub('abçd', '(.)', '%0@', 2) @@ -184,6 +184,7 @@ do local function setglobal (n,v) rawset(_G, n, v) end string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) assert(_G.a=="roberto" and _G.roberto=="a") + _G.a = nil; _G.roberto = nil end function f(a,b) return string.gsub(a,'.',b) end @@ -195,20 +196,21 @@ assert(string.gsub("alo $a='x'$ novamente $return a$", "$([^$]*)%$", dostring) == "alo novamente x") -x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", +local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", "$([^$]*)%$", dostring) assert(x == ' assim vai para ALO') - -t = {} -s = 'a alo jose joao' -r = string.gsub(s, '()(%w+)()', function (a,w,b) - assert(string.len(w) == b-a); - t[a] = b-a; - end) +_G.a, _G.x = nil + +local t = {} +local s = 'a alo jose joao' +local r = string.gsub(s, '()(%w+)()', function (a,w,b) + assert(string.len(w) == b-a); + t[a] = b-a; + end) assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) -function isbalanced (s) +local function isbalanced (s) return not string.find(string.gsub(s, "%b()", ""), "[()]") end @@ -251,7 +253,7 @@ if not _soft then end -- recursive nest of gsubs -function rev (s) +local function rev (s) return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) end 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 checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) local x,y,z,a,n -a = {}; lim = _soft and 200 or 2000 +a = {}; local lim = _soft and 200 or 2000 for i=1, lim do a[i]=i end assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) x = unpack(a) @@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", table.sort(a) check(a) -function perm (s, n) +local function perm (s, n) n = n or #s if n == 1 then local t = {unpack(s)} @@ -248,7 +248,7 @@ perm{1,2,3,3,5} perm{1,2,3,4,5,6} perm{2,2,3,3,5,6} -function timesort (a, n, func, msg, pre) +local function timesort (a, n, func, msg, pre) local x = os.clock() table.sort(a, func) x = (os.clock() - x) * 1000 @@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre) check(a, func) end -limit = 50000 +local limit = 50000 if _soft then limit = 5000 end a = {} @@ -274,7 +274,7 @@ for i=1,limit do a[i] = math.random() end -x = os.clock(); i=0 +local x = os.clock(); local i = 0 table.sort(a, function(x,y) i=i+1; return y64k)" -- template to create a very big test file -prog = [[$ +local prog = [[$ local a,b @@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end assert(b:xxx(10, 12) == 22) -- pushself with non-constant index b["xxx"] = undef -s = 0; n=0 +local s = 0; local n=0 for a,b in pairs(b) do s=s+b; n=n+1 end -- with 32-bit floats, exact value of 's' depends on summation order assert(81800000.0 < s and s < 81860000 and n == 70001) @@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001) a = nil; b = nil print'+' -function f(x) b=x end +local function f(x) b=x end a = f{$3$} or 10 @@ -118,7 +118,7 @@ local function sig (x) return (x % 2 == 0) and '' or '-' end -F = { +local F = { function () -- $1$ for i=10,50009 do io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') @@ -138,14 +138,14 @@ function () -- $3$ end, } -file = os.tmpname() +local file = os.tmpname() io.output(file) for s in string.gmatch(prog, "$([^$]+)") do local n = tonumber(s) if not n then io.write(s) else F[n]() end end io.close() -result = dofile(file) +local result = dofile(file) assert(os.remove(file)) print'OK' return result