@@ -574,6 +574,87 @@ self.test("\(testNamePrefix).sorted/${'Predicate' if predicate else 'WhereElemen
574
574
575
575
% end
576
576
577
+ //===----------------------------------------------------------------------===//
578
+ // partition()
579
+ //===----------------------------------------------------------------------===//
580
+
581
+ func checkPartition(
582
+ sequence: [Int],
583
+ pivotValue: Int,
584
+ lessImpl: ((Int, Int) -> Bool),
585
+ verifyOrder: Bool
586
+ ) {
587
+ let extract = extractValue
588
+ let elements: [OpaqueValue<Int>] =
589
+ zip(sequence, 0..<sequence.count).map {
590
+ OpaqueValue($0, identity: $1)
591
+ }
592
+
593
+ var c = makeWrappedCollection(elements)
594
+ let closureLifetimeTracker = LifetimeTracked(0)
595
+ let pivot = c.partition(by: { val in
596
+ _blackHole(closureLifetimeTracker)
597
+ return !lessImpl(extract(val).value, pivotValue)
598
+ })
599
+
600
+ // Check that we didn't lose any elements.
601
+ let identities = c.map { extract($0).identity }
602
+ expectEqualsUnordered(0..<sequence.count, identities)
603
+
604
+ if verifyOrder {
605
+ // All the elements in the first partition are less than the pivot
606
+ // value.
607
+ for i in c[c.startIndex..<pivot].indices {
608
+ expectLT(extract(c[i]).value, pivotValue)
609
+ }
610
+ // All the elements in the second partition are greater or equal to
611
+ // the pivot value.
612
+ for i in c[pivot..<c.endIndex].indices {
613
+ expectGE(extract(c[i]).value, pivotValue)
614
+ }
615
+ }
616
+ }
617
+
618
+ self.test("\(testNamePrefix).partition") {
619
+ for test in partitionExhaustiveTests {
620
+ forAllPermutations(test.sequence) { (sequence) in
621
+ checkPartition(
622
+ sequence: sequence,
623
+ pivotValue: sequence.first ?? 0,
624
+ lessImpl: { $0 < $1 },
625
+ verifyOrder: true)
626
+
627
+ // Pivot value where all elements will pass the partitioning predicate
628
+ checkPartition(
629
+ sequence: sequence,
630
+ pivotValue: Int.min,
631
+ lessImpl: { $0 < $1 },
632
+ verifyOrder: true)
633
+
634
+ // Pivot value where no element will pass the partitioning predicate
635
+ checkPartition(
636
+ sequence: sequence,
637
+ pivotValue: Int.max,
638
+ lessImpl: { $0 < $1 },
639
+ verifyOrder: true)
640
+ }
641
+ }
642
+ }
643
+
644
+ self.test("\(testNamePrefix).partition/InvalidOrderings") {
645
+ withInvalidOrderings { (comparisonPredicate) in
646
+ for i in 0..<7 {
647
+ forAllPermutations(i) { (sequence) in
648
+ checkPartition(
649
+ sequence: sequence,
650
+ pivotValue: sequence.first ?? 0,
651
+ lessImpl: comparisonPredicate,
652
+ verifyOrder: false)
653
+ }
654
+ }
655
+ }
656
+ }
657
+
577
658
//===----------------------------------------------------------------------===//
578
659
579
660
} // addMutableCollectionTests
@@ -879,83 +960,7 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
879
960
// partition()
880
961
//===----------------------------------------------------------------------===//
881
962
882
- func checkPartition(
883
- sequence: [Int],
884
- pivotValue: Int,
885
- lessImpl: ((Int, Int) -> Bool),
886
- verifyOrder: Bool
887
- ) {
888
- let extract = extractValue
889
- let elements: [OpaqueValue<Int>] =
890
- zip(sequence, 0..<sequence.count).map {
891
- OpaqueValue($0, identity: $1)
892
- }
893
-
894
- var c = makeWrappedCollection(elements)
895
- let closureLifetimeTracker = LifetimeTracked(0)
896
- let pivot = c.partition(by: { val in
897
- _blackHole(closureLifetimeTracker)
898
- return !lessImpl(extract(val).value, pivotValue)
899
- })
900
-
901
- // Check that we didn't lose any elements.
902
- let identities = c.map { extract($0).identity }
903
- expectEqualsUnordered(0..<sequence.count, identities)
904
-
905
- if verifyOrder {
906
- // All the elements in the first partition are less than the pivot
907
- // value.
908
- for i in c[c.startIndex..<pivot].indices {
909
- expectLT(extract(c[i]).value, pivotValue)
910
- }
911
- // All the elements in the second partition are greater or equal to
912
- // the pivot value.
913
- for i in c[pivot..<c.endIndex].indices {
914
- expectGE(extract(c[i]).value, pivotValue)
915
- }
916
- }
917
- }
918
-
919
- self.test("\(testNamePrefix).partition") {
920
- for test in partitionExhaustiveTests {
921
- forAllPermutations(test.sequence) { (sequence) in
922
- checkPartition(
923
- sequence: sequence,
924
- pivotValue: sequence.first ?? 0,
925
- lessImpl: { $0 < $1 },
926
- verifyOrder: true)
927
-
928
- // Pivot value where all elements will pass the partitioning predicate
929
- checkPartition(
930
- sequence: sequence,
931
- pivotValue: Int.min,
932
- lessImpl: { $0 < $1 },
933
- verifyOrder: true)
934
-
935
- // Pivot value where no element will pass the partitioning predicate
936
- checkPartition(
937
- sequence: sequence,
938
- pivotValue: Int.max,
939
- lessImpl: { $0 < $1 },
940
- verifyOrder: true)
941
- }
942
- }
943
- }
944
-
945
- self.test("\(testNamePrefix).partition/InvalidOrderings") {
946
- withInvalidOrderings { (comparisonPredicate) in
947
- for i in 0..<7 {
948
- forAllPermutations(i) { (sequence) in
949
- checkPartition(
950
- sequence: sequence,
951
- pivotValue: sequence.first ?? 0,
952
- lessImpl: comparisonPredicate,
953
- verifyOrder: false)
954
- }
955
- }
956
- }
957
- }
958
-
963
+ // FIXME(tests): Move to addMutableCollectionTests?
959
964
self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported") {
960
965
let sequence = [ 5, 4, 3, 2, 1 ]
961
966
let extract = extractValue
0 commit comments