1-- $Id: testes/nextvar.lua $
  2-- See Copyright Notice in file all.lua
  3
  4print('testing tables, next, and for')
  5
  6local function checkerror (msg, f, ...)
  7  local s, err = pcall(f, ...)
  8  assert(not s and string.find(err, msg))
  9end
 10
 11
 12local function check (t, na, nh)
 13  if not T then return end
 14  local a, h = T.querytab(t)
 15  if a ~= na or h ~= nh then
 16    print(na, nh, a, h)
 17    assert(nil)
 18  end
 19end
 20
 21
 22local a = {}
 23
 24-- make sure table has lots of space in hash part
 25for i=1,100 do a[i.."+"] = true end
 26for i=1,100 do a[i.."+"] = undef end
 27-- fill hash part with numeric indices testing size operator
 28for i=1,100 do
 29  a[i] = true
 30  assert(#a == i)
 31end
 32
 33
 34do   -- rehash moving elements from array to hash
 35  local a = {}
 36  for i = 1, 100 do a[i] = i end
 37  check(a, 128, 0)
 38
 39  for i = 5, 95 do a[i] = nil end
 40  check(a, 128, 0)
 41
 42  a.x = 1     -- force a re-hash
 43  check(a, 4, 8)
 44
 45  for i = 1, 4 do assert(a[i] == i) end
 46  for i = 5, 95 do assert(a[i] == nil) end
 47  for i = 96, 100 do assert(a[i] == i) end
 48  assert(a.x == 1)
 49end
 50
 51
 52-- testing ipairs
 53local x = 0
 54for k,v in ipairs{10,20,30;x=12} do
 55  x = x + 1
 56  assert(k == x and v == x * 10)
 57end
 58
 59for _ in ipairs{x=12, y=24} do assert(nil) end
 60
 61-- test for 'false' x ipair
 62x = false
 63local i = 0
 64for k,v in ipairs{true,false,true,false} do
 65  i = i + 1
 66  x = not x
 67  assert(x == v)
 68end
 69assert(i == 4)
 70
 71-- iterator function is always the same
 72assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
 73
 74
 75do   -- overflow (must wrap-around)
 76  local f = ipairs{}
 77  local k, v = f({[math.mininteger] = 10}, math.maxinteger)
 78  assert(k == math.mininteger and v == 10)
 79  k, v = f({[math.mininteger] = 10}, k)
 80  assert(k == nil)
 81end
 82
 83if not T then
 84  (Message or print)
 85    ('\n >>> testC not active: skipping tests for table sizes <<<\n')
 86else --[
 87-- testing table sizes
 88
 89
 90local function mp2 (n)   -- minimum power of 2 >= n
 91  local mp = 2^math.ceil(math.log(n, 2))
 92  assert(n == 0 or (mp/2 < n and n <= mp))
 93  return mp
 94end
 95
 96
 97-- testing C library sizes
 98do
 99  local s = 0
100  for _ in pairs(math) do s = s + 1 end
101  check(math, 0, mp2(s))
102end
103
104
105-- testing constructor sizes
106local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17,
107  30, 31, 32, 33, 34, 254, 255, 256, 500, 1000}
108
109for _, sa in ipairs(sizes) do    -- 'sa' is size of the array part
110  local arr = {"return {"}
111  for i = 1, sa do arr[1 + i] = "1," end    -- build array part
112  for _, sh in ipairs(sizes) do    -- 'sh' is size of the hash part
113    for j = 1, sh do   -- build hash part
114      arr[1 + sa + j] = string.format('k%x=%d,', j, j)
115    end
116    arr[1 + sa + sh + 1] = "}"
117    local prog = table.concat(arr)
118    local f = assert(load(prog))
119    collectgarbage("stop")
120    f()    -- call once to ensure stack space
121    -- make sure table is not resized after being created
122    if sa == 0 or sh == 0 then
123      T.alloccount(2);  -- header + array or hash part
124    else
125      T.alloccount(3);  -- header + array part + hash part
126    end
127    local t = f()
128    T.alloccount();
129    collectgarbage("restart")
130    assert(#t == sa)
131    check(t, sa, mp2(sh))
132  end
133end
134
135
136-- tests with unknown number of elements
137local a = {}
138for i=1,sizes[#sizes] do a[i] = i end   -- build auxiliary table
139for k in ipairs(sizes) do
140  local t = {table.unpack(a,1,k)}
141  assert(#t == k)
142  check(t, k, 0)
143  t = {1,2,3,table.unpack(a,1,k)}
144  check(t, k+3, 0)
145  assert(#t == k + 3)
146end
147
148
149-- testing tables dynamically built
150local lim = 130
151local a = {}; a[2] = 1; check(a, 0, 1)
152a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
153a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
154a = {}
155for i = 1,lim do
156  a[i] = 1
157  assert(#a == i)
158  check(a, mp2(i), 0)
159end
160
161a = {}
162for i = 1,lim do
163  a['a'..i] = 1
164  assert(#a == 0)
165  check(a, 0, mp2(i))
166end
167
168a = {}
169for i=1,16 do a[i] = i end
170check(a, 16, 0)
171do
172  for i=1,11 do a[i] = undef end
173  for i=30,50 do a[i] = true; a[i] = undef end   -- force a rehash (?)
174  check(a, 0, 8)   -- 5 elements in the table
175  a[10] = 1
176  for i=30,50 do a[i] = true; a[i] = undef end   -- force a rehash (?)
177  check(a, 0, 8)   -- only 6 elements in the table
178  for i=1,14 do a[i] = true; a[i] = undef end
179  for i=18,50 do a[i] = true; a[i] = undef end   -- force a rehash (?)
180  check(a, 0, 4)   -- only 2 elements ([15] and [16])
181end
182
183-- reverse filling
184for i=1,lim do
185  local a = {}
186  for i=i,1,-1 do a[i] = i end   -- fill in reverse
187  check(a, mp2(i), 0)
188end
189
190-- size tests for vararg
191lim = 35
192local function foo (n, ...)
193  local arg = {...}
194  check(arg, n, 0)
195  assert(select('#', ...) == n)
196  arg[n+1] = true
197  check(arg, mp2(n+1), 0)
198  arg.x = true
199  check(arg, mp2(n+1), 1)
200end
201local a = {}
202for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
203
204
205-- Table length with limit smaller than maximum value at array
206local a = {}
207for i = 1,64 do a[i] = true end    -- make its array size 64
208for i = 1,64 do a[i] = nil end     -- erase all elements
209assert(T.querytab(a) == 64)    -- array part has 64 elements
210a[32] = true; a[48] = true;    -- binary search will find these ones
211a[51] = true                   -- binary search will miss this one
212assert(#a == 48)               -- this will set the limit
213assert(select(4, T.querytab(a)) == 48)  -- this is the limit now
214a[50] = true                   -- this will set a new limit
215assert(select(4, T.querytab(a)) == 50)  -- this is the limit now
216-- but the size is larger (and still inside the array part)
217assert(#a == 51)
218
219end  --]
220
221
222-- test size operation on tables with nils
223assert(#{} == 0)
224assert(#{nil} == 0)
225assert(#{nil, nil} == 0)
226assert(#{nil, nil, nil} == 0)
227assert(#{nil, nil, nil, nil} == 0)
228assert(#{1, 2, 3, nil, nil} == 3)
229print'+'
230
231
232local nofind = {}
233
234a,b,c = 1,2,3
235a,b,c = nil
236
237
238-- next uses always the same iteraction function
239assert(next{} == next{})
240
241local function find (name)
242  local n,v
243  while 1 do
244    n,v = next(_G, n)
245    if not n then return nofind end
246    assert(_G[n] ~= undef)
247    if n == name then return v end
248  end
249end
250
251local function find1 (name)
252  for n,v in pairs(_G) do
253    if n==name then return v end
254  end
255  return nil  -- not found
256end
257
258
259assert(print==find("print") and print == find1("print"))
260assert(_G["print"]==find("print"))
261assert(assert==find1("assert"))
262assert(nofind==find("return"))
263assert(not find1("return"))
264_G["ret" .. "urn"] = undef
265assert(nofind==find("return"))
266_G["xxx"] = 1
267assert(xxx==find("xxx"))
268
269-- invalid key to 'next'
270checkerror("invalid key", next, {10,20}, 3)
271
272-- both 'pairs' and 'ipairs' need an argument
273checkerror("bad argument", pairs)
274checkerror("bad argument", ipairs)
275
276print('+')
277
278a = {}
279for i=0,10000 do
280  if math.fmod(i,10) ~= 0 then
281    a['x'..i] = i
282  end
283end
284
285n = {n=0}
286for i,v in pairs(a) do
287  n.n = n.n+1
288  assert(i and v and a[i] == v)
289end
290assert(n.n == 9000)
291a = nil
292
293do   -- clear global table
294  local a = {}
295  for n,v in pairs(_G) do a[n]=v end
296  for n,v in pairs(a) do
297    if not package.loaded[n] and type(v) ~= "function" and
298       not string.find(n, "^[%u_]") then
299      _G[n] = undef
300    end
301    collectgarbage()
302  end
303end
304
305
306--
307
308local function checknext (a)
309  local b = {}
310  do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
311  for k,v in pairs(b) do assert(a[k] == v) end
312  for k,v in pairs(a) do assert(b[k] == v) end
313end
314
315checknext{1,x=1,y=2,z=3}
316checknext{1,2,x=1,y=2,z=3}
317checknext{1,2,3,x=1,y=2,z=3}
318checknext{1,2,3,4,x=1,y=2,z=3}
319checknext{1,2,3,4,5,x=1,y=2,z=3}
320
321assert(#{} == 0)
322assert(#{[-1] = 2} == 0)
323for i=0,40 do
324  local a = {}
325  for j=1,i do a[j]=j end
326  assert(#a == i)
327end
328
329-- 'maxn' is now deprecated, but it is easily defined in Lua
330function table.maxn (t)
331  local max = 0
332  for k in pairs(t) do
333    max = (type(k) == 'number') and math.max(max, k) or max
334  end
335  return max
336end
337
338assert(table.maxn{} == 0)
339assert(table.maxn{["1000"] = true} == 0)
340assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
341assert(table.maxn{[1000] = true} == 1000)
342assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
343
344table.maxn = nil
345
346-- int overflow
347a = {}
348for i=0,50 do a[2^i] = true end
349assert(a[#a])
350
351print('+')
352
353
354do    -- testing 'next' with all kinds of keys
355  local a = {
356    [1] = 1,                        -- integer
357    [1.1] = 2,                      -- float
358    ['x'] = 3,                      -- short string
359    [string.rep('x', 1000)] = 4,    -- long string
360    [print] = 5,                    -- C function
361    [checkerror] = 6,               -- Lua function
362    [coroutine.running()] = 7,      -- thread
363    [true] = 8,                     -- boolean
364    [io.stdin] = 9,                 -- userdata
365    [{}] = 10,                      -- table
366  }
367  local b = {}; for i = 1, 10 do b[i] = true end
368  for k, v in pairs(a) do
369    assert(b[v]); b[v] = undef
370  end
371  assert(next(b) == nil)        -- 'b' now is empty
372end
373
374
375-- erasing values
376local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
377           [100.3] = 4, [4] = 5}
378
379local n = 0
380for k, v in pairs( t ) do
381  n = n+1
382  assert(t[k] == v)
383  t[k] = undef
384  collectgarbage()
385  assert(t[k] == undef)
386end
387assert(n == 5)
388
389
390do
391  print("testing next x GC of deleted keys")
392  -- bug in 5.4.1
393  local co = coroutine.wrap(function (t)
394    for k, v in pairs(t) do
395        local k1 = next(t)    -- all previous keys were deleted
396        assert(k == k1)       -- current key is the first in the table
397        t[k] = nil
398        local expected = (type(k) == "table" and k[1] or
399                          type(k) == "function" and k() or
400                          string.sub(k, 1, 1))
401        assert(expected == v)
402        coroutine.yield(v)
403    end
404  end)
405  local t = {}
406  t[{1}] = 1    -- add several unanchored, collectable keys
407  t[{2}] = 2
408  t[string.rep("a", 50)] = "a"    -- long string
409  t[string.rep("b", 50)] = "b"
410  t[{3}] = 3
411  t[string.rep("c", 10)] = "c"    -- short string
412  t[function () return 10 end] = 10
413  local count = 7
414  while co(t) do
415    collectgarbage("collect")   -- collect dead keys
416    count = count - 1
417  end
418  assert(count == 0 and next(t) == nil)    -- traversed the whole table
419end
420
421
422local function test (a)
423  assert(not pcall(table.insert, a, 2, 20));
424  table.insert(a, 10); table.insert(a, 2, 20);
425  table.insert(a, 1, -1); table.insert(a, 40);
426  table.insert(a, #a+1, 50)
427  table.insert(a, 2, -2)
428  assert(a[2] ~= undef)
429  assert(a["2"] == undef)
430  assert(not pcall(table.insert, a, 0, 20));
431  assert(not pcall(table.insert, a, #a + 2, 20));
432  assert(table.remove(a,1) == -1)
433  assert(table.remove(a,1) == -2)
434  assert(table.remove(a,1) == 10)
435  assert(table.remove(a,1) == 20)
436  assert(table.remove(a,1) == 40)
437  assert(table.remove(a,1) == 50)
438  assert(table.remove(a,1) == nil)
439  assert(table.remove(a) == nil)
440  assert(table.remove(a, #a) == nil)
441end
442
443a = {n=0, [-7] = "ban"}
444test(a)
445assert(a.n == 0 and a[-7] == "ban")
446
447a = {[-7] = "ban"};
448test(a)
449assert(a.n == nil and #a == 0 and a[-7] == "ban")
450
451a = {[-1] = "ban"}
452test(a)
453assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
454
455a = {[0] = "ban"}
456assert(#a == 0 and table.remove(a) == "ban" and a[0] == undef)
457
458table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
459assert(table.remove(a) == 10)
460assert(table.remove(a) == 20)
461assert(table.remove(a) == -1)
462assert(table.remove(a) == nil)
463
464a = {'c', 'd'}
465table.insert(a, 3, 'a')
466table.insert(a, 'b')
467assert(table.remove(a, 1) == 'c')
468assert(table.remove(a, 1) == 'd')
469assert(table.remove(a, 1) == 'a')
470assert(table.remove(a, 1) == 'b')
471assert(table.remove(a, 1) == nil)
472assert(#a == 0 and a.n == nil)
473
474a = {10,20,30,40}
475assert(table.remove(a, #a + 1) == nil)
476assert(not pcall(table.remove, a, 0))
477assert(a[#a] == 40)
478assert(table.remove(a, #a) == 40)
479assert(a[#a] == 30)
480assert(table.remove(a, 2) == 20)
481assert(a[#a] == 30 and #a == 2)
482
483do   -- testing table library with metamethods
484  local function test (proxy, t)
485    for i = 1, 10 do
486      table.insert(proxy, 1, i)
487    end
488    assert(#proxy == 10 and #t == 10 and proxy[1] ~= undef)
489    for i = 1, 10 do
490      assert(t[i] == 11 - i)
491    end
492    table.sort(proxy)
493    for i = 1, 10 do
494      assert(t[i] == i and proxy[i] == i)
495    end
496    assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
497    for i = 1, 8 do
498      assert(table.remove(proxy, 1) == i)
499    end
500    assert(#proxy == 2 and #t == 2)
501    local a, b, c = table.unpack(proxy)
502    assert(a == 9 and b == 10 and c == nil)
503  end
504
505  -- all virtual
506  local t = {}
507  local proxy = setmetatable({}, {
508    __len = function () return #t end,
509    __index = t,
510    __newindex = t,
511  })
512  test(proxy, t)
513
514  -- only __newindex
515  local count = 0
516  t = setmetatable({}, {
517    __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
518  test(t, t)
519  assert(count == 10)   -- after first 10, all other sets are not new
520
521  -- no __newindex
522  t = setmetatable({}, {
523    __index = function (_,k) return k + 1 end,
524    __len = function (_) return 5 end})
525  assert(table.concat(t, ";") == "2;3;4;5;6")
526
527end
528
529
530do   -- testing overflow in table.insert (must wrap-around)
531
532  local t = setmetatable({},
533            {__len = function () return math.maxinteger end})
534  table.insert(t, 20)
535  local k, v = next(t)
536  assert(k == math.mininteger and v == 20)
537end
538
539if not T then
540  (Message or print)
541    ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
542else --[
543  local debug = require'debug'
544  local tab = {10, 20, 30}
545  local mt = {}
546  local u = T.newuserdata(0)
547  checkerror("table expected", table.insert, u, 40)
548  checkerror("table expected", table.remove, u)
549  debug.setmetatable(u, mt)
550  checkerror("table expected", table.insert, u, 40)
551  checkerror("table expected", table.remove, u)
552  mt.__index = tab
553  checkerror("table expected", table.insert, u, 40)
554  checkerror("table expected", table.remove, u)
555  mt.__newindex = tab
556  checkerror("table expected", table.insert, u, 40)
557  checkerror("table expected", table.remove, u)
558  mt.__len = function () return #tab end
559  table.insert(u, 40)
560  assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
561  assert(table.remove(u) == 40)
562  table.insert(u, 1, 50)
563  assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
564
565  mt.__newindex = nil
566  mt.__len = nil
567  local tab2 = {}
568  local u2 = T.newuserdata(0)
569  debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
570  table.move(u, 1, 4, 1, u2)
571  assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
572
573end -- ]
574
575print('+')
576
577a = {}
578for i=1,1000 do
579  a[i] = i; a[i - 1] = undef
580end
581assert(next(a,nil) == 1000 and next(a,1000) == nil)
582
583assert(next({}) == nil)
584assert(next({}, nil) == nil)
585
586for a,b in pairs{} do error"not here" end
587for i=1,0 do error'not here' end
588for i=0,1,-1 do error'not here' end
589a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
590a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
591
592do
593  print("testing floats in numeric for")
594  local a
595  -- integer count
596  a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
597  a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
598  a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
599  a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
600  a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
601
602  -- float count
603  a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
604  a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
605  a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
606  a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
607  a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
608  a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
609  a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
610end
611
612do   -- changing the control variable
613  local a
614  a = 0; for i = 1, 10 do a = a + 1; i = "x" end; assert(a == 10)
615  a = 0; for i = 10.0, 1, -1 do a = a + 1; i = "x" end; assert(a == 10)
616end
617
618-- conversion
619a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
620
621do  -- checking types
622  local c
623  local function checkfloat (i)
624    assert(math.type(i) == "float")
625    c = c + 1
626  end
627
628  c = 0; for i = 1.0, 10 do checkfloat(i) end
629  assert(c == 10)
630
631  c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
632  assert(c == 10)
633
634  local function checkint (i)
635    assert(math.type(i) == "integer")
636    c = c + 1
637  end
638
639  local m = math.maxinteger
640  c = 0; for i = m, m - 10, -1 do checkint(i) end
641  assert(c == 11)
642
643  c = 0; for i = 1, 10.9 do checkint(i) end
644  assert(c == 10)
645
646  c = 0; for i = 10, 0.001, -1 do checkint(i) end
647  assert(c == 10)
648
649  c = 0; for i = 1, "10.8" do checkint(i) end
650  assert(c == 10)
651
652  c = 0; for i = 9, "3.4", -1 do checkint(i) end
653  assert(c == 6)
654
655  c = 0; for i = 0, " -3.4  ", -1 do checkint(i) end
656  assert(c == 4)
657
658  c = 0; for i = 100, "96.3", -2 do checkint(i) end
659  assert(c == 2)
660
661  c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
662  assert(c == 10)
663
664  c = 0; for i = -1, -math.huge, -1 do
665           if i < -10 then break end; checkint(i)
666          end
667  assert(c == 10)
668
669
670  for i = math.mininteger, -10e100 do assert(false) end
671  for i = math.maxinteger, 10e100, -1 do assert(false) end
672
673end
674
675
676do   -- testing other strange cases for numeric 'for'
677
678  local function checkfor (from, to, step, t)
679    local c = 0
680    for i = from, to, step do
681      c = c + 1
682      assert(i == t[c])
683    end
684    assert(c == #t)
685  end
686
687  local maxi = math.maxinteger
688  local mini = math.mininteger
689
690  checkfor(mini, maxi, maxi, {mini, -1, maxi - 1})
691
692  checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1})
693
694  checkfor(maxi, mini, mini, {maxi, -1})
695
696  checkfor(maxi, mini, -maxi, {maxi, 0, -maxi})
697
698  checkfor(maxi, -math.huge, mini, {maxi, -1})
699
700  checkfor(maxi, mini, 1, {})
701  checkfor(mini, maxi, -1, {})
702
703  checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi})
704  checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini})
705
706  local step = maxi // 10
707  local c = mini
708  for i = mini, maxi, step do
709    assert(i == c)
710    c = c + step
711  end
712
713  c = maxi
714  for i = maxi, mini, -step do
715    assert(i == c)
716    c = c - step
717  end
718
719  checkfor(maxi, maxi, maxi, {maxi})
720  checkfor(maxi, maxi, mini, {maxi})
721  checkfor(mini, mini, maxi, {mini})
722  checkfor(mini, mini, mini, {mini})
723end
724
725
726checkerror("'for' step is zero", function ()
727  for i = 1, 10, 0 do end
728end)
729
730checkerror("'for' step is zero", function ()
731  for i = 1, -10, 0 do end
732end)
733
734checkerror("'for' step is zero", function ()
735  for i = 1.0, -10, 0.0 do end
736end)
737
738collectgarbage()
739
740
741-- testing generic 'for'
742
743local function f (n, p)
744  local t = {}; for i=1,p do t[i] = i*10 end
745  return function (_, n, ...)
746           assert(select("#", ...) == 0)  -- no extra arguments
747           if n > 0 then
748             n = n-1
749             return n, table.unpack(t)
750           end
751         end, nil, n
752end
753
754local x = 0
755for n,a,b,c,d in f(5,3) do
756  x = x+1
757  assert(a == 10 and b == 20 and c == 30 and d == nil)
758end
759assert(x == 5)
760
761
762
763-- testing __pairs and __ipairs metamethod
764a = {}
765do
766  local x,y,z = pairs(a)
767  assert(type(x) == 'function' and y == a and z == nil)
768end
769
770local function foo (e,i)
771  assert(e == a)
772  if i <= 10 then return i+1, i+2 end
773end
774
775local function foo1 (e,i)
776  i = i + 1
777  assert(e == a)
778  if i <= e.n then return i,a[i] end
779end
780
781setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
782
783local i = 0
784for k,v in pairs(a) do
785  i = i + 1
786  assert(k == i and v == k+1)
787end
788
789a.n = 5
790a[3] = 30
791
792-- testing ipairs with metamethods
793a = {n=10}
794setmetatable(a, { __index = function (t,k)
795                     if k <= t.n then return k * 10 end
796                  end})
797i = 0
798for k,v in ipairs(a) do
799  i = i + 1
800  assert(k == i and v == i * 10)
801end
802assert(i == a.n)
803
804
805-- testing yield inside __pairs
806do
807  local t = setmetatable({10, 20, 30}, {__pairs = function (t)
808    local inc = coroutine.yield()
809    return function (t, i)
810             if i > 1 then return i - inc, t[i - inc]  else return nil end
811           end, t, #t + 1
812  end})
813
814  local res = {}
815  local co = coroutine.wrap(function ()
816    for i,p in pairs(t) do res[#res + 1] = p end
817  end)
818
819  co()     -- start coroutine
820  co(1)    -- continue after yield
821  assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3)
822  
823end
824
825print"OK"