Skip to content

Commit

Permalink
Merge pull request #33 from ole/fix-lastWhere
Browse files Browse the repository at this point in the history
Fix last(where:) implementation
  • Loading branch information
ole authored Feb 20, 2022
2 parents 55da517 + 39dfef5 commit 54c8639
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ extension SortedArray {
///
/// - Complexity: O(_log(n)_), where _n_ is the size of the array.
public func last(where predicate: (Element) throws -> Bool) rethrows -> Element? {
guard let index = try firstIndex(where: predicate) else { return nil }
guard let index = try lastIndex(where: predicate) else { return nil }
return self[index]
}
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/UnitTests/SortedArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ class SortedArrayTests: XCTestCase {
XCTAssertEqual(index, 2)
}

func testFirstWhereFindsFirstMatchingElement() {
let sut = SortedArray(unsorted: [4, 3, 2, 1])
let last = sut.first(where: { $0 >= 2 })
XCTAssertEqual(last, 2)
}

func testLastIndexOfFindsElementInMiddle() {
let sut = SortedArray(unsorted: ["a","z","r","k"])
let index = sut.lastIndex(of: "k")
Expand Down Expand Up @@ -350,6 +356,12 @@ class SortedArrayTests: XCTestCase {
XCTAssertEqual(index, 9)
}

func testLastWhereFindsLastMatchingElement() {
let sut = SortedArray(unsorted: [4, 3, 2, 1])
let last = sut.last(where: { $0 <= 3 })
XCTAssertEqual(last, 3)
}

func testsContains() {
let sut = SortedArray(unsorted: "Lorem ipsum")
XCTAssertTrue(sut.contains(" "))
Expand Down Expand Up @@ -532,6 +544,7 @@ extension SortedArrayTests {
("testFirstIndexWhereFindsFirstIndexOfDuplicateElements3", testFirstIndexWhereFindsFirstIndexOfDuplicateElements3),
("testFirstIndexWhereFindsFirstIndexOfDuplicateElements4", testFirstIndexWhereFindsFirstIndexOfDuplicateElements4),
("testFirstIndexWhereFindsFirstIndexOfDuplicateElements5", testFirstIndexWhereFindsFirstIndexOfDuplicateElements5),
("testFirstWhereFindsFirstMatchingElement", testFirstWhereFindsFirstMatchingElement),
("testLastIndexOfFindsElementInMiddle", testLastIndexOfFindsElementInMiddle),
("testLastIndexOfFindsFirstElement", testLastIndexOfFindsFirstElement),
("testLastIndexOfFindsLastElement", testLastIndexOfFindsLastElement),
Expand All @@ -550,6 +563,7 @@ extension SortedArrayTests {
("testLastIndexWhereFindsLastIndexOfDuplicateElements1", testLastIndexWhereFindsLastIndexOfDuplicateElements1),
("testLastIndexWhereFindsLastIndexOfDuplicateElements2", testLastIndexWhereFindsLastIndexOfDuplicateElements2),
("testLastIndexWhereFindsLastIndexOfDuplicateElements3", testLastIndexWhereFindsLastIndexOfDuplicateElements3),
("testLastWhereFindsLastMatchingElement", testLastWhereFindsLastMatchingElement),
("testsContains", testsContains),
("testMin", testMin),
("testMax", testMax),
Expand All @@ -575,3 +589,15 @@ extension SortedArrayTests {
]
}
}

struct Pair<A, B> {
let first: A
let second: B

init(_ first: A, _ second: B) {
self.first = first
self.second = second
}
}

extension Pair: Equatable where A: Equatable, B: Equatable {}

0 comments on commit 54c8639

Please sign in to comment.