nextvar.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. do print("testing attack on table length")
  289. local t = {}
  290. local lim = math.floor(math.log(math.maxinteger, 2)) - 1
  291. for i = lim, 0, -1 do
  292. t[2^i] = true
  293. end
  294. assert(t[1 << lim])
  295. -- next loop should not take forever
  296. for i = 1, #t do end
  297. end
  298. local nofind = {}
  299. -- next uses always the same iteration function
  300. assert(next{} == next{})
  301. local function find (name)
  302. local n,v
  303. while 1 do
  304. n,v = next(_G, n)
  305. if not n then return nofind end
  306. assert(_G[n] ~= undef)
  307. if n == name then return v end
  308. end
  309. end
  310. local function find1 (name)
  311. for n,v in pairs(_G) do
  312. if n==name then return v end
  313. end
  314. return nil -- not found
  315. end
  316. assert(print==find("print") and print == find1("print"))
  317. assert(_G["print"]==find("print"))
  318. assert(assert==find1("assert"))
  319. assert(nofind==find("return"))
  320. assert(not find1("return"))
  321. _G["ret" .. "urn"] = undef
  322. assert(nofind==find("return"))
  323. _G["xxx"] = 1
  324. assert(xxx==find("xxx"))
  325. -- invalid key to 'next'
  326. checkerror("invalid key", next, {10,20}, 3)
  327. -- both 'pairs' and 'ipairs' need an argument
  328. checkerror("bad argument", pairs)
  329. checkerror("bad argument", ipairs)
  330. print('+')
  331. a = {}
  332. for i=0,10000 do
  333. if math.fmod(i,10) ~= 0 then
  334. a['x'..i] = i
  335. end
  336. end
  337. local n = {n=0}
  338. for i,v in pairs(a) do
  339. n.n = n.n+1
  340. assert(i and v and a[i] == v)
  341. end
  342. assert(n.n == 9000)
  343. a = nil
  344. do -- clear global table
  345. local a = {}
  346. for n,v in pairs(_G) do a[n]=v end
  347. for n,v in pairs(a) do
  348. if not package.loaded[n] and type(v) ~= "function" and
  349. not string.find(n, "^[%u_]") then
  350. _G[n] = undef
  351. end
  352. collectgarbage()
  353. end
  354. end
  355. --
  356. local function checknext (a)
  357. local b = {}
  358. do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
  359. for k,v in pairs(b) do assert(a[k] == v) end
  360. for k,v in pairs(a) do assert(b[k] == v) end
  361. end
  362. checknext{1,x=1,y=2,z=3}
  363. checknext{1,2,x=1,y=2,z=3}
  364. checknext{1,2,3,x=1,y=2,z=3}
  365. checknext{1,2,3,4,x=1,y=2,z=3}
  366. checknext{1,2,3,4,5,x=1,y=2,z=3}
  367. assert(#{} == 0)
  368. assert(#{[-1] = 2} == 0)
  369. for i=0,40 do
  370. local a = {}
  371. for j=1,i do a[j]=j end
  372. assert(#a == i)
  373. end
  374. -- 'maxn' is now deprecated, but it is easily defined in Lua
  375. function table.maxn (t)
  376. local max = 0
  377. for k in pairs(t) do
  378. max = (type(k) == 'number') and math.max(max, k) or max
  379. end
  380. return max
  381. end
  382. assert(table.maxn{} == 0)
  383. assert(table.maxn{["1000"] = true} == 0)
  384. assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
  385. assert(table.maxn{[1000] = true} == 1000)
  386. assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
  387. table.maxn = nil
  388. -- int overflow
  389. a = {}
  390. for i=0,50 do a[2^i] = true end
  391. assert(a[#a])
  392. print('+')
  393. do -- testing 'next' with all kinds of keys
  394. local a = {
  395. [1] = 1, -- integer
  396. [1.1] = 2, -- float
  397. ['x'] = 3, -- short string
  398. [string.rep('x', 1000)] = 4, -- long string
  399. [print] = 5, -- C function
  400. [checkerror] = 6, -- Lua function
  401. [coroutine.running()] = 7, -- thread
  402. [true] = 8, -- boolean
  403. [io.stdin] = 9, -- userdata
  404. [{}] = 10, -- table
  405. }
  406. local b = {}; for i = 1, 10 do b[i] = true end
  407. for k, v in pairs(a) do
  408. assert(b[v]); b[v] = undef
  409. end
  410. assert(next(b) == nil) -- 'b' now is empty
  411. end
  412. -- erasing values
  413. local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
  414. [100.3] = 4, [4] = 5}
  415. local n = 0
  416. for k, v in pairs( t ) do
  417. n = n+1
  418. assert(t[k] == v)
  419. t[k] = undef
  420. collectgarbage()
  421. assert(t[k] == undef)
  422. end
  423. assert(n == 5)
  424. do
  425. print("testing next x GC of deleted keys")
  426. -- bug in 5.4.1
  427. local co = coroutine.wrap(function (t)
  428. for k, v in pairs(t) do
  429. local k1 = next(t) -- all previous keys were deleted
  430. assert(k == k1) -- current key is the first in the table
  431. t[k] = nil
  432. local expected = (type(k) == "table" and k[1] or
  433. type(k) == "function" and k() or
  434. string.sub(k, 1, 1))
  435. assert(expected == v)
  436. coroutine.yield(v)
  437. end
  438. end)
  439. local t = {}
  440. t[{1}] = 1 -- add several unanchored, collectable keys
  441. t[{2}] = 2
  442. t[string.rep("a", 50)] = "a" -- long string
  443. t[string.rep("b", 50)] = "b"
  444. t[{3}] = 3
  445. t[string.rep("c", 10)] = "c" -- short string
  446. t[function () return 10 end] = 10
  447. local count = 7
  448. while co(t) do
  449. collectgarbage("collect") -- collect dead keys
  450. count = count - 1
  451. end
  452. assert(count == 0 and next(t) == nil) -- traversed the whole table
  453. end
  454. local function test (a)
  455. assert(not pcall(table.insert, a, 2, 20));
  456. table.insert(a, 10); table.insert(a, 2, 20);
  457. table.insert(a, 1, -1); table.insert(a, 40);
  458. table.insert(a, #a+1, 50)
  459. table.insert(a, 2, -2)
  460. assert(a[2] ~= undef)
  461. assert(a["2"] == undef)
  462. assert(not pcall(table.insert, a, 0, 20));
  463. assert(not pcall(table.insert, a, #a + 2, 20));
  464. assert(table.remove(a,1) == -1)
  465. assert(table.remove(a,1) == -2)
  466. assert(table.remove(a,1) == 10)
  467. assert(table.remove(a,1) == 20)
  468. assert(table.remove(a,1) == 40)
  469. assert(table.remove(a,1) == 50)
  470. assert(table.remove(a,1) == nil)
  471. assert(table.remove(a) == nil)
  472. assert(table.remove(a, #a) == nil)
  473. end
  474. a = {n=0, [-7] = "ban"}
  475. test(a)
  476. assert(a.n == 0 and a[-7] == "ban")
  477. a = {[-7] = "ban"};
  478. test(a)
  479. assert(a.n == nil and #a == 0 and a[-7] == "ban")
  480. a = {[-1] = "ban"}
  481. test(a)
  482. assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
  483. a = {[0] = "ban"}
  484. assert(#a == 0 and table.remove(a) == "ban" and a[0] == undef)
  485. table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
  486. assert(table.remove(a) == 10)
  487. assert(table.remove(a) == 20)
  488. assert(table.remove(a) == -1)
  489. assert(table.remove(a) == nil)
  490. a = {'c', 'd'}
  491. table.insert(a, 3, 'a')
  492. table.insert(a, 'b')
  493. assert(table.remove(a, 1) == 'c')
  494. assert(table.remove(a, 1) == 'd')
  495. assert(table.remove(a, 1) == 'a')
  496. assert(table.remove(a, 1) == 'b')
  497. assert(table.remove(a, 1) == nil)
  498. assert(#a == 0 and a.n == nil)
  499. a = {10,20,30,40}
  500. assert(table.remove(a, #a + 1) == nil)
  501. assert(not pcall(table.remove, a, 0))
  502. assert(a[#a] == 40)
  503. assert(table.remove(a, #a) == 40)
  504. assert(a[#a] == 30)
  505. assert(table.remove(a, 2) == 20)
  506. assert(a[#a] == 30 and #a == 2)
  507. do -- testing table library with metamethods
  508. local function test (proxy, t)
  509. for i = 1, 10 do
  510. table.insert(proxy, 1, i)
  511. end
  512. assert(#proxy == 10 and #t == 10 and proxy[1] ~= undef)
  513. for i = 1, 10 do
  514. assert(t[i] == 11 - i)
  515. end
  516. table.sort(proxy)
  517. for i = 1, 10 do
  518. assert(t[i] == i and proxy[i] == i)
  519. end
  520. assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
  521. for i = 1, 8 do
  522. assert(table.remove(proxy, 1) == i)
  523. end
  524. assert(#proxy == 2 and #t == 2)
  525. local a, b, c = table.unpack(proxy)
  526. assert(a == 9 and b == 10 and c == nil)
  527. end
  528. -- all virtual
  529. local t = {}
  530. local proxy = setmetatable({}, {
  531. __len = function () return #t end,
  532. __index = t,
  533. __newindex = t,
  534. })
  535. test(proxy, t)
  536. -- only __newindex
  537. local count = 0
  538. t = setmetatable({}, {
  539. __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
  540. test(t, t)
  541. assert(count == 10) -- after first 10, all other sets are not new
  542. -- no __newindex
  543. t = setmetatable({}, {
  544. __index = function (_,k) return k + 1 end,
  545. __len = function (_) return 5 end})
  546. assert(table.concat(t, ";") == "2;3;4;5;6")
  547. end
  548. do -- testing overflow in table.insert (must wrap-around)
  549. local t = setmetatable({},
  550. {__len = function () return math.maxinteger end})
  551. table.insert(t, 20)
  552. local k, v = next(t)
  553. assert(k == math.mininteger and v == 20)
  554. end
  555. if not T then
  556. (Message or print)
  557. ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
  558. else --[
  559. local debug = require'debug'
  560. local tab = {10, 20, 30}
  561. local mt = {}
  562. local u = T.newuserdata(0)
  563. checkerror("table expected", table.insert, u, 40)
  564. checkerror("table expected", table.remove, u)
  565. debug.setmetatable(u, mt)
  566. checkerror("table expected", table.insert, u, 40)
  567. checkerror("table expected", table.remove, u)
  568. mt.__index = tab
  569. checkerror("table expected", table.insert, u, 40)
  570. checkerror("table expected", table.remove, u)
  571. mt.__newindex = tab
  572. checkerror("table expected", table.insert, u, 40)
  573. checkerror("table expected", table.remove, u)
  574. mt.__len = function () return #tab end
  575. table.insert(u, 40)
  576. assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
  577. assert(table.remove(u) == 40)
  578. table.insert(u, 1, 50)
  579. assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
  580. mt.__newindex = nil
  581. mt.__len = nil
  582. local tab2 = {}
  583. local u2 = T.newuserdata(0)
  584. debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
  585. table.move(u, 1, 4, 1, u2)
  586. assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
  587. end -- ]
  588. print('+')
  589. a = {}
  590. for i=1,1000 do
  591. a[i] = i; a[i - 1] = undef
  592. end
  593. assert(next(a,nil) == 1000 and next(a,1000) == nil)
  594. assert(next({}) == nil)
  595. assert(next({}, nil) == nil)
  596. for a,b in pairs{} do error"not here" end
  597. for i=1,0 do error'not here' end
  598. for i=0,1,-1 do error'not here' end
  599. a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
  600. a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
  601. do
  602. print("testing floats in numeric for")
  603. local a
  604. -- integer count
  605. a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
  606. a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
  607. a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
  608. a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
  609. a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
  610. -- float count
  611. a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
  612. a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
  613. a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
  614. a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
  615. a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
  616. a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
  617. a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
  618. end
  619. do -- attempt to change the control variable
  620. local st, msg = load "for i = 1, 10 do i = 10 end"
  621. assert(not st and string.find(msg, "assign to const variable 'i'"))
  622. local st, msg = load "for v, k in pairs{} do v = 10 end"
  623. assert(not st and string.find(msg, "assign to const variable 'v'"))
  624. end
  625. -- conversion
  626. a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
  627. do -- checking types
  628. local c
  629. local function checkfloat (i)
  630. assert(math.type(i) == "float")
  631. c = c + 1
  632. end
  633. c = 0; for i = 1.0, 10 do checkfloat(i) end
  634. assert(c == 10)
  635. c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
  636. assert(c == 10)
  637. local function checkint (i)
  638. assert(math.type(i) == "integer")
  639. c = c + 1
  640. end
  641. local m = math.maxinteger
  642. c = 0; for i = m, m - 10, -1 do checkint(i) end
  643. assert(c == 11)
  644. c = 0; for i = 1, 10.9 do checkint(i) end
  645. assert(c == 10)
  646. c = 0; for i = 10, 0.001, -1 do checkint(i) end
  647. assert(c == 10)
  648. c = 0; for i = 1, "10.8" do checkint(i) end
  649. assert(c == 10)
  650. c = 0; for i = 9, "3.4", -1 do checkint(i) end
  651. assert(c == 6)
  652. c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end
  653. assert(c == 4)
  654. c = 0; for i = 100, "96.3", -2 do checkint(i) end
  655. assert(c == 2)
  656. c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
  657. assert(c == 10)
  658. c = 0; for i = -1, -math.huge, -1 do
  659. if i < -10 then break end; checkint(i)
  660. end
  661. assert(c == 10)
  662. for i = math.mininteger, -10e100 do assert(false) end
  663. for i = math.maxinteger, 10e100, -1 do assert(false) end
  664. end
  665. do -- testing other strange cases for numeric 'for'
  666. local function checkfor (from, to, step, t)
  667. local c = 0
  668. for i = from, to, step do
  669. c = c + 1
  670. assert(i == t[c])
  671. end
  672. assert(c == #t)
  673. end
  674. local maxi = math.maxinteger
  675. local mini = math.mininteger
  676. checkfor(mini, maxi, maxi, {mini, -1, maxi - 1})
  677. checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1})
  678. checkfor(maxi, mini, mini, {maxi, -1})
  679. checkfor(maxi, mini, -maxi, {maxi, 0, -maxi})
  680. checkfor(maxi, -math.huge, mini, {maxi, -1})
  681. checkfor(maxi, mini, 1, {})
  682. checkfor(mini, maxi, -1, {})
  683. checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi})
  684. checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini})
  685. local step = maxi // 10
  686. local c = mini
  687. for i = mini, maxi, step do
  688. assert(i == c)
  689. c = c + step
  690. end
  691. c = maxi
  692. for i = maxi, mini, -step do
  693. assert(i == c)
  694. c = c - step
  695. end
  696. checkfor(maxi, maxi, maxi, {maxi})
  697. checkfor(maxi, maxi, mini, {maxi})
  698. checkfor(mini, mini, maxi, {mini})
  699. checkfor(mini, mini, mini, {mini})
  700. end
  701. checkerror("'for' step is zero", function ()
  702. for i = 1, 10, 0 do end
  703. end)
  704. checkerror("'for' step is zero", function ()
  705. for i = 1, -10, 0 do end
  706. end)
  707. checkerror("'for' step is zero", function ()
  708. for i = 1.0, -10, 0.0 do end
  709. end)
  710. collectgarbage()
  711. -- testing generic 'for'
  712. local function f (n, p)
  713. local t = {}; for i=1,p do t[i] = i*10 end
  714. return function (_, n, ...)
  715. assert(select("#", ...) == 0) -- no extra arguments
  716. if n > 0 then
  717. n = n-1
  718. return n, table.unpack(t)
  719. end
  720. end, nil, n
  721. end
  722. local x = 0
  723. for n,a,b,c,d in f(5,3) do
  724. x = x+1
  725. assert(a == 10 and b == 20 and c == 30 and d == nil)
  726. end
  727. assert(x == 5)
  728. -- testing __pairs and __ipairs metamethod
  729. a = {}
  730. do
  731. local x,y,z = pairs(a)
  732. assert(type(x) == 'function' and y == a and z == nil)
  733. end
  734. local function foo (e,i)
  735. assert(e == a)
  736. if i <= 10 then return i+1, i+2 end
  737. end
  738. local function foo1 (e,i)
  739. i = i + 1
  740. assert(e == a)
  741. if i <= e.n then return i,a[i] end
  742. end
  743. setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
  744. local i = 0
  745. for k,v in pairs(a) do
  746. i = i + 1
  747. assert(k == i and v == k+1)
  748. end
  749. a.n = 5
  750. a[3] = 30
  751. -- testing ipairs with metamethods
  752. a = {n=10}
  753. setmetatable(a, { __index = function (t,k)
  754. if k <= t.n then return k * 10 end
  755. end})
  756. i = 0
  757. for k,v in ipairs(a) do
  758. i = i + 1
  759. assert(k == i and v == i * 10)
  760. end
  761. assert(i == a.n)
  762. -- testing yield inside __pairs
  763. do
  764. local t = setmetatable({10, 20, 30}, {__pairs = function (t)
  765. local inc = coroutine.yield()
  766. return function (t, i)
  767. if i > 1 then return i - inc, t[i - inc] else return nil end
  768. end, t, #t + 1
  769. end})
  770. local res = {}
  771. local co = coroutine.wrap(function ()
  772. for i,p in pairs(t) do res[#res + 1] = p end
  773. end)
  774. co() -- start coroutine
  775. co(1) -- continue after yield
  776. assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3)
  777. end
  778. print"OK"