add core::iter::repeat_with
This commit is contained in:
parent
b8398d947d
commit
0f789aad2b
2 changed files with 148 additions and 0 deletions
|
@ -1549,6 +1549,50 @@ fn test_repeat_take_collect() {
|
|||
assert_eq!(v, vec![42, 42, 42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_with() {
|
||||
struct NotClone(usize);
|
||||
let mut it = repeat_with(|| NotClone(42));
|
||||
assert_eq!(it.next(), Some(NotClone(42)));
|
||||
assert_eq!(it.next(), Some(NotClone(42)));
|
||||
assert_eq!(it.next(), Some(NotClone(42)));
|
||||
assert_eq!(repeat_with(|| NotClone(42)).size_hint(), (usize::MAX, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_with_rev() {
|
||||
let mut curr = 1;
|
||||
let mut pow2 = repeat_with(|| { let tmp = curr; curr *= 2; tmp })
|
||||
.rev().take(4);
|
||||
assert_eq!(it.next(), Some(1));
|
||||
assert_eq!(it.next(), Some(2));
|
||||
assert_eq!(it.next(), Some(4));
|
||||
assert_eq!(it.next(), Some(8));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_with_take() {
|
||||
let mut it = repeat_with(|| 42).take(3);
|
||||
assert_eq!(it.next(), Some(42));
|
||||
assert_eq!(it.next(), Some(42));
|
||||
assert_eq!(it.next(), Some(42));
|
||||
assert_eq!(it.next(), None);
|
||||
is_trusted_len(repeat_with(|| 42).take(3));
|
||||
assert_eq!(repeat_with(|| 42).take(3).size_hint(), (3, Some(3)));
|
||||
assert_eq!(repeat_with(|| 42).take(0).size_hint(), (0, Some(0)));
|
||||
assert_eq!(repeat_with(|| 42).take(usize::MAX).size_hint(),
|
||||
(usize::MAX, Some(usize::MAX)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_take_collect() {
|
||||
let mut curr = 1;
|
||||
let v: Vec<_> = repeat_with(|| { let tmp = curr; curr *= 2; tmp })
|
||||
.take(5).collect();
|
||||
assert_eq!(v, vec![1, 2, 4, 8, 16]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fuse() {
|
||||
let mut it = 0..3;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue