api.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. if T==nil then
  2. (Message or print)('\a\n >>> testC not active: skipping API tests <<<\n\a')
  3. return
  4. end
  5. function tcheck (t1, t2)
  6. table.remove(t1, 1) -- remove code
  7. assert(table.getn(t1) == table.getn(t2))
  8. for i=1,table.getn(t1) do assert(t1[i] == t2[i]) end
  9. end
  10. function pack(...) return arg end
  11. print('testing C API')
  12. -- testing allignment
  13. a = T.d2s(12458954321123)
  14. assert(string.len(a) == 8) -- sizeof(double)
  15. assert(T.s2d(a) == 12458954321123)
  16. a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
  17. assert(a == 2 and b == 3 and not c)
  18. -- test that all trues are equal
  19. a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
  20. assert(a == b and a == true and c == false)
  21. a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
  22. tobool -3; tobool -3; tobool -3; return 3"
  23. assert(a==0 and b==1 and c==0)
  24. a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
  25. assert(a == 40 and b == 5 and not c)
  26. t = pack(T.testC("settop 5; gettop; return .", 2, 3))
  27. tcheck(t, {n=4,2,3})
  28. t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
  29. assert(t.n == 10 and t[1] == nil and t[10] == nil)
  30. t = pack(T.testC("remove -2; gettop; return .", 2, 3, 4))
  31. tcheck(t, {n=2,2,4})
  32. t = pack(T.testC("insert -1; gettop; return .", 2, 3))
  33. tcheck(t, {n=2,2,3})
  34. t = pack(T.testC("insert 3; gettop; return .", 2, 3, 4, 5))
  35. tcheck(t, {n=4,2,5,3,4})
  36. t = pack(T.testC("replace 2; gettop; return .", 2, 3, 4, 5))
  37. tcheck(t, {n=3,5,3,4})
  38. t = pack(T.testC("replace -2; gettop; return .", 2, 3, 4, 5))
  39. tcheck(t, {n=3,2,3,5})
  40. t = pack(T.testC("remove 3; gettop; return .", 2, 3, 4, 5))
  41. tcheck(t, {n=3,2,4,5})
  42. t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
  43. insert 2; pushvalue 1; remove 1; insert 1; \
  44. insert -2; pushvalue -2; remove -3; gettop; return .",
  45. 2, 3, 4, 5, 10, 40, 90))
  46. tcheck(t, {n=7,2,3,4,5,10,40,90})
  47. t = pack(T.testC("concat 5; gettop; return .", "alo", 2, 3, "joao", 12))
  48. tcheck(t, {n=1,"alo23joao12"})
  49. -- testing MULTRET
  50. t = pack(T.testC("rawcall 2,-1; gettop; return .",
  51. function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
  52. tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
  53. do -- test returning more results than fit in the caller stack
  54. local a = {}
  55. for i=1,1000 do a[i] = true end; a[999] = 10
  56. local b = T.testC([[call 1 -1; pop 1; tostring -1; return 1]], unpack, a)
  57. assert(b == "10")
  58. end
  59. -- testing lessthan
  60. assert(T.testC("lessthan 2 5, return 1", 3, 2, 2, 4, 2, 2))
  61. assert(T.testC("lessthan 5 2, return 1", 4, 2, 2, 3, 2, 2))
  62. assert(not T.testC("lessthan 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
  63. assert(not T.testC("lessthan -3 2, return 1", "3", "2", "2", "4", "2", "2"))
  64. local b = {__lt = function (a,b) return a[1] < b[1] end}
  65. local a1,a3,a4 = setmetatable({1}, b),
  66. setmetatable({3}, b),
  67. setmetatable({4}, b)
  68. assert(T.testC("lessthan 2 5, return 1", a3, 2, 2, a4, 2, 2))
  69. assert(T.testC("lessthan 5 -6, return 1", a4, 2, 2, a3, 2, 2))
  70. a,b = T.testC("lessthan 5 -6, return 2", a1, 2, 2, a3, 2, 20)
  71. assert(a == 20 and b == false)
  72. -- testing lua_is
  73. function count (x, n)
  74. n = n or 2
  75. local prog = [[
  76. isnumber %d;
  77. isstring %d;
  78. isfunction %d;
  79. iscfunction %d;
  80. istable %d;
  81. isuserdata %d;
  82. isnil %d;
  83. isnull %d;
  84. return 8
  85. ]]
  86. prog = string.format(prog, n, n, n, n, n, n, n, n)
  87. local a,b,c,d,e,f,g,h = T.testC(prog, x)
  88. return a+b+c+d+e+f+g+(100*h)
  89. end
  90. assert(count(3) == 2)
  91. assert(count('alo') == 1)
  92. assert(count('32') == 2)
  93. assert(count({}) == 1)
  94. assert(count(print) == 2)
  95. assert(count(function () end) == 1)
  96. assert(count(nil) == 1)
  97. assert(count(io.stdin) == 1)
  98. assert(count(nil, 15) == 100)
  99. -- testing lua_to...
  100. function to (s, x, n)
  101. n = n or 2
  102. return T.testC(string.format("%s %d; return 1", s, n), x)
  103. end
  104. assert(to("tostring", {}) == nil)
  105. assert(to("tostring", "alo") == "alo")
  106. assert(to("tostring", 12) == "12")
  107. assert(to("tostring", 12, 3) == nil)
  108. assert(to("objsize", {}) == 0)
  109. assert(to("objsize", "alo\0\0a") == 6)
  110. assert(to("objsize", T.newuserdata(0)) == 0)
  111. assert(to("objsize", T.newuserdata(101)) == 101)
  112. assert(to("objsize", 12) == 2)
  113. assert(to("objsize", 12, 3) == 0)
  114. assert(to("tonumber", {}) == 0)
  115. assert(to("tonumber", "12") == 12)
  116. assert(to("tonumber", "s2") == 0)
  117. assert(to("tonumber", 1, 20) == 0)
  118. a = to("tocfunction", math.deg)
  119. assert(a(3) == math.deg(3) and a ~= math.deg)
  120. -- testing errors
  121. a = T.testC([[
  122. loadstring 2; call 0,1;
  123. pushvalue 3; insert -2; call 1, 1;
  124. call 0, 0;
  125. return 1
  126. ]], "x=150", function (a) assert(a==nil); return 3 end)
  127. assert(type(a) == 'string' and x == 150)
  128. function check3(p, ...)
  129. assert(arg.n == 3)
  130. assert(string.find(arg[3], p))
  131. end
  132. check3(":1:", T.testC("loadstring 2; gettop; return .", "x="))
  133. check3("cannot read", T.testC("loadfile 2; gettop; return .", "."))
  134. check3("cannot open xxxx", T.testC("loadfile 2; gettop; return .", "xxxx"))
  135. -- testing table access
  136. a = {x=0, y=12}
  137. x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
  138. a, 3, "y", 4, "x")
  139. assert(x == 0 and y == 12)
  140. T.testC("settable -5", a, 3, 4, "x", 15)
  141. assert(a.x == 15)
  142. a[a] = print
  143. x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
  144. assert(x == print)
  145. T.testC("settable 2", a, "x") -- table and key are the same object!
  146. assert(a[a] == "x")
  147. b = setmetatable({p = a}, {})
  148. getmetatable(b).__index = function (t, i) return t.p[i] end
  149. k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
  150. assert(x == 15 and k == 35)
  151. getmetatable(b).__index = function (t, i) return a[i] end
  152. getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
  153. y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
  154. assert(y == 12)
  155. k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
  156. assert(a.x == 16 and k == 4)
  157. a[b] = 'xuxu'
  158. y = T.testC("gettable 2, return 1", b)
  159. assert(y == 'xuxu')
  160. T.testC("settable 2", b, 19)
  161. assert(a[b] == 19)
  162. -- testing next
  163. a = {}
  164. t = pack(T.testC("next; gettop; return .", a, nil))
  165. tcheck(t, {n=1,a})
  166. a = {a=3}
  167. t = pack(T.testC("next; gettop; return .", a, nil))
  168. tcheck(t, {n=3,a,'a',3})
  169. t = pack(T.testC("next; pop 1; next; gettop; return .", a, nil))
  170. tcheck(t, {n=1,a})
  171. -- testing upvalues
  172. do
  173. local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  174. t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
  175. assert(b == 10 and c == 20 and type(t) == 'table')
  176. a, b = A([[tostring U3; tonumber U4; return 2]])
  177. assert(a == nil and b == 0)
  178. A([[pushnum 100; pushnum 200; replace U2; replace U1]])
  179. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  180. assert(b == 100 and c == 200)
  181. A([[replace U2; replace U1]], {x=1}, {x=2})
  182. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  183. assert(b.x == 1 and c.x == 2)
  184. T.checkmemory()
  185. end
  186. local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  187. assert(T.upvalue(f, 1) == 10 and
  188. T.upvalue(f, 2) == 20 and
  189. T.upvalue(f, 3) == nil)
  190. T.upvalue(f, 2, "xuxu")
  191. assert(T.upvalue(f, 2) == "xuxu")
  192. -- testing environments
  193. assert(T.testC"pushvalue G; return 1" == _G)
  194. assert(T.testC"pushvalue E; return 1" == _G)
  195. local a = {}
  196. T.testC("replace E; return 1", a)
  197. assert(T.testC"pushvalue G; return 1" == _G)
  198. assert(T.testC"pushvalue E; return 1" == a)
  199. assert(debug.getfenv(T.testC) == a)
  200. assert(debug.getfenv(T.upvalue) == _G)
  201. -- userdata inherit environment
  202. local u = T.testC"newuserdata 0; return 1"
  203. assert(debug.getfenv(u) == a)
  204. -- functions inherit environment
  205. u = T.testC"pushcclosure 0; return 1"
  206. assert(debug.getfenv(u) == a)
  207. debug.setfenv(T.testC, _G)
  208. assert(T.testC"pushvalue E; return 1" == _G)
  209. local b = newproxy()
  210. assert(debug.getfenv(b) == _G)
  211. assert(debug.setfenv(b, a))
  212. assert(debug.getfenv(b) == a)
  213. -- testing locks (refs)
  214. -- reuse of references
  215. local i = T.ref{}
  216. T.unref(i)
  217. assert(T.ref{} == i)
  218. Arr = {}
  219. Lim = 100
  220. for i=1,Lim do -- lock many objects
  221. Arr[i] = T.ref({})
  222. end
  223. assert(T.ref(nil) == -1 and T.getref(-1) == nil)
  224. T.unref(-1); T.unref(-1)
  225. for i=1,Lim do -- unlock all them
  226. T.unref(Arr[i])
  227. end
  228. function printlocks ()
  229. local n = T.testC("gettable R; return 1", "n")
  230. print("n", n)
  231. for i=0,n do
  232. print(i, T.testC("gettable R; return 1", i))
  233. end
  234. end
  235. for i=1,Lim do -- lock many objects
  236. Arr[i] = T.ref({})
  237. end
  238. for i=1,Lim,2 do -- unlock half of them
  239. T.unref(Arr[i])
  240. end
  241. assert(type(T.getref(Arr[2])) == 'table')
  242. assert(T.getref(-1) == nil)
  243. a = T.ref({})
  244. collectgarbage()
  245. assert(type(T.getref(a)) == 'table')
  246. -- colect in cl the `val' of all collected userdata
  247. tt = {}
  248. cl = {n=0}
  249. A = nil; B = nil
  250. local F
  251. F = function (x)
  252. local udval = T.udataval(x)
  253. table.insert(cl, udval)
  254. local d = T.newuserdata(100) -- cria lixo
  255. d = nil
  256. assert(debug.getmetatable(x).__gc == F)
  257. loadstring("table.insert({}, {})")() -- cria mais lixo
  258. collectgarbage() -- forca coleta de lixo durante coleta!
  259. assert(debug.getmetatable(x).__gc == F) -- coleta anterior nao melou isso?
  260. local dummy = {} -- cria lixo durante coleta
  261. if A ~= nil then
  262. assert(type(A) == "userdata")
  263. assert(T.udataval(A) == B)
  264. debug.getmetatable(A) -- just acess it
  265. end
  266. A = x -- ressucita userdata
  267. B = udval
  268. return 1,2,3
  269. end
  270. tt.__gc = F
  271. -- test whether udate collection frees memory in the right time
  272. do
  273. collectgarbage();
  274. collectgarbage();
  275. local x = collectgarbage("count");
  276. local a = T.newuserdata(5001)
  277. assert(T.testC("objsize 2; return 1", a) == 5001)
  278. assert(collectgarbage("count") >= x+4)
  279. a = nil
  280. collectgarbage();
  281. assert(collectgarbage("count") <= x+1)
  282. -- udata without finalizer
  283. x = collectgarbage("count")
  284. collectgarbage("stop")
  285. for i=1,1000 do newproxy(false) end
  286. assert(collectgarbage("count") > x+10)
  287. collectgarbage()
  288. assert(collectgarbage("count") <= x+1)
  289. -- udata with finalizer
  290. x = collectgarbage("count")
  291. collectgarbage()
  292. collectgarbage("stop")
  293. a = newproxy(true)
  294. getmetatable(a).__gc = function () end
  295. for i=1,1000 do newproxy(a) end
  296. assert(collectgarbage("count") >= x+10)
  297. collectgarbage() -- this collection only calls TM, without freeing memory
  298. assert(collectgarbage("count") >= x+10)
  299. collectgarbage() -- now frees memory
  300. assert(collectgarbage("count") <= x+1)
  301. end
  302. collectgarbage("stop")
  303. -- create 3 userdatas with tag `tt'
  304. a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
  305. b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
  306. c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
  307. -- create userdata without meta table
  308. x = T.newuserdata(4)
  309. y = T.newuserdata(0)
  310. assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
  311. d=T.ref(a);
  312. e=T.ref(b);
  313. f=T.ref(c);
  314. t = {T.getref(d), T.getref(e), T.getref(f)}
  315. assert(t[1] == a and t[2] == b and t[3] == c)
  316. t=nil; a=nil; c=nil;
  317. T.unref(e); T.unref(f)
  318. collectgarbage()
  319. -- check that unref objects have been collected
  320. assert(table.getn(cl) == 1 and cl[1] == nc)
  321. x = T.getref(d)
  322. assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
  323. x =nil
  324. tt.b = b -- create cycle
  325. tt=nil -- frees tt for GC
  326. A = nil
  327. b = nil
  328. T.unref(d);
  329. n5 = T.newuserdata(0)
  330. debug.setmetatable(n5, {__gc=F})
  331. n5 = T.udataval(n5)
  332. collectgarbage()
  333. assert(table.getn(cl) == 4)
  334. -- check order of collection
  335. assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
  336. a, na = {}, {}
  337. for i=30,1,-1 do
  338. a[i] = T.newuserdata(0)
  339. debug.setmetatable(a[i], {__gc=F})
  340. na[i] = T.udataval(a[i])
  341. end
  342. cl = {}
  343. a = nil; collectgarbage()
  344. assert(table.getn(cl) == 30)
  345. for i=1,30 do assert(cl[i] == na[i]) end
  346. na = nil
  347. for i=2,Lim,2 do -- unlock the other half
  348. T.unref(Arr[i])
  349. end
  350. x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
  351. assert(T.testC("objsize 2; return 1", x) == 41)
  352. cl = {}
  353. a = {[x] = 1}
  354. x = T.udataval(x)
  355. collectgarbage()
  356. -- old `x' cannot be collected (`a' still uses it)
  357. assert(table.getn(cl) == 0)
  358. for n in pairs(a) do a[n] = nil end
  359. collectgarbage()
  360. assert(table.getn(cl) == 1 and cl[1] == x) -- old `x' must be collected
  361. -- testing lua_equal
  362. assert(T.testC("equal 2 4; return 1", print, 1, print, 20))
  363. assert(T.testC("equal 3 2; return 1", 'alo', "alo"))
  364. assert(T.testC("equal 2 3; return 1", nil, nil))
  365. assert(not T.testC("equal 2 3; return 1", {}, {}))
  366. assert(not T.testC("equal 2 3; return 1"))
  367. assert(not T.testC("equal 2 3; return 1", 3))
  368. -- testing lua_equal with fallbacks
  369. do
  370. local map = {}
  371. local t = {__eq = function (a,b) return map[a] == map[b] end}
  372. local function f(x)
  373. local u = T.newuserdata(0)
  374. debug.setmetatable(u, t)
  375. map[u] = x
  376. return u
  377. end
  378. assert(f(10) == f(10))
  379. assert(f(10) ~= f(11))
  380. assert(T.testC("equal 2 3; return 1", f(10), f(10)))
  381. assert(not T.testC("equal 2 3; return 1", f(10), f(20)))
  382. t.__eq = nil
  383. assert(f(10) ~= f(10))
  384. end
  385. print'+'
  386. -------------------------------------------------------------------------
  387. do -- testing errors during GC
  388. local a = {}
  389. for i=1,20 do
  390. a[i] = T.newuserdata(i) -- creates several udata
  391. end
  392. for i=1,20,2 do -- mark half of them to raise error during GC
  393. debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end})
  394. end
  395. for i=2,20,2 do -- mark the other half to count and to create more garbage
  396. debug.setmetatable(a[i], {__gc = function (x) loadstring("A=A+1")() end})
  397. end
  398. _G.A = 0
  399. a = 0
  400. while 1 do
  401. if xpcall(collectgarbage, function (s) a=a+1 end) then
  402. break -- stop if no more errors
  403. end
  404. end
  405. assert(a == 10) -- number of errors
  406. assert(A == 10) -- number of normal collections
  407. end
  408. -------------------------------------------------------------------------
  409. -- test for userdata vals
  410. do
  411. local a = {}; local lim = 30
  412. for i=0,lim do a[i] = T.pushuserdata(i) end
  413. for i=0,lim do assert(T.udataval(a[i]) == i) end
  414. for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
  415. for i=0,lim do a[a[i]] = i end
  416. for i=0,lim do a[T.pushuserdata(i)] = i end
  417. assert(type(tostring(a[1])) == "string")
  418. end
  419. -------------------------------------------------------------------------
  420. -- testing multiple states
  421. T.closestate(T.newstate());
  422. L1 = T.newstate()
  423. assert(L1)
  424. assert(pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")).n == 0)
  425. a, b = T.doremote(L1, "return f()")
  426. assert(a == 'alo' and b == '3')
  427. T.doremote(L1, "_ERRORMESSAGE = nil")
  428. -- error: `sin' is not defined
  429. a, b = T.doremote(L1, "return sin(1)")
  430. assert(a == nil and b == 2) -- 2 == run-time error
  431. -- error: syntax error
  432. a, b, c = T.doremote(L1, "return a+")
  433. assert(a == nil and b == 3 and type(c) == "string") -- 3 == syntax error
  434. T.loadlib(L1)
  435. a, b = T.doremote(L1, [[
  436. a = strlibopen()
  437. a = packageopen()
  438. a = baselibopen(); assert(a == _G and require("_G") == a)
  439. a = iolibopen(); assert(type(a.read) == "function")
  440. assert(require("io") == a)
  441. a = tablibopen(); assert(type(a.insert) == "function")
  442. a = dblibopen(); assert(type(a.getlocal) == "function")
  443. a = mathlibopen(); assert(type(a.sin) == "function")
  444. return string.sub('okinama', 1, 2)
  445. ]])
  446. assert(a == "ok")
  447. T.closestate(L1);
  448. L1 = T.newstate()
  449. T.loadlib(L1)
  450. T.doremote(L1, "a = {}")
  451. T.testC(L1, [[pushstring a; gettable G; pushstring x; pushnum 1;
  452. settable -3]])
  453. assert(T.doremote(L1, "return a.x") == "1")
  454. T.closestate(L1)
  455. L1 = nil
  456. print('+')
  457. -------------------------------------------------------------------------
  458. -- testing memory limits
  459. -------------------------------------------------------------------------
  460. collectgarbage()
  461. T.totalmem(T.totalmem()+5000) -- set low memory limit (+5k)
  462. assert(not pcall(loadstring"local a={}; for i=1,100000 do a[i]=i end"))
  463. T.totalmem(1000000000) -- restore high limit
  464. local function stack(x) if x>0 then stack(x-1) end end
  465. -- test memory errors; increase memory limit in small steps, so that
  466. -- we get memory errors in different parts of a given task, up to there
  467. -- is enough memory to complete the task without errors
  468. function testamem (s, f)
  469. collectgarbage()
  470. stack(10) -- ensure minimum stack size
  471. local M = T.totalmem()
  472. local oldM = M
  473. local a,b = nil
  474. while 1 do
  475. M = M+3 -- increase memory limit in small steps
  476. T.totalmem(M)
  477. a, b = pcall(f)
  478. if a and b then break end -- stop when no more errors
  479. collectgarbage()
  480. if not a and not string.find(b, "memory") then -- `real' error?
  481. T.totalmem(1000000000) -- restore high limit
  482. error(b, 0)
  483. end
  484. end
  485. T.totalmem(1000000000) -- restore high limit
  486. print("\nlimit for " .. s .. ": " .. M-oldM)
  487. return b
  488. end
  489. -- testing memory errors when creating a new state
  490. b = testamem("state creation", T.newstate)
  491. T.closestate(b); -- close new state
  492. -- testing threads
  493. function expand (n,s)
  494. if n==0 then return "" end
  495. local e = string.rep("=", n)
  496. return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
  497. e, s, expand(n-1,s), e)
  498. end
  499. G=0; collectgarbage(); a =collectgarbage("count")
  500. loadstring(expand(20,"G=G+1"))()
  501. assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
  502. testamem("thread creation", function ()
  503. return T.doonnewstack("x=1") == 0 -- try to create thread
  504. end)
  505. -- testing memory x compiler
  506. testamem("loadstring", function ()
  507. return loadstring("x=1") -- try to do a loadstring
  508. end)
  509. local testprog = [[
  510. local function foo () return end
  511. local t = {"x"}
  512. a = "aaa"
  513. for _, v in ipairs(t) do a=a..v end
  514. return true
  515. ]]
  516. -- testing memory x dofile
  517. _G.a = nil
  518. local t =os.tmpname()
  519. local f = assert(io.open(t, "w"))
  520. f:write(testprog)
  521. f:close()
  522. testamem("dofile", function ()
  523. local a = loadfile(t)
  524. return a and a()
  525. end)
  526. assert(os.remove(t))
  527. assert(_G.a == "aaax")
  528. -- other generic tests
  529. testamem("string creation", function ()
  530. local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
  531. return (a == 'ablo ablo')
  532. end)
  533. testamem("dump/undump", function ()
  534. local a = loadstring(testprog)
  535. local b = a and string.dump(a)
  536. a = b and loadstring(b)
  537. return a and a()
  538. end)
  539. local t = os.tmpname()
  540. testamem("file creation", function ()
  541. local f = assert(io.open(t, 'w'))
  542. assert (not io.open"nomenaoexistente")
  543. io.close(f);
  544. return not loadfile'nomenaoexistente'
  545. end)
  546. assert(os.remove(t))
  547. testamem("table creation", function ()
  548. local a, lim = {}, 10
  549. for i=1,lim do a[i] = i; a[i..'a'] = {} end
  550. return (type(a[lim..'a']) == 'table' and a[lim] == lim)
  551. end)
  552. local a = 1
  553. close = nil
  554. testamem("closure creation", function ()
  555. function close (b,c)
  556. return function (x) return a+b+c+x end
  557. end
  558. return (close(2,3)(4) == 10)
  559. end)
  560. testamem("coroutines", function ()
  561. local a = coroutine.wrap(function ()
  562. coroutine.yield(string.rep("a", 10))
  563. return {}
  564. end)
  565. assert(string.len(a()) == 10)
  566. return a()
  567. end)
  568. print'+'
  569. -- testing some auxlib functions
  570. assert(T.gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
  571. assert(T.gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
  572. assert(T.gsub("", "alo", "//") == "")
  573. assert(T.gsub("...", ".", "/.") == "/././.")
  574. assert(T.gsub("...", "...", "") == "")
  575. print'OK'