diff --git a/Tests/SwiftAlgorithmsTests/PartitionTests.swift b/Tests/SwiftAlgorithmsTests/PartitionTests.swift index ad8ba8e..cbc0a08 100644 --- a/Tests/SwiftAlgorithmsTests/PartitionTests.swift +++ b/Tests/SwiftAlgorithmsTests/PartitionTests.swift @@ -64,14 +64,17 @@ final class PartitionTests: XCTestCase { func testStablePartitionWithSubrange() { for length in 10...20 { let a = Array(0.. 0 ? 1 : 0) - XCTAssertEqualSequences(b[partitionRange.lowerBound.. where Base: MutableCollection { + var base: Base +} + +extension AnyMutableCollection: MutableCollection { + typealias Index = Base.Index + typealias Element = Base.Element + + var startIndex: Base.Index { base.startIndex } + var endIndex: Base.Index { base.endIndex } + + func index(after i: Index) -> Index { + return base.index(after: i) + } + + subscript(position: Base.Index) -> Base.Element { + _read { yield base[position] } + set { base[position] = newValue } + } +} + +extension MutableCollection { + func eraseToAnyMutableCollection() -> AnyMutableCollection { + AnyMutableCollection(base: self) + } +} + func XCTAssertEqualSequences( _ expression1: @autoclosure () throws -> S1, _ expression2: @autoclosure () throws -> S2, @@ -56,6 +84,33 @@ func XCTAssertEqualSequences( message(), file: file, line: line) } +// Two sequences contains exactly the same element but not necessarily in the same order. +func XCTAssertUnorderedEqualSequences( + _ expression1: @autoclosure () throws -> S1, + _ expression2: @autoclosure () throws -> S2, + file: StaticString = #file, line: UInt = #line +) rethrows where S1.Element: Equatable, S1.Element == S2.Element { + var s1 = Array(try expression1()) + var missing: [S1.Element] = [] + for elt in try expression2() { + guard let idx = s1.firstIndex(of: elt) else { + missing.append(elt) + continue + } + s1.remove(at: idx) + } + + XCTAssertTrue( + missing.isEmpty, "first sequence missing '\(missing)' elements from second sequence", + file: file, line: line + ) + + XCTAssertTrue( + s1.isEmpty, "first sequence contains \(s1) missing from second sequence", + file: file, line: line + ) +} + func XCTAssertEqualSequences( _ expression1: @autoclosure () throws -> S1, _ expression2: @autoclosure () throws -> S2,