summaryrefslogtreecommitdiff
path: root/testes/nextvar.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-14 15:46:58 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-14 15:46:58 -0300
commit52c86797608f1bf927be5bab1e9b97b7d35bdf2c (patch)
tree41165b997293675eeb2a40bffe8417939e76c95e /testes/nextvar.lua
parent849b2ecbd28793408d21ebd734525ab56ae5ca1e (diff)
downloadlua-github-52c86797608f1bf927be5bab1e9b97b7d35bdf2c.tar.gz
Fixed bug of keys removed from tables vs 'next'
Fixed the bug that a key removed from a table might not be found again by 'next'. (This is needed to allow keys to be removed during a traversal.) This bug was introduced in commit 73ec04fc.
Diffstat (limited to 'testes/nextvar.lua')
-rw-r--r--testes/nextvar.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/testes/nextvar.lua b/testes/nextvar.lua
index a16d557b..29cb05d5 100644
--- a/testes/nextvar.lua
+++ b/testes/nextvar.lua
@@ -359,6 +359,38 @@ end
assert(n == 5)
+do
+ print("testing next x GC of deleted keys")
+ -- bug in 5.4.1
+ local co = coroutine.wrap(function (t)
+ for k, v in pairs(t) do
+ local k1 = next(t) -- all previous keys were deleted
+ assert(k == k1) -- current key is the first in the table
+ t[k] = nil
+ local expected = (type(k) == "table" and k[1] or
+ type(k) == "function" and k() or
+ string.sub(k, 1, 1))
+ assert(expected == v)
+ coroutine.yield(v)
+ end
+ end)
+ local t = {}
+ t[{1}] = 1 -- add several unanchored, collectable keys
+ t[{2}] = 2
+ t[string.rep("a", 50)] = "a" -- long string
+ t[string.rep("b", 50)] = "b"
+ t[{3}] = 3
+ t[string.rep("c", 10)] = "c" -- short string
+ t[function () return 10 end] = 10
+ local count = 7
+ while co(t) do
+ collectgarbage("collect") -- collect dead keys
+ count = count - 1
+ end
+ assert(count == 0 and next(t) == nil) -- traversed the whole table
+end
+
+
local function test (a)
assert(not pcall(table.insert, a, 2, 20));
table.insert(a, 10); table.insert(a, 2, 20);