nextvar.lua 17 KB

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