interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / memerr.lua

309 lines · 7512 bytes Raw Blame History
1-- $Id: testes/memerr.lua $
2-- See Copyright Notice in file lua.h
3
4
5local function checkerr (msg, f, ...)
6 local stat, err = pcall(f, ...)
7 assert(not stat and string.find(err, msg))
8end
9
10if T==nil then
11 (Message or print)
12 ('\n >>> testC not active: skipping memory error tests <<<\n')
13 return
14end
15
16print("testing memory-allocation errors")
17
18local debug = require "debug"
19
20local pack = table.pack
21
22-- standard error message for memory errors
23local MEMERRMSG = "not enough memory"
24
25
26-- memory error in panic function
27T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
28assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
29T.totalmem(0) -- restore high limit
30
31
32
33-- {==================================================================
34-- Testing memory limits
35-- ===================================================================
36
37checkerr("block too big", T.newuserdata, math.maxinteger)
38collectgarbage()
39local f = load"local a={}; for i=1,100000 do a[i]=i end"
40T.alloccount(10)
41checkerr(MEMERRMSG, f)
42T.alloccount() -- remove limit
43
44
45-- preallocate stack space
46local function deep (n) if n > 0 then deep(n - 1) end end
47
48
49-- test memory errors; increase limit for maximum memory by steps,
50-- so that we get memory errors in all allocations of a given
51-- task, until there is enough memory to complete the task without
52-- errors.
53local function testbytes (s, f)
54 collectgarbage()
55 local M = T.totalmem()
56 local oldM = M
57 local a,b = nil
58 while true do
59 collectgarbage(); collectgarbage()
60 deep(4)
61 T.totalmem(M)
62 a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
63 T.totalmem(0) -- remove limit
64 if a and b == "OK" then break end -- stop when no more errors
65 if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
66 error(a, 0) -- propagate it
67 end
68 M = M + 7 -- increase memory limit
69 end
70 print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
71 return a
72end
73
74-- test memory errors; increase limit for number of allocations one
75-- by one, so that we get memory errors in all allocations of a given
76-- task, until there is enough allocations to complete the task without
77-- errors.
78
79local function testalloc (s, f)
80 collectgarbage()
81 local M = 0
82 local a,b = nil
83 while true do
84 collectgarbage(); collectgarbage()
85 deep(4)
86 T.alloccount(M)
87 a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
88 T.alloccount() -- remove limit
89 if a and b == "OK" then break end -- stop when no more errors
90 if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
91 error(a, 0) -- propagate it
92 end
93 M = M + 1 -- increase allocation limit
94 end
95 print(string.format("minimum allocations for %s: %d allocations", s, M))
96 return M
97end
98
99
100local function testamem (s, f)
101 local aloc = testalloc(s, f)
102 local res = testbytes(s, f)
103 return {aloc = aloc, res = res}
104end
105
106
107local b = testamem("function call", function () return 10 end)
108assert(b.res == 10 and b.aloc == 0)
109
110testamem("state creation", function ()
111 local st = T.newstate()
112 if st then T.closestate(st) end -- close new state
113 return st
114end)
115
116testamem("empty-table creation", function ()
117 return {}
118end)
119
120testamem("string creation", function ()
121 return "XXX" .. "YYY"
122end)
123
124testamem("coroutine creation", function()
125 return coroutine.create(print)
126end)
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
140
141-- testing to-be-closed variables
142testamem("to-be-closed variables", function()
143 local flag
144 do
145 local x <close> =
146 setmetatable({}, {__close = function () flag = true end})
147 flag = false
148 local x = {}
149 end
150 return flag
151end)
152
153
154-- testing threads
155
156-- get main thread from registry
157local mt = T.testC("rawgeti R !M; return 1")
158assert(type(mt) == "thread" and coroutine.running() == mt)
159
160
161
162local function expand (n,s)
163 if n==0 then return "" end
164 local e = string.rep("=", n)
165 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
166 e, s, expand(n-1,s), e)
167end
168
169G=0; collectgarbage()
170load(expand(20,"G=G+1"))()
171assert(G==20); collectgarbage()
172G = nil
173
174testamem("running code on new thread", function ()
175 return T.doonnewstack("local x=1") == 0 -- try to create thread
176end)
177
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
187-- testing memory x compiler
188
189testamem("loadstring", function ()
190 return load("x=1") -- try to do load a string
191end)
192
193
194local testprog = [[
195local function foo () return end
196local t = {"x"}
197AA = "aaa"
198for i = 1, #t do AA = AA .. t[i] end
199return true
200]]
201
202-- testing memory x dofile
203_G.AA = nil
204local t =os.tmpname()
205local f = assert(io.open(t, "w"))
206f:write(testprog)
207f:close()
208testamem("dofile", function ()
209 local a = loadfile(t)
210 return a and a()
211end)
212assert(os.remove(t))
213assert(_G.AA == "aaax")
214
215
216-- other generic tests
217
218testamem("gsub", function ()
219 local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
220 return (a == 'ablo ablo')
221end)
222
223testamem("dump/undump", function ()
224 local a = load(testprog)
225 local b = a and string.dump(a)
226 a = b and load(b)
227 return a and a()
228end)
229
230_G.AA = nil
231
232local t = os.tmpname()
233testamem("file creation", function ()
234 local f = assert(io.open(t, 'w'))
235 assert (not io.open"nomenaoexistente")
236 io.close(f);
237 return not loadfile'nomenaoexistente'
238end)
239assert(os.remove(t))
240
241testamem("table creation", function ()
242 local a, lim = {}, 10
243 for i=1,lim do a[i] = i; a[i..'a'] = {} end
244 return (type(a[lim..'a']) == 'table' and a[lim] == lim)
245end)
246
247testamem("constructors", function ()
248 local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
249 return (type(a) == 'table' and a.e == 5)
250end)
251
252local a = 1
253local close = nil
254testamem("closure creation", function ()
255 function close (b)
256 return function (x) return b + x end
257 end
258 return (close(2)(4) == 6)
259end)
260
261testamem("using coroutines", function ()
262 local a = coroutine.wrap(function ()
263 coroutine.yield(string.rep("a", 10))
264 return {}
265 end)
266 assert(string.len(a()) == 10)
267 return a()
268end)
269
270do -- auxiliary buffer
271 local lim = 100
272 local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
273 testamem("auxiliary buffer", function ()
274 return (#table.concat(a, ",") == 20*lim + lim - 1)
275 end)
276end
277
278testamem("growing stack", function ()
279 local function foo (n)
280 if n == 0 then return 1 else return 1 + foo(n - 1) end
281 end
282 return foo(100)
283end)
284
285
286collectgarbage()
287collectgarbage()
288global io, T, setmetatable, collectgarbage, print
289
290local Count = 0
291testamem("finalizers", function ()
292 local X = false
293 local obj = setmetatable({}, {__gc = function () X = true end})
294 obj = nil
295 T.resetCI() -- remove extra CallInfos
296 T.reallocstack(18) -- remove extra stack slots
297 Count = Count + 1
298 io.stderr:write(Count, "\n")
299 T.trick(io)
300 collectgarbage()
301 return X
302end)
303
304-- }==================================================================
305
306
307print "Ok"
308
309