aboutsummaryrefslogtreecommitdiff
path: root/testes/calls.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/calls.lua')
-rw-r--r--testes/calls.lua44
1 files changed, 25 insertions, 19 deletions
diff --git a/testes/calls.lua b/testes/calls.lua
index ee8cce73..a1938584 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -16,7 +16,7 @@ assert(type(nil) == 'nil'
16 and type(type) == 'function') 16 and type(type) == 'function')
17 17
18assert(type(assert) == type(print)) 18assert(type(assert) == type(print))
19function f (x) return a:x (x) end 19local function f (x) return a:x (x) end
20assert(type(f) == 'function') 20assert(type(f) == 'function')
21assert(not pcall(type)) 21assert(not pcall(type))
22 22
@@ -33,10 +33,11 @@ do
33 assert(fact(5) == 120) 33 assert(fact(5) == 120)
34end 34end
35assert(fact == false) 35assert(fact == false)
36fact = nil
36 37
37-- testing declarations 38-- testing declarations
38a = {i = 10} 39local a = {i = 10}
39self = 20 40local self = 20
40function a:x (x) return x+self.i end 41function a:x (x) return x+self.i end
41function a.y (x) return x+self end 42function a.y (x) return x+self end
42 43
@@ -72,6 +73,8 @@ f(1,2, -- this one too
72 3,4) 73 3,4)
73assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') 74assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
74 75
76t = nil -- delete 't'
77
75function fat(x) 78function fat(x)
76 if x <= 1 then return 1 79 if x <= 1 then return 1
77 else return x*load("return fat(" .. x-1 .. ")", "")() 80 else return x*load("return fat(" .. x-1 .. ")", "")()
@@ -80,26 +83,29 @@ end
80 83
81assert(load "load 'assert(fat(6)==720)' () ")() 84assert(load "load 'assert(fat(6)==720)' () ")()
82a = load('return fat(5), 3') 85a = load('return fat(5), 3')
83a,b = a() 86local a,b = a()
84assert(a == 120 and b == 3) 87assert(a == 120 and b == 3)
88fat = nil
85print('+') 89print('+')
86 90
87function err_on_n (n) 91local function err_on_n (n)
88 if n==0 then error(); exit(1); 92 if n==0 then error(); exit(1);
89 else err_on_n (n-1); exit(1); 93 else err_on_n (n-1); exit(1);
90 end 94 end
91end 95end
92 96
93do 97do
94 function dummy (n) 98 local function dummy (n)
95 if n > 0 then 99 if n > 0 then
96 assert(not pcall(err_on_n, n)) 100 assert(not pcall(err_on_n, n))
97 dummy(n-1) 101 dummy(n-1)
98 end 102 end
99 end 103 end
104
105 dummy(10)
100end 106end
101 107
102dummy(10) 108_G.deep = nil -- "declaration" (used by 'all.lua')
103 109
104function deep (n) 110function deep (n)
105 if n>0 then deep(n-1) end 111 if n>0 then deep(n-1) end
@@ -209,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40)
209-- testing closures 215-- testing closures
210 216
211-- fixed-point operator 217-- fixed-point operator
212Z = function (le) 218local Z = function (le)
213 local function a (f) 219 local function a (f)
214 return le(function (x) return f(f)(x) end) 220 return le(function (x) return f(f)(x) end)
215 end 221 end
@@ -219,14 +225,14 @@ Z = function (le)
219 225
220-- non-recursive factorial 226-- non-recursive factorial
221 227
222F = function (f) 228local F = function (f)
223 return function (n) 229 return function (n)
224 if n == 0 then return 1 230 if n == 0 then return 1
225 else return n*f(n-1) end 231 else return n*f(n-1) end
226 end 232 end
227 end 233 end
228 234
229fat = Z(F) 235local fat = Z(F)
230 236
231assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) 237assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
232 238
@@ -237,22 +243,21 @@ local function g (z)
237 return f(z,z+1,z+2,z+3) 243 return f(z,z+1,z+2,z+3)
238end 244end
239 245
240f = g(10) 246local f = g(10)
241assert(f(9, 16) == 10+11+12+13+10+9+16+10) 247assert(f(9, 16) == 10+11+12+13+10+9+16+10)
242 248
243Z, F, f = nil
244print('+') 249print('+')
245 250
246-- testing multiple returns 251-- testing multiple returns
247 252
248function unlpack (t, i) 253local function unlpack (t, i)
249 i = i or 1 254 i = i or 1
250 if (i <= #t) then 255 if (i <= #t) then
251 return t[i], unlpack(t, i+1) 256 return t[i], unlpack(t, i+1)
252 end 257 end
253end 258end
254 259
255function equaltab (t1, t2) 260local function equaltab (t1, t2)
256 assert(#t1 == #t2) 261 assert(#t1 == #t2)
257 for i = 1, #t1 do 262 for i = 1, #t1 do
258 assert(t1[i] == t2[i]) 263 assert(t1[i] == t2[i])
@@ -261,8 +266,8 @@ end
261 266
262local pack = function (...) return (table.pack(...)) end 267local pack = function (...) return (table.pack(...)) end
263 268
264function f() return 1,2,30,4 end 269local function f() return 1,2,30,4 end
265function ret2 (a,b) return a,b end 270local function ret2 (a,b) return a,b end
266 271
267local a,b,c,d = unlpack{1,2,3} 272local a,b,c,d = unlpack{1,2,3}
268assert(a==1 and b==2 and c==3 and d==nil) 273assert(a==1 and b==2 and c==3 and d==nil)
@@ -291,7 +296,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
291local x = "-- a comment\0\0\0\n x = 10 + \n23; \ 296local x = "-- a comment\0\0\0\n x = 10 + \n23; \
292 local a = function () x = 'hi' end; \ 297 local a = function () x = 'hi' end; \
293 return '\0'" 298 return '\0'"
294function read1 (x) 299local function read1 (x)
295 local i = 0 300 local i = 0
296 return function () 301 return function ()
297 collectgarbage() 302 collectgarbage()
@@ -300,7 +305,7 @@ function read1 (x)
300 end 305 end
301end 306end
302 307
303function cannotload (msg, a,b) 308local function cannotload (msg, a,b)
304 assert(not a and string.find(b, msg)) 309 assert(not a and string.find(b, msg))
305end 310end
306 311
@@ -342,6 +347,7 @@ a = assert(load(read1(x), nil, "b"))
342assert(a() == 1 and _G.x == 1) 347assert(a() == 1 and _G.x == 1)
343cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) 348cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
344cannotload("attempt to load a binary chunk", load(x, nil, "t")) 349cannotload("attempt to load a binary chunk", load(x, nil, "t"))
350_G.x = nil
345 351
346assert(not pcall(string.dump, print)) -- no dump of C functions 352assert(not pcall(string.dump, print)) -- no dump of C functions
347 353
@@ -366,7 +372,7 @@ debug.setupvalue(x, 2, _G)
366assert(x() == 123) 372assert(x() == 123)
367 373
368assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) 374assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
369 375XX = nil
370 376
371-- test generic load with nested functions 377-- test generic load with nested functions
372x = [[ 378x = [[