@@ -974,6 +974,59 @@ class TestData : TestDataSuper {
974
974
expectTrue ( d. classForKeyedArchiver == expected)
975
975
}
976
976
977
+ func test_Hashing( ) {
978
+ func bridgedData( _ bytes: [ UInt8 ] ) -> Data {
979
+ return bytes. withUnsafeBytes { buffer in
980
+ NSData ( bytes: buffer. baseAddress, length: buffer. count) as Data
981
+ }
982
+ }
983
+
984
+ let simpleTests : [ [ Data ] ] = [
985
+ [
986
+ Data ( ) ,
987
+ bridgedData ( [ ] ) ,
988
+ ] ,
989
+ [
990
+ Data ( [ 1 ] ) ,
991
+ bridgedData ( [ 1 ] ) ,
992
+ ] ,
993
+ [
994
+ Data ( [ 1 , 2 ] ) ,
995
+ bridgedData ( [ 1 , 2 ] ) ,
996
+ ] ,
997
+ [
998
+ Data ( [ 1 , 2 , 3 ] ) ,
999
+ Data ( [ 1 , 2 , 3 ] ) ,
1000
+ bridgedData ( [ 1 , 2 , 3 ] ) ,
1001
+ bridgedData ( [ 1 , 2 , 3 ] ) ,
1002
+ ] ,
1003
+ [
1004
+ Data ( [ 2 , 1 , 3 ] ) ,
1005
+ bridgedData ( [ 2 , 1 , 3 ] ) ,
1006
+ ] ,
1007
+ ]
1008
+ checkHashableGroups ( simpleTests)
1009
+
1010
+ // To ensure strong hashing, all bytes must be fed into the hasher.
1011
+ let longTest : [ Data ] = [
1012
+ Data ( [ 1 ] ) + Data( UInt8 . min ... UInt8 . max) ,
1013
+ Data ( [ 2 ] ) + Data( UInt8 . min ... UInt8 . max) ,
1014
+ Data ( UInt8 . min ... UInt8 . max) + Data( [ 1 ] ) ,
1015
+ Data ( UInt8 . min ... UInt8 . max) + Data( [ 2 ] ) ,
1016
+ Data ( UInt8 . min ..< 128 ) + Data( [ 1 ] ) + Data( 128 ... UInt8 . max) ,
1017
+ Data ( UInt8 . min ..< 128 ) + Data( [ 2 ] ) + Data( 128 ... UInt8 . max) ,
1018
+ ]
1019
+ checkHashable ( longTest, equalityOracle: { $0 == $1 } )
1020
+
1021
+ let concatenationTest : [ [ Data ] ] = [
1022
+ [ Data ( [ 1 , 2 , 3 ] ) , Data ( ) ] ,
1023
+ [ Data ( [ 1 , 2 ] ) , Data ( [ 3 ] ) ] ,
1024
+ [ Data ( [ 1 ] ) , Data ( [ 2 , 3 ] ) ] ,
1025
+ [ Data ( ) , Data ( [ 1 , 2 , 3 ] ) ] ,
1026
+ ]
1027
+ checkHashable ( concatenationTest, equalityOracle: { $0 == $1 } )
1028
+ }
1029
+
977
1030
func test_AnyHashableContainingData( ) {
978
1031
let values : [ Data ] = [
979
1032
Data ( base64Encoded: " AAAA " ) !,
@@ -3708,13 +3761,14 @@ class TestData : TestDataSuper {
3708
3761
let base2 = Data ( bytes: [ 0 , 0xFF , 0xFF , 0 ] )
3709
3762
let base3 = Data ( bytes: [ 0xFF , 0xFF , 0xFF , 0 ] )
3710
3763
let sliceEmulation = Data ( bytes: [ 0xFF , 0xFF ] )
3711
- expectEqual ( base1. hashValue, base2. hashValue)
3712
3764
let slice1 = base1 [ base1. startIndex. advanced ( by: 1 ) ..< base1. endIndex. advanced ( by: - 1 ) ]
3713
3765
let slice2 = base2 [ base2. startIndex. advanced ( by: 1 ) ..< base2. endIndex. advanced ( by: - 1 ) ]
3714
3766
let slice3 = base3 [ base3. startIndex. advanced ( by: 1 ) ..< base3. endIndex. advanced ( by: - 1 ) ]
3715
- expectEqual ( slice1. hashValue, sliceEmulation. hashValue)
3716
- expectEqual ( slice1. hashValue, slice2. hashValue)
3717
- expectEqual ( slice2. hashValue, slice3. hashValue)
3767
+ checkHashableGroups ( [
3768
+ [ base1, base2] ,
3769
+ [ base3] ,
3770
+ [ sliceEmulation, slice1, slice2, slice3] ,
3771
+ ] )
3718
3772
}
3719
3773
3720
3774
func test_slice_resize_growth( ) {
@@ -3725,16 +3779,10 @@ class TestData : TestDataSuper {
3725
3779
3726
3780
func test_hashEmptyData( ) {
3727
3781
let d1 = Data ( )
3728
- let h1 = d1. hashValue
3729
-
3730
3782
let d2 = NSData ( ) as Data
3731
- let h2 = d2. hashValue
3732
- expectEqual ( h1, h2)
3733
-
3734
3783
let data = Data ( bytes: [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] )
3735
3784
let d3 = data [ 4 ..< 4 ]
3736
- let h3 = d3. hashValue
3737
- expectEqual ( h1, h3)
3785
+ checkHashableGroups ( [ [ d1, d2, d3] ] )
3738
3786
}
3739
3787
3740
3788
func test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
@@ -3847,6 +3895,7 @@ DataTests.test("test_basicMutableDataMutation") { TestData().test_basicMutableDa
3847
3895
DataTests . test ( " test_passing " ) { TestData ( ) . test_passing ( ) }
3848
3896
DataTests . test ( " test_bufferSizeCalculation " ) { TestData ( ) . test_bufferSizeCalculation ( ) }
3849
3897
DataTests . test ( " test_classForCoder " ) { TestData ( ) . test_classForCoder ( ) }
3898
+ DataTests . test ( " test_Hashing " ) { TestData ( ) . test_Hashing ( ) }
3850
3899
DataTests . test ( " test_AnyHashableContainingData " ) { TestData ( ) . test_AnyHashableContainingData ( ) }
3851
3900
DataTests . test ( " test_AnyHashableCreatedFromNSData " ) { TestData ( ) . test_AnyHashableCreatedFromNSData ( ) }
3852
3901
DataTests . test ( " test_noCopyBehavior " ) { TestData ( ) . test_noCopyBehavior ( ) }
0 commit comments