vararg.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. print('testing vararg')
  2. _G.arg = nil
  3. function f(a, ...)
  4. local arg = {n = select('#', ...), ...}
  5. for i=1,arg.n do assert(a[i]==arg[i]) end
  6. return arg.n
  7. end
  8. function c12 (...)
  9. assert(arg == nil)
  10. local x = {...}; x.n = #x
  11. local res = (x.n==2 and x[1] == 1 and x[2] == 2)
  12. if res then res = 55 end
  13. return res, 2
  14. end
  15. function vararg (...) return {n = select('#', ...), ...} end
  16. local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
  17. assert(f() == 0)
  18. assert(f({1,2,3}, 1, 2, 3) == 3)
  19. assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5)
  20. assert(c12(1,2)==55)
  21. a,b = assert(call(c12, {1,2}))
  22. assert(a == 55 and b == 2)
  23. a = call(c12, {1,2;n=2})
  24. assert(a == 55 and b == 2)
  25. a = call(c12, {1,2;n=1})
  26. assert(not a)
  27. assert(c12(1,2,3) == false)
  28. local a = vararg(call(next, {_G,nil;n=2}))
  29. local b,c = next(_G)
  30. assert(a[1] == b and a[2] == c and a.n == 2)
  31. a = vararg(call(call, {c12, {1,2}}))
  32. assert(a.n == 2 and a[1] == 55 and a[2] == 2)
  33. a = call(print, {'+'})
  34. assert(a == nil)
  35. local t = {1, 10}
  36. function t:f (...) local arg = {...}; return self[...]+#arg end
  37. assert(t:f(1,4) == 3 and t:f(2) == 11)
  38. print('+')
  39. lim = 20
  40. local i, a = 1, {}
  41. while i <= lim do a[i] = i+0.3; i=i+1 end
  42. function f(a, b, c, d, ...)
  43. local more = {...}
  44. assert(a == 1.3 and more[1] == 5.3 and
  45. more[lim-4] == lim+0.3 and not more[lim-3])
  46. end
  47. function g(a,b,c)
  48. assert(a == 1.3 and b == 2.3 and c == 3.3)
  49. end
  50. call(f, a)
  51. call(g, a)
  52. a = {}
  53. i = 1
  54. while i <= lim do a[i] = i; i=i+1 end
  55. assert(call(math.max, a) == lim)
  56. print("+")
  57. -- new-style varargs
  58. function oneless (a, ...) return ... end
  59. function f (n, a, ...)
  60. local b
  61. assert(arg == nil)
  62. if n == 0 then
  63. local b, c, d = ...
  64. return a, b, c, d, oneless(oneless(oneless(...)))
  65. else
  66. n, b, a = n-1, ..., a
  67. assert(b == ...)
  68. return f(n, a, ...)
  69. end
  70. end
  71. a,b,c,d,e = assert(f(10,5,4,3,2,1))
  72. assert(a==5 and b==4 and c==3 and d==2 and e==1)
  73. a,b,c,d,e = f(4)
  74. assert(a==nil and b==nil and c==nil and d==nil and e==nil)
  75. -- varargs for main chunks
  76. f = load[[ return {...} ]]
  77. x = f(2,3)
  78. assert(x[1] == 2 and x[2] == 3 and x[3] == nil)
  79. f = load[[
  80. local x = {...}
  81. for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end
  82. assert(x[select('#', ...)+1] == nil)
  83. return true
  84. ]]
  85. assert(f("a", "b", nil, {}, assert))
  86. assert(f())
  87. a = {select(3, table.unpack{10,20,30,40})}
  88. assert(#a == 2 and a[1] == 30 and a[2] == 40)
  89. a = {select(1)}
  90. assert(next(a) == nil)
  91. a = {select(-1, 3, 5, 7)}
  92. assert(a[1] == 7 and a[2] == nil)
  93. a = {select(-2, 3, 5, 7)}
  94. assert(a[1] == 5 and a[2] == 7 and a[3] == nil)
  95. pcall(select, 10000)
  96. pcall(select, -10000)
  97. print('OK')