github.com/hootrhino/gopher-lua@v1.0.3/_lua5.1-tests/vararg.lua (about)

     1  print('testing vararg')
     2  
     3  _G.arg = nil
     4  
     5  function 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
    10  end
    11  
    12  function 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
    18  end
    19  
    20  function vararg (...) return arg end
    21  
    22  local call = function (f, args) return f(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(c12(1,2)==55)
    29  a,b = assert(call(c12, {1,2}))
    30  assert(a == 55 and b == 2)
    31  a = call(c12, {1,2;n=2})
    32  assert(a == 55 and b == 2)
    33  a = call(c12, {1,2;n=1})
    34  assert(not a)
    35  assert(c12(1,2,3) == false)
    36  --[[
    37  local a = vararg(call(next, {_G,nil;n=2}))
    38  local b,c = next(_G)
    39  assert(a[1] == b and a[2] == c and a.n == 2)
    40  a = vararg(call(call, {c12, {1,2}}))
    41  assert(a.n == 2 and a[1] == 55 and a[2] == 2)
    42  a = call(print, {'+'})
    43  assert(a == nil)
    44  --]]
    45  
    46  local t = {1, 10}
    47  function t:f (...) return self[arg[1]]+arg.n end
    48  assert(t:f(1,4) == 3 and t:f(2) == 11)
    49  print('+')
    50  
    51  lim = 20
    52  local i, a = 1, {}
    53  while i <= lim do a[i] = i+0.3; i=i+1 end
    54  
    55  function 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])
    59  end
    60  
    61  function g(a,b,c)
    62    assert(a == 1.3 and b == 2.3 and c == 3.3)
    63  end
    64  
    65  call(f, a)
    66  call(g, a)
    67  
    68  a = {}
    69  i = 1
    70  while i <= lim do a[i] = i; i=i+1 end
    71  assert(call(math.max, a) == lim)
    72  
    73  print("+")
    74  
    75  
    76  -- new-style varargs
    77  
    78  function oneless (a, ...) return ... end
    79  
    80  function 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
    91  end
    92  
    93  a,b,c,d,e = assert(f(10,5,4,3,2,1))
    94  assert(a==5 and b==4 and c==3 and d==2 and e==1)
    95  
    96  a,b,c,d,e = f(4)
    97  assert(a==nil and b==nil and c==nil and d==nil and e==nil)
    98  
    99  
   100  -- varargs for main chunks
   101  f = loadstring[[ return {...} ]]
   102  x = f(2,3)
   103  assert(x[1] == 2 and x[2] == 3 and x[3] == nil)
   104  
   105  
   106  f = 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  
   113  assert(f("a", "b", nil, {}, assert))
   114  assert(f())
   115  
   116  a = {select(3, unpack{10,20,30,40})}
   117  assert(table.getn(a) == 2 and a[1] == 30 and a[2] == 40)
   118  a = {select(1)}
   119  assert(next(a) == nil)
   120  a = {select(-1, 3, 5, 7)}
   121  assert(a[1] == 7 and a[2] == nil)
   122  a = {select(-2, 3, 5, 7)}
   123  assert(a[1] == 5 and a[2] == 7 and a[3] == nil)
   124  pcall(select, 10000)
   125  pcall(select, -10000)
   126  
   127  print('OK')
   128