diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
commit | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch) | |
tree | 594b7e873f2c29113d95c75147ab10865cdd772c /testes/calls.lua | |
parent | 0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff) | |
download | lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.gz lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.bz2 lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.zip |
Avoid excessive name pollution in test files
Test files are more polite regarding the use of globals when locals
would do, and when globals are necessary deleting them after use.
Diffstat (limited to 'testes/calls.lua')
-rw-r--r-- | testes/calls.lua | 44 |
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 | ||
18 | assert(type(assert) == type(print)) | 18 | assert(type(assert) == type(print)) |
19 | function f (x) return a:x (x) end | 19 | local function f (x) return a:x (x) end |
20 | assert(type(f) == 'function') | 20 | assert(type(f) == 'function') |
21 | assert(not pcall(type)) | 21 | assert(not pcall(type)) |
22 | 22 | ||
@@ -33,10 +33,11 @@ do | |||
33 | assert(fact(5) == 120) | 33 | assert(fact(5) == 120) |
34 | end | 34 | end |
35 | assert(fact == false) | 35 | assert(fact == false) |
36 | fact = nil | ||
36 | 37 | ||
37 | -- testing declarations | 38 | -- testing declarations |
38 | a = {i = 10} | 39 | local a = {i = 10} |
39 | self = 20 | 40 | local self = 20 |
40 | function a:x (x) return x+self.i end | 41 | function a:x (x) return x+self.i end |
41 | function a.y (x) return x+self end | 42 | function 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) |
73 | assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') | 74 | assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') |
74 | 75 | ||
76 | t = nil -- delete 't' | ||
77 | |||
75 | function fat(x) | 78 | function 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 | ||
81 | assert(load "load 'assert(fat(6)==720)' () ")() | 84 | assert(load "load 'assert(fat(6)==720)' () ")() |
82 | a = load('return fat(5), 3') | 85 | a = load('return fat(5), 3') |
83 | a,b = a() | 86 | local a,b = a() |
84 | assert(a == 120 and b == 3) | 87 | assert(a == 120 and b == 3) |
88 | fat = nil | ||
85 | print('+') | 89 | print('+') |
86 | 90 | ||
87 | function err_on_n (n) | 91 | local 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 |
91 | end | 95 | end |
92 | 96 | ||
93 | do | 97 | do |
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) | ||
100 | end | 106 | end |
101 | 107 | ||
102 | dummy(10) | 108 | _G.deep = nil -- "declaration" (used by 'all.lua') |
103 | 109 | ||
104 | function deep (n) | 110 | function 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 |
212 | Z = function (le) | 218 | local 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 | ||
222 | F = function (f) | 228 | local 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 | ||
229 | fat = Z(F) | 235 | local fat = Z(F) |
230 | 236 | ||
231 | assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) | 237 | assert(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) |
238 | end | 244 | end |
239 | 245 | ||
240 | f = g(10) | 246 | local f = g(10) |
241 | assert(f(9, 16) == 10+11+12+13+10+9+16+10) | 247 | assert(f(9, 16) == 10+11+12+13+10+9+16+10) |
242 | 248 | ||
243 | Z, F, f = nil | ||
244 | print('+') | 249 | print('+') |
245 | 250 | ||
246 | -- testing multiple returns | 251 | -- testing multiple returns |
247 | 252 | ||
248 | function unlpack (t, i) | 253 | local 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 |
253 | end | 258 | end |
254 | 259 | ||
255 | function equaltab (t1, t2) | 260 | local 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 | ||
262 | local pack = function (...) return (table.pack(...)) end | 267 | local pack = function (...) return (table.pack(...)) end |
263 | 268 | ||
264 | function f() return 1,2,30,4 end | 269 | local function f() return 1,2,30,4 end |
265 | function ret2 (a,b) return a,b end | 270 | local function ret2 (a,b) return a,b end |
266 | 271 | ||
267 | local a,b,c,d = unlpack{1,2,3} | 272 | local a,b,c,d = unlpack{1,2,3} |
268 | assert(a==1 and b==2 and c==3 and d==nil) | 273 | assert(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") | |||
291 | local x = "-- a comment\0\0\0\n x = 10 + \n23; \ | 296 | local 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'" |
294 | function read1 (x) | 299 | local 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 |
301 | end | 306 | end |
302 | 307 | ||
303 | function cannotload (msg, a,b) | 308 | local function cannotload (msg, a,b) |
304 | assert(not a and string.find(b, msg)) | 309 | assert(not a and string.find(b, msg)) |
305 | end | 310 | end |
306 | 311 | ||
@@ -342,6 +347,7 @@ a = assert(load(read1(x), nil, "b")) | |||
342 | assert(a() == 1 and _G.x == 1) | 347 | assert(a() == 1 and _G.x == 1) |
343 | cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) | 348 | cannotload("attempt to load a binary chunk", load(read1(x), nil, "t")) |
344 | cannotload("attempt to load a binary chunk", load(x, nil, "t")) | 349 | cannotload("attempt to load a binary chunk", load(x, nil, "t")) |
350 | _G.x = nil | ||
345 | 351 | ||
346 | assert(not pcall(string.dump, print)) -- no dump of C functions | 352 | assert(not pcall(string.dump, print)) -- no dump of C functions |
347 | 353 | ||
@@ -366,7 +372,7 @@ debug.setupvalue(x, 2, _G) | |||
366 | assert(x() == 123) | 372 | assert(x() == 123) |
367 | 373 | ||
368 | assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) | 374 | assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17) |
369 | 375 | XX = nil | |
370 | 376 | ||
371 | -- test generic load with nested functions | 377 | -- test generic load with nested functions |
372 | x = [[ | 378 | x = [[ |