nextvar.lua 22 KB

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