mirror of https://github.com/rusefi/lua.git
Improvements in 'testes/cstack.lua'
- tests show progress in real time, so that we can see maximum stack levels even if test crashes. - new test for recursion continuing into message handler.
This commit is contained in:
parent
2b8b53864c
commit
7d0f41df41
|
@ -13,20 +13,40 @@ local function checkerror (msg, f, ...)
|
|||
assert(not s and string.find(err, msg))
|
||||
end
|
||||
|
||||
local count
|
||||
local back = string.rep("\b", 8)
|
||||
local function progress ()
|
||||
count = count + 1
|
||||
local n = string.format("%-8d", count)
|
||||
io.stderr:write(back, n)
|
||||
end
|
||||
|
||||
do -- simple recursion
|
||||
local count = 0
|
||||
|
||||
do print("testing simple recursion:")
|
||||
count = 0
|
||||
local function foo ()
|
||||
count = count + 1
|
||||
progress()
|
||||
foo()
|
||||
end
|
||||
checkerror("stack overflow", foo)
|
||||
print(" maximum recursion: " .. count)
|
||||
print("\tfinal count: ", count)
|
||||
end
|
||||
|
||||
|
||||
do print("testing stack overflow in message handling")
|
||||
count = 0
|
||||
local function loop (x, y, z)
|
||||
progress()
|
||||
return 1 + loop(x, y, z)
|
||||
end
|
||||
local res, msg = xpcall(loop, loop)
|
||||
assert(msg == "error in error handling")
|
||||
print("\tfinal count: ", count)
|
||||
end
|
||||
|
||||
|
||||
-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
|
||||
do
|
||||
do print("testing recursion inside pattern matching")
|
||||
local function f (size)
|
||||
local s = string.rep("a", size)
|
||||
local p = string.rep(".?", size)
|
||||
|
@ -38,25 +58,25 @@ do
|
|||
end
|
||||
|
||||
|
||||
-- testing stack-overflow in recursive 'gsub'
|
||||
do
|
||||
local count = 0
|
||||
do print("testing stack-overflow in recursive 'gsub'")
|
||||
count = 0
|
||||
local function foo ()
|
||||
count = count + 1
|
||||
progress()
|
||||
string.gsub("a", ".", foo)
|
||||
end
|
||||
checkerror("stack overflow", foo)
|
||||
print(" maximum 'gsub' nest (calls): " .. count)
|
||||
print("\tfinal count: ", count)
|
||||
|
||||
-- can be done with metamethods, too
|
||||
print("testing stack-overflow in recursive 'gsub' with metatables")
|
||||
count = 0
|
||||
local t = setmetatable({}, {__index = foo})
|
||||
foo = function ()
|
||||
count = count + 1
|
||||
progress(count)
|
||||
string.gsub("a", ".", t)
|
||||
end
|
||||
checkerror("stack overflow", foo)
|
||||
print(" maximum 'gsub' nest (metamethods): " .. count)
|
||||
print("\tfinal count: ", count)
|
||||
end
|
||||
|
||||
print'OK'
|
||||
|
|
Loading…
Reference in New Issue