aboutsummaryrefslogtreecommitdiff
path: root/testes/vararg.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
commit314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch)
tree594b7e873f2c29113d95c75147ab10865cdd772c /testes/vararg.lua
parent0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff)
downloadlua-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/vararg.lua')
-rw-r--r--testes/vararg.lua18
1 files changed, 9 insertions, 9 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua
index 44848d25..1b025102 100644
--- a/testes/vararg.lua
+++ b/testes/vararg.lua
@@ -3,13 +3,13 @@
3 3
4print('testing vararg') 4print('testing vararg')
5 5
6function f(a, ...) 6local function f (a, ...)
7 local x = {n = select('#', ...), ...} 7 local x = {n = select('#', ...), ...}
8 for i = 1, x.n do assert(a[i] == x[i]) end 8 for i = 1, x.n do assert(a[i] == x[i]) end
9 return x.n 9 return x.n
10end 10end
11 11
12function c12 (...) 12local function c12 (...)
13 assert(arg == _G.arg) -- no local 'arg' 13 assert(arg == _G.arg) -- no local 'arg'
14 local x = {...}; x.n = #x 14 local x = {...}; x.n = #x
15 local res = (x.n==2 and x[1] == 1 and x[2] == 2) 15 local res = (x.n==2 and x[1] == 1 and x[2] == 2)
@@ -17,7 +17,7 @@ function c12 (...)
17 return res, 2 17 return res, 2
18end 18end
19 19
20function vararg (...) return {n = select('#', ...), ...} end 20local function vararg (...) return {n = select('#', ...), ...} end
21 21
22local call = function (f, args) return f(table.unpack(args, 1, args.n)) end 22local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
23 23
@@ -29,7 +29,7 @@ assert(vararg().n == 0)
29assert(vararg(nil, nil).n == 2) 29assert(vararg(nil, nil).n == 2)
30 30
31assert(c12(1,2)==55) 31assert(c12(1,2)==55)
32a,b = assert(call(c12, {1,2})) 32local a,b = assert(call(c12, {1,2}))
33assert(a == 55 and b == 2) 33assert(a == 55 and b == 2)
34a = call(c12, {1,2;n=2}) 34a = call(c12, {1,2;n=2})
35assert(a == 55 and b == 2) 35assert(a == 55 and b == 2)
@@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end
49assert(t:f(1,4) == 3 and t:f(2) == 11) 49assert(t:f(1,4) == 3 and t:f(2) == 11)
50print('+') 50print('+')
51 51
52lim = 20 52local lim = 20
53local i, a = 1, {} 53local i, a = 1, {}
54while i <= lim do a[i] = i+0.3; i=i+1 end 54while i <= lim do a[i] = i+0.3; i=i+1 end
55 55
@@ -59,7 +59,7 @@ function f(a, b, c, d, ...)
59 more[lim-4] == lim+0.3 and not more[lim-3]) 59 more[lim-4] == lim+0.3 and not more[lim-3])
60end 60end
61 61
62function g(a,b,c) 62local function g (a,b,c)
63 assert(a == 1.3 and b == 2.3 and c == 3.3) 63 assert(a == 1.3 and b == 2.3 and c == 3.3)
64end 64end
65 65
@@ -76,7 +76,7 @@ print("+")
76 76
77-- new-style varargs 77-- new-style varargs
78 78
79function oneless (a, ...) return ... end 79local function oneless (a, ...) return ... end
80 80
81function f (n, a, ...) 81function f (n, a, ...)
82 local b 82 local b
@@ -99,8 +99,8 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil)
99 99
100 100
101-- varargs for main chunks 101-- varargs for main chunks
102f = load[[ return {...} ]] 102local f = load[[ return {...} ]]
103x = f(2,3) 103local x = f(2,3)
104assert(x[1] == 2 and x[2] == 3 and x[3] == undef) 104assert(x[1] == 2 and x[2] == 3 and x[3] == undef)
105 105
106 106