@@ -76,6 +76,8 @@ final class StridingTests: XCTestCase {
76
76
}
77
77
78
78
func testCount( ) {
79
+ let empty = [ Int] ( ) . striding ( by: 2 )
80
+ XCTAssertEqual ( empty. count, 0 )
79
81
let a = ( 0 ... 10 )
80
82
XCTAssertEqual ( a. striding ( by: 1 ) . count, ( 0 ... 10 ) . count)
81
83
XCTAssertEqual ( a. striding ( by: 2 ) . count, [ 0 , 2 , 4 , 6 , 8 , 10 ] . count)
@@ -87,12 +89,50 @@ final class StridingTests: XCTestCase {
87
89
}
88
90
89
91
func testDistance( ) {
90
- let a = ( 0 ... 100 ) . striding ( by: 12 )
91
- var i = a. startIndex
92
- a. formIndex ( & i, offsetBy: 3 )
93
- XCTAssertEqual ( a. distance ( from: a. startIndex, to: i) , 3 )
94
- XCTAssertEqual ( a [ i] , 36 )
95
- XCTAssertEqual ( a. distance ( from: a. startIndex, to: a. endIndex) , a. count - 1 )
92
+
93
+ do {
94
+ let a = ( 0 ... 100 ) . striding ( by: 11 )
95
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: a. endIndex) , a. count)
96
+ for (i, index) in a. indices. enumerated ( ) {
97
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: index) , i)
98
+ }
99
+
100
+ var i = a. startIndex
101
+ a. formIndex ( & i, offsetBy: 3 )
102
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: i) , 3 )
103
+ XCTAssertEqual ( a [ i] , 33 )
104
+ }
105
+
106
+ do {
107
+
108
+ let a = ( 0 ... 100 ) . striding ( by: 10 )
109
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: a. endIndex) , a. count)
110
+
111
+ for (i, index) in a. indices. enumerated ( ) {
112
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: index) , i)
113
+ }
114
+
115
+ var i = a. startIndex
116
+ a. formIndex ( & i, offsetBy: 3 )
117
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: i) , 3 )
118
+ XCTAssertEqual ( a [ i] , 30 )
119
+ }
120
+
121
+ do {
122
+
123
+ let a = ( 0 ... 100 ) . striding ( by: 101 )
124
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: a. endIndex) , a. count)
125
+
126
+ for (i, index) in a. indices. enumerated ( ) {
127
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: index) , i)
128
+ }
129
+
130
+ var i = a. startIndex
131
+ a. formIndex ( & i, offsetBy: 3 )
132
+ XCTAssertEqual ( a. distance ( from: a. startIndex, to: i) , a. count)
133
+ XCTAssertEqual ( i, a. endIndex)
134
+ // a[i] // == Fatal error: Index out of range
135
+ }
96
136
}
97
137
98
138
func testOffsetBy( ) {
@@ -102,4 +142,11 @@ final class StridingTests: XCTestCase {
102
142
XCTAssertEqual ( a [ a. index ( a. startIndex, offsetBy: i) ] , b [ i] )
103
143
}
104
144
}
145
+
146
+ func testOffsetByEndIndex( ) {
147
+ let a = 1 ... 5
148
+ let b = a. striding ( by: 3 ) // [1, 4]
149
+ let i = b. index ( b. startIndex, offsetBy: 2 )
150
+ XCTAssertEqual ( i, b. endIndex)
151
+ }
105
152
}
0 commit comments