@@ -1,1543 +0,0 @@
1−-- $Id: testes/api.lua $
2−-- See Copyright Notice in file all.lua
3−
4−if T==nil then
5− (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
6− return
7−end
8−
9−local debug = require "debug"
10−
11−local pack = table.pack
12−
13−
14−-- standard error message for memory errors
15−local MEMERRMSG = "not enough memory"
16−
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
20−end
21−
22−
23−local function checkerr (msg, f, ...)
24− local stat, err = pcall(f, ...)
25− assert(not stat and string.find(err, msg))
26−end
27−
28−
29−print('testing C API')
30−
31−local a = T.testC("pushvalue R; return 1")
32−assert(a == debug.getregistry())
33−
34−
35−-- absindex
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)
40−
41−-- testing alignment
42−a = T.d2s(12458954321123.0)
43−assert(a == string.pack("d", 12458954321123.0))
44−assert(T.s2d(a) == 12458954321123.0)
45−
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)
48−
49−local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
50−a,b,c = f()
51−assert(a == 2 and b == 3 and not c)
52−
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)
59−
60−
61−a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
62−assert(a == 40 and b == 5 and not c)
63−
64−local t = pack(T.testC("settop 5; return *", 2, 3))
65−tcheck(t, {n=4,2,3})
66−
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)
69−
70−t = pack(T.testC("remove -2; return *", 2, 3, 4))
71−tcheck(t, {n=2,2,4})
72−
73−t = pack(T.testC("insert -1; return *", 2, 3))
74−tcheck(t, {n=2,2,3})
75−
76−t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
77−tcheck(t, {n=4,2,5,3,4})
78−
79−t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
80−tcheck(t, {n=3,5,3,4})
81−
82−t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
83−tcheck(t, {n=3,2,3,5})
84−
85−t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
86−tcheck(t, {n=3,2,4,5})
87−
88−t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
89−tcheck(t, {n=4,2,3,3,5})
90−
91−t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
92−tcheck(t, {n=4,2,3,4,3})
93−
94−do -- testing 'rotate'
95− local t = {10, 20, 30, 40, 50, 60}
96− for i = -6, 6 do
97− local s = string.format("rotate 2 %d; return 7", i)
98− local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
99− tcheck(t1, t)
100− table.insert(t, 1, table.remove(t))
101− end
102−
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})
107−
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})
115−end
116−
117−
118−-- testing warnings
119−T.testC([[
120− warningC "#This shold be a"
121− warningC " single "
122− warning "warning"
123− warningC "#This should be "
124− warning "another one"
125−]])
126−
127−
128−-- testing message handlers
129−do
130− local f = T.makeCfunc[[
131− getglobal error
132− pushstring bola
133− pcall 1 1 1 # call 'error' with given handler
134− pushstatus
135− return 2 # return error message and status
136− ]]
137−
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)
142−
143−end
144−
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})
150−
151−t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
152−tcheck(t, {n=1,"alo23joao12"})
153−
154−-- testing MULTRET
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"})
158−
159−do -- test returning more results than fit in the caller stack
160− local a = {}
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]],
163− table.unpack, a)
164− assert(b == "10")
165−end
166−
167−
168−-- testing globals
169−_G.AA = 14; _G.BB = "a31"
170−local a = {T.testC[[
171− getglobal AA;
172− getglobal BB;
173− getglobal BB;
174− setglobal AA;
175− return *
176−]]}
177−assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
178−
179−_G.AA, _G.BB = nil
180−
181−-- testing arith
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")
203−local mt = {
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] ==
214− 8 % (4 + (-3)*2))
215−
216−-- errors in arithmetic
217−checkerr("divide by zero", T.testC, "arith \\", 10, 0)
218−checkerr("%%0", T.testC, "arith %", 10, 0)
219−
220−
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"))
229−
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"))
234−
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)
248−
249−
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)
256− end
257−
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)
264−end
265−
266−-- testing length
267−local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
268−a,b,c = T.testC([[
269− len 2;
270− Llen 2;
271− objsize 2;
272− return 3
273−]], t)
274−assert(a == 20 and b == 20 and c == 0)
275−
276−t.x = "234"; t[1] = 20
277−a,b,c = T.testC([[
278− len 2;
279− Llen 2;
280− objsize 2;
281− return 3
282−]], t)
283−assert(a == "234" and b == 234 and c == 1)
284−
285−t.x = print; t[1] = 20
286−a,c = T.testC([[
287− len 2;
288− objsize 2;
289− return 2
290−]], t)
291−assert(a == print and c == 1)
292−
293−
294−-- testing __concat
295−
296−a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
297−x,y = T.testC([[
298− pushnum 5
299− pushvalue 2;
300− pushvalue 2;
301− concat 2;
302− pushvalue -2;
303− return 2;
304−]], a, a)
305−assert(x == a..a and y == 5)
306−
307−-- concat with 0 elements
308−assert(T.testC("concat 0; return 1") == "")
309−
310−-- concat with 1 element
311−assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
312−
313−
314−
315−-- testing lua_is
316−
317−local function B (x) return x and 1 or 0 end
318−
319−local function count (x, n)
320− n = n or 2
321− local prog = [[
322− isnumber %d;
323− isstring %d;
324− isfunction %d;
325− iscfunction %d;
326− istable %d;
327− isuserdata %d;
328− isnil %d;
329− isnull %d;
330− return 8
331− ]]
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))
335−end
336−
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)
346−
347−
348−-- testing lua_to...
349−
350−local function to (s, x, n)
351− n = n or 2
352− return T.testC(string.format("%s %d; return 1", s, n), x)
353−end
354−
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
379−do -- long strings
380− local s1 = string.rep("x", 300)
381− local s2 = string.rep("x", 300)
382− assert(to("topointer", s1) ~= to("topointer", s2))
383−end
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)
395−
396−
397−print("testing panic function")
398−do
399− -- trivial error
400− assert(T.checkpanic("pushstring hi; error") == "hi")
401−
402− -- using the stack inside panic
403− assert(T.checkpanic("pushstring hi; error;",
404− [[checkstack 5 XX
405− pushstring ' alo'
406− pushstring ' mundo'
407− concat 3]]) == "hi alo mundo")
408−
409− -- "argerror" without frames
410− assert(T.checkpanic("loadstring 4") ==
411− "bad argument #4 (string expected, got no value)")
412−
413−
414− -- memory error
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
418−
419− -- stack error
420− if not _soft then
421− local msg = T.checkpanic[[
422− pushstring "function f() f() end"
423− loadstring -1; call 0 0
424− getglobal f; call 0 0
425− ]]
426− assert(string.find(msg, "stack overflow"))
427− end
428−
429− -- exit in panic still close to-be-closed variables
430− assert(T.checkpanic([[
431− pushstring "return {__close = function () Y = 'ho'; end}"
432− newtable
433− loadstring -2
434− call 0 1
435− setmetatable -2
436− toclose -1
437− pushstring "hi"
438− error
439− ]],
440− [[
441− getglobal Y
442− concat 2 # concat original error with global Y
443− ]]) == "hiho")
444−
445−
446−end
447−
448−-- testing deep C stack
449−if not _soft then
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")
458− print'+'
459−end
460−
461−local lim = _soft and 500 or 12000
462−local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
463−for i = 1,lim do
464− prog[#prog + 1] = "pushnum " .. i
465− prog[#prog + 1] = "pushnum " .. i * 10
466−end
467−
468−prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
469−prog[#prog + 1] = "insert " .. -(2*lim + 2)
470−
471−for i = 1,lim do
472− prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
473−end
474−
475−prog[#prog + 1] = "return 2"
476−
477−prog = table.concat(prog, ";")
478−local g, t = T.testC(prog)
479−assert(g == _G)
480−for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
481−assert(next(t) == nil)
482−prog, g, t = nil
483−
484−-- testing errors
485−
486−a = T.testC([[
487− loadstring 2; pcall 0 1 0;
488− pushvalue 3; insert -2; pcall 1 1 0;
489− pcall 0 0 0;
490− return 1
491−]], "XX=150", function (a) assert(a==nil); return 3 end)
492−
493−assert(type(a) == 'string' and XX == 150)
494−_G.XX = nil
495−
496−local function check3(p, ...)
497− local arg = {...}
498− assert(#arg == 3)
499− assert(string.find(arg[3], p))
500−end
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"))
504−
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))
510−end
511−
512−if not _soft then
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")
518− F = nil
519− collectgarbage("restart")
520−end
521−print"+"
522−
523−
524−-- testing table access
525−
526−do -- getp/setp
527− local a = {}
528− local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
529− assert(a == a1)
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)
533−end
534−
535−
536−do -- using the table itself as index
537− local a = {}
538− a[a] = 10
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)
542−
543− local prog = "settable -2; return *"
544− local res = {T.testC(prog, a, 20)}
545− assert(a[a] == 20)
546− assert(#res == 1 and res[1] == prog)
547−
548− -- raw
549− a[a] = 10
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)
553−
554− local prog = "rawset -2; return *"
555− local res = {T.testC(prog, a, 20)}
556− assert(a[a] == 20)
557− assert(#res == 1 and res[1] == prog)
558−
559− -- using the table as the value to set
560− local prog = "rawset -1; return *"
561− local res = {T.testC(prog, 30, a)}
562− assert(a[30] == a)
563− assert(#res == 1 and res[1] == prog)
564−
565− local prog = "settable -1; return *"
566− local res = {T.testC(prog, 40, a)}
567− assert(a[40] == a)
568− assert(#res == 1 and res[1] == prog)
569−
570− local prog = "rawseti -1 100; return *"
571− local res = {T.testC(prog, a)}
572− assert(a[100] == a)
573− assert(#res == 1 and res[1] == prog)
574−
575− local prog = "seti -1 200; return *"
576− local res = {T.testC(prog, a)}
577− assert(a[200] == a)
578− assert(#res == 1 and res[1] == prog)
579−end
580−
581−a = {x=0, y=12}
582−x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
583− a, 3, "y", 4, "x")
584−assert(x == 0 and y == 12)
585−T.testC("settable -5", a, 3, 4, "x", 15)
586−assert(a.x == 15)
587−a[a] = print
588−x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
589−assert(x == print)
590−T.testC("settable 2", a, "x") -- table and key are the same object!
591−assert(a[a] == "x")
592−
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)
598−assert(k == 12)
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)
602−assert(y == 12)
603−k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
604−assert(a.x == 16 and k == 4)
605−a[b] = 'xuxu'
606−y = T.testC("gettable 2, return 1", b)
607−assert(y == 'xuxu')
608−T.testC("settable 2", b, 19)
609−assert(a[b] == 19)
610−
611−--
612−do -- testing getfield/setfield with long keys
613− local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
614− local a = T.testC([[
615− getfield 2 _012345678901234567890123456789012345678901234567890123456789
616− return 1
617− ]], t)
618− assert(a == 32)
619− local a = T.testC([[
620− pushnum 33
621− setglobal _012345678901234567890123456789012345678901234567890123456789
622− ]])
623− assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
624− _012345678901234567890123456789012345678901234567890123456789 = nil
625−end
626−
627−-- testing next
628−a = {}
629−t = pack(T.testC("next; return *", a, nil))
630−tcheck(t, {n=1,a})
631−a = {a=3}
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))
635−tcheck(t, {n=1,a})
636−
637−
638−
639−-- testing upvalues
640−
641−do
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)
653− T.checkmemory()
654−end
655−
656−
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)
661−
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")
668−
669−
670−-- large closures
671−do
672− local A = "checkstack 300 msg;" ..
673− string.rep("pushnum 10;", 255) ..
674− "pushcclosure 255; return 1"
675− A = T.testC(A)
676− for i=1,255 do
677− assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
678− end
679− assert(A("isnull U256; return 1"))
680− assert(not A("isnil U256; return 1"))
681−end
682−
683−
684−
685−-- testing get/setuservalue
686−-- bug in 5.1.2
687−checkerr("got number", debug.setuservalue, 3, {})
688−checkerr("got nil", debug.setuservalue, nil, {})
689−checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
690−
691−-- testing multiple user values
692−local b = T.newuserdata(0, 10)
693−for i = 1, 10 do
694− local v, p = debug.getuservalue(b, i)
695− assert(v == nil and p)
696−end
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)
702−end
703−local t = {true, false, 4.56, print, {}, b, "XYZ"}
704−for k, v in ipairs(t) do
705− debug.setuservalue(b, v, k)
706−end
707−for k, v in ipairs(t) do
708− local v1, p = debug.getuservalue(b, k)
709− assert(v1 == v and p)
710−end
711−
712−assert(not debug.getuservalue(4))
713−
714−debug.setuservalue(b, function () return 10 end, 10)
715−collectgarbage() -- function should not be collected
716−assert(debug.getuservalue(b, 10)() == 10)
717−
718−debug.setuservalue(b, 134)
719−collectgarbage() -- number should not be a problem for collector
720−assert(debug.getuservalue(b) == 134)
721−
722−
723−-- test barrier for uservalues
724−do
725− local oldmode = collectgarbage("incremental")
726− T.gcstate("atomic")
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)
732−end
733−
734−-- long chain of userdata
735−for i = 1, 1000 do
736− local bb = T.newuserdata(0, 1)
737− debug.setuservalue(bb, b)
738− b = bb
739−end
740−collectgarbage() -- nothing should not be collected
741−for i = 1, 1000 do
742− b = debug.getuservalue(b)
743−end
744−assert(debug.getuservalue(b).x == 100)
745−b = nil
746−
747−
748−-- testing locks (refs)
749−
750−-- reuse of references
751−local i = T.ref{}
752−T.unref(i)
753−assert(T.ref{} == i)
754−
755−local Arr = {}
756−local Lim = 100
757−for i=1,Lim do -- lock many objects
758− Arr[i] = T.ref({})
759−end
760−
761−assert(T.ref(nil) == -1 and T.getref(-1) == nil)
762−T.unref(-1); T.unref(-1)
763−
764−for i=1,Lim do -- unlock all them
765− T.unref(Arr[i])
766−end
767−
768−local function printlocks ()
769− local f = T.makeCfunc("gettable R; return 1")
770− local n = f("n")
771− print("n", n)
772− for i=0,n do
773− print(i, f(i))
774− end
775−end
776−
777−
778−for i=1,Lim do -- lock many objects
779− Arr[i] = T.ref({})
780−end
781−
782−for i=1,Lim,2 do -- unlock half of them
783− T.unref(Arr[i])
784−end
785−
786−assert(type(T.getref(Arr[2])) == 'table')
787−
788−
789−assert(T.getref(-1) == nil)
790−
791−
792−a = T.ref({})
793−
794−collectgarbage()
795−
796−assert(type(T.getref(a)) == 'table')
797−
798−
799−-- colect in cl the `val' of all collected userdata
800−local tt = {}
801−local cl = {n=0}
802−A = nil; B = nil
803−local F
804−F = function (x)
805− local udval = T.udataval(x)
806− table.insert(cl, udval)
807− local d = T.newuserdata(100) -- create garbage
808− d = nil
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
813− if A ~= nil then
814− assert(type(A) == "userdata")
815− assert(T.udataval(A) == B)
816− debug.getmetatable(A) -- just access it
817− end
818− A = x -- ressurect userdata
819− B = udval
820− return 1,2,3
821−end
822−tt.__gc = F
823−
824−
825−-- test whether udate collection frees memory in the right time
826−do
827− collectgarbage();
828− collectgarbage();
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)
833− a = nil
834− collectgarbage();
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)
841− collectgarbage()
842− assert(collectgarbage("count") <= x+1)
843− -- udata with finalizer
844− collectgarbage()
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")
855−end
856−
857−
858−collectgarbage("stop")
859−
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)
864−
865−-- create userdata without meta table
866−x = T.newuserdata(4)
867−y = T.newuserdata(0)
868−
869−checkerr("FILE%* expected, got userdata", io.input, a)
870−checkerr("FILE%* expected, got userdata", io.input, x)
871−
872−assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
873−
874−local d = T.ref(a);
875−local e = T.ref(b);
876−local f = T.ref(c);
877−t = {T.getref(d), T.getref(e), T.getref(f)}
878−assert(t[1] == a and t[2] == b and t[3] == c)
879−
880−t=nil; a=nil; c=nil;
881−T.unref(e); T.unref(f)
882−
883−collectgarbage()
884−
885−-- check that unref objects have been collected
886−assert(#cl == 1 and cl[1] == nc)
887−
888−x = T.getref(d)
889−assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
890−x =nil
891−tt.b = b -- create cycle
892−tt=nil -- frees tt for GC
893−A = nil
894−b = nil
895−T.unref(d);
896−local n5 = T.newuserdata(0)
897−debug.setmetatable(n5, {__gc=F})
898−n5 = T.udataval(n5)
899−collectgarbage()
900−assert(#cl == 4)
901−-- check order of collection
902−assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
903−
904−collectgarbage"restart"
905−
906−
907−a, na = {}, {}
908−for i=30,1,-1 do
909− a[i] = T.newuserdata(0)
910− debug.setmetatable(a[i], {__gc=F})
911− na[i] = T.udataval(a[i])
912−end
913−cl = {}
914−a = nil; collectgarbage()
915−assert(#cl == 30)
916−for i=1,30 do assert(cl[i] == na[i]) end
917−na = nil
918−
919−
920−for i=2,Lim,2 do -- unlock the other half
921− T.unref(Arr[i])
922−end
923−
924−x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
925−assert(T.testC("objsize 2; return 1", x) == 41)
926−cl = {}
927−a = {[x] = 1}
928−x = T.udataval(x)
929−collectgarbage()
930−-- old `x' cannot be collected (`a' still uses it)
931−assert(#cl == 0)
932−for n in pairs(a) do a[n] = undef end
933−collectgarbage()
934−assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
935−
936−-- testing lua_equal
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))
943−
944−-- testing lua_equal with fallbacks
945−do
946− local map = {}
947− local t = {__eq = function (a,b) return map[a] == map[b] end}
948− local function f(x)
949− local u = T.newuserdata(0)
950− debug.setmetatable(u, t)
951− map[u] = x
952− return u
953− end
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)))
958− t.__eq = nil
959− assert(f(10) ~= f(10))
960−end
961−
962−print'+'
963−
964−
965−
966−-- testing changing hooks during hooks
967−_G.TT = {}
968−T.sethook([[
969− # set a line hook after 3 count hooks
970− sethook 4 0 '
971− getglobal TT;
972− pushvalue -3; append -2
973− pushvalue -2; append -2
974− ']], "c", 3)
975−local a = 1 -- counting
976−a = 1 -- counting
977−a = 1 -- count hook (set line hook)
978−a = 1 -- line hook
979−a = 1 -- line hook
980−debug.sethook()
981−local t = _G.TT
982−assert(t[1] == "line")
983−local line = t[2]
984−assert(t[3] == "line" and t[4] == line + 1)
985−assert(t[5] == "line" and t[6] == line + 2)
986−assert(t[7] == nil)
987−_G.TT = nil
988−
989−
990−-------------------------------------------------------------------------
991−do -- testing errors during GC
992− warn("@off")
993− collectgarbage("stop")
994− local a = {}
995− for i=1,20 do
996− a[i] = T.newuserdata(i) -- creates several udata
997− end
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})
1001− 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})
1004− end
1005− a = nil
1006− _G.A = 0
1007− collectgarbage()
1008− assert(A == 10) -- number of normal collections
1009− collectgarbage("restart")
1010− warn("@on")
1011−end
1012−_G.A = nil
1013−-------------------------------------------------------------------------
1014−-- test for userdata vals
1015−do
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")
1023−end
1024−
1025−
1026−-------------------------------------------------------------------------
1027−-- testing multiple states
1028−T.closestate(T.newstate());
1029−L1 = T.newstate()
1030−assert(L1)
1031−
1032−assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
1033−
1034−
1035−assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
1036−
1037−a, b = T.doremote(L1, "return f()")
1038−assert(a == 'alo' and b == '3')
1039−
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
1044−
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
1048−
1049−T.loadlib(L1)
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)
1059−]])
1060−assert(a == "ok")
1061−
1062−T.closestate(L1);
1063−
1064−
1065−L1 = T.newstate()
1066−T.loadlib(L1)
1067−T.doremote(L1, "a = {}")
1068−T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
1069− settable -3]])
1070−assert(T.doremote(L1, "return a.x") == "1")
1071−
1072−T.closestate(L1)
1073−
1074−L1 = nil
1075−
1076−print('+')
1077−-------------------------------------------------------------------------
1078−-- testing to-be-closed variables
1079−-------------------------------------------------------------------------
1080−print"testing to-be-closed variables"
1081−
1082−do
1083− local openresource = {}
1084−
1085− local function newresource ()
1086− local x = setmetatable({10}, {__close = function(y)
1087− assert(openresource[#openresource] == y)
1088− openresource[#openresource] = nil
1089− y[1] = y[1] + 1
1090− end})
1091− openresource[#openresource + 1] = x
1092− return x
1093− end
1094−
1095− local a, b = T.testC([[
1096− call 0 1 # create resource
1097− pushnil
1098− toclose -2 # mark call result to be closed
1099− toclose -1 # mark nil to be closed (will be ignored)
1100− return 2
1101− ]], newresource)
1102− assert(a[1] == 11 and b == nil)
1103− assert(#openresource == 0) -- was closed
1104−
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
1109− return 2
1110− ]], newresource)}
1111− assert(type(a[1]) == "string" and a[2][1] == 11)
1112− assert(#openresource == 0) -- was closed
1113−
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
1119− ]], newresource)
1120− assert(a == false and b[1] == 11)
1121− assert(#openresource == 0) -- was closed
1122−
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
1128− ]])
1129− assert(a == false and
1130− string.find(b, "non%-closable value"))
1131−
1132− local function check (n)
1133− assert(#openresource == n)
1134− end
1135−
1136− -- closing resources with 'closeslot'
1137− _ENV.xxx = true
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
1152− closeslot 4
1153− setglobal "xxx" # previous op. erased the slot
1154− pop 1 # pop other resource from the stack
1155− pushint *
1156− return 1 # return stack size
1157− ]], newresource, check)
1158− assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack
1159−
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
1179− pushint *
1180− return 1 # return stack size
1181− ]], newresource, check)
1182− assert(a == 3) -- no extra items left in the stack
1183−
1184− -- non-closable value
1185− local a, b = pcall(T.makeCfunc[[
1186− pushint 32
1187− toclose -1
1188− ]])
1189− assert(not a and string.find(b, "(C temporary)"))
1190−
1191−end
1192−
1193−
1194−--[[
1195−** {==================================================================
1196−** Testing memory limits
1197−** ===================================================================
1198−--]]
1199−
1200−print("memory-allocation errors")
1201−
1202−checkerr("block too big", T.newuserdata, math.maxinteger)
1203−collectgarbage()
1204−local f = load"local a={}; for i=1,100000 do a[i]=i end"
1205−T.alloccount(10)
1206−checkerr(MEMERRMSG, f)
1207−T.alloccount() -- remove limit
1208−
1209−
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
1213−-- errors.
1214−local function testbytes (s, f)
1215− collectgarbage()
1216− local M = T.totalmem()
1217− local oldM = M
1218− local a,b = nil
1219− while true do
1220− collectgarbage(); collectgarbage()
1221− T.totalmem(M)
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
1227− end
1228− M = M + 7 -- increase memory limit
1229− end
1230− print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
1231− return a
1232−end
1233−
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
1237−-- errors.
1238−
1239−local function testalloc (s, f)
1240− collectgarbage()
1241− local M = 0
1242− local a,b = nil
1243− while true do
1244− collectgarbage(); collectgarbage()
1245− T.alloccount(M)
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
1251− end
1252− M = M + 1 -- increase allocation limit
1253− end
1254− print(string.format("minimum allocations for %s: %d allocations", s, M))
1255− return a
1256−end
1257−
1258−
1259−local function testamem (s, f)
1260− testalloc(s, f)
1261− return testbytes(s, f)
1262−end
1263−
1264−
1265−-- doing nothing
1266−b = testamem("doing nothing", function () return 10 end)
1267−assert(b == 10)
1268−
1269−-- testing memory errors when creating a new state
1270−
1271−testamem("state creation", function ()
1272− local st = T.newstate()
1273− if st then T.closestate(st) end -- close new state
1274− return st
1275−end)
1276−
1277−testamem("empty-table creation", function ()
1278− return {}
1279−end)
1280−
1281−testamem("string creation", function ()
1282− return "XXX" .. "YYY"
1283−end)
1284−
1285−testamem("coroutine creation", function()
1286− return coroutine.create(print)
1287−end)
1288−
1289−
1290−-- testing to-be-closed variables
1291−testamem("to-be-closed variables", function()
1292− local flag
1293− do
1294− local x <close> =
1295− setmetatable({}, {__close = function () flag = true end})
1296− flag = false
1297− local x = {}
1298− end
1299− return flag
1300−end)
1301−
1302−
1303−-- testing threads
1304−
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)
1308−
1309−
1310−
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)
1316−end
1317−
1318−G=0; collectgarbage(); a =collectgarbage("count")
1319−load(expand(20,"G=G+1"))()
1320−assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1321−G = nil
1322−
1323−testamem("running code on new thread", function ()
1324− return T.doonnewstack("local x=1") == 0 -- try to create thread
1325−end)
1326−
1327−
1328−-- testing memory x compiler
1329−
1330−testamem("loadstring", function ()
1331− return load("x=1") -- try to do load a string
1332−end)
1333−
1334−
1335−local testprog = [[
1336−local function foo () return end
1337−local t = {"x"}
1338−AA = "aaa"
1339−for i = 1, #t do AA = AA .. t[i] end
1340−return true
1341−]]
1342−
1343−-- testing memory x dofile
1344−_G.AA = nil
1345−local t =os.tmpname()
1346−local f = assert(io.open(t, "w"))
1347−f:write(testprog)
1348−f:close()
1349−testamem("dofile", function ()
1350− local a = loadfile(t)
1351− return a and a()
1352−end)
1353−assert(os.remove(t))
1354−assert(_G.AA == "aaax")
1355−
1356−
1357−-- other generic tests
1358−
1359−testamem("gsub", function ()
1360− local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
1361− return (a == 'ablo ablo')
1362−end)
1363−
1364−testamem("dump/undump", function ()
1365− local a = load(testprog)
1366− local b = a and string.dump(a)
1367− a = b and load(b)
1368− return a and a()
1369−end)
1370−
1371−_G.AA = nil
1372−
1373−local t = os.tmpname()
1374−testamem("file creation", function ()
1375− local f = assert(io.open(t, 'w'))
1376− assert (not io.open"nomenaoexistente")
1377− io.close(f);
1378− return not loadfile'nomenaoexistente'
1379−end)
1380−assert(os.remove(t))
1381−
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)
1386−end)
1387−
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)
1391−end)
1392−
1393−local a = 1
1394−local close = nil
1395−testamem("closure creation", function ()
1396− function close (b)
1397− return function (x) return b + x end
1398− end
1399− return (close(2)(4) == 6)
1400−end)
1401−
1402−testamem("using coroutines", function ()
1403− local a = coroutine.wrap(function ()
1404− coroutine.yield(string.rep("a", 10))
1405− return {}
1406− end)
1407− assert(string.len(a()) == 10)
1408− return a()
1409−end)
1410−
1411−do -- auxiliary buffer
1412− local lim = 100
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)
1416− end)
1417−end
1418−
1419−testamem("growing stack", function ()
1420− local function foo (n)
1421− if n == 0 then return 1 else return 1 + foo(n - 1) end
1422− end
1423− return foo(100)
1424−end)
1425−
1426−-- }==================================================================
1427−
1428−
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]])
1435− T.alloccount()
1436− T.closestate(L)
1437− assert(res == false)
1438−end
1439−
1440−do -- closing state with no extra memory
1441− local L = T.newstate()
1442− T.alloccount(0)
1443− T.closestate(L)
1444− T.alloccount()
1445−end
1446−
1447−do -- garbage collection with no extra memory
1448− local L = T.newstate()
1449− T.loadlib(L)
1450− local res = (T.doremote(L, [[
1451− _ENV = require"_G"
1452− local T = require"T"
1453− local a = {}
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
1459− end
1460− foo(180) -- grow stack
1461− local _, stksize = T.stacklevel()
1462− assert(stksize > 180)
1463− a = nil
1464− T.alloccount(0)
1465− collectgarbage()
1466− T.alloccount()
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)
1471− return 'ok'
1472− ]]))
1473− assert(res == 'ok')
1474− T.closestate(L)
1475−end
1476−
1477−print'+'
1478−
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)
1482− assert(b == 5)
1483− return a
1484−end
1485−
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("...", "...", "") == "")
1491−
1492−
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)
1500−
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)
1505−
1506−-- testing luaL_testudata
1507−-- correct metatable
1508−local res1, res2, top = T.testC([[testudata -1 xuxu
1509− testudata 2 xuxu
1510− gettop
1511− return 3]], x)
1512−assert(res1 and res2 and top == 4)
1513−
1514−-- wrong metatable
1515−res1, res2, top = T.testC([[testudata -1 xuxu1
1516− testudata 2 xuxu1
1517− gettop
1518− return 3]], x)
1519−assert(not res1 and not res2 and top == 4)
1520−
1521−-- non-existent type
1522−res1, res2, top = T.testC([[testudata -1 xuxu2
1523− testudata 2 xuxu2
1524− gettop
1525− return 3]], x)
1526−assert(not res1 and not res2 and top == 4)
1527−
1528−-- userdata has no metatable
1529−res1, res2, top = T.testC([[testudata -1 xuxu
1530− testudata 2 xuxu
1531− gettop
1532− return 3]], y)
1533−assert(not res1 and not res2 and top == 4)
1534−
1535−-- erase metatables
1536−do
1537− local r = debug.getregistry()
1538− assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
1539− r.xuxu = nil; r.xuxu1 = nil
1540−end
1541−
1542−print'OK'
1543−