Skip to content

Commit 1d22ef9

Browse files
authored
Add more tests (#4)
1 parent 36e9a39 commit 1d22ef9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Tests/SwiftAlgorithmsTests/PartialSortTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,33 @@ final class PartialSortTests: XCTestCase {
114114
[1, 2, 3, 4, 7, 20, 70, 90, 100]
115115
)
116116
}
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+
}
117146
}

0 commit comments

Comments
 (0)