PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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