aboutsummaryrefslogtreecommitdiff
path: root/testes/calls.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/calls.lua')
-rw-r--r--testes/calls.lua435
1 files changed, 435 insertions, 0 deletions
diff --git a/testes/calls.lua b/testes/calls.lua
new file mode 100644
index 00000000..95d9d6d6
--- /dev/null
+++ b/testes/calls.lua
@@ -0,0 +1,435 @@
1-- $Id: calls.lua,v 1.66 2018/02/09 16:35:21 roberto Exp $
2-- See Copyright Notice in file all.lua
3
4print("testing functions and calls")
5
6local debug = require "debug"
7
8-- get the opportunity to test 'type' too ;)
9
10assert(type(1<2) == 'boolean')
11assert(type(true) == 'boolean' and type(false) == 'boolean')
12assert(type(nil) == 'nil'
13 and type(-3) == 'number'
14 and type'x' == 'string'
15 and type{} == 'table'
16 and type(type) == 'function')
17
18assert(type(assert) == type(print))
19function f (x) return a:x (x) end
20assert(type(f) == 'function')
21assert(not pcall(type))
22
23
24do -- test error in 'print' too...
25 local tostring = _ENV.tostring
26
27 _ENV.tostring = nil
28 local st, msg = pcall(print, 1)
29 assert(st == false and string.find(msg, "attempt to call a nil value"))
30
31 _ENV.tostring = function () return {} end
32 local st, msg = pcall(print, 1)
33 assert(st == false and string.find(msg, "must return a string"))
34
35 _ENV.tostring = tostring
36end
37
38
39-- testing local-function recursion
40fact = false
41do
42 local res = 1
43 local function fact (n)
44 if n==0 then return res
45 else return n*fact(n-1)
46 end
47 end
48 assert(fact(5) == 120)
49end
50assert(fact == false)
51
52-- testing declarations
53a = {i = 10}
54self = 20
55function a:x (x) return x+self.i end
56function a.y (x) return x+self end
57
58assert(a:x(1)+10 == a.y(1))
59
60a.t = {i=-100}
61a["t"].x = function (self, a,b) return self.i+a+b end
62
63assert(a.t:x(2,3) == -95)
64
65do
66 local a = {x=0}
67 function a:add (x) self.x, a.y = self.x+x, 20; return self end
68 assert(a:add(10):add(20):add(30).x == 60 and a.y == 20)
69end
70
71local a = {b={c={}}}
72
73function a.b.c.f1 (x) return x+1 end
74function a.b.c:f2 (x,y) self[x] = y end
75assert(a.b.c.f1(4) == 5)
76a.b.c:f2('k', 12); assert(a.b.c.k == 12)
77
78print('+')
79
80t = nil -- 'declare' t
81function f(a,b,c) local d = 'a'; t={a,b,c,d} end
82
83f( -- this line change must be valid
84 1,2)
85assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
86f(1,2, -- this one too
87 3,4)
88assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
89
90function fat(x)
91 if x <= 1 then return 1
92 else return x*load("return fat(" .. x-1 .. ")", "")()
93 end
94end
95
96assert(load "load 'assert(fat(6)==720)' () ")()
97a = load('return fat(5), 3')
98a,b = a()
99assert(a == 120 and b == 3)
100print('+')
101
102function err_on_n (n)
103 if n==0 then error(); exit(1);
104 else err_on_n (n-1); exit(1);
105 end
106end
107
108do
109 function dummy (n)
110 if n > 0 then
111 assert(not pcall(err_on_n, n))
112 dummy(n-1)
113 end
114 end
115end
116
117dummy(10)
118
119function deep (n)
120 if n>0 then deep(n-1) end
121end
122deep(10)
123deep(180)
124
125-- testing tail calls
126function deep (n) if n>0 then return deep(n-1) else return 101 end end
127assert(deep(30000) == 101)
128a = {}
129function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
130assert(a:deep(30000) == 101)
131
132do -- tail calls x varargs
133 local function foo (x, ...) local a = {...}; return x, a[1], a[2] end
134
135 local function foo1 (x) return foo(10, x, x + 1) end
136
137 local a, b, c = foo1(-2)
138 assert(a == 10 and b == -2 and c == -1)
139
140 -- tail calls x metamethods
141 local t = setmetatable({}, {__call = foo})
142 local function foo2 (x) return t(10, x) end
143 a, b, c = foo2(100)
144 assert(a == t and b == 10 and c == 100)
145
146 a, b = (function () return foo() end)()
147 assert(a == nil and b == nil)
148
149 local X, Y, A
150 local function foo (x, y, ...) X = x; Y = y; A = {...} end
151 local function foo1 (...) return foo(...) end
152
153 local a, b, c = foo1()
154 assert(X == nil and Y == nil and #A == 0)
155
156 a, b, c = foo1(10)
157 assert(X == 10 and Y == nil and #A == 0)
158
159 a, b, c = foo1(10, 20)
160 assert(X == 10 and Y == 20 and #A == 0)
161
162 a, b, c = foo1(10, 20, 30)
163 assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
164end
165
166print('+')
167
168
169a = nil
170(function (x) a=x end)(23)
171assert(a == 23 and (function (x) return x*2 end)(20) == 40)
172
173
174-- testing closures
175
176-- fixed-point operator
177Z = function (le)
178 local function a (f)
179 return le(function (x) return f(f)(x) end)
180 end
181 return a(a)
182 end
183
184
185-- non-recursive factorial
186
187F = function (f)
188 return function (n)
189 if n == 0 then return 1
190 else return n*f(n-1) end
191 end
192 end
193
194fat = Z(F)
195
196assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
197
198local function g (z)
199 local function f (a,b,c,d)
200 return function (x,y) return a+b+c+d+a+x+y+z end
201 end
202 return f(z,z+1,z+2,z+3)
203end
204
205f = g(10)
206assert(f(9, 16) == 10+11+12+13+10+9+16+10)
207
208Z, F, f = nil
209print('+')
210
211-- testing multiple returns
212
213function unlpack (t, i)
214 i = i or 1
215 if (i <= #t) then
216 return t[i], unlpack(t, i+1)
217 end
218end
219
220function equaltab (t1, t2)
221 assert(#t1 == #t2)
222 for i = 1, #t1 do
223 assert(t1[i] == t2[i])
224 end
225end
226
227local pack = function (...) return (table.pack(...)) end
228
229function f() return 1,2,30,4 end
230function ret2 (a,b) return a,b end
231
232local a,b,c,d = unlpack{1,2,3}
233assert(a==1 and b==2 and c==3 and d==nil)
234a = {1,2,3,4,false,10,'alo',false,assert}
235equaltab(pack(unlpack(a)), a)
236equaltab(pack(unlpack(a), -1), {1,-1})
237a,b,c,d = ret2(f()), ret2(f())
238assert(a==1 and b==1 and c==2 and d==nil)
239a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
240assert(a==1 and b==1 and c==2 and d==nil)
241a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
242assert(a==1 and b==1 and c==nil and d==nil)
243
244a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
245assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
246
247
248-- testing calls with 'incorrect' arguments
249rawget({}, "x", 1)
250rawset({}, "x", 1, 2)
251assert(math.sin(1,2) == math.sin(1))
252table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
253
254
255-- test for generic load
256local x = "-- a comment\0\0\0\n x = 10 + \n23; \
257 local a = function () x = 'hi' end; \
258 return '\0'"
259function read1 (x)
260 local i = 0
261 return function ()
262 collectgarbage()
263 i=i+1
264 return string.sub(x, i, i)
265 end
266end
267
268function cannotload (msg, a,b)
269 assert(not a and string.find(b, msg))
270end
271
272a = assert(load(read1(x), "modname", "t", _G))
273assert(a() == "\0" and _G.x == 33)
274assert(debug.getinfo(a).source == "modname")
275-- cannot read text in binary mode
276cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
277cannotload("attempt to load a text chunk", load(x, "modname", "b"))
278
279a = assert(load(function () return nil end))
280a() -- empty chunk
281
282assert(not load(function () return true end))
283
284
285-- small bug
286local t = {nil, "return ", "3"}
287f, msg = load(function () return table.remove(t, 1) end)
288assert(f() == nil) -- should read the empty chunk
289
290-- another small bug (in 5.2.1)
291f = load(string.dump(function () return 1 end), nil, "b", {})
292assert(type(f) == "function" and f() == 1)
293
294
295x = string.dump(load("x = 1; return x"))
296a = assert(load(read1(x), nil, "b"))
297assert(a() == 1 and _G.x == 1)
298cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
299cannotload("attempt to load a binary chunk", load(x, nil, "t"))
300
301assert(not pcall(string.dump, print)) -- no dump of C functions
302
303cannotload("unexpected symbol", load(read1("*a = 123")))
304cannotload("unexpected symbol", load("*a = 123"))
305cannotload("hhi", load(function () error("hhi") end))
306
307-- any value is valid for _ENV
308assert(load("return _ENV", nil, nil, 123)() == 123)
309
310
311-- load when _ENV is not first upvalue
312local x; XX = 123
313local function h ()
314 local y=x -- use 'x', so that it becomes 1st upvalue
315 return XX -- global name
316end
317local d = string.dump(h)
318x = load(d, "", "b")
319assert(debug.getupvalue(x, 2) == '_ENV')
320debug.setupvalue(x, 2, _G)
321assert(x() == 123)
322
323assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
324
325
326-- test generic load with nested functions
327x = [[
328 return function (x)
329 return function (y)
330 return function (z)
331 return x+y+z
332 end
333 end
334 end
335]]
336
337a = assert(load(read1(x)))
338assert(a()(2)(3)(10) == 15)
339
340
341-- test for dump/undump with upvalues
342local a, b = 20, 30
343x = load(string.dump(function (x)
344 if x == "set" then a = 10+b; b = b+1 else
345 return a
346 end
347end), "", "b", nil)
348assert(x() == nil)
349assert(debug.setupvalue(x, 1, "hi") == "a")
350assert(x() == "hi")
351assert(debug.setupvalue(x, 2, 13) == "b")
352assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
353x("set")
354assert(x() == 23)
355x("set")
356assert(x() == 24)
357
358-- test for dump/undump with many upvalues
359do
360 local nup = 200 -- maximum number of local variables
361 local prog = {"local a1"}
362 for i = 2, nup do prog[#prog + 1] = ", a" .. i end
363 prog[#prog + 1] = " = 1"
364 for i = 2, nup do prog[#prog + 1] = ", " .. i end
365 local sum = 1
366 prog[#prog + 1] = "; return function () return a1"
367 for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
368 prog[#prog + 1] = " end"
369 prog = table.concat(prog)
370 local f = assert(load(prog))()
371 assert(f() == sum)
372
373 f = load(string.dump(f)) -- main chunk now has many upvalues
374 local a = 10
375 local h = function () return a end
376 for i = 1, nup do
377 debug.upvaluejoin(f, i, h, 1)
378 end
379 assert(f() == 10 * nup)
380end
381
382-- test for long method names
383do
384 local t = {x = 1}
385 function t:_012345678901234567890123456789012345678901234567890123456789 ()
386 return self.x
387 end
388 assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
389end
390
391
392-- test for bug in parameter adjustment
393assert((function () return nil end)(4) == nil)
394assert((function () local a; return a end)(4) == nil)
395assert((function (a) return a end)() == nil)
396
397
398print("testing binary chunks")
399do
400 local header = string.pack("c4BBc6BBBBBj",
401 "\27Lua", -- signature
402 5*16 + 4, -- version 5.4
403 0, -- format
404 "\x19\x93\r\n\x1a\n", -- data
405 string.packsize("i"), -- sizeof(int)
406 string.packsize("T"), -- sizeof(size_t)
407 4, -- size of instruction
408 string.packsize("j"), -- sizeof(lua integer)
409 string.packsize("n"), -- sizeof(lua number)
410 0x5678 -- LUAC_INT
411 -- LUAC_NUM may not have a unique binary representation (padding...)
412 )
413 local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)
414
415 assert(string.sub(c, 1, #header) == header)
416
417 -- corrupted header
418 for i = 1, #header do
419 local s = string.sub(c, 1, i - 1) ..
420 string.char(string.byte(string.sub(c, i, i)) + 1) ..
421 string.sub(c, i + 1, -1)
422 assert(#s == #c)
423 assert(not load(s))
424 end
425
426 -- loading truncated binary chunks
427 for i = 1, #c - 1 do
428 local st, msg = load(string.sub(c, 1, i))
429 assert(not st and string.find(msg, "truncated"))
430 end
431 assert(assert(load(c))() == 10)
432end
433
434print('OK')
435return deep