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

603 lines · 14874 bytes Raw Blame History
1-- $Id: testes/calls.lua $
2-- See Copyright Notice in file lua.h
3
4global <const> *
5
6print("testing functions and calls")
7
8local debug = require "debug"
9
10-- get the opportunity to test 'type' too ;)
11
12assert(type(1<2) == 'boolean')
13assert(type(true) == 'boolean' and type(false) == 'boolean')
14assert(type(nil) == 'nil'
15 and type(-3) == 'number'
16 and type'x' == 'string'
17 and type{} == 'table'
18 and type(type) == 'function')
19
20assert(type(assert) == type(print))
21local function f (x) return a:x (x) end
22assert(type(f) == 'function')
23assert(not pcall(type))
24
25
26-- testing local-function recursion
27global fact = false
28do
29 local res = 1
30 local function fact (n)
31 if n==0 then return res
32 else return n*fact(n-1)
33 end
34 end
35 assert(fact(5) == 120)
36end
37assert(fact == false)
38fact = nil
39
40-- testing declarations
41local a = {i = 10}
42local self = 20
43function a:x (x) return x+self.i end
44function a.y (x) return x+self end
45
46assert(a:x(1)+10 == a.y(1))
47
48a.t = {i=-100}
49a["t"].x = function (self, a,b) return self.i+a+b end
50
51assert(a.t:x(2,3) == -95)
52
53do
54 local a = {x=0}
55 function a:add (x) self.x, a.y = self.x+x, 20; return self end
56 assert(a:add(10):add(20):add(30).x == 60 and a.y == 20)
57end
58
59local a = {b={c={}}}
60
61function a.b.c.f1 (x) return x+1 end
62function a.b.c:f2 (x,y) self[x] = y end
63assert(a.b.c.f1(4) == 5)
64a.b.c:f2('k', 12); assert(a.b.c.k == 12)
65
66print('+')
67
68global t = nil -- 'declare' t
69function f(a,b,c) local d = 'a'; t={a,b,c,d} end
70
71f( -- this line change must be valid
72 1,2)
73assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
74f(1,2, -- this one too
75 3,4)
76assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
77
78t = nil -- delete 't'
79
80global function fat(x)
81 if x <= 1 then return 1
82 else return x*load("return fat(" .. x-1 .. ")", "")()
83 end
84end
85
86assert(load "load 'assert(fat(6)==720)' () ")()
87a = load('return fat(5), 3')
88local a,b = a()
89assert(a == 120 and b == 3)
90fat = nil
91print('+')
92
93local function err_on_n (n)
94 if n==0 then error(); exit(1);
95 else err_on_n (n-1); exit(1);
96 end
97end
98
99do
100 local function dummy (n)
101 if n > 0 then
102 assert(not pcall(err_on_n, n))
103 dummy(n-1)
104 end
105 end
106
107 dummy(10)
108end
109
110_G.deep = nil -- "declaration" (used by 'all.lua')
111
112global function deep (n)
113 if n>0 then deep(n-1) end
114end
115deep(10)
116deep(180)
117
118
119print"testing tail calls"
120
121function deep (n) if n>0 then return deep(n-1) else return 101 end end
122assert(deep(30000) == 101)
123a = {}
124function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
125assert(a:deep(30000) == 101)
126
127do -- tail calls x varargs
128 local function foo (x, ...) local a = {...}; return x, a[1], a[2] end
129
130 local function foo1 (x) return foo(10, x, x + 1) end
131
132 local a, b, c = foo1(-2)
133 assert(a == 10 and b == -2 and c == -1)
134
135 -- tail calls x metamethods
136 local t = setmetatable({}, {__call = foo})
137 local function foo2 (x) return t(10, x) end
138 a, b, c = foo2(100)
139 assert(a == t and b == 10 and c == 100)
140
141 a, b = (function () return foo() end)()
142 assert(a == nil and b == nil)
143
144 local X, Y, A
145 local function foo (x, y, ...) X = x; Y = y; A = {...} end
146 local function foo1 (...) return foo(...) end
147
148 local a, b, c = foo1()
149 assert(X == nil and Y == nil and #A == 0)
150
151 a, b, c = foo1(10)
152 assert(X == 10 and Y == nil and #A == 0)
153
154 a, b, c = foo1(10, 20)
155 assert(X == 10 and Y == 20 and #A == 0)
156
157 a, b, c = foo1(10, 20, 30)
158 assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
159end
160
161
162do -- C-stack overflow while handling C-stack overflow
163 local function loop ()
164 assert(pcall(loop))
165 end
166
167 local err, msg = xpcall(loop, loop)
168 assert(not err and string.find(msg, "error"))
169end
170
171
172
173do -- tail calls x chain of __call
174 local n = 10000 -- depth
175
176 local function foo ()
177 if n == 0 then return 1023
178 else n = n - 1; return foo()
179 end
180 end
181
182 -- build a chain of __call metamethods ending in function 'foo'
183 for i = 1, 15 do
184 foo = setmetatable({}, {__call = foo})
185 end
186
187 -- call the first one as a tail call in a new coroutine
188 -- (to ensure stack is not preallocated)
189 assert(coroutine.wrap(function() return foo() end)() == 1023)
190end
191
192print('+')
193
194
195do print"testing chains of '__call'"
196 local N = 15
197 local u = table.pack
198 for i = 1, N do
199 u = setmetatable({i}, {__call = u})
200 end
201
202 local Res = u("a", "b", "c")
203
204 assert(Res.n == N + 3)
205 for i = 1, N do
206 assert(Res[i][1] == i)
207 end
208 assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
209
210 local function u (...)
211 local n = debug.getinfo(1, 't').extraargs
212 assert(select("#", ...) == n)
213 return n
214 end
215
216 for i = 0, N do
217 assert(u() == i)
218 u = setmetatable({}, {__call = u})
219 end
220end
221
222
223do -- testing chains too long
224 local a = {}
225 for i = 1, 16 do -- one too many
226 a = setmetatable({}, {__call = a})
227 end
228 local status, msg = pcall(a)
229 assert(not status and string.find(msg, "too long"))
230
231 setmetatable(a, {__call = a}) -- infinite chain
232 local status, msg = pcall(a)
233 assert(not status and string.find(msg, "too long"))
234
235 -- again, with a tail call
236 local status, msg = pcall(function () return a() end)
237 assert(not status and string.find(msg, "too long"))
238end
239
240a = nil
241(function (x) a=x end)(23)
242assert(a == 23 and (function (x) return x*2 end)(20) == 40)
243
244
245-- testing closures
246
247-- fixed-point operator
248local Z = function (le)
249 local function a (f)
250 return le(function (x) return f(f)(x) end)
251 end
252 return a(a)
253 end
254
255
256-- non-recursive factorial
257
258local F = function (f)
259 return function (n)
260 if n == 0 then return 1
261 else return n*f(n-1) end
262 end
263 end
264
265local fat = Z(F)
266
267assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
268
269local function g (z)
270 local function f (a,b,c,d)
271 return function (x,y) return a+b+c+d+a+x+y+z end
272 end
273 return f(z,z+1,z+2,z+3)
274end
275
276local f = g(10)
277assert(f(9, 16) == 10+11+12+13+10+9+16+10)
278
279print('+')
280
281-- testing multiple returns
282
283local function unlpack (t, i)
284 i = i or 1
285 if (i <= #t) then
286 return t[i], unlpack(t, i+1)
287 end
288end
289
290local function equaltab (t1, t2)
291 assert(#t1 == #t2)
292 for i = 1, #t1 do
293 assert(t1[i] == t2[i])
294 end
295end
296
297local pack = function (...) return (table.pack(...)) end
298
299local function f() return 1,2,30,4 end
300local function ret2 (a,b) return a,b end
301
302local a,b,c,d = unlpack{1,2,3}
303assert(a==1 and b==2 and c==3 and d==nil)
304a = {1,2,3,4,false,10,'alo',false,assert}
305equaltab(pack(unlpack(a)), a)
306equaltab(pack(unlpack(a), -1), {1,-1})
307a,b,c,d = ret2(f()), ret2(f())
308assert(a==1 and b==1 and c==2 and d==nil)
309a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
310assert(a==1 and b==1 and c==2 and d==nil)
311a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
312assert(a==1 and b==1 and c==nil and d==nil)
313
314a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
315assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
316
317
318-- testing calls with 'incorrect' arguments
319rawget({}, "x", 1)
320rawset({}, "x", 1, 2)
321assert(math.sin(1,2) == math.sin(1))
322table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
323
324
325-- test for generic load
326local x = "-- a comment\0\0\0\n x = 10 + \n23; \
327 local a = function () x = 'hi' end; \
328 return '\0'"
329local function read1 (x)
330 local i = 0
331 return function ()
332 collectgarbage()
333 i=i+1
334 return string.sub(x, i, i)
335 end
336end
337
338local function cannotload (msg, a,b)
339 assert(not a and string.find(b, msg))
340end
341
342a = assert(load(read1(x), "modname", "t", _G))
343assert(a() == "\0" and _G.x == 33)
344assert(debug.getinfo(a).source == "modname")
345-- cannot read text in binary mode
346cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
347cannotload("attempt to load a text chunk", load(x, "modname", "b"))
348
349a = assert(load(function () return nil end))
350a() -- empty chunk
351
352assert(not load(function () return true end))
353
354
355-- small bug
356local t = {nil, "return ", "3"}
357local f, msg = load(function () return table.remove(t, 1) end)
358assert(f() == nil) -- should read the empty chunk
359
360-- another small bug (in 5.2.1)
361f = load(string.dump(function () return 1 end), nil, "b", {})
362assert(type(f) == "function" and f() == 1)
363
364
365do -- another bug (in 5.4.0)
366 -- loading a binary long string interrupted by GC cycles
367 local f = string.dump(function ()
368 return '01234567890123456789012345678901234567890123456789'
369 end)
370 f = load(read1(f))
371 assert(f() == '01234567890123456789012345678901234567890123456789')
372end
373
374
375if T then
376 -- check stack level when calling reader function
377 local function get (str)
378 local pos = 0
379 local level = nil
380 return function ()
381 pos = pos + 1
382 local c = string.sub(str, pos, pos)
383 local newlevel = T.stacklevel()
384 if not level then
385 level = newlevel
386 else
387 assert(level == newlevel)
388 end
389 return #c > 0 and c or nil
390 end
391 end
392
393 local str = "local function foo () end; return 121"
394 assert(assert(load(get(str)))() == 121)
395
396 str = string.dump(load(str))
397 assert(assert(load(get(str)))() == 121)
398end
399
400
401x = string.dump(load("x = 1; return x"))
402a = assert(load(read1(x), nil, "b"))
403assert(a() == 1 and _G.x == 1)
404cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
405cannotload("attempt to load a binary chunk", load(x, nil, "t"))
406_G.x = nil
407
408assert(not pcall(string.dump, print)) -- no dump of C functions
409
410cannotload("unexpected symbol", load(read1("*a = 123")))
411cannotload("unexpected symbol", load("*a = 123"))
412cannotload("hhi", load(function () error("hhi") end))
413
414-- any value is valid for _ENV
415assert(load("return _ENV", nil, nil, 123)() == 123)
416
417
418-- load when _ENV is not first upvalue
419global XX; local x
420XX = 123
421local function h ()
422 local y=x -- use 'x', so that it becomes 1st upvalue
423 return XX -- global name
424end
425local d = string.dump(h)
426x = load(d, "", "b")
427assert(debug.getupvalue(x, 2) == '_ENV')
428debug.setupvalue(x, 2, _G)
429assert(x() == 123)
430
431assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
432XX = nil
433
434-- test generic load with nested functions
435x = [[
436 return function (x)
437 return function (y)
438 return function (z)
439 return x+y+z
440 end
441 end
442 end
443]]
444a = assert(load(read1(x), "read", "t"))
445assert(a()(2)(3)(10) == 15)
446
447-- repeat the test loading a binary chunk
448x = string.dump(a)
449a = assert(load(read1(x), "read", "b"))
450assert(a()(2)(3)(10) == 15)
451
452
453-- test for dump/undump with upvalues
454local a, b = 20, 30
455x = load(string.dump(function (x)
456 if x == "set" then a = 10+b; b = b+1 else
457 return a
458 end
459end), "", "b", nil)
460assert(x() == nil)
461assert(debug.setupvalue(x, 1, "hi") == "a")
462assert(x() == "hi")
463assert(debug.setupvalue(x, 2, 13) == "b")
464assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
465x("set")
466assert(x() == 23)
467x("set")
468assert(x() == 24)
469
470-- test for dump/undump with many upvalues
471do
472 local nup = 200 -- maximum number of local variables
473 local prog = {"local a1"}
474 for i = 2, nup do prog[#prog + 1] = ", a" .. i end
475 prog[#prog + 1] = " = 1"
476 for i = 2, nup do prog[#prog + 1] = ", " .. i end
477 local sum = 1
478 prog[#prog + 1] = "; return function () return a1"
479 for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
480 prog[#prog + 1] = " end"
481 prog = table.concat(prog)
482 local f = assert(load(prog))()
483 assert(f() == sum)
484
485 f = load(string.dump(f)) -- main chunk now has many upvalues
486 local a = 10
487 local h = function () return a end
488 for i = 1, nup do
489 debug.upvaluejoin(f, i, h, 1)
490 end
491 assert(f() == 10 * nup)
492end
493
494-- test for long method names
495do
496 local t = {x = 1}
497 function t:_012345678901234567890123456789012345678901234567890123456789 ()
498 return self.x
499 end
500 assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
501end
502
503
504-- test for bug in parameter adjustment
505assert((function () return nil end)(4) == nil)
506assert((function () local a; return a end)(4) == nil)
507assert((function (a) return a end)() == nil)
508
509
510print("testing binary chunks")
511do
512 local headformat = "c4BBc6BiBI4BjBn"
513 local header = { -- header components
514 "\27Lua", -- signature
515 0x55, -- version 5.5 (0x55)
516 0, -- format
517 "\x19\x93\r\n\x1a\n", -- a binary string
518 string.packsize("i"), -- size of an int
519 -0x5678, -- an int
520 4, -- size of an instruction
521 0x12345678, -- an instruction (4 bytes)
522 string.packsize("j"), -- size of a Lua integer
523 -0x5678, -- a Lua integer
524 string.packsize("n"), -- size of a Lua float
525 -370.5, -- a Lua float
526 }
527
528 local c = string.dump(function ()
529 local a = 1; local b = 3;
530 local f = function () return a + b + _ENV.c; end -- upvalues
531 local s1 = "a constant"
532 local s2 = "another constant"
533 return a + b * 3
534 end)
535
536 assert(assert(load(c))() == 10)
537
538 -- check header
539 local t = {string.unpack(headformat, c)}
540 for i = 1, #header do
541 assert(t[i] == header[i])
542 end
543
544 -- Testing corrupted header.
545 -- A single wrong byte in the head invalidates the chunk,
546 -- except for the Lua float check. (If numbers are long double,
547 -- the representation may need padding, and changing that padding
548 -- will not invalidate the chunk.)
549 local headlen = string.packsize(headformat)
550 headlen = headlen - string.packsize("n") -- remove float check
551 for i = 1, headlen do
552 local s = string.sub(c, 1, i - 1) ..
553 string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) ..
554 string.sub(c, i + 1, -1)
555 assert(#s == #c and s ~= c)
556 assert(not load(s))
557 end
558
559 -- loading truncated binary chunks
560 for i = 1, #c - 1 do
561 local st, msg = load(string.sub(c, 1, i))
562 assert(not st and string.find(msg, "truncated"))
563 end
564end
565
566
567do -- check reuse of strings in dumps
568 local str = "|" .. string.rep("X", 50) .. "|"
569 local foo = load(string.format([[
570 local str <const> = "%s"
571 return {
572 function () return str end,
573 function () return str end,
574 function () return str end
575 }
576 ]], str))
577 -- count occurrences of 'str' inside the dump
578 local dump = string.dump(foo)
579 local _, count = string.gsub(dump, str, {})
580 -- there should be only two occurrences:
581 -- one inside the source, other the string itself.
582 assert(count == 2)
583
584 if T then -- check reuse of strings in undump
585 local funcs = load(dump)()
586 assert(string.format("%p", T.listk(funcs[1])[1]) ==
587 string.format("%p", T.listk(funcs[3])[1]))
588 end
589end
590
591
592do -- test limit of multiple returns (254 values)
593 local code = "return 10" .. string.rep(",10", 253)
594 local res = {assert(load(code))()}
595 assert(#res == 254 and res[254] == 10)
596
597 code = code .. ",10"
598 local status, msg = load(code)
599 assert(not status and string.find(msg, "too many returns"))
600end
601
602print('OK')
603return deep