1print('testing vararg')
2
3_G.arg = nil
4
5function f(a, ...)
6  assert(type(arg) == 'table')
7  assert(type(arg.n) == 'number')
8  for i=1,arg.n do assert(a[i]==arg[i]) end
9  return arg.n
10end
11
12function c12 (...)
13  assert(arg == nil)
14  local x = {...}; x.n = table.getn(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 arg end
21
22local call = function (f, args) return f(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)
36--[[
37local a = vararg(call(next, {_G,nil;n=2}))
38local b,c = next(_G)
39assert(a[1] == b and a[2] == c and a.n == 2)
40a = vararg(call(call, {c12, {1,2}}))
41assert(a.n == 2 and a[1] == 55 and a[2] == 2)
42a = call(print, {'+'})
43assert(a == nil)
44--]]
45
46local t = {1, 10}
47function t:f (...) return self[arg[1]]+arg.n end
48assert(t:f(1,4) == 3 and t:f(2) == 11)
49print('+')
50
51lim = 20
52local i, a = 1, {}
53while i <= lim do a[i] = i+0.3; i=i+1 end
54
55function f(a, b, c, d, ...)
56  local more = {...}
57  assert(a == 1.3 and more[1] == 5.3 and
58         more[lim-4] == lim+0.3 and not more[lim-3])
59end
60
61function g(a,b,c)
62  assert(a == 1.3 and b == 2.3 and c == 3.3)
63end
64
65call(f, a)
66call(g, a)
67
68a = {}
69i = 1
70while i <= lim do a[i] = i; i=i+1 end
71assert(call(math.max, a) == lim)
72
73print("+")
74
75
76-- new-style varargs
77
78function oneless (a, ...) return ... end
79
80function f (n, a, ...)
81  local b
82  assert(arg == nil)
83  if n == 0 then
84    local b, c, d = ...
85    return a, b, c, d, oneless(oneless(oneless(...)))
86  else
87    n, b, a = n-1, ..., a
88    assert(b == ...)
89    return f(n, a, ...)
90  end
91end
92
93a,b,c,d,e = assert(f(10,5,4,3,2,1))
94assert(a==5 and b==4 and c==3 and d==2 and e==1)
95
96a,b,c,d,e = f(4)
97assert(a==nil and b==nil and c==nil and d==nil and e==nil)
98
99
100-- varargs for main chunks
101f = loadstring[[ return {...} ]]
102x = f(2,3)
103assert(x[1] == 2 and x[2] == 3 and x[3] == nil)
104
105
106f = loadstring[[
107  local x = {...}
108  for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end
109  assert(x[select('#', ...)+1] == nil)
110  return true
111]]
112
113assert(f("a", "b", nil, {}, assert))
114assert(f())
115
116a = {select(3, unpack{10,20,30,40})}
117assert(table.getn(a) == 2 and a[1] == 30 and a[2] == 40)
118a = {select(1)}
119assert(next(a) == nil)
120a = {select(-1, 3, 5, 7)}
121assert(a[1] == 7 and a[2] == nil)
122a = {select(-2, 3, 5, 7)}
123assert(a[1] == 5 and a[2] == 7 and a[3] == nil)
124pcall(select, 10000)
125pcall(select, -10000)
126
127print('OK')
128
129