summaryrefslogtreecommitdiff
path: root/testes/calls.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-17 14:46:37 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-17 14:46:37 -0200
commit063d4e4543088e7a21965bda8ee5a0f952a9029e (patch)
tree6c3f2f8e98c26f071a94a32f9f2754396a66a9de /testes/calls.lua
parente354c6355e7f48e087678ec49e340ca0696725b1 (diff)
downloadlua-5.3.5.tar.gz
lua-5.3.5.tar.bz2
lua-5.3.5.zip
Lua 5.3.5 ported to gitv5.3.5
This is the first commit for the branch Lua 5.3. All source files were copied from the official distribution of 5.3.5 in the Lua site. The test files are the same of 5.3.4. The manual came from the previous RCS repository, revision 1.167.1.2.
Diffstat (limited to 'testes/calls.lua')
-rw-r--r--testes/calls.lua401
1 files changed, 401 insertions, 0 deletions
diff --git a/testes/calls.lua b/testes/calls.lua
new file mode 100644
index 00000000..6d6fb7b2
--- /dev/null
+++ b/testes/calls.lua
@@ -0,0 +1,401 @@
1-- $Id: calls.lua,v 1.60 2016/11/07 13:11:28 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(200)
124
125-- testing tail call
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
132print('+')
133
134
135a = nil
136(function (x) a=x end)(23)
137assert(a == 23 and (function (x) return x*2 end)(20) == 40)
138
139
140-- testing closures
141
142-- fixed-point operator
143Z = function (le)
144 local function a (f)
145 return le(function (x) return f(f)(x) end)
146 end
147 return a(a)
148 end
149
150
151-- non-recursive factorial
152
153F = function (f)
154 return function (n)
155 if n == 0 then return 1
156 else return n*f(n-1) end
157 end
158 end
159
160fat = Z(F)
161
162assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
163
164local function g (z)
165 local function f (a,b,c,d)
166 return function (x,y) return a+b+c+d+a+x+y+z end
167 end
168 return f(z,z+1,z+2,z+3)
169end
170
171f = g(10)
172assert(f(9, 16) == 10+11+12+13+10+9+16+10)
173
174Z, F, f = nil
175print('+')
176
177-- testing multiple returns
178
179function unlpack (t, i)
180 i = i or 1
181 if (i <= #t) then
182 return t[i], unlpack(t, i+1)
183 end
184end
185
186function equaltab (t1, t2)
187 assert(#t1 == #t2)
188 for i = 1, #t1 do
189 assert(t1[i] == t2[i])
190 end
191end
192
193local pack = function (...) return (table.pack(...)) end
194
195function f() return 1,2,30,4 end
196function ret2 (a,b) return a,b end
197
198local a,b,c,d = unlpack{1,2,3}
199assert(a==1 and b==2 and c==3 and d==nil)
200a = {1,2,3,4,false,10,'alo',false,assert}
201equaltab(pack(unlpack(a)), a)
202equaltab(pack(unlpack(a), -1), {1,-1})
203a,b,c,d = ret2(f()), ret2(f())
204assert(a==1 and b==1 and c==2 and d==nil)
205a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
206assert(a==1 and b==1 and c==2 and d==nil)
207a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
208assert(a==1 and b==1 and c==nil and d==nil)
209
210a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
211assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
212
213
214-- testing calls with 'incorrect' arguments
215rawget({}, "x", 1)
216rawset({}, "x", 1, 2)
217assert(math.sin(1,2) == math.sin(1))
218table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
219
220
221-- test for generic load
222local x = "-- a comment\0\0\0\n x = 10 + \n23; \
223 local a = function () x = 'hi' end; \
224 return '\0'"
225function read1 (x)
226 local i = 0
227 return function ()
228 collectgarbage()
229 i=i+1
230 return string.sub(x, i, i)
231 end
232end
233
234function cannotload (msg, a,b)
235 assert(not a and string.find(b, msg))
236end
237
238a = assert(load(read1(x), "modname", "t", _G))
239assert(a() == "\0" and _G.x == 33)
240assert(debug.getinfo(a).source == "modname")
241-- cannot read text in binary mode
242cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
243cannotload("attempt to load a text chunk", load(x, "modname", "b"))
244
245a = assert(load(function () return nil end))
246a() -- empty chunk
247
248assert(not load(function () return true end))
249
250
251-- small bug
252local t = {nil, "return ", "3"}
253f, msg = load(function () return table.remove(t, 1) end)
254assert(f() == nil) -- should read the empty chunk
255
256-- another small bug (in 5.2.1)
257f = load(string.dump(function () return 1 end), nil, "b", {})
258assert(type(f) == "function" and f() == 1)
259
260
261x = string.dump(load("x = 1; return x"))
262a = assert(load(read1(x), nil, "b"))
263assert(a() == 1 and _G.x == 1)
264cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
265cannotload("attempt to load a binary chunk", load(x, nil, "t"))
266
267assert(not pcall(string.dump, print)) -- no dump of C functions
268
269cannotload("unexpected symbol", load(read1("*a = 123")))
270cannotload("unexpected symbol", load("*a = 123"))
271cannotload("hhi", load(function () error("hhi") end))
272
273-- any value is valid for _ENV
274assert(load("return _ENV", nil, nil, 123)() == 123)
275
276
277-- load when _ENV is not first upvalue
278local x; XX = 123
279local function h ()
280 local y=x -- use 'x', so that it becomes 1st upvalue
281 return XX -- global name
282end
283local d = string.dump(h)
284x = load(d, "", "b")
285assert(debug.getupvalue(x, 2) == '_ENV')
286debug.setupvalue(x, 2, _G)
287assert(x() == 123)
288
289assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
290
291
292-- test generic load with nested functions
293x = [[
294 return function (x)
295 return function (y)
296 return function (z)
297 return x+y+z
298 end
299 end
300 end
301]]
302
303a = assert(load(read1(x)))
304assert(a()(2)(3)(10) == 15)
305
306
307-- test for dump/undump with upvalues
308local a, b = 20, 30
309x = load(string.dump(function (x)
310 if x == "set" then a = 10+b; b = b+1 else
311 return a
312 end
313end), "", "b", nil)
314assert(x() == nil)
315assert(debug.setupvalue(x, 1, "hi") == "a")
316assert(x() == "hi")
317assert(debug.setupvalue(x, 2, 13) == "b")
318assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
319x("set")
320assert(x() == 23)
321x("set")
322assert(x() == 24)
323
324-- test for dump/undump with many upvalues
325do
326 local nup = 200 -- maximum number of local variables
327 local prog = {"local a1"}
328 for i = 2, nup do prog[#prog + 1] = ", a" .. i end
329 prog[#prog + 1] = " = 1"
330 for i = 2, nup do prog[#prog + 1] = ", " .. i end
331 local sum = 1
332 prog[#prog + 1] = "; return function () return a1"
333 for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
334 prog[#prog + 1] = " end"
335 prog = table.concat(prog)
336 local f = assert(load(prog))()
337 assert(f() == sum)
338
339 f = load(string.dump(f)) -- main chunk now has many upvalues
340 local a = 10
341 local h = function () return a end
342 for i = 1, nup do
343 debug.upvaluejoin(f, i, h, 1)
344 end
345 assert(f() == 10 * nup)
346end
347
348-- test for long method names
349do
350 local t = {x = 1}
351 function t:_012345678901234567890123456789012345678901234567890123456789 ()
352 return self.x
353 end
354 assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
355end
356
357
358-- test for bug in parameter adjustment
359assert((function () return nil end)(4) == nil)
360assert((function () local a; return a end)(4) == nil)
361assert((function (a) return a end)() == nil)
362
363
364print("testing binary chunks")
365do
366 local header = string.pack("c4BBc6BBBBBj",
367 "\27Lua", -- signature
368 5*16 + 3, -- version 5.3
369 0, -- format
370 "\x19\x93\r\n\x1a\n", -- data
371 string.packsize("i"), -- sizeof(int)
372 string.packsize("T"), -- sizeof(size_t)
373 4, -- size of instruction
374 string.packsize("j"), -- sizeof(lua integer)
375 string.packsize("n"), -- sizeof(lua number)
376 0x5678 -- LUAC_INT
377 -- LUAC_NUM may not have a unique binary representation (padding...)
378 )
379 local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)
380
381 assert(string.sub(c, 1, #header) == header)
382
383 -- corrupted header
384 for i = 1, #header do
385 local s = string.sub(c, 1, i - 1) ..
386 string.char(string.byte(string.sub(c, i, i)) + 1) ..
387 string.sub(c, i + 1, -1)
388 assert(#s == #c)
389 assert(not load(s))
390 end
391
392 -- loading truncated binary chunks
393 for i = 1, #c - 1 do
394 local st, msg = load(string.sub(c, 1, i))
395 assert(not st and string.find(msg, "truncated"))
396 end
397 assert(assert(load(c))() == 10)
398end
399
400print('OK')
401return deep