Skip to content

Commit 92ad413

Browse files
committed
Add more tests
- Prefix greater than input's count - Algorithm stability
1 parent 36e9a39 commit 92ad413

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Tests/SwiftAlgorithmsTests/PartialSortTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,46 @@ 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 testSortedPrfixStability() {
126+
let random = (0...100)
127+
.map { (UUID().uuidString, $0) }
128+
.map(Something.init)
129+
130+
let stable = [Something("0", 100), Something("0", 101), Something("0", 102)]
131+
let sample = random + stable
132+
133+
XCTAssertEqual(
134+
Array(sample.sortedPrefix(.max)[0...2]),
135+
stable
136+
)
137+
}
138+
}
139+
140+
private class Something: CustomDebugStringConvertible {
141+
var debugDescription: String { "\(value) - \(position)" }
142+
let value: String
143+
let position: Int
144+
145+
init(_ value: String, _ position: Int) {
146+
self.value = value
147+
self.position = position
148+
}
149+
}
150+
151+
extension Something: Comparable {
152+
static func == (lhs: Something, rhs: Something) -> Bool {
153+
lhs.value == rhs.value
154+
}
155+
156+
static func < (lhs: Something, rhs: Something) -> Bool {
157+
lhs.value < rhs.value
158+
}
117159
}

0 commit comments

Comments
 (0)