4 fn test_iterator_flat_map() {
6 let ys = [0, 1, 2, 3, 4, 5, 6, 7, 8];
7 let it = xs.iter().flat_map(|&x| (x..).step_by(1).take(3));
13 assert_eq!(i, ys.len());
16 /// Tests `FlatMap::fold` with items already picked off the front and back,
17 /// to make sure all parts of the `FlatMap` are folded correctly.
19 fn test_iterator_flat_map_fold() {
21 let ys = [1, 2, 3, 4, 5, 6, 7];
22 let mut it = xs.iter().flat_map(|&x| x..x + 3);
23 assert_eq!(it.next(), Some(0));
24 assert_eq!(it.next_back(), Some(8));
25 let i = it.fold(0, |i, x| {
29 assert_eq!(i, ys.len());
31 let mut it = xs.iter().flat_map(|&x| x..x + 3);
32 assert_eq!(it.next(), Some(0));
33 assert_eq!(it.next_back(), Some(8));
34 let i = it.rfold(ys.len(), |i, x| {
35 assert_eq!(x, ys[i - 1]);
42 fn test_flat_map_try_folds() {
43 let f = &|acc, x| i32::checked_add(acc * 2 / 3, x);
44 let mr = &|x| (5 * x)..(5 * x + 5);
45 assert_eq!((0..10).flat_map(mr).try_fold(7, f), (0..50).try_fold(7, f));
46 assert_eq!((0..10).flat_map(mr).try_rfold(7, f), (0..50).try_rfold(7, f));
47 let mut iter = (0..10).flat_map(mr);
49 iter.next_back(); // have front and back iters in progress
50 assert_eq!(iter.try_rfold(7, f), (1..49).try_rfold(7, f));
52 let mut iter = (0..10).flat_map(|x| (4 * x)..(4 * x + 4));
53 assert_eq!(iter.try_fold(0, i8::checked_add), None);
54 assert_eq!(iter.next(), Some(17));
55 assert_eq!(iter.try_rfold(0, i8::checked_add), None);
56 assert_eq!(iter.next_back(), Some(35));
60 fn test_double_ended_flat_map() {
63 let mut it = u.iter().flat_map(|x| &v[*x..v.len()]);
64 assert_eq!(it.next_back().unwrap(), &8);
65 assert_eq!(it.next().unwrap(), &5);
66 assert_eq!(it.next_back().unwrap(), &7);
67 assert_eq!(it.next_back().unwrap(), &6);
68 assert_eq!(it.next_back().unwrap(), &8);
69 assert_eq!(it.next().unwrap(), &6);
70 assert_eq!(it.next_back().unwrap(), &7);
71 assert_eq!(it.next_back(), None);
72 assert_eq!(it.next(), None);
73 assert_eq!(it.next_back(), None);