Skip to content

Commit 29bbff2

Browse files
committed
[stdlib] Update partition tests with feedback
1 parent 13d1ac5 commit 29bbff2

File tree

4 files changed

+44
-84
lines changed

4 files changed

+44
-84
lines changed

stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -879,49 +879,24 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
879879
// partition()
880880
//===----------------------------------------------------------------------===//
881881

882-
% for predicate in [False, True]:
883-
884-
func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
882+
func checkPartition(
885883
sequence: [Int],
886-
equalImpl: ((Int, Int) -> Bool),
884+
pivotValue: Int,
887885
lessImpl: ((Int, Int) -> Bool),
888886
verifyOrder: Bool
889887
) {
890-
% if predicate:
891888
let extract = extractValue
892889
let elements: [OpaqueValue<Int>] =
893890
zip(sequence, 0..<sequence.count).map {
894891
OpaqueValue($0, identity: $1)
895892
}
896893

897894
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:
913895
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+
})
925900

926901
// Check that we didn't lose any elements.
927902
let identities = c.map { extract($0).identity }
@@ -931,80 +906,73 @@ func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
931906
// All the elements in the first partition are less than the pivot
932907
// value.
933908
for i in c[c.startIndex..<pivot].indices {
934-
expectLT(extract(c[i]).value, extract(pivotElt!).value)
909+
expectLT(extract(c[i]).value, pivotValue)
935910
}
936911
// All the elements in the second partition are greater or equal to
937912
// the pivot value.
938913
for i in c[pivot..<c.endIndex].indices {
939-
expectLE(extract(pivotElt!).value, extract(c[i]).value)
914+
expectGE(extract(c[i]).value, pivotValue)
940915
}
941916
}
942917
}
943918

944-
self.test("\(testNamePrefix).partition/${'Predicate' if predicate else 'WhereElementIsComparable'}") {
919+
self.test("\(testNamePrefix).partition") {
945920
for test in partitionExhaustiveTests {
946921
forAllPermutations(test.sequence) { (sequence) in
947-
checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
922+
checkPartition(
948923
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,
950939
lessImpl: { $0 < $1 },
951940
verifyOrder: true)
952941
}
953942
}
954943
}
955944

956-
self.test("\(testNamePrefix).partition/${'Predicate' if predicate else 'WhereElementIsComparable'}/InvalidOrderings") {
945+
self.test("\(testNamePrefix).partition/InvalidOrderings") {
957946
withInvalidOrderings { (comparisonPredicate) in
958947
for i in 0..<7 {
959948
forAllPermutations(i) { (sequence) in
960-
checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
949+
checkPartition(
961950
sequence: sequence,
962-
equalImpl: {
963-
!comparisonPredicate($0, $1) &&
964-
!comparisonPredicate($1, $0)
965-
},
951+
pivotValue: sequence.first ?? 0,
966952
lessImpl: comparisonPredicate,
967953
verifyOrder: false)
968954
}
969955
}
970956
}
971957
}
972958

973-
self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported/${'Predicate' if predicate else 'WhereElementIsComparable'}") {
959+
self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBufferPointerIfSupported") {
974960
let sequence = [ 5, 4, 3, 2, 1 ]
975-
% if predicate:
976961
let extract = extractValue
977962
let elements: [OpaqueValue<Int>] =
978963
zip(sequence, 0..<sequence.count).map {
979964
OpaqueValue($0, identity: $1)
980965
}
981966
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
990967

991968
var lc = LoggingMutableRandomAccessCollection(wrapping: c)
992969

993-
% if predicate:
994970
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+
})
1008976

1009977
expectEqual(
1010978
1, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
@@ -1016,8 +984,6 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
1016984
expectEqualSequence([ 1, 4, 3, 2, 5 ], lc.map { extract($0).value })
1017985
}
1018986

1019-
% end
1020-
1021987
//===----------------------------------------------------------------------===//
1022988

1023989
} // addMutableRandomAccessCollectionTests

test/1_stdlib/sort_integers.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,22 @@ print("Test1 - Done")
7878

7979
let partition_verifier: ([Int]) -> Void = {
8080
var y = $0
81-
// Partition() returns the index to the pivot value.
82-
var idx = y.startIndex
83-
var pivot = -1
84-
if let first = y.first {
85-
pivot = first
86-
idx = y.partition(by: { $0 >= first })
87-
}
81+
// partition(by:) returns the index to the pivot value.
82+
let first = y.first
83+
let idx = y.partition(by: { $0 >= first! })
84+
8885
// Check that all of the elements in the first partition are smaller than
8986
// the pivot value.
9087
for i in 0..<idx {
91-
if y[i] >= pivot {
88+
if y[i] >= first! {
9289
print("Error!\n", terminator: "")
9390
return
9491
}
9592
}
9693
// Check that all of the elements in the second partition are greater or
9794
// equal to the pivot value.
9895
for i in idx..<y.count - 1 {
99-
if y[i] < pivot {
96+
if y[i] < first! {
10097
print("Error!\n", terminator: "")
10198
return
10299
}

test/Interpreter/protocol_extensions.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ extension MutableCollection
7676
where Self: RandomAccessCollection, Self.Iterator.Element : Comparable {
7777

7878
public final mutating func myPartition() -> Index {
79-
if let first = self.first {
80-
return self.partition(by: { $0 >= first})
81-
}
82-
return self.startIndex
79+
let first = self.first
80+
return self.partition(by: { $0 >= first! })
8381
}
8482
}
8583

validation-test/stdlib/Algorithm.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ Algorithm.test("invalidOrderings") {
150150
var a: A<Int>
151151
a = randomArray()
152152
let lt = $0
153-
if let first = a.first {
154-
_ = a.partition(by: { !lt($0, first) })
155-
}
153+
let first = a.first
154+
_ = a.partition(by: { !lt($0, first!) })
156155
}
157156
/*
158157
// FIXME: Disabled due to <rdar://problem/17734737> Unimplemented:

0 commit comments

Comments
 (0)