summaryrefslogtreecommitdiffstats
path: root/library/core/tests/iter/sources.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
commit5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch)
tree35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /library/core/tests/iter/sources.rs
parentAdding debian version 1.66.0+dfsg1-1. (diff)
downloadrustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz
rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/core/tests/iter/sources.rs')
-rw-r--r--library/core/tests/iter/sources.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/library/core/tests/iter/sources.rs b/library/core/tests/iter/sources.rs
index d0114ade6..a15f3a514 100644
--- a/library/core/tests/iter/sources.rs
+++ b/library/core/tests/iter/sources.rs
@@ -106,3 +106,52 @@ fn test_empty() {
let mut it = empty::<i32>();
assert_eq!(it.next(), None);
}
+
+#[test]
+fn test_repeat_n_drop() {
+ #[derive(Clone, Debug)]
+ struct DropCounter<'a>(&'a Cell<usize>);
+ impl Drop for DropCounter<'_> {
+ fn drop(&mut self) {
+ self.0.set(self.0.get() + 1);
+ }
+ }
+
+ // `repeat_n(x, 0)` drops `x` immediately
+ let count = Cell::new(0);
+ let item = DropCounter(&count);
+ let mut it = repeat_n(item, 0);
+ assert_eq!(count.get(), 1);
+ assert!(it.next().is_none());
+ assert_eq!(count.get(), 1);
+ drop(it);
+ assert_eq!(count.get(), 1);
+
+ // Dropping the iterator needs to drop the item if it's non-empty
+ let count = Cell::new(0);
+ let item = DropCounter(&count);
+ let it = repeat_n(item, 3);
+ assert_eq!(count.get(), 0);
+ drop(it);
+ assert_eq!(count.get(), 1);
+
+ // Dropping the iterator doesn't drop the item if it was exhausted
+ let count = Cell::new(0);
+ let item = DropCounter(&count);
+ let mut it = repeat_n(item, 3);
+ assert_eq!(count.get(), 0);
+ let x0 = it.next().unwrap();
+ assert_eq!(count.get(), 0);
+ let x1 = it.next().unwrap();
+ assert_eq!(count.get(), 0);
+ let x2 = it.next().unwrap();
+ assert_eq!(count.get(), 0);
+ assert!(it.next().is_none());
+ assert_eq!(count.get(), 0);
+ assert!(it.next().is_none());
+ assert_eq!(count.get(), 0);
+ drop(it);
+ assert_eq!(count.get(), 0);
+ drop((x0, x1, x2));
+ assert_eq!(count.get(), 3);
+}