Skip to content

[Foundation] Fix Data.count’s setter #28919

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 1 commit into from
Jan 9, 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 stdlib/public/Darwin/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,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
50 changes: 50 additions & 0 deletions test/stdlib/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3836,6 +3836,53 @@ class TestData : TestDataSuper {
}
}
}

func test_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
expectEqualSequence(
Array(initial) + Array(repeating: 0, count: diff),
data)
}
}
}

func test_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
expectEqualSequence(
initial.dropLast(diff),
data)
}
}
}
}

#if !FOUNDATION_XCTEST
Expand Down Expand Up @@ -4159,6 +4206,9 @@ if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
DataTests.test("test_nsdataSequence") { TestData().test_nsdataSequence() }
DataTests.test("test_dispatchSequence") { TestData().test_dispatchSequence() }
}
DataTests.test("test_increaseCount") { TestData().test_increaseCount() }
DataTests.test("test_decreaseCount") { TestData().test_decreaseCount() }


// XCTest does not have a crash detection, whereas lit does
DataTests.test("bounding failure subdata") {
Expand Down