RegressionTests__WeakCollectionTest.st
author Claus Gittinger <cg@exept.de>
Sat, 01 Mar 2014 14:54:00 +0100
changeset 1061 34aa7fc79330
parent 868 745b639be55b
child 1302 8149bc9ff84f
permissions -rw-r--r--
category

"{ Package: 'exept:regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#WeakCollectionTest
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-Collections'
!


!WeakCollectionTest methodsFor:'tests'!

fetchValueAtKey:k from:aWeakValueDict

    ^ aWeakValueDict at:k

    "
     self run:#testWeakValueDictionary3
    "
!

fetchValueAtKey:k from:aWeakValueDict ifAbsent:exArg

    ^ aWeakValueDict at:k ifAbsent:exArg

    "
     self run:#testWeakValueDictionary3
    "
!

testWeakIdentityDictionary1
    |w o1 o2|

    w := WeakIdentityDictionary new.
    o1 := Object new.
    o2 := Object new.

    w at:o1 put:'o1'.
    w at:o2 put:'o2'.

    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 2).

    o1 := nil.
    
    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 1).

    o2 := nil.

    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 0).

    "
     self run:#testWeakIdentityDictionary1
    "
!

testWeakIdentityDictionary2
    |w o1 o2|

    w := WeakIdentityDictionary new.
    o1 := '1' copy.
    o2 := '2' copy.

    w at:o1 put:'o1'.
    w at:o2 put:'o2'.

    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 2).

    o1 := nil.
    
    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 1).

    o2 := nil.

    ObjectMemory garbageCollect.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.
    ObjectMemory finalize.

    self assert:(w size == 0).

    "
     self run:#testWeakIdentityDictionary1
    "
!

testWeakValueDictionary1
    |ww N|

    N := 10000.

    ww := WeakValueDictionary new.

    1 "100" timesRepeat:[
	(1 to:N) do:[:idx |
	    |k v|

	    k := 'k_' , idx printString , '_12345678901234567890'.
	    v := 'v_' , idx printString , '_12345678901234567890'.
	    ww at:k put:v.
	].

	(1 to:N) do:[:idx |
	    |k v|

	    k := 'k_' , idx printString , '_12345678901234567890'.
	    v := ww at:k ifAbsent:nil.
	    v notNil ifTrue:[
		self halt.
	    ]
	].
    ].

    "
     self run:#testWeakValueDictionary1
    "
!

testWeakValueDictionary2
    |ww k v|

    ww := WeakValueDictionary new.

    ObjectMemory scavenge.

    k := 'k_' , 1 printString , '_12345678901234567890'.
    v := 'v_' , 1 printString , '_12345678901234567890'.
    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 ).
    ObjectMemory scavenge.
    self assert: ( (ww at:k ifAbsent:nil) == nil ).


    "
     self run:#testWeakValueDictionary2
    "
!

testWeakValueDictionary3
    |ww k v v2|

    ww := WeakValueDictionary new.

    ObjectMemory scavenge.

    k := 'k_' , 1 printString , '_12345678901234567890'.
    v := 'v_' , 1 printString , '_12345678901234567890'.
    ww at:k put:v.

    v2 := self fetchValueAtKey:k from:ww.
    self assert: ( v2 == v ).
    v2 := nil.

    v := v copy.
    ObjectMemory scavenge.
    v2 := self fetchValueAtKey:k from:ww ifAbsent:nil.
    self assert: ( v2 == nil ).
    v2 := nil.


    "
     self run:#testWeakValueDictionary3
    "
! !

!WeakCollectionTest class methodsFor:'documentation'!

version
    ^ '$Header$'
! !