Skip to content

Commit 08f5dd7

Browse files
committed
wip
1 parent ca42a0c commit 08f5dd7

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Sources/Algorithms/Stride.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ extension Stride: Collection {
103103
}
104104

105105
public func index(_ i: Index, offsetBy distance: Int) -> Index {
106-
precondition(i.base < base.endIndex, "Advancing past end index")
107-
let limit = distance > 0 ? endIndex : startIndex
108-
return index(i, offsetBy: distance, limitedBy: limit) ?? limit
106+
precondition(distance <= 0 || i.base < base.endIndex, "Advancing past end index")
107+
precondition(distance >= 0 || i.base > base.startIndex, "Incrementing past start index")
108+
return Index(base.index(i.base, offsetBy: distance * stride))
109109
}
110110
}
111111

Tests/SwiftAlgorithmsTests/StrideTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,16 @@ final class StridingTests: XCTestCase {
8888
XCTAssertEqual(a.striding(by: 11).count, [0].count)
8989
}
9090

91+
func testIndexTraversals() {
92+
validateIndexTraversals(
93+
(0...100).striding(by: 10)
94+
// (0...100).striding(by: 11)
95+
// (0...100).striding(by: 101)
96+
)
97+
}
98+
9199
func testDistance() {
92-
100+
93101
do {
94102
let a = (0...100).striding(by: 11)
95103
XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count)
@@ -128,7 +136,7 @@ final class StridingTests: XCTestCase {
128136
}
129137

130138
var i = a.startIndex
131-
a.formIndex(&i, offsetBy: 3)
139+
a.formIndex(&i, offsetBy: 1)
132140
XCTAssertEqual(a.distance(from: a.startIndex, to: i), a.count)
133141
XCTAssertEqual(i, a.endIndex)
134142
// a[i] // == Fatal error: Index out of range

0 commit comments

Comments
 (0)