Skip to content
Open
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/DispatchData+DataProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension DispatchData : DataProtocol {
}

public subscript(position: DispatchData.Index) -> UInt8 {
precondition(index <= position && position <= index + bytes.count)
precondition(index <= position && position < index + bytes.count)
return bytes[position - index]
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/Foundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4481,6 +4481,26 @@ extension TestNSData {
}
}

func test_dispatchDataRegionSubscriptBounds() {
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
let bytes: [UInt8] = [0xAA, 0xBB, 0xCC]
var data = DispatchData.empty
bytes.withUnsafeBytes { data.append($0) }

guard let region = data.regions.first else {
XCTFail("Expected a single region")
return
}

XCTAssertEqual(region.startIndex, 0)
XCTAssertEqual(region.endIndex, bytes.count)

for (offset, expected) in bytes.enumerated() {
XCTAssertEqual(region[region.startIndex + offset], expected)
}
}
}

func test_Data_increaseCount() {
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return }
let initials: [Range<UInt8>] = [
Expand Down