print("Starting GC stack stress test...")
local function deep_tables(depth)
local t = { depth = depth, data = {} }
for i = 1, 10 do
t.data[i] = { x = i, y = i * 2 }
end
if depth > 0 then
for i = 1, 100 do
local garbage = { a = i, b = {} }
end
local result = deep_tables(depth - 1)
if t.depth ~= depth then
error("Table corrupted at depth " .. depth)
end
return result + t.depth
end
return t.depth
end
local result = deep_tables(50)
print("Deep tables result:", result)
local function test_metatables()
local mt = {
__index = function(t, k)
for i = 1, 50 do
local g = { x = i }
end
return k .. "_value"
end
}
for i = 1, 500 do
local t = setmetatable({}, mt)
local v = t.test if v ~= "test_value" then
error("Metatable broken at iteration " .. i)
end
end
end
test_metatables()
print("Metatable test done")
local function test_ipairs_gc()
local arr = {}
for i = 1, 100 do
arr[i] = { id = i, name = "item" .. i }
end
local count = 0
for i, v in ipairs(arr) do
for j = 1, 20 do
local g = { x = j }
end
if v.id ~= i then
error("Array corrupted at index " .. i)
end
count = count + 1
end
if count ~= 100 then
error("Expected 100 iterations, got " .. count)
end
end
test_ipairs_gc()
print("ipairs GC test done")
local function multi_table_stack()
for i = 1, 200 do
local t1 = { a = 1 }
local t2 = { b = 2 }
local t3 = { c = 3 }
local t4 = { d = 4 }
for j = 1, 50 do
local g = { garbage = j }
end
if t1.a ~= 1 or t2.b ~= 2 or t3.c ~= 3 or t4.d ~= 4 then
error("Multi-table corruption at iteration " .. i)
end
end
end
multi_table_stack()
print("Multi-table stack test done")
print("PASS - All GC stack stress tests completed")