/test/language/gc/weak_reference/test_weak_ref04.e

http://github.com/tybor/Liberty · Specman e · 154 lines · 115 code · 14 blank · 25 comment · 4 complexity · 2060f50b117a3fdcb2e86525fa208682 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_REF04
  5. -- Test GC of WEAK_REFERENCE[X] with various types of X (anchored...).
  6. inherit
  7. EIFFELTEST_TOOLS
  8. create {}
  9. make
  10. feature {}
  11. wr_count: INTEGER 1000
  12. make
  13. local
  14. mem: MEMORY; aux_wr: AUX_WEAK_REF04; a_aux_wr1, a_aux_wr2, a_aux_wr3: FAST_ARRAY[AUX_WEAK_REF04]
  15. i, void_count_1, void_count_2, void_count_3: INTEGER
  16. do
  17. assert(mem.collecting)
  18. -- Test # 1
  19. create aux_wrb -- not really used
  20. create aux_wr.make(Current)
  21. aux_wrb := polymorph(aux_wr, aux_wrb)
  22. aux_wrb.do_something
  23. -- to make AUX_WEAK_REF02B polymorphic, hence tagged
  24. aux_wrb := Void
  25. create a_aux_wr1.with_capacity(wr_count)
  26. create a_aux_wr2.with_capacity(wr_count)
  27. create a_aux_wr3.with_capacity(wr_count)
  28. create a_wr1.with_capacity(wr_count)
  29. create a_wr2.with_capacity(wr_count)
  30. create a_wr3.with_capacity(wr_count)
  31. from
  32. i := wr_count - 1
  33. until
  34. i < 0
  35. loop
  36. a_aux_wr1.add_last(create {AUX_WEAK_REF04}.make(Current))
  37. a_wr1.add_last(create {WEAK_REFERENCE[AUX_WEAK_REF02B]}.set_item(a_aux_wr1.last))
  38. a_aux_wr2.add_last(create {AUX_WEAK_REF04}.make(Current))
  39. a_wr2.add_last(create {WEAK_REFERENCE[AUX_WEAK_REF02B]}.set_item(a_aux_wr2.last))
  40. a_aux_wr3.add_last(create {AUX_WEAK_REF04}.make(Current))
  41. a_wr3.add_last(create {WEAK_REFERENCE[AUX_WEAK_REF02B]}.set_item(a_aux_wr3.last))
  42. i := i - 1
  43. end
  44. generate_garbage
  45. mem.collection_on
  46. mem.full_collect
  47. assert(aux_wr_nb_disposed = 0) -- Test # 2
  48. from
  49. i := wr_count - 1
  50. until
  51. i < 0
  52. loop
  53. assert(a_wr1.item(i).item = a_aux_wr1.item(i))
  54. a_aux_wr1.put(Void, i)
  55. assert(a_wr2.item(i).item = a_aux_wr2.item(i))
  56. a_aux_wr2.put(Void, i)
  57. assert(a_wr3.item(i).item = a_aux_wr3.item(i))
  58. a_aux_wr3.put(Void, i)
  59. i := i - 1
  60. end
  61. generate_garbage
  62. mem.collection_on
  63. mem.full_collect
  64. from
  65. i := wr_count - 1
  66. until
  67. i < 0
  68. loop
  69. if a_wr1.item(i).item = Void then
  70. void_count_1 := void_count_1 + 1
  71. end
  72. if a_wr2.item(i).item = Void then
  73. void_count_2 := void_count_2 + 1
  74. end
  75. if a_wr3.item(i).item = Void then
  76. void_count_3 := void_count_3 + 1
  77. end
  78. i := i - 1
  79. end
  80. -- Actually, the next test is quite pessimistic
  81. assert(void_count_1 > wr_count / 2)
  82. assert(void_count_2 > wr_count / 2)
  83. assert(void_count_3 > wr_count / 2)
  84. assert(aux_wr_nb_disposed = void_count_1 + void_count_2 + void_count_3)
  85. assert(aux_wr /= Void) -- To prevent gcc's optimizer from recycling aux_wr
  86. end
  87. generate_garbage
  88. local
  89. i: INTEGER; s: STRING
  90. do
  91. from
  92. i := 1
  93. until
  94. i = 10000
  95. loop
  96. create s.make_from_string("quark ends here")
  97. i := i + 1
  98. end
  99. end
  100. aux_wrb: AUX_WEAK_REF02B
  101. wr: WEAK_REFERENCE[AUX_WEAK_REF02B]
  102. a_wr1: FAST_ARRAY[WEAK_REFERENCE[AUX_WEAK_REF02B]]
  103. a_wr2: FAST_ARRAY[like wr]
  104. a_wr3: FAST_ARRAY[WEAK_REFERENCE[like aux_wrb]]
  105. aux_wr_nb_disposed: INTEGER
  106. polymorph (awr: AUX_WEAK_REF04; awrb: AUX_WEAK_REF02B): AUX_WEAK_REF02B
  107. local
  108. rnd: MINIMAL_RANDOM_NUMBER_GENERATOR
  109. do
  110. create rnd.make
  111. if rnd.last_integer(100).is_odd then
  112. Result := awr
  113. else
  114. Result := awrb
  115. end
  116. end
  117. feature {AUX_WEAK_REF04}
  118. increment_aux_wr_nb_disposed
  119. do
  120. aux_wr_nb_disposed := aux_wr_nb_disposed + 1
  121. end
  122. end -- class TEST_WEAK_REF04
  123. --
  124. -- ------------------------------------------------------------------------------------------------------------------------------
  125. -- Copyright notice below. Please read.
  126. --
  127. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  128. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  129. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  130. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  131. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  132. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  133. --
  134. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  135. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  136. --
  137. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  138. --
  139. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  140. -- ------------------------------------------------------------------------------------------------------------------------------