Skip to content

Commit 1d03780

Browse files
committed
[stdlib] Update tests for new partition APIs
Special thanks to @aschwaighofer for help with these tests and fix-its!
1 parent d7ee560 commit 1d03780

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -907,16 +907,20 @@ func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
907907

908908
var c = makeWrappedCollectionWithComparableElement(elements)
909909
% end
910-
910+
let pivotElt = c.first
911+
var pivot = c.startIndex
911912
% if predicate:
912913
let closureLifetimeTracker = LifetimeTracked(0)
913-
let pivot = c.partition() {
914-
(lhs, rhs) in
915-
_blackHole(closureLifetimeTracker)
916-
return extract(lhs).value < extract(rhs).value
914+
if let first = pivotElt {
915+
pivot = c.partition(by: {
916+
val in
917+
_blackHole(closureLifetimeTracker)
918+
return !(extract(val).value < extract(first).value) })
917919
}
918920
% else:
919-
let pivot = c.partition()
921+
if let first = c.first {
922+
pivot = c.partition(by: { !($0 < first) })
923+
}
920924
% end
921925

922926
// Check that we didn't lose any elements.
@@ -927,12 +931,12 @@ func checkPartition_${'Predicate' if predicate else 'WhereElementIsComparable'}(
927931
// All the elements in the first partition are less than the pivot
928932
// value.
929933
for i in c[c.startIndex..<pivot].indices {
930-
expectLT(extract(c[i]).value, extract(c[pivot]).value)
934+
expectLT(extract(c[i]).value, extract(pivotElt!).value)
931935
}
932936
// All the elements in the second partition are greater or equal to
933937
// the pivot value.
934938
for i in c[pivot..<c.endIndex].indices {
935-
expectLE(extract(c[pivot]).value, extract(c[i]).value)
939+
expectLE(extract(pivotElt!).value, extract(c[i]).value)
936940
}
937941
}
938942
}
@@ -988,13 +992,18 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
988992

989993
% if predicate:
990994
let closureLifetimeTracker = LifetimeTracked(0)
991-
let pivot = lc.partition() {
992-
(lhs, rhs) in
993-
_blackHole(closureLifetimeTracker)
994-
return extract(lhs).value < extract(rhs).value
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) })
9951001
}
9961002
% else:
997-
let pivot = lc.partition()
1003+
var pivot = lc.startIndex
1004+
if let first = lc.first {
1005+
pivot = lc.partition(by: { !($0 < first) })
1006+
}
9981007
% end
9991008

10001009
expectEqual(

test/1_stdlib/Renames.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ func _CollectionAlgorithms<C : MutableCollection, I>(c: C, i: I) where C : Rando
7272
var c = c
7373
_ = c.partition(i..<i) { _, _ in true } // expected-error {{slice the collection using the range, and call partition(by:)}} {{none}}
7474
c.sortInPlace { _, _ in true } // expected-error {{'sortInPlace' has been renamed to 'sort(by:)'}} {{5-16=sort}} {{none}}
75+
_ = c.partition { _, _ in true } // expected-error {{call partition(by:)}} {{none}}
7576
}
7677

7778
func _CollectionAlgorithms<C : MutableCollection, I>(c: C, i: I) where C : RandomAccessCollection, C.Iterator.Element : Comparable, C.Index == I {
7879
var c = c
79-
_ = c.partition(i..<i) // expected-error {{slice the collection using the range, and call partition()}} {{none}}
80+
_ = c.partition() // expected-error {{call partition(by:)}} {{none}}
81+
_ = c.partition(i..<i) // expected-error {{slice the collection using the range, and call partition(by:)}} {{none}}
8082
c.sortInPlace() // expected-error {{'sortInPlace()' has been renamed to 'sort()'}} {{5-16=sort}} {{none}}
8183
}
8284

test/1_stdlib/sort_integers.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func randomize(_ size: Int, _ verify: ([Int]) -> Void) {
3636
var arr : [Int] = []
3737
var N = 1
3838
var M = 1
39-
for i in 0..<size {
39+
for _ in 0..<size {
4040
N = N * 19 % 1024
4141
M = (N + M) % size
4242
arr.append(N)
@@ -79,19 +79,24 @@ print("Test1 - Done")
7979
let partition_verifier: ([Int]) -> Void = {
8080
var y = $0
8181
// Partition() returns the index to the pivot value.
82-
let idx = y.partition()
83-
// Check that all of the elements in the first partition are smaller or
84-
// equal 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+
}
88+
// Check that all of the elements in the first partition are smaller than
89+
// the pivot value.
8590
for i in 0..<idx {
86-
if y[i] > y[idx] {
91+
if y[i] >= pivot {
8792
print("Error!\n", terminator: "")
8893
return
8994
}
9095
}
9196
// Check that all of the elements in the second partition are greater or
9297
// equal to the pivot value.
9398
for i in idx..<y.count - 1 {
94-
if y[i] < y[idx] {
99+
if y[i] < pivot {
95100
print("Error!\n", terminator: "")
96101
return
97102
}

test/Interpreter/protocol_extensions.swift

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

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

validation-test/stdlib/Algorithm.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ Algorithm.test("invalidOrderings") {
149149
withInvalidOrderings {
150150
var a: A<Int>
151151
a = randomArray()
152-
_ = a.partition(by: $0)
152+
let lt = $0
153+
if let first = a.first {
154+
_ = a.partition(by: { !lt($0, first) })
155+
}
153156
}
154157
/*
155158
// FIXME: Disabled due to <rdar://problem/17734737> Unimplemented:

validation-test/stdlib/Sort.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ Algorithm.test("sort/CollectionsWithUnusualIndices") {
153153

154154
Algorithm.test("partition/CrashOnSingleElement") {
155155
var a = DefaultedMutableRandomAccessCollection([10])
156-
expectEqual(a.startIndex, a.partition())
156+
let first = a.first!
157+
expectEqual(a.startIndex, a.partition(by: {$0 >= first}))
157158
}
158159

159160
runAllTests()

0 commit comments

Comments
 (0)