Skip to content

Commit ebf7f50

Browse files
authored
Merge pull request #2601 from lorentey/fix-Data.count-setter
Fix Data.count’s setter
2 parents 9991f6a + bd97fe2 commit ebf7f50

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Sources/Foundation/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
14401440
if newValue == 0 {
14411441
return nil
14421442
} else if InlineData.canStore(count: newValue) {
1443-
return .inline(InlineData())
1443+
return .inline(InlineData(count: newValue))
14441444
} else if InlineSlice.canStore(count: newValue) {
14451445
return .slice(InlineSlice(count: newValue))
14461446
} else {

Tests/Foundation/Tests/TestNSData.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ class TestNSData: LoopbackServerTest {
531531
("test_rangeOfSlice", test_rangeOfSlice),
532532
("test_nsdataSequence", test_nsdataSequence),
533533
("test_dispatchSequence", test_dispatchSequence),
534+
("test_Data_increaseCount", test_Data_increaseCount),
535+
("test_Data_decreaseCount", test_Data_decreaseCount),
534536
]
535537
}
536538

@@ -4605,5 +4607,51 @@ extension TestNSData {
46054607
}
46064608
}
46074609

4610+
func test_Data_increaseCount() {
4611+
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return }
4612+
let initials: [Range<UInt8>] = [
4613+
0..<0,
4614+
0..<2,
4615+
0..<4,
4616+
0..<8,
4617+
0..<16,
4618+
0..<32,
4619+
0..<64
4620+
]
4621+
let diffs = [0, 1, 2, 4, 8, 16, 32]
4622+
for initial in initials {
4623+
for diff in diffs {
4624+
var data = Data(initial)
4625+
data.count += diff
4626+
XCTAssertEqual(
4627+
Data(Array(initial) + Array(repeating: 0, count: diff)),
4628+
data)
4629+
}
4630+
}
4631+
}
4632+
4633+
func test_Data_decreaseCount() {
4634+
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return }
4635+
let initials: [Range<UInt8>] = [
4636+
0..<0,
4637+
0..<2,
4638+
0..<4,
4639+
0..<8,
4640+
0..<16,
4641+
0..<32,
4642+
0..<64
4643+
]
4644+
let diffs = [0, 1, 2, 4, 8, 16, 32]
4645+
for initial in initials {
4646+
for diff in diffs {
4647+
guard initial.count >= diff else { continue }
4648+
var data = Data(initial)
4649+
data.count -= diff
4650+
XCTAssertEqual(
4651+
Data(initial.dropLast(diff)),
4652+
data)
4653+
}
4654+
}
4655+
}
46084656
}
46094657

0 commit comments

Comments
 (0)