nextvar.lua 17 KB

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