/test/language/gc/weak_reference/test_weak_ref01.e

http://github.com/tybor/Liberty · Specman e · 126 lines · 86 code · 13 blank · 27 comment · 1 complexity · c7699af7da3a4cd8b0be403f033f1a85 MD5 · raw file

  1. -- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries.
  2. -- See the Copyright notice at the end of this file.
  3. --
  4. class TEST_WEAK_REF01
  5. -- Test GC of WEAK_REFERENCE[X] and X.
  6. -- X is a non-tagged reference type.
  7. inherit
  8. EIFFELTEST_TOOLS
  9. create {}
  10. make
  11. feature {}
  12. wr_count: INTEGER 1_000
  13. make
  14. local
  15. mem: MEMORY; aux_wr: AUX_WEAK_REF01; wr: WEAK_REFERENCE[AUX_WEAK_REF01]; i, void_count: INTEGER
  16. do
  17. label_assert("mem.collecting", mem.collecting)
  18. create list.make
  19. create weak_list.make
  20. from
  21. i := 1
  22. until
  23. i > wr_count
  24. loop
  25. create aux_wr.make(Current)
  26. create wr.set_item(aux_wr)
  27. list.add_last(aux_wr)
  28. weak_list.add_last(wr)
  29. i := i + 1
  30. end
  31. aux_wr := Void
  32. wr := Void
  33. generate_garbage
  34. mem.collection_off
  35. label_assert("not mem.collecting", not mem.collecting)
  36. mem.full_collect
  37. label_assert("check no weak refs disposed", aux_wr_disposed = 0)
  38. from
  39. i := 1
  40. until
  41. i > wr_count
  42. loop
  43. label_assert("check weak_list.item(#(1))" # i.out, weak_list.item(i).item = list.item(i))
  44. i := i + 1
  45. end
  46. from
  47. i := 1
  48. until
  49. i > wr_count
  50. loop
  51. list.put(Void, i)
  52. i := i + 1
  53. end
  54. generate_garbage
  55. mem.collection_on
  56. mem.full_collect
  57. -- Actually, the next test is quite pessimistic
  58. label_assert("check some weak refs disposed (#(1))" # aux_wr_disposed.out, aux_wr_disposed > wr_count / 2)
  59. from
  60. i := 1
  61. until
  62. i > wr_count
  63. loop
  64. if weak_list.item(i).item = Void then
  65. void_count := void_count + 1
  66. end
  67. i := i + 1
  68. end
  69. -- Should be equal, but there may be other GC during the following call
  70. label_assert("check disposed weak refs are set to Void (#(1) = #(2))" # void_count.out # aux_wr_disposed.out, void_count >= aux_wr_disposed)
  71. end
  72. generate_garbage
  73. local
  74. i: INTEGER; s: STRING
  75. do
  76. from
  77. i := 1
  78. until
  79. i = 10_000
  80. loop
  81. create s.make_from_string("quark ends here")
  82. i := i + 1
  83. end
  84. end
  85. -- We're using LISTs rather than ARRAYs because we don't want to have NATIVE_ARRAY[WEAK_REFERENCE[...]] in
  86. -- the first test. (Although they are actually not a special case)
  87. list: LINKED_LIST[AUX_WEAK_REF01]
  88. weak_list: LINKED_LIST[WEAK_REFERENCE[AUX_WEAK_REF01]]
  89. aux_wr_disposed: INTEGER
  90. feature {AUX_WEAK_REF01}
  91. set_aux_wr_disposed
  92. do
  93. aux_wr_disposed := aux_wr_disposed + 1
  94. end
  95. end -- class TEST_WEAK_REF01
  96. --
  97. -- ------------------------------------------------------------------------------------------------------------------------------
  98. -- Copyright notice below. Please read.
  99. --
  100. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  101. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  102. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  103. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  104. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  105. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  106. --
  107. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  108. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  109. --
  110. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  111. --
  112. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  113. -- ------------------------------------------------------------------------------------------------------------------------------