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.
This commit is contained in:
@@ -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
|
||||
-- <close> versus pcall in coroutines
|
||||
local X = false
|
||||
local Y = false
|
||||
function foo ()
|
||||
local function foo ()
|
||||
local x <close> = 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 ()
|
||||
|
||||
Reference in New Issue
Block a user