diff options
| author | Tim Vermeulen <tvermeulen@me.com> | 2019-08-18 21:47:23 +0200 |
|---|---|---|
| committer | Tim Vermeulen <tvermeulen@me.com> | 2019-08-18 21:47:23 +0200 |
| commit | ec54340756f325324f4b710105a708da1cf26564 (patch) | |
| tree | f688298392ae0abf0a545e771ce639623b3b4671 /src/libcore/tests | |
| parent | ea52be482ab4945fda63cb65b6a198309a041e3c (diff) | |
| download | rust-ec54340756f325324f4b710105a708da1cf26564.tar.gz rust-ec54340756f325324f4b710105a708da1cf26564.zip | |
Fix bug in iter::Chain::size_hint
Diffstat (limited to 'src/libcore/tests')
| -rw-r--r-- | src/libcore/tests/iter.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index a1a27e1d538..3a4f76852a0 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -153,6 +153,54 @@ fn test_iterator_chain_find() { } #[test] +fn test_iterator_chain_size_hint() { + struct Iter { + is_empty: bool, + } + + impl Iterator for Iter { + type Item = (); + + // alternates between `None` and `Some(())` + fn next(&mut self) -> Option<Self::Item> { + if self.is_empty { + self.is_empty = false; + None + } else { + self.is_empty = true; + Some(()) + } + } + + fn size_hint(&self) -> (usize, Option<usize>) { + if self.is_empty { + (0, Some(0)) + } else { + (1, Some(1)) + } + } + } + + impl DoubleEndedIterator for Iter { + fn next_back(&mut self) -> Option<Self::Item> { + self.next() + } + } + + // this chains an iterator of length 0 with an iterator of length 1, + // so after calling `.next()` once, the iterator is empty and the + // state is `ChainState::Back`. `.size_hint()` should now disregard + // the size hint of the left iterator + let mut iter = Iter { is_empty: true }.chain(once(())); + assert_eq!(iter.next(), Some(())); + assert_eq!(iter.size_hint(), (0, Some(0))); + + let mut iter = once(()).chain(Iter { is_empty: true }); + assert_eq!(iter.next_back(), Some(())); + assert_eq!(iter.size_hint(), (0, Some(0))); +} + +#[test] fn test_zip_nth() { let xs = [0, 1, 2, 4, 5]; let ys = [10, 11, 12]; |
