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 / api.lua

1416 lines · 40029 bytes Raw Blame History
1-- $Id: testes/api.lua $
2-- See Copyright Notice in file lua.h
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
14local 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
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
28local a = 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
43local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
44assert(a == 2 and b == 3 and not c)
45
46local f = 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
61local t = 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
115-- testing warnings
116T.testC([[
117 warningC "#This should be a"
118 warningC " single "
119 warning "warning"
120 warningC "#This should be "
121 warning "another one"
122]])
123
124
125-- testing message handlers
126do
127 local f = T.makeCfunc[[
128 getglobal error
129 pushstring bola
130 pcall 1 1 1 # call 'error' with given handler
131 pushstatus
132 return 2 # return error message and status
133 ]]
134
135 local msg, st = f(string.upper) -- function handler
136 assert(st == "ERRRUN" and msg == "BOLA")
137 local msg, st = f(string.len) -- function handler
138 assert(st == "ERRRUN" and msg == 4)
139
140end
141
142t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
143 insert 2; pushvalue 1; remove 1; insert 1; \
144 insert -2; pushvalue -2; remove -3; return *",
145 2, 3, 4, 5, 10, 40, 90))
146tcheck(t, {n=7,2,3,4,5,10,40,90})
147
148t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
149tcheck(t, {n=1,"alo23joao12"})
150
151-- testing MULTRET
152t = pack(T.testC("call 2,-1; return *",
153 function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
154tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
155
156do -- test returning more results than fit in the caller stack
157 local a = {}
158 for i=1,1000 do a[i] = true end; a[999] = 10
159 local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
160 table.unpack, a)
161 assert(b == "10")
162end
163
164
165do -- testing multiple returns
166 local function foo (n)
167 if n > 0 then return n, foo(n - 1) end
168 end
169
170 local t = {T.testC("call 1 10; return 10", foo, 20)}
171 assert(t[1] == 20 and t[10] == 11 and t[11] == nil)
172
173 local t = table.pack(T.testC("call 1 10; return 10", foo, 2))
174 assert(t[1] == 2 and t[2] == 1 and t[3] == nil and t.n == 10)
175
176 local t = {T.testC([[
177 checkstack 300 "error"; call 1 250; return 250]], foo, 250)}
178 assert(t[1] == 250 and t[250] == 1 and t[251] == nil)
179end
180
181
182-- testing globals
183_G.AA = 14; _G.BB = "a31"
184local a = {T.testC[[
185 getglobal AA;
186 getglobal BB;
187 getglobal BB;
188 setglobal AA;
189 return *
190]]}
191assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
192
193_G.AA, _G.BB = nil
194
195-- testing arith
196assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
197assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
198assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
199assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
200assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
201assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
202assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
203assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
204assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
205a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
206assert(a == 3.0 and math.type(a) == "float")
207a = T.testC("pushint 10; pushint 3; arith \\; return 1")
208assert(a == 3 and math.type(a) == "integer")
209a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
210assert(a == 13 and math.type(a) == "integer")
211a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
212assert(a == 13 and math.type(a) == "float")
213a,b,c = T.testC([[pushnum 1;
214 pushstring 10; arith _;
215 pushstring 5; return 3]])
216assert(a == 1 and b == -10 and c == "5")
217local mt = {
218 __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
219 __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
220 __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
221a,b,c = setmetatable({4}, mt),
222 setmetatable({8}, mt),
223 setmetatable({-3}, mt)
224local x,y,z = T.testC("arith +; return 2", 10, a, b)
225assert(x == 10 and y[1] == 12 and z == nil)
226assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
227assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
228 8 % (4 + (-3)*2))
229
230-- errors in arithmetic
231checkerr("divide by zero", T.testC, "arith \\", 10, 0)
232checkerr("%%0", T.testC, "arith %", 10, 0)
233
234
235-- testing lessthan and lessequal
236assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
237assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
238assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
239assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
240assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
241assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
242assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
243
244-- non-valid indices produce false
245assert(not T.testC("compare LT 1 4, return 1"))
246assert(not T.testC("compare LE 9 1, return 1"))
247assert(not T.testC("compare EQ 9 9, return 1"))
248
249local b = {__lt = function (a,b) return a[1] < b[1] end,
250 __le = function (a,b) return a[1] <= b[1] end}
251local a1,a3,a4 = setmetatable({1}, b),
252 setmetatable({3}, b),
253 setmetatable({4}, b)
254assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
255assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
256assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
257a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
258assert(a == 20 and b == false)
259a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
260assert(a == 20 and b == false)
261a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
262assert(a == 20 and b == true)
263
264
265do -- testing lessthan and lessequal with metamethods
266 local mt = {__lt = function (a,b) return a[1] < b[1] end,
267 __le = function (a,b) return a[1] <= b[1] end,
268 __eq = function (a,b) return a[1] == b[1] end}
269 local function O (x)
270 return setmetatable({x}, mt)
271 end
272
273 local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
274 assert(a == true and b == 10)
275 local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
276 assert(a == false and b == 10)
277 local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
278 assert(a == true and b == 10)
279end
280
281-- testing length
282local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
283a,b,c = T.testC([[
284 len 2;
285 Llen 2;
286 objsize 2;
287 return 3
288]], t)
289assert(a == 20 and b == 20 and c == 0)
290
291t.x = "234"; t[1] = 20
292a,b,c = T.testC([[
293 len 2;
294 Llen 2;
295 objsize 2;
296 return 3
297]], t)
298assert(a == "234" and b == 234 and c == 1)
299
300t.x = print; t[1] = 20
301a,c = T.testC([[
302 len 2;
303 objsize 2;
304 return 2
305]], t)
306assert(a == print and c == 1)
307
308
309-- testing __concat
310
311a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
312x,y = T.testC([[
313 pushnum 5
314 pushvalue 2;
315 pushvalue 2;
316 concat 2;
317 pushvalue -2;
318 return 2;
319]], a, a)
320assert(x == a..a and y == 5)
321
322-- concat with 0 elements
323assert(T.testC("concat 0; return 1") == "")
324
325-- concat with 1 element
326assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
327
328
329
330-- testing lua_is
331
332local function B (x) return x and 1 or 0 end
333
334local function count (x, n)
335 n = n or 2
336 local prog = [[
337 isnumber %d;
338 isstring %d;
339 isfunction %d;
340 iscfunction %d;
341 istable %d;
342 isuserdata %d;
343 isnil %d;
344 isnull %d;
345 return 8
346 ]]
347 prog = string.format(prog, n, n, n, n, n, n, n, n)
348 local a,b,c,d,e,f,g,h = T.testC(prog, x)
349 return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
350end
351
352assert(count(3) == 2)
353assert(count('alo') == 1)
354assert(count('32') == 2)
355assert(count({}) == 1)
356assert(count(print) == 2)
357assert(count(function () end) == 1)
358assert(count(nil) == 1)
359assert(count(io.stdin) == 1)
360assert(count(nil, 15) == 100)
361
362
363-- testing lua_to...
364
365local function to (s, x, n)
366 n = n or 2
367 return T.testC(string.format("%s %d; return 1", s, n), x)
368end
369
370local null = T.pushuserdata(0)
371local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
372assert(debug.getupvalue(hfunc, 1))
373assert(to("tostring", {}) == nil)
374assert(to("tostring", "alo") == "alo")
375assert(to("tostring", 12) == "12")
376assert(to("tostring", 12, 3) == nil)
377assert(to("objsize", {}) == 0)
378assert(to("objsize", {1,2,3}) == 3)
379assert(to("objsize", "alo\0\0a") == 6)
380assert(to("objsize", T.newuserdata(0)) == 0)
381assert(to("objsize", T.newuserdata(101)) == 101)
382assert(to("objsize", 124) == 0)
383assert(to("objsize", true) == 0)
384assert(to("tonumber", {}) == 0)
385assert(to("tonumber", "12") == 12)
386assert(to("tonumber", "s2") == 0)
387assert(to("tonumber", 1, 20) == 0)
388assert(to("topointer", 10) == null)
389assert(to("topointer", true) == null)
390assert(to("topointer", nil) == null)
391assert(to("topointer", "abc") ~= null)
392assert(to("topointer", string.rep("x", 10)) ==
393 to("topointer", string.rep("x", 10))) -- short strings
394do -- long strings
395 local s1 = string.rep("x", 300)
396 local s2 = string.rep("x", 300)
397 assert(to("topointer", s1) ~= to("topointer", s2))
398end
399assert(to("topointer", T.pushuserdata(20)) ~= null)
400assert(to("topointer", io.read) ~= null) -- light C function
401assert(to("topointer", hfunc) ~= null) -- "heavy" C function
402assert(to("topointer", function () end) ~= null) -- Lua function
403assert(to("topointer", io.stdin) ~= null) -- full userdata
404assert(to("func2num", 20) == 0)
405assert(to("func2num", T.pushuserdata(10)) == 0)
406assert(to("func2num", io.read) ~= 0) -- light C function
407assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
408a = to("tocfunction", math.deg)
409assert(a(3) == math.deg(3) and a == math.deg)
410
411
412print("testing panic function")
413do
414 -- trivial error
415 assert(T.checkpanic("pushstring hi; error") == "hi")
416
417 -- thread status inside panic (bug in 5.4.4)
418 assert(T.checkpanic("pushstring hi; error", "threadstatus; return 2") ==
419 "ERRRUN")
420
421 -- using the stack inside panic
422 assert(T.checkpanic("pushstring hi; error;",
423 [[checkstack 5 XX
424 pushstring ' alo'
425 pushstring ' mundo'
426 concat 3]]) == "hi alo mundo")
427
428 -- "argerror" without frames
429 assert(T.checkpanic("loadstring 4 name bt") ==
430 "bad argument #4 (string expected, got no value)")
431
432
433 -- memory error + thread status
434 local x = T.checkpanic(
435 [[ alloccount 0 # force a memory error in next line
436 newtable
437 ]],
438 [[
439 alloccount -1 # allow free allocations again
440 pushstring XX
441 threadstatus
442 concat 2 # to make sure message came from here
443 return 1
444 ]])
445 T.alloccount()
446 assert(x == "XX" .. "not enough memory")
447
448 -- stack error
449 if not _soft then
450 local msg = T.checkpanic[[
451 pushstring "function f() f() end"
452 loadstring -1 name t; call 0 0
453 getglobal f; call 0 0
454 ]]
455 assert(string.find(msg, "stack overflow"))
456 end
457
458 -- exit in panic still close to-be-closed variables
459 assert(T.checkpanic([[
460 pushstring "return {__close = function () Y = 'ho'; end}"
461 newtable
462 loadstring -2 name t
463 call 0 1
464 setmetatable -2
465 toclose -1
466 pushstring "hi"
467 error
468 ]],
469 [[
470 getglobal Y
471 concat 2 # concat original error with global Y
472 ]]) == "hiho")
473
474
475end
476
477-- testing deep C stack
478if not _soft then
479 print("testing stack overflow")
480 collectgarbage("stop")
481 checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep
482 -- too deep (with no message)
483 checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
484 local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
485 checkerr("overflow", T.testC, s)
486 collectgarbage("restart")
487 print'+'
488end
489
490
491
492local lim = _soft and 500 or 12000
493local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
494for i = 1,lim do
495 prog[#prog + 1] = "pushnum " .. i
496 prog[#prog + 1] = "pushnum " .. i * 10
497end
498
499prog[#prog + 1] = "rawgeti R !G" -- get global table in registry
500prog[#prog + 1] = "insert " .. -(2*lim + 2)
501
502for i = 1,lim do
503 prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
504end
505
506prog[#prog + 1] = "return 2"
507
508prog = table.concat(prog, ";")
509local g, t = T.testC(prog)
510assert(g == _G)
511for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
512assert(next(t) == nil)
513prog, g, t = nil
514
515do -- shrink stack
516 local m1, m2 = 0, collectgarbage"count" * 1024
517 while m1 ~= m2 do -- repeat until stable
518 collectgarbage()
519 m1 = m2
520 m2 = collectgarbage"count" * 1024
521 end
522end
523
524
525-- testing errors
526
527a = T.testC([[
528 loadstring 2 name t; pcall 0 1 0;
529 pushvalue 3; insert -2; pcall 1 1 0;
530 pcall 0 0 0;
531 return 1
532]], "XX=150", function (a) assert(a==nil); return 3 end)
533
534assert(type(a) == 'string' and XX == 150)
535_G.XX = nil
536
537local function check3(p, ...)
538 local arg = {...}
539 assert(#arg == 3)
540 assert(string.find(arg[3], p))
541end
542check3(":1:", T.testC("loadstring 2 name t; return *", "x="))
543check3("%.", T.testC("loadfile 2; return *", "."))
544check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
545
546-- test errors in non protected threads
547local function checkerrnopro (code, msg)
548 local th = coroutine.create(function () end) -- create new thread
549 local stt, err = pcall(T.testC, th, code) -- run code there
550 assert(not stt and string.find(err, msg))
551end
552
553
554do
555 print("testing load of binaries in fixed buffers")
556 local source = {}
557 local N = 1000
558 -- create a somewhat "large" source
559 for i = 1, N do source[i] = "X = X + 1; " end
560 -- add a long string to the source
561 source[#source + 1] = string.format("Y = '%s'", string.rep("a", N));
562 source = table.concat(source)
563 -- give chunk an explicit name to avoid using source as name
564 source = load(source, "name1")
565 -- dump without debug information
566 source = string.dump(source, true)
567 -- each "X=X+1" generates 4 opcodes with 4 bytes each, plus the string
568 assert(#source > N * 4 * 4 + N)
569 collectgarbage(); collectgarbage()
570 local m1 = collectgarbage"count" * 1024
571 -- load dump using fixed buffer
572 local code = T.testC([[
573 loadstring 2 name B;
574 return 1
575 ]], source)
576 collectgarbage()
577 local m2 = collectgarbage"count" * 1024
578 -- load used fewer than 400 bytes. Code alone has more than 3*N bytes,
579 -- and string literal has N bytes. Both were not loaded.
580 assert(m2 > m1 and m2 - m1 < 400)
581 X = 0; code(); assert(X == N and Y == string.rep("a", N))
582 X = nil; Y = nil
583
584 -- testing debug info in fixed buffers
585 source = {"X = 0"}
586 for i = 2, 300 do source[i] = "X = X + 1" end
587 source[#source + 1] = "X = X + {}" -- error in last line
588 source = table.concat(source, "\n")
589 source = load(source, "name1")
590 source = string.dump(source)
591 -- load dump using fixed buffer
592 local code = T.testC([[
593 loadstring 2 name B;
594 return 1
595 ]], source)
596 checkerr(":301:", code) -- correct line information
597end
598
599
600if not _soft then
601 collectgarbage("stop") -- avoid __gc with full stack
602 checkerrnopro("pushnum 3; call 0 0", "attempt to call")
603 print"testing stack overflow in unprotected thread"
604 function F () F() end
605 checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
606 F = nil
607 collectgarbage("restart")
608end
609print"+"
610
611
612-- testing table access
613
614do -- getp/setp
615 local a = {}
616 local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
617 assert(a == a1)
618 assert(a[T.pushuserdata(1)] == 20)
619 local a1, res = T.testC("rawgetp -1 1; return 2", a)
620 assert(a == a1 and res == 20)
621end
622
623
624do -- using the table itself as index
625 local a = {}
626 a[a] = 10
627 local prog = "gettable -1; return *"
628 local res = {T.testC(prog, a)}
629 assert(#res == 2 and res[1] == prog and res[2] == 10)
630
631 local prog = "settable -2; return *"
632 local res = {T.testC(prog, a, 20)}
633 assert(a[a] == 20)
634 assert(#res == 1 and res[1] == prog)
635
636 -- raw
637 a[a] = 10
638 local prog = "rawget -1; return *"
639 local res = {T.testC(prog, a)}
640 assert(#res == 2 and res[1] == prog and res[2] == 10)
641
642 local prog = "rawset -2; return *"
643 local res = {T.testC(prog, a, 20)}
644 assert(a[a] == 20)
645 assert(#res == 1 and res[1] == prog)
646
647 -- using the table as the value to set
648 local prog = "rawset -1; return *"
649 local res = {T.testC(prog, 30, a)}
650 assert(a[30] == a)
651 assert(#res == 1 and res[1] == prog)
652
653 local prog = "settable -1; return *"
654 local res = {T.testC(prog, 40, a)}
655 assert(a[40] == a)
656 assert(#res == 1 and res[1] == prog)
657
658 local prog = "rawseti -1 100; return *"
659 local res = {T.testC(prog, a)}
660 assert(a[100] == a)
661 assert(#res == 1 and res[1] == prog)
662
663 local prog = "seti -1 200; return *"
664 local res = {T.testC(prog, a)}
665 assert(a[200] == a)
666 assert(#res == 1 and res[1] == prog)
667end
668
669a = {x=0, y=12}
670x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
671 a, 3, "y", 4, "x")
672assert(x == 0 and y == 12)
673T.testC("settable -5", a, 3, 4, "x", 15)
674assert(a.x == 15)
675a[a] = print
676x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
677assert(x == print)
678T.testC("settable 2", a, "x") -- table and key are the same object!
679assert(a[a] == "x")
680
681b = setmetatable({p = a}, {})
682getmetatable(b).__index = function (t, i) return t.p[i] end
683local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
684assert(x == 15 and k == 35)
685k = T.testC("getfield 2 y, return 1", b)
686assert(k == 12)
687getmetatable(b).__index = function (t, i) return a[i] end
688getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
689y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
690assert(y == 12)
691k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
692assert(a.x == 16 and k == 4)
693a[b] = 'xuxu'
694y = T.testC("gettable 2, return 1", b)
695assert(y == 'xuxu')
696T.testC("settable 2", b, 19)
697assert(a[b] == 19)
698
699--
700do -- testing getfield/setfield with long keys
701 local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
702 local a = T.testC([[
703 getfield 2 _012345678901234567890123456789012345678901234567890123456789
704 return 1
705 ]], t)
706 assert(a == 32)
707 local a = T.testC([[
708 pushnum 33
709 setglobal _012345678901234567890123456789012345678901234567890123456789
710 ]])
711 assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
712 _012345678901234567890123456789012345678901234567890123456789 = nil
713end
714
715-- testing next
716a = {}
717t = pack(T.testC("next; return *", a, nil))
718tcheck(t, {n=1,a})
719a = {a=3}
720t = pack(T.testC("next; return *", a, nil))
721tcheck(t, {n=3,a,'a',3})
722t = pack(T.testC("next; pop 1; next; return *", a, nil))
723tcheck(t, {n=1,a})
724
725
726
727-- testing upvalues
728
729do
730 local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
731 t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
732 assert(b == 10 and c == 20 and type(t) == 'table')
733 a, b = A([[tostring U3; tonumber U4; return 2]])
734 assert(a == nil and b == 0)
735 A([[pushnum 100; pushnum 200; replace U2; replace U1]])
736 b, c = A([[pushvalue U1; pushvalue U2; return 2]])
737 assert(b == 100 and c == 200)
738 A([[replace U2; replace U1]], {x=1}, {x=2})
739 b, c = A([[pushvalue U1; pushvalue U2; return 2]])
740 assert(b.x == 1 and c.x == 2)
741 T.checkmemory()
742end
743
744
745-- testing absent upvalues from C-function pointers
746assert(T.testC[[isnull U1; return 1]] == true)
747assert(T.testC[[isnull U100; return 1]] == true)
748assert(T.testC[[pushvalue U1; return 1]] == nil)
749
750local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
751assert(T.upvalue(f, 1) == 10 and
752 T.upvalue(f, 2) == 20 and
753 T.upvalue(f, 3) == nil)
754T.upvalue(f, 2, "xuxu")
755assert(T.upvalue(f, 2) == "xuxu")
756
757
758-- large closures
759do
760 local A = "checkstack 300 msg;" ..
761 string.rep("pushnum 10;", 255) ..
762 "pushcclosure 255; return 1"
763 A = T.testC(A)
764 for i=1,255 do
765 assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
766 end
767 assert(A("isnull U256; return 1"))
768 assert(not A("isnil U256; return 1"))
769end
770
771
772
773-- testing get/setuservalue
774-- bug in 5.1.2
775checkerr("got number", debug.setuservalue, 3, {})
776checkerr("got nil", debug.setuservalue, nil, {})
777checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
778
779-- testing multiple user values
780local b = T.newuserdata(0, 10)
781for i = 1, 10 do
782 local v, p = debug.getuservalue(b, i)
783 assert(v == nil and p)
784end
785do -- indices out of range
786 local v, p = debug.getuservalue(b, -2)
787 assert(v == nil and not p)
788 local v, p = debug.getuservalue(b, 11)
789 assert(v == nil and not p)
790end
791local t = {true, false, 4.56, print, {}, b, "XYZ"}
792for k, v in ipairs(t) do
793 debug.setuservalue(b, v, k)
794end
795for k, v in ipairs(t) do
796 local v1, p = debug.getuservalue(b, k)
797 assert(v1 == v and p)
798end
799
800assert(not debug.getuservalue(4))
801
802debug.setuservalue(b, function () return 10 end, 10)
803collectgarbage() -- function should not be collected
804assert(debug.getuservalue(b, 10)() == 10)
805
806debug.setuservalue(b, 134)
807collectgarbage() -- number should not be a problem for collector
808assert(debug.getuservalue(b) == 134)
809
810
811-- test barrier for uservalues
812do
813 local oldmode = collectgarbage("incremental")
814 T.gcstate("enteratomic")
815 assert(T.gccolor(b) == "black")
816 debug.setuservalue(b, {x = 100})
817 T.gcstate("pause") -- complete collection
818 assert(debug.getuservalue(b).x == 100) -- uvalue should be there
819 collectgarbage(oldmode)
820end
821
822-- long chain of userdata
823for i = 1, 1000 do
824 local bb = T.newuserdata(0, 1)
825 debug.setuservalue(bb, b)
826 b = bb
827end
828collectgarbage() -- nothing should not be collected
829for i = 1, 1000 do
830 b = debug.getuservalue(b)
831end
832assert(debug.getuservalue(b).x == 100)
833b = nil
834
835
836-- testing locks (refs)
837
838-- reuse of references
839local i = T.ref{}
840T.unref(i)
841assert(T.ref{} == i)
842
843local Arr = {}
844local Lim = 100
845for i=1,Lim do -- lock many objects
846 Arr[i] = T.ref({})
847end
848
849assert(T.ref(nil) == -1 and T.getref(-1) == nil)
850T.unref(-1); T.unref(-1)
851
852for i=1,Lim do -- unlock all them
853 T.unref(Arr[i])
854end
855
856local function printlocks ()
857 local f = T.makeCfunc("gettable R; return 1")
858 local n = f("n")
859 print("n", n)
860 for i=0,n do
861 print(i, f(i))
862 end
863end
864
865
866for i=1,Lim do -- lock many objects
867 Arr[i] = T.ref({})
868end
869
870for i=1,Lim,2 do -- unlock half of them
871 T.unref(Arr[i])
872end
873
874assert(type(T.getref(Arr[2])) == 'table')
875
876
877assert(T.getref(-1) == nil)
878
879
880a = T.ref({})
881
882collectgarbage()
883
884assert(type(T.getref(a)) == 'table')
885
886
887-- colect in cl the `val' of all collected userdata
888local tt = {}
889local cl = {n=0}
890A = nil; B = nil
891local F
892F = function (x)
893 local udval = T.udataval(x)
894 table.insert(cl, udval)
895 local d = T.newuserdata(100) -- create garbage
896 d = nil
897 assert(debug.getmetatable(x).__gc == F)
898 assert(load("table.insert({}, {})"))() -- create more garbage
899 assert(not collectgarbage()) -- GC during GC (no op)
900 local dummy = {} -- create more garbage during GC
901 if A ~= nil then
902 assert(type(A) == "userdata")
903 assert(T.udataval(A) == B)
904 debug.getmetatable(A) -- just access it
905 end
906 A = x -- resurrect userdata
907 B = udval
908 return 1,2,3
909end
910tt.__gc = F
911
912
913-- test whether udate collection frees memory in the right time
914do
915 collectgarbage();
916 collectgarbage();
917 local x = collectgarbage("count");
918 local a = T.newuserdata(5001)
919 assert(T.testC("objsize 2; return 1", a) == 5001)
920 assert(collectgarbage("count") >= x+4)
921 a = nil
922 collectgarbage();
923 assert(collectgarbage("count") <= x+1)
924 -- udata without finalizer
925 x = collectgarbage("count")
926 collectgarbage("stop")
927 for i=1,1000 do T.newuserdata(0) end
928 assert(collectgarbage("count") > x+10)
929 collectgarbage()
930 assert(collectgarbage("count") <= x+1)
931 -- udata with finalizer
932 collectgarbage()
933 x = collectgarbage("count")
934 collectgarbage("stop")
935 a = {__gc = function () end}
936 for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
937 assert(collectgarbage("count") >= x+10)
938 collectgarbage() -- this collection only calls TM, without freeing memory
939 assert(collectgarbage("count") >= x+10)
940 collectgarbage() -- now frees memory
941 assert(collectgarbage("count") <= x+1)
942 collectgarbage("restart")
943end
944
945
946collectgarbage("stop")
947
948-- create 3 userdatas with tag `tt'
949a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
950b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
951c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
952
953-- create userdata without meta table
954x = T.newuserdata(4)
955y = T.newuserdata(0)
956
957checkerr("FILE%* expected, got userdata", io.input, a)
958checkerr("FILE%* expected, got userdata", io.input, x)
959
960assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
961
962-- Test references in an arbitrary table
963local reftable = {}
964local d = T.ref(a, reftable);
965local e = T.ref(b, reftable);
966local f = T.ref(c, reftable);
967t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)}
968assert(t[1] == a and t[2] == b and t[3] == c)
969
970t=nil; a=nil; c=nil;
971T.unref(e, reftable); T.unref(f, reftable)
972
973collectgarbage()
974
975-- check that unref objects have been collected
976assert(#cl == 1 and cl[1] == nc)
977
978x = T.getref(d, reftable)
979assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
980x =nil
981tt.b = b -- create cycle
982tt=nil -- frees tt for GC
983A = nil
984b = nil
985T.unref(d, reftable);
986local n5 = T.newuserdata(0)
987debug.setmetatable(n5, {__gc=F})
988n5 = T.udataval(n5)
989collectgarbage()
990assert(#cl == 4)
991-- check order of collection
992assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
993
994-- reuse a reference in 'reftable'
995T.unref(T.ref(23, reftable), reftable)
996
997do -- check reftable
998 local count = 0
999 local i = 1
1000 while reftable[i] ~= 0 do
1001 i = reftable[i] -- traverse linked list of free references
1002 count = count + 1
1003 end
1004 -- maximum number of simultaneously locked objects was 3
1005 assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1]
1006end
1007
1008
1009collectgarbage"restart"
1010
1011
1012a, na = {}, {}
1013for i=30,1,-1 do
1014 a[i] = T.newuserdata(0)
1015 debug.setmetatable(a[i], {__gc=F})
1016 na[i] = T.udataval(a[i])
1017end
1018cl = {}
1019a = nil; collectgarbage()
1020assert(#cl == 30)
1021for i=1,30 do assert(cl[i] == na[i]) end
1022na = nil
1023
1024
1025for i=2,Lim,2 do -- unlock the other half
1026 T.unref(Arr[i])
1027end
1028
1029x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
1030assert(T.testC("objsize 2; return 1", x) == 41)
1031cl = {}
1032a = {[x] = 1}
1033x = T.udataval(x)
1034collectgarbage()
1035-- old `x' cannot be collected (`a' still uses it)
1036assert(#cl == 0)
1037for n in pairs(a) do a[n] = undef end
1038collectgarbage()
1039assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
1040
1041-- testing lua_equal
1042assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
1043assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
1044assert(T.testC("compare EQ 2 3; return 1", nil, nil))
1045assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
1046assert(not T.testC("compare EQ 2 3; return 1"))
1047assert(not T.testC("compare EQ 2 3; return 1", 3))
1048
1049-- testing lua_equal with fallbacks
1050do
1051 local map = {}
1052 local t = {__eq = function (a,b) return map[a] == map[b] end}
1053 local function f(x)
1054 local u = T.newuserdata(0)
1055 debug.setmetatable(u, t)
1056 map[u] = x
1057 return u
1058 end
1059 assert(f(10) == f(10))
1060 assert(f(10) ~= f(11))
1061 assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
1062 assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
1063 t.__eq = nil
1064 assert(f(10) ~= f(10))
1065end
1066
1067print'+'
1068
1069
1070
1071-- testing changing hooks during hooks
1072_G.TT = {}
1073T.sethook([[
1074 # set a line hook after 3 count hooks
1075 sethook 4 0 '
1076 getglobal TT;
1077 pushvalue -3; append -2
1078 pushvalue -2; append -2
1079 ']], "c", 3)
1080local a = 1 -- counting
1081a = 1 -- counting
1082a = 1 -- count hook (set line hook)
1083a = 1 -- line hook
1084a = 1 -- line hook
1085debug.sethook()
1086local t = _G.TT
1087assert(t[1] == "line")
1088local line = t[2]
1089assert(t[3] == "line" and t[4] == line + 1)
1090assert(t[5] == "line" and t[6] == line + 2)
1091assert(t[7] == nil)
1092_G.TT = nil
1093
1094
1095-------------------------------------------------------------------------
1096do -- testing errors during GC
1097 warn("@off")
1098 collectgarbage("stop")
1099 local a = {}
1100 for i=1,20 do
1101 a[i] = T.newuserdata(i) -- creates several udata
1102 end
1103 for i=1,20,2 do -- mark half of them to raise errors during GC
1104 debug.setmetatable(a[i],
1105 {__gc = function (x) error("@expected error in gc") end})
1106 end
1107 for i=2,20,2 do -- mark the other half to count and to create more garbage
1108 debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
1109 end
1110 a = nil
1111 _G.A = 0
1112 collectgarbage()
1113 assert(A == 10) -- number of normal collections
1114 collectgarbage("restart")
1115 warn("@on")
1116end
1117_G.A = nil
1118-------------------------------------------------------------------------
1119-- test for userdata vals
1120do
1121 local a = {}; local lim = 30
1122 for i=0,lim do a[i] = T.pushuserdata(i) end
1123 for i=0,lim do assert(T.udataval(a[i]) == i) end
1124 for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
1125 for i=0,lim do a[a[i]] = i end
1126 for i=0,lim do a[T.pushuserdata(i)] = i end
1127 assert(type(tostring(a[1])) == "string")
1128end
1129
1130
1131-------------------------------------------------------------------------
1132-- testing multiple states
1133T.closestate(T.newstate());
1134L1 = T.newstate()
1135assert(L1)
1136
1137assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
1138
1139
1140assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
1141
1142a, b = T.doremote(L1, "return f()")
1143assert(a == 'alo' and b == '3')
1144
1145T.doremote(L1, "_ERRORMESSAGE = nil")
1146-- error: `sin' is not defined
1147a, b, c = T.doremote(L1, "return sin(1)")
1148assert(a == nil and c == 2) -- 2 == run-time error
1149
1150-- error: syntax error
1151a, b, c = T.doremote(L1, "return a+")
1152assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
1153
1154T.loadlib(L1, 2, ~2) -- load only 'package', preload all others
1155a, b, c = T.doremote(L1, [[
1156 string = require'string'
1157 local initialG = _G -- not loaded yet
1158 local a = require'_G'; assert(a == _G and require("_G") == a)
1159 assert(initialG == nil and io == nil) -- now we have 'assert'
1160 io = require'io'; assert(type(io.read) == "function")
1161 assert(require("io") == io)
1162 a = require'table'; assert(type(a.insert) == "function")
1163 a = require'debug'; assert(type(a.getlocal) == "function")
1164 a = require'math'; assert(type(a.sin) == "function")
1165 return string.sub('okinama', 1, 2)
1166]])
1167assert(a == "ok")
1168
1169T.closestate(L1);
1170
1171
1172L1 = T.newstate()
1173T.loadlib(L1, 0, 0)
1174T.doremote(L1, "a = {}")
1175T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
1176 settable -3]])
1177assert(T.doremote(L1, "return a.x") == "1")
1178
1179T.closestate(L1)
1180
1181L1 = nil
1182
1183print('+')
1184-------------------------------------------------------------------------
1185-- testing to-be-closed variables
1186-------------------------------------------------------------------------
1187print"testing to-be-closed variables"
1188
1189do
1190 local openresource = {}
1191
1192 local function newresource ()
1193 local x = setmetatable({10}, {__close = function(y)
1194 assert(openresource[#openresource] == y)
1195 openresource[#openresource] = nil
1196 y[1] = y[1] + 1
1197 end})
1198 openresource[#openresource + 1] = x
1199 return x
1200 end
1201
1202 local a, b = T.testC([[
1203 call 0 1 # create resource
1204 pushnil
1205 toclose -2 # mark call result to be closed
1206 toclose -1 # mark nil to be closed (will be ignored)
1207 return 2
1208 ]], newresource)
1209 assert(a[1] == 11 and b == nil)
1210 assert(#openresource == 0) -- was closed
1211
1212 -- repeat the test, but calling function in a 'multret' context
1213 local a = {T.testC([[
1214 call 0 1 # create resource
1215 toclose 2 # mark it to be closed
1216 return 2
1217 ]], newresource)}
1218 assert(type(a[1]) == "string" and a[2][1] == 11)
1219 assert(#openresource == 0) -- was closed
1220
1221 -- closing by error
1222 local a, b = pcall(T.makeCfunc[[
1223 call 0 1 # create resource
1224 toclose -1 # mark it to be closed
1225 pushvalue -1 # replicate it as error object
1226 error # resource right after error object
1227 ]], newresource)
1228 assert(a == false and b[1] == 11)
1229 assert(#openresource == 0) -- was closed
1230
1231 -- non-closable value
1232 local a, b = pcall(T.makeCfunc[[
1233 newtable # create non-closable object
1234 toclose -1 # mark it to be closed (should raise an error)
1235 abort # will not be executed
1236 ]])
1237 assert(a == false and
1238 string.find(b, "non%-closable value"))
1239
1240 local function check (n)
1241 assert(#openresource == n)
1242 end
1243
1244 -- closing resources with 'closeslot'
1245 _ENV.xxx = true
1246 local a = T.testC([[
1247 pushvalue 2 # stack: S, NR, CH, NR
1248 call 0 1 # create resource; stack: S, NR, CH, R
1249 toclose -1 # mark it to be closed
1250 pushvalue 2 # stack: S, NR, CH, R, NR
1251 call 0 1 # create another resource; stack: S, NR, CH, R, R
1252 toclose -1 # mark it to be closed
1253 pushvalue 3 # stack: S, NR, CH, R, R, CH
1254 pushint 2 # there should be two open resources
1255 call 1 0 # stack: S, NR, CH, R, R
1256 closeslot -1 # close second resource
1257 pushvalue 3 # stack: S, NR, CH, R, R, CH
1258 pushint 1 # there should be one open resource
1259 call 1 0 # stack: S, NR, CH, R, R
1260 closeslot 4
1261 setglobal "xxx" # previous op. erased the slot
1262 pop 1 # pop other resource from the stack
1263 pushint *
1264 return 1 # return stack size
1265 ]], newresource, check)
1266 assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack
1267
1268 -- closing resources with 'pop'
1269 local a = T.testC([[
1270 pushvalue 2 # stack: S, NR, CH, NR
1271 call 0 1 # create resource; stack: S, NR, CH, R
1272 toclose -1 # mark it to be closed
1273 pushvalue 2 # stack: S, NR, CH, R, NR
1274 call 0 1 # create another resource; stack: S, NR, CH, R, R
1275 toclose -1 # mark it to be closed
1276 pushvalue 3 # stack: S, NR, CH, R, R, CH
1277 pushint 2 # there should be two open resources
1278 call 1 0 # stack: S, NR, CH, R, R
1279 pop 1 # pop second resource
1280 pushvalue 3 # stack: S, NR, CH, R, CH
1281 pushint 1 # there should be one open resource
1282 call 1 0 # stack: S, NR, CH, R
1283 pop 1 # pop other resource from the stack
1284 pushvalue 3 # stack: S, NR, CH, CH
1285 pushint 0 # there should be no open resources
1286 call 1 0 # stack: S, NR, CH
1287 pushint *
1288 return 1 # return stack size
1289 ]], newresource, check)
1290 assert(a == 3) -- no extra items left in the stack
1291
1292 -- non-closable value
1293 local a, b = pcall(T.makeCfunc[[
1294 pushint 32
1295 toclose -1
1296 ]])
1297 assert(not a and string.find(b, "(C temporary)"))
1298
1299end
1300
1301
1302do -- testing failing in 'lua_checkstack'
1303 local res = T.testC([[rawcheckstack 500000; return 1]])
1304 assert(res == false)
1305 local L = T.newstate()
1306 T.alloccount(0) -- will be unable to reallocate the stack
1307 res = T.testC(L, [[rawcheckstack 5000; return 1]])
1308 T.alloccount()
1309 T.closestate(L)
1310 assert(res == false)
1311end
1312
1313do -- closing state with no extra memory
1314 local L = T.newstate()
1315 T.alloccount(0)
1316 T.closestate(L)
1317 T.alloccount()
1318end
1319
1320do -- garbage collection with no extra memory
1321 local L = T.newstate()
1322 T.loadlib(L, 1 | 2, 0) -- load _G and 'package'
1323 local res = (T.doremote(L, [[
1324 _ENV = _G
1325 assert(string == nil)
1326 local a = {}
1327 for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
1328 local stsize, stuse = T.querystr()
1329 assert(stuse > 1000)
1330 local function foo (n)
1331 if n > 0 then foo(n - 1) end
1332 end
1333 foo(180) -- grow stack
1334 local _, stksize = T.stacklevel()
1335 assert(stksize > 180)
1336 a = nil
1337 T.alloccount(0)
1338 collectgarbage()
1339 T.alloccount()
1340 -- stack and string table could not be reallocated,
1341 -- so they kept their sizes (without errors)
1342 assert(select(2, T.stacklevel()) == stksize)
1343 assert(T.querystr() == stsize)
1344 return 'ok'
1345 ]]))
1346 assert(res == 'ok')
1347 T.closestate(L)
1348end
1349
1350print'+'
1351
1352-- testing some auxlib functions
1353local function gsub (a, b, c)
1354 a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
1355 assert(b == 5)
1356 return a
1357end
1358
1359assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
1360assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
1361assert(gsub("", "alo", "//") == "")
1362assert(gsub("...", ".", "/.") == "/././.")
1363assert(gsub("...", "...", "") == "")
1364
1365
1366-- testing luaL_newmetatable
1367local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1368assert(type(mt_xuxu) == "table" and res and top == 3)
1369local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1370assert(mt_xuxu == d and not res and top == 3)
1371d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
1372assert(mt_xuxu ~= d and res and top == 3)
1373
1374x = T.newuserdata(0);
1375y = T.newuserdata(0);
1376T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
1377assert(getmetatable(x) == mt_xuxu)
1378
1379-- testing luaL_testudata
1380-- correct metatable
1381local res1, res2, top = T.testC([[testudata -1 xuxu
1382 testudata 2 xuxu
1383 gettop
1384 return 3]], x)
1385assert(res1 and res2 and top == 4)
1386
1387-- wrong metatable
1388res1, res2, top = T.testC([[testudata -1 xuxu1
1389 testudata 2 xuxu1
1390 gettop
1391 return 3]], x)
1392assert(not res1 and not res2 and top == 4)
1393
1394-- non-existent type
1395res1, res2, top = T.testC([[testudata -1 xuxu2
1396 testudata 2 xuxu2
1397 gettop
1398 return 3]], x)
1399assert(not res1 and not res2 and top == 4)
1400
1401-- userdata has no metatable
1402res1, res2, top = T.testC([[testudata -1 xuxu
1403 testudata 2 xuxu
1404 gettop
1405 return 3]], y)
1406assert(not res1 and not res2 and top == 4)
1407
1408-- erase metatables
1409do
1410 local r = debug.getregistry()
1411 assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
1412 r.xuxu = nil; r.xuxu1 = nil
1413end
1414
1415print'OK'
1416