Skip to content

Commit 262aa13

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

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Tests/SwiftAlgorithmsTests/PartialSortTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,53 @@ 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 array = ["a", "b", "b", "c", "a"]
127+
.enumerated()
128+
.map { ($0.element, $0.offset) }
129+
.map(Something.init)
130+
131+
let expected = [
132+
Something("a", 0),
133+
Something("a", 4),
134+
Something("b", 1),
135+
Something("b", 2),
136+
Something("c", 3)
137+
]
138+
139+
XCTAssertEqual(
140+
array.sortedPrefix(5),
141+
expected
142+
)
143+
}
144+
}
145+
146+
private class Something: CustomDebugStringConvertible {
147+
var debugDescription: String { "Something(\"\(value)\", \(position)\"" }
148+
149+
let position: Int
150+
let value: String
151+
152+
init(_ value: String, _ position: Int) {
153+
self.value = value
154+
self.position = position
155+
}
156+
}
157+
158+
extension Something: Comparable {
159+
static func == (lhs: Something, rhs: Something) -> Bool {
160+
lhs.value == rhs.value
161+
}
162+
163+
static func < (lhs: Something, rhs: Something) -> Bool {
164+
lhs.value < rhs.value
165+
}
117166
}

0 commit comments

Comments
 (0)