While inquiring generalization of #removeAll and #removeAll: related to
Klaus fix http://bugs.squeak.org/view.php?id=6937 ,
I came across two SmallDictionary minor bugs
http://bugs.squeak.org/view.php?id=7256 ,
http://bugs.squeak.org/view.php?id=7257 .
The first one by reading code, the second one by this procedure :
- a) fileOut DictionaryTest,
- b) replaced Dictionary with SmallDictionary (ALT+j),
- c) fileIn again.
Of course, the tests should be factored better rather than just duplicated.
But I don't know how to inherit tests from super...
(super tests are not executed by the TestRunner)
See attached .st
--------------------------------
Oh, and i had to change this test:
testKeyAtValue
| dict |
dict := Dictionary new.
dict at: #a put: 1.
dict at: #c put: 1.
self assert: (dict keyAtValue: 1) = #c.
"ugly may be a bug, why not having a set #a and #c"
Should be :
self assert: ((dict keyAtValue: 1) = #c) | (dict keyAtValue: 1) = #a).
The result highly depends on:
- 1) hash algorithm
- 2) Dictionary capacity (the default capacity for this test)
Asserting that it is #c :
- 1) does not help the reader to guess the specification (i would
understand it is always #c)
- 2) is very fragile, not portable, and might change unexpectedly
For the case of SmallDictionary, we could eventually assert:
self assert: (dict keyAtValue: 1) = #a.
Because keys are ordered and will retain addition order.
--------------------------------
Another point: these two methods should be removed shouldn't they?
SmallDictionary removeSelector: #keyForPatternVariableNode:.
SmallDictionary removeSelector: #valueForPatternVariableNode:.
'From Pharo0.1 of 16 May 2008 [Latest update: #10201] on 30 December 2008 at
10:45:19 pm'!
TestCase subclass: #SmallDictionaryTest
uses: TIncludesTest + TCloneTest
instanceVariableNames: 'emptyDict nonEmptyDict'
classVariableNames: ''
poolDictionaries: ''
category: 'CollectionsTests-Unordered'!
!SmallDictionaryTest methodsFor: 'testing' stamp: 'nice 12/30/2008 19:26'!
testCapacity
"this is a non regression test for
http://bugs.squeak.org/view.php?id=7256"
self shouldnt: [SmallDictionary new capacity] raise: Error! !
!SmallDictionaryTest methodsFor: 'testing' stamp: 'ar 6/13/2008 00:14'!
testCollect
"Ensure that SmallDictionary>>collect: answers a dictionary not
something else"
| dict expected result |
dict := SmallDictionary newFromPairs:{
#first. 1.
#second. 2.
#third. 3.
#fourth. 4.
#fifth. 5.
}.
result := dict collect:[:each| each asWords].
expected := SmallDictionary newFromPairs:{
#first. 'one'.
#second. 'two'.
#third. 'three'.
#fourth. 'four'.
#fifth. 'five'.
}.
self assert: result = expected.! !
!SmallDictionaryTest methodsFor: 'testing' stamp: 'nice 12/30/2008 19:43'!
testDictionaryConcatenation
"self run: #testDictionaryConcatenation"
| dict1 dict2 dict3 |
dict1 := SmallDictionary new.
dict1 at: #a put: 'Nicolas' ; at: #b put: 'Damien'.
dict2 := SmallDictionary new.
dict2 at: #a put: 'Christophe' ; at: #c put: 'Anthony'.
dict3 := dict1, dict2.
self assert: (dict3 at: #a) = 'Christophe'.
self assert: (dict3 at: #b) = 'Damien'.
self assert: (dict3 at: #c) = 'Anthony'.
! !
!SmallDictionaryTest methodsFor: 'testing' stamp: 'ar 6/13/2008 00:13'!
testReject
"Ensure that SmallDictionary>>reject: answers a dictionary not
something else"
| dict expected result |
dict := SmallDictionary newFromPairs:{
#first. 1.
#second. 2.
#third. 3.
#fourth. 4.
#fifth. 5.
}.
result := dict reject:[:each| each odd].
expected := SmallDictionary newFromPairs:{
#second. 2.
#fourth. 4.
}.
self assert: result = expected.! !
!SmallDictionaryTest methodsFor: 'testing' stamp: 'ar 6/13/2008 00:12'!
testSelect
"Ensure that SmallDictionary>>select: answers a dictionary not
something else"
| dict expected result |
dict := SmallDictionary newFromPairs:{
#first. 1.
#second. 2.
#third. 3.
#fourth. 4.
#fifth. 5.
}.
result := dict select:[:each| each odd].
expected := SmallDictionary newFromPairs:{
#first. 1.
#third. 3.
#fifth. 5.
}.
self assert: result = expected.! !
!SmallDictionaryTest methodsFor: 'setup' stamp: 'stephane.ducasse 11/21/2008
15:03'!
setUp
emptyDict := SmallDictionary new.
nonEmptyDict := SmallDictionary new.
nonEmptyDict
at: #a put: 20;
at: #b put: 30;
at: #c put: 40;
at: #d put: 30.! !
!SmallDictionaryTest methodsFor: 'tests - includes' stamp: 'stephane.ducasse
11/21/2008 15:05'!
anotherElementNotIn
^ 42! !
!SmallDictionaryTest methodsFor: 'tests - includes' stamp: 'stephane.ducasse
11/21/2008 15:04'!
collection
^ #(1 2 3)! !
!SmallDictionaryTest methodsFor: 'tests - includes' stamp: 'stephane.ducasse
11/21/2008 15:04'!
elementNotIn
^ 666! !
!SmallDictionaryTest methodsFor: 'tests - includes' stamp: 'stephane.ducasse
11/21/2008 15:04'!
empty
^ emptyDict! !
!SmallDictionaryTest methodsFor: 'tests - includes' stamp: 'stephane.ducasse
11/21/2008 15:04'!
nonEmpty
^ nonEmptyDict! !
!SmallDictionaryTest methodsFor: 'association tests' stamp: 'NDCC 3/8/2006
08:14'!
testAddAssociation
"self run:#testAddAssociation"
"self debug:#testAddAssociation"
| dict |
dict := SmallDictionary new.
dict at: #a put: 1.
dict at: #b put: 2.
self assert: (dict at: #a) = 1.
self assert: (dict at: #b) = 2.
dict at: #a put: 10.
dict at: #c put: 2.
self assert: (dict at: #a) = 10.
self assert: (dict at: #b) = 2.
self assert: (dict at: #c) = 2
! !
!SmallDictionaryTest methodsFor: 'association tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:30'!
testAssociationsSelect
| answer d |
d := SmallDictionary new.
d at: (Array with: #hello with: #world)
put: #fooBar.
d at: Smalltalk put: #'Smalltalk is the key'.
d at: #Smalltalk put: Smalltalk.
answer := d
associationsSelect: [:assoc | assoc key ==
#Smalltalk
and: [assoc value ==
Smalltalk]].
self
should: [answer isKindOf: SmallDictionary].
self
should: [answer size == 1].
self
should: [(answer at: #Smalltalk)
== Smalltalk].
answer := d
associationsSelect: [:assoc | assoc key ==
#NoSuchKey
and: [assoc value ==
#NoSuchValue]].
self
should: [answer isKindOf: SmallDictionary].
self
should: [answer size == 0]! !
!SmallDictionaryTest methodsFor: 'association tests' stamp: 'stephane.ducasse
11/21/2008 15:00'!
testIncludesAssociation
"self run:#testIncludesAssociation"
| dict |
dict := SmallDictionary new.
dict at: #a put: 1.
dict at: #b put: 2.
self assert: (dict includesAssociation: (#a -> 1)).
self assert: (dict includesAssociation: (#b -> 2)).
! !
!SmallDictionaryTest methodsFor: 'association tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:25'!
testIncludesAssociationNoValue
"self run:#testIncludesAssociationNoValue"
"self debug:#testIncludesAssociationNoValue"
| dict a1 a3 |
a1 := Association key: #Italie.
a3 := Association key: #France value: 'Paris'.
self assert: (a1 key = #Italie).
self assert: (a1 value isNil).
dict := SmallDictionary new.
dict add: a1.
dict add: a3.
self assert: (dict includesKey: #France).
self assert: (dict includesKey: #Italie).
self assert: (dict at: #Italie) isNil.
self assert: (dict at: #France) = 'Paris'
! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:26'!
testAtError
"self run: #testAtError"
| dict |
dict := SmallDictionary new.
dict at: #a put: 666.
self shouldnt: [ dict at: #a ] raise: Error.
self should: [ dict at: #b ] raise: Error.
! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp: 'NDCC 3/1/2006 14:27'!
testAtIfAbsent
"self run: #testAtIfAbsent"
| dict |
dict := SmallDictionary new.
dict at: #a put: 666.
self assert: (dict at: #a ifAbsent: [nil]) = 666.
self assert: (dict at: #b ifAbsent: [nil]) isNil.
! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp: 'NDCC 3/8/2006 09:28'!
testAtPut
"self run: #testAtPut"
"self debug: #testAtPut"
| adictionary |
adictionary := SmallDictionary new.
adictionary at: #a put: 3.
self assert: (adictionary at: #a) = 3.
adictionary at: #a put: 3.
adictionary at: #a put: 4.
self assert: (adictionary at: #a) = 4.
adictionary at: nil put: 666.
self assert: (adictionary at: nil) = 666! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp: 'marcus.denker 9/14/2008
21:15'!
testAtPutNil
"self run: #testAtPut"
"self debug: #testAtPut"
| dict |
dict := SmallDictionary new.
dict at: nil put: 1.
self assert: (dict at: nil) = 1.
dict at: #a put: nil.
self assert: (dict at: #a) isNil.
dict at: nil put: nil.
self assert: (dict at: nil) isNil.
! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:24'!
testIncludesKey
"self run:#testIncludesKey"
"self debug:#testIncludesKey"
| dict a1 a2 a3 |
a1 := Association key: 'Italie'.
a2 := Association new.
a3 := Association key: 'France' value: 'Paris'.
dict := SmallDictionary new.
dict add: a1 .
dict add: a2.
dict add: a3.
self assert: (dict includesKey: #France).
self assert: (dict includesKey: 'France').
self assert: (dict includesKey: #Italie).
self assert: (dict includesKey: nil).
self assert: (dict at: 'France' ) = 'Paris'.
! !
!SmallDictionaryTest methodsFor: 'basic tests' stamp: 'NDCC 3/8/2006 09:41'!
testOccurrencesOf
"self run:#testOccurrencesOf"
| dict |
dict := SmallDictionary new.
dict at: #a put: 1.
dict at: #b put: 2.
dict at: #c put: 1.
dict at: #d put: 3.
dict at: nil put: nil.
dict at: #z put: nil.
self assert: (dict occurrencesOf: 1 ) = 2.
self assert: (dict occurrencesOf: nil ) = 2.
! !
!SmallDictionaryTest methodsFor: 'implementation tests' stamp: 'stephaneducasse
9/18/2005 10:48'!
testAtNil
"(self run: #testAtNil)"
"nil is a valid key in squeak. In VW nil is not a valid key"
"Ansi 1.9 p, 168
5.7.2.5 Message: at: key put: newElement
Synopsis
Store newElement at key in the receiver. Answer
newElement.
Definition: <abstractSmallDictionary>
If lookup succeeds for key, then newElement replaces the
element previously stored at key.
Otherwise, the newElement is stored at the new key. In either
case, subsequent successful
lookups for key will answer newElement. Answer newElement.
The result is undefined if the key is nil.
This clearly indicates that different smalltalks where doing
different assumptions."
| dict1 |
dict1 := SmallDictionary new.
self shouldnt: [ dict1 at: nil put: #none] raise: Error.
self assert: (dict1 at: nil) = #none.
! !
!SmallDictionaryTest methodsFor: 'implementation tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:29'!
testPseudoVariablesAreValidKeys
"(self run: #testPseudoVariablesAreValidKeys)"
"true and false are valid keys"
| dict1 |
dict1 := SmallDictionary new.
self shouldnt: [dict1 at: true put: #true] raise: Error.
self assert: (dict1 at: true) = #true.
self shouldnt: [dict1 at: false put: #false] raise: Error.
self assert: (dict1 at: false) = #false.! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp: 'nice 12/30/2008
22:43'!
testKeyAtValue
"self run: #testKeyAtValue"
"self debug: #testKeyAtValue"
| dict |
dict := SmallDictionary new.
dict at: #a put: 1.
dict at: #b put: 2.
dict at: #c put: 1.
self assert: (dict keyAtValue: 2) = #b.
self assert: (dict keyAtValue: 1) = #c | ((dict keyAtValue: 1) = #a).
"ugly may be a bug, why not having a set #a and #c"
self should: [dict keyAtValue: 0] raise: Error
! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:23'!
testKeys
"self run:#testKeys "
| a1 a2 dict |
a1 := Association key: 'France' value: 'Paris'.
a2 := Association key: 'Italie' value: 'Rome'.
dict := SmallDictionary new.
dict add: a1.
dict add: a2.
self assert: (dict keys size) = 2.
self assert: (dict keys includes: #France)
! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp: 'NDCC 3/8/2006
09:13'!
testKeysDo
"self run: #testKeysDo"
"self debug: #testKeysDo"
| dict res |
dict := SmallDictionary new.
dict at: #a put: 33.
dict at: #b put: 66.
res := OrderedCollection new.
dict keysDo: [ :each | res add: each].
self assert: res asSet = #(a b) asSet.
! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp: 'NDCC 3/8/2006
08:56'!
testRemoveKey
"self run:#testRemoveKey "
| dict |
dict := SmallDictionary new.
dict at: #a put: 1.
dict at: #b put: 2.
self assert: (dict keys size) = 2.
dict removeKey: #a.
self assert: dict keys size = 1.
self should: [dict at: #a] raise: Error.
self assert: (dict at: #b) = 2
! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp: 'NDCC 3/8/2006
08:16'!
testSmallDictionaryConcatenation
"self run: #testSmallDictionaryConcatenation"
| dict1 dict2 dict3 |
dict1 := SmallDictionary new.
dict1 at: #a put: 'Nicolas' ; at: #b put: 'Damien'.
dict2 := SmallDictionary new.
dict2 at: #a put: 'Christophe' ; at: #c put: 'Anthony'.
dict3 := dict1, dict2.
self assert: (dict3 at: #a) = 'Christophe'.
self assert: (dict3 at: #b) = 'Damien'.
self assert: (dict3 at: #c) = 'Anthony'.
! !
!SmallDictionaryTest methodsFor: 'keys and value tests' stamp:
'ndCollectionsTests-Unordered 3/16/2006 10:23'!
testValues
"self run:#testValues "
| a1 a2 a3 dict |
a1 := Association key: 'France' value: 'Paris'.
a2 := Association key: 'Italie' value: 'Rome'.
dict := SmallDictionary new.
dict add: a1.
dict add: a2.
self assert: (dict values size ) = 2.
self assert: (dict values includes: 'Paris').
a3 := Association new.
dict add: a3.
self assert: (dict values size ) = 3.
self assert: (dict values includes: nil).
! !
!SmallDictionaryTest methodsFor: 'tests' stamp: 'zz 12/7/2005 19:59'!
testAdd
| dict |
dict := SmallDictionary new.
dict add: #a -> 1.
dict add: #b -> 2.
self assert: (dict at: #a) = 1.
self assert: (dict at: #b) = 2! !
!SmallDictionaryTest methodsFor: 'tests' stamp: 'zz 12/7/2005 19:55'!
testAddAll
| dict1 dict2 |
dict1 := SmallDictionary new.
dict1 at: #a put:1 ; at: #b put: 2.
dict2 := SmallDictionary new.
dict2 at: #a put: 3 ; at: #c put: 4.
dict1 addAll: dict2.
self assert: (dict1 at: #a) = 3.
self assert: (dict1 at: #b) = 2.
self assert: (dict1 at: #c) = 4.! !
!SmallDictionaryTest methodsFor: 'tests' stamp: 'zz 12/7/2005 19:57'!
testComma
| dict1 dict2 dict3 |
dict1 := SmallDictionary new.
dict1 at: #a put:1 ; at: #b put: 2.
dict2 := SmallDictionary new.
dict2 at: #a put: 3 ; at: #c put: 4.
dict3 := dict1, dict2.
self assert: (dict3 at: #a) = 3.
self assert: (dict3 at: #b) = 2.
self assert: (dict3 at: #c) = 4.! !
!SmallDictionaryTest methodsFor: 'tests' stamp: 'stephaneducasse 9/18/2005
10:45'!
testPseudo
"(self run: #testPseudo)"
"true and false are valid keys"
| dict1 |
dict1 := SmallDictionary new.
self shouldnt: [dict1 at: true put: #true] raise: Error.
self assert: (dict1 at: true) = #true.
self shouldnt: [dict1 at: false put: #false] raise: Error.
self assert: (dict1 at: false) = #false.! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
SmallDictionaryTest class
uses: TIncludesTest classTrait + TCloneTest classTrait
instanceVariableNames: 'testToto pt1'!
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project