diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-03 11:34:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-03 11:34:32 -0300 |
| commit | 7d0f41df41e9c513e7282356541b54beaf9ed20d (patch) | |
| tree | d06d36a05a84e602d47ebafda3aaf67a797f05db | |
| parent | 2b8b53864c6b8655aa7198699884075b3e2f15fa (diff) | |
| download | lua-7d0f41df41e9c513e7282356541b54beaf9ed20d.tar.gz lua-7d0f41df41e9c513e7282356541b54beaf9ed20d.tar.bz2 lua-7d0f41df41e9c513e7282356541b54beaf9ed20d.zip | |
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.
| -rw-r--r-- | testes/cstack.lua | 44 |
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)) |
| 14 | end | 14 | end |
| 15 | 15 | ||
| 16 | local count | ||
| 17 | local back = string.rep("\b", 8) | ||
| 18 | local function progress () | ||
| 19 | count = count + 1 | ||
| 20 | local n = string.format("%-8d", count) | ||
| 21 | io.stderr:write(back, n) | ||
| 22 | end | ||
| 23 | |||
| 16 | 24 | ||
| 17 | do -- simple recursion | 25 | do 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) |
| 33 | end | ||
| 34 | |||
| 35 | |||
| 36 | do 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) | ||
| 25 | end | 45 | end |
| 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) |
| 29 | do | 49 | do 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 | |||
| 38 | end | 58 | end |
| 39 | 59 | ||
| 40 | 60 | ||
| 41 | -- testing stack-overflow in recursive 'gsub' | 61 | do print("testing stack-overflow in recursive 'gsub'") |
| 42 | do | 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) |
| 60 | end | 80 | end |
| 61 | 81 | ||
| 62 | print'OK' | 82 | print'OK' |
