aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testes/cstack.lua44
1 files changed, 32 insertions, 12 deletions
diff --git a/testes/cstack.lua b/testes/cstack.lua
index 9e5bbaef..3cb1e4ce 100644
--- a/testes/cstack.lua
+++ b/testes/cstack.lua
@@ -13,20 +13,40 @@ local function checkerror (msg, f, ...)
13 assert(not s and string.find(err, msg)) 13 assert(not s and string.find(err, msg))
14end 14end
15 15
16local count
17local back = string.rep("\b", 8)
18local function progress ()
19 count = count + 1
20 local n = string.format("%-8d", count)
21 io.stderr:write(back, n)
22end
23
16 24
17do -- simple recursion 25do print("testing simple recursion:")
18 local count = 0 26 count = 0
19 local function foo () 27 local function foo ()
20 count = count + 1 28 progress()
21 foo() 29 foo()
22 end 30 end
23 checkerror("stack overflow", foo) 31 checkerror("stack overflow", foo)
24 print(" maximum recursion: " .. count) 32 print("\tfinal count: ", count)
33end
34
35
36do print("testing stack overflow in message handling")
37 count = 0
38 local function loop (x, y, z)
39 progress()
40 return 1 + loop(x, y, z)
41 end
42 local res, msg = xpcall(loop, loop)
43 assert(msg == "error in error handling")
44 print("\tfinal count: ", count)
25end 45end
26 46
27 47
28-- bug since 2.5 (C-stack overflow in recursion inside pattern matching) 48-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
29do 49do print("testing recursion inside pattern matching")
30 local function f (size) 50 local function f (size)
31 local s = string.rep("a", size) 51 local s = string.rep("a", size)
32 local p = string.rep(".?", size) 52 local p = string.rep(".?", size)
@@ -38,25 +58,25 @@ do
38end 58end
39 59
40 60
41-- testing stack-overflow in recursive 'gsub' 61do print("testing stack-overflow in recursive 'gsub'")
42do 62 count = 0
43 local count = 0
44 local function foo () 63 local function foo ()
45 count = count + 1 64 progress()
46 string.gsub("a", ".", foo) 65 string.gsub("a", ".", foo)
47 end 66 end
48 checkerror("stack overflow", foo) 67 checkerror("stack overflow", foo)
49 print(" maximum 'gsub' nest (calls): " .. count) 68 print("\tfinal count: ", count)
50 69
51 -- can be done with metamethods, too 70 print("testing stack-overflow in recursive 'gsub' with metatables")
52 count = 0 71 count = 0
53 local t = setmetatable({}, {__index = foo}) 72 local t = setmetatable({}, {__index = foo})
54 foo = function () 73 foo = function ()
55 count = count + 1 74 count = count + 1
75 progress(count)
56 string.gsub("a", ".", t) 76 string.gsub("a", ".", t)
57 end 77 end
58 checkerror("stack overflow", foo) 78 checkerror("stack overflow", foo)
59 print(" maximum 'gsub' nest (metamethods): " .. count) 79 print("\tfinal count: ", count)
60end 80end
61 81
62print'OK' 82print'OK'