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