From a401207e582d97de7d1620ecabde76e446a820e2 Mon Sep 17 00:00:00 2001 From: Frizlab Date: Mon, 29 Mar 2021 09:28:47 +0200 Subject: [PATCH 1/4] Add tests that fail on Data --- Tests/Foundation/Tests/TestNSData.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/Foundation/Tests/TestNSData.swift b/Tests/Foundation/Tests/TestNSData.swift index 5e77aee13f..69c61c0748 100644 --- a/Tests/Foundation/Tests/TestNSData.swift +++ b/Tests/Foundation/Tests/TestNSData.swift @@ -213,6 +213,8 @@ class TestNSData: LoopbackServerTest { ("testCustomDeallocator", testCustomDeallocator), ("testDataInSet", testDataInSet), ("testEquality", testEquality), + ("testFirstRange", testFirstRange), + ("testLastRange", testLastRange), ("testGenericAlgorithms", testGenericAlgorithms), ("testInitializationWithArray", testInitializationWithArray), ("testInsertData", testInsertData), @@ -1181,6 +1183,22 @@ extension TestNSData { XCTAssertTrue(d1 == d2, "Data should be equal") } + func testFirstRange() { + let d = Data([1, 2, 3, 1, 2, 3, 0]) + XCTAssertEqual(d.firstRange(of: Data([1])), 0..<1) + XCTAssertEqual(d.firstRange(of: Data([2])), 1..<2) + XCTAssertEqual(d.firstRange(of: Data([3])), 2..<3) + XCTAssertEqual(d.firstRange(of: Data([0])), 6..<7) + } + + func testLastRange() { + let d = Data([0, 1, 2, 3, 1, 2, 3]) + XCTAssertEqual(d.lastRange(of: Data([1])), 4..<5) + XCTAssertEqual(d.lastRange(of: Data([2])), 5..<6) + XCTAssertEqual(d.lastRange(of: Data([3])), 6..<7) + XCTAssertEqual(d.lastRange(of: Data([0])), 0..<1) + } + func testDataInSet() { let d1 = dataFrom("Hello") let d2 = dataFrom("Hello") From 144e29903b2f9e1a1424b5ccb0899cfdd2bb4b2f Mon Sep 17 00:00:00 2001 From: Frizlab Date: Mon, 29 Mar 2021 09:50:04 +0200 Subject: [PATCH 2/4] Fix firstRange and lastRange implementation in DataProtocol --- Sources/Foundation/DataProtocol.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Foundation/DataProtocol.swift b/Sources/Foundation/DataProtocol.swift index 3e9f88cf22..83288bf884 100644 --- a/Sources/Foundation/DataProtocol.swift +++ b/Sources/Foundation/DataProtocol.swift @@ -143,7 +143,7 @@ extension DataProtocol { return nil } var haystackIndex = r.lowerBound - let haystackEnd = index(r.upperBound, offsetBy: -data.count) + let haystackEnd = index(r.upperBound, offsetBy: -data.count + 1) while haystackIndex < haystackEnd { var compareIndex = haystackIndex var needleIndex = data.startIndex @@ -172,19 +172,19 @@ extension DataProtocol { return nil } var haystackIndex = r.upperBound - let haystackStart = index(r.lowerBound, offsetBy: data.count) + let haystackStart = index(r.lowerBound, offsetBy: data.count - 1) while haystackIndex > haystackStart { var compareIndex = haystackIndex var needleIndex = data.endIndex let needleStart = data.startIndex var matched = true while compareIndex > haystackStart && needleIndex > needleStart { + needleIndex = data.index(before: needleIndex) + compareIndex = index(before: compareIndex) if self[compareIndex] != data[needleIndex] { matched = false break } - needleIndex = data.index(before: needleIndex) - compareIndex = index(before: compareIndex) } if matched { return compareIndex.. Date: Mon, 29 Mar 2021 09:58:13 +0200 Subject: [PATCH 3/4] Add more tests that fail --- Tests/Foundation/Tests/TestNSData.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Tests/Foundation/Tests/TestNSData.swift b/Tests/Foundation/Tests/TestNSData.swift index 69c61c0748..ec6db82bb5 100644 --- a/Tests/Foundation/Tests/TestNSData.swift +++ b/Tests/Foundation/Tests/TestNSData.swift @@ -1189,6 +1189,8 @@ extension TestNSData { XCTAssertEqual(d.firstRange(of: Data([2])), 1..<2) XCTAssertEqual(d.firstRange(of: Data([3])), 2..<3) XCTAssertEqual(d.firstRange(of: Data([0])), 6..<7) + XCTAssertEqual(d.firstRange(of: Data([2, 3, 0])), 4..<7) + XCTAssertEqual(d.firstRange(of: Data([1, 2, 3, 1, 2, 3, 0])), 0..<7) } func testLastRange() { @@ -1197,6 +1199,9 @@ extension TestNSData { XCTAssertEqual(d.lastRange(of: Data([2])), 5..<6) XCTAssertEqual(d.lastRange(of: Data([3])), 6..<7) XCTAssertEqual(d.lastRange(of: Data([0])), 0..<1) + XCTAssertEqual(d.lastRange(of: Data([0, 1, 2])), 0..<3) + XCTAssertEqual(d.lastRange(of: Data([1, 2, 3])), 4..<7) + XCTAssertEqual(d.lastRange(of: Data([0, 1, 2, 3, 1, 2, 3])), 0..<7) } func testDataInSet() { From 3d5c05a182986ff9e5ec1442ec602d355cb53a09 Mon Sep 17 00:00:00 2001 From: Frizlab Date: Mon, 29 Mar 2021 10:15:03 +0200 Subject: [PATCH 4/4] Fix firstRange and lastRange new tests --- Sources/Foundation/DataProtocol.swift | 12 +++++++----- Tests/Foundation/Tests/TestNSData.swift | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/Foundation/DataProtocol.swift b/Sources/Foundation/DataProtocol.swift index 83288bf884..0f3f0d9945 100644 --- a/Sources/Foundation/DataProtocol.swift +++ b/Sources/Foundation/DataProtocol.swift @@ -143,8 +143,9 @@ extension DataProtocol { return nil } var haystackIndex = r.lowerBound - let haystackEnd = index(r.upperBound, offsetBy: -data.count + 1) - while haystackIndex < haystackEnd { + let haystackEnd = r.upperBound + let haystackSearchEnd = index(r.upperBound, offsetBy: -data.count) + while haystackIndex <= haystackSearchEnd { var compareIndex = haystackIndex var needleIndex = data.startIndex let needleEnd = data.endIndex @@ -172,13 +173,14 @@ extension DataProtocol { return nil } var haystackIndex = r.upperBound - let haystackStart = index(r.lowerBound, offsetBy: data.count - 1) - while haystackIndex > haystackStart { + let haystackStart = r.lowerBound + let haystackSearchStart = index(r.lowerBound, offsetBy: data.count) + while haystackIndex >= haystackSearchStart { var compareIndex = haystackIndex var needleIndex = data.endIndex let needleStart = data.startIndex var matched = true - while compareIndex > haystackStart && needleIndex > needleStart { + while compareIndex >= haystackStart && needleIndex > needleStart { needleIndex = data.index(before: needleIndex) compareIndex = index(before: compareIndex) if self[compareIndex] != data[needleIndex] { diff --git a/Tests/Foundation/Tests/TestNSData.swift b/Tests/Foundation/Tests/TestNSData.swift index ec6db82bb5..22aea26f83 100644 --- a/Tests/Foundation/Tests/TestNSData.swift +++ b/Tests/Foundation/Tests/TestNSData.swift @@ -1190,6 +1190,7 @@ extension TestNSData { XCTAssertEqual(d.firstRange(of: Data([3])), 2..<3) XCTAssertEqual(d.firstRange(of: Data([0])), 6..<7) XCTAssertEqual(d.firstRange(of: Data([2, 3, 0])), 4..<7) + XCTAssertEqual(d.firstRange(of: Data([3, 1, 2])), 2..<5) XCTAssertEqual(d.firstRange(of: Data([1, 2, 3, 1, 2, 3, 0])), 0..<7) }