nextvar.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. if T then
  12. -- testing table sizes
  13. local l2 = math.log(2)
  14. local function log2 (x) return math.log(x)/l2 end
  15. local function mp2 (n) -- minimum power of 2 >= n
  16. local mp = 2^math.ceil(log2(n))
  17. assert(n == 0 or (mp/2 < n and n <= mp))
  18. return mp
  19. end
  20. local function fb (n)
  21. local r, nn = T.int2fb(n)
  22. assert(r < 256)
  23. return nn
  24. end
  25. -- test fb function
  26. local a = 1
  27. local lim = 2^30
  28. while a < lim do
  29. local n = fb(a)
  30. assert(a <= n and n <= a*1.125)
  31. a = math.ceil(a*1.3)
  32. end
  33. local function check (t, na, nh)
  34. local a, h = T.querytab(t)
  35. if a ~= na or h ~= nh then
  36. print(na, nh, a, h)
  37. assert(nil)
  38. end
  39. end
  40. -- testing constructor sizes
  41. local lim = 40
  42. local s = 'return {'
  43. for i=1,lim do
  44. s = s..i..','
  45. local s = s
  46. for k=0,lim do
  47. local t = loadstring(s..'}')()
  48. assert(#t == i)
  49. check(t, fb(i), mp2(k))
  50. s = string.format('%sa%d=%d,', s, k, k)
  51. end
  52. end
  53. -- tests with unknown number of elements
  54. local a = {}
  55. for i=1,lim do a[i] = i end -- build auxiliary table
  56. for k=0,lim do
  57. local a = {unpack(a,1,k)}
  58. assert(#a == k)
  59. check(a, k, 0)
  60. a = {1,2,3,unpack(a,1,k)}
  61. check(a, k+3, 0)
  62. assert(#a == k + 3)
  63. end
  64. print'+'
  65. -- testing tables dynamically built
  66. local lim = 130
  67. local a = {}; a[2] = 1; check(a, 0, 1)
  68. a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
  69. a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
  70. a = {}
  71. for i = 1,lim do
  72. a[i] = 1
  73. assert(#a == i)
  74. check(a, mp2(i), 0)
  75. end
  76. a = {}
  77. for i = 1,lim do
  78. a['a'..i] = 1
  79. assert(#a == 0)
  80. check(a, 0, mp2(i))
  81. end
  82. a = {}
  83. for i=1,16 do a[i] = i end
  84. check(a, 16, 0)
  85. for i=1,11 do a[i] = nil end
  86. for i=30,40 do a[i] = nil end -- force a rehash (?)
  87. check(a, 0, 8)
  88. a[10] = 1
  89. for i=30,40 do a[i] = nil end -- force a rehash (?)
  90. check(a, 0, 8)
  91. for i=1,14 do a[i] = nil end
  92. for i=30,50 do a[i] = nil end -- force a rehash (?)
  93. check(a, 0, 4)
  94. -- reverse filling
  95. for i=1,lim do
  96. local a = {}
  97. for i=i,1,-1 do a[i] = i end -- fill in reverse
  98. check(a, mp2(i), 0)
  99. end
  100. -- size tests for vararg
  101. lim = 35
  102. function foo (n, ...)
  103. local arg = {...}
  104. check(arg, n, 0)
  105. assert(select('#', ...) == n)
  106. arg[n+1] = true
  107. check(arg, mp2(n+1), 0)
  108. arg.x = true
  109. check(arg, mp2(n+1), 1)
  110. end
  111. local a = {}
  112. for i=1,lim do a[i] = true; foo(i, unpack(a)) end
  113. end
  114. -- test size operation on empty tables
  115. assert(#{} == 0)
  116. assert(#{nil} == 0)
  117. assert(#{nil, nil} == 0)
  118. assert(#{nil, nil, nil} == 0)
  119. assert(#{nil, nil, nil, nil} == 0)
  120. print'+'
  121. local nofind = {}
  122. a,b,c = 1,2,3
  123. a,b,c = nil
  124. local function find (name)
  125. local n,v
  126. while 1 do
  127. n,v = next(_G, n)
  128. if not n then return nofind end
  129. assert(v ~= nil)
  130. if n == name then return v end
  131. end
  132. end
  133. local function find1 (name)
  134. for n,v in pairs(_G) do
  135. if n==name then return v end
  136. end
  137. return nil -- not found
  138. end
  139. do -- create 10000 new global variables
  140. for i=1,10000 do _G[i] = i end
  141. end
  142. a = {x=90, y=8, z=23}
  143. assert(table.foreach(a, function(i,v) if i=='x' then return v end end) == 90)
  144. assert(table.foreach(a, function(i,v) if i=='a' then return v end end) == nil)
  145. table.foreach({}, error)
  146. table.foreachi({x=10, y=20}, error)
  147. local a = {n = 1}
  148. table.foreachi({n=3}, function (i, v)
  149. assert(a.n == i and not v)
  150. a.n=a.n+1
  151. end)
  152. a = {10,20,30,nil,50}
  153. table.foreachi(a, function (i,v) assert(a[i] == v) end)
  154. assert(table.foreachi({'a', 'b', 'c'}, function (i,v)
  155. if i==2 then return v end
  156. end) == 'b')
  157. assert(print==find("print") and print == find1("print"))
  158. assert(_G["print"]==find("print"))
  159. assert(assert==find1("assert"))
  160. assert(nofind==find("return"))
  161. assert(not find1("return"))
  162. _G["ret" .. "urn"] = nil
  163. assert(nofind==find("return"))
  164. _G["xxx"] = 1
  165. assert(xxx==find("xxx"))
  166. print('+')
  167. a = {}
  168. for i=0,10000 do
  169. if math.mod(i,10) ~= 0 then
  170. a['x'..i] = i
  171. end
  172. end
  173. n = {n=0}
  174. for i,v in pairs(a) do
  175. n.n = n.n+1
  176. assert(i and v and a[i] == v)
  177. end
  178. assert(n.n == 9000)
  179. a = nil
  180. -- remove those 10000 new global variables
  181. for i=1,10000 do _G[i] = nil end
  182. do -- clear global table
  183. local a = {}
  184. local preserve = {io = 1, string = 1, debug = 1, os = 1,
  185. coroutine = 1, table = 1, math = 1}
  186. for n,v in pairs(_G) do a[n]=v end
  187. for n,v in pairs(a) do
  188. if not preserve[n] and type(v) ~= "function" and
  189. not string.find(n, "^[%u_]") then
  190. _G[n] = nil
  191. end
  192. collectgarbage()
  193. end
  194. end
  195. local function foo ()
  196. local getfenv, setfenv, assert, next =
  197. getfenv, setfenv, assert, next
  198. local n = {gl1=3}
  199. setfenv(foo, n)
  200. assert(getfenv(foo) == getfenv(1))
  201. assert(getfenv(foo) == n)
  202. assert(print == nil and gl1 == 3)
  203. gl1 = nil
  204. gl = 1
  205. assert(n.gl == 1 and next(n, 'gl') == nil)
  206. end
  207. foo()
  208. print'+'
  209. local function checknext (a)
  210. local b = {}
  211. table.foreach(a, function (k,v) b[k] = v end)
  212. for k,v in pairs(b) do assert(a[k] == v) end
  213. for k,v in pairs(a) do assert(b[k] == v) end
  214. b = {}
  215. do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
  216. for k,v in pairs(b) do assert(a[k] == v) end
  217. for k,v in pairs(a) do assert(b[k] == v) end
  218. end
  219. checknext{1,x=1,y=2,z=3}
  220. checknext{1,2,x=1,y=2,z=3}
  221. checknext{1,2,3,x=1,y=2,z=3}
  222. checknext{1,2,3,4,x=1,y=2,z=3}
  223. checknext{1,2,3,4,5,x=1,y=2,z=3}
  224. assert(table.getn{} == 0)
  225. assert(table.getn{[-1] = 2} == 0)
  226. assert(table.getn{1,2,3,nil,nil} == 3)
  227. for i=0,40 do
  228. local a = {}
  229. for j=1,i do a[j]=j end
  230. assert(table.getn(a) == i)
  231. end
  232. assert(table.maxn{} == 0)
  233. assert(table.maxn{["1000"] = true} == 0)
  234. assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
  235. assert(table.maxn{[1000] = true} == 1000)
  236. assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
  237. -- int overflow
  238. a = {}
  239. for i=0,50 do a[math.pow(2,i)] = true end
  240. assert(a[table.getn(a)])
  241. print("+")
  242. -- erasing values
  243. local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
  244. [100.3] = 4, [4] = 5}
  245. local n = 0
  246. for k, v in pairs( t ) do
  247. n = n+1
  248. assert(t[k] == v)
  249. t[k] = nil
  250. collectgarbage()
  251. assert(t[k] == nil)
  252. end
  253. assert(n == 5)
  254. local function test (a)
  255. table.insert(a, 10); table.insert(a, 2, 20);
  256. table.insert(a, 1, -1); table.insert(a, 40);
  257. table.insert(a, table.getn(a)+1, 50)
  258. table.insert(a, 2, -2)
  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. end
  267. a = {n=0, [-7] = "ban"}
  268. test(a)
  269. assert(a.n == 0 and a[-7] == "ban")
  270. a = {[-7] = "ban"};
  271. test(a)
  272. assert(a.n == nil and table.getn(a) == 0 and a[-7] == "ban")
  273. table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
  274. assert(table.remove(a) == 10)
  275. assert(table.remove(a) == 20)
  276. assert(table.remove(a) == -1)
  277. a = {'c', 'd'}
  278. table.insert(a, 3, 'a')
  279. table.insert(a, 'b')
  280. assert(table.remove(a, 1) == 'c')
  281. assert(table.remove(a, 1) == 'd')
  282. assert(table.remove(a, 1) == 'a')
  283. assert(table.remove(a, 1) == 'b')
  284. assert(table.getn(a) == 0 and a.n == nil)
  285. print("+")
  286. a = {}
  287. for i=1,1000 do
  288. a[i] = i; a[i-1] = nil
  289. end
  290. assert(next(a,nil) == 1000 and next(a,1000) == nil)
  291. assert(next({}) == nil)
  292. assert(next({}, nil) == nil)
  293. for a,b in pairs{} do error"not here" end
  294. for i=1,0 do error'not here' end
  295. for i=0,1,-1 do error'not here' end
  296. a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
  297. a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
  298. a = 0; for i=0, 1, 0.1 do a=a+1 end; assert(a==11)
  299. -- precision problems
  300. --a = 0; for i=1, 0, -0.01 do a=a+1 end; assert(a==101)
  301. a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
  302. a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
  303. a = 0; for i=1e10, 1e10, -1 do a=a+1 end; assert(a==1)
  304. a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
  305. a = 0; for i=99999, 1e5, -1 do a=a+1 end; assert(a==0)
  306. a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
  307. -- conversion
  308. a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
  309. collectgarbage()
  310. -- testing generic 'for'
  311. local function f (n, p)
  312. local t = {}; for i=1,p do t[i] = i*10 end
  313. return function (_,n)
  314. if n > 0 then
  315. n = n-1
  316. return n, unpack(t)
  317. end
  318. end, nil, n
  319. end
  320. local x = 0
  321. for n,a,b,c,d in f(5,3) do
  322. x = x+1
  323. assert(a == 10 and b == 20 and c == 30 and d == nil)
  324. end
  325. assert(x == 5)
  326. print"OK"