nextvar.lua 19 KB

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