summaryrefslogtreecommitdiff
path: root/testes/vararg.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/vararg.lua')
-rw-r--r--testes/vararg.lua142
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
4print('testing vararg')
5
6function f(a, ...)
7 local arg = {n = select('#', ...), ...}
8 for i=1,arg.n do assert(a[i]==arg[i]) end
9 return arg.n
10end
11
12function 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
18end
19
20function vararg (...) return {n = select('#', ...), ...} end
21
22local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
23
24assert(f() == 0)
25assert(f({1,2,3}, 1, 2, 3) == 3)
26assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5)
27
28assert(c12(1,2)==55)
29a,b = assert(call(c12, {1,2}))
30assert(a == 55 and b == 2)
31a = call(c12, {1,2;n=2})
32assert(a == 55 and b == 2)
33a = call(c12, {1,2;n=1})
34assert(not a)
35assert(c12(1,2,3) == false)
36local a = vararg(call(next, {_G,nil;n=2}))
37local b,c = next(_G)
38assert(a[1] == b and a[2] == c and a.n == 2)
39a = vararg(call(call, {c12, {1,2}}))
40assert(a.n == 2 and a[1] == 55 and a[2] == 2)
41a = call(print, {'+'})
42assert(a == nil)
43
44local t = {1, 10}
45function t:f (...) local arg = {...}; return self[...]+#arg end
46assert(t:f(1,4) == 3 and t:f(2) == 11)
47print('+')
48
49lim = 20
50local i, a = 1, {}
51while i <= lim do a[i] = i+0.3; i=i+1 end
52
53function 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])
57end
58
59function g(a,b,c)
60 assert(a == 1.3 and b == 2.3 and c == 3.3)
61end
62
63call(f, a)
64call(g, a)
65
66a = {}
67i = 1
68while i <= lim do a[i] = i; i=i+1 end
69assert(call(math.max, a) == lim)
70
71print("+")
72
73
74-- new-style varargs
75
76function oneless (a, ...) return ... end
77
78function 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
89end
90
91a,b,c,d,e = assert(f(10,5,4,3,2,1))
92assert(a==5 and b==4 and c==3 and d==2 and e==1)
93
94a,b,c,d,e = f(4)
95assert(a==nil and b==nil and c==nil and d==nil and e==nil)
96
97
98-- varargs for main chunks
99f = load[[ return {...} ]]
100x = f(2,3)
101assert(x[1] == 2 and x[2] == 3 and x[3] == nil)
102
103
104f = 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
111assert(f("a", "b", nil, {}, assert))
112assert(f())
113
114a = {select(3, table.unpack{10,20,30,40})}
115assert(#a == 2 and a[1] == 30 and a[2] == 40)
116a = {select(1)}
117assert(next(a) == nil)
118a = {select(-1, 3, 5, 7)}
119assert(a[1] == 7 and a[2] == nil)
120a = {select(-2, 3, 5, 7)}
121assert(a[1] == 5 and a[2] == 7 and a[3] == nil)
122pcall(select, 10000)
123pcall(select, -10000)
124
125
126-- bug in 5.2.2
127
128function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10,
129p11, p12, p13, p14, p15, p16, p17, p18, p19, p20,
130p21, p22, p23, p24, p25, p26, p27, p28, p29, p30,
131p31, p32, p33, p34, p35, p36, p37, p38, p39, p40,
132p41, 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
135end
136
137-- assertion fail here
138f()
139
140
141print('OK')
142