diff options
Diffstat (limited to 'testes/gengc.lua')
| -rw-r--r-- | testes/gengc.lua | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/testes/gengc.lua b/testes/gengc.lua new file mode 100644 index 00000000..36aed806 --- /dev/null +++ b/testes/gengc.lua | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | -- $Id: gengc.lua $ | ||
| 2 | -- See Copyright Notice in file all.lua | ||
| 3 | |||
| 4 | print('testing generational garbage collection') | ||
| 5 | |||
| 6 | local debug = require"debug" | ||
| 7 | |||
| 8 | assert(collectgarbage("isrunning")) | ||
| 9 | |||
| 10 | collectgarbage() | ||
| 11 | |||
| 12 | local oldmode = collectgarbage("generational") | ||
| 13 | |||
| 14 | |||
| 15 | -- ensure that table barrier evolves correctly | ||
| 16 | do | ||
| 17 | local U = {} | ||
| 18 | -- full collection makes 'U' old | ||
| 19 | collectgarbage() | ||
| 20 | assert(not T or T.gcage(U) == "old") | ||
| 21 | |||
| 22 | -- U refers to a new table, so it becomes 'touched1' | ||
| 23 | U[1] = {x = {234}} | ||
| 24 | assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new")) | ||
| 25 | |||
| 26 | -- both U and the table survive one more collection | ||
| 27 | collectgarbage("step", 0) | ||
| 28 | assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival")) | ||
| 29 | |||
| 30 | -- both U and the table survive yet another collection | ||
| 31 | -- now everything is old | ||
| 32 | collectgarbage("step", 0) | ||
| 33 | assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1")) | ||
| 34 | |||
| 35 | -- data was not corrupted | ||
| 36 | assert(U[1].x[1] == 234) | ||
| 37 | end | ||
| 38 | |||
| 39 | |||
| 40 | if T == nil then | ||
| 41 | (Message or print)('\n >>> testC not active: \z | ||
| 42 | skipping some generational tests <<<\n') | ||
| 43 | print 'OK' | ||
| 44 | return | ||
| 45 | end | ||
| 46 | |||
| 47 | |||
| 48 | -- ensure that userdata barrier evolves correctly | ||
| 49 | do | ||
| 50 | local U = T.newuserdata(0, 1) | ||
| 51 | -- full collection makes 'U' old | ||
| 52 | collectgarbage() | ||
| 53 | assert(T.gcage(U) == "old") | ||
| 54 | |||
| 55 | -- U refers to a new table, so it becomes 'touched1' | ||
| 56 | debug.setuservalue(U, {x = {234}}) | ||
| 57 | assert(T.gcage(U) == "touched1" and | ||
| 58 | T.gcage(debug.getuservalue(U)) == "new") | ||
| 59 | |||
| 60 | -- both U and the table survive one more collection | ||
| 61 | collectgarbage("step", 0) | ||
| 62 | assert(T.gcage(U) == "touched2" and | ||
| 63 | T.gcage(debug.getuservalue(U)) == "survival") | ||
| 64 | |||
| 65 | -- both U and the table survive yet another collection | ||
| 66 | -- now everything is old | ||
| 67 | collectgarbage("step", 0) | ||
| 68 | assert(T.gcage(U) == "old" and | ||
| 69 | T.gcage(debug.getuservalue(U)) == "old1") | ||
| 70 | |||
| 71 | -- data was not corrupted | ||
| 72 | assert(debug.getuservalue(U).x[1] == 234) | ||
| 73 | end | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | -- just to make sure | ||
| 78 | assert(collectgarbage'isrunning') | ||
| 79 | |||
| 80 | collectgarbage(oldmode) | ||
| 81 | |||
| 82 | print('OK') | ||
| 83 | |||
