@@ -9,31 +9,47 @@ import StdlibCollectionUnittest
9
9
var DictionaryTestSuite = TestSuite ( " Dictionary4 " )
10
10
11
11
DictionaryTestSuite . test ( " Hashable " ) {
12
- let d1 : Dictionary < Int , String > = [ 1 : " meow " , 2 : " meow " , 3 : " meow " ]
13
- let d2 : Dictionary < Int , String > = [ 1 : " meow " , 2 : " meow " , 3 : " mooo " ]
14
- let d3 : Dictionary < Int , String > = [ 1 : " meow " , 2 : " meow " , 4 : " meow " ]
15
- let d4 : Dictionary < Int , String > = [ 1 : " meow " , 2 : " meow " , 4 : " mooo " ]
16
- checkHashable ( [ d1, d2, d3, d4] , equalityOracle: { $0 == $1 } )
17
-
18
- let dd1 : Dictionary < Int , Dictionary < Int , String > > = [ 1 : [ 2 : " meow " ] ]
19
- let dd2 : Dictionary < Int , Dictionary < Int , String > > = [ 2 : [ 1 : " meow " ] ]
20
- let dd3 : Dictionary < Int , Dictionary < Int , String > > = [ 2 : [ 2 : " meow " ] ]
21
- let dd4 : Dictionary < Int , Dictionary < Int , String > > = [ 1 : [ 1 : " meow " ] ]
22
- let dd5 : Dictionary < Int , Dictionary < Int , String > > = [ 2 : [ 2 : " mooo " ] ]
23
- let dd6 : Dictionary < Int , Dictionary < Int , String > > = [ 2 : [ : ] ]
24
- let dd7 : Dictionary < Int , Dictionary < Int , String > > = [ : ]
25
- checkHashable (
26
- [ dd1, dd2, dd3, dd4, dd5, dd6, dd7] ,
27
- equalityOracle: { $0 == $1 } )
28
-
29
- // Check that hash is equal even though dictionary is traversed differently
30
- var d5 : Dictionary < Int , String > =
31
- [ 1 : " meow " , 2 : " meow " , 3 : " mooo " , 4 : " woof " , 5 : " baah " , 6 : " mooo " ]
32
- let expected = d5. hashValue
33
- for capacity in [ 4 , 8 , 16 , 32 , 64 , 128 , 256 ] {
34
- d5. reserveCapacity ( capacity)
35
- expectEqual ( d5. hashValue, expected)
12
+ let d1 : [ Dictionary < Int , String > ] = [
13
+ [ 1 : " meow " , 2 : " meow " , 3 : " meow " ] ,
14
+ [ 1 : " meow " , 2 : " meow " , 3 : " mooo " ] ,
15
+ [ 1 : " meow " , 2 : " meow " , 4 : " meow " ] ,
16
+ [ 1 : " meow " , 2 : " meow " , 4 : " mooo " ] ]
17
+ checkHashable ( d1, equalityOracle: { $0 == $1 } )
18
+
19
+ let d2 : [ Dictionary < Int , Dictionary < Int , String > > ] = [
20
+ [ 1 : [ 2 : " meow " ] ] ,
21
+ [ 2 : [ 1 : " meow " ] ] ,
22
+ [ 2 : [ 2 : " meow " ] ] ,
23
+ [ 1 : [ 1 : " meow " ] ] ,
24
+ [ 2 : [ 2 : " mooo " ] ] ,
25
+ [ 2 : [ : ] ] ,
26
+ [ : ] ]
27
+ checkHashable ( d2, equalityOracle: { $0 == $1 } )
28
+
29
+ // Dictionary should hash itself in a way that ensures instances get correctly
30
+ // delineated even when they are nested in other commutative collections.
31
+ // These are different Sets, so they should produce different hashes:
32
+ let remix : [ Set < Dictionary < String , Int > > ] = [
33
+ [ [ " Blanche " : 1 , " Rose " : 2 ] , [ " Dorothy " : 3 , " Sophia " : 4 ] ] ,
34
+ [ [ " Blanche " : 1 , " Dorothy " : 3 ] , [ " Rose " : 2 , " Sophia " : 4 ] ] ,
35
+ [ [ " Blanche " : 1 , " Sophia " : 4 ] , [ " Rose " : 2 , " Dorothy " : 3 ] ]
36
+ ]
37
+ checkHashable ( remix, equalityOracle: { $0 == $1 } )
38
+
39
+ // Dictionary ordering is not guaranteed to be consistent across equal
40
+ // instances. In particular, ordering is highly sensitive to the size of the
41
+ // allocated storage buffer. Generate a few copies of the same dictionary with
42
+ // different capacities, and verify that they compare and hash the same.
43
+ var variants : [ Dictionary < String , Int > ] = [ ]
44
+ for i in 4 ..< 12 {
45
+ var set : Dictionary < String , Int > = [
46
+ " one " : 1 , " two " : 2 ,
47
+ " three " : 3 , " four " : 4 ,
48
+ " five " : 5 , " six " : 6 ]
49
+ set. reserveCapacity ( 1 << i)
50
+ variants. append ( set)
36
51
}
52
+ checkHashable ( variants, equalityOracle: { _, _ in true } )
37
53
}
38
54
39
55
runAllTests ( )
0 commit comments