File tree Expand file tree Collapse file tree 2 files changed +13
-5
lines changed
Tests/SwiftAlgorithmsTests Expand file tree Collapse file tree 2 files changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -103,9 +103,9 @@ extension Stride: Collection {
103
103
}
104
104
105
105
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 ) )
109
109
}
110
110
}
111
111
Original file line number Diff line number Diff line change @@ -88,8 +88,16 @@ final class StridingTests: XCTestCase {
88
88
XCTAssertEqual ( a. striding ( by: 11 ) . count, [ 0 ] . count)
89
89
}
90
90
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
+
91
99
func testDistance( ) {
92
-
100
+
93
101
do {
94
102
let a = ( 0 ... 100 ) . striding ( by: 11 )
95
103
XCTAssertEqual ( a. distance ( from: a. startIndex, to: a. endIndex) , a. count)
@@ -128,7 +136,7 @@ final class StridingTests: XCTestCase {
128
136
}
129
137
130
138
var i = a. startIndex
131
- a. formIndex ( & i, offsetBy: 3 )
139
+ a. formIndex ( & i, offsetBy: 1 )
132
140
XCTAssertEqual ( a. distance ( from: a. startIndex, to: i) , a. count)
133
141
XCTAssertEqual ( i, a. endIndex)
134
142
// a[i] // == Fatal error: Index out of range
You can’t perform that action at this time.
0 commit comments