nextvar.lua 20 KB

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