PageRenderTime 12ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 1ms

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