12
12
13
13
import StdlibUnittest
14
14
15
- // These tests are shared between partition() and sort().
15
+ // These tests are shared between partition(by: ) and sort().
16
16
public struct PartitionExhaustiveTest {
17
17
public let sequence: [Int]
18
18
public let loc: SourceLoc
@@ -67,6 +67,18 @@ internal func _mapInPlace<C : MutableCollection>(
67
67
}
68
68
}
69
69
70
+ internal func makeBufferAccessLoggingMutableCollection<
71
+ C : MutableCollection & BidirectionalCollection
72
+ >(wrapping c: C) -> BufferAccessLoggingMutableBidirectionalCollection<C> {
73
+ return BufferAccessLoggingMutableBidirectionalCollection(wrapping: c)
74
+ }
75
+
76
+ internal func makeBufferAccessLoggingMutableCollection<
77
+ C : MutableCollection & RandomAccessCollection
78
+ >(wrapping c: C) -> BufferAccessLoggingMutableBidirectionalCollection<C> {
79
+ return BufferAccessLoggingMutableBidirectionalCollection(wrapping: c)
80
+ }
81
+
70
82
extension TestSuite {
71
83
public func addMutableCollectionTests<
72
84
C : MutableCollection,
@@ -575,7 +587,7 @@ self.test("\(testNamePrefix).sorted/${'Predicate' if predicate else 'WhereElemen
575
587
% end
576
588
577
589
//===----------------------------------------------------------------------===//
578
- // partition()
590
+ // partition(by: )
579
591
//===----------------------------------------------------------------------===//
580
592
581
593
func checkPartition(
@@ -584,7 +596,6 @@ func checkPartition(
584
596
lessImpl: ((Int, Int) -> Bool),
585
597
verifyOrder: Bool
586
598
) {
587
- let extract = extractValue
588
599
let elements: [OpaqueValue<Int>] =
589
600
zip(sequence, 0..<sequence.count).map {
590
601
OpaqueValue($0, identity: $1)
@@ -594,23 +605,23 @@ func checkPartition(
594
605
let closureLifetimeTracker = LifetimeTracked(0)
595
606
let pivot = c.partition(by: { val in
596
607
_blackHole(closureLifetimeTracker)
597
- return !lessImpl(extract (val).value, pivotValue)
608
+ return !lessImpl(extractValue (val).value, pivotValue)
598
609
})
599
610
600
611
// Check that we didn't lose any elements.
601
- let identities = c.map { extract ($0).identity }
612
+ let identities = c.map { extractValue ($0).identity }
602
613
expectEqualsUnordered(0..<sequence.count, identities)
603
614
604
615
if verifyOrder {
605
616
// All the elements in the first partition are less than the pivot
606
617
// value.
607
618
for i in c[c.startIndex..<pivot].indices {
608
- expectLT(extract (c[i]).value, pivotValue)
619
+ expectLT(extractValue (c[i]).value, pivotValue)
609
620
}
610
621
// All the elements in the second partition are greater or equal to
611
622
// the pivot value.
612
623
for i in c[pivot..<c.endIndex].indices {
613
- expectGE(extract (c[i]).value, pivotValue)
624
+ expectGE(extractValue (c[i]).value, pivotValue)
614
625
}
615
626
}
616
627
}
@@ -776,6 +787,37 @@ self.test("\(testNamePrefix).reverse()") {
776
787
}
777
788
}
778
789
790
+ //===----------------------------------------------------------------------===//
791
+ // partition(by:)
792
+ //===----------------------------------------------------------------------===//
793
+
794
+ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported") {
795
+ let sequence = [ 5, 4, 3, 2, 1 ]
796
+ let elements: [OpaqueValue<Int>] =
797
+ zip(sequence, 0..<sequence.count).map {
798
+ OpaqueValue($0, identity: $1)
799
+ }
800
+ let c = makeWrappedCollection(elements)
801
+ var lc = makeBufferAccessLoggingMutableCollection(wrapping: c)
802
+
803
+ let closureLifetimeTracker = LifetimeTracked(0)
804
+ let first = c.first
805
+ let pivot = lc.partition(by: { val in
806
+ _blackHole(closureLifetimeTracker)
807
+ return !(extractValue(val).value < extractValue(first!).value)
808
+ })
809
+
810
+ expectEqual(
811
+ 1, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
812
+ expectEqual(
813
+ withUnsafeMutableBufferPointerIsSupported ? 1 : 0,
814
+ lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[lc.dynamicType])
815
+
816
+ expectEqual(4, lc.distance(from: lc.startIndex, to: pivot))
817
+ expectEqualsUnordered([1, 2, 3, 4], lc.prefix(upTo: pivot).map { extractValue($0).value })
818
+ expectEqualsUnordered([5], lc.suffix(from: pivot).map { extractValue($0).value })
819
+ }
820
+
779
821
//===----------------------------------------------------------------------===//
780
822
781
823
} // addMutableBidirectionalCollectionTests
@@ -956,39 +998,6 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
956
998
957
999
% end
958
1000
959
- //===----------------------------------------------------------------------===//
960
- // partition()
961
- //===----------------------------------------------------------------------===//
962
-
963
- // FIXME(tests): Move to addMutableCollectionTests?
964
- self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported") {
965
- let sequence = [ 5, 4, 3, 2, 1 ]
966
- let extract = extractValue
967
- let elements: [OpaqueValue<Int>] =
968
- zip(sequence, 0..<sequence.count).map {
969
- OpaqueValue($0, identity: $1)
970
- }
971
- let c = makeWrappedCollection(elements)
972
-
973
- var lc = LoggingMutableRandomAccessCollection(wrapping: c)
974
-
975
- let closureLifetimeTracker = LifetimeTracked(0)
976
- let first = c.first
977
- let pivot = lc.partition(by: { val in
978
- _blackHole(closureLifetimeTracker)
979
- return !(extract(val).value < extract(first!).value)
980
- })
981
-
982
- expectEqual(
983
- 1, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
984
- expectEqual(
985
- withUnsafeMutableBufferPointerIsSupported ? 1 : 0,
986
- lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[lc.dynamicType])
987
-
988
- expectEqual(4, lc.distance(from: lc.startIndex, to: pivot))
989
- expectEqualSequence([ 1, 4, 3, 2, 5 ], lc.map { extract($0).value })
990
- }
991
-
992
1001
//===----------------------------------------------------------------------===//
993
1002
994
1003
} // addMutableRandomAccessCollectionTests
0 commit comments