#REFACTORING
authorStefan Vogel <sv@exept.de>
Fri, 22 Jan 2016 17:21:19 +0100
changeset 1302 8149bc9ff84f
parent 1301 662f6e8448f5
child 1303 609c8210dd2c
#REFACTORING class: RegressionTests::WeakCollectionTest changed:5 methods category of: #fetchValueAtKey:from: #fetchValueAtKey:from:ifAbsent:
RegressionTests__WeakCollectionTest.st
--- a/RegressionTests__WeakCollectionTest.st	Fri Jan 22 13:32:14 2016 +0100
+++ b/RegressionTests__WeakCollectionTest.st	Fri Jan 22 17:21:19 2016 +0100
@@ -10,7 +10,7 @@
 !
 
 
-!WeakCollectionTest methodsFor:'tests'!
+!WeakCollectionTest methodsFor:'helpers'!
 
 fetchValueAtKey:k from:aWeakValueDict
 
@@ -28,7 +28,9 @@
     "
      self run:#testWeakValueDictionary3
     "
-!
+! !
+
+!WeakCollectionTest methodsFor:'tests'!
 
 testWeakIdentityDictionary1
     |w o1 o2|
@@ -41,8 +43,6 @@
     w at:o2 put:'o2'.
 
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 2).
@@ -50,8 +50,6 @@
     o1 := nil.
     
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 1).
@@ -59,8 +57,6 @@
     o2 := nil.
 
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 0).
@@ -81,8 +77,6 @@
     w at:o2 put:'o2'.
 
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 2).
@@ -90,8 +84,6 @@
     o1 := nil.
     
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 1).
@@ -99,8 +91,6 @@
     o2 := nil.
 
     ObjectMemory garbageCollect.
-    ObjectMemory tenure.
-    ObjectMemory garbageCollect.
     ObjectMemory finalize.
 
     self assert:(w size == 0).
@@ -111,30 +101,31 @@
 !
 
 testWeakValueDictionary1
-    |ww N|
+    |ww n|
 
-    N := 10000.
+    n := 10000.
 
     ww := WeakValueDictionary new.
 
     1 "100" timesRepeat:[
-	(1 to:N) do:[:idx |
-	    |k v|
+        (1 to:n) do:[:idx |
+            |k v|
 
-	    k := 'k_' , idx printString , '_12345678901234567890'.
-	    v := 'v_' , idx printString , '_12345678901234567890'.
-	    ww at:k put:v.
-	].
+            k := 'k_' , idx printString , '_12345678901234567890'.
+            v := 'v_' , idx printString , '_12345678901234567890'.
+            ww at:k put:v.
+            k := v := nil.
+        ].
 
-	(1 to:N) do:[:idx |
-	    |k v|
+        ObjectMemory garbageCollect.
+
+        1 to:n do:[:idx |
+            |k v|
 
-	    k := 'k_' , idx printString , '_12345678901234567890'.
-	    v := ww at:k ifAbsent:nil.
-	    v notNil ifTrue:[
-		self halt.
-	    ]
-	].
+            k := 'k_' , idx printString , '_12345678901234567890'.
+            v := ww at:k ifAbsent:nil.
+            self assert:v isNil.
+        ].
     ].
 
     "
@@ -143,23 +134,31 @@
 !
 
 testWeakValueDictionary2
-    |ww k v|
+    |ww k v v1|
 
     ww := WeakValueDictionary new.
 
     ObjectMemory scavenge.
 
-    k := 'k_' , 1 printString , '_12345678901234567890'.
-    v := 'v_' , 1 printString , '_12345678901234567890'.
+    k := 'k_12345678901234567890' copy.
+    v := 'v_12345678901234567890' copy.
     ww at:k put:v.
 
     self assert: ( (ww at:k) == v ).
     ObjectMemory scavenge.
     self assert: ( (ww at:k) == v ).
-    v := v copy.
-    self assert: ( (ww at:k) = v ).
+
+    v1 := v copy.
+    v := nil.
+
+    "/ now the original v is not referenced any longer and should be garbage collected
+    self assert: ( (ww at:k) ~~ v1 ).
+    self assert: ( (ww at:k) = v1 ).
+
     ObjectMemory scavenge.
-    self assert: ( (ww at:k ifAbsent:nil) == nil ).
+
+"/    self assert: ( ww size = 0 ).
+    self assert: ( (ww at:k ifAbsent:nil) isNil ).
 
 
     "
@@ -174,8 +173,8 @@
 
     ObjectMemory scavenge.
 
-    k := 'k_' , 1 printString , '_12345678901234567890'.
-    v := 'v_' , 1 printString , '_12345678901234567890'.
+    k := 'k_12345678901234567890' copy.
+    v := 'v_12345678901234567890' copy.
     ww at:k put:v.
 
     v2 := self fetchValueAtKey:k from:ww.
@@ -185,7 +184,7 @@
     v := v copy.
     ObjectMemory scavenge.
     v2 := self fetchValueAtKey:k from:ww ifAbsent:nil.
-    self assert: ( v2 == nil ).
+    self assert: ( v2 isNil ).
     v2 := nil.