gengc.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- $Id: testes/gengc.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing generational garbage collection')
  4. local debug = require"debug"
  5. assert(collectgarbage("isrunning"))
  6. collectgarbage()
  7. local oldmode = collectgarbage("generational")
  8. -- ensure that table barrier evolves correctly
  9. do
  10. local U = {}
  11. -- full collection makes 'U' old
  12. collectgarbage()
  13. assert(not T or T.gcage(U) == "old")
  14. -- U refers to a new table, so it becomes 'touched1'
  15. U[1] = {x = {234}}
  16. assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new"))
  17. -- both U and the table survive one more collection
  18. collectgarbage("step", 0)
  19. assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival"))
  20. -- both U and the table survive yet another collection
  21. -- now everything is old
  22. collectgarbage("step", 0)
  23. assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1"))
  24. -- data was not corrupted
  25. assert(U[1].x[1] == 234)
  26. end
  27. if T == nil then
  28. (Message or print)('\n >>> testC not active: \z
  29. skipping some generational tests <<<\n')
  30. print 'OK'
  31. return
  32. end
  33. -- ensure that userdata barrier evolves correctly
  34. do
  35. local U = T.newuserdata(0, 1)
  36. -- full collection makes 'U' old
  37. collectgarbage()
  38. assert(T.gcage(U) == "old")
  39. -- U refers to a new table, so it becomes 'touched1'
  40. debug.setuservalue(U, {x = {234}})
  41. assert(T.gcage(U) == "touched1" and
  42. T.gcage(debug.getuservalue(U)) == "new")
  43. -- both U and the table survive one more collection
  44. collectgarbage("step", 0)
  45. assert(T.gcage(U) == "touched2" and
  46. T.gcage(debug.getuservalue(U)) == "survival")
  47. -- both U and the table survive yet another collection
  48. -- now everything is old
  49. collectgarbage("step", 0)
  50. assert(T.gcage(U) == "old" and
  51. T.gcage(debug.getuservalue(U)) == "old1")
  52. -- data was not corrupted
  53. assert(debug.getuservalue(U).x[1] == 234)
  54. end
  55. -- just to make sure
  56. assert(collectgarbage'isrunning')
  57. collectgarbage(oldmode)
  58. print('OK')