nextvar.lua 19 KB

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