diff options
| author | kennytm <kennytm@gmail.com> | 2018-02-14 16:14:42 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-02-14 18:25:22 +0800 |
| commit | bebd2fbfc836f871721057766a90775efa86e6a2 (patch) | |
| tree | 95c390683b7b3bc02ce8f7741ad131fcf381f875 /src/libcore/tests | |
| parent | dcb15269f676f5188f048d4ba014c402a1378086 (diff) | |
| parent | db13296b6fd6b68ab06055bdcb9a22078b11de6a (diff) | |
| download | rust-bebd2fbfc836f871721057766a90775efa86e6a2.tar.gz rust-bebd2fbfc836f871721057766a90775efa86e6a2.zip | |
Rollup merge of #48156 - Centril:feature/iterator_repeat_with, r=alexcrichton
Add std/core::iter::repeat_with Adds an iterator primitive `repeat_with` which is the "lazy" version of `repeat` but also more flexible since you can build up state with the `FnMut`. The design is mostly taken from `repeat`. r? @rust-lang/libs cc @withoutboats, @scottmcm
Diffstat (limited to 'src/libcore/tests')
| -rw-r--r-- | src/libcore/tests/iter.rs | 45 | ||||
| -rw-r--r-- | src/libcore/tests/lib.rs | 1 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index d8c9dcd8664..f91c919d744 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -1593,6 +1593,51 @@ fn test_repeat_take_collect() { } #[test] +fn test_repeat_with() { + #[derive(PartialEq, Debug)] + 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!(pow2.next(), Some(1)); + assert_eq!(pow2.next(), Some(2)); + assert_eq!(pow2.next(), Some(4)); + assert_eq!(pow2.next(), Some(8)); + assert_eq!(pow2.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_with_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; assert_eq!(it.len(), 3); diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 0d55e6eeeb1..3e901a9d442 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -27,6 +27,7 @@ #![feature(iterator_try_fold)] #![feature(iter_rfind)] #![feature(iter_rfold)] +#![feature(iterator_repeat_with)] #![feature(nonzero)] #![feature(pattern)] #![feature(range_is_empty)] |
