@@ -1441,7 +1441,7 @@ private final class DataTests {
14411441 }
14421442
14431443 @Test func validateMutation_cow_mutableBytes( ) {
1444- var data = Data ( Array ( repeating : 0 , count: 32 ) )
1444+ var data = Data ( count: 32 )
14451445 holdReference ( data) {
14461446 var bytes = data. mutableBytes
14471447 bytes. storeBytes ( of: 1 , toByteOffset: 0 , as: UInt8 . self)
@@ -1450,7 +1450,7 @@ private final class DataTests {
14501450 #expect( heldData ? [ 0 ] == 0 )
14511451 }
14521452
1453- var data2 = Data ( Array ( repeating : 0 , count: 32 ) )
1453+ var data2 = Data ( count: 32 )
14541454 // Escape the pointer to compare after a mutation without dereferencing the pointer
14551455 let originalPointer = data2. withUnsafeBytes { $0. baseAddress }
14561456
@@ -1463,7 +1463,7 @@ private final class DataTests {
14631463 }
14641464
14651465 @Test func validateMutation_cow_mutableSpan( ) {
1466- var data = Data ( Array ( repeating : 0 , count: 32 ) )
1466+ var data = Data ( count: 32 )
14671467 holdReference ( data) {
14681468 var bytes = data. mutableSpan
14691469 bytes [ 0 ] = 1
@@ -1472,7 +1472,7 @@ private final class DataTests {
14721472 #expect( heldData ? [ 0 ] == 0 )
14731473 }
14741474
1475- var data2 = Data ( Array ( repeating : 0 , count: 32 ) )
1475+ var data2 = Data ( count: 32 )
14761476 // Escape the pointer to compare after a mutation without dereferencing the pointer
14771477 let originalPointer = data2. withUnsafeBytes { $0. baseAddress }
14781478
@@ -2429,17 +2429,16 @@ extension DataTests {
24292429// These tests require allocating an extremely large amount of data and are serialized to prevent the test runner from using all available memory at once
24302430@Suite( " Large Data Tests " , . serialized)
24312431struct LargeDataTests {
2432- @Test
2433- func largeSliceDataSpan( ) throws {
24342432#if _pointerBitWidth(_64)
2435- let count = Int ( Int32 . max)
2433+ let largeCount = Int ( Int32 . max)
24362434#elseif _pointerBitWidth(_32)
2437- let count = Int ( Int16 . max)
2435+ let largeCount = Int ( Int16 . max)
24382436#else
24392437#error("This test needs updating")
24402438#endif
2441-
2442- let source = Data ( repeating: 0 , count: count) . dropFirst ( )
2439+ @Test
2440+ func largeSliceDataSpan( ) throws {
2441+ let source = Data ( repeating: 0 , count: largeCount) . dropFirst ( )
24432442 #expect( source. startIndex != 0 )
24442443 let span = source. span
24452444 let isEmpty = span. isEmpty
@@ -2448,20 +2447,12 @@ struct LargeDataTests {
24482447
24492448 @Test
24502449 func largeSliceDataMutableSpan( ) throws {
2451- #if _pointerBitWidth(_64)
2452- var count = Int ( Int32 . max)
2453- #elseif _pointerBitWidth(_32)
2454- var count = Int ( Int16 . max)
2455- #else
2456- #error("This test needs updating")
2457- #endif
2458-
24592450#if !canImport(Darwin) || FOUNDATION_FRAMEWORK
2460- var source = Data ( repeating: 0 , count: count ) . dropFirst ( )
2451+ var source = Data ( repeating: 0 , count: largeCount ) . dropFirst ( )
24612452 #expect( source. startIndex != 0 )
24622453 count = source. count
24632454 var span = source. mutableSpan
2464- #expect( span. count == count )
2455+ #expect( span. count == largeCount - 1 )
24652456 let i = try #require( span. indices. dropFirst ( ) . randomElement ( ) )
24662457 span [ i] = . max
24672458 #expect( source [ i] == 0 )
@@ -2471,23 +2462,56 @@ struct LargeDataTests {
24712462
24722463 @Test
24732464 func largeSliceDataMutableRawSpan( ) throws {
2474- #if _pointerBitWidth(_64)
2475- var count = Int ( Int32 . max)
2476- #elseif _pointerBitWidth(_32)
2477- var count = Int ( Int16 . max)
2478- #else
2479- #error("This test needs updating")
2480- #endif
2481-
2482- var source = Data ( repeating: 0 , count: count) . dropFirst ( )
2465+ var source = Data ( repeating: 0 , count: largeCount) . dropFirst ( )
24832466 #expect( source. startIndex != 0 )
2484- count = source. count
24852467 var span = source. mutableBytes
24862468 let byteCount = span. byteCount
2487- #expect( byteCount == count )
2469+ #expect( byteCount == largeCount - 1 )
24882470 let i = try #require( span. byteOffsets. dropFirst ( ) . randomElement ( ) )
24892471 span. storeBytes ( of: - 1 , toByteOffset: i, as: Int8 . self)
24902472 #expect( source [ i] == 0 )
24912473 #expect( source [ i+ 1 ] == . max)
24922474 }
2475+
2476+ @Test func validateMutation_cow_largeMutableBytes( ) {
2477+ var data = Data ( count: largeCount)
2478+ let heldData = data
2479+ var bytes = data. mutableBytes
2480+ bytes. storeBytes ( of: 1 , toByteOffset: 0 , as: UInt8 . self)
2481+
2482+ #expect( data [ 0 ] == 1 )
2483+ #expect( heldData [ 0 ] == 0 )
2484+
2485+ var data2 = Data ( count: largeCount)
2486+ // Escape the pointer to compare after a mutation without dereferencing the pointer
2487+ let originalPointer = data2. withUnsafeBytes { $0. baseAddress }
2488+
2489+ var bytes2 = data2. mutableBytes
2490+ bytes2. storeBytes ( of: 1 , toByteOffset: 0 , as: UInt8 . self)
2491+ #expect( data2 [ 0 ] == 1 )
2492+ data2. withUnsafeBytes {
2493+ #expect( $0. baseAddress == originalPointer)
2494+ }
2495+ }
2496+
2497+ @Test func validateMutation_cow_largeMutableSpan( ) {
2498+ var data = Data ( count: largeCount)
2499+ let heldData = data
2500+ var bytes = data. mutableSpan
2501+ bytes [ 0 ] = 1
2502+
2503+ #expect( data [ 0 ] == 1 )
2504+ #expect( heldData [ 0 ] == 0 )
2505+
2506+ var data2 = Data ( count: largeCount)
2507+ // Escape the pointer to compare after a mutation without dereferencing the pointer
2508+ let originalPointer = data2. withUnsafeBytes { $0. baseAddress }
2509+
2510+ var bytes2 = data2. mutableSpan
2511+ bytes2 [ 0 ] = 1
2512+ #expect( data2 [ 0 ] == 1 )
2513+ data2. withUnsafeBytes {
2514+ #expect( $0. baseAddress == originalPointer)
2515+ }
2516+ }
24932517}
0 commit comments