nextvar.lua 18 KB

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