nextvar.lua 22 KB

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