Issue 3233 in pharo: Tests for WeakSet

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Issue 3233 in pharo: Tests for WeakSet

pharo
Status: FixedWaitingToBePharoed
Owner: stephane.ducasse
Labels: Type-Squeak

New issue 3233 by stephane.ducasse: Tests for WeakSet
http://code.google.com/p/pharo/issues/detail?id=3233

Levente Uzonyi uploaded a new version of CollectionsTests to project The  
Trunk:
http://source.squeak.org/trunk/CollectionsTests-ul.175.mcz

==================== Summary ====================

Name: CollectionsTests-ul.175
Author: ul
Time: 7 November 2010, 3:17:19.921 am
UUID: 4b82952d-1507-5249-ba8a-9f93a4aaa9f9
Ancestors: CollectionsTests-ul.174

- integrated tests for WeakSet from http://bugs.squeak.org/view.php?id=7350

=============== Diff against CollectionsTests-ul.174 ===============

Item was added:
+ ----- Method: WeakSetTest>>testAddIncludesSizeReclaim (in  
category 'testing') -----
+ testAddIncludesSizeReclaim
+       | ws o2 o3 |
+       o2 := 1/2.
+       o3 := '123' copy.
+       ws := WeakSet new.
+       ws add: o2.
+       ws add: o3.
+       self assert: ws size = 2.
+       self assert: (ws includes: o2).
+       self assert: (ws includes: o3).
+
+       "inclusion test does use equality, not identity"
+       self assert: (ws includes: o3 copy) description: 'WeakSet are not  
WeakIdentitySet'.
+
+       "only one copy is added"
+       ws add: o3 copy.
+       self assert: ws size = 2.
+
+       "reclame objects so that slots of ws are nilled out"
+       o2 := o3 := nil.
+       Smalltalk garbageCollect.
+       self deny: (ws includes: 1/2).
+       self deny: (ws includes: '123' copy).
+
+       "fast #size is not updated automatically by dead object reclamation
+       But there is a slowSize trying to tell the truth"
+       self assert: ws slowSize = 0.!

Item was added:
+ ----- Method: WeakSetTest>>testCollisions (in category 'testing') -----
+ testCollisions
+
+       | ws o1 o2 o5 on remember forget |
+
+       "create a weak set"
+       ws := WeakSet new: 15.
+
+       "select some fractions wanting same place in ws array"
+       o1 := (2 to: 100) select: [:i | (ws scanFor: 1 / i) == 1].
+       o2 := (2 to: 100) select: [:i | (ws scanFor: 1 / i) == 2].
+       o5 := (2 to: 100) select: [:i | (ws scanFor: 1 / i) == 5].
+       on := (2 to: 100) select: [:i | (ws scanFor: 1 / i) == (ws array  
size - 1)].
+
+       "Add some fractions to the weak set, and remember a pointer for a  
few of them"
+       remember := OrderedCollection new.
+       forget := OrderedCollection new.
+       ws add: (remember add: 1 / o1 first).
+       ws add: (forget add: 1 / on second).
+       ws add: (forget add: 1 / o1 second).
+       ws add: (forget add: 1 / o5 second).
+       ws add: (forget add: 1 / o2 second).
+       ws add: (forget add: 1 / o1 third).
+       ws add: (remember add: 1 / o2 first).
+       ws add: (forget add: 1 / o5 third).
+       ws add: (forget add: 1 / on third).
+       ws add: (remember add: 1 / o2 fourth).
+       ws add: (remember add: 1 / on first).
+       ws add: (remember add: 1 / o5 first).
+
+       "forget and reclaim all entries but those in remember"
+       forget := nil.
+       Smalltalk garbageCollect.
+
+       remember do: [:m | self assert: (ws includes: m)].
+       ws add: 1/on second.
+       remember do: [:m | self assert: (ws includes: m)].
+
+       ws add: (remember add: 1 / o1 fourth).
+       ws add: (remember add: 1 / on fourth).
+       remember remove: (ws remove: (1 / o5 first)).
+       remember remove: (ws remove: (1 / on first)).
+       remember remove: (ws remove: (1 / o2 first)).
+       remember remove: (ws remove: (1 / o1 first)).
+       remember do: [:m | self assert: (ws includes: m)].
+       ws add: 1/on second.
+       ws add: 1/o5 second.
+       remember do: [:m | self assert: (ws includes: m)].
+       !

Item was added:
+ ----- Method: WeakSetTest>>testDoDontIterateOnReclaimedObjects (in  
category 'testing') -----
+ testDoDontIterateOnReclaimedObjects
+
+       | ws o1 o2 size |
+       ws := WeakSet new.
+       2 to: 20 do: [:i | ws add: 1 / i].
+
+       o1 := 3.0.
+       o2 := 4.0.
+       ws add: o1; add: o2.
+
+       "Reclaim memory"
+       Smalltalk garbageCollect.
+
+       size := 0.
+       ws do: [:each |
+               size := size + 1.
+               self deny: each isNil].
+
+       self assert: size = 2!

Item was added:
+ ----- Method: WeakSetTest>>testGrowWhenNecessary (in category 'testing')  
-----
+ testGrowWhenNecessary
+       | ws objs initialSize |
+       objs := (2 to: 20) collect: [:i | 1 / i].
+       ws := WeakSet new: 5.
+       initialSize := ws array size.
+       1 to: objs size do: [:k | ws add: (objs at: k)].
+       self assert: ws array size > initialSize
+               description: 'The WeakSet grown because not enough room  
were preallocated'.!

Item was added:
+ ----- Method: WeakSetTest>>testIncludesNil (in category 'testing') -----
+ testIncludesNil
+
+       | ws |
+       ws := WeakSet new.
+       self deny: (ws includes: nil).
+
+       "After reclamation, should not includes nil: nil counts for nothing"
+       ws add: 1/2.
+       ws add: 1/3.
+       Smalltalk garbageCollect.
+       self deny: (ws includes: nil).!