Two new memory tests

For external strings and for vararg tables.
This commit is contained in:
Roberto I
2025-10-12 15:13:28 -03:00
parent 7a92f3f99a
commit 30a7b93439

View File

@@ -42,8 +42,12 @@ checkerr(MEMERRMSG, f)
T.alloccount() -- remove limit T.alloccount() -- remove limit
-- preallocate stack space
local function deep (n) if n > 0 then deep(n - 1) end end
-- test memory errors; increase limit for maximum memory by steps, -- test memory errors; increase limit for maximum memory by steps,
-- o that we get memory errors in all allocations of a given -- so that we get memory errors in all allocations of a given
-- task, until there is enough memory to complete the task without -- task, until there is enough memory to complete the task without
-- errors. -- errors.
local function testbytes (s, f) local function testbytes (s, f)
@@ -53,6 +57,7 @@ local function testbytes (s, f)
local a,b = nil local a,b = nil
while true do while true do
collectgarbage(); collectgarbage() collectgarbage(); collectgarbage()
deep(4)
T.totalmem(M) T.totalmem(M)
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
T.totalmem(0) -- remove limit T.totalmem(0) -- remove limit
@@ -77,6 +82,7 @@ local function testalloc (s, f)
local a,b = nil local a,b = nil
while true do while true do
collectgarbage(); collectgarbage() collectgarbage(); collectgarbage()
deep(4)
T.alloccount(M) T.alloccount(M)
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
T.alloccount() -- remove limit T.alloccount() -- remove limit
@@ -87,21 +93,19 @@ local function testalloc (s, f)
M = M + 1 -- increase allocation limit M = M + 1 -- increase allocation limit
end end
print(string.format("minimum allocations for %s: %d allocations", s, M)) print(string.format("minimum allocations for %s: %d allocations", s, M))
return a return M
end end
local function testamem (s, f) local function testamem (s, f)
testalloc(s, f) local aloc = testalloc(s, f)
return testbytes(s, f) local res = testbytes(s, f)
return {aloc = aloc, res = res}
end end
-- doing nothing local b = testamem("function call", function () return 10 end)
b = testamem("doing nothing", function () return 10 end) assert(b.res == 10 and b.aloc == 0)
assert(b == 10)
-- testing memory errors when creating a new state
testamem("state creation", function () testamem("state creation", function ()
local st = T.newstate() local st = T.newstate()
@@ -121,6 +125,18 @@ testamem("coroutine creation", function()
return coroutine.create(print) return coroutine.create(print)
end) end)
do -- vararg tables
local function pack (... | t) return t end
local b = testamem("vararg table", function ()
return pack(10, 20, 30, 40, "hello")
end)
assert(b.aloc == 3) -- new table uses three memory blocks
-- table optimized away
local function sel (n, ...|arg) return arg[n] + arg.n end
local b = testamem("optimized vararg table",
function () return sel(2.0, 20, 30) end)
assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case
end
-- testing to-be-closed variables -- testing to-be-closed variables
testamem("to-be-closed variables", function() testamem("to-be-closed variables", function()
@@ -160,6 +176,14 @@ testamem("running code on new thread", function ()
end) end)
do -- external strings
local str = string.rep("a", 100)
testamem("creating external strings", function ()
return T.externstr(str)
end)
end
-- testing memory x compiler -- testing memory x compiler
testamem("loadstring", function () testamem("loadstring", function ()