@@ -879,49 +879,24 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
879
879
// partition()
880
880
//===----------------------------------------------------------------------===//
881
881
882
- % for predicate in [False, True]:
883
-
884
- func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
882
+ func checkPartition(
885
883
sequence: [Int],
886
- equalImpl: (( Int, Int) -> Bool) ,
884
+ pivotValue: Int,
887
885
lessImpl: ((Int, Int) -> Bool),
888
886
verifyOrder: Bool
889
887
) {
890
- % if predicate:
891
888
let extract = extractValue
892
889
let elements: [OpaqueValue<Int>] =
893
890
zip(sequence, 0..<sequence.count).map {
894
891
OpaqueValue($0, identity: $1)
895
892
}
896
893
897
894
var c = makeWrappedCollection(elements)
898
- % else:
899
- MinimalComparableValue.equalImpl.value = equalImpl
900
- MinimalComparableValue.lessImpl.value = lessImpl
901
-
902
- let extract = extractValueFromComparable
903
- let elements: [MinimalComparableValue] =
904
- zip(sequence, 0..<sequence.count).map {
905
- MinimalComparableValue($0, identity: $1)
906
- }
907
-
908
- var c = makeWrappedCollectionWithComparableElement(elements)
909
- % end
910
- let pivotElt = c.first
911
- var pivot = c.startIndex
912
- % if predicate:
913
895
let closureLifetimeTracker = LifetimeTracked(0)
914
- if let first = pivotElt {
915
- pivot = c.partition(by: {
916
- val in
917
- _blackHole(closureLifetimeTracker)
918
- return !(extract(val).value < extract(first).value) })
919
- }
920
- % else:
921
- if let first = c.first {
922
- pivot = c.partition(by: { !($0 < first) })
923
- }
924
- % end
896
+ let pivot = c.partition(by: { val in
897
+ _blackHole(closureLifetimeTracker)
898
+ return !lessImpl(extract(val).value, pivotValue)
899
+ })
925
900
926
901
// Check that we didn't lose any elements.
927
902
let identities = c.map { extract($0).identity }
@@ -931,80 +906,73 @@ func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
931
906
// All the elements in the first partition are less than the pivot
932
907
// value.
933
908
for i in c[c.startIndex..<pivot].indices {
934
- expectLT(extract(c[i]).value, extract(pivotElt!).value )
909
+ expectLT(extract(c[i]).value, pivotValue )
935
910
}
936
911
// All the elements in the second partition are greater or equal to
937
912
// the pivot value.
938
913
for i in c[pivot..<c.endIndex].indices {
939
- expectLE (extract(pivotElt!).value, extract( c[i]).value)
914
+ expectGE (extract(c[i]).value, pivotValue )
940
915
}
941
916
}
942
917
}
943
918
944
- self.test("\(testNamePrefix).partition/${'Predicate' if predicate else 'WhereElementIsComparable'} ") {
919
+ self.test("\(testNamePrefix).partition") {
945
920
for test in partitionExhaustiveTests {
946
921
forAllPermutations(test.sequence) { (sequence) in
947
- checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'} (
922
+ checkPartition (
948
923
sequence: sequence,
949
- equalImpl: { $0 == $1 },
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,
950
939
lessImpl: { $0 < $1 },
951
940
verifyOrder: true)
952
941
}
953
942
}
954
943
}
955
944
956
- self.test("\(testNamePrefix).partition/${'Predicate' if predicate else 'WhereElementIsComparable'}/ InvalidOrderings") {
945
+ self.test("\(testNamePrefix).partition/InvalidOrderings") {
957
946
withInvalidOrderings { (comparisonPredicate) in
958
947
for i in 0..<7 {
959
948
forAllPermutations(i) { (sequence) in
960
- checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'} (
949
+ checkPartition (
961
950
sequence: sequence,
962
- equalImpl: {
963
- !comparisonPredicate($0, $1) &&
964
- !comparisonPredicate($1, $0)
965
- },
951
+ pivotValue: sequence.first ?? 0,
966
952
lessImpl: comparisonPredicate,
967
953
verifyOrder: false)
968
954
}
969
955
}
970
956
}
971
957
}
972
958
973
- self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported/${'Predicate' if predicate else 'WhereElementIsComparable'} ") {
959
+ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported") {
974
960
let sequence = [ 5, 4, 3, 2, 1 ]
975
- % if predicate:
976
961
let extract = extractValue
977
962
let elements: [OpaqueValue<Int>] =
978
963
zip(sequence, 0..<sequence.count).map {
979
964
OpaqueValue($0, identity: $1)
980
965
}
981
966
let c = makeWrappedCollection(elements)
982
- % else:
983
- let extract = extractValueFromComparable
984
- let elements: [MinimalComparableValue] =
985
- zip(sequence, 0..<sequence.count).map {
986
- MinimalComparableValue($0, identity: $1)
987
- }
988
- let c = makeWrappedCollectionWithComparableElement(elements)
989
- % end
990
967
991
968
var lc = LoggingMutableRandomAccessCollection(wrapping: c)
992
969
993
- % if predicate:
994
970
let closureLifetimeTracker = LifetimeTracked(0)
995
- var pivot = lc.startIndex
996
- if let first = c.first {
997
- pivot = lc.partition(by: {
998
- val in
999
- _blackHole(closureLifetimeTracker)
1000
- return !(extract(val).value < extract(first).value) })
1001
- }
1002
- % else:
1003
- var pivot = lc.startIndex
1004
- if let first = lc.first {
1005
- pivot = lc.partition(by: { !($0 < first) })
1006
- }
1007
- % end
971
+ let first = c.first
972
+ let pivot = lc.partition(by: { val in
973
+ _blackHole(closureLifetimeTracker)
974
+ return !(extract(val).value < extract(first!).value)
975
+ })
1008
976
1009
977
expectEqual(
1010
978
1, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
@@ -1016,8 +984,6 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
1016
984
expectEqualSequence([ 1, 4, 3, 2, 5 ], lc.map { extract($0).value })
1017
985
}
1018
986
1019
- % end
1020
-
1021
987
//===----------------------------------------------------------------------===//
1022
988
1023
989
} // addMutableRandomAccessCollectionTests
0 commit comments