nextvar.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. -- $Id: testes/nextvar.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing tables, next, and for')
  4. local function checkerror (msg, f, ...)
  5. local s, err = pcall(f, ...)
  6. assert(not s and string.find(err, msg))
  7. end
  8. local a = {}
  9. -- make sure table has lots of space in hash part
  10. for i=1,100 do a[i.."+"] = true end
  11. for i=1,100 do a[i.."+"] = undef end
  12. -- fill hash part with numeric indices testing size operator
  13. for i=1,100 do
  14. a[i] = true
  15. assert(#a == i)
  16. end
  17. -- testing ipairs
  18. local x = 0
  19. for k,v in ipairs{10,20,30;x=12} do
  20. x = x + 1
  21. assert(k == x and v == x * 10)
  22. end
  23. for _ in ipairs{x=12, y=24} do assert(nil) end
  24. -- test for 'false' x ipair
  25. x = false
  26. local i = 0
  27. for k,v in ipairs{true,false,true,false} do
  28. i = i + 1
  29. x = not x
  30. assert(x == v)
  31. end
  32. assert(i == 4)
  33. -- iterator function is always the same
  34. assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
  35. if not T then
  36. (Message or print)
  37. ('\n >>> testC not active: skipping tests for table sizes <<<\n')
  38. else --[
  39. -- testing table sizes
  40. local function log2 (x) return math.log(x, 2) end
  41. local function mp2 (n) -- minimum power of 2 >= n
  42. local mp = 2^math.ceil(log2(n))
  43. assert(n == 0 or (mp/2 < n and n <= mp))
  44. return mp
  45. end
  46. local function fb (n)
  47. local r, nn = T.int2fb(n)
  48. assert(r < 256)
  49. return nn
  50. end
  51. -- test fb function
  52. for a = 1, 10000 do -- all numbers up to 10^4
  53. local n = fb(a)
  54. assert(a <= n and n <= a*1.125)
  55. end
  56. local a = 1024 -- plus a few up to 2 ^30
  57. local lim = 2^30
  58. while a < lim do
  59. local n = fb(a)
  60. assert(a <= n and n <= a*1.125)
  61. a = math.ceil(a*1.3)
  62. end
  63. local function check (t, na, nh)
  64. local a, h = T.querytab(t)
  65. if a ~= na or h ~= nh then
  66. print(na, nh, a, h)
  67. assert(nil)
  68. end
  69. end
  70. -- testing C library sizes
  71. do
  72. local s = 0
  73. for _ in pairs(math) do s = s + 1 end
  74. check(math, 0, mp2(s))
  75. end
  76. -- testing constructor sizes
  77. local lim = 40
  78. local s = 'return {'
  79. for i=1,lim do
  80. s = s..i..','
  81. local s = s
  82. for k=0,lim do
  83. local t = load(s..'}', '')()
  84. assert(#t == i)
  85. check(t, fb(i), mp2(k))
  86. s = string.format('%sa%d=%d,', s, k, k)
  87. end
  88. end
  89. -- tests with unknown number of elements
  90. local a = {}
  91. for i=1,lim do a[i] = i end -- build auxiliary table
  92. for k=0,lim do
  93. local a = {table.unpack(a,1,k)}
  94. assert(#a == k)
  95. check(a, k, 0)
  96. a = {1,2,3,table.unpack(a,1,k)}
  97. check(a, k+3, 0)
  98. assert(#a == k + 3)
  99. end
  100. -- testing tables dynamically built
  101. local lim = 130
  102. local a = {}; a[2] = 1; check(a, 0, 1)
  103. a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
  104. a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
  105. a = {}
  106. for i = 1,lim do
  107. a[i] = 1
  108. assert(#a == i)
  109. check(a, mp2(i), 0)
  110. end
  111. a = {}
  112. for i = 1,lim do
  113. a['a'..i] = 1
  114. assert(#a == 0)
  115. check(a, 0, mp2(i))
  116. end
  117. a = {}
  118. for i=1,16 do a[i] = i end
  119. check(a, 16, 0)
  120. do
  121. for i=1,11 do a[i] = undef end
  122. for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
  123. check(a, 0, 8) -- 5 elements in the table
  124. a[10] = 1
  125. for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
  126. check(a, 0, 8) -- only 6 elements in the table
  127. for i=1,14 do a[i] = true; a[i] = undef end
  128. for i=18,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
  129. check(a, 0, 4) -- only 2 elements ([15] and [16])
  130. end
  131. -- reverse filling
  132. for i=1,lim do
  133. local a = {}
  134. for i=i,1,-1 do a[i] = i end -- fill in reverse
  135. check(a, mp2(i), 0)
  136. end
  137. -- size tests for vararg
  138. lim = 35
  139. function foo (n, ...)
  140. local arg = {...}
  141. check(arg, n, 0)
  142. assert(select('#', ...) == n)
  143. arg[n+1] = true
  144. check(arg, mp2(n+1), 0)
  145. arg.x = true
  146. check(arg, mp2(n+1), 1)
  147. end
  148. local a = {}
  149. for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
  150. -- Table length with limit smaller than maximum value at array
  151. local a = {}
  152. for i = 1,64 do a[i] = true end -- make its array size 64
  153. for i = 1,64 do a[i] = nil end -- erase all elements
  154. assert(T.querytab(a) == 64) -- array part has 64 elements
  155. a[32] = true; a[48] = true; -- binary search will find these ones
  156. a[51] = true -- binary search will miss this one
  157. assert(#a == 48) -- this will set the limit
  158. assert(select(4, T.querytab(a)) == 48) -- this is the limit now
  159. a[50] = true -- this will set a new limit
  160. assert(select(4, T.querytab(a)) == 50) -- this is the limit now
  161. -- but the size is larger (and still inside the array part)
  162. assert(#a == 51)
  163. end --]
  164. -- test size operation on tables with nils
  165. assert(#{} == 0)
  166. assert(#{nil} == 0)
  167. assert(#{nil, nil} == 0)
  168. assert(#{nil, nil, nil} == 0)
  169. assert(#{nil, nil, nil, nil} == 0)
  170. assert(#{1, 2, 3, nil, nil} == 3)
  171. print'+'
  172. local nofind = {}
  173. a,b,c = 1,2,3
  174. a,b,c = nil
  175. -- next uses always the same iteraction function
  176. assert(next{} == next{})
  177. local function find (name)
  178. local n,v
  179. while 1 do
  180. n,v = next(_G, n)
  181. if not n then return nofind end
  182. assert(_G[n] ~= undef)
  183. if n == name then return v end
  184. end
  185. end
  186. local function find1 (name)
  187. for n,v in pairs(_G) do
  188. if n==name then return v end
  189. end
  190. return nil -- not found
  191. end
  192. assert(print==find("print") and print == find1("print"))
  193. assert(_G["print"]==find("print"))
  194. assert(assert==find1("assert"))
  195. assert(nofind==find("return"))
  196. assert(not find1("return"))
  197. _G["ret" .. "urn"] = undef
  198. assert(nofind==find("return"))
  199. _G["xxx"] = 1
  200. assert(xxx==find("xxx"))
  201. -- invalid key to 'next'
  202. checkerror("invalid key", next, {10,20}, 3)
  203. -- both 'pairs' and 'ipairs' need an argument
  204. checkerror("bad argument", pairs)
  205. checkerror("bad argument", ipairs)
  206. print('+')
  207. a = {}
  208. for i=0,10000 do
  209. if math.fmod(i,10) ~= 0 then
  210. a['x'..i] = i
  211. end
  212. end
  213. n = {n=0}
  214. for i,v in pairs(a) do
  215. n.n = n.n+1
  216. assert(i and v and a[i] == v)
  217. end
  218. assert(n.n == 9000)
  219. a = nil
  220. do -- clear global table
  221. local a = {}
  222. for n,v in pairs(_G) do a[n]=v end
  223. for n,v in pairs(a) do
  224. if not package.loaded[n] and type(v) ~= "function" and
  225. not string.find(n, "^[%u_]") then
  226. _G[n] = undef
  227. end
  228. collectgarbage()
  229. end
  230. end
  231. --
  232. local function checknext (a)
  233. local b = {}
  234. do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
  235. for k,v in pairs(b) do assert(a[k] == v) end
  236. for k,v in pairs(a) do assert(b[k] == v) end
  237. end
  238. checknext{1,x=1,y=2,z=3}
  239. checknext{1,2,x=1,y=2,z=3}
  240. checknext{1,2,3,x=1,y=2,z=3}
  241. checknext{1,2,3,4,x=1,y=2,z=3}
  242. checknext{1,2,3,4,5,x=1,y=2,z=3}
  243. assert(#{} == 0)
  244. assert(#{[-1] = 2} == 0)
  245. for i=0,40 do
  246. local a = {}
  247. for j=1,i do a[j]=j end
  248. assert(#a == i)
  249. end
  250. -- 'maxn' is now deprecated, but it is easily defined in Lua
  251. function table.maxn (t)
  252. local max = 0
  253. for k in pairs(t) do
  254. max = (type(k) == 'number') and math.max(max, k) or max
  255. end
  256. return max
  257. end
  258. assert(table.maxn{} == 0)
  259. assert(table.maxn{["1000"] = true} == 0)
  260. assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
  261. assert(table.maxn{[1000] = true} == 1000)
  262. assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
  263. table.maxn = nil
  264. -- int overflow
  265. a = {}
  266. for i=0,50 do a[2^i] = true end
  267. assert(a[#a])
  268. print('+')
  269. do -- testing 'next' with all kinds of keys
  270. local a = {
  271. [1] = 1, -- integer
  272. [1.1] = 2, -- float
  273. ['x'] = 3, -- short string
  274. [string.rep('x', 1000)] = 4, -- long string
  275. [print] = 5, -- C function
  276. [checkerror] = 6, -- Lua function
  277. [coroutine.running()] = 7, -- thread
  278. [true] = 8, -- boolean
  279. [io.stdin] = 9, -- userdata
  280. [{}] = 10, -- table
  281. }
  282. local b = {}; for i = 1, 10 do b[i] = true end
  283. for k, v in pairs(a) do
  284. assert(b[v]); b[v] = undef
  285. end
  286. assert(next(b) == nil) -- 'b' now is empty
  287. end
  288. -- erasing values
  289. local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
  290. [100.3] = 4, [4] = 5}
  291. local n = 0
  292. for k, v in pairs( t ) do
  293. n = n+1
  294. assert(t[k] == v)
  295. t[k] = undef
  296. collectgarbage()
  297. assert(t[k] == undef)
  298. end
  299. assert(n == 5)
  300. local function test (a)
  301. assert(not pcall(table.insert, a, 2, 20));
  302. table.insert(a, 10); table.insert(a, 2, 20);
  303. table.insert(a, 1, -1); table.insert(a, 40);
  304. table.insert(a, #a+1, 50)
  305. table.insert(a, 2, -2)
  306. assert(a[2] ~= undef)
  307. assert(a["2"] == undef)
  308. assert(not pcall(table.insert, a, 0, 20));
  309. assert(not pcall(table.insert, a, #a + 2, 20));
  310. assert(table.remove(a,1) == -1)
  311. assert(table.remove(a,1) == -2)
  312. assert(table.remove(a,1) == 10)
  313. assert(table.remove(a,1) == 20)
  314. assert(table.remove(a,1) == 40)
  315. assert(table.remove(a,1) == 50)
  316. assert(table.remove(a,1) == nil)
  317. assert(table.remove(a) == nil)
  318. assert(table.remove(a, #a) == nil)
  319. end
  320. a = {n=0, [-7] = "ban"}
  321. test(a)
  322. assert(a.n == 0 and a[-7] == "ban")
  323. a = {[-7] = "ban"};
  324. test(a)
  325. assert(a.n == nil and #a == 0 and a[-7] == "ban")
  326. a = {[-1] = "ban"}
  327. test(a)
  328. assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
  329. a = {[0] = "ban"}
  330. assert(#a == 0 and table.remove(a) == "ban" and a[0] == undef)
  331. table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
  332. assert(table.remove(a) == 10)
  333. assert(table.remove(a) == 20)
  334. assert(table.remove(a) == -1)
  335. assert(table.remove(a) == nil)
  336. a = {'c', 'd'}
  337. table.insert(a, 3, 'a')
  338. table.insert(a, 'b')
  339. assert(table.remove(a, 1) == 'c')
  340. assert(table.remove(a, 1) == 'd')
  341. assert(table.remove(a, 1) == 'a')
  342. assert(table.remove(a, 1) == 'b')
  343. assert(table.remove(a, 1) == nil)
  344. assert(#a == 0 and a.n == nil)
  345. a = {10,20,30,40}
  346. assert(table.remove(a, #a + 1) == nil)
  347. assert(not pcall(table.remove, a, 0))
  348. assert(a[#a] == 40)
  349. assert(table.remove(a, #a) == 40)
  350. assert(a[#a] == 30)
  351. assert(table.remove(a, 2) == 20)
  352. assert(a[#a] == 30 and #a == 2)
  353. do -- testing table library with metamethods
  354. local function test (proxy, t)
  355. for i = 1, 10 do
  356. table.insert(proxy, 1, i)
  357. end
  358. assert(#proxy == 10 and #t == 10 and proxy[1] ~= undef)
  359. for i = 1, 10 do
  360. assert(t[i] == 11 - i)
  361. end
  362. table.sort(proxy)
  363. for i = 1, 10 do
  364. assert(t[i] == i and proxy[i] == i)
  365. end
  366. assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
  367. for i = 1, 8 do
  368. assert(table.remove(proxy, 1) == i)
  369. end
  370. assert(#proxy == 2 and #t == 2)
  371. local a, b, c = table.unpack(proxy)
  372. assert(a == 9 and b == 10 and c == nil)
  373. end
  374. -- all virtual
  375. local t = {}
  376. local proxy = setmetatable({}, {
  377. __len = function () return #t end,
  378. __index = t,
  379. __newindex = t,
  380. })
  381. test(proxy, t)
  382. -- only __newindex
  383. local count = 0
  384. t = setmetatable({}, {
  385. __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
  386. test(t, t)
  387. assert(count == 10) -- after first 10, all other sets are not new
  388. -- no __newindex
  389. t = setmetatable({}, {
  390. __index = function (_,k) return k + 1 end,
  391. __len = function (_) return 5 end})
  392. assert(table.concat(t, ";") == "2;3;4;5;6")
  393. end
  394. if not T then
  395. (Message or print)
  396. ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
  397. else --[
  398. local debug = require'debug'
  399. local tab = {10, 20, 30}
  400. local mt = {}
  401. local u = T.newuserdata(0)
  402. checkerror("table expected", table.insert, u, 40)
  403. checkerror("table expected", table.remove, u)
  404. debug.setmetatable(u, mt)
  405. checkerror("table expected", table.insert, u, 40)
  406. checkerror("table expected", table.remove, u)
  407. mt.__index = tab
  408. checkerror("table expected", table.insert, u, 40)
  409. checkerror("table expected", table.remove, u)
  410. mt.__newindex = tab
  411. checkerror("table expected", table.insert, u, 40)
  412. checkerror("table expected", table.remove, u)
  413. mt.__len = function () return #tab end
  414. table.insert(u, 40)
  415. assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
  416. assert(table.remove(u) == 40)
  417. table.insert(u, 1, 50)
  418. assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
  419. mt.__newindex = nil
  420. mt.__len = nil
  421. local tab2 = {}
  422. local u2 = T.newuserdata(0)
  423. debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
  424. table.move(u, 1, 4, 1, u2)
  425. assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
  426. end -- ]
  427. print('+')
  428. a = {}
  429. for i=1,1000 do
  430. a[i] = i; a[i - 1] = undef
  431. end
  432. assert(next(a,nil) == 1000 and next(a,1000) == nil)
  433. assert(next({}) == nil)
  434. assert(next({}, nil) == nil)
  435. for a,b in pairs{} do error"not here" end
  436. for i=1,0 do error'not here' end
  437. for i=0,1,-1 do error'not here' end
  438. a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
  439. a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
  440. do
  441. print("testing floats in numeric for")
  442. local a
  443. -- integer count
  444. a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
  445. a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
  446. a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
  447. a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
  448. a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
  449. -- float count
  450. a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
  451. a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
  452. a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
  453. a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
  454. a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
  455. a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
  456. a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
  457. end
  458. -- conversion
  459. a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
  460. do -- checking types
  461. local c
  462. local function checkfloat (i)
  463. assert(math.type(i) == "float")
  464. c = c + 1
  465. end
  466. c = 0; for i = 1.0, 10 do checkfloat(i) end
  467. assert(c == 10)
  468. c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
  469. assert(c == 10)
  470. local function checkint (i)
  471. assert(math.type(i) == "integer")
  472. c = c + 1
  473. end
  474. local m = math.maxinteger
  475. c = 0; for i = m, m - 10, -1 do checkint(i) end
  476. assert(c == 11)
  477. c = 0; for i = 1, 10.9 do checkint(i) end
  478. assert(c == 10)
  479. c = 0; for i = 10, 0.001, -1 do checkint(i) end
  480. assert(c == 10)
  481. c = 0; for i = 1, "10.8" do checkint(i) end
  482. assert(c == 10)
  483. c = 0; for i = 9, "3.4", -1 do checkint(i) end
  484. assert(c == 6)
  485. c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end
  486. assert(c == 4)
  487. c = 0; for i = 100, "96.3", -2 do checkint(i) end
  488. assert(c == 2)
  489. c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
  490. assert(c == 10)
  491. c = 0; for i = -1, -math.huge, -1 do
  492. if i < -10 then break end; checkint(i)
  493. end
  494. assert(c == 10)
  495. for i = math.mininteger, -10e100 do assert(false) end
  496. for i = math.maxinteger, 10e100, -1 do assert(false) end
  497. end
  498. do -- testing other strange cases for numeric 'for'
  499. local function checkfor (from, to, step, t)
  500. local c = 0
  501. for i = from, to, step do
  502. c = c + 1
  503. assert(i == t[c])
  504. end
  505. assert(c == #t)
  506. end
  507. local maxi = math.maxinteger
  508. local mini = math.mininteger
  509. checkfor(mini, maxi, maxi, {mini, -1, maxi - 1})
  510. checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1})
  511. checkfor(maxi, mini, mini, {maxi, -1})
  512. checkfor(maxi, mini, -maxi, {maxi, 0, -maxi})
  513. checkfor(maxi, -math.huge, mini, {maxi, -1})
  514. checkfor(maxi, mini, 1, {})
  515. checkfor(mini, maxi, -1, {})
  516. checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi})
  517. checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini})
  518. local step = maxi // 10
  519. local c = mini
  520. for i = mini, maxi, step do
  521. assert(i == c)
  522. c = c + step
  523. end
  524. c = maxi
  525. for i = maxi, mini, -step do
  526. assert(i == c)
  527. c = c - step
  528. end
  529. checkfor(maxi, maxi, maxi, {maxi})
  530. checkfor(maxi, maxi, mini, {maxi})
  531. checkfor(mini, mini, maxi, {mini})
  532. checkfor(mini, mini, mini, {mini})
  533. end
  534. checkerror("'for' step is zero", function ()
  535. for i = 1, 10, 0 do end
  536. end)
  537. checkerror("'for' step is zero", function ()
  538. for i = 1, -10, 0 do end
  539. end)
  540. checkerror("'for' step is zero", function ()
  541. for i = 1.0, -10, 0.0 do end
  542. end)
  543. collectgarbage()
  544. -- testing generic 'for'
  545. local function f (n, p)
  546. local t = {}; for i=1,p do t[i] = i*10 end
  547. return function (_,n)
  548. if n > 0 then
  549. n = n-1
  550. return n, table.unpack(t)
  551. end
  552. end, nil, n
  553. end
  554. local x = 0
  555. for n,a,b,c,d in f(5,3) do
  556. x = x+1
  557. assert(a == 10 and b == 20 and c == 30 and d == nil)
  558. end
  559. assert(x == 5)
  560. -- testing __pairs and __ipairs metamethod
  561. a = {}
  562. do
  563. local x,y,z = pairs(a)
  564. assert(type(x) == 'function' and y == a and z == nil)
  565. end
  566. local function foo (e,i)
  567. assert(e == a)
  568. if i <= 10 then return i+1, i+2 end
  569. end
  570. local function foo1 (e,i)
  571. i = i + 1
  572. assert(e == a)
  573. if i <= e.n then return i,a[i] end
  574. end
  575. setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
  576. local i = 0
  577. for k,v in pairs(a) do
  578. i = i + 1
  579. assert(k == i and v == k+1)
  580. end
  581. a.n = 5
  582. a[3] = 30
  583. -- testing ipairs with metamethods
  584. a = {n=10}
  585. setmetatable(a, { __index = function (t,k)
  586. if k <= t.n then return k * 10 end
  587. end})
  588. i = 0
  589. for k,v in ipairs(a) do
  590. i = i + 1
  591. assert(k == i and v == i * 10)
  592. end
  593. assert(i == a.n)
  594. print"OK"