diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:38:20 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:38:20 -0300 |
| commit | b5ab31a475ccf5f96c2ffb34fccc1d6592913794 (patch) | |
| tree | ef6d2b1844ca672dc81ab4411e37abbef2b7c48f /testes/vararg.lua | |
| parent | 140cdcced5e28bde7a89e88fcae428f318565f1d (diff) | |
| parent | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (diff) | |
| download | lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.gz lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.bz2 lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.zip | |
Merge branch 'master' into nextversion
Diffstat (limited to 'testes/vararg.lua')
| -rw-r--r-- | testes/vararg.lua | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua index 44848d25..1b025102 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua | |||
| @@ -3,13 +3,13 @@ | |||
| 3 | 3 | ||
| 4 | print('testing vararg') | 4 | print('testing vararg') |
| 5 | 5 | ||
| 6 | function f(a, ...) | 6 | local function f (a, ...) |
| 7 | local x = {n = select('#', ...), ...} | 7 | local x = {n = select('#', ...), ...} |
| 8 | for i = 1, x.n do assert(a[i] == x[i]) end | 8 | for i = 1, x.n do assert(a[i] == x[i]) end |
| 9 | return x.n | 9 | return x.n |
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | function c12 (...) | 12 | local function c12 (...) |
| 13 | assert(arg == _G.arg) -- no local 'arg' | 13 | assert(arg == _G.arg) -- no local 'arg' |
| 14 | local x = {...}; x.n = #x | 14 | local x = {...}; x.n = #x |
| 15 | local res = (x.n==2 and x[1] == 1 and x[2] == 2) | 15 | local res = (x.n==2 and x[1] == 1 and x[2] == 2) |
| @@ -17,7 +17,7 @@ function c12 (...) | |||
| 17 | return res, 2 | 17 | return res, 2 |
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | function vararg (...) return {n = select('#', ...), ...} end | 20 | local function vararg (...) return {n = select('#', ...), ...} end |
| 21 | 21 | ||
| 22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end | 22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
| 23 | 23 | ||
| @@ -29,7 +29,7 @@ assert(vararg().n == 0) | |||
| 29 | assert(vararg(nil, nil).n == 2) | 29 | assert(vararg(nil, nil).n == 2) |
| 30 | 30 | ||
| 31 | assert(c12(1,2)==55) | 31 | assert(c12(1,2)==55) |
| 32 | a,b = assert(call(c12, {1,2})) | 32 | local a,b = assert(call(c12, {1,2})) |
| 33 | assert(a == 55 and b == 2) | 33 | assert(a == 55 and b == 2) |
| 34 | a = call(c12, {1,2;n=2}) | 34 | a = call(c12, {1,2;n=2}) |
| 35 | assert(a == 55 and b == 2) | 35 | assert(a == 55 and b == 2) |
| @@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end | |||
| 49 | assert(t:f(1,4) == 3 and t:f(2) == 11) | 49 | assert(t:f(1,4) == 3 and t:f(2) == 11) |
| 50 | print('+') | 50 | print('+') |
| 51 | 51 | ||
| 52 | lim = 20 | 52 | local lim = 20 |
| 53 | local i, a = 1, {} | 53 | local i, a = 1, {} |
| 54 | while i <= lim do a[i] = i+0.3; i=i+1 end | 54 | while i <= lim do a[i] = i+0.3; i=i+1 end |
| 55 | 55 | ||
| @@ -59,7 +59,7 @@ function f(a, b, c, d, ...) | |||
| 59 | more[lim-4] == lim+0.3 and not more[lim-3]) | 59 | more[lim-4] == lim+0.3 and not more[lim-3]) |
| 60 | end | 60 | end |
| 61 | 61 | ||
| 62 | function g(a,b,c) | 62 | local function g (a,b,c) |
| 63 | assert(a == 1.3 and b == 2.3 and c == 3.3) | 63 | assert(a == 1.3 and b == 2.3 and c == 3.3) |
| 64 | end | 64 | end |
| 65 | 65 | ||
| @@ -76,7 +76,7 @@ print("+") | |||
| 76 | 76 | ||
| 77 | -- new-style varargs | 77 | -- new-style varargs |
| 78 | 78 | ||
| 79 | function oneless (a, ...) return ... end | 79 | local function oneless (a, ...) return ... end |
| 80 | 80 | ||
| 81 | function f (n, a, ...) | 81 | function f (n, a, ...) |
| 82 | local b | 82 | local b |
| @@ -99,8 +99,8 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil) | |||
| 99 | 99 | ||
| 100 | 100 | ||
| 101 | -- varargs for main chunks | 101 | -- varargs for main chunks |
| 102 | f = load[[ return {...} ]] | 102 | local f = load[[ return {...} ]] |
| 103 | x = f(2,3) | 103 | local x = f(2,3) |
| 104 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) | 104 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) |
| 105 | 105 | ||
| 106 | 106 | ||
