1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use core::{i8, i16, isize};
19 let empty: [isize; 0] = [];
23 assert!(!xs.iter().lt(ys.iter()));
24 assert!(!xs.iter().le(ys.iter()));
25 assert!( xs.iter().gt(ys.iter()));
26 assert!( xs.iter().ge(ys.iter()));
28 assert!( ys.iter().lt(xs.iter()));
29 assert!( ys.iter().le(xs.iter()));
30 assert!(!ys.iter().gt(xs.iter()));
31 assert!(!ys.iter().ge(xs.iter()));
33 assert!( empty.iter().lt(xs.iter()));
34 assert!( empty.iter().le(xs.iter()));
35 assert!(!empty.iter().gt(xs.iter()));
36 assert!(!empty.iter().ge(xs.iter()));
39 let u = [1.0f64, 2.0];
40 let v = [0.0f64/0.0, 3.0];
42 assert!(!u.iter().lt(v.iter()));
43 assert!(!u.iter().le(v.iter()));
44 assert!(!u.iter().gt(v.iter()));
45 assert!(!u.iter().ge(v.iter()));
51 assert!(a.iter().lt(b.iter()) == (a[0] < b[0]));
52 assert!(a.iter().le(b.iter()) == (a[0] <= b[0]));
53 assert!(a.iter().gt(b.iter()) == (a[0] > b[0]));
54 assert!(a.iter().ge(b.iter()) == (a[0] >= b[0]));
56 assert!(c.iter().lt(b.iter()) == (c[0] < b[0]));
57 assert!(c.iter().le(b.iter()) == (c[0] <= b[0]));
58 assert!(c.iter().gt(b.iter()) == (c[0] > b[0]));
59 assert!(c.iter().ge(b.iter()) == (c[0] >= b[0]));
63 fn test_multi_iter() {
66 assert!(xs.iter().eq(ys.iter().rev()));
67 assert!(xs.iter().lt(xs.iter().skip(2)));
71 fn test_counter_from_iter() {
72 let it = (0..).step_by(5).take(10);
73 let xs: Vec<isize> = FromIterator::from_iter(it);
74 assert_eq!(xs, [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
78 fn test_iterator_chain() {
79 let xs = [0, 1, 2, 3, 4, 5];
80 let ys = [30, 40, 50, 60];
81 let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
82 let it = xs.iter().chain(&ys);
85 assert_eq!(x, expected[i]);
88 assert_eq!(i, expected.len());
90 let ys = (30..).step_by(10).take(4);
91 let it = xs.iter().cloned().chain(ys);
94 assert_eq!(x, expected[i]);
97 assert_eq!(i, expected.len());
101 fn test_iterator_chain_nth() {
102 let xs = [0, 1, 2, 3, 4, 5];
103 let ys = [30, 40, 50, 60];
105 let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
106 for (i, x) in expected.iter().enumerate() {
107 assert_eq!(Some(x), xs.iter().chain(&ys).nth(i));
109 assert_eq!(zs.iter().chain(&xs).nth(0), Some(&0));
111 let mut it = xs.iter().chain(&zs);
112 assert_eq!(it.nth(5), Some(&5));
113 assert_eq!(it.next(), None);
117 fn test_iterator_chain_last() {
118 let xs = [0, 1, 2, 3, 4, 5];
119 let ys = [30, 40, 50, 60];
121 assert_eq!(xs.iter().chain(&ys).last(), Some(&60));
122 assert_eq!(zs.iter().chain(&ys).last(), Some(&60));
123 assert_eq!(ys.iter().chain(&zs).last(), Some(&60));
124 assert_eq!(zs.iter().chain(&zs).last(), None);
128 fn test_iterator_chain_count() {
129 let xs = [0, 1, 2, 3, 4, 5];
130 let ys = [30, 40, 50, 60];
132 assert_eq!(xs.iter().chain(&ys).count(), 10);
133 assert_eq!(zs.iter().chain(&ys).count(), 4);
137 fn test_filter_map() {
138 let it = (0..).step_by(1).take(10)
139 .filter_map(|x| if x % 2 == 0 { Some(x*x) } else { None });
140 assert_eq!(it.collect::<Vec<usize>>(), [0*0, 2*2, 4*4, 6*6, 8*8]);
144 fn test_iterator_enumerate() {
145 let xs = [0, 1, 2, 3, 4, 5];
146 let it = xs.iter().enumerate();
153 fn test_iterator_enumerate_nth() {
154 let xs = [0, 1, 2, 3, 4, 5];
155 for (i, &x) in xs.iter().enumerate() {
159 let mut it = xs.iter().enumerate();
160 while let Some((i, &x)) = it.nth(0) {
164 let mut it = xs.iter().enumerate();
165 while let Some((i, &x)) = it.nth(1) {
169 let (i, &x) = xs.iter().enumerate().nth(3).unwrap();
175 fn test_iterator_enumerate_count() {
176 let xs = [0, 1, 2, 3, 4, 5];
177 assert_eq!(xs.iter().count(), 6);
181 fn test_iterator_peekable() {
182 let xs = vec![0, 1, 2, 3, 4, 5];
183 let mut it = xs.iter().cloned().peekable();
185 assert_eq!(it.len(), 6);
186 assert_eq!(it.peek().unwrap(), &0);
187 assert_eq!(it.len(), 6);
188 assert_eq!(it.next().unwrap(), 0);
189 assert_eq!(it.len(), 5);
190 assert_eq!(it.next().unwrap(), 1);
191 assert_eq!(it.len(), 4);
192 assert_eq!(it.next().unwrap(), 2);
193 assert_eq!(it.len(), 3);
194 assert_eq!(it.peek().unwrap(), &3);
195 assert_eq!(it.len(), 3);
196 assert_eq!(it.peek().unwrap(), &3);
197 assert_eq!(it.len(), 3);
198 assert_eq!(it.next().unwrap(), 3);
199 assert_eq!(it.len(), 2);
200 assert_eq!(it.next().unwrap(), 4);
201 assert_eq!(it.len(), 1);
202 assert_eq!(it.peek().unwrap(), &5);
203 assert_eq!(it.len(), 1);
204 assert_eq!(it.next().unwrap(), 5);
205 assert_eq!(it.len(), 0);
206 assert!(it.peek().is_none());
207 assert_eq!(it.len(), 0);
208 assert!(it.next().is_none());
209 assert_eq!(it.len(), 0);
213 fn test_iterator_peekable_count() {
214 let xs = [0, 1, 2, 3, 4, 5];
216 let zs: [i32; 0] = [];
218 assert_eq!(xs.iter().peekable().count(), 6);
220 let mut it = xs.iter().peekable();
221 assert_eq!(it.peek(), Some(&&0));
222 assert_eq!(it.count(), 6);
224 assert_eq!(ys.iter().peekable().count(), 1);
226 let mut it = ys.iter().peekable();
227 assert_eq!(it.peek(), Some(&&10));
228 assert_eq!(it.count(), 1);
230 assert_eq!(zs.iter().peekable().count(), 0);
232 let mut it = zs.iter().peekable();
233 assert_eq!(it.peek(), None);
238 fn test_iterator_peekable_nth() {
239 let xs = [0, 1, 2, 3, 4, 5];
240 let mut it = xs.iter().peekable();
242 assert_eq!(it.peek(), Some(&&0));
243 assert_eq!(it.nth(0), Some(&0));
244 assert_eq!(it.peek(), Some(&&1));
245 assert_eq!(it.nth(1), Some(&2));
246 assert_eq!(it.peek(), Some(&&3));
247 assert_eq!(it.nth(2), Some(&5));
248 assert_eq!(it.next(), None);
252 fn test_iterator_peekable_last() {
253 let xs = [0, 1, 2, 3, 4, 5];
256 let mut it = xs.iter().peekable();
257 assert_eq!(it.peek(), Some(&&0));
258 assert_eq!(it.last(), Some(&5));
260 let mut it = ys.iter().peekable();
261 assert_eq!(it.peek(), Some(&&0));
262 assert_eq!(it.last(), Some(&0));
266 fn test_iterator_take_while() {
267 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
268 let ys = [0, 1, 2, 3, 5, 13];
269 let it = xs.iter().take_while(|&x| *x < 15);
272 assert_eq!(*x, ys[i]);
275 assert_eq!(i, ys.len());
279 fn test_iterator_skip_while() {
280 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
281 let ys = [15, 16, 17, 19];
282 let it = xs.iter().skip_while(|&x| *x < 15);
285 assert_eq!(*x, ys[i]);
288 assert_eq!(i, ys.len());
292 fn test_iterator_skip() {
293 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
294 let ys = [13, 15, 16, 17, 19, 20, 30];
295 let mut it = xs.iter().skip(5);
297 while let Some(&x) = it.next() {
298 assert_eq!(x, ys[i]);
300 assert_eq!(it.len(), xs.len()-5-i);
302 assert_eq!(i, ys.len());
303 assert_eq!(it.len(), 0);
307 fn test_iterator_skip_doubleended() {
308 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
309 let mut it = xs.iter().rev().skip(5);
310 assert_eq!(it.next(), Some(&15));
311 assert_eq!(it.by_ref().rev().next(), Some(&0));
312 assert_eq!(it.next(), Some(&13));
313 assert_eq!(it.by_ref().rev().next(), Some(&1));
314 assert_eq!(it.next(), Some(&5));
315 assert_eq!(it.by_ref().rev().next(), Some(&2));
316 assert_eq!(it.next(), Some(&3));
317 assert_eq!(it.next(), None);
318 let mut it = xs.iter().rev().skip(5).rev();
319 assert_eq!(it.next(), Some(&0));
320 assert_eq!(it.rev().next(), Some(&15));
321 let mut it_base = xs.iter();
323 let mut it = it_base.by_ref().skip(5).rev();
324 assert_eq!(it.next(), Some(&30));
325 assert_eq!(it.next(), Some(&20));
326 assert_eq!(it.next(), Some(&19));
327 assert_eq!(it.next(), Some(&17));
328 assert_eq!(it.next(), Some(&16));
329 assert_eq!(it.next(), Some(&15));
330 assert_eq!(it.next(), Some(&13));
331 assert_eq!(it.next(), None);
333 // make sure the skipped parts have not been consumed
334 assert_eq!(it_base.next(), Some(&0));
335 assert_eq!(it_base.next(), Some(&1));
336 assert_eq!(it_base.next(), Some(&2));
337 assert_eq!(it_base.next(), Some(&3));
338 assert_eq!(it_base.next(), Some(&5));
339 assert_eq!(it_base.next(), None);
340 let it = xs.iter().skip(5).rev();
341 assert_eq!(it.last(), Some(&13));
345 fn test_iterator_skip_nth() {
346 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
348 let mut it = xs.iter().skip(0);
349 assert_eq!(it.nth(0), Some(&0));
350 assert_eq!(it.nth(1), Some(&2));
352 let mut it = xs.iter().skip(5);
353 assert_eq!(it.nth(0), Some(&13));
354 assert_eq!(it.nth(1), Some(&16));
356 let mut it = xs.iter().skip(12);
357 assert_eq!(it.nth(0), None);
362 fn test_iterator_skip_count() {
363 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
365 assert_eq!(xs.iter().skip(0).count(), 12);
366 assert_eq!(xs.iter().skip(1).count(), 11);
367 assert_eq!(xs.iter().skip(11).count(), 1);
368 assert_eq!(xs.iter().skip(12).count(), 0);
369 assert_eq!(xs.iter().skip(13).count(), 0);
373 fn test_iterator_skip_last() {
374 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
376 assert_eq!(xs.iter().skip(0).last(), Some(&30));
377 assert_eq!(xs.iter().skip(1).last(), Some(&30));
378 assert_eq!(xs.iter().skip(11).last(), Some(&30));
379 assert_eq!(xs.iter().skip(12).last(), None);
380 assert_eq!(xs.iter().skip(13).last(), None);
382 let mut it = xs.iter().skip(5);
383 assert_eq!(it.next(), Some(&13));
384 assert_eq!(it.last(), Some(&30));
388 fn test_iterator_take() {
389 let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
390 let ys = [0, 1, 2, 3, 5];
391 let mut it = xs.iter().take(5);
393 assert_eq!(it.len(), 5);
394 while let Some(&x) = it.next() {
395 assert_eq!(x, ys[i]);
397 assert_eq!(it.len(), 5-i);
399 assert_eq!(i, ys.len());
400 assert_eq!(it.len(), 0);
404 fn test_iterator_take_nth() {
405 let xs = [0, 1, 2, 4, 5];
406 let mut it = xs.iter();
408 let mut take = it.by_ref().take(3);
410 while let Some(&x) = take.nth(0) {
415 assert_eq!(it.nth(1), Some(&5));
416 assert_eq!(it.nth(0), None);
418 let xs = [0, 1, 2, 3, 4];
419 let mut it = xs.iter().take(7);
421 while let Some(&x) = it.nth(1) {
428 fn test_iterator_take_short() {
429 let xs = [0, 1, 2, 3];
430 let ys = [0, 1, 2, 3];
431 let mut it = xs.iter().take(5);
433 assert_eq!(it.len(), 4);
434 while let Some(&x) = it.next() {
435 assert_eq!(x, ys[i]);
437 assert_eq!(it.len(), 4-i);
439 assert_eq!(i, ys.len());
440 assert_eq!(it.len(), 0);
444 fn test_iterator_scan() {
445 // test the type inference
446 fn add(old: &mut isize, new: &usize) -> Option<f64> {
447 *old += *new as isize;
450 let xs = [0, 1, 2, 3, 4];
451 let ys = [0f64, 1.0, 3.0, 6.0, 10.0];
453 let it = xs.iter().scan(0, add);
456 assert_eq!(x, ys[i]);
459 assert_eq!(i, ys.len());
463 fn test_iterator_flat_map() {
465 let ys = [0, 1, 2, 3, 4, 5, 6, 7, 8];
466 let it = xs.iter().flat_map(|&x| (x..).step_by(1).take(3));
469 assert_eq!(x, ys[i]);
472 assert_eq!(i, ys.len());
477 let xs = [1, 2, 3, 4];
483 .collect::<Vec<usize>>();
485 assert_eq!(n, xs.len());
486 assert_eq!(&xs[..], &ys[..]);
492 let it = (0..).step_by(1).take(cycle_len).cycle();
493 assert_eq!(it.size_hint(), (usize::MAX, None));
494 for (i, x) in it.take(100).enumerate() {
495 assert_eq!(i % cycle_len, x);
498 let mut it = (0..).step_by(1).take(0).cycle();
499 assert_eq!(it.size_hint(), (0, Some(0)));
500 assert_eq!(it.next(), None);
504 fn test_iterator_nth() {
505 let v: &[_] = &[0, 1, 2, 3, 4];
506 for i in 0..v.len() {
507 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
509 assert_eq!(v.iter().nth(v.len()), None);
513 fn test_iterator_last() {
514 let v: &[_] = &[0, 1, 2, 3, 4];
515 assert_eq!(v.iter().last().unwrap(), &4);
516 assert_eq!(v[..1].iter().last().unwrap(), &0);
520 fn test_iterator_len() {
521 let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
522 assert_eq!(v[..4].iter().count(), 4);
523 assert_eq!(v[..10].iter().count(), 10);
524 assert_eq!(v[..0].iter().count(), 0);
528 fn test_iterator_sum() {
529 let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
530 assert_eq!(v[..4].iter().cloned().sum::<i32>(), 6);
531 assert_eq!(v.iter().cloned().sum::<i32>(), 55);
532 assert_eq!(v[..0].iter().cloned().sum::<i32>(), 0);
536 fn test_iterator_product() {
537 let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
538 assert_eq!(v[..4].iter().cloned().product::<i32>(), 0);
539 assert_eq!(v[1..5].iter().cloned().product::<i32>(), 24);
540 assert_eq!(v[..0].iter().cloned().product::<i32>(), 1);
544 fn test_iterator_max() {
545 let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
546 assert_eq!(v[..4].iter().cloned().max(), Some(3));
547 assert_eq!(v.iter().cloned().max(), Some(10));
548 assert_eq!(v[..0].iter().cloned().max(), None);
552 fn test_iterator_min() {
553 let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
554 assert_eq!(v[..4].iter().cloned().min(), Some(0));
555 assert_eq!(v.iter().cloned().min(), Some(0));
556 assert_eq!(v[..0].iter().cloned().min(), None);
560 fn test_iterator_size_hint() {
561 let c = (0..).step_by(1);
562 let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
563 let v2 = &[10, 11, 12];
566 assert_eq!(c.size_hint(), (usize::MAX, None));
567 assert_eq!(vi.clone().size_hint(), (10, Some(10)));
569 assert_eq!(c.clone().take(5).size_hint(), (5, Some(5)));
570 assert_eq!(c.clone().skip(5).size_hint().1, None);
571 assert_eq!(c.clone().take_while(|_| false).size_hint(), (0, None));
572 assert_eq!(c.clone().skip_while(|_| false).size_hint(), (0, None));
573 assert_eq!(c.clone().enumerate().size_hint(), (usize::MAX, None));
574 assert_eq!(c.clone().chain(vi.clone().cloned()).size_hint(), (usize::MAX, None));
575 assert_eq!(c.clone().zip(vi.clone()).size_hint(), (10, Some(10)));
576 assert_eq!(c.clone().scan(0, |_,_| Some(0)).size_hint(), (0, None));
577 assert_eq!(c.clone().filter(|_| false).size_hint(), (0, None));
578 assert_eq!(c.clone().map(|_| 0).size_hint(), (usize::MAX, None));
579 assert_eq!(c.filter_map(|_| Some(0)).size_hint(), (0, None));
581 assert_eq!(vi.clone().take(5).size_hint(), (5, Some(5)));
582 assert_eq!(vi.clone().take(12).size_hint(), (10, Some(10)));
583 assert_eq!(vi.clone().skip(3).size_hint(), (7, Some(7)));
584 assert_eq!(vi.clone().skip(12).size_hint(), (0, Some(0)));
585 assert_eq!(vi.clone().take_while(|_| false).size_hint(), (0, Some(10)));
586 assert_eq!(vi.clone().skip_while(|_| false).size_hint(), (0, Some(10)));
587 assert_eq!(vi.clone().enumerate().size_hint(), (10, Some(10)));
588 assert_eq!(vi.clone().chain(v2).size_hint(), (13, Some(13)));
589 assert_eq!(vi.clone().zip(v2).size_hint(), (3, Some(3)));
590 assert_eq!(vi.clone().scan(0, |_,_| Some(0)).size_hint(), (0, Some(10)));
591 assert_eq!(vi.clone().filter(|_| false).size_hint(), (0, Some(10)));
592 assert_eq!(vi.clone().map(|&i| i+1).size_hint(), (10, Some(10)));
593 assert_eq!(vi.filter_map(|_| Some(0)).size_hint(), (0, Some(10)));
598 let a = vec![1, 2, 3, 4, 5];
599 let b: Vec<isize> = a.iter().cloned().collect();
605 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
606 let v: Box<[isize]> = Box::new([1, 2, 3, 4, 5]);
607 assert!(v.iter().all(|&x| x < 10));
608 assert!(!v.iter().all(|&x| x % 2 == 0));
609 assert!(!v.iter().all(|&x| x > 100));
610 assert!(v[..0].iter().all(|_| panic!()));
615 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
616 let v: Box<[isize]> = Box::new([1, 2, 3, 4, 5]);
617 assert!(v.iter().any(|&x| x < 10));
618 assert!(v.iter().any(|&x| x % 2 == 0));
619 assert!(!v.iter().any(|&x| x > 100));
620 assert!(!v[..0].iter().any(|_| panic!()));
625 let v: &[isize] = &[1, 3, 9, 27, 103, 14, 11];
626 assert_eq!(*v.iter().find(|&&x| x & 1 == 0).unwrap(), 14);
627 assert_eq!(*v.iter().find(|&&x| x % 3 == 0).unwrap(), 3);
628 assert!(v.iter().find(|&&x| x % 12 == 0).is_none());
633 let v = &[1, 3, 9, 27, 103, 14, 11];
634 assert_eq!(v.iter().position(|x| *x & 1 == 0).unwrap(), 5);
635 assert_eq!(v.iter().position(|x| *x % 3 == 0).unwrap(), 1);
636 assert!(v.iter().position(|x| *x % 12 == 0).is_none());
641 let xs = &[1, 2, 2, 1, 5, 9, 0, 2];
642 assert_eq!(xs.iter().filter(|x| **x == 2).count(), 3);
643 assert_eq!(xs.iter().filter(|x| **x == 5).count(), 1);
644 assert_eq!(xs.iter().filter(|x| **x == 95).count(), 0);
648 fn test_max_by_key() {
649 let xs: &[isize] = &[-3, 0, 1, 5, -10];
650 assert_eq!(*xs.iter().max_by_key(|x| x.abs()).unwrap(), -10);
654 fn test_min_by_key() {
655 let xs: &[isize] = &[-3, 0, 1, 5, -10];
656 assert_eq!(*xs.iter().min_by_key(|x| x.abs()).unwrap(), 0);
662 // sum the first five values
663 let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
664 assert_eq!(partial_sum, 10);
665 assert_eq!(xs.next(), Some(5));
670 let xs = [2, 4, 6, 8, 10, 12, 14, 16];
671 let mut it = xs.iter();
674 assert!(it.rev().cloned().collect::<Vec<isize>>() ==
675 vec![16, 14, 12, 10, 8, 6]);
680 let xs = [2u8, 4, 6, 8];
682 let mut it = xs.iter().cloned();
683 assert_eq!(it.len(), 4);
684 assert_eq!(it.next(), Some(2));
685 assert_eq!(it.len(), 3);
686 assert_eq!(it.next(), Some(4));
687 assert_eq!(it.len(), 2);
688 assert_eq!(it.next_back(), Some(8));
689 assert_eq!(it.len(), 1);
690 assert_eq!(it.next_back(), Some(6));
691 assert_eq!(it.len(), 0);
692 assert_eq!(it.next_back(), None);
696 fn test_double_ended_map() {
697 let xs = [1, 2, 3, 4, 5, 6];
698 let mut it = xs.iter().map(|&x| x * -1);
699 assert_eq!(it.next(), Some(-1));
700 assert_eq!(it.next(), Some(-2));
701 assert_eq!(it.next_back(), Some(-6));
702 assert_eq!(it.next_back(), Some(-5));
703 assert_eq!(it.next(), Some(-3));
704 assert_eq!(it.next_back(), Some(-4));
705 assert_eq!(it.next(), None);
709 fn test_double_ended_enumerate() {
710 let xs = [1, 2, 3, 4, 5, 6];
711 let mut it = xs.iter().cloned().enumerate();
712 assert_eq!(it.next(), Some((0, 1)));
713 assert_eq!(it.next(), Some((1, 2)));
714 assert_eq!(it.next_back(), Some((5, 6)));
715 assert_eq!(it.next_back(), Some((4, 5)));
716 assert_eq!(it.next_back(), Some((3, 4)));
717 assert_eq!(it.next_back(), Some((2, 3)));
718 assert_eq!(it.next(), None);
722 fn test_double_ended_zip() {
723 let xs = [1, 2, 3, 4, 5, 6];
724 let ys = [1, 2, 3, 7];
725 let a = xs.iter().cloned();
726 let b = ys.iter().cloned();
727 let mut it = a.zip(b);
728 assert_eq!(it.next(), Some((1, 1)));
729 assert_eq!(it.next(), Some((2, 2)));
730 assert_eq!(it.next_back(), Some((4, 7)));
731 assert_eq!(it.next_back(), Some((3, 3)));
732 assert_eq!(it.next(), None);
736 fn test_double_ended_filter() {
737 let xs = [1, 2, 3, 4, 5, 6];
738 let mut it = xs.iter().filter(|&x| *x & 1 == 0);
739 assert_eq!(it.next_back().unwrap(), &6);
740 assert_eq!(it.next_back().unwrap(), &4);
741 assert_eq!(it.next().unwrap(), &2);
742 assert_eq!(it.next_back(), None);
746 fn test_double_ended_filter_map() {
747 let xs = [1, 2, 3, 4, 5, 6];
748 let mut it = xs.iter().filter_map(|&x| if x & 1 == 0 { Some(x * 2) } else { None });
749 assert_eq!(it.next_back().unwrap(), 12);
750 assert_eq!(it.next_back().unwrap(), 8);
751 assert_eq!(it.next().unwrap(), 4);
752 assert_eq!(it.next_back(), None);
756 fn test_double_ended_chain() {
757 let xs = [1, 2, 3, 4, 5];
759 let mut it = xs.iter().chain(&ys).rev();
760 assert_eq!(it.next().unwrap(), &11);
761 assert_eq!(it.next().unwrap(), &9);
762 assert_eq!(it.next_back().unwrap(), &1);
763 assert_eq!(it.next_back().unwrap(), &2);
764 assert_eq!(it.next_back().unwrap(), &3);
765 assert_eq!(it.next_back().unwrap(), &4);
766 assert_eq!(it.next_back().unwrap(), &5);
767 assert_eq!(it.next_back().unwrap(), &7);
768 assert_eq!(it.next_back(), None);
771 // test that .chain() is well behaved with an unfused iterator
772 struct CrazyIterator(bool);
773 impl CrazyIterator { fn new() -> CrazyIterator { CrazyIterator(false) } }
774 impl Iterator for CrazyIterator {
776 fn next(&mut self) -> Option<i32> {
777 if self.0 { Some(99) } else { self.0 = true; None }
781 impl DoubleEndedIterator for CrazyIterator {
782 fn next_back(&mut self) -> Option<i32> {
787 assert_eq!(CrazyIterator::new().chain(0..10).rev().last(), Some(0));
788 assert!((0..10).chain(CrazyIterator::new()).rev().any(|i| i == 0));
792 fn test_rposition() {
793 fn f(xy: &(isize, char)) -> bool { let (_x, y) = *xy; y == 'b' }
794 fn g(xy: &(isize, char)) -> bool { let (_x, y) = *xy; y == 'd' }
795 let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
797 assert_eq!(v.iter().rposition(f), Some(3));
798 assert!(v.iter().rposition(g).is_none());
803 fn test_rposition_panic() {
804 let v: [(Box<_>, Box<_>); 4] =
805 [(box 0, box 0), (box 0, box 0),
806 (box 0, box 0), (box 0, box 0)];
808 v.iter().rposition(|_elt| {
819 fn test_double_ended_flat_map() {
822 let mut it = u.iter().flat_map(|x| &v[*x..v.len()]);
823 assert_eq!(it.next_back().unwrap(), &8);
824 assert_eq!(it.next().unwrap(), &5);
825 assert_eq!(it.next_back().unwrap(), &7);
826 assert_eq!(it.next_back().unwrap(), &6);
827 assert_eq!(it.next_back().unwrap(), &8);
828 assert_eq!(it.next().unwrap(), &6);
829 assert_eq!(it.next_back().unwrap(), &7);
830 assert_eq!(it.next_back(), None);
831 assert_eq!(it.next(), None);
832 assert_eq!(it.next_back(), None);
836 fn test_double_ended_range() {
837 assert_eq!((11..14).rev().collect::<Vec<_>>(), [13, 12, 11]);
838 for _ in (10..0).rev() {
839 panic!("unreachable");
842 assert_eq!((11..14).rev().collect::<Vec<_>>(), [13, 12, 11]);
843 for _ in (10..0).rev() {
844 panic!("unreachable");
850 assert_eq!((0..5).collect::<Vec<_>>(), [0, 1, 2, 3, 4]);
851 assert_eq!((-10..-1).collect::<Vec<_>>(), [-10, -9, -8, -7, -6, -5, -4, -3, -2]);
852 assert_eq!((0..5).rev().collect::<Vec<_>>(), [4, 3, 2, 1, 0]);
853 assert_eq!((200..-5).count(), 0);
854 assert_eq!((200..-5).rev().count(), 0);
855 assert_eq!((200..200).count(), 0);
856 assert_eq!((200..200).rev().count(), 0);
858 assert_eq!((0..100).size_hint(), (100, Some(100)));
859 // this test is only meaningful when sizeof usize < sizeof u64
860 assert_eq!((usize::MAX - 1..usize::MAX).size_hint(), (1, Some(1)));
861 assert_eq!((-10..-1).size_hint(), (9, Some(9)));
862 assert_eq!((-1..-10).size_hint(), (0, Some(0)));
864 assert_eq!((-70..58i8).size_hint(), (128, Some(128)));
865 assert_eq!((-128..127i8).size_hint(), (255, Some(255)));
866 assert_eq!((-2..isize::MAX).size_hint(),
867 (isize::MAX as usize + 2, Some(isize::MAX as usize + 2)));
871 fn test_range_step() {
872 assert_eq!((0..20).step_by(5).collect::<Vec<isize>>(), [0, 5, 10, 15]);
873 assert_eq!((20..0).step_by(-5).collect::<Vec<isize>>(), [20, 15, 10, 5]);
874 assert_eq!((20..0).step_by(-6).collect::<Vec<isize>>(), [20, 14, 8, 2]);
875 assert_eq!((200..255).step_by(50).collect::<Vec<u8>>(), [200, 250]);
876 assert_eq!((200..-5).step_by(1).collect::<Vec<isize>>(), []);
877 assert_eq!((200..200).step_by(1).collect::<Vec<isize>>(), []);
879 assert_eq!((0..20).step_by(1).size_hint(), (20, Some(20)));
880 assert_eq!((0..20).step_by(21).size_hint(), (1, Some(1)));
881 assert_eq!((0..20).step_by(5).size_hint(), (4, Some(4)));
882 assert_eq!((20..0).step_by(-5).size_hint(), (4, Some(4)));
883 assert_eq!((20..0).step_by(-6).size_hint(), (4, Some(4)));
884 assert_eq!((20..-5).step_by(1).size_hint(), (0, Some(0)));
885 assert_eq!((20..20).step_by(1).size_hint(), (0, Some(0)));
886 assert_eq!((0..1).step_by(0).size_hint(), (0, None));
887 assert_eq!((i8::MAX..i8::MIN).step_by(i8::MIN).size_hint(), (2, Some(2)));
888 assert_eq!((i16::MIN..i16::MAX).step_by(i16::MAX).size_hint(), (3, Some(3)));
889 assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
893 fn test_peekable_is_empty() {
895 let mut it = a.iter().peekable();
896 assert!( !it.is_empty() );
898 assert!( it.is_empty() );
903 let mut it = repeat(42);
904 assert_eq!(it.next(), Some(42));
905 assert_eq!(it.next(), Some(42));
906 assert_eq!(it.next(), Some(42));
912 assert_eq!(it.len(), 3);
913 assert_eq!(it.next(), Some(0));
914 assert_eq!(it.len(), 2);
915 assert_eq!(it.next(), Some(1));
916 assert_eq!(it.len(), 1);
917 assert_eq!(it.next(), Some(2));
918 assert_eq!(it.len(), 0);
919 assert_eq!(it.next(), None);
920 assert_eq!(it.len(), 0);
921 assert_eq!(it.next(), None);
922 assert_eq!(it.len(), 0);
923 assert_eq!(it.next(), None);
924 assert_eq!(it.len(), 0);
930 let mut it = xs.iter();
932 assert_eq!(it.len(), 3);
933 assert_eq!(it.nth(2), Some(&2));
934 assert_eq!(it.len(), 0);
935 assert_eq!(it.nth(2), None);
936 assert_eq!(it.len(), 0);
940 fn test_fuse_last() {
944 assert_eq!(it.len(), 3);
945 assert_eq!(it.last(), Some(&2));
949 fn test_fuse_count() {
953 assert_eq!(it.len(), 3);
954 assert_eq!(it.count(), 3);
955 // Can't check len now because count consumes.
960 let mut it = once(42);
961 assert_eq!(it.next(), Some(42));
962 assert_eq!(it.next(), None);
967 let mut it = empty::<i32>();
968 assert_eq!(it.next(), None);
972 fn bench_rposition(b: &mut Bencher) {
973 let it: Vec<usize> = (0..300).collect();
975 it.iter().rposition(|&x| x <= 150);
980 fn bench_skip_while(b: &mut Bencher) {
984 it.skip_while(|&x| { sum += x; sum < 4000 }).all(|_| true);
989 fn bench_multiple_take(b: &mut Bencher) {
990 let mut it = (0..42).cycle();
992 let n = it.next().unwrap();
994 it.clone().take(it.next().unwrap()).all(|_| true);
999 fn scatter(x: i32) -> i32 { (x * 31) % 127 }
1002 fn bench_max_by_key(b: &mut Bencher) {
1005 it.max_by_key(|&x| scatter(x))
1009 // http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
1011 fn bench_max_by_key2(b: &mut Bencher) {
1012 fn max_index_iter(array: &[i32]) -> usize {
1013 array.iter().enumerate().max_by_key(|&(_, item)| item).unwrap().0
1016 let mut data = vec![0i32; 1638];
1019 b.iter(|| max_index_iter(&data));
1023 fn bench_max(b: &mut Bencher) {
1026 it.map(scatter).max()