@@ -114,4 +114,33 @@ final class PartialSortTests: XCTestCase {
114
114
[ 1 , 2 , 3 , 4 , 7 , 20 , 70 , 90 , 100 ]
115
115
)
116
116
}
117
+
118
+ func testSortedPrefixWithHugePrefix( ) {
119
+ XCTAssertEqual (
120
+ [ 4 , 2 , 1 , 3 ] . sortedPrefix ( . max) ,
121
+ [ 1 , 2 , 3 , 4 ]
122
+ )
123
+ }
124
+
125
+ func testStability( ) {
126
+ assertStability ( [ 1 , 1 , 1 , 2 , 5 , 7 , 3 , 6 , 2 , 5 , 7 , 3 , 6 ] , withPrefix: 3 )
127
+ assertStability ( [ 1 , 1 , 1 , 2 , 5 , 7 , 3 , 6 , 2 , 5 , 7 , 3 , 6 ] , withPrefix: 6 )
128
+ assertStability ( [ 1 , 1 , 1 , 2 , 5 , 7 , 3 , 6 , 2 , 5 , 7 , 3 , 6 ] , withPrefix: 20 )
129
+ assertStability ( [ 1 , 1 , 1 , 2 , 5 , 7 , 3 , 6 , 2 , 5 , 7 , 3 , 6 ] , withPrefix: 1000 )
130
+ }
131
+
132
+ func assertStability(
133
+ _ actual: [ Int ] ,
134
+ withPrefix prefixCount: Int ,
135
+ file: StaticString = #file,
136
+ line: UInt = #line
137
+ ) {
138
+ let indexed = actual. enumerated ( )
139
+ let sorted = indexed. map { $0 } . sortedPrefix ( prefixCount) { $0. element < $1. element }
140
+
141
+ for element in Set ( actual) {
142
+ let filtered = sorted. filter { $0. element == element } . map ( \. offset)
143
+ XCTAssertEqual ( filtered, filtered. sorted ( ) )
144
+ }
145
+ }
117
146
}
0 commit comments