diff options
Diffstat (limited to 'testes/vararg.lua')
| -rw-r--r-- | testes/vararg.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua index a01598ff..043fa7d4 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua | |||
| @@ -101,6 +101,38 @@ a,b,c,d,e = f(4) | |||
| 101 | assert(a==nil and b==nil and c==nil and d==nil and e==nil) | 101 | assert(a==nil and b==nil and c==nil and d==nil and e==nil) |
| 102 | 102 | ||
| 103 | 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 | |||
| 104 | -- varargs for main chunks | 136 | -- varargs for main chunks |
| 105 | local f = assert(load[[ return {...} ]]) | 137 | local f = assert(load[[ return {...} ]]) |
| 106 | local x = f(2,3) | 138 | local x = f(2,3) |
| @@ -205,6 +237,7 @@ do -- access to vararg parameter | |||
| 205 | assert(t[k] == v[k]) | 237 | assert(t[k] == v[k]) |
| 206 | end | 238 | end |
| 207 | assert(t.n == v.n) | 239 | assert(t.n == v.n) |
| 240 | return ... | ||
| 208 | end | 241 | end |
| 209 | 242 | ||
| 210 | local t = table.pack(10, 20, 30) | 243 | local t = table.pack(10, 20, 30) |
