nextvar.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. -- $Id: nextvar.lua,v 1.79 2016/11/07 13:11:28 roberto Exp $
  2. -- See Copyright Notice in file all.lua
  3. print('testing tables, next, and for')
  4. local function checkerror (msg, f, ...)
  5. local s, err = pcall(f, ...)
  6. assert(not s and string.find(err, msg))
  7. end
  8. local a = {}
  9. -- make sure table has lots of space in hash part
  10. for i=1,100 do a[i.."+"] = true end
  11. for i=1,100 do a[i.."+"] = nil end
  12. -- fill hash part with numeric indices testing size operator
  13. for i=1,100 do
  14. a[i] = true
  15. assert(#a == i)
  16. end
  17. -- testing ipairs
  18. local x = 0
  19. for k,v in ipairs{10,20,30;x=12} do
  20. x = x + 1
  21. assert(k == x and v == x * 10)
  22. end
  23. for _ in ipairs{x=12, y=24} do assert(nil) end
  24. -- test for 'false' x ipair
  25. x = false
  26. local i = 0
  27. for k,v in ipairs{true,false,true,false} do
  28. i = i + 1
  29. x = not x
  30. assert(x == v)
  31. end
  32. assert(i == 4)
  33. -- iterator function is always the same
  34. assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
  35. if not T then
  36. (Message or print)
  37. ('\n >>> testC not active: skipping tests for table sizes <<<\n')
  38. else --[
  39. -- testing table sizes
  40. local function log2 (x) return math.log(x, 2) end
  41. local function mp2 (n) -- minimum power of 2 >= n
  42. local mp = 2^math.ceil(log2(n))
  43. assert(n == 0 or (mp/2 < n and n <= mp))
  44. return mp
  45. end
  46. local function fb (n)
  47. local r, nn = T.int2fb(n)
  48. assert(r < 256)
  49. return nn
  50. end
  51. -- test fb function
  52. for a = 1, 10000 do -- all numbers up to 10^4
  53. local n = fb(a)
  54. assert(a <= n and n <= a*1.125)
  55. end
  56. local a = 1024 -- plus a few up to 2 ^30
  57. local lim = 2^30
  58. while a < lim do
  59. local n = fb(a)
  60. assert(a <= n and n <= a*1.125)
  61. a = math.ceil(a*1.3)
  62. end
  63. local function check (t, na, nh)
  64. local a, h = T.querytab(t)
  65. if a ~= na or h ~= nh then
  66. print(na, nh, a, h)
  67. assert(nil)
  68. end
  69. end
  70. -- testing C library sizes
  71. do
  72. local s = 0
  73. for _ in pairs(math) do s = s + 1 end
  74. check(math, 0, mp2(s))
  75. end
  76. -- testing constructor sizes
  77. local lim = 40
  78. local s = 'return {'
  79. for i=1,lim do
  80. s = s..i..','
  81. local s = s
  82. for k=0,lim do
  83. local t = load(s..'}', '')()
  84. assert(#t == i)
  85. check(t, fb(i), mp2(k))
  86. s = string.format('%sa%d=%d,', s, k, k)
  87. end
  88. end
  89. -- tests with unknown number of elements
  90. local a = {}
  91. for i=1,lim do a[i] = i end -- build auxiliary table
  92. for k=0,lim do
  93. local a = {table.unpack(a,1,k)}
  94. assert(#a == k)
  95. check(a, k, 0)
  96. a = {1,2,3,table.unpack(a,1,k)}
  97. check(a, k+3, 0)
  98. assert(#a == k + 3)
  99. end
  100. -- testing tables dynamically built
  101. local lim = 130
  102. local a = {}; a[2] = 1; check(a, 0, 1)
  103. a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
  104. a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
  105. a = {}
  106. for i = 1,lim do
  107. a[i] = 1
  108. assert(#a == i)
  109. check(a, mp2(i), 0)
  110. end
  111. a = {}
  112. for i = 1,lim do
  113. a['a'..i] = 1
  114. assert(#a == 0)
  115. check(a, 0, mp2(i))
  116. end
  117. a = {}
  118. for i=1,16 do a[i] = i end
  119. check(a, 16, 0)
  120. do
  121. for i=1,11 do a[i] = nil end
  122. for i=30,50 do a[i] = nil end -- force a rehash (?)
  123. check(a, 0, 8) -- only 5 elements in the table
  124. a[10] = 1
  125. for i=30,50 do a[i] = nil end -- force a rehash (?)
  126. check(a, 0, 8) -- only 6 elements in the table
  127. for i=1,14 do a[i] = nil end
  128. for i=18,50 do a[i] = nil end -- force a rehash (?)
  129. check(a, 0, 4) -- only 2 elements ([15] and [16])
  130. end
  131. -- reverse filling
  132. for i=1,lim do
  133. local a = {}
  134. for i=i,1,-1 do a[i] = i end -- fill in reverse
  135. check(a, mp2(i), 0)
  136. end
  137. -- size tests for vararg
  138. lim = 35
  139. function foo (n, ...)
  140. local arg = {...}
  141. check(arg, n, 0)
  142. assert(select('#', ...) == n)
  143. arg[n+1] = true
  144. check(arg, mp2(n+1), 0)
  145. arg.x = true
  146. check(arg, mp2(n+1), 1)
  147. end
  148. local a = {}
  149. for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
  150. end --]
  151. -- test size operation on empty tables
  152. assert(#{} == 0)
  153. assert(#{nil} == 0)
  154. assert(#{nil, nil} == 0)
  155. assert(#{nil, nil, nil} == 0)
  156. assert(#{nil, nil, nil, nil} == 0)
  157. print'+'
  158. local nofind = {}
  159. a,b,c = 1,2,3
  160. a,b,c = nil
  161. -- next uses always the same iteraction function
  162. assert(next{} == next{})
  163. local function find (name)
  164. local n,v
  165. while 1 do
  166. n,v = next(_G, n)
  167. if not n then return nofind end
  168. assert(v ~= nil)
  169. if n == name then return v end
  170. end
  171. end
  172. local function find1 (name)
  173. for n,v in pairs(_G) do
  174. if n==name then return v end
  175. end
  176. return nil -- not found
  177. end
  178. assert(print==find("print") and print == find1("print"))
  179. assert(_G["print"]==find("print"))
  180. assert(assert==find1("assert"))
  181. assert(nofind==find("return"))
  182. assert(not find1("return"))
  183. _G["ret" .. "urn"] = nil
  184. assert(nofind==find("return"))
  185. _G["xxx"] = 1
  186. assert(xxx==find("xxx"))
  187. -- invalid key to 'next'
  188. checkerror("invalid key", next, {10,20}, 3)
  189. -- both 'pairs' and 'ipairs' need an argument
  190. checkerror("bad argument", pairs)
  191. checkerror("bad argument", ipairs)
  192. print('+')
  193. a = {}
  194. for i=0,10000 do
  195. if math.fmod(i,10) ~= 0 then
  196. a['x'..i] = i
  197. end
  198. end
  199. n = {n=0}
  200. for i,v in pairs(a) do
  201. n.n = n.n+1
  202. assert(i and v and a[i] == v)
  203. end
  204. assert(n.n == 9000)
  205. a = nil
  206. do -- clear global table
  207. local a = {}
  208. for n,v in pairs(_G) do a[n]=v end
  209. for n,v in pairs(a) do
  210. if not package.loaded[n] and type(v) ~= "function" and
  211. not string.find(n, "^[%u_]") then
  212. _G[n] = nil
  213. end
  214. collectgarbage()
  215. end
  216. end
  217. --
  218. local function checknext (a)
  219. local b = {}
  220. do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
  221. for k,v in pairs(b) do assert(a[k] == v) end
  222. for k,v in pairs(a) do assert(b[k] == v) end
  223. end
  224. checknext{1,x=1,y=2,z=3}
  225. checknext{1,2,x=1,y=2,z=3}
  226. checknext{1,2,3,x=1,y=2,z=3}
  227. checknext{1,2,3,4,x=1,y=2,z=3}
  228. checknext{1,2,3,4,5,x=1,y=2,z=3}
  229. assert(#{} == 0)
  230. assert(#{[-1] = 2} == 0)
  231. assert(#{1,2,3,nil,nil} == 3)
  232. for i=0,40 do
  233. local a = {}
  234. for j=1,i do a[j]=j end
  235. assert(#a == i)
  236. end
  237. -- 'maxn' is now deprecated, but it is easily defined in Lua
  238. function table.maxn (t)
  239. local max = 0
  240. for k in pairs(t) do
  241. max = (type(k) == 'number') and math.max(max, k) or max
  242. end
  243. return max
  244. end
  245. assert(table.maxn{} == 0)
  246. assert(table.maxn{["1000"] = true} == 0)
  247. assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
  248. assert(table.maxn{[1000] = true} == 1000)
  249. assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
  250. table.maxn = nil
  251. -- int overflow
  252. a = {}
  253. for i=0,50 do a[2^i] = true end
  254. assert(a[#a])
  255. print('+')
  256. -- erasing values
  257. local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
  258. [100.3] = 4, [4] = 5}
  259. local n = 0
  260. for k, v in pairs( t ) do
  261. n = n+1
  262. assert(t[k] == v)
  263. t[k] = nil
  264. collectgarbage()
  265. assert(t[k] == nil)
  266. end
  267. assert(n == 5)
  268. local function test (a)
  269. assert(not pcall(table.insert, a, 2, 20));
  270. table.insert(a, 10); table.insert(a, 2, 20);
  271. table.insert(a, 1, -1); table.insert(a, 40);
  272. table.insert(a, #a+1, 50)
  273. table.insert(a, 2, -2)
  274. assert(not pcall(table.insert, a, 0, 20));
  275. assert(not pcall(table.insert, a, #a + 2, 20));
  276. assert(table.remove(a,1) == -1)
  277. assert(table.remove(a,1) == -2)
  278. assert(table.remove(a,1) == 10)
  279. assert(table.remove(a,1) == 20)
  280. assert(table.remove(a,1) == 40)
  281. assert(table.remove(a,1) == 50)
  282. assert(table.remove(a,1) == nil)
  283. assert(table.remove(a) == nil)
  284. assert(table.remove(a, #a) == nil)
  285. end
  286. a = {n=0, [-7] = "ban"}
  287. test(a)
  288. assert(a.n == 0 and a[-7] == "ban")
  289. a = {[-7] = "ban"};
  290. test(a)
  291. assert(a.n == nil and #a == 0 and a[-7] == "ban")
  292. a = {[-1] = "ban"}
  293. test(a)
  294. assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
  295. a = {[0] = "ban"}
  296. assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil)
  297. table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
  298. assert(table.remove(a) == 10)
  299. assert(table.remove(a) == 20)
  300. assert(table.remove(a) == -1)
  301. assert(table.remove(a) == nil)
  302. a = {'c', 'd'}
  303. table.insert(a, 3, 'a')
  304. table.insert(a, 'b')
  305. assert(table.remove(a, 1) == 'c')
  306. assert(table.remove(a, 1) == 'd')
  307. assert(table.remove(a, 1) == 'a')
  308. assert(table.remove(a, 1) == 'b')
  309. assert(table.remove(a, 1) == nil)
  310. assert(#a == 0 and a.n == nil)
  311. a = {10,20,30,40}
  312. assert(table.remove(a, #a + 1) == nil)
  313. assert(not pcall(table.remove, a, 0))
  314. assert(a[#a] == 40)
  315. assert(table.remove(a, #a) == 40)
  316. assert(a[#a] == 30)
  317. assert(table.remove(a, 2) == 20)
  318. assert(a[#a] == 30 and #a == 2)
  319. do -- testing table library with metamethods
  320. local function test (proxy, t)
  321. for i = 1, 10 do
  322. table.insert(proxy, 1, i)
  323. end
  324. assert(#proxy == 10 and #t == 10)
  325. for i = 1, 10 do
  326. assert(t[i] == 11 - i)
  327. end
  328. table.sort(proxy)
  329. for i = 1, 10 do
  330. assert(t[i] == i and proxy[i] == i)
  331. end
  332. assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
  333. for i = 1, 8 do
  334. assert(table.remove(proxy, 1) == i)
  335. end
  336. assert(#proxy == 2 and #t == 2)
  337. local a, b, c = table.unpack(proxy)
  338. assert(a == 9 and b == 10 and c == nil)
  339. end
  340. -- all virtual
  341. local t = {}
  342. local proxy = setmetatable({}, {
  343. __len = function () return #t end,
  344. __index = t,
  345. __newindex = t,
  346. })
  347. test(proxy, t)
  348. -- only __newindex
  349. local count = 0
  350. t = setmetatable({}, {
  351. __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
  352. test(t, t)
  353. assert(count == 10) -- after first 10, all other sets are not new
  354. -- no __newindex
  355. t = setmetatable({}, {
  356. __index = function (_,k) return k + 1 end,
  357. __len = function (_) return 5 end})
  358. assert(table.concat(t, ";") == "2;3;4;5;6")
  359. end
  360. if not T then
  361. (Message or print)
  362. ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
  363. else --[
  364. local debug = require'debug'
  365. local tab = {10, 20, 30}
  366. local mt = {}
  367. local u = T.newuserdata(0)
  368. checkerror("table expected", table.insert, u, 40)
  369. checkerror("table expected", table.remove, u)
  370. debug.setmetatable(u, mt)
  371. checkerror("table expected", table.insert, u, 40)
  372. checkerror("table expected", table.remove, u)
  373. mt.__index = tab
  374. checkerror("table expected", table.insert, u, 40)
  375. checkerror("table expected", table.remove, u)
  376. mt.__newindex = tab
  377. checkerror("table expected", table.insert, u, 40)
  378. checkerror("table expected", table.remove, u)
  379. mt.__len = function () return #tab end
  380. table.insert(u, 40)
  381. assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
  382. assert(table.remove(u) == 40)
  383. table.insert(u, 1, 50)
  384. assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
  385. mt.__newindex = nil
  386. mt.__len = nil
  387. local tab2 = {}
  388. local u2 = T.newuserdata(0)
  389. debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
  390. table.move(u, 1, 4, 1, u2)
  391. assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
  392. end -- ]
  393. print('+')
  394. a = {}
  395. for i=1,1000 do
  396. a[i] = i; a[i-1] = nil
  397. end
  398. assert(next(a,nil) == 1000 and next(a,1000) == nil)
  399. assert(next({}) == nil)
  400. assert(next({}, nil) == nil)
  401. for a,b in pairs{} do error"not here" end
  402. for i=1,0 do error'not here' end
  403. for i=0,1,-1 do error'not here' end
  404. a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
  405. a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
  406. do
  407. print("testing floats in numeric for")
  408. local a
  409. -- integer count
  410. a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
  411. a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
  412. a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
  413. a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
  414. a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
  415. -- float count
  416. a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
  417. a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
  418. a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
  419. a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
  420. a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
  421. a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
  422. a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
  423. end
  424. -- conversion
  425. a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
  426. do -- checking types
  427. local c
  428. local function checkfloat (i)
  429. assert(math.type(i) == "float")
  430. c = c + 1
  431. end
  432. c = 0; for i = 1.0, 10 do checkfloat(i) end
  433. assert(c == 10)
  434. c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
  435. assert(c == 10)
  436. local function checkint (i)
  437. assert(math.type(i) == "integer")
  438. c = c + 1
  439. end
  440. local m = math.maxinteger
  441. c = 0; for i = m, m - 10, -1 do checkint(i) end
  442. assert(c == 11)
  443. c = 0; for i = 1, 10.9 do checkint(i) end
  444. assert(c == 10)
  445. c = 0; for i = 10, 0.001, -1 do checkint(i) end
  446. assert(c == 10)
  447. c = 0; for i = 1, "10.8" do checkint(i) end
  448. assert(c == 10)
  449. c = 0; for i = 9, "3.4", -1 do checkint(i) end
  450. assert(c == 6)
  451. c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end
  452. assert(c == 4)
  453. c = 0; for i = 100, "96.3", -2 do checkint(i) end
  454. assert(c == 2)
  455. c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
  456. assert(c == 10)
  457. c = 0; for i = -1, -math.huge, -1 do
  458. if i < -10 then break end; checkint(i)
  459. end
  460. assert(c == 10)
  461. for i = math.mininteger, -10e100 do assert(false) end
  462. for i = math.maxinteger, 10e100, -1 do assert(false) end
  463. end
  464. collectgarbage()
  465. -- testing generic 'for'
  466. local function f (n, p)
  467. local t = {}; for i=1,p do t[i] = i*10 end
  468. return function (_,n)
  469. if n > 0 then
  470. n = n-1
  471. return n, table.unpack(t)
  472. end
  473. end, nil, n
  474. end
  475. local x = 0
  476. for n,a,b,c,d in f(5,3) do
  477. x = x+1
  478. assert(a == 10 and b == 20 and c == 30 and d == nil)
  479. end
  480. assert(x == 5)
  481. -- testing __pairs and __ipairs metamethod
  482. a = {}
  483. do
  484. local x,y,z = pairs(a)
  485. assert(type(x) == 'function' and y == a and z == nil)
  486. end
  487. local function foo (e,i)
  488. assert(e == a)
  489. if i <= 10 then return i+1, i+2 end
  490. end
  491. local function foo1 (e,i)
  492. i = i + 1
  493. assert(e == a)
  494. if i <= e.n then return i,a[i] end
  495. end
  496. setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
  497. local i = 0
  498. for k,v in pairs(a) do
  499. i = i + 1
  500. assert(k == i and v == k+1)
  501. end
  502. a.n = 5
  503. a[3] = 30
  504. -- testing ipairs with metamethods
  505. a = {n=10}
  506. setmetatable(a, { __index = function (t,k)
  507. if k <= t.n then return k * 10 end
  508. end})
  509. i = 0
  510. for k,v in ipairs(a) do
  511. i = i + 1
  512. assert(k == i and v == i * 10)
  513. end
  514. assert(i == a.n)
  515. print"OK"