aboutsummaryrefslogtreecommitdiff
path: root/testes/api.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/api.lua')
-rw-r--r--testes/api.lua1264
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
4if T==nil then
5 (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
6 return
7end
8
9local debug = require "debug"
10
11local pack = table.pack
12
13
14function 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
17end
18
19
20local function checkerr (msg, f, ...)
21 local stat, err = pcall(f, ...)
22 assert(not stat and string.find(err, msg))
23end
24
25
26print('testing C API')
27
28a = T.testC("pushvalue R; return 1")
29assert(a == debug.getregistry())
30
31
32-- absindex
33assert(T.testC("settop 10; absindex -1; return 1") == 10)
34assert(T.testC("settop 5; absindex -5; return 1") == 1)
35assert(T.testC("settop 10; absindex 1; return 1") == 1)
36assert(T.testC("settop 10; absindex R; return 1") < -10)
37
38-- testing alignment
39a = T.d2s(12458954321123.0)
40assert(a == string.pack("d", 12458954321123.0))
41assert(T.s2d(a) == 12458954321123.0)
42
43a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
44assert(a == 2 and b == 3 and not c)
45
46f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
47a,b,c = f()
48assert(a == 2 and b == 3 and not c)
49
50-- test that all trues are equal
51a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
52assert(a == b and a == true and c == false)
53a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
54 tobool -3; tobool -3; tobool -3; return 3"
55assert(a==false and b==true and c==false)
56
57
58a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
59assert(a == 40 and b == 5 and not c)
60
61t = pack(T.testC("settop 5; return *", 2, 3))
62tcheck(t, {n=4,2,3})
63
64t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
65assert(t.n == 10 and t[1] == nil and t[10] == nil)
66
67t = pack(T.testC("remove -2; return *", 2, 3, 4))
68tcheck(t, {n=2,2,4})
69
70t = pack(T.testC("insert -1; return *", 2, 3))
71tcheck(t, {n=2,2,3})
72
73t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
74tcheck(t, {n=4,2,5,3,4})
75
76t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
77tcheck(t, {n=3,5,3,4})
78
79t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
80tcheck(t, {n=3,2,3,5})
81
82t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
83tcheck(t, {n=3,2,4,5})
84
85t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
86tcheck(t, {n=4,2,3,3,5})
87
88t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
89tcheck(t, {n=4,2,3,4,3})
90
91do -- 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})
112end
113
114-- testing message handlers
115do
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
129end
130
131t = 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))
135tcheck(t, {n=7,2,3,4,5,10,40,90})
136
137t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
138tcheck(t, {n=1,"alo23joao12"})
139
140-- testing MULTRET
141t = pack(T.testC("call 2,-1; return *",
142 function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
143tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
144
145do -- 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")
151end
152
153
154-- testing globals
155_G.a = 14; _G.b = "a31"
156local a = {T.testC[[
157 getglobal a;
158 getglobal b;
159 getglobal b;
160 setglobal a;
161 return *
162]]}
163assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
164
165
166-- testing arith
167assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
168assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
169assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
170assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
171assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
172assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
173assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
174assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
175assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
176a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
177assert(a == 3.0 and math.type(a) == "float")
178a = T.testC("pushint 10; pushint 3; arith \\; return 1")
179assert(a == 3 and math.type(a) == "integer")
180a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
181assert(a == 13 and math.type(a) == "integer")
182a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
183assert(a == 13 and math.type(a) == "float")
184a,b,c = T.testC([[pushnum 1;
185 pushstring 10; arith _;
186 pushstring 5; return 3]])
187assert(a == 1 and b == -10 and c == "5")
188mt = {__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}
191a,b,c = setmetatable({4}, mt),
192 setmetatable({8}, mt),
193 setmetatable({-3}, mt)
194x,y,z = T.testC("arith +; return 2", 10, a, b)
195assert(x == 10 and y[1] == 12 and z == nil)
196assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
197assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
198 8 % (4 + (-3)*2))
199
200-- errors in arithmetic
201checkerr("divide by zero", T.testC, "arith \\", 10, 0)
202checkerr("%%0", T.testC, "arith %", 10, 0)
203
204
205-- testing lessthan and lessequal
206assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
207assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
208assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
209assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
210assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
211assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
212assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
213
214-- non-valid indices produce false
215assert(not T.testC("compare LT 1 4, return 1"))
216assert(not T.testC("compare LE 9 1, return 1"))
217assert(not T.testC("compare EQ 9 9, return 1"))
218
219local b = {__lt = function (a,b) return a[1] < b[1] end}
220local a1,a3,a4 = setmetatable({1}, b),
221 setmetatable({3}, b),
222 setmetatable({4}, b)
223assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
224assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
225assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
226a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
227assert(a == 20 and b == false)
228a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
229assert(a == 20 and b == false)
230a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
231assert(a == 20 and b == true)
232
233-- testing length
234local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
235a,b,c = T.testC([[
236 len 2;
237 Llen 2;
238 objsize 2;
239 return 3
240]], t)
241assert(a == 20 and b == 20 and c == 0)
242
243t.x = "234"; t[1] = 20
244a,b,c = T.testC([[
245 len 2;
246 Llen 2;
247 objsize 2;
248 return 3
249]], t)
250assert(a == "234" and b == 234 and c == 1)
251
252t.x = print; t[1] = 20
253a,c = T.testC([[
254 len 2;
255 objsize 2;
256 return 2
257]], t)
258assert(a == print and c == 1)
259
260
261-- testing __concat
262
263a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
264x,y = T.testC([[
265 pushnum 5
266 pushvalue 2;
267 pushvalue 2;
268 concat 2;
269 pushvalue -2;
270 return 2;
271]], a, a)
272assert(x == a..a and y == 5)
273
274-- concat with 0 elements
275assert(T.testC("concat 0; return 1") == "")
276
277-- concat with 1 element
278assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
279
280
281
282-- testing lua_is
283
284function B(x) return x and 1 or 0 end
285
286function 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))
302end
303
304assert(count(3) == 2)
305assert(count('alo') == 1)
306assert(count('32') == 2)
307assert(count({}) == 1)
308assert(count(print) == 2)
309assert(count(function () end) == 1)
310assert(count(nil) == 1)
311assert(count(io.stdin) == 1)
312assert(count(nil, 15) == 100)
313
314
315-- testing lua_to...
316
317function to (s, x, n)
318 n = n or 2
319 return T.testC(string.format("%s %d; return 1", s, n), x)
320end
321
322local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
323assert(debug.getupvalue(hfunc, 1))
324assert(to("tostring", {}) == nil)
325assert(to("tostring", "alo") == "alo")
326assert(to("tostring", 12) == "12")
327assert(to("tostring", 12, 3) == nil)
328assert(to("objsize", {}) == 0)
329assert(to("objsize", {1,2,3}) == 3)
330assert(to("objsize", "alo\0\0a") == 6)
331assert(to("objsize", T.newuserdata(0)) == 0)
332assert(to("objsize", T.newuserdata(101)) == 101)
333assert(to("objsize", 124) == 0)
334assert(to("objsize", true) == 0)
335assert(to("tonumber", {}) == 0)
336assert(to("tonumber", "12") == 12)
337assert(to("tonumber", "s2") == 0)
338assert(to("tonumber", 1, 20) == 0)
339assert(to("topointer", 10) == 0)
340assert(to("topointer", true) == 0)
341assert(to("topointer", T.pushuserdata(20)) == 20)
342assert(to("topointer", io.read) ~= 0) -- light C function
343assert(to("topointer", hfunc) ~= 0) -- "heavy" C function
344assert(to("topointer", function () end) ~= 0) -- Lua function
345assert(to("topointer", io.stdin) ~= 0) -- full userdata
346assert(to("func2num", 20) == 0)
347assert(to("func2num", T.pushuserdata(10)) == 0)
348assert(to("func2num", io.read) ~= 0) -- light C function
349assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
350a = to("tocfunction", math.deg)
351assert(a(3) == math.deg(3) and a == math.deg)
352
353
354print("testing panic function")
355do
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
386end
387
388-- testing deep C stack
389if 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'+'
399end
400
401local lim = _soft and 500 or 12000
402local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
403for i = 1,lim do
404 prog[#prog + 1] = "pushnum " .. i
405 prog[#prog + 1] = "pushnum " .. i * 10
406end
407
408prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
409prog[#prog + 1] = "insert " .. -(2*lim + 2)
410
411for i = 1,lim do
412 prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
413end
414
415prog[#prog + 1] = "return 2"
416
417prog = table.concat(prog, ";")
418local g, t = T.testC(prog)
419assert(g == _G)
420for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
421assert(next(t) == nil)
422prog, g, t = nil
423
424-- testing errors
425
426a = 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
433assert(type(a) == 'string' and x == 150)
434
435function check3(p, ...)
436 local arg = {...}
437 assert(#arg == 3)
438 assert(string.find(arg[3], p))
439end
440check3(":1:", T.testC("loadstring 2; return *", "x="))
441check3("%.", T.testC("loadfile 2; return *", "."))
442check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
443
444-- test errors in non protected threads
445function 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))
449end
450
451if 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")
456end
457print"+"
458
459
460-- testing table access
461
462do -- 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)
467end
468
469a = {x=0, y=12}
470x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
471 a, 3, "y", 4, "x")
472assert(x == 0 and y == 12)
473T.testC("settable -5", a, 3, 4, "x", 15)
474assert(a.x == 15)
475a[a] = print
476x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
477assert(x == print)
478T.testC("settable 2", a, "x") -- table and key are the same object!
479assert(a[a] == "x")
480
481b = setmetatable({p = a}, {})
482getmetatable(b).__index = function (t, i) return t.p[i] end
483k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
484assert(x == 15 and k == 35)
485k = T.testC("getfield 2 y, return 1", b)
486assert(k == 12)
487getmetatable(b).__index = function (t, i) return a[i] end
488getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
489y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
490assert(y == 12)
491k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
492assert(a.x == 16 and k == 4)
493a[b] = 'xuxu'
494y = T.testC("gettable 2, return 1", b)
495assert(y == 'xuxu')
496T.testC("settable 2", b, 19)
497assert(a[b] == 19)
498
499--
500do -- 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
513end
514
515-- testing next
516a = {}
517t = pack(T.testC("next; return *", a, nil))
518tcheck(t, {n=1,a})
519a = {a=3}
520t = pack(T.testC("next; return *", a, nil))
521tcheck(t, {n=3,a,'a',3})
522t = pack(T.testC("next; pop 1; next; return *", a, nil))
523tcheck(t, {n=1,a})
524
525
526
527-- testing upvalues
528
529do
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()
542end
543
544
545-- testing absent upvalues from C-function pointers
546assert(T.testC[[isnull U1; return 1]] == true)
547assert(T.testC[[isnull U100; return 1]] == true)
548assert(T.testC[[pushvalue U1; return 1]] == nil)
549
550local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
551assert(T.upvalue(f, 1) == 10 and
552 T.upvalue(f, 2) == 20 and
553 T.upvalue(f, 3) == nil)
554T.upvalue(f, 2, "xuxu")
555assert(T.upvalue(f, 2) == "xuxu")
556
557
558-- large closures
559do
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"))
569end
570
571
572
573-- testing get/setuservalue
574-- bug in 5.1.2
575checkerr("got number", debug.setuservalue, 3, {})
576checkerr("got nil", debug.setuservalue, nil, {})
577checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
578
579-- testing multiple user values
580local b = T.newuserdata(0, 10)
581for i = 1, 10 do
582 local v, p = debug.getuservalue(b, i)
583 assert(v == nil and p)
584end
585do -- 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)
590end
591local t = {true, false, 4.56, print, {}, b, "XYZ"}
592for k, v in ipairs(t) do
593 debug.setuservalue(b, v, k)
594end
595for k, v in ipairs(t) do
596 local v1, p = debug.getuservalue(b, k)
597 assert(v1 == v and p)
598end
599
600assert(debug.getuservalue(4) == nil)
601
602debug.setuservalue(b, function () return 10 end, 10)
603collectgarbage() -- function should not be collected
604assert(debug.getuservalue(b, 10)() == 10)
605
606debug.setuservalue(b, 134)
607collectgarbage() -- number should not be a problem for collector
608assert(debug.getuservalue(b) == 134)
609
610
611-- test barrier for uservalues
612do
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)
620end
621
622-- long chain of userdata
623for i = 1, 1000 do
624 local bb = T.newuserdata(0, 1)
625 debug.setuservalue(bb, b)
626 b = bb
627end
628collectgarbage() -- nothing should not be collected
629for i = 1, 1000 do
630 b = debug.getuservalue(b)
631end
632assert(debug.getuservalue(b).x == 100)
633b = nil
634
635
636-- testing locks (refs)
637
638-- reuse of references
639local i = T.ref{}
640T.unref(i)
641assert(T.ref{} == i)
642
643Arr = {}
644Lim = 100
645for i=1,Lim do -- lock many objects
646 Arr[i] = T.ref({})
647end
648
649assert(T.ref(nil) == -1 and T.getref(-1) == nil)
650T.unref(-1); T.unref(-1)
651
652for i=1,Lim do -- unlock all them
653 T.unref(Arr[i])
654end
655
656function 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
663end
664
665
666for i=1,Lim do -- lock many objects
667 Arr[i] = T.ref({})
668end
669
670for i=1,Lim,2 do -- unlock half of them
671 T.unref(Arr[i])
672end
673
674assert(type(T.getref(Arr[2])) == 'table')
675
676
677assert(T.getref(-1) == nil)
678
679
680a = T.ref({})
681
682collectgarbage()
683
684assert(type(T.getref(a)) == 'table')
685
686
687-- colect in cl the `val' of all collected userdata
688tt = {}
689cl = {n=0}
690A = nil; B = nil
691local F
692F = 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
710end
711tt.__gc = F
712
713-- test whether udate collection frees memory in the right time
714do
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")
743end
744
745
746collectgarbage("stop")
747
748-- create 3 userdatas with tag `tt'
749a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
750b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
751c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
752
753-- create userdata without meta table
754x = T.newuserdata(4)
755y = T.newuserdata(0)
756
757checkerr("FILE%* expected, got userdata", io.input, a)
758checkerr("FILE%* expected, got userdata", io.input, x)
759
760assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
761
762d=T.ref(a);
763e=T.ref(b);
764f=T.ref(c);
765t = {T.getref(d), T.getref(e), T.getref(f)}
766assert(t[1] == a and t[2] == b and t[3] == c)
767
768t=nil; a=nil; c=nil;
769T.unref(e); T.unref(f)
770
771collectgarbage()
772
773-- check that unref objects have been collected
774assert(#cl == 1 and cl[1] == nc)
775
776x = T.getref(d)
777assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
778x =nil
779tt.b = b -- create cycle
780tt=nil -- frees tt for GC
781A = nil
782b = nil
783T.unref(d);
784n5 = T.newuserdata(0)
785debug.setmetatable(n5, {__gc=F})
786n5 = T.udataval(n5)
787collectgarbage()
788assert(#cl == 4)
789-- check order of collection
790assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
791
792collectgarbage"restart"
793
794
795a, na = {}, {}
796for 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])
800end
801cl = {}
802a = nil; collectgarbage()
803assert(#cl == 30)
804for i=1,30 do assert(cl[i] == na[i]) end
805na = nil
806
807
808for i=2,Lim,2 do -- unlock the other half
809 T.unref(Arr[i])
810end
811
812x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
813assert(T.testC("objsize 2; return 1", x) == 41)
814cl = {}
815a = {[x] = 1}
816x = T.udataval(x)
817collectgarbage()
818-- old `x' cannot be collected (`a' still uses it)
819assert(#cl == 0)
820for n in pairs(a) do a[n] = undef end
821collectgarbage()
822assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
823
824-- testing lua_equal
825assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
826assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
827assert(T.testC("compare EQ 2 3; return 1", nil, nil))
828assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
829assert(not T.testC("compare EQ 2 3; return 1"))
830assert(not T.testC("compare EQ 2 3; return 1", 3))
831
832-- testing lua_equal with fallbacks
833do
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))
848end
849
850print'+'
851
852
853
854-- testing changing hooks during hooks
855_G.t = {}
856T.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)
863local a = 1 -- counting
864a = 1 -- counting
865a = 1 -- count hook (set line hook)
866a = 1 -- line hook
867a = 1 -- line hook
868debug.sethook()
869t = _G.t
870assert(t[1] == "line")
871line = t[2]
872assert(t[3] == "line" and t[4] == line + 1)
873assert(t[5] == "line" and t[6] == line + 2)
874assert(t[7] == nil)
875
876
877-------------------------------------------------------------------------
878do -- 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")
905end
906-------------------------------------------------------------------------
907-- test for userdata vals
908do
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")
916end
917
918
919-------------------------------------------------------------------------
920-- testing multiple states
921T.closestate(T.newstate());
922L1 = T.newstate()
923assert(L1)
924
925assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
926
927
928assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
929
930a, b = T.doremote(L1, "return f()")
931assert(a == 'alo' and b == '3')
932
933T.doremote(L1, "_ERRORMESSAGE = nil")
934-- error: `sin' is not defined
935a, _, b = T.doremote(L1, "return sin(1)")
936assert(a == nil and b == 2) -- 2 == run-time error
937
938-- error: syntax error
939a, b, c = T.doremote(L1, "return a+")
940assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
941
942T.loadlib(L1)
943a, 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]])
953assert(a == "ok")
954
955T.closestate(L1);
956
957
958L1 = T.newstate()
959T.loadlib(L1)
960T.doremote(L1, "a = {}")
961T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
962 settable -3]])
963assert(T.doremote(L1, "return a.x") == "1")
964
965T.closestate(L1)
966
967L1 = nil
968
969print('+')
970
971-------------------------------------------------------------------------
972-- testing memory limits
973-------------------------------------------------------------------------
974print("memory-allocation errors")
975
976checkerr("block too big", T.newuserdata, math.maxinteger)
977collectgarbage()
978local f = load"local a={}; for i=1,100000 do a[i]=i end"
979T.alloccount(10)
980checkerr("not enough memory", f)
981T.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
988function 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
1005end
1006
1007
1008-- doing nothing
1009b = testamem("doing nothing", function () return 10 end)
1010assert(b == 10)
1011
1012-- testing memory errors when creating a new state
1013
1014b = testamem("state creation", T.newstate)
1015T.closestate(b); -- close new state
1016
1017testamem("empty-table creation", function ()
1018 return {}
1019end)
1020
1021testamem("string creation", function ()
1022 return "XXX" .. "YYY"
1023end)
1024
1025testamem("coroutine creation", function()
1026 return coroutine.create(print)
1027end)
1028
1029
1030-- testing threads
1031
1032-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
1033mt = T.testC("rawgeti R 1; return 1")
1034assert(type(mt) == "thread" and coroutine.running() == mt)
1035
1036
1037
1038function 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)
1043end
1044
1045G=0; collectgarbage(); a =collectgarbage("count")
1046load(expand(20,"G=G+1"))()
1047assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1048
1049testamem("running code on new thread", function ()
1050 return T.doonnewstack("x=1") == 0 -- try to create thread
1051end)
1052
1053
1054-- testing memory x compiler
1055
1056testamem("loadstring", function ()
1057 return load("x=1") -- try to do load a string
1058end)
1059
1060
1061local testprog = [[
1062local function foo () return end
1063local t = {"x"}
1064a = "aaa"
1065for i = 1, #t do a=a..t[i] end
1066return true
1067]]
1068
1069-- testing memory x dofile
1070_G.a = nil
1071local t =os.tmpname()
1072local f = assert(io.open(t, "w"))
1073f:write(testprog)
1074f:close()
1075testamem("dofile", function ()
1076 local a = loadfile(t)
1077 return a and a()
1078end)
1079assert(os.remove(t))
1080assert(_G.a == "aaax")
1081
1082
1083-- other generic tests
1084
1085testamem("gsub", function ()
1086 local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
1087 return (a == 'ablo ablo')
1088end)
1089
1090testamem("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()
1095end)
1096
1097local t = os.tmpname()
1098testamem("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'
1103end)
1104assert(os.remove(t))
1105
1106testamem("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)
1110end)
1111
1112testamem("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)
1115end)
1116
1117local a = 1
1118close = nil
1119testamem("closure creation", function ()
1120 function close (b)
1121 return function (x) return b + x end
1122 end
1123 return (close(2)(4) == 6)
1124end)
1125
1126testamem("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()
1133end)
1134
1135do -- 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)
1141end
1142
1143testamem("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)
1148end)
1149
1150do -- 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)
1159end
1160
1161do -- closing state with no extra memory
1162 local L = T.newstate()
1163 T.alloccount(0)
1164 T.closestate(L)
1165 T.alloccount()
1166end
1167
1168do -- 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)
1196end
1197
1198print'+'
1199
1200-- testing some auxlib functions
1201local 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
1205end
1206
1207assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
1208assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
1209assert(gsub("", "alo", "//") == "")
1210assert(gsub("...", ".", "/.") == "/././.")
1211assert(gsub("...", "...", "") == "")
1212
1213
1214-- testing luaL_newmetatable
1215local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1216assert(type(mt_xuxu) == "table" and res and top == 3)
1217local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1218assert(mt_xuxu == d and not res and top == 3)
1219d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
1220assert(mt_xuxu ~= d and res and top == 3)
1221
1222x = T.newuserdata(0);
1223y = T.newuserdata(0);
1224T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
1225assert(getmetatable(x) == mt_xuxu)
1226
1227-- testing luaL_testudata
1228-- correct metatable
1229local res1, res2, top = T.testC([[testudata -1 xuxu
1230 testudata 2 xuxu
1231 gettop
1232 return 3]], x)
1233assert(res1 and res2 and top == 4)
1234
1235-- wrong metatable
1236res1, res2, top = T.testC([[testudata -1 xuxu1
1237 testudata 2 xuxu1
1238 gettop
1239 return 3]], x)
1240assert(not res1 and not res2 and top == 4)
1241
1242-- non-existent type
1243res1, res2, top = T.testC([[testudata -1 xuxu2
1244 testudata 2 xuxu2
1245 gettop
1246 return 3]], x)
1247assert(not res1 and not res2 and top == 4)
1248
1249-- userdata has no metatable
1250res1, res2, top = T.testC([[testudata -1 xuxu
1251 testudata 2 xuxu
1252 gettop
1253 return 3]], y)
1254assert(not res1 and not res2 and top == 4)
1255
1256-- erase metatables
1257do
1258 local r = debug.getregistry()
1259 assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
1260 r.xuxu = nil; r.xuxu1 = nil
1261end
1262
1263print'OK'
1264