lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/vararg.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | print('testing vararg') |
| 5 | |
| 6 | local 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 |
| 13 | end |
| 14 | |
| 15 | local 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 |
| 21 | end |
| 22 | |
| 23 | local function vararg (... t) return t end |
| 24 | |
| 25 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
| 26 | |
| 27 | assert(f() == 0) |
| 28 | assert(f({1,2,3}, 1, 2, 3) == 3) |
| 29 | assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5) |
| 30 | |
| 31 | assert(vararg().n == 0) |
| 32 | assert(vararg(nil, nil).n == 2) |
| 33 | |
| 34 | assert(c12(1,2)==55) |
| 35 | local a,b = assert(call(c12, {1,2})) |
| 36 | assert(a == 55 and b == 2) |
| 37 | a = call(c12, {1,2;n=2}) |
| 38 | assert(a == 55 and b == 2) |
| 39 | a = call(c12, {1,2;n=1}) |
| 40 | assert(not a) |
| 41 | assert(c12(1,2,3) == false) |
| 42 | local a = vararg(call(next, {_G,nil;n=2})) |
| 43 | local b,c = next(_G) |
| 44 | assert(a[1] == b and a[2] == c and a.n == 2) |
| 45 | a = vararg(call(call, {c12, {1,2}})) |
| 46 | assert(a.n == 2 and a[1] == 55 and a[2] == 2) |
| 47 | a = call(print, {'+'}) |
| 48 | assert(a == nil) |
| 49 | |
| 50 | local t = {1, 10} |
| 51 | function t:f (...) local arg = {...}; return self[...]+#arg end |
| 52 | assert(t:f(1,4) == 3 and t:f(2) == 11) |
| 53 | print('+') |
| 54 | |
| 55 | local lim = 20 |
| 56 | local i, a = 1, {} |
| 57 | while i <= lim do a[i] = i+0.3; i=i+1 end |
| 58 | |
| 59 | function 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]) |
| 63 | end |
| 64 | |
| 65 | local function g (a,b,c) |
| 66 | assert(a == 1.3 and b == 2.3 and c == 3.3) |
| 67 | end |
| 68 | |
| 69 | call(f, a) |
| 70 | call(g, a) |
| 71 | |
| 72 | a = {} |
| 73 | i = 1 |
| 74 | while i <= lim do a[i] = i; i=i+1 end |
| 75 | assert(call(math.max, a) == lim) |
| 76 | |
| 77 | print("+") |
| 78 | |
| 79 | |
| 80 | -- new-style varargs |
| 81 | |
| 82 | local function oneless (a, ...) return ... end |
| 83 | |
| 84 | function 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 |
| 95 | end |
| 96 | |
| 97 | a,b,c,d,e = assert(f(10,5,4,3,2,1)) |
| 98 | assert(a==5 and b==4 and c==3 and d==2 and e==1) |
| 99 | |
| 100 | a,b,c,d,e = f(4) |
| 101 | assert(a==nil and b==nil and c==nil and d==nil and e==nil) |
| 102 | |
| 103 | |
| 104 | do -- 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) |
| 134 | end |
| 135 | |
| 136 | -- varargs for main chunks |
| 137 | local f = assert(load[[ return {...} ]]) |
| 138 | local x = f(2,3) |
| 139 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) |
| 140 | |
| 141 | |
| 142 | f = 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 | |
| 149 | assert(f("a", "b", nil, {}, assert)) |
| 150 | assert(f()) |
| 151 | |
| 152 | a = {select(3, table.unpack{10,20,30,40})} |
| 153 | assert(#a == 2 and a[1] == 30 and a[2] == 40) |
| 154 | a = {select(1)} |
| 155 | assert(next(a) == nil) |
| 156 | a = {select(-1, 3, 5, 7)} |
| 157 | assert(a[1] == 7 and a[2] == undef) |
| 158 | a = {select(-2, 3, 5, 7)} |
| 159 | assert(a[1] == 5 and a[2] == 7 and a[3] == undef) |
| 160 | pcall(select, 10000) |
| 161 | pcall(select, -10000) |
| 162 | |
| 163 | |
| 164 | -- bug in 5.2.2 |
| 165 | |
| 166 | function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, |
| 167 | p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, |
| 168 | p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, |
| 169 | p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, |
| 170 | p41, 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 |
| 173 | end |
| 174 | |
| 175 | -- assertion fail here |
| 176 | f() |
| 177 | |
| 178 | -- missing arguments in tail call |
| 179 | do |
| 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) |
| 184 | end |
| 185 | |
| 186 | |
| 187 | do -- 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") |
| 197 | end |
| 198 | |
| 199 | do -- 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'")) |
| 209 | end |
| 210 | |
| 211 | |
| 212 | do -- _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) |
| 231 | end |
| 232 | |
| 233 | |
| 234 | do -- 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) |
| 257 | end |
| 258 | |
| 259 | print('OK') |
| 260 |