aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testes/memerr.lua42
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)
42T.alloccount() -- remove limit 42T.alloccount() -- remove limit
43 43
44 44
45-- preallocate stack space
46local 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.
49local function testbytes (s, f) 53local 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
91end 97end
92 98
93 99
94local function testamem (s, f) 100local 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}
97end 104end
98 105
99 106
100-- doing nothing 107local b = testamem("function call", function () return 10 end)
101b = testamem("doing nothing", function () return 10 end) 108assert(b.res == 10 and b.aloc == 0)
102assert(b == 10)
103
104-- testing memory errors when creating a new state
105 109
106testamem("state creation", function () 110testamem("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)
122end) 126end)
123 127
128do -- 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
139end
124 140
125-- testing to-be-closed variables 141-- testing to-be-closed variables
126testamem("to-be-closed variables", function() 142testamem("to-be-closed variables", function()
@@ -160,6 +176,14 @@ testamem("running code on new thread", function ()
160end) 176end)
161 177
162 178
179do -- external strings
180 local str = string.rep("a", 100)
181 testamem("creating external strings", function ()
182 return T.externstr(str)
183 end)
184end
185
186
163-- testing memory x compiler 187-- testing memory x compiler
164 188
165testamem("loadstring", function () 189testamem("loadstring", function ()