@@ -161,6 +161,68 @@ fn test_iterator_step_by() {
161161 assert_eq ! ( it. next( ) , None ) ;
162162}
163163
164+ #[ test]
165+ fn test_iterator_step_by_nth ( ) {
166+ let mut it = ( 0 ..16 ) . step_by ( 5 ) ;
167+ assert_eq ! ( it. nth( 0 ) , Some ( 0 ) ) ;
168+ assert_eq ! ( it. nth( 0 ) , Some ( 5 ) ) ;
169+ assert_eq ! ( it. nth( 0 ) , Some ( 10 ) ) ;
170+ assert_eq ! ( it. nth( 0 ) , Some ( 15 ) ) ;
171+ assert_eq ! ( it. nth( 0 ) , None ) ;
172+
173+ let it = ( 0 ..18 ) . step_by ( 5 ) ;
174+ assert_eq ! ( it. clone( ) . nth( 0 ) , Some ( 0 ) ) ;
175+ assert_eq ! ( it. clone( ) . nth( 1 ) , Some ( 5 ) ) ;
176+ assert_eq ! ( it. clone( ) . nth( 2 ) , Some ( 10 ) ) ;
177+ assert_eq ! ( it. clone( ) . nth( 3 ) , Some ( 15 ) ) ;
178+ assert_eq ! ( it. clone( ) . nth( 4 ) , None ) ;
179+ assert_eq ! ( it. clone( ) . nth( 42 ) , None ) ;
180+ }
181+
182+ #[ test]
183+ fn test_iterator_step_by_nth_overflow ( ) {
184+ #[ cfg( target_pointer_width = "8" ) ]
185+ type Bigger = u16 ;
186+ #[ cfg( target_pointer_width = "16" ) ]
187+ type Bigger = u32 ;
188+ #[ cfg( target_pointer_width = "32" ) ]
189+ type Bigger = u64 ;
190+ #[ cfg( target_pointer_width = "64" ) ]
191+ type Bigger = u128 ;
192+
193+ #[ derive( Clone ) ]
194+ struct Test ( Bigger ) ;
195+ impl < ' a > Iterator for & ' a mut Test {
196+ type Item = i32 ;
197+ fn next ( & mut self ) -> Option < Self :: Item > { Some ( 21 ) }
198+ fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
199+ self . 0 += n as Bigger + 1 ;
200+ Some ( 42 )
201+ }
202+ }
203+
204+ let mut it = Test ( 0 ) ;
205+ let root = usize:: MAX >> ( :: std:: mem:: size_of :: < usize > ( ) * 8 / 2 ) ;
206+ let n = root + 20 ;
207+ ( & mut it) . step_by ( n) . nth ( n) ;
208+ assert_eq ! ( it. 0 , n as Bigger * n as Bigger ) ;
209+
210+ // large step
211+ let mut it = Test ( 0 ) ;
212+ ( & mut it) . step_by ( usize:: MAX ) . nth ( 5 ) ;
213+ assert_eq ! ( it. 0 , ( usize :: MAX as Bigger ) * 5 ) ;
214+
215+ // n + 1 overflows
216+ let mut it = Test ( 0 ) ;
217+ ( & mut it) . step_by ( 2 ) . nth ( usize:: MAX ) ;
218+ assert_eq ! ( it. 0 , ( usize :: MAX as Bigger ) * 2 ) ;
219+
220+ // n + 1 overflows
221+ let mut it = Test ( 0 ) ;
222+ ( & mut it) . step_by ( 1 ) . nth ( usize:: MAX ) ;
223+ assert_eq ! ( it. 0 , ( usize :: MAX as Bigger ) * 1 ) ;
224+ }
225+
164226#[ test]
165227#[ should_panic]
166228fn test_iterator_step_by_zero ( ) {
0 commit comments