diff options
Diffstat (limited to 'testes/api.lua')
-rw-r--r-- | testes/api.lua | 1264 |
1 files changed, 1264 insertions, 0 deletions
diff --git a/testes/api.lua b/testes/api.lua new file mode 100644 index 00000000..836a6070 --- /dev/null +++ b/testes/api.lua | |||
@@ -0,0 +1,1264 @@ | |||
1 | -- $Id: api.lua,v 1.155 2018/03/09 14:23:48 roberto Exp $ | ||
2 | -- See Copyright Notice in file all.lua | ||
3 | |||
4 | if T==nil then | ||
5 | (Message or print)('\n >>> testC not active: skipping API tests <<<\n') | ||
6 | return | ||
7 | end | ||
8 | |||
9 | local debug = require "debug" | ||
10 | |||
11 | local pack = table.pack | ||
12 | |||
13 | |||
14 | function tcheck (t1, t2) | ||
15 | assert(t1.n == (t2.n or #t2) + 1) | ||
16 | for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end | ||
17 | end | ||
18 | |||
19 | |||
20 | local function checkerr (msg, f, ...) | ||
21 | local stat, err = pcall(f, ...) | ||
22 | assert(not stat and string.find(err, msg)) | ||
23 | end | ||
24 | |||
25 | |||
26 | print('testing C API') | ||
27 | |||
28 | a = T.testC("pushvalue R; return 1") | ||
29 | assert(a == debug.getregistry()) | ||
30 | |||
31 | |||
32 | -- absindex | ||
33 | assert(T.testC("settop 10; absindex -1; return 1") == 10) | ||
34 | assert(T.testC("settop 5; absindex -5; return 1") == 1) | ||
35 | assert(T.testC("settop 10; absindex 1; return 1") == 1) | ||
36 | assert(T.testC("settop 10; absindex R; return 1") < -10) | ||
37 | |||
38 | -- testing alignment | ||
39 | a = T.d2s(12458954321123.0) | ||
40 | assert(a == string.pack("d", 12458954321123.0)) | ||
41 | assert(T.s2d(a) == 12458954321123.0) | ||
42 | |||
43 | a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") | ||
44 | assert(a == 2 and b == 3 and not c) | ||
45 | |||
46 | f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") | ||
47 | a,b,c = f() | ||
48 | assert(a == 2 and b == 3 and not c) | ||
49 | |||
50 | -- test that all trues are equal | ||
51 | a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3") | ||
52 | assert(a == b and a == true and c == false) | ||
53 | a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\ | ||
54 | tobool -3; tobool -3; tobool -3; return 3" | ||
55 | assert(a==false and b==true and c==false) | ||
56 | |||
57 | |||
58 | a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) | ||
59 | assert(a == 40 and b == 5 and not c) | ||
60 | |||
61 | t = pack(T.testC("settop 5; return *", 2, 3)) | ||
62 | tcheck(t, {n=4,2,3}) | ||
63 | |||
64 | t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) | ||
65 | assert(t.n == 10 and t[1] == nil and t[10] == nil) | ||
66 | |||
67 | t = pack(T.testC("remove -2; return *", 2, 3, 4)) | ||
68 | tcheck(t, {n=2,2,4}) | ||
69 | |||
70 | t = pack(T.testC("insert -1; return *", 2, 3)) | ||
71 | tcheck(t, {n=2,2,3}) | ||
72 | |||
73 | t = pack(T.testC("insert 3; return *", 2, 3, 4, 5)) | ||
74 | tcheck(t, {n=4,2,5,3,4}) | ||
75 | |||
76 | t = pack(T.testC("replace 2; return *", 2, 3, 4, 5)) | ||
77 | tcheck(t, {n=3,5,3,4}) | ||
78 | |||
79 | t = pack(T.testC("replace -2; return *", 2, 3, 4, 5)) | ||
80 | tcheck(t, {n=3,2,3,5}) | ||
81 | |||
82 | t = pack(T.testC("remove 3; return *", 2, 3, 4, 5)) | ||
83 | tcheck(t, {n=3,2,4,5}) | ||
84 | |||
85 | t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5)) | ||
86 | tcheck(t, {n=4,2,3,3,5}) | ||
87 | |||
88 | t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5)) | ||
89 | tcheck(t, {n=4,2,3,4,3}) | ||
90 | |||
91 | do -- testing 'rotate' | ||
92 | local t = {10, 20, 30, 40, 50, 60} | ||
93 | for i = -6, 6 do | ||
94 | local s = string.format("rotate 2 %d; return 7", i) | ||
95 | local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60)) | ||
96 | tcheck(t1, t) | ||
97 | table.insert(t, 1, table.remove(t)) | ||
98 | end | ||
99 | |||
100 | t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40)) | ||
101 | tcheck(t, {10, 20, 40, 30}) | ||
102 | t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40)) | ||
103 | tcheck(t, {10, 20, 40, 30}) | ||
104 | |||
105 | -- some corner cases | ||
106 | t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40)) | ||
107 | tcheck(t, {10, 20, 30, 40}) | ||
108 | t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40)) | ||
109 | tcheck(t, {10, 20, 30, 40}) | ||
110 | t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40)) | ||
111 | tcheck(t, {10, 20, 30, 40}) | ||
112 | end | ||
113 | |||
114 | -- testing message handlers | ||
115 | do | ||
116 | local f = T.makeCfunc[[ | ||
117 | getglobal error | ||
118 | pushstring bola | ||
119 | pcall 1 1 1 # call 'error' with given handler | ||
120 | pushstatus | ||
121 | return 2 # return error message and status | ||
122 | ]] | ||
123 | |||
124 | local msg, st = f(string.upper) -- function handler | ||
125 | assert(st == "ERRRUN" and msg == "BOLA") | ||
126 | local msg, st = f(string.len) -- function handler | ||
127 | assert(st == "ERRRUN" and msg == 4) | ||
128 | |||
129 | end | ||
130 | |||
131 | t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \ | ||
132 | insert 2; pushvalue 1; remove 1; insert 1; \ | ||
133 | insert -2; pushvalue -2; remove -3; return *", | ||
134 | 2, 3, 4, 5, 10, 40, 90)) | ||
135 | tcheck(t, {n=7,2,3,4,5,10,40,90}) | ||
136 | |||
137 | t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12)) | ||
138 | tcheck(t, {n=1,"alo23joao12"}) | ||
139 | |||
140 | -- testing MULTRET | ||
141 | t = pack(T.testC("call 2,-1; return *", | ||
142 | function (a,b) return 1,2,3,4,a,b end, "alo", "joao")) | ||
143 | tcheck(t, {n=6,1,2,3,4,"alo", "joao"}) | ||
144 | |||
145 | do -- test returning more results than fit in the caller stack | ||
146 | local a = {} | ||
147 | for i=1,1000 do a[i] = true end; a[999] = 10 | ||
148 | local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]], | ||
149 | table.unpack, a) | ||
150 | assert(b == "10") | ||
151 | end | ||
152 | |||
153 | |||
154 | -- testing globals | ||
155 | _G.a = 14; _G.b = "a31" | ||
156 | local a = {T.testC[[ | ||
157 | getglobal a; | ||
158 | getglobal b; | ||
159 | getglobal b; | ||
160 | setglobal a; | ||
161 | return * | ||
162 | ]]} | ||
163 | assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") | ||
164 | |||
165 | |||
166 | -- testing arith | ||
167 | assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) | ||
168 | assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10) | ||
169 | assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200) | ||
170 | assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000) | ||
171 | assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5) | ||
172 | assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10) | ||
173 | assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200) | ||
174 | assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000) | ||
175 | assert(T.testC("arith /; return 1", 2, 0) == 10.0/0) | ||
176 | a = T.testC("pushnum 10; pushint 3; arith \\; return 1") | ||
177 | assert(a == 3.0 and math.type(a) == "float") | ||
178 | a = T.testC("pushint 10; pushint 3; arith \\; return 1") | ||
179 | assert(a == 3 and math.type(a) == "integer") | ||
180 | a = assert(T.testC("pushint 10; pushint 3; arith +; return 1")) | ||
181 | assert(a == 13 and math.type(a) == "integer") | ||
182 | a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1")) | ||
183 | assert(a == 13 and math.type(a) == "float") | ||
184 | a,b,c = T.testC([[pushnum 1; | ||
185 | pushstring 10; arith _; | ||
186 | pushstring 5; return 3]]) | ||
187 | assert(a == 1 and b == -10 and c == "5") | ||
188 | mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, | ||
189 | __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, | ||
190 | __unm = function (a) return setmetatable({a[1]* 2}, mt) end} | ||
191 | a,b,c = setmetatable({4}, mt), | ||
192 | setmetatable({8}, mt), | ||
193 | setmetatable({-3}, mt) | ||
194 | x,y,z = T.testC("arith +; return 2", 10, a, b) | ||
195 | assert(x == 10 and y[1] == 12 and z == nil) | ||
196 | assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) | ||
197 | assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == | ||
198 | 8 % (4 + (-3)*2)) | ||
199 | |||
200 | -- errors in arithmetic | ||
201 | checkerr("divide by zero", T.testC, "arith \\", 10, 0) | ||
202 | checkerr("%%0", T.testC, "arith %", 10, 0) | ||
203 | |||
204 | |||
205 | -- testing lessthan and lessequal | ||
206 | assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2)) | ||
207 | assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2)) | ||
208 | assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2)) | ||
209 | assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2)) | ||
210 | assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2)) | ||
211 | assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2")) | ||
212 | assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2")) | ||
213 | |||
214 | -- non-valid indices produce false | ||
215 | assert(not T.testC("compare LT 1 4, return 1")) | ||
216 | assert(not T.testC("compare LE 9 1, return 1")) | ||
217 | assert(not T.testC("compare EQ 9 9, return 1")) | ||
218 | |||
219 | local b = {__lt = function (a,b) return a[1] < b[1] end} | ||
220 | local a1,a3,a4 = setmetatable({1}, b), | ||
221 | setmetatable({3}, b), | ||
222 | setmetatable({4}, b) | ||
223 | assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2)) | ||
224 | assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2)) | ||
225 | assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2)) | ||
226 | a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20) | ||
227 | assert(a == 20 and b == false) | ||
228 | a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20) | ||
229 | assert(a == 20 and b == false) | ||
230 | a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) | ||
231 | assert(a == 20 and b == true) | ||
232 | |||
233 | -- testing length | ||
234 | local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) | ||
235 | a,b,c = T.testC([[ | ||
236 | len 2; | ||
237 | Llen 2; | ||
238 | objsize 2; | ||
239 | return 3 | ||
240 | ]], t) | ||
241 | assert(a == 20 and b == 20 and c == 0) | ||
242 | |||
243 | t.x = "234"; t[1] = 20 | ||
244 | a,b,c = T.testC([[ | ||
245 | len 2; | ||
246 | Llen 2; | ||
247 | objsize 2; | ||
248 | return 3 | ||
249 | ]], t) | ||
250 | assert(a == "234" and b == 234 and c == 1) | ||
251 | |||
252 | t.x = print; t[1] = 20 | ||
253 | a,c = T.testC([[ | ||
254 | len 2; | ||
255 | objsize 2; | ||
256 | return 2 | ||
257 | ]], t) | ||
258 | assert(a == print and c == 1) | ||
259 | |||
260 | |||
261 | -- testing __concat | ||
262 | |||
263 | a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end}) | ||
264 | x,y = T.testC([[ | ||
265 | pushnum 5 | ||
266 | pushvalue 2; | ||
267 | pushvalue 2; | ||
268 | concat 2; | ||
269 | pushvalue -2; | ||
270 | return 2; | ||
271 | ]], a, a) | ||
272 | assert(x == a..a and y == 5) | ||
273 | |||
274 | -- concat with 0 elements | ||
275 | assert(T.testC("concat 0; return 1") == "") | ||
276 | |||
277 | -- concat with 1 element | ||
278 | assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") | ||
279 | |||
280 | |||
281 | |||
282 | -- testing lua_is | ||
283 | |||
284 | function B(x) return x and 1 or 0 end | ||
285 | |||
286 | function count (x, n) | ||
287 | n = n or 2 | ||
288 | local prog = [[ | ||
289 | isnumber %d; | ||
290 | isstring %d; | ||
291 | isfunction %d; | ||
292 | iscfunction %d; | ||
293 | istable %d; | ||
294 | isuserdata %d; | ||
295 | isnil %d; | ||
296 | isnull %d; | ||
297 | return 8 | ||
298 | ]] | ||
299 | prog = string.format(prog, n, n, n, n, n, n, n, n) | ||
300 | local a,b,c,d,e,f,g,h = T.testC(prog, x) | ||
301 | return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h)) | ||
302 | end | ||
303 | |||
304 | assert(count(3) == 2) | ||
305 | assert(count('alo') == 1) | ||
306 | assert(count('32') == 2) | ||
307 | assert(count({}) == 1) | ||
308 | assert(count(print) == 2) | ||
309 | assert(count(function () end) == 1) | ||
310 | assert(count(nil) == 1) | ||
311 | assert(count(io.stdin) == 1) | ||
312 | assert(count(nil, 15) == 100) | ||
313 | |||
314 | |||
315 | -- testing lua_to... | ||
316 | |||
317 | function to (s, x, n) | ||
318 | n = n or 2 | ||
319 | return T.testC(string.format("%s %d; return 1", s, n), x) | ||
320 | end | ||
321 | |||
322 | local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues) | ||
323 | assert(debug.getupvalue(hfunc, 1)) | ||
324 | assert(to("tostring", {}) == nil) | ||
325 | assert(to("tostring", "alo") == "alo") | ||
326 | assert(to("tostring", 12) == "12") | ||
327 | assert(to("tostring", 12, 3) == nil) | ||
328 | assert(to("objsize", {}) == 0) | ||
329 | assert(to("objsize", {1,2,3}) == 3) | ||
330 | assert(to("objsize", "alo\0\0a") == 6) | ||
331 | assert(to("objsize", T.newuserdata(0)) == 0) | ||
332 | assert(to("objsize", T.newuserdata(101)) == 101) | ||
333 | assert(to("objsize", 124) == 0) | ||
334 | assert(to("objsize", true) == 0) | ||
335 | assert(to("tonumber", {}) == 0) | ||
336 | assert(to("tonumber", "12") == 12) | ||
337 | assert(to("tonumber", "s2") == 0) | ||
338 | assert(to("tonumber", 1, 20) == 0) | ||
339 | assert(to("topointer", 10) == 0) | ||
340 | assert(to("topointer", true) == 0) | ||
341 | assert(to("topointer", T.pushuserdata(20)) == 20) | ||
342 | assert(to("topointer", io.read) ~= 0) -- light C function | ||
343 | assert(to("topointer", hfunc) ~= 0) -- "heavy" C function | ||
344 | assert(to("topointer", function () end) ~= 0) -- Lua function | ||
345 | assert(to("topointer", io.stdin) ~= 0) -- full userdata | ||
346 | assert(to("func2num", 20) == 0) | ||
347 | assert(to("func2num", T.pushuserdata(10)) == 0) | ||
348 | assert(to("func2num", io.read) ~= 0) -- light C function | ||
349 | assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue) | ||
350 | a = to("tocfunction", math.deg) | ||
351 | assert(a(3) == math.deg(3) and a == math.deg) | ||
352 | |||
353 | |||
354 | print("testing panic function") | ||
355 | do | ||
356 | -- trivial error | ||
357 | assert(T.checkpanic("pushstring hi; error") == "hi") | ||
358 | |||
359 | -- using the stack inside panic | ||
360 | assert(T.checkpanic("pushstring hi; error;", | ||
361 | [[checkstack 5 XX | ||
362 | pushstring ' alo' | ||
363 | pushstring ' mundo' | ||
364 | concat 3]]) == "hi alo mundo") | ||
365 | |||
366 | -- "argerror" without frames | ||
367 | assert(T.checkpanic("loadstring 4") == | ||
368 | "bad argument #4 (string expected, got no value)") | ||
369 | |||
370 | |||
371 | -- memory error | ||
372 | T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) | ||
373 | assert(T.checkpanic("newuserdata 20000") == "not enough memory") | ||
374 | T.totalmem(0) -- restore high limit | ||
375 | |||
376 | -- stack error | ||
377 | if not _soft then | ||
378 | local msg = T.checkpanic[[ | ||
379 | pushstring "function f() f() end" | ||
380 | loadstring -1; call 0 0 | ||
381 | getglobal f; call 0 0 | ||
382 | ]] | ||
383 | assert(string.find(msg, "stack overflow")) | ||
384 | end | ||
385 | |||
386 | end | ||
387 | |||
388 | -- testing deep C stack | ||
389 | if not _soft then | ||
390 | print("testing stack overflow") | ||
391 | collectgarbage("stop") | ||
392 | checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep | ||
393 | -- too deep (with no message) | ||
394 | checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''") | ||
395 | local s = string.rep("pushnil;checkstack 1 XX;", 1000000) | ||
396 | checkerr("overflow", T.testC, s) | ||
397 | collectgarbage("restart") | ||
398 | print'+' | ||
399 | end | ||
400 | |||
401 | local lim = _soft and 500 or 12000 | ||
402 | local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} | ||
403 | for i = 1,lim do | ||
404 | prog[#prog + 1] = "pushnum " .. i | ||
405 | prog[#prog + 1] = "pushnum " .. i * 10 | ||
406 | end | ||
407 | |||
408 | prog[#prog + 1] = "rawgeti R 2" -- get global table in registry | ||
409 | prog[#prog + 1] = "insert " .. -(2*lim + 2) | ||
410 | |||
411 | for i = 1,lim do | ||
412 | prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1) | ||
413 | end | ||
414 | |||
415 | prog[#prog + 1] = "return 2" | ||
416 | |||
417 | prog = table.concat(prog, ";") | ||
418 | local g, t = T.testC(prog) | ||
419 | assert(g == _G) | ||
420 | for i = 1,lim do assert(t[i] == i*10); t[i] = undef end | ||
421 | assert(next(t) == nil) | ||
422 | prog, g, t = nil | ||
423 | |||
424 | -- testing errors | ||
425 | |||
426 | a = T.testC([[ | ||
427 | loadstring 2; pcall 0 1 0; | ||
428 | pushvalue 3; insert -2; pcall 1 1 0; | ||
429 | pcall 0 0 0; | ||
430 | return 1 | ||
431 | ]], "x=150", function (a) assert(a==nil); return 3 end) | ||
432 | |||
433 | assert(type(a) == 'string' and x == 150) | ||
434 | |||
435 | function check3(p, ...) | ||
436 | local arg = {...} | ||
437 | assert(#arg == 3) | ||
438 | assert(string.find(arg[3], p)) | ||
439 | end | ||
440 | check3(":1:", T.testC("loadstring 2; return *", "x=")) | ||
441 | check3("%.", T.testC("loadfile 2; return *", ".")) | ||
442 | check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) | ||
443 | |||
444 | -- test errors in non protected threads | ||
445 | function checkerrnopro (code, msg) | ||
446 | local th = coroutine.create(function () end) -- create new thread | ||
447 | local stt, err = pcall(T.testC, th, code) -- run code there | ||
448 | assert(not stt and string.find(err, msg)) | ||
449 | end | ||
450 | |||
451 | if not _soft then | ||
452 | checkerrnopro("pushnum 3; call 0 0", "attempt to call") | ||
453 | print"testing stack overflow in unprotected thread" | ||
454 | function f () f() end | ||
455 | checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") | ||
456 | end | ||
457 | print"+" | ||
458 | |||
459 | |||
460 | -- testing table access | ||
461 | |||
462 | do -- getp/setp | ||
463 | local a = {} | ||
464 | T.testC("rawsetp 2 1", a, 20) | ||
465 | assert(a[T.pushuserdata(1)] == 20) | ||
466 | assert(T.testC("rawgetp 2 1; return 1", a) == 20) | ||
467 | end | ||
468 | |||
469 | a = {x=0, y=12} | ||
470 | x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2", | ||
471 | a, 3, "y", 4, "x") | ||
472 | assert(x == 0 and y == 12) | ||
473 | T.testC("settable -5", a, 3, 4, "x", 15) | ||
474 | assert(a.x == 15) | ||
475 | a[a] = print | ||
476 | x = T.testC("gettable 2; return 1", a) -- table and key are the same object! | ||
477 | assert(x == print) | ||
478 | T.testC("settable 2", a, "x") -- table and key are the same object! | ||
479 | assert(a[a] == "x") | ||
480 | |||
481 | b = setmetatable({p = a}, {}) | ||
482 | getmetatable(b).__index = function (t, i) return t.p[i] end | ||
483 | k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") | ||
484 | assert(x == 15 and k == 35) | ||
485 | k = T.testC("getfield 2 y, return 1", b) | ||
486 | assert(k == 12) | ||
487 | getmetatable(b).__index = function (t, i) return a[i] end | ||
488 | getmetatable(b).__newindex = function (t, i,v ) a[i] = v end | ||
489 | y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b) | ||
490 | assert(y == 12) | ||
491 | k = T.testC("settable -5, return 1", b, 3, 4, "x", 16) | ||
492 | assert(a.x == 16 and k == 4) | ||
493 | a[b] = 'xuxu' | ||
494 | y = T.testC("gettable 2, return 1", b) | ||
495 | assert(y == 'xuxu') | ||
496 | T.testC("settable 2", b, 19) | ||
497 | assert(a[b] == 19) | ||
498 | |||
499 | -- | ||
500 | do -- testing getfield/setfield with long keys | ||
501 | local t = {_012345678901234567890123456789012345678901234567890123456789 = 32} | ||
502 | local a = T.testC([[ | ||
503 | getfield 2 _012345678901234567890123456789012345678901234567890123456789 | ||
504 | return 1 | ||
505 | ]], t) | ||
506 | assert(a == 32) | ||
507 | local a = T.testC([[ | ||
508 | pushnum 33 | ||
509 | setglobal _012345678901234567890123456789012345678901234567890123456789 | ||
510 | ]]) | ||
511 | assert(_012345678901234567890123456789012345678901234567890123456789 == 33) | ||
512 | _012345678901234567890123456789012345678901234567890123456789 = nil | ||
513 | end | ||
514 | |||
515 | -- testing next | ||
516 | a = {} | ||
517 | t = pack(T.testC("next; return *", a, nil)) | ||
518 | tcheck(t, {n=1,a}) | ||
519 | a = {a=3} | ||
520 | t = pack(T.testC("next; return *", a, nil)) | ||
521 | tcheck(t, {n=3,a,'a',3}) | ||
522 | t = pack(T.testC("next; pop 1; next; return *", a, nil)) | ||
523 | tcheck(t, {n=1,a}) | ||
524 | |||
525 | |||
526 | |||
527 | -- testing upvalues | ||
528 | |||
529 | do | ||
530 | local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] | ||
531 | t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]]) | ||
532 | assert(b == 10 and c == 20 and type(t) == 'table') | ||
533 | a, b = A([[tostring U3; tonumber U4; return 2]]) | ||
534 | assert(a == nil and b == 0) | ||
535 | A([[pushnum 100; pushnum 200; replace U2; replace U1]]) | ||
536 | b, c = A([[pushvalue U1; pushvalue U2; return 2]]) | ||
537 | assert(b == 100 and c == 200) | ||
538 | A([[replace U2; replace U1]], {x=1}, {x=2}) | ||
539 | b, c = A([[pushvalue U1; pushvalue U2; return 2]]) | ||
540 | assert(b.x == 1 and c.x == 2) | ||
541 | T.checkmemory() | ||
542 | end | ||
543 | |||
544 | |||
545 | -- testing absent upvalues from C-function pointers | ||
546 | assert(T.testC[[isnull U1; return 1]] == true) | ||
547 | assert(T.testC[[isnull U100; return 1]] == true) | ||
548 | assert(T.testC[[pushvalue U1; return 1]] == nil) | ||
549 | |||
550 | local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] | ||
551 | assert(T.upvalue(f, 1) == 10 and | ||
552 | T.upvalue(f, 2) == 20 and | ||
553 | T.upvalue(f, 3) == nil) | ||
554 | T.upvalue(f, 2, "xuxu") | ||
555 | assert(T.upvalue(f, 2) == "xuxu") | ||
556 | |||
557 | |||
558 | -- large closures | ||
559 | do | ||
560 | local A = "checkstack 300 msg;" .. | ||
561 | string.rep("pushnum 10;", 255) .. | ||
562 | "pushcclosure 255; return 1" | ||
563 | A = T.testC(A) | ||
564 | for i=1,255 do | ||
565 | assert(A(("pushvalue U%d; return 1"):format(i)) == 10) | ||
566 | end | ||
567 | assert(A("isnull U256; return 1")) | ||
568 | assert(not A("isnil U256; return 1")) | ||
569 | end | ||
570 | |||
571 | |||
572 | |||
573 | -- testing get/setuservalue | ||
574 | -- bug in 5.1.2 | ||
575 | checkerr("got number", debug.setuservalue, 3, {}) | ||
576 | checkerr("got nil", debug.setuservalue, nil, {}) | ||
577 | checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {}) | ||
578 | |||
579 | -- testing multiple user values | ||
580 | local b = T.newuserdata(0, 10) | ||
581 | for i = 1, 10 do | ||
582 | local v, p = debug.getuservalue(b, i) | ||
583 | assert(v == nil and p) | ||
584 | end | ||
585 | do -- indices out of range | ||
586 | local v, p = debug.getuservalue(b, -2) | ||
587 | assert(v == nil and not p) | ||
588 | local v, p = debug.getuservalue(b, 11) | ||
589 | assert(v == nil and not p) | ||
590 | end | ||
591 | local t = {true, false, 4.56, print, {}, b, "XYZ"} | ||
592 | for k, v in ipairs(t) do | ||
593 | debug.setuservalue(b, v, k) | ||
594 | end | ||
595 | for k, v in ipairs(t) do | ||
596 | local v1, p = debug.getuservalue(b, k) | ||
597 | assert(v1 == v and p) | ||
598 | end | ||
599 | |||
600 | assert(debug.getuservalue(4) == nil) | ||
601 | |||
602 | debug.setuservalue(b, function () return 10 end, 10) | ||
603 | collectgarbage() -- function should not be collected | ||
604 | assert(debug.getuservalue(b, 10)() == 10) | ||
605 | |||
606 | debug.setuservalue(b, 134) | ||
607 | collectgarbage() -- number should not be a problem for collector | ||
608 | assert(debug.getuservalue(b) == 134) | ||
609 | |||
610 | |||
611 | -- test barrier for uservalues | ||
612 | do | ||
613 | local oldmode = collectgarbage("incremental") | ||
614 | T.gcstate("atomic") | ||
615 | assert(T.gccolor(b) == "black") | ||
616 | debug.setuservalue(b, {x = 100}) | ||
617 | T.gcstate("pause") -- complete collection | ||
618 | assert(debug.getuservalue(b).x == 100) -- uvalue should be there | ||
619 | collectgarbage(oldmode) | ||
620 | end | ||
621 | |||
622 | -- long chain of userdata | ||
623 | for i = 1, 1000 do | ||
624 | local bb = T.newuserdata(0, 1) | ||
625 | debug.setuservalue(bb, b) | ||
626 | b = bb | ||
627 | end | ||
628 | collectgarbage() -- nothing should not be collected | ||
629 | for i = 1, 1000 do | ||
630 | b = debug.getuservalue(b) | ||
631 | end | ||
632 | assert(debug.getuservalue(b).x == 100) | ||
633 | b = nil | ||
634 | |||
635 | |||
636 | -- testing locks (refs) | ||
637 | |||
638 | -- reuse of references | ||
639 | local i = T.ref{} | ||
640 | T.unref(i) | ||
641 | assert(T.ref{} == i) | ||
642 | |||
643 | Arr = {} | ||
644 | Lim = 100 | ||
645 | for i=1,Lim do -- lock many objects | ||
646 | Arr[i] = T.ref({}) | ||
647 | end | ||
648 | |||
649 | assert(T.ref(nil) == -1 and T.getref(-1) == nil) | ||
650 | T.unref(-1); T.unref(-1) | ||
651 | |||
652 | for i=1,Lim do -- unlock all them | ||
653 | T.unref(Arr[i]) | ||
654 | end | ||
655 | |||
656 | function printlocks () | ||
657 | local f = T.makeCfunc("gettable R; return 1") | ||
658 | local n = f("n") | ||
659 | print("n", n) | ||
660 | for i=0,n do | ||
661 | print(i, f(i)) | ||
662 | end | ||
663 | end | ||
664 | |||
665 | |||
666 | for i=1,Lim do -- lock many objects | ||
667 | Arr[i] = T.ref({}) | ||
668 | end | ||
669 | |||
670 | for i=1,Lim,2 do -- unlock half of them | ||
671 | T.unref(Arr[i]) | ||
672 | end | ||
673 | |||
674 | assert(type(T.getref(Arr[2])) == 'table') | ||
675 | |||
676 | |||
677 | assert(T.getref(-1) == nil) | ||
678 | |||
679 | |||
680 | a = T.ref({}) | ||
681 | |||
682 | collectgarbage() | ||
683 | |||
684 | assert(type(T.getref(a)) == 'table') | ||
685 | |||
686 | |||
687 | -- colect in cl the `val' of all collected userdata | ||
688 | tt = {} | ||
689 | cl = {n=0} | ||
690 | A = nil; B = nil | ||
691 | local F | ||
692 | F = function (x) | ||
693 | local udval = T.udataval(x) | ||
694 | table.insert(cl, udval) | ||
695 | local d = T.newuserdata(100) -- create garbage | ||
696 | d = nil | ||
697 | assert(debug.getmetatable(x).__gc == F) | ||
698 | assert(load("table.insert({}, {})"))() -- create more garbage | ||
699 | collectgarbage() -- force a GC during GC | ||
700 | assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this? | ||
701 | local dummy = {} -- create more garbage during GC | ||
702 | if A ~= nil then | ||
703 | assert(type(A) == "userdata") | ||
704 | assert(T.udataval(A) == B) | ||
705 | debug.getmetatable(A) -- just acess it | ||
706 | end | ||
707 | A = x -- ressucita userdata | ||
708 | B = udval | ||
709 | return 1,2,3 | ||
710 | end | ||
711 | tt.__gc = F | ||
712 | |||
713 | -- test whether udate collection frees memory in the right time | ||
714 | do | ||
715 | collectgarbage(); | ||
716 | collectgarbage(); | ||
717 | local x = collectgarbage("count"); | ||
718 | local a = T.newuserdata(5001) | ||
719 | assert(T.testC("objsize 2; return 1", a) == 5001) | ||
720 | assert(collectgarbage("count") >= x+4) | ||
721 | a = nil | ||
722 | collectgarbage(); | ||
723 | assert(collectgarbage("count") <= x+1) | ||
724 | -- udata without finalizer | ||
725 | x = collectgarbage("count") | ||
726 | collectgarbage("stop") | ||
727 | for i=1,1000 do T.newuserdata(0) end | ||
728 | assert(collectgarbage("count") > x+10) | ||
729 | collectgarbage() | ||
730 | assert(collectgarbage("count") <= x+1) | ||
731 | -- udata with finalizer | ||
732 | collectgarbage() | ||
733 | x = collectgarbage("count") | ||
734 | collectgarbage("stop") | ||
735 | a = {__gc = function () end} | ||
736 | for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end | ||
737 | assert(collectgarbage("count") >= x+10) | ||
738 | collectgarbage() -- this collection only calls TM, without freeing memory | ||
739 | assert(collectgarbage("count") >= x+10) | ||
740 | collectgarbage() -- now frees memory | ||
741 | assert(collectgarbage("count") <= x+1) | ||
742 | collectgarbage("restart") | ||
743 | end | ||
744 | |||
745 | |||
746 | collectgarbage("stop") | ||
747 | |||
748 | -- create 3 userdatas with tag `tt' | ||
749 | a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) | ||
750 | b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) | ||
751 | c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) | ||
752 | |||
753 | -- create userdata without meta table | ||
754 | x = T.newuserdata(4) | ||
755 | y = T.newuserdata(0) | ||
756 | |||
757 | checkerr("FILE%* expected, got userdata", io.input, a) | ||
758 | checkerr("FILE%* expected, got userdata", io.input, x) | ||
759 | |||
760 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) | ||
761 | |||
762 | d=T.ref(a); | ||
763 | e=T.ref(b); | ||
764 | f=T.ref(c); | ||
765 | t = {T.getref(d), T.getref(e), T.getref(f)} | ||
766 | assert(t[1] == a and t[2] == b and t[3] == c) | ||
767 | |||
768 | t=nil; a=nil; c=nil; | ||
769 | T.unref(e); T.unref(f) | ||
770 | |||
771 | collectgarbage() | ||
772 | |||
773 | -- check that unref objects have been collected | ||
774 | assert(#cl == 1 and cl[1] == nc) | ||
775 | |||
776 | x = T.getref(d) | ||
777 | assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) | ||
778 | x =nil | ||
779 | tt.b = b -- create cycle | ||
780 | tt=nil -- frees tt for GC | ||
781 | A = nil | ||
782 | b = nil | ||
783 | T.unref(d); | ||
784 | n5 = T.newuserdata(0) | ||
785 | debug.setmetatable(n5, {__gc=F}) | ||
786 | n5 = T.udataval(n5) | ||
787 | collectgarbage() | ||
788 | assert(#cl == 4) | ||
789 | -- check order of collection | ||
790 | assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) | ||
791 | |||
792 | collectgarbage"restart" | ||
793 | |||
794 | |||
795 | a, na = {}, {} | ||
796 | for i=30,1,-1 do | ||
797 | a[i] = T.newuserdata(0) | ||
798 | debug.setmetatable(a[i], {__gc=F}) | ||
799 | na[i] = T.udataval(a[i]) | ||
800 | end | ||
801 | cl = {} | ||
802 | a = nil; collectgarbage() | ||
803 | assert(#cl == 30) | ||
804 | for i=1,30 do assert(cl[i] == na[i]) end | ||
805 | na = nil | ||
806 | |||
807 | |||
808 | for i=2,Lim,2 do -- unlock the other half | ||
809 | T.unref(Arr[i]) | ||
810 | end | ||
811 | |||
812 | x = T.newuserdata(41); debug.setmetatable(x, {__gc=F}) | ||
813 | assert(T.testC("objsize 2; return 1", x) == 41) | ||
814 | cl = {} | ||
815 | a = {[x] = 1} | ||
816 | x = T.udataval(x) | ||
817 | collectgarbage() | ||
818 | -- old `x' cannot be collected (`a' still uses it) | ||
819 | assert(#cl == 0) | ||
820 | for n in pairs(a) do a[n] = undef end | ||
821 | collectgarbage() | ||
822 | assert(#cl == 1 and cl[1] == x) -- old `x' must be collected | ||
823 | |||
824 | -- testing lua_equal | ||
825 | assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20)) | ||
826 | assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo")) | ||
827 | assert(T.testC("compare EQ 2 3; return 1", nil, nil)) | ||
828 | assert(not T.testC("compare EQ 2 3; return 1", {}, {})) | ||
829 | assert(not T.testC("compare EQ 2 3; return 1")) | ||
830 | assert(not T.testC("compare EQ 2 3; return 1", 3)) | ||
831 | |||
832 | -- testing lua_equal with fallbacks | ||
833 | do | ||
834 | local map = {} | ||
835 | local t = {__eq = function (a,b) return map[a] == map[b] end} | ||
836 | local function f(x) | ||
837 | local u = T.newuserdata(0) | ||
838 | debug.setmetatable(u, t) | ||
839 | map[u] = x | ||
840 | return u | ||
841 | end | ||
842 | assert(f(10) == f(10)) | ||
843 | assert(f(10) ~= f(11)) | ||
844 | assert(T.testC("compare EQ 2 3; return 1", f(10), f(10))) | ||
845 | assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20))) | ||
846 | t.__eq = nil | ||
847 | assert(f(10) ~= f(10)) | ||
848 | end | ||
849 | |||
850 | print'+' | ||
851 | |||
852 | |||
853 | |||
854 | -- testing changing hooks during hooks | ||
855 | _G.t = {} | ||
856 | T.sethook([[ | ||
857 | # set a line hook after 3 count hooks | ||
858 | sethook 4 0 ' | ||
859 | getglobal t; | ||
860 | pushvalue -3; append -2 | ||
861 | pushvalue -2; append -2 | ||
862 | ']], "c", 3) | ||
863 | local a = 1 -- counting | ||
864 | a = 1 -- counting | ||
865 | a = 1 -- count hook (set line hook) | ||
866 | a = 1 -- line hook | ||
867 | a = 1 -- line hook | ||
868 | debug.sethook() | ||
869 | t = _G.t | ||
870 | assert(t[1] == "line") | ||
871 | line = t[2] | ||
872 | assert(t[3] == "line" and t[4] == line + 1) | ||
873 | assert(t[5] == "line" and t[6] == line + 2) | ||
874 | assert(t[7] == nil) | ||
875 | |||
876 | |||
877 | ------------------------------------------------------------------------- | ||
878 | do -- testing errors during GC | ||
879 | collectgarbage("stop") | ||
880 | local a = {} | ||
881 | for i=1,20 do | ||
882 | a[i] = T.newuserdata(i) -- creates several udata | ||
883 | end | ||
884 | for i=1,20,2 do -- mark half of them to raise errors during GC | ||
885 | debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) | ||
886 | end | ||
887 | for i=2,20,2 do -- mark the other half to count and to create more garbage | ||
888 | debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) | ||
889 | end | ||
890 | _G.A = 0 | ||
891 | a = 0 | ||
892 | while 1 do | ||
893 | local stat, msg = pcall(collectgarbage) | ||
894 | if stat then | ||
895 | break -- stop when no more errors | ||
896 | else | ||
897 | a = a + 1 | ||
898 | assert(string.find(msg, "__gc")) | ||
899 | end | ||
900 | end | ||
901 | assert(a == 10) -- number of errors | ||
902 | |||
903 | assert(A == 10) -- number of normal collections | ||
904 | collectgarbage("restart") | ||
905 | end | ||
906 | ------------------------------------------------------------------------- | ||
907 | -- test for userdata vals | ||
908 | do | ||
909 | local a = {}; local lim = 30 | ||
910 | for i=0,lim do a[i] = T.pushuserdata(i) end | ||
911 | for i=0,lim do assert(T.udataval(a[i]) == i) end | ||
912 | for i=0,lim do assert(T.pushuserdata(i) == a[i]) end | ||
913 | for i=0,lim do a[a[i]] = i end | ||
914 | for i=0,lim do a[T.pushuserdata(i)] = i end | ||
915 | assert(type(tostring(a[1])) == "string") | ||
916 | end | ||
917 | |||
918 | |||
919 | ------------------------------------------------------------------------- | ||
920 | -- testing multiple states | ||
921 | T.closestate(T.newstate()); | ||
922 | L1 = T.newstate() | ||
923 | assert(L1) | ||
924 | |||
925 | assert(T.doremote(L1, "X='a'; return 'a'") == 'a') | ||
926 | |||
927 | |||
928 | assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0) | ||
929 | |||
930 | a, b = T.doremote(L1, "return f()") | ||
931 | assert(a == 'alo' and b == '3') | ||
932 | |||
933 | T.doremote(L1, "_ERRORMESSAGE = nil") | ||
934 | -- error: `sin' is not defined | ||
935 | a, _, b = T.doremote(L1, "return sin(1)") | ||
936 | assert(a == nil and b == 2) -- 2 == run-time error | ||
937 | |||
938 | -- error: syntax error | ||
939 | a, b, c = T.doremote(L1, "return a+") | ||
940 | assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error | ||
941 | |||
942 | T.loadlib(L1) | ||
943 | a, b, c = T.doremote(L1, [[ | ||
944 | string = require'string' | ||
945 | a = require'_G'; assert(a == _G and require("_G") == a) | ||
946 | io = require'io'; assert(type(io.read) == "function") | ||
947 | assert(require("io") == io) | ||
948 | a = require'table'; assert(type(a.insert) == "function") | ||
949 | a = require'debug'; assert(type(a.getlocal) == "function") | ||
950 | a = require'math'; assert(type(a.sin) == "function") | ||
951 | return string.sub('okinama', 1, 2) | ||
952 | ]]) | ||
953 | assert(a == "ok") | ||
954 | |||
955 | T.closestate(L1); | ||
956 | |||
957 | |||
958 | L1 = T.newstate() | ||
959 | T.loadlib(L1) | ||
960 | T.doremote(L1, "a = {}") | ||
961 | T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; | ||
962 | settable -3]]) | ||
963 | assert(T.doremote(L1, "return a.x") == "1") | ||
964 | |||
965 | T.closestate(L1) | ||
966 | |||
967 | L1 = nil | ||
968 | |||
969 | print('+') | ||
970 | |||
971 | ------------------------------------------------------------------------- | ||
972 | -- testing memory limits | ||
973 | ------------------------------------------------------------------------- | ||
974 | print("memory-allocation errors") | ||
975 | |||
976 | checkerr("block too big", T.newuserdata, math.maxinteger) | ||
977 | collectgarbage() | ||
978 | local f = load"local a={}; for i=1,100000 do a[i]=i end" | ||
979 | T.alloccount(10) | ||
980 | checkerr("not enough memory", f) | ||
981 | T.alloccount() -- remove limit | ||
982 | |||
983 | -- test memory errors; increase limit for number of allocations one | ||
984 | -- by one, so that we get memory errors in all allocations of a given | ||
985 | -- task, until there is enough allocations to complete the task without | ||
986 | -- errors. | ||
987 | |||
988 | function testamem (s, f) | ||
989 | collectgarbage(); collectgarbage() | ||
990 | local M = 0 | ||
991 | local a,b = nil | ||
992 | while true do | ||
993 | T.alloccount(M) | ||
994 | a, b = pcall(f) | ||
995 | T.alloccount() -- remove limit | ||
996 | if a and b then break end -- stop when no more errors | ||
997 | if not a and not -- `real' error? | ||
998 | (string.find(b, "memory") or string.find(b, "overflow")) then | ||
999 | error(b, 0) -- propagate it | ||
1000 | end | ||
1001 | M = M + 1 -- increase allocation limit | ||
1002 | end | ||
1003 | print(string.format("limit for %s: %d allocations", s, M)) | ||
1004 | return b | ||
1005 | end | ||
1006 | |||
1007 | |||
1008 | -- doing nothing | ||
1009 | b = testamem("doing nothing", function () return 10 end) | ||
1010 | assert(b == 10) | ||
1011 | |||
1012 | -- testing memory errors when creating a new state | ||
1013 | |||
1014 | b = testamem("state creation", T.newstate) | ||
1015 | T.closestate(b); -- close new state | ||
1016 | |||
1017 | testamem("empty-table creation", function () | ||
1018 | return {} | ||
1019 | end) | ||
1020 | |||
1021 | testamem("string creation", function () | ||
1022 | return "XXX" .. "YYY" | ||
1023 | end) | ||
1024 | |||
1025 | testamem("coroutine creation", function() | ||
1026 | return coroutine.create(print) | ||
1027 | end) | ||
1028 | |||
1029 | |||
1030 | -- testing threads | ||
1031 | |||
1032 | -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) | ||
1033 | mt = T.testC("rawgeti R 1; return 1") | ||
1034 | assert(type(mt) == "thread" and coroutine.running() == mt) | ||
1035 | |||
1036 | |||
1037 | |||
1038 | function expand (n,s) | ||
1039 | if n==0 then return "" end | ||
1040 | local e = string.rep("=", n) | ||
1041 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", | ||
1042 | e, s, expand(n-1,s), e) | ||
1043 | end | ||
1044 | |||
1045 | G=0; collectgarbage(); a =collectgarbage("count") | ||
1046 | load(expand(20,"G=G+1"))() | ||
1047 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) | ||
1048 | |||
1049 | testamem("running code on new thread", function () | ||
1050 | return T.doonnewstack("x=1") == 0 -- try to create thread | ||
1051 | end) | ||
1052 | |||
1053 | |||
1054 | -- testing memory x compiler | ||
1055 | |||
1056 | testamem("loadstring", function () | ||
1057 | return load("x=1") -- try to do load a string | ||
1058 | end) | ||
1059 | |||
1060 | |||
1061 | local testprog = [[ | ||
1062 | local function foo () return end | ||
1063 | local t = {"x"} | ||
1064 | a = "aaa" | ||
1065 | for i = 1, #t do a=a..t[i] end | ||
1066 | return true | ||
1067 | ]] | ||
1068 | |||
1069 | -- testing memory x dofile | ||
1070 | _G.a = nil | ||
1071 | local t =os.tmpname() | ||
1072 | local f = assert(io.open(t, "w")) | ||
1073 | f:write(testprog) | ||
1074 | f:close() | ||
1075 | testamem("dofile", function () | ||
1076 | local a = loadfile(t) | ||
1077 | return a and a() | ||
1078 | end) | ||
1079 | assert(os.remove(t)) | ||
1080 | assert(_G.a == "aaax") | ||
1081 | |||
1082 | |||
1083 | -- other generic tests | ||
1084 | |||
1085 | testamem("gsub", function () | ||
1086 | local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) | ||
1087 | return (a == 'ablo ablo') | ||
1088 | end) | ||
1089 | |||
1090 | testamem("dump/undump", function () | ||
1091 | local a = load(testprog) | ||
1092 | local b = a and string.dump(a) | ||
1093 | a = b and load(b) | ||
1094 | return a and a() | ||
1095 | end) | ||
1096 | |||
1097 | local t = os.tmpname() | ||
1098 | testamem("file creation", function () | ||
1099 | local f = assert(io.open(t, 'w')) | ||
1100 | assert (not io.open"nomenaoexistente") | ||
1101 | io.close(f); | ||
1102 | return not loadfile'nomenaoexistente' | ||
1103 | end) | ||
1104 | assert(os.remove(t)) | ||
1105 | |||
1106 | testamem("table creation", function () | ||
1107 | local a, lim = {}, 10 | ||
1108 | for i=1,lim do a[i] = i; a[i..'a'] = {} end | ||
1109 | return (type(a[lim..'a']) == 'table' and a[lim] == lim) | ||
1110 | end) | ||
1111 | |||
1112 | testamem("constructors", function () | ||
1113 | local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} | ||
1114 | return (type(a) == 'table' and a.e == 5) | ||
1115 | end) | ||
1116 | |||
1117 | local a = 1 | ||
1118 | close = nil | ||
1119 | testamem("closure creation", function () | ||
1120 | function close (b) | ||
1121 | return function (x) return b + x end | ||
1122 | end | ||
1123 | return (close(2)(4) == 6) | ||
1124 | end) | ||
1125 | |||
1126 | testamem("using coroutines", function () | ||
1127 | local a = coroutine.wrap(function () | ||
1128 | coroutine.yield(string.rep("a", 10)) | ||
1129 | return {} | ||
1130 | end) | ||
1131 | assert(string.len(a()) == 10) | ||
1132 | return a() | ||
1133 | end) | ||
1134 | |||
1135 | do -- auxiliary buffer | ||
1136 | local lim = 100 | ||
1137 | local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end | ||
1138 | testamem("auxiliary buffer", function () | ||
1139 | return (#table.concat(a, ",") == 20*lim + lim - 1) | ||
1140 | end) | ||
1141 | end | ||
1142 | |||
1143 | testamem("growing stack", function () | ||
1144 | local function foo (n) | ||
1145 | if n == 0 then return 1 else return 1 + foo(n - 1) end | ||
1146 | end | ||
1147 | return foo(100) | ||
1148 | end) | ||
1149 | |||
1150 | do -- testing failing in 'lua_checkstack' | ||
1151 | local res = T.testC([[rawcheckstack 500000; return 1]]) | ||
1152 | assert(res == false) | ||
1153 | local L = T.newstate() | ||
1154 | T.alloccount(0) -- will be unable to reallocate the stack | ||
1155 | res = T.testC(L, [[rawcheckstack 5000; return 1]]) | ||
1156 | T.alloccount() | ||
1157 | T.closestate(L) | ||
1158 | assert(res == false) | ||
1159 | end | ||
1160 | |||
1161 | do -- closing state with no extra memory | ||
1162 | local L = T.newstate() | ||
1163 | T.alloccount(0) | ||
1164 | T.closestate(L) | ||
1165 | T.alloccount() | ||
1166 | end | ||
1167 | |||
1168 | do -- garbage collection with no extra memory | ||
1169 | local L = T.newstate() | ||
1170 | T.loadlib(L) | ||
1171 | local res = (T.doremote(L, [[ | ||
1172 | _ENV = require"_G" | ||
1173 | local T = require"T" | ||
1174 | local a = {} | ||
1175 | for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table | ||
1176 | local stsize, stuse = T.querystr() | ||
1177 | assert(stuse > 1000) | ||
1178 | local function foo (n) | ||
1179 | if n > 0 then foo(n - 1) end | ||
1180 | end | ||
1181 | foo(180) -- grow stack | ||
1182 | local _, stksize = T.stacklevel() | ||
1183 | assert(stksize > 180) | ||
1184 | a = nil | ||
1185 | T.alloccount(0) | ||
1186 | collectgarbage() | ||
1187 | T.alloccount() | ||
1188 | -- stack and string table could not be reallocated, | ||
1189 | -- so they kept their sizes (without errors) | ||
1190 | assert(select(2, T.stacklevel()) == stksize) | ||
1191 | assert(T.querystr() == stsize) | ||
1192 | return 'ok' | ||
1193 | ]])) | ||
1194 | assert(res == 'ok') | ||
1195 | T.closestate(L) | ||
1196 | end | ||
1197 | |||
1198 | print'+' | ||
1199 | |||
1200 | -- testing some auxlib functions | ||
1201 | local function gsub (a, b, c) | ||
1202 | a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c) | ||
1203 | assert(b == 5) | ||
1204 | return a | ||
1205 | end | ||
1206 | |||
1207 | assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//") | ||
1208 | assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.") | ||
1209 | assert(gsub("", "alo", "//") == "") | ||
1210 | assert(gsub("...", ".", "/.") == "/././.") | ||
1211 | assert(gsub("...", "...", "") == "") | ||
1212 | |||
1213 | |||
1214 | -- testing luaL_newmetatable | ||
1215 | local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3") | ||
1216 | assert(type(mt_xuxu) == "table" and res and top == 3) | ||
1217 | local d, res, top = T.testC("newmetatable xuxu; gettop; return 3") | ||
1218 | assert(mt_xuxu == d and not res and top == 3) | ||
1219 | d, res, top = T.testC("newmetatable xuxu1; gettop; return 3") | ||
1220 | assert(mt_xuxu ~= d and res and top == 3) | ||
1221 | |||
1222 | x = T.newuserdata(0); | ||
1223 | y = T.newuserdata(0); | ||
1224 | T.testC("pushstring xuxu; gettable R; setmetatable 2", x) | ||
1225 | assert(getmetatable(x) == mt_xuxu) | ||
1226 | |||
1227 | -- testing luaL_testudata | ||
1228 | -- correct metatable | ||
1229 | local res1, res2, top = T.testC([[testudata -1 xuxu | ||
1230 | testudata 2 xuxu | ||
1231 | gettop | ||
1232 | return 3]], x) | ||
1233 | assert(res1 and res2 and top == 4) | ||
1234 | |||
1235 | -- wrong metatable | ||
1236 | res1, res2, top = T.testC([[testudata -1 xuxu1 | ||
1237 | testudata 2 xuxu1 | ||
1238 | gettop | ||
1239 | return 3]], x) | ||
1240 | assert(not res1 and not res2 and top == 4) | ||
1241 | |||
1242 | -- non-existent type | ||
1243 | res1, res2, top = T.testC([[testudata -1 xuxu2 | ||
1244 | testudata 2 xuxu2 | ||
1245 | gettop | ||
1246 | return 3]], x) | ||
1247 | assert(not res1 and not res2 and top == 4) | ||
1248 | |||
1249 | -- userdata has no metatable | ||
1250 | res1, res2, top = T.testC([[testudata -1 xuxu | ||
1251 | testudata 2 xuxu | ||
1252 | gettop | ||
1253 | return 3]], y) | ||
1254 | assert(not res1 and not res2 and top == 4) | ||
1255 | |||
1256 | -- erase metatables | ||
1257 | do | ||
1258 | local r = debug.getregistry() | ||
1259 | assert(r.xuxu == mt_xuxu and r.xuxu1 == d) | ||
1260 | r.xuxu = nil; r.xuxu1 = nil | ||
1261 | end | ||
1262 | |||
1263 | print'OK' | ||
1264 | |||