rust/library/core/tests/iter/adapters/fuse.rs
2021-01-22 16:58:21 -05:00

58 lines
1.2 KiB
Rust

use core::iter::*;
#[test]
fn test_fuse_nth() {
let xs = [0, 1, 2];
let mut it = xs.iter();
assert_eq!(it.len(), 3);
assert_eq!(it.nth(2), Some(&2));
assert_eq!(it.len(), 0);
assert_eq!(it.nth(2), None);
assert_eq!(it.len(), 0);
}
#[test]
fn test_fuse_last() {
let xs = [0, 1, 2];
let it = xs.iter();
assert_eq!(it.len(), 3);
assert_eq!(it.last(), Some(&2));
}
#[test]
fn test_fuse_count() {
let xs = [0, 1, 2];
let it = xs.iter();
assert_eq!(it.len(), 3);
assert_eq!(it.count(), 3);
// Can't check len now because count consumes.
}
#[test]
fn test_fuse_fold() {
let xs = [0, 1, 2];
let it = xs.iter(); // `FusedIterator`
let i = it.fuse().fold(0, |i, &x| {
assert_eq!(x, xs[i]);
i + 1
});
assert_eq!(i, xs.len());
let it = xs.iter(); // `FusedIterator`
let i = it.fuse().rfold(xs.len(), |i, &x| {
assert_eq!(x, xs[i - 1]);
i - 1
});
assert_eq!(i, 0);
let it = xs.iter().scan((), |_, &x| Some(x)); // `!FusedIterator`
let i = it.fuse().fold(0, |i, x| {
assert_eq!(x, xs[i]);
i + 1
});
assert_eq!(i, xs.len());
}