diff options
| -rw-r--r-- | testes/memerr.lua | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/testes/memerr.lua b/testes/memerr.lua index 77cb47cb..69d2ef85 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua | |||
| @@ -42,8 +42,12 @@ checkerr(MEMERRMSG, f) | |||
| 42 | T.alloccount() -- remove limit | 42 | T.alloccount() -- remove limit |
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | -- preallocate stack space | ||
| 46 | local function deep (n) if n > 0 then deep(n - 1) end end | ||
| 47 | |||
| 48 | |||
| 45 | -- test memory errors; increase limit for maximum memory by steps, | 49 | -- test memory errors; increase limit for maximum memory by steps, |
| 46 | -- o that we get memory errors in all allocations of a given | 50 | -- so that we get memory errors in all allocations of a given |
| 47 | -- task, until there is enough memory to complete the task without | 51 | -- task, until there is enough memory to complete the task without |
| 48 | -- errors. | 52 | -- errors. |
| 49 | local function testbytes (s, f) | 53 | local function testbytes (s, f) |
| @@ -53,6 +57,7 @@ local function testbytes (s, f) | |||
| 53 | local a,b = nil | 57 | local a,b = nil |
| 54 | while true do | 58 | while true do |
| 55 | collectgarbage(); collectgarbage() | 59 | collectgarbage(); collectgarbage() |
| 60 | deep(4) | ||
| 56 | T.totalmem(M) | 61 | T.totalmem(M) |
| 57 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | 62 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) |
| 58 | T.totalmem(0) -- remove limit | 63 | T.totalmem(0) -- remove limit |
| @@ -77,6 +82,7 @@ local function testalloc (s, f) | |||
| 77 | local a,b = nil | 82 | local a,b = nil |
| 78 | while true do | 83 | while true do |
| 79 | collectgarbage(); collectgarbage() | 84 | collectgarbage(); collectgarbage() |
| 85 | deep(4) | ||
| 80 | T.alloccount(M) | 86 | T.alloccount(M) |
| 81 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | 87 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) |
| 82 | T.alloccount() -- remove limit | 88 | T.alloccount() -- remove limit |
| @@ -87,21 +93,19 @@ local function testalloc (s, f) | |||
| 87 | M = M + 1 -- increase allocation limit | 93 | M = M + 1 -- increase allocation limit |
| 88 | end | 94 | end |
| 89 | print(string.format("minimum allocations for %s: %d allocations", s, M)) | 95 | print(string.format("minimum allocations for %s: %d allocations", s, M)) |
| 90 | return a | 96 | return M |
| 91 | end | 97 | end |
| 92 | 98 | ||
| 93 | 99 | ||
| 94 | local function testamem (s, f) | 100 | local function testamem (s, f) |
| 95 | testalloc(s, f) | 101 | local aloc = testalloc(s, f) |
| 96 | return testbytes(s, f) | 102 | local res = testbytes(s, f) |
| 103 | return {aloc = aloc, res = res} | ||
| 97 | end | 104 | end |
| 98 | 105 | ||
| 99 | 106 | ||
| 100 | -- doing nothing | 107 | local b = testamem("function call", function () return 10 end) |
| 101 | b = testamem("doing nothing", function () return 10 end) | 108 | assert(b.res == 10 and b.aloc == 0) |
| 102 | assert(b == 10) | ||
| 103 | |||
| 104 | -- testing memory errors when creating a new state | ||
| 105 | 109 | ||
| 106 | testamem("state creation", function () | 110 | testamem("state creation", function () |
| 107 | local st = T.newstate() | 111 | local st = T.newstate() |
| @@ -121,6 +125,18 @@ testamem("coroutine creation", function() | |||
| 121 | return coroutine.create(print) | 125 | return coroutine.create(print) |
| 122 | end) | 126 | end) |
| 123 | 127 | ||
| 128 | do -- vararg tables | ||
| 129 | local function pack (... | t) return t end | ||
| 130 | local b = testamem("vararg table", function () | ||
| 131 | return pack(10, 20, 30, 40, "hello") | ||
| 132 | end) | ||
| 133 | assert(b.aloc == 3) -- new table uses three memory blocks | ||
| 134 | -- table optimized away | ||
| 135 | local function sel (n, ...|arg) return arg[n] + arg.n end | ||
| 136 | local b = testamem("optimized vararg table", | ||
| 137 | function () return sel(2.0, 20, 30) end) | ||
| 138 | assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case | ||
| 139 | end | ||
| 124 | 140 | ||
| 125 | -- testing to-be-closed variables | 141 | -- testing to-be-closed variables |
| 126 | testamem("to-be-closed variables", function() | 142 | testamem("to-be-closed variables", function() |
| @@ -160,6 +176,14 @@ testamem("running code on new thread", function () | |||
| 160 | end) | 176 | end) |
| 161 | 177 | ||
| 162 | 178 | ||
| 179 | do -- external strings | ||
| 180 | local str = string.rep("a", 100) | ||
| 181 | testamem("creating external strings", function () | ||
| 182 | return T.externstr(str) | ||
| 183 | end) | ||
| 184 | end | ||
| 185 | |||
| 186 | |||
| 163 | -- testing memory x compiler | 187 | -- testing memory x compiler |
| 164 | 188 | ||
| 165 | testamem("loadstring", function () | 189 | testamem("loadstring", function () |
