@@ -1,1543 +0,0 @@
1−-- $Id: testes/api.lua $
2−-- See Copyright Notice in file all.lua
5− (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
9−local debug = require "debug"
11−local pack = table.pack
14−-- standard error message for memory errors
15−local MEMERRMSG = "not enough memory"
17−local function tcheck (t1, t2)
18− assert(t1.n == (t2.n or #t2) + 1)
19− for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
23−local function checkerr (msg, f, ...)
24− local stat, err = pcall(f, ...)
25− assert(not stat and string.find(err, msg))
29−print('testing C API')
31−local a = T.testC("pushvalue R; return 1")
32−assert(a == debug.getregistry())
36−assert(T.testC("settop 10; absindex -1; return 1") == 10)
37−assert(T.testC("settop 5; absindex -5; return 1") == 1)
38−assert(T.testC("settop 10; absindex 1; return 1") == 1)
39−assert(T.testC("settop 10; absindex R; return 1") < -10)
42−a = T.d2s(12458954321123.0)
43−assert(a == string.pack("d", 12458954321123.0))
44−assert(T.s2d(a) == 12458954321123.0)
46−local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
47−assert(a == 2 and b == 3 and not c)
49−local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
51−assert(a == 2 and b == 3 and not c)
53−-- test that all trues are equal
54−a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
55−assert(a == b and a == true and c == false)
56−a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
57− tobool -3; tobool -3; tobool -3; return 3"
58−assert(a==false and b==true and c==false)
61−a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
62−assert(a == 40 and b == 5 and not c)
64−local t = pack(T.testC("settop 5; return *", 2, 3))
67−t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
68−assert(t.n == 10 and t[1] == nil and t[10] == nil)
70−t = pack(T.testC("remove -2; return *", 2, 3, 4))
73−t = pack(T.testC("insert -1; return *", 2, 3))
76−t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
77−tcheck(t, {n=4,2,5,3,4})
79−t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
80−tcheck(t, {n=3,5,3,4})
82−t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
83−tcheck(t, {n=3,2,3,5})
85−t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
86−tcheck(t, {n=3,2,4,5})
88−t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
89−tcheck(t, {n=4,2,3,3,5})
91−t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
92−tcheck(t, {n=4,2,3,4,3})
94−do -- testing 'rotate'
95− local t = {10, 20, 30, 40, 50, 60}
97− local s = string.format("rotate 2 %d; return 7", i)
98− local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
100− table.insert(t, 1, table.remove(t))
103− t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40))
104− tcheck(t, {10, 20, 40, 30})
105− t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40))
106− tcheck(t, {10, 20, 40, 30})
108− -- some corner cases
109− t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40))
110− tcheck(t, {10, 20, 30, 40})
111− t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40))
112− tcheck(t, {10, 20, 30, 40})
113− t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40))
114− tcheck(t, {10, 20, 30, 40})
120− warningC "#This shold be a"
123− warningC "#This should be "
124− warning "another one"
128−-- testing message handlers
130− local f = T.makeCfunc[[
133− pcall 1 1 1 # call 'error' with given handler
135− return 2 # return error message and status
138− local msg, st = f(string.upper) -- function handler
139− assert(st == "ERRRUN" and msg == "BOLA")
140− local msg, st = f(string.len) -- function handler
141− assert(st == "ERRRUN" and msg == 4)
145−t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
146− insert 2; pushvalue 1; remove 1; insert 1; \
147− insert -2; pushvalue -2; remove -3; return *",
148− 2, 3, 4, 5, 10, 40, 90))
149−tcheck(t, {n=7,2,3,4,5,10,40,90})
151−t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
152−tcheck(t, {n=1,"alo23joao12"})
155−t = pack(T.testC("call 2,-1; return *",
156− function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
157−tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
159−do -- test returning more results than fit in the caller stack
161− for i=1,1000 do a[i] = true end; a[999] = 10
162− local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
169−_G.AA = 14; _G.BB = "a31"
177−assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
182−assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
183−assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
184−assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
185−assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
186−assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
187−assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
188−assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
189−assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
190−assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
191−a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
192−assert(a == 3.0 and math.type(a) == "float")
193−a = T.testC("pushint 10; pushint 3; arith \\; return 1")
194−assert(a == 3 and math.type(a) == "integer")
195−a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
196−assert(a == 13 and math.type(a) == "integer")
197−a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
198−assert(a == 13 and math.type(a) == "float")
199−a,b,c = T.testC([[pushnum 1;
200− pushstring 10; arith _;
201− pushstring 5; return 3]])
202−assert(a == 1 and b == -10 and c == "5")
204− __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
205− __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
206− __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
207−a,b,c = setmetatable({4}, mt),
208− setmetatable({8}, mt),
209− setmetatable({-3}, mt)
210−local x,y,z = T.testC("arith +; return 2", 10, a, b)
211−assert(x == 10 and y[1] == 12 and z == nil)
212−assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
213−assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
216−-- errors in arithmetic
217−checkerr("divide by zero", T.testC, "arith \\", 10, 0)
218−checkerr("%%0", T.testC, "arith %", 10, 0)
221−-- testing lessthan and lessequal
222−assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
223−assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
224−assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
225−assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
226−assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
227−assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
228−assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
230−-- non-valid indices produce false
231−assert(not T.testC("compare LT 1 4, return 1"))
232−assert(not T.testC("compare LE 9 1, return 1"))
233−assert(not T.testC("compare EQ 9 9, return 1"))
235−local b = {__lt = function (a,b) return a[1] < b[1] end}
236−local a1,a3,a4 = setmetatable({1}, b),
237− setmetatable({3}, b),
238− setmetatable({4}, b)
239−assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
240−assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
241−assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
242−a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
243−assert(a == 20 and b == false)
244−a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
245−assert(a == 20 and b == false)
246−a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
247−assert(a == 20 and b == true)
250−do -- testing lessthan and lessequal with metamethods
251− local mt = {__lt = function (a,b) return a[1] < b[1] end,
252− __le = function (a,b) return a[1] <= b[1] end,
253− __eq = function (a,b) return a[1] == b[1] end}
254− local function O (x)
255− return setmetatable({x}, mt)
258− local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
259− assert(a == true and b == 10)
260− local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
261− assert(a == false and b == 10)
262− local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
263− assert(a == true and b == 10)
267−local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
274−assert(a == 20 and b == 20 and c == 0)
276−t.x = "234"; t[1] = 20
283−assert(a == "234" and b == 234 and c == 1)
285−t.x = print; t[1] = 20
291−assert(a == print and c == 1)
296−a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
305−assert(x == a..a and y == 5)
307−-- concat with 0 elements
308−assert(T.testC("concat 0; return 1") == "")
310−-- concat with 1 element
311−assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
317−local function B (x) return x and 1 or 0 end
319−local function count (x, n)
332− prog = string.format(prog, n, n, n, n, n, n, n, n)
333− local a,b,c,d,e,f,g,h = T.testC(prog, x)
334− return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
337−assert(count(3) == 2)
338−assert(count('alo') == 1)
339−assert(count('32') == 2)
340−assert(count({}) == 1)
341−assert(count(print) == 2)
342−assert(count(function () end) == 1)
343−assert(count(nil) == 1)
344−assert(count(io.stdin) == 1)
345−assert(count(nil, 15) == 100)
350−local function to (s, x, n)
352− return T.testC(string.format("%s %d; return 1", s, n), x)
355−local null = T.pushuserdata(0)
356−local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
357−assert(debug.getupvalue(hfunc, 1))
358−assert(to("tostring", {}) == nil)
359−assert(to("tostring", "alo") == "alo")
360−assert(to("tostring", 12) == "12")
361−assert(to("tostring", 12, 3) == nil)
362−assert(to("objsize", {}) == 0)
363−assert(to("objsize", {1,2,3}) == 3)
364−assert(to("objsize", "alo\0\0a") == 6)
365−assert(to("objsize", T.newuserdata(0)) == 0)
366−assert(to("objsize", T.newuserdata(101)) == 101)
367−assert(to("objsize", 124) == 0)
368−assert(to("objsize", true) == 0)
369−assert(to("tonumber", {}) == 0)
370−assert(to("tonumber", "12") == 12)
371−assert(to("tonumber", "s2") == 0)
372−assert(to("tonumber", 1, 20) == 0)
373−assert(to("topointer", 10) == null)
374−assert(to("topointer", true) == null)
375−assert(to("topointer", nil) == null)
376−assert(to("topointer", "abc") ~= null)
377−assert(to("topointer", string.rep("x", 10)) ==
378− to("topointer", string.rep("x", 10))) -- short strings
380− local s1 = string.rep("x", 300)
381− local s2 = string.rep("x", 300)
382− assert(to("topointer", s1) ~= to("topointer", s2))
384−assert(to("topointer", T.pushuserdata(20)) ~= null)
385−assert(to("topointer", io.read) ~= null) -- light C function
386−assert(to("topointer", hfunc) ~= null) -- "heavy" C function
387−assert(to("topointer", function () end) ~= null) -- Lua function
388−assert(to("topointer", io.stdin) ~= null) -- full userdata
389−assert(to("func2num", 20) == 0)
390−assert(to("func2num", T.pushuserdata(10)) == 0)
391−assert(to("func2num", io.read) ~= 0) -- light C function
392−assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
393−a = to("tocfunction", math.deg)
394−assert(a(3) == math.deg(3) and a == math.deg)
397−print("testing panic function")
400− assert(T.checkpanic("pushstring hi; error") == "hi")
402− -- using the stack inside panic
403− assert(T.checkpanic("pushstring hi; error;",
407− concat 3]]) == "hi alo mundo")
409− -- "argerror" without frames
410− assert(T.checkpanic("loadstring 4") ==
411− "bad argument #4 (string expected, got no value)")
415− T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
416− assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
417− T.totalmem(0) -- restore high limit
421− local msg = T.checkpanic[[
422− pushstring "function f() f() end"
423− loadstring -1; call 0 0
424− getglobal f; call 0 0
426− assert(string.find(msg, "stack overflow"))
429− -- exit in panic still close to-be-closed variables
430− assert(T.checkpanic([[
431− pushstring "return {__close = function () Y = 'ho'; end}"
442− concat 2 # concat original error with global Y
448−-- testing deep C stack
450− print("testing stack overflow")
451− collectgarbage("stop")
452− checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep
453− -- too deep (with no message)
454− checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
455− local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
456− checkerr("overflow", T.testC, s)
457− collectgarbage("restart")
461−local lim = _soft and 500 or 12000
462−local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
464− prog[#prog + 1] = "pushnum " .. i
465− prog[#prog + 1] = "pushnum " .. i * 10
468−prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
469−prog[#prog + 1] = "insert " .. -(2*lim + 2)
472− prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
475−prog[#prog + 1] = "return 2"
477−prog = table.concat(prog, ";")
478−local g, t = T.testC(prog)
480−for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
481−assert(next(t) == nil)
487− loadstring 2; pcall 0 1 0;
488− pushvalue 3; insert -2; pcall 1 1 0;
491−]], "XX=150", function (a) assert(a==nil); return 3 end)
493−assert(type(a) == 'string' and XX == 150)
496−local function check3(p, ...)
499− assert(string.find(arg[3], p))
501−check3(":1:", T.testC("loadstring 2; return *", "x="))
502−check3("%.", T.testC("loadfile 2; return *", "."))
503−check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
505−-- test errors in non protected threads
506−local function checkerrnopro (code, msg)
507− local th = coroutine.create(function () end) -- create new thread
508− local stt, err = pcall(T.testC, th, code) -- run code there
509− assert(not stt and string.find(err, msg))
513− collectgarbage("stop") -- avoid __gc with full stack
514− checkerrnopro("pushnum 3; call 0 0", "attempt to call")
515− print"testing stack overflow in unprotected thread"
516− function F () F() end
517− checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
519− collectgarbage("restart")
524−-- testing table access
528− local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
530− assert(a[T.pushuserdata(1)] == 20)
531− local a1, res = T.testC("rawgetp -1 1; return 2", a)
532− assert(a == a1 and res == 20)
536−do -- using the table itself as index
539− local prog = "gettable -1; return *"
540− local res = {T.testC(prog, a)}
541− assert(#res == 2 and res[1] == prog and res[2] == 10)
543− local prog = "settable -2; return *"
544− local res = {T.testC(prog, a, 20)}
546− assert(#res == 1 and res[1] == prog)
550− local prog = "rawget -1; return *"
551− local res = {T.testC(prog, a)}
552− assert(#res == 2 and res[1] == prog and res[2] == 10)
554− local prog = "rawset -2; return *"
555− local res = {T.testC(prog, a, 20)}
557− assert(#res == 1 and res[1] == prog)
559− -- using the table as the value to set
560− local prog = "rawset -1; return *"
561− local res = {T.testC(prog, 30, a)}
563− assert(#res == 1 and res[1] == prog)
565− local prog = "settable -1; return *"
566− local res = {T.testC(prog, 40, a)}
568− assert(#res == 1 and res[1] == prog)
570− local prog = "rawseti -1 100; return *"
571− local res = {T.testC(prog, a)}
573− assert(#res == 1 and res[1] == prog)
575− local prog = "seti -1 200; return *"
576− local res = {T.testC(prog, a)}
578− assert(#res == 1 and res[1] == prog)
582−x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
584−assert(x == 0 and y == 12)
585−T.testC("settable -5", a, 3, 4, "x", 15)
588−x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
590−T.testC("settable 2", a, "x") -- table and key are the same object!
593−b = setmetatable({p = a}, {})
594−getmetatable(b).__index = function (t, i) return t.p[i] end
595−local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
596−assert(x == 15 and k == 35)
597−k = T.testC("getfield 2 y, return 1", b)
599−getmetatable(b).__index = function (t, i) return a[i] end
600−getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
601−y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
603−k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
604−assert(a.x == 16 and k == 4)
606−y = T.testC("gettable 2, return 1", b)
608−T.testC("settable 2", b, 19)
612−do -- testing getfield/setfield with long keys
613− local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
614− local a = T.testC([[
615− getfield 2 _012345678901234567890123456789012345678901234567890123456789
619− local a = T.testC([[
621− setglobal _012345678901234567890123456789012345678901234567890123456789
623− assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
624− _012345678901234567890123456789012345678901234567890123456789 = nil
629−t = pack(T.testC("next; return *", a, nil))
632−t = pack(T.testC("next; return *", a, nil))
633−tcheck(t, {n=3,a,'a',3})
634−t = pack(T.testC("next; pop 1; next; return *", a, nil))
642− local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
643− t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
644− assert(b == 10 and c == 20 and type(t) == 'table')
645− a, b = A([[tostring U3; tonumber U4; return 2]])
646− assert(a == nil and b == 0)
647− A([[pushnum 100; pushnum 200; replace U2; replace U1]])
648− b, c = A([[pushvalue U1; pushvalue U2; return 2]])
649− assert(b == 100 and c == 200)
650− A([[replace U2; replace U1]], {x=1}, {x=2})
651− b, c = A([[pushvalue U1; pushvalue U2; return 2]])
652− assert(b.x == 1 and c.x == 2)
657−-- testing absent upvalues from C-function pointers
658−assert(T.testC[[isnull U1; return 1]] == true)
659−assert(T.testC[[isnull U100; return 1]] == true)
660−assert(T.testC[[pushvalue U1; return 1]] == nil)
662−local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
663−assert(T.upvalue(f, 1) == 10 and
664− T.upvalue(f, 2) == 20 and
665− T.upvalue(f, 3) == nil)
666−T.upvalue(f, 2, "xuxu")
667−assert(T.upvalue(f, 2) == "xuxu")
672− local A = "checkstack 300 msg;" ..
673− string.rep("pushnum 10;", 255) ..
674− "pushcclosure 255; return 1"
677− assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
679− assert(A("isnull U256; return 1"))
680− assert(not A("isnil U256; return 1"))
685−-- testing get/setuservalue
687−checkerr("got number", debug.setuservalue, 3, {})
688−checkerr("got nil", debug.setuservalue, nil, {})
689−checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
691−-- testing multiple user values
692−local b = T.newuserdata(0, 10)
694− local v, p = debug.getuservalue(b, i)
695− assert(v == nil and p)
697−do -- indices out of range
698− local v, p = debug.getuservalue(b, -2)
699− assert(v == nil and not p)
700− local v, p = debug.getuservalue(b, 11)
701− assert(v == nil and not p)
703−local t = {true, false, 4.56, print, {}, b, "XYZ"}
704−for k, v in ipairs(t) do
705− debug.setuservalue(b, v, k)
707−for k, v in ipairs(t) do
708− local v1, p = debug.getuservalue(b, k)
709− assert(v1 == v and p)
712−assert(not debug.getuservalue(4))
714−debug.setuservalue(b, function () return 10 end, 10)
715−collectgarbage() -- function should not be collected
716−assert(debug.getuservalue(b, 10)() == 10)
718−debug.setuservalue(b, 134)
719−collectgarbage() -- number should not be a problem for collector
720−assert(debug.getuservalue(b) == 134)
723−-- test barrier for uservalues
725− local oldmode = collectgarbage("incremental")
727− assert(T.gccolor(b) == "black")
728− debug.setuservalue(b, {x = 100})
729− T.gcstate("pause") -- complete collection
730− assert(debug.getuservalue(b).x == 100) -- uvalue should be there
731− collectgarbage(oldmode)
734−-- long chain of userdata
736− local bb = T.newuserdata(0, 1)
737− debug.setuservalue(bb, b)
740−collectgarbage() -- nothing should not be collected
742− b = debug.getuservalue(b)
744−assert(debug.getuservalue(b).x == 100)
748−-- testing locks (refs)
750−-- reuse of references
757−for i=1,Lim do -- lock many objects
761−assert(T.ref(nil) == -1 and T.getref(-1) == nil)
762−T.unref(-1); T.unref(-1)
764−for i=1,Lim do -- unlock all them
768−local function printlocks ()
769− local f = T.makeCfunc("gettable R; return 1")
778−for i=1,Lim do -- lock many objects
782−for i=1,Lim,2 do -- unlock half of them
786−assert(type(T.getref(Arr[2])) == 'table')
789−assert(T.getref(-1) == nil)
796−assert(type(T.getref(a)) == 'table')
799−-- colect in cl the `val' of all collected userdata
805− local udval = T.udataval(x)
806− table.insert(cl, udval)
807− local d = T.newuserdata(100) -- create garbage
809− assert(debug.getmetatable(x).__gc == F)
810− assert(load("table.insert({}, {})"))() -- create more garbage
811− assert(not collectgarbage()) -- GC during GC (no op)
812− local dummy = {} -- create more garbage during GC
814− assert(type(A) == "userdata")
815− assert(T.udataval(A) == B)
816− debug.getmetatable(A) -- just access it
818− A = x -- ressurect userdata
825−-- test whether udate collection frees memory in the right time
829− local x = collectgarbage("count");
830− local a = T.newuserdata(5001)
831− assert(T.testC("objsize 2; return 1", a) == 5001)
832− assert(collectgarbage("count") >= x+4)
835− assert(collectgarbage("count") <= x+1)
836− -- udata without finalizer
837− x = collectgarbage("count")
838− collectgarbage("stop")
839− for i=1,1000 do T.newuserdata(0) end
840− assert(collectgarbage("count") > x+10)
842− assert(collectgarbage("count") <= x+1)
843− -- udata with finalizer
845− x = collectgarbage("count")
846− collectgarbage("stop")
847− a = {__gc = function () end}
848− for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
849− assert(collectgarbage("count") >= x+10)
850− collectgarbage() -- this collection only calls TM, without freeing memory
851− assert(collectgarbage("count") >= x+10)
852− collectgarbage() -- now frees memory
853− assert(collectgarbage("count") <= x+1)
854− collectgarbage("restart")
858−collectgarbage("stop")
860−-- create 3 userdatas with tag `tt'
861−a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
862−b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
863−c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
865−-- create userdata without meta table
869−checkerr("FILE%* expected, got userdata", io.input, a)
870−checkerr("FILE%* expected, got userdata", io.input, x)
872−assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
877−t = {T.getref(d), T.getref(e), T.getref(f)}
878−assert(t[1] == a and t[2] == b and t[3] == c)
881−T.unref(e); T.unref(f)
885−-- check that unref objects have been collected
886−assert(#cl == 1 and cl[1] == nc)
889−assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
891−tt.b = b -- create cycle
892−tt=nil -- frees tt for GC
896−local n5 = T.newuserdata(0)
897−debug.setmetatable(n5, {__gc=F})
901−-- check order of collection
902−assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
904−collectgarbage"restart"
909− a[i] = T.newuserdata(0)
910− debug.setmetatable(a[i], {__gc=F})
911− na[i] = T.udataval(a[i])
914−a = nil; collectgarbage()
916−for i=1,30 do assert(cl[i] == na[i]) end
920−for i=2,Lim,2 do -- unlock the other half
924−x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
925−assert(T.testC("objsize 2; return 1", x) == 41)
930−-- old `x' cannot be collected (`a' still uses it)
932−for n in pairs(a) do a[n] = undef end
934−assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
937−assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
938−assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
939−assert(T.testC("compare EQ 2 3; return 1", nil, nil))
940−assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
941−assert(not T.testC("compare EQ 2 3; return 1"))
942−assert(not T.testC("compare EQ 2 3; return 1", 3))
944−-- testing lua_equal with fallbacks
947− local t = {__eq = function (a,b) return map[a] == map[b] end}
949− local u = T.newuserdata(0)
950− debug.setmetatable(u, t)
954− assert(f(10) == f(10))
955− assert(f(10) ~= f(11))
956− assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
957− assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
959− assert(f(10) ~= f(10))
966−-- testing changing hooks during hooks
969− # set a line hook after 3 count hooks
972− pushvalue -3; append -2
973− pushvalue -2; append -2
975−local a = 1 -- counting
977−a = 1 -- count hook (set line hook)
982−assert(t[1] == "line")
984−assert(t[3] == "line" and t[4] == line + 1)
985−assert(t[5] == "line" and t[6] == line + 2)
990−-------------------------------------------------------------------------
991−do -- testing errors during GC
993− collectgarbage("stop")
996− a[i] = T.newuserdata(i) -- creates several udata
998− for i=1,20,2 do -- mark half of them to raise errors during GC
999− debug.setmetatable(a[i],
1000− {__gc = function (x) error("@expected error in gc") end})
1002− for i=2,20,2 do -- mark the other half to count and to create more garbage
1003− debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
1008− assert(A == 10) -- number of normal collections
1009− collectgarbage("restart")
1013−-------------------------------------------------------------------------
1014−-- test for userdata vals
1016− local a = {}; local lim = 30
1017− for i=0,lim do a[i] = T.pushuserdata(i) end
1018− for i=0,lim do assert(T.udataval(a[i]) == i) end
1019− for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
1020− for i=0,lim do a[a[i]] = i end
1021− for i=0,lim do a[T.pushuserdata(i)] = i end
1022− assert(type(tostring(a[1])) == "string")
1026−-------------------------------------------------------------------------
1027−-- testing multiple states
1028−T.closestate(T.newstate());
1032−assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
1035−assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
1037−a, b = T.doremote(L1, "return f()")
1038−assert(a == 'alo' and b == '3')
1040−T.doremote(L1, "_ERRORMESSAGE = nil")
1041−-- error: `sin' is not defined
1042−a, b, c = T.doremote(L1, "return sin(1)")
1043−assert(a == nil and c == 2) -- 2 == run-time error
1045−-- error: syntax error
1046−a, b, c = T.doremote(L1, "return a+")
1047−assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
1050−a, b, c = T.doremote(L1, [[
1051− string = require'string'
1052− a = require'_G'; assert(a == _G and require("_G") == a)
1053− io = require'io'; assert(type(io.read) == "function")
1054− assert(require("io") == io)
1055− a = require'table'; assert(type(a.insert) == "function")
1056− a = require'debug'; assert(type(a.getlocal) == "function")
1057− a = require'math'; assert(type(a.sin) == "function")
1058− return string.sub('okinama', 1, 2)
1067−T.doremote(L1, "a = {}")
1068−T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
1070−assert(T.doremote(L1, "return a.x") == "1")
1077−-------------------------------------------------------------------------
1078−-- testing to-be-closed variables
1079−-------------------------------------------------------------------------
1080−print"testing to-be-closed variables"
1083− local openresource = {}
1085− local function newresource ()
1086− local x = setmetatable({10}, {__close = function(y)
1087− assert(openresource[#openresource] == y)
1088− openresource[#openresource] = nil
1091− openresource[#openresource + 1] = x
1095− local a, b = T.testC([[
1096− call 0 1 # create resource
1098− toclose -2 # mark call result to be closed
1099− toclose -1 # mark nil to be closed (will be ignored)
1102− assert(a[1] == 11 and b == nil)
1103− assert(#openresource == 0) -- was closed
1105− -- repeat the test, but calling function in a 'multret' context
1106− local a = {T.testC([[
1107− call 0 1 # create resource
1108− toclose 2 # mark it to be closed
1111− assert(type(a[1]) == "string" and a[2][1] == 11)
1112− assert(#openresource == 0) -- was closed
1114− -- closing by error
1115− local a, b = pcall(T.makeCfunc[[
1116− call 0 1 # create resource
1117− toclose -1 # mark it to be closed
1118− error # resource is the error object
1120− assert(a == false and b[1] == 11)
1121− assert(#openresource == 0) -- was closed
1123− -- non-closable value
1124− local a, b = pcall(T.makeCfunc[[
1125− newtable # create non-closable object
1126− toclose -1 # mark it to be closed (should raise an error)
1127− abort # will not be executed
1129− assert(a == false and
1130− string.find(b, "non%-closable value"))
1132− local function check (n)
1133− assert(#openresource == n)
1136− -- closing resources with 'closeslot'
1138− local a = T.testC([[
1139− pushvalue 2 # stack: S, NR, CH, NR
1140− call 0 1 # create resource; stack: S, NR, CH, R
1141− toclose -1 # mark it to be closed
1142− pushvalue 2 # stack: S, NR, CH, R, NR
1143− call 0 1 # create another resource; stack: S, NR, CH, R, R
1144− toclose -1 # mark it to be closed
1145− pushvalue 3 # stack: S, NR, CH, R, R, CH
1146− pushint 2 # there should be two open resources
1147− call 1 0 # stack: S, NR, CH, R, R
1148− closeslot -1 # close second resource
1149− pushvalue 3 # stack: S, NR, CH, R, R, CH
1150− pushint 1 # there should be one open resource
1151− call 1 0 # stack: S, NR, CH, R, R
1153− setglobal "xxx" # previous op. erased the slot
1154− pop 1 # pop other resource from the stack
1156− return 1 # return stack size
1157− ]], newresource, check)
1158− assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack
1160− -- closing resources with 'pop'
1161− local a = T.testC([[
1162− pushvalue 2 # stack: S, NR, CH, NR
1163− call 0 1 # create resource; stack: S, NR, CH, R
1164− toclose -1 # mark it to be closed
1165− pushvalue 2 # stack: S, NR, CH, R, NR
1166− call 0 1 # create another resource; stack: S, NR, CH, R, R
1167− toclose -1 # mark it to be closed
1168− pushvalue 3 # stack: S, NR, CH, R, R, CH
1169− pushint 2 # there should be two open resources
1170− call 1 0 # stack: S, NR, CH, R, R
1171− pop 1 # pop second resource
1172− pushvalue 3 # stack: S, NR, CH, R, CH
1173− pushint 1 # there should be one open resource
1174− call 1 0 # stack: S, NR, CH, R
1175− pop 1 # pop other resource from the stack
1176− pushvalue 3 # stack: S, NR, CH, CH
1177− pushint 0 # there should be no open resources
1178− call 1 0 # stack: S, NR, CH
1180− return 1 # return stack size
1181− ]], newresource, check)
1182− assert(a == 3) -- no extra items left in the stack
1184− -- non-closable value
1185− local a, b = pcall(T.makeCfunc[[
1189− assert(not a and string.find(b, "(C temporary)"))
1195−** {==================================================================
1196−** Testing memory limits
1197−** ===================================================================
1200−print("memory-allocation errors")
1202−checkerr("block too big", T.newuserdata, math.maxinteger)
1204−local f = load"local a={}; for i=1,100000 do a[i]=i end"
1206−checkerr(MEMERRMSG, f)
1207−T.alloccount() -- remove limit
1210−-- test memory errors; increase limit for maximum memory by steps,
1211−-- o that we get memory errors in all allocations of a given
1212−-- task, until there is enough memory to complete the task without
1214−local function testbytes (s, f)
1216− local M = T.totalmem()
1220− collectgarbage(); collectgarbage()
1222− a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
1223− T.totalmem(0) -- remove limit
1224− if a and b == "OK" then break end -- stop when no more errors
1225− if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
1226− error(a, 0) -- propagate it
1228− M = M + 7 -- increase memory limit
1230− print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
1234−-- test memory errors; increase limit for number of allocations one
1235−-- by one, so that we get memory errors in all allocations of a given
1236−-- task, until there is enough allocations to complete the task without
1239−local function testalloc (s, f)
1244− collectgarbage(); collectgarbage()
1246− a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
1247− T.alloccount() -- remove limit
1248− if a and b == "OK" then break end -- stop when no more errors
1249− if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
1250− error(a, 0) -- propagate it
1252− M = M + 1 -- increase allocation limit
1254− print(string.format("minimum allocations for %s: %d allocations", s, M))
1259−local function testamem (s, f)
1261− return testbytes(s, f)
1266−b = testamem("doing nothing", function () return 10 end)
1269−-- testing memory errors when creating a new state
1271−testamem("state creation", function ()
1272− local st = T.newstate()
1273− if st then T.closestate(st) end -- close new state
1277−testamem("empty-table creation", function ()
1281−testamem("string creation", function ()
1282− return "XXX" .. "YYY"
1285−testamem("coroutine creation", function()
1286− return coroutine.create(print)
1290−-- testing to-be-closed variables
1291−testamem("to-be-closed variables", function()
1295− setmetatable({}, {__close = function () flag = true end})
1305−-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
1306−local mt = T.testC("rawgeti R 1; return 1")
1307−assert(type(mt) == "thread" and coroutine.running() == mt)
1311−local function expand (n,s)
1312− if n==0 then return "" end
1313− local e = string.rep("=", n)
1314− return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
1315− e, s, expand(n-1,s), e)
1318−G=0; collectgarbage(); a =collectgarbage("count")
1319−load(expand(20,"G=G+1"))()
1320−assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1323−testamem("running code on new thread", function ()
1324− return T.doonnewstack("local x=1") == 0 -- try to create thread
1328−-- testing memory x compiler
1330−testamem("loadstring", function ()
1331− return load("x=1") -- try to do load a string
1336−local function foo () return end
1339−for i = 1, #t do AA = AA .. t[i] end
1343−-- testing memory x dofile
1345−local t =os.tmpname()
1346−local f = assert(io.open(t, "w"))
1349−testamem("dofile", function ()
1350− local a = loadfile(t)
1353−assert(os.remove(t))
1354−assert(_G.AA == "aaax")
1357−-- other generic tests
1359−testamem("gsub", function ()
1360− local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
1361− return (a == 'ablo ablo')
1364−testamem("dump/undump", function ()
1365− local a = load(testprog)
1366− local b = a and string.dump(a)
1373−local t = os.tmpname()
1374−testamem("file creation", function ()
1375− local f = assert(io.open(t, 'w'))
1376− assert (not io.open"nomenaoexistente")
1378− return not loadfile'nomenaoexistente'
1380−assert(os.remove(t))
1382−testamem("table creation", function ()
1383− local a, lim = {}, 10
1384− for i=1,lim do a[i] = i; a[i..'a'] = {} end
1385− return (type(a[lim..'a']) == 'table' and a[lim] == lim)
1388−testamem("constructors", function ()
1389− local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
1390− return (type(a) == 'table' and a.e == 5)
1395−testamem("closure creation", function ()
1397− return function (x) return b + x end
1399− return (close(2)(4) == 6)
1402−testamem("using coroutines", function ()
1403− local a = coroutine.wrap(function ()
1404− coroutine.yield(string.rep("a", 10))
1407− assert(string.len(a()) == 10)
1411−do -- auxiliary buffer
1413− local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
1414− testamem("auxiliary buffer", function ()
1415− return (#table.concat(a, ",") == 20*lim + lim - 1)
1419−testamem("growing stack", function ()
1420− local function foo (n)
1421− if n == 0 then return 1 else return 1 + foo(n - 1) end
1426−-- }==================================================================
1429−do -- testing failing in 'lua_checkstack'
1430− local res = T.testC([[rawcheckstack 500000; return 1]])
1431− assert(res == false)
1432− local L = T.newstate()
1433− T.alloccount(0) -- will be unable to reallocate the stack
1434− res = T.testC(L, [[rawcheckstack 5000; return 1]])
1437− assert(res == false)
1440−do -- closing state with no extra memory
1441− local L = T.newstate()
1447−do -- garbage collection with no extra memory
1448− local L = T.newstate()
1450− local res = (T.doremote(L, [[
1452− local T = require"T"
1454− for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
1455− local stsize, stuse = T.querystr()
1456− assert(stuse > 1000)
1457− local function foo (n)
1458− if n > 0 then foo(n - 1) end
1460− foo(180) -- grow stack
1461− local _, stksize = T.stacklevel()
1462− assert(stksize > 180)
1467− -- stack and string table could not be reallocated,
1468− -- so they kept their sizes (without errors)
1469− assert(select(2, T.stacklevel()) == stksize)
1470− assert(T.querystr() == stsize)
1473− assert(res == 'ok')
1479−-- testing some auxlib functions
1480−local function gsub (a, b, c)
1481− a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
1486−assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
1487−assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
1488−assert(gsub("", "alo", "//") == "")
1489−assert(gsub("...", ".", "/.") == "/././.")
1490−assert(gsub("...", "...", "") == "")
1493−-- testing luaL_newmetatable
1494−local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1495−assert(type(mt_xuxu) == "table" and res and top == 3)
1496−local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1497−assert(mt_xuxu == d and not res and top == 3)
1498−d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
1499−assert(mt_xuxu ~= d and res and top == 3)
1501−x = T.newuserdata(0);
1502−y = T.newuserdata(0);
1503−T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
1504−assert(getmetatable(x) == mt_xuxu)
1506−-- testing luaL_testudata
1507−-- correct metatable
1508−local res1, res2, top = T.testC([[testudata -1 xuxu
1512−assert(res1 and res2 and top == 4)
1515−res1, res2, top = T.testC([[testudata -1 xuxu1
1519−assert(not res1 and not res2 and top == 4)
1521−-- non-existent type
1522−res1, res2, top = T.testC([[testudata -1 xuxu2
1526−assert(not res1 and not res2 and top == 4)
1528−-- userdata has no metatable
1529−res1, res2, top = T.testC([[testudata -1 xuxu
1533−assert(not res1 and not res2 and top == 4)
1537− local r = debug.getregistry()
1538− assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
1539− r.xuxu = nil; r.xuxu1 = nil