2
0

nextvar.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. print('testing tables, next, and for')
  2. local a = {}
  3. -- make sure table has lots of space in hash part
  4. for i=1,100 do a[i.."+"] = true end
  5. for i=1,100 do a[i.."+"] = nil end
  6. -- fill hash part with numeric indices testing size operator
  7. for i=1,100 do
  8. a[i] = true
  9. assert(#a == i)
  10. end
  11. -- testing ipairs
  12. local x = 0
  13. for k,v in ipairs{10,20,30;x=12} do
  14. x = x + 1
  15. assert(k == x and v == x * 10)
  16. end
  17. for _ in ipairs{x=12, y=24} do assert(nil) end
  18. -- test for 'false' x ipair
  19. x = false
  20. local i = 0
  21. for k,v in ipairs{true,false,true,false} do
  22. i = i + 1
  23. x = not x
  24. assert(x == v)
  25. end
  26. assert(i == 4)
  27. -- iterator function is always the same
  28. assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
  29. if T then --[
  30. -- testing table sizes
  31. local function log2 (x) return math.log(x, 2) end
  32. local function mp2 (n) -- minimum power of 2 >= n
  33. local mp = 2^math.ceil(log2(n))
  34. assert(n == 0 or (mp/2 < n and n <= mp))
  35. return mp
  36. end
  37. local function fb (n)
  38. local r, nn = T.int2fb(n)
  39. assert(r < 256)
  40. return nn
  41. end
  42. -- test fb function
  43. local a = 1
  44. local lim = 2^30
  45. while a < lim do
  46. local n = fb(a)
  47. assert(a <= n and n <= a*1.125)
  48. a = math.ceil(a*1.3)
  49. end
  50. local function check (t, na, nh)
  51. local a, h = T.querytab(t)
  52. if a ~= na or h ~= nh then
  53. print(na, nh, a, h)
  54. assert(nil)
  55. end
  56. end
  57. -- testing C library sizes
  58. do
  59. local s = 0
  60. for _ in pairs(math) do s = s + 1 end
  61. check(math, 0, mp2(s))
  62. end
  63. -- testing constructor sizes
  64. local lim = 40
  65. local s = 'return {'
  66. for i=1,lim do
  67. s = s..i..','
  68. local s = s
  69. for k=0,lim do
  70. local t = load(s..'}')()
  71. assert(#t == i)
  72. check(t, fb(i), mp2(k))
  73. s = string.format('%sa%d=%d,', s, k, k)
  74. end
  75. end
  76. -- tests with unknown number of elements
  77. local a = {}
  78. for i=1,lim do a[i] = i end -- build auxiliary table
  79. for k=0,lim do
  80. local a = {table.unpack(a,1,k)}
  81. assert(#a == k)
  82. check(a, k, 0)
  83. a = {1,2,3,table.unpack(a,1,k)}
  84. check(a, k+3, 0)
  85. assert(#a == k + 3)
  86. end
  87. -- testing tables dynamically built
  88. local lim = 130
  89. local a = {}; a[2] = 1; check(a, 0, 1)
  90. a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
  91. a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
  92. a = {}
  93. for i = 1,lim do
  94. a[i] = 1
  95. assert(#a == i)
  96. check(a, mp2(i), 0)
  97. end
  98. a = {}
  99. for i = 1,lim do
  100. a['a'..i] = 1
  101. assert(#a == 0)
  102. check(a, 0, mp2(i))
  103. end
  104. a = {}
  105. for i=1,16 do a[i] = i end
  106. check(a, 16, 0)
  107. if not _port then
  108. for i=1,11 do a[i] = nil end
  109. for i=30,50 do a[i] = nil end -- force a rehash (?)
  110. check(a, 0, 8) -- only 5 elements in the table
  111. a[10] = 1
  112. for i=30,50 do a[i] = nil end -- force a rehash (?)
  113. check(a, 0, 8) -- only 6 elements in the table
  114. for i=1,14 do a[i] = nil end
  115. for i=18,50 do a[i] = nil end -- force a rehash (?)
  116. check(a, 0, 4) -- only 2 elements ([15] and [16])
  117. end
  118. -- reverse filling
  119. for i=1,lim do
  120. local a = {}
  121. for i=i,1,-1 do a[i] = i end -- fill in reverse
  122. check(a, mp2(i), 0)
  123. end
  124. -- size tests for vararg
  125. lim = 35
  126. function foo (n, ...)
  127. local arg = {...}
  128. check(arg, n, 0)
  129. assert(select('#', ...) == n)
  130. arg[n+1] = true
  131. check(arg, mp2(n+1), 0)
  132. arg.x = true
  133. check(arg, mp2(n+1), 1)
  134. end
  135. local a = {}
  136. for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
  137. end --]
  138. -- test size operation on empty tables
  139. assert(#{} == 0)
  140. assert(#{nil} == 0)
  141. assert(#{nil, nil} == 0)
  142. assert(#{nil, nil, nil} == 0)
  143. assert(#{nil, nil, nil, nil} == 0)
  144. print'+'
  145. local nofind = {}
  146. a,b,c = 1,2,3
  147. a,b,c = nil
  148. -- next uses always the same iteraction function
  149. assert(next{} == next{})
  150. local function find (name)
  151. local n,v
  152. while 1 do
  153. n,v = next(_G, n)
  154. if not n then return nofind end
  155. assert(v ~= nil)
  156. if n == name then return v end
  157. end
  158. end
  159. local function find1 (name)
  160. for n,v in pairs(_G) do
  161. if n==name then return v end
  162. end
  163. return nil -- not found
  164. end
  165. assert(print==find("print") and print == find1("print"))
  166. assert(_G["print"]==find("print"))
  167. assert(assert==find1("assert"))
  168. assert(nofind==find("return"))
  169. assert(not find1("return"))
  170. _G["ret" .. "urn"] = nil
  171. assert(nofind==find("return"))
  172. _G["xxx"] = 1
  173. assert(xxx==find("xxx"))
  174. print('+')
  175. a = {}
  176. for i=0,10000 do
  177. if math.fmod(i,10) ~= 0 then
  178. a['x'..i] = i
  179. end
  180. end
  181. n = {n=0}
  182. for i,v in pairs(a) do
  183. n.n = n.n+1
  184. assert(i and v and a[i] == v)
  185. end
  186. assert(n.n == 9000)
  187. a = nil
  188. -- do -- clear global table
  189. -- local a = {}
  190. -- for n,v in pairs(_G) do a[n]=v end
  191. -- for n,v in pairs(a) do
  192. -- if not package.loaded[n] and type(v) ~= "function" and
  193. -- not string.find(n, "^[%u_]") then
  194. -- _G[n] = nil
  195. -- end
  196. -- collectgarbage()
  197. -- end
  198. -- end
  199. --
  200. local function checknext (a)
  201. local b = {}
  202. do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
  203. for k,v in pairs(b) do assert(a[k] == v) end
  204. for k,v in pairs(a) do assert(b[k] == v) end
  205. end
  206. checknext{1,x=1,y=2,z=3}
  207. checknext{1,2,x=1,y=2,z=3}
  208. checknext{1,2,3,x=1,y=2,z=3}
  209. checknext{1,2,3,4,x=1,y=2,z=3}
  210. checknext{1,2,3,4,5,x=1,y=2,z=3}
  211. assert(#{} == 0)
  212. assert(#{[-1] = 2} == 0)
  213. assert(#{1,2,3,nil,nil} == 3)
  214. for i=0,40 do
  215. local a = {}
  216. for j=1,i do a[j]=j end
  217. assert(#a == i)
  218. end
  219. -- 'maxn' is now deprecated, but it is easily defined in Lua
  220. function table.maxn (t)
  221. local max = 0
  222. for k in pairs(t) do
  223. max = (type(k) == 'number') and math.max(max, k) or max
  224. end
  225. return max
  226. end
  227. assert(table.maxn{} == 0)
  228. assert(table.maxn{["1000"] = true} == 0)
  229. assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
  230. assert(table.maxn{[1000] = true} == 1000)
  231. assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
  232. table.maxn = nil
  233. -- int overflow
  234. a = {}
  235. for i=0,50 do a[math.pow(2,i)] = true end
  236. assert(a[#a])
  237. print('+')
  238. -- erasing values
  239. local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
  240. [100.3] = 4, [4] = 5}
  241. local n = 0
  242. for k, v in pairs(t) do
  243. print(k, v)
  244. n = n+1
  245. assert(t[k] == v)
  246. t[k] = nil
  247. collectgarbage()
  248. assert(t[k] == nil)
  249. end
  250. assert(n == 5)
  251. local function test (a)
  252. assert(not pcall(table.insert, a, 2, 20));
  253. table.insert(a, 10); table.insert(a, 2, 20);
  254. table.insert(a, 1, -1); table.insert(a, 40);
  255. table.insert(a, #a+1, 50)
  256. table.insert(a, 2, -2)
  257. assert(not pcall(table.insert, a, 0, 20));
  258. assert(not pcall(table.insert, a, #a + 2, 20));
  259. assert(table.remove(a,1) == -1)
  260. assert(table.remove(a,1) == -2)
  261. assert(table.remove(a,1) == 10)
  262. assert(table.remove(a,1) == 20)
  263. assert(table.remove(a,1) == 40)
  264. assert(table.remove(a,1) == 50)
  265. assert(table.remove(a,1) == nil)
  266. assert(table.remove(a) == nil)
  267. -- assert(table.remove(a, #a) == nil)
  268. end
  269. a = {n=0, [-7] = "ban"}
  270. test(a)
  271. assert(a.n == 0 and a[-7] == "ban")
  272. a = {[-7] = "ban"};
  273. test(a)
  274. assert(a.n == nil and #a == 0 and a[-7] == "ban")
  275. a = {[-1] = "ban"}
  276. test(a)
  277. assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
  278. -- a = {[0] = "ban"}
  279. -- assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil)
  280. table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
  281. assert(table.remove(a) == 10)
  282. assert(table.remove(a) == 20)
  283. assert(table.remove(a) == -1)
  284. assert(table.remove(a) == nil)
  285. a = {'c', 'd'}
  286. table.insert(a, 3, 'a')
  287. table.insert(a, 'b')
  288. assert(table.remove(a, 1) == 'c')
  289. assert(table.remove(a, 1) == 'd')
  290. assert(table.remove(a, 1) == 'a')
  291. assert(table.remove(a, 1) == 'b')
  292. assert(table.remove(a, 1) == nil)
  293. assert(#a == 0 and a.n == nil)
  294. a = {10,20,30,40}
  295. assert(table.remove(a, #a + 1) == nil)
  296. assert(not pcall(table.remove, a, 0))
  297. assert(a[#a] == 40)
  298. assert(table.remove(a, #a) == 40)
  299. assert(a[#a] == 30)
  300. assert(table.remove(a, 2) == 20)
  301. assert(a[#a] == 30 and #a == 2)
  302. print('+')
  303. a = {}
  304. for i=1,1000 do
  305. a[i] = i; a[i-1] = nil
  306. end
  307. assert(next(a,nil) == 1000 and next(a,1000) == nil)
  308. assert(next({}) == nil)
  309. assert(next({}, nil) == nil)
  310. for a,b in pairs{} do error"not here" end
  311. for i=1,0 do error'not here' end
  312. for i=0,1,-1 do error'not here' end
  313. a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
  314. a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
  315. if not _port then
  316. print("testing precision in numeric for")
  317. local a = 0; for i=0, 1, 0.1 do a=a+1 end; assert(a==11)
  318. a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
  319. a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
  320. a = 0; for i=1e10, 1e10, -1 do a=a+1 end; assert(a==1)
  321. a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
  322. a = 0; for i=99999, 1e5, -1 do a=a+1 end; assert(a==0)
  323. a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
  324. end
  325. -- conversion
  326. a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
  327. collectgarbage()
  328. -- testing generic 'for'
  329. local function f (n, p)
  330. local t = {}; for i=1,p do t[i] = i*10 end
  331. return function (_,n)
  332. if n > 0 then
  333. n = n-1
  334. return n, table.unpack(t)
  335. end
  336. end, nil, n
  337. end
  338. local x = 0
  339. for n,a,b,c,d in f(5,3) do
  340. x = x+1
  341. assert(a == 10 and b == 20 and c == 30 and d == nil)
  342. end
  343. assert(x == 5)
  344. -- testing __pairs and __ipairs metamethod
  345. a = {}
  346. do
  347. local x,y,z = pairs(a)
  348. assert(type(x) == 'function' and y == a and z == nil)
  349. end
  350. local function foo (e,i)
  351. assert(e == a)
  352. if i <= 10 then return i+1, i+2 end
  353. end
  354. local function foo1 (e,i)
  355. i = i + 1
  356. assert(e == a)
  357. if i <= e.n then return i,a[i] end
  358. end
  359. setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
  360. local i = 0
  361. for k,v in pairs(a) do
  362. i = i + 1
  363. assert(k == i and v == k+1)
  364. end
  365. a.n = 5
  366. a[3] = 30
  367. a = {n=10}
  368. setmetatable(a, {__len = function (x) return x.n end,
  369. __ipairs = function (x) return function (e,i)
  370. if i < #e then return i+1 end
  371. end, x, 0 end})
  372. i = 0
  373. for k,v in ipairs(a) do
  374. i = i + 1
  375. assert(k == i and v == nil)
  376. end
  377. assert(i == a.n)
  378. print"OK"