2021-01-09 16:55:41 -06:00
|
|
|
use super::*;
|
|
|
|
use core::iter::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_flatten() {
|
|
|
|
let xs = [0, 3, 6];
|
|
|
|
let ys = [0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
let it = xs.iter().map(|&x| (x..).step_by(1).take(3)).flatten();
|
|
|
|
let mut i = 0;
|
|
|
|
for x in it {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
2021-01-22 15:58:21 -06:00
|
|
|
|
2021-01-22 16:20:42 -06:00
|
|
|
/// Tests `Flatten::fold` with items already picked off the front and back,
|
|
|
|
/// to make sure all parts of the `Flatten` are folded correctly.
|
2021-01-09 16:55:41 -06:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_flatten_fold() {
|
|
|
|
let xs = [0, 3, 6];
|
|
|
|
let ys = [1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let mut it = xs.iter().map(|&x| x..x + 3).flatten();
|
|
|
|
assert_eq!(it.next(), Some(0));
|
|
|
|
assert_eq!(it.next_back(), Some(8));
|
|
|
|
let i = it.fold(0, |i, x| {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i + 1
|
|
|
|
});
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
|
|
|
|
let mut it = xs.iter().map(|&x| x..x + 3).flatten();
|
|
|
|
assert_eq!(it.next(), Some(0));
|
|
|
|
assert_eq!(it.next_back(), Some(8));
|
|
|
|
let i = it.rfold(ys.len(), |i, x| {
|
|
|
|
assert_eq!(x, ys[i - 1]);
|
|
|
|
i - 1
|
|
|
|
});
|
|
|
|
assert_eq!(i, 0);
|
|
|
|
}
|
2021-01-22 15:58:21 -06:00
|
|
|
|
2021-01-09 16:55:41 -06:00
|
|
|
#[test]
|
|
|
|
fn test_flatten_try_folds() {
|
|
|
|
let f = &|acc, x| i32::checked_add(acc * 2 / 3, x);
|
|
|
|
let mr = &|x| (5 * x)..(5 * x + 5);
|
|
|
|
assert_eq!((0..10).map(mr).flatten().try_fold(7, f), (0..50).try_fold(7, f));
|
|
|
|
assert_eq!((0..10).map(mr).flatten().try_rfold(7, f), (0..50).try_rfold(7, f));
|
|
|
|
let mut iter = (0..10).map(mr).flatten();
|
|
|
|
iter.next();
|
|
|
|
iter.next_back(); // have front and back iters in progress
|
|
|
|
assert_eq!(iter.try_rfold(7, f), (1..49).try_rfold(7, f));
|
|
|
|
|
|
|
|
let mut iter = (0..10).map(|x| (4 * x)..(4 * x + 4)).flatten();
|
|
|
|
assert_eq!(iter.try_fold(0, i8::checked_add), None);
|
|
|
|
assert_eq!(iter.next(), Some(17));
|
|
|
|
assert_eq!(iter.try_rfold(0, i8::checked_add), None);
|
|
|
|
assert_eq!(iter.next_back(), Some(35));
|
|
|
|
}
|
2021-01-22 15:58:21 -06:00
|
|
|
|
2021-07-12 14:40:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_flatten_advance_by() {
|
|
|
|
let mut it = once(0..10).chain(once(10..30)).chain(once(30..40)).flatten();
|
2021-10-14 05:10:18 -05:00
|
|
|
|
2021-07-12 14:40:38 -05:00
|
|
|
it.advance_by(5).unwrap();
|
|
|
|
assert_eq!(it.next(), Some(5));
|
|
|
|
it.advance_by(9).unwrap();
|
|
|
|
assert_eq!(it.next(), Some(15));
|
|
|
|
it.advance_back_by(4).unwrap();
|
|
|
|
assert_eq!(it.next_back(), Some(35));
|
|
|
|
it.advance_back_by(9).unwrap();
|
|
|
|
assert_eq!(it.next_back(), Some(25));
|
|
|
|
|
|
|
|
assert_eq!(it.advance_by(usize::MAX), Err(9));
|
|
|
|
assert_eq!(it.advance_back_by(usize::MAX), Err(0));
|
2021-10-14 05:10:18 -05:00
|
|
|
it.advance_by(0).unwrap();
|
|
|
|
it.advance_back_by(0).unwrap();
|
2021-07-12 14:40:38 -05:00
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
}
|
|
|
|
|
2021-01-09 16:55:41 -06:00
|
|
|
#[test]
|
|
|
|
fn test_flatten_non_fused_outer() {
|
|
|
|
let mut iter = NonFused::new(once(0..2)).flatten();
|
|
|
|
|
|
|
|
assert_eq!(iter.next_back(), Some(1));
|
|
|
|
assert_eq!(iter.next(), Some(0));
|
|
|
|
assert_eq!(iter.next(), None);
|
2021-01-23 13:52:59 -06:00
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
|
|
|
|
let mut iter = NonFused::new(once(0..2)).flatten();
|
|
|
|
|
|
|
|
assert_eq!(iter.next(), Some(0));
|
|
|
|
assert_eq!(iter.next_back(), Some(1));
|
|
|
|
assert_eq!(iter.next_back(), None);
|
|
|
|
assert_eq!(iter.next_back(), None);
|
2021-01-09 16:55:41 -06:00
|
|
|
}
|
2021-01-22 15:58:21 -06:00
|
|
|
|
2021-01-09 16:55:41 -06:00
|
|
|
#[test]
|
|
|
|
fn test_flatten_non_fused_inner() {
|
|
|
|
let mut iter = once(0..1).chain(once(1..3)).flat_map(NonFused::new);
|
|
|
|
|
|
|
|
assert_eq!(iter.next_back(), Some(2));
|
|
|
|
assert_eq!(iter.next(), Some(0));
|
|
|
|
assert_eq!(iter.next(), Some(1));
|
|
|
|
assert_eq!(iter.next(), None);
|
2021-01-23 13:52:59 -06:00
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
|
|
|
|
let mut iter = once(0..1).chain(once(1..3)).flat_map(NonFused::new);
|
|
|
|
|
|
|
|
assert_eq!(iter.next(), Some(0));
|
|
|
|
assert_eq!(iter.next_back(), Some(2));
|
|
|
|
assert_eq!(iter.next_back(), Some(1));
|
|
|
|
assert_eq!(iter.next_back(), None);
|
|
|
|
assert_eq!(iter.next_back(), None);
|
2021-01-09 16:55:41 -06:00
|
|
|
}
|
2021-01-22 16:20:42 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_flatten() {
|
|
|
|
let u = [0, 1];
|
|
|
|
let v = [5, 6, 7, 8];
|
|
|
|
let mut it = u.iter().map(|x| &v[*x..v.len()]).flatten();
|
|
|
|
assert_eq!(it.next_back().unwrap(), &8);
|
|
|
|
assert_eq!(it.next().unwrap(), &5);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &7);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &6);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &8);
|
|
|
|
assert_eq!(it.next().unwrap(), &6);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &7);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
}
|
2021-07-15 15:59:26 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trusted_len_flatten() {
|
|
|
|
fn assert_trusted_len<T: TrustedLen>(_: &T) {}
|
2021-09-03 05:36:33 -05:00
|
|
|
let mut iter = IntoIterator::into_iter([[0; 3]; 4]).flatten();
|
2021-07-15 15:59:26 -05:00
|
|
|
assert_trusted_len(&iter);
|
|
|
|
|
|
|
|
assert_eq!(iter.size_hint(), (12, Some(12)));
|
|
|
|
iter.next();
|
|
|
|
assert_eq!(iter.size_hint(), (11, Some(11)));
|
|
|
|
iter.next_back();
|
|
|
|
assert_eq!(iter.size_hint(), (10, Some(10)));
|
|
|
|
|
2021-09-03 05:36:33 -05:00
|
|
|
let iter = IntoIterator::into_iter([[(); usize::MAX]; 1]).flatten();
|
2021-07-15 15:59:26 -05:00
|
|
|
assert_eq!(iter.size_hint(), (usize::MAX, Some(usize::MAX)));
|
|
|
|
|
2021-09-03 05:36:33 -05:00
|
|
|
let iter = IntoIterator::into_iter([[(); usize::MAX]; 2]).flatten();
|
2021-07-15 15:59:26 -05:00
|
|
|
assert_eq!(iter.size_hint(), (usize::MAX, None));
|
|
|
|
|
2021-07-16 13:30:53 -05:00
|
|
|
let mut a = [(); 10];
|
|
|
|
let mut b = [(); 10];
|
|
|
|
|
2021-09-03 05:36:33 -05:00
|
|
|
let iter = IntoIterator::into_iter([&mut a, &mut b]).flatten();
|
2021-07-16 13:30:53 -05:00
|
|
|
assert_trusted_len(&iter);
|
|
|
|
assert_eq!(iter.size_hint(), (20, Some(20)));
|
|
|
|
core::mem::drop(iter);
|
|
|
|
|
2021-09-03 05:36:33 -05:00
|
|
|
let iter = IntoIterator::into_iter([&a, &b]).flatten();
|
2021-07-16 13:30:53 -05:00
|
|
|
assert_trusted_len(&iter);
|
|
|
|
assert_eq!(iter.size_hint(), (20, Some(20)));
|
|
|
|
|
2021-07-15 15:59:26 -05:00
|
|
|
let iter = [(), (), ()].iter().flat_map(|_| [(); 1000]);
|
|
|
|
assert_trusted_len(&iter);
|
|
|
|
assert_eq!(iter.size_hint(), (3000, Some(3000)));
|
2021-07-16 13:30:53 -05:00
|
|
|
|
|
|
|
let iter = [(), ()].iter().flat_map(|_| &a);
|
|
|
|
assert_trusted_len(&iter);
|
|
|
|
assert_eq!(iter.size_hint(), (20, Some(20)));
|
2021-07-15 15:59:26 -05:00
|
|
|
}
|