@@ -509,6 +509,12 @@ class TestNSData: XCTestCase {
509
509
// ("test_sliceEnumeration", test_sliceEnumeration),
510
510
( " test_sliceInsertion " , test_sliceInsertion) ,
511
511
( " test_sliceDeletion " , test_sliceDeletion) ,
512
+ ( " test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound " , test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound) ,
513
+ ( " test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound " , test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound) ,
514
+ ( " test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound " , test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound) ,
515
+ ( " test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound " , test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound) ,
516
+ ( " test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound " ,
517
+ test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound) ,
512
518
]
513
519
}
514
520
@@ -3979,5 +3985,49 @@ extension TestNSData {
3979
3985
XCTAssertEqual ( mutableSliceData. startIndex, 2 )
3980
3986
XCTAssertEqual ( mutableSliceData. endIndex, sliceData. endIndex - numberOfElementsToDelete)
3981
3987
}
3988
+
3989
+ func test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
3990
+ var data = Data ( bytes: [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) [ 4 ..< 6 ]
3991
+ data. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
3992
+ ptr. advanced ( by: 1 ) . pointee = 0xFF
3993
+ }
3994
+ XCTAssertEqual ( data, Data ( bytes: [ 4 , 0xFF ] ) )
3995
+ }
3996
+
3997
+ func test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
3998
+ var data = Data ( referencing: NSData ( bytes: " hello world " , length: 11 ) ) [ 4 ..< 6 ]
3999
+ data. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
4000
+ ptr. advanced ( by: 1 ) . pointee = 0xFF
4001
+ }
4002
+ XCTAssertEqual ( data [ data. startIndex. advanced ( by: 1 ) ] , 0xFF )
4003
+ }
4004
+
4005
+ func test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
4006
+ var base = Data ( referencing: NSData ( bytes: " hello world " , length: 11 ) )
4007
+ base. append ( contentsOf: [ 1 , 2 , 3 , 4 , 5 , 6 ] )
4008
+ var data = base [ 4 ..< 6 ]
4009
+ data. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
4010
+ ptr. advanced ( by: 1 ) . pointee = 0xFF
4011
+ }
4012
+ XCTAssertEqual ( data [ data. startIndex. advanced ( by: 1 ) ] , 0xFF )
4013
+ }
4014
+
4015
+ func test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
4016
+ var data = Data ( referencing: AllOnesImmutableData ( length: 10 ) ) [ 4 ..< 6 ]
4017
+ data. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
4018
+ ptr. advanced ( by: 1 ) . pointee = 0xFF
4019
+ }
4020
+ XCTAssertEqual ( data [ data. startIndex. advanced ( by: 1 ) ] , 0xFF )
4021
+ }
4022
+
4023
+ func test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound( ) {
4024
+ var base = Data ( referencing: AllOnesData ( length: 1 ) )
4025
+ base. count = 10
4026
+ var data = base [ 4 ..< 6 ]
4027
+ data. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
4028
+ ptr. advanced ( by: 1 ) . pointee = 0xFF
4029
+ }
4030
+ XCTAssertEqual ( data [ data. startIndex. advanced ( by: 1 ) ] , 0xFF )
4031
+ }
3982
4032
}
3983
4033
0 commit comments