Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizlab committed Dec 5, 2022
1 parent c75b8b9 commit bae1bfb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Sources/StreamReader/Matching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ internal func matchDelimiters(inData data: UnsafeRawBufferPointer, dataStartOffs
/* Reversed enumeration in order to be able to remove an element from the unmatchedDelimiters array while still enumerating it and keeping valid indexes. */
for (delimiterIdx, delimiter) in unmatchedDelimiters.enumerated().reversed() {
let delimiterLength = delimiter.element.count
/* If the delimiter is empty or bigger than the remaining space it cannot match. */
guard delimiterLength > 0 else {continue}
/* If the delimiter is empty or bigger than the remaining space it cannot match.
* Emtpy delimiters are filtered before. */
assert(delimiterLength > 0)
guard delimiterLength <= curRemainingSpace else {
/* The delimiter is too big to compare to the whole data.
* If the delimiter is a potential match (the data available is a prefix of the delimiter),
Expand Down
34 changes: 33 additions & 1 deletion Tests/StreamReaderTests/StreamReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,46 @@ class StreamReaderTests : XCTestCase {
}
}

func testUpToWithSepBiggerThanBuffer() throws {
func testUpToWithSepBiggerThanBufferWODelimiter() throws {
try runTest(hexDataString: "01 23 45 67 89", bufferSizes: Array(1...9), bufferSizeIncrements: Array(1...9), underlyingStreamReadSizeLimits: [nil]){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let rd = try reader.readData(upTo: [Data(hexEncoded: "23 45 67 89")!, Data(hexEncoded: "45")!], matchingMode: .shortestDataWins, includeDelimiter: false)
XCTAssertEqual(rd.data, data[0..<1])
XCTAssertFalse(try reader.checkForEOF())
}
}

func testUpToWithSepBiggerThanBufferWDelimiter() throws {
try runTest(hexDataString: "01 23 45 67 89", bufferSizes: Array(1...9), bufferSizeIncrements: Array(1...9), underlyingStreamReadSizeLimits: [nil]){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let rd = try reader.readData(upTo: [Data(hexEncoded: "23 45 67 89")!, Data(hexEncoded: "45")!], matchingMode: .shortestDataWins, includeDelimiter: true)
XCTAssertEqual(rd.data, data[0..<3])
XCTAssertFalse(try reader.checkForEOF())
}
}

func testUpToMatchingMinLength() throws {
try runTest(hexDataString: "01 23 45 67 89", bufferSizes: Array(1...9), bufferSizeIncrements: Array(1...9), underlyingStreamReadSizeLimits: [nil]){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let rd = try reader.readData(upTo: [Data(hexEncoded: "01 23")!, Data(hexEncoded: "45 67")!], matchingMode: .shortestDataWins, includeDelimiter: true)
XCTAssertEqual(rd.data, data[0..<2])
XCTAssertFalse(try reader.checkForEOF())
}
}

func testUpToWithWithOverlappingSeps() throws {
try runTest(hexDataString: "01 23 45 67 89", bufferSizes: Array(1...1), bufferSizeIncrements: Array(1...1), underlyingStreamReadSizeLimits: [nil]){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let rd = try reader.readData(upTo: [Data(hexEncoded: "23 45")!], matchingMode: .anyMatchWins, includeDelimiter: true)
XCTAssertEqual(rd.data, data[0..<3])
XCTAssertFalse(try reader.checkForEOF())
}
}

func testUpToFirstMatchingWinsMultipleCandidates() throws {
try runTest(hexDataString: "01 23 45 67 89", bufferSizes: Array(1...1), bufferSizeIncrements: Array(1...1), underlyingStreamReadSizeLimits: [nil]){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let rd = try reader.readData(upTo: [Data(hexEncoded: "23 45")!, Data(hexEncoded: "01 23")!], matchingMode: .firstMatchingDelimiterWins, includeDelimiter: false)
XCTAssertEqual(rd.data, data[0..<1])
XCTAssertFalse(try reader.checkForEOF())
}
}

func testReadInt() throws {
try runTest(hexDataString: "01 23 45 67", bufferSizes: Array(1...9), bufferSizeIncrements: Array(1...9), underlyingStreamReadSizeLimits: [nil] + Array(1...9)){ reader, data, limit, bufferSize, bufferSizeIncrement, underlyingStreamReadSizeLimit in
let v: Int32 = try reader.readType()
Expand Down

0 comments on commit bae1bfb

Please sign in to comment.