mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-26 04:27:48 +00:00
[test] Adding missing BidirectionalCollection::partition(subrange:) and MutableCollection::partition(subrange:) unit tests (#95)
This commit is contained in:
1 parent
e077f67639
commit
ca8af77420
2 files changed
+108
-8
No files matched your search
@@ -64,14 +64,17 @@ final class PartitionTests: XCTestCase {
|
||||
func testStablePartitionWithSubrange() {
|
||||
for length in 10...20 {
|
||||
let a = Array(0..<length)
|
||||
for j in 0..<length {
|
||||
var b = a
|
||||
let partitionRange = 0..<j
|
||||
let condition = { $0 < j - 1 }
|
||||
let p = b.stablePartition(subrange: partitionRange, by: condition)
|
||||
XCTAssertEqual(p, partitionRange.count > 0 ? 1 : 0)
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
|
||||
XCTAssertEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
|
||||
for i in 0..<length {
|
||||
for j in 0...i {
|
||||
var b = a
|
||||
let partitionRange = j..<i
|
||||
let condition = { $0 < i - 1 }
|
||||
let p = b.stablePartition(subrange: partitionRange, by: condition)
|
||||
|
||||
XCTAssertEqual(p, partitionRange.count == 0 ? j : j + 1)
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
|
||||
XCTAssertEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,4 +91,46 @@ final class PartitionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testPartitionWithSubrangeBidirectionalCollection() {
|
||||
for length in 10...20 {
|
||||
let a = Array(0..<length)
|
||||
for i in 0..<length {
|
||||
for j in 0...i {
|
||||
var b = a
|
||||
let partitionRange = j..<i
|
||||
let condition = { $0 < i - 1 }
|
||||
let p = b.partition(subrange: partitionRange, by: condition)
|
||||
|
||||
XCTAssertEqual(p, partitionRange.count == 0 ? j : j + 1)
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
|
||||
XCTAssertUnorderedEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testPartitionWithSubrangeMutableCollection() {
|
||||
for length in 10...20 {
|
||||
let a = Array(0..<length)
|
||||
for i in 0..<length {
|
||||
for j in 0...i {
|
||||
var b = a.eraseToAnyMutableCollection()
|
||||
var bdc = a
|
||||
let partitionRange = j..<i
|
||||
let condition = { $0 < i - 1 }
|
||||
let p = b.partition(subrange: partitionRange, by: condition)
|
||||
let bdcp = bdc.partition(subrange: partitionRange, by: condition)
|
||||
|
||||
XCTAssertEqual(p, partitionRange.count == 0 ? j : j + 1)
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], a[partitionRange].filter { !condition($0) })
|
||||
XCTAssertUnorderedEqualSequences(b[p..<partitionRange.upperBound], a[partitionRange].filter(condition))
|
||||
|
||||
// Must produce the same result as the `BidirectionalCollection` specialized overload.
|
||||
XCTAssertEqualSequences(b[partitionRange.lowerBound..<p], bdc[partitionRange.lowerBound..<bdcp])
|
||||
XCTAssertUnorderedEqualSequences(b[p..<partitionRange.upperBound], bdc[bdcp..<partitionRange.upperBound])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,34 @@ struct SplitMix64: RandomNumberGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// An eraser helper to any mutable collection
|
||||
struct AnyMutableCollection<Base> 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<Self> {
|
||||
AnyMutableCollection(base: self)
|
||||
}
|
||||
}
|
||||
|
||||
func XCTAssertEqualSequences<S1: Sequence, S2: Sequence>(
|
||||
_ expression1: @autoclosure () throws -> S1,
|
||||
_ expression2: @autoclosure () throws -> S2,
|
||||
@@ -56,6 +84,33 @@ func XCTAssertEqualSequences<S1: Sequence, S2: Sequence>(
|
||||
message(), file: file, line: line)
|
||||
}
|
||||
|
||||
// Two sequences contains exactly the same element but not necessarily in the same order.
|
||||
func XCTAssertUnorderedEqualSequences<S1: Sequence, S2: Sequence>(
|
||||
_ 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<S1: Sequence, S2: Sequence>(
|
||||
_ expression1: @autoclosure () throws -> S1,
|
||||
_ expression2: @autoclosure () throws -> S2,
|
||||
|
||||
Reference in new issue
Block a user