Skip to content

Fix Data.count’s setter #2601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
if newValue == 0 {
return nil
} else if InlineData.canStore(count: newValue) {
return .inline(InlineData())
return .inline(InlineData(count: newValue))
} else if InlineSlice.canStore(count: newValue) {
return .slice(InlineSlice(count: newValue))
} else {
Expand Down
48 changes: 48 additions & 0 deletions Tests/Foundation/Tests/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ class TestNSData: LoopbackServerTest {
("test_rangeOfSlice", test_rangeOfSlice),
("test_nsdataSequence", test_nsdataSequence),
("test_dispatchSequence", test_dispatchSequence),
("test_Data_increaseCount", test_Data_increaseCount),
("test_Data_decreaseCount", test_Data_decreaseCount),
]
}

Expand Down Expand Up @@ -4605,5 +4607,51 @@ extension TestNSData {
}
}

func test_Data_increaseCount() {
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return }
let initials: [Range<UInt8>] = [
0..<0,
0..<2,
0..<4,
0..<8,
0..<16,
0..<32,
0..<64
]
let diffs = [0, 1, 2, 4, 8, 16, 32]
for initial in initials {
for diff in diffs {
var data = Data(initial)
data.count += diff
XCTAssertEqual(
Data(Array(initial) + Array(repeating: 0, count: diff)),
data)
}
}
}

func test_Data_decreaseCount() {
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return }
let initials: [Range<UInt8>] = [
0..<0,
0..<2,
0..<4,
0..<8,
0..<16,
0..<32,
0..<64
]
let diffs = [0, 1, 2, 4, 8, 16, 32]
for initial in initials {
for diff in diffs {
guard initial.count >= diff else { continue }
var data = Data(initial)
data.count -= diff
XCTAssertEqual(
Data(initial.dropLast(diff)),
data)
}
}
}
}