diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-17 14:46:37 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-17 14:46:37 -0200 |
commit | 063d4e4543088e7a21965bda8ee5a0f952a9029e (patch) | |
tree | 6c3f2f8e98c26f071a94a32f9f2754396a66a9de /testes/vararg.lua | |
parent | e354c6355e7f48e087678ec49e340ca0696725b1 (diff) | |
download | lua-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/vararg.lua')
-rw-r--r-- | testes/vararg.lua | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua new file mode 100644 index 00000000..d617a98b --- /dev/null +++ b/testes/vararg.lua | |||
@@ -0,0 +1,142 @@ | |||
1 | -- $Id: vararg.lua,v 1.25 2016/11/07 13:11:28 roberto Exp $ | ||
2 | -- See Copyright Notice in file all.lua | ||
3 | |||
4 | print('testing vararg') | ||
5 | |||
6 | function f(a, ...) | ||
7 | local arg = {n = select('#', ...), ...} | ||
8 | for i=1,arg.n do assert(a[i]==arg[i]) end | ||
9 | return arg.n | ||
10 | end | ||
11 | |||
12 | function c12 (...) | ||
13 | assert(arg == _G.arg) -- no local 'arg' | ||
14 | local x = {...}; x.n = #x | ||
15 | local res = (x.n==2 and x[1] == 1 and x[2] == 2) | ||
16 | if res then res = 55 end | ||
17 | return res, 2 | ||
18 | end | ||
19 | |||
20 | function vararg (...) return {n = select('#', ...), ...} end | ||
21 | |||
22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end | ||
23 | |||
24 | assert(f() == 0) | ||
25 | assert(f({1,2,3}, 1, 2, 3) == 3) | ||
26 | assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5) | ||
27 | |||
28 | assert(c12(1,2)==55) | ||
29 | a,b = assert(call(c12, {1,2})) | ||
30 | assert(a == 55 and b == 2) | ||
31 | a = call(c12, {1,2;n=2}) | ||
32 | assert(a == 55 and b == 2) | ||
33 | a = call(c12, {1,2;n=1}) | ||
34 | assert(not a) | ||
35 | assert(c12(1,2,3) == false) | ||
36 | local a = vararg(call(next, {_G,nil;n=2})) | ||
37 | local b,c = next(_G) | ||
38 | assert(a[1] == b and a[2] == c and a.n == 2) | ||
39 | a = vararg(call(call, {c12, {1,2}})) | ||
40 | assert(a.n == 2 and a[1] == 55 and a[2] == 2) | ||
41 | a = call(print, {'+'}) | ||
42 | assert(a == nil) | ||
43 | |||
44 | local t = {1, 10} | ||
45 | function t:f (...) local arg = {...}; return self[...]+#arg end | ||
46 | assert(t:f(1,4) == 3 and t:f(2) == 11) | ||
47 | print('+') | ||
48 | |||
49 | lim = 20 | ||
50 | local i, a = 1, {} | ||
51 | while i <= lim do a[i] = i+0.3; i=i+1 end | ||
52 | |||
53 | function f(a, b, c, d, ...) | ||
54 | local more = {...} | ||
55 | assert(a == 1.3 and more[1] == 5.3 and | ||
56 | more[lim-4] == lim+0.3 and not more[lim-3]) | ||
57 | end | ||
58 | |||
59 | function g(a,b,c) | ||
60 | assert(a == 1.3 and b == 2.3 and c == 3.3) | ||
61 | end | ||
62 | |||
63 | call(f, a) | ||
64 | call(g, a) | ||
65 | |||
66 | a = {} | ||
67 | i = 1 | ||
68 | while i <= lim do a[i] = i; i=i+1 end | ||
69 | assert(call(math.max, a) == lim) | ||
70 | |||
71 | print("+") | ||
72 | |||
73 | |||
74 | -- new-style varargs | ||
75 | |||
76 | function oneless (a, ...) return ... end | ||
77 | |||
78 | function f (n, a, ...) | ||
79 | local b | ||
80 | assert(arg == _G.arg) -- no local 'arg' | ||
81 | if n == 0 then | ||
82 | local b, c, d = ... | ||
83 | return a, b, c, d, oneless(oneless(oneless(...))) | ||
84 | else | ||
85 | n, b, a = n-1, ..., a | ||
86 | assert(b == ...) | ||
87 | return f(n, a, ...) | ||
88 | end | ||
89 | end | ||
90 | |||
91 | a,b,c,d,e = assert(f(10,5,4,3,2,1)) | ||
92 | assert(a==5 and b==4 and c==3 and d==2 and e==1) | ||
93 | |||
94 | a,b,c,d,e = f(4) | ||
95 | assert(a==nil and b==nil and c==nil and d==nil and e==nil) | ||
96 | |||
97 | |||
98 | -- varargs for main chunks | ||
99 | f = load[[ return {...} ]] | ||
100 | x = f(2,3) | ||
101 | assert(x[1] == 2 and x[2] == 3 and x[3] == nil) | ||
102 | |||
103 | |||
104 | f = load[[ | ||
105 | local x = {...} | ||
106 | for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end | ||
107 | assert(x[select('#', ...)+1] == nil) | ||
108 | return true | ||
109 | ]] | ||
110 | |||
111 | assert(f("a", "b", nil, {}, assert)) | ||
112 | assert(f()) | ||
113 | |||
114 | a = {select(3, table.unpack{10,20,30,40})} | ||
115 | assert(#a == 2 and a[1] == 30 and a[2] == 40) | ||
116 | a = {select(1)} | ||
117 | assert(next(a) == nil) | ||
118 | a = {select(-1, 3, 5, 7)} | ||
119 | assert(a[1] == 7 and a[2] == nil) | ||
120 | a = {select(-2, 3, 5, 7)} | ||
121 | assert(a[1] == 5 and a[2] == 7 and a[3] == nil) | ||
122 | pcall(select, 10000) | ||
123 | pcall(select, -10000) | ||
124 | |||
125 | |||
126 | -- bug in 5.2.2 | ||
127 | |||
128 | function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, | ||
129 | p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, | ||
130 | p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, | ||
131 | p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, | ||
132 | p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) | ||
133 | local a1,a2,a3,a4,a5,a6,a7 | ||
134 | local a8,a9,a10,a11,a12,a13,a14 | ||
135 | end | ||
136 | |||
137 | -- assertion fail here | ||
138 | f() | ||
139 | |||
140 | |||
141 | print('OK') | ||
142 | |||