PageRenderTime 54ms CodeModel.GetById 48ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/test/language/gc/weak_reference/test_weak_ref02.e

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