diff options
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/coroutine.lua | 4 | ||||
| -rw-r--r-- | testes/db.lua | 9 | ||||
| -rw-r--r-- | testes/vararg.lua | 33 |
3 files changed, 41 insertions, 5 deletions
diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 4881d964..ba394e0c 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua | |||
| @@ -702,7 +702,9 @@ else | |||
| 702 | assert(t.currentline == t.linedefined + 2) | 702 | assert(t.currentline == t.linedefined + 2) |
| 703 | assert(not debug.getinfo(c, 1)) -- no other level | 703 | assert(not debug.getinfo(c, 1)) -- no other level |
| 704 | assert(coroutine.resume(c)) -- run next line | 704 | assert(coroutine.resume(c)) -- run next line |
| 705 | local n,v = debug.getlocal(c, 0, 2) -- check next local | 705 | local n,v = debug.getlocal(c, 0, 2) -- check vararg table |
| 706 | assert(n == "(vararg table)" and v == nil) | ||
| 707 | local n,v = debug.getlocal(c, 0, 3) -- check next local | ||
| 706 | assert(n == "b" and v == 10) | 708 | assert(n == "b" and v == 10) |
| 707 | v = {coroutine.resume(c)} -- finish coroutine | 709 | v = {coroutine.resume(c)} -- finish coroutine |
| 708 | assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) | 710 | assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) |
diff --git a/testes/db.lua b/testes/db.lua index 0f174f17..4220b68b 100644 --- a/testes/db.lua +++ b/testes/db.lua | |||
| @@ -356,8 +356,8 @@ function f(a,b) | |||
| 356 | global assert, g, string | 356 | global assert, g, string |
| 357 | local _, y = debug.getlocal(1, 2) | 357 | local _, y = debug.getlocal(1, 2) |
| 358 | assert(x == a and y == b) | 358 | assert(x == a and y == b) |
| 359 | assert(debug.setlocal(2, 3, "pera") == "AA".."AA") | 359 | assert(debug.setlocal(2, 4, "pera") == "AA".."AA") |
| 360 | assert(debug.setlocal(2, 4, "manga") == "B") | 360 | assert(debug.setlocal(2, 5, "manga") == "B") |
| 361 | x = debug.getinfo(2) | 361 | x = debug.getinfo(2) |
| 362 | assert(x.func == g and x.what == "Lua" and x.name == 'g' and | 362 | assert(x.func == g and x.what == "Lua" and x.name == 'g' and |
| 363 | x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) | 363 | x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) |
| @@ -392,7 +392,7 @@ function g (...) | |||
| 392 | global * | 392 | global * |
| 393 | local B = 13 | 393 | local B = 13 |
| 394 | global<const> assert | 394 | global<const> assert |
| 395 | local x,y = debug.getlocal(1,5) | 395 | local x,y = debug.getlocal(1,6) |
| 396 | assert(x == 'B' and y == 13) | 396 | assert(x == 'B' and y == 13) |
| 397 | end | 397 | end |
| 398 | end | 398 | end |
| @@ -458,7 +458,8 @@ local function collectlocals (level) | |||
| 458 | local tab = {} | 458 | local tab = {} |
| 459 | for i = 1, math.huge do | 459 | for i = 1, math.huge do |
| 460 | local n, v = debug.getlocal(level + 1, i) | 460 | local n, v = debug.getlocal(level + 1, i) |
| 461 | if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then | 461 | if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or |
| 462 | n == "(vararg table)") then | ||
| 462 | break -- consider only real variables | 463 | break -- consider only real variables |
| 463 | end | 464 | end |
| 464 | tab[n] = v | 465 | tab[n] = v |
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) |
