interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / vararg.lua

260 lines · 6216 bytes Raw Blame History
1-- $Id: testes/vararg.lua $
2-- See Copyright Notice in file lua.h
3
4print('testing vararg')
5
6local function f (a, ...t)
7 local x = {n = select('#', ...), ...}
8 assert(x.n == t.n)
9 for i = 1, x.n do
10 assert(a[i] == x[i] and x[i] == t[i])
11 end
12 return x.n
13end
14
15local function c12 (...)
16 assert(arg == _G.arg) -- no local 'arg'
17 local x = {...}; x.n = #x
18 local res = (x.n==2 and x[1] == 1 and x[2] == 2)
19 if res then res = 55 end
20 return res, 2
21end
22
23local function vararg (... t) return t end
24
25local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
26
27assert(f() == 0)
28assert(f({1,2,3}, 1, 2, 3) == 3)
29assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5)
30
31assert(vararg().n == 0)
32assert(vararg(nil, nil).n == 2)
33
34assert(c12(1,2)==55)
35local a,b = assert(call(c12, {1,2}))
36assert(a == 55 and b == 2)
37a = call(c12, {1,2;n=2})
38assert(a == 55 and b == 2)
39a = call(c12, {1,2;n=1})
40assert(not a)
41assert(c12(1,2,3) == false)
42local a = vararg(call(next, {_G,nil;n=2}))
43local b,c = next(_G)
44assert(a[1] == b and a[2] == c and a.n == 2)
45a = vararg(call(call, {c12, {1,2}}))
46assert(a.n == 2 and a[1] == 55 and a[2] == 2)
47a = call(print, {'+'})
48assert(a == nil)
49
50local t = {1, 10}
51function t:f (...) local arg = {...}; return self[...]+#arg end
52assert(t:f(1,4) == 3 and t:f(2) == 11)
53print('+')
54
55local lim = 20
56local i, a = 1, {}
57while i <= lim do a[i] = i+0.3; i=i+1 end
58
59function f(a, b, c, d, ...)
60 local more = {...}
61 assert(a == 1.3 and more[1] == 5.3 and
62 more[lim-4] == lim+0.3 and not more[lim-3])
63end
64
65local function g (a,b,c)
66 assert(a == 1.3 and b == 2.3 and c == 3.3)
67end
68
69call(f, a)
70call(g, a)
71
72a = {}
73i = 1
74while i <= lim do a[i] = i; i=i+1 end
75assert(call(math.max, a) == lim)
76
77print("+")
78
79
80-- new-style varargs
81
82local function oneless (a, ...) return ... end
83
84function f (n, a, ...)
85 local b
86 assert(arg == _G.arg) -- no local 'arg'
87 if n == 0 then
88 local b, c, d = ...
89 return a, b, c, d, oneless(oneless(oneless(...)))
90 else
91 n, b, a = n-1, ..., a
92 assert(b == ...)
93 return f(n, a, ...)
94 end
95end
96
97a,b,c,d,e = assert(f(10,5,4,3,2,1))
98assert(a==5 and b==4 and c==3 and d==2 and e==1)
99
100a,b,c,d,e = f(4)
101assert(a==nil and b==nil and c==nil and d==nil and e==nil)
102
103
104do -- vararg expressions using unpack
105 local function aux (a, v, ...t)
106 for k, val in pairs(v) do t[k] = val end
107 return ...
108 end
109
110 local t = table.pack(aux(10, {11, [5] = 24}, 1, 2, 3, nil, 4))
111 assert(t.n == 5 and t[1] == 11 and t[2] == 2 and t[3] == 3
112 and t[4] == nil and t[5] == 24)
113
114 local t = table.pack(aux(nil, {1, [20] = "a", [30] = "b", n = 30}))
115 assert(t.n == 30 and t[1] == 1 and t[20] == "a" and t[30] == "b")
116 -- table has only those four elements
117 assert(next(t, next(t, next(t, next(t, next(t, nil))))) == nil)
118
119 local a, b, c, d = aux(nil, {}, 10, 20, 30)
120 assert(a == 10 and b == 20 and c == 30 and d == nil)
121
122 local function aux (a, b, n, ...t) t.n = n; return b, ... end
123 local t = table.pack(aux(10, 1, 10000))
124 assert(t.n == 10001 and t[1] == 1 and #t == 1)
125
126 local function checkerr (emsg, f, ...)
127 local st, msg = pcall(f, ...)
128 assert(not st and string.find(msg, emsg))
129 end
130 checkerr("no proper 'n'", aux, 1, 1, -1)
131 checkerr("no proper 'n'", aux, 1, 1, math.maxinteger)
132 checkerr("no proper 'n'", aux, 1, 1, math.mininteger)
133 checkerr("no proper 'n'", aux, 1, 1, 1.0)
134end
135
136-- varargs for main chunks
137local f = assert(load[[ return {...} ]])
138local x = f(2,3)
139assert(x[1] == 2 and x[2] == 3 and x[3] == undef)
140
141
142f = load[[
143 local x = {...}
144 for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end
145 assert(x[select('#', ...)+1] == undef)
146 return true
147]]
148
149assert(f("a", "b", nil, {}, assert))
150assert(f())
151
152a = {select(3, table.unpack{10,20,30,40})}
153assert(#a == 2 and a[1] == 30 and a[2] == 40)
154a = {select(1)}
155assert(next(a) == nil)
156a = {select(-1, 3, 5, 7)}
157assert(a[1] == 7 and a[2] == undef)
158a = {select(-2, 3, 5, 7)}
159assert(a[1] == 5 and a[2] == 7 and a[3] == undef)
160pcall(select, 10000)
161pcall(select, -10000)
162
163
164-- bug in 5.2.2
165
166function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10,
167p11, p12, p13, p14, p15, p16, p17, p18, p19, p20,
168p21, p22, p23, p24, p25, p26, p27, p28, p29, p30,
169p31, p32, p33, p34, p35, p36, p37, p38, p39, p40,
170p41, p42, p43, p44, p45, p46, p48, p49, p50, ...)
171 local a1,a2,a3,a4,a5,a6,a7
172 local a8,a9,a10,a11,a12,a13,a14
173end
174
175-- assertion fail here
176f()
177
178-- missing arguments in tail call
179do
180 local function f(a,b,c) return c, b end
181 local function g() return f(1,2) end
182 local a, b = g()
183 assert(a == nil and b == 2)
184end
185
186
187do -- vararg parameter used in nested functions
188 local function foo (...tab1)
189 return function (...tab2)
190 return {tab1, tab2}
191 end
192 end
193 local f = foo(10, 20, 30)
194 local t = f("a", "b")
195 assert(t[1].n == 3 and t[1][1] == 10)
196 assert(t[2].n == 2 and t[2][1] == "a")
197end
198
199do -- vararg parameter is read-only
200 local st, msg = load("return function (... t) t = 10 end")
201 assert(string.find(msg, "const variable 't'"))
202
203 local st, msg = load[[
204 local function foo (...extra)
205 return function (...) extra = nil end
206 end
207 ]]
208 assert(string.find(msg, "const variable 'extra'"))
209end
210
211
212do -- _ENV as vararg parameter
213 local st, msg = load[[
214 local function aux (... _ENV)
215 global <const> a
216 a = 10
217 end ]]
218 assert(string.find(msg, "const variable 'a'"))
219
220 local function aux (..._ENV)
221 global a; a = 10
222 return a
223 end
224 assert(aux() == 10)
225
226 local function aux (... _ENV)
227 global a = 10
228 return a
229 end
230 assert(aux() == 10)
231end
232
233
234do -- access to vararg parameter
235 local function notab (keys, t, ...v)
236 for _, k in pairs(keys) do
237 assert(t[k] == v[k])
238 end
239 assert(t.n == v.n)
240 return ...
241 end
242
243 local t = table.pack(10, 20, 30)
244 local keys = {-1, 0, 1, t.n, t.n + 1, 1.0, 1.1, "n", print, "k", "1"}
245 notab(keys, t, 10, 20, 30) -- ensure stack space
246 local m = collectgarbage"count"
247 notab(keys, t, 10, 20, 30)
248 -- 'notab' does not create any table/object
249 assert(m == collectgarbage"count")
250
251 -- writing to the vararg table
252 local function foo (...t)
253 t[1] = t[1] + 10
254 return t[1]
255 end
256 assert(foo(10, 30) == 20)
257end
258
259print('OK')
260