diff options
| author | Stein Somers <git@steinsomers.be> | 2022-02-18 16:01:39 +0100 |
|---|---|---|
| committer | Stein Somers <git@steinsomers.be> | 2022-05-02 10:07:51 +0200 |
| commit | a80e685e11a6876d3ec1e54f4e5fafd92c6c4eb2 (patch) | |
| tree | 27e5000b29463aa32e6632aacd03c06430773caa | |
| parent | 204f854586cebc5b2e46b1c83fb6d83fe01acf6a (diff) | |
| download | rust-a80e685e11a6876d3ec1e54f4e5fafd92c6c4eb2.tar.gz rust-a80e685e11a6876d3ec1e54f4e5fafd92c6c4eb2.zip | |
Test leaking of BinaryHeap Drain iterators
| -rw-r--r-- | library/alloc/src/collections/binary_heap/tests.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/library/alloc/src/collections/binary_heap/tests.rs b/library/alloc/src/collections/binary_heap/tests.rs index 4e54d7d5ab6..4a4c6fbdcfe 100644 --- a/library/alloc/src/collections/binary_heap/tests.rs +++ b/library/alloc/src/collections/binary_heap/tests.rs @@ -318,6 +318,59 @@ fn test_drain_sorted_leak() { } #[test] +fn test_drain_forget() { + let a = CrashTestDummy::new(0); + let b = CrashTestDummy::new(1); + let c = CrashTestDummy::new(2); + let mut q = + BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]); + + catch_unwind(AssertUnwindSafe(|| { + let mut it = q.drain(); + it.next(); + mem::forget(it); + })) + .unwrap(); + // Behaviour after leaking is explicitly unspecified and order is arbitrary, + // so it's fine if these start failing, but probably worth knowing. + assert!(q.is_empty()); + assert_eq!(a.dropped() + b.dropped() + c.dropped(), 1); + assert_eq!(a.dropped(), 0); + assert_eq!(b.dropped(), 0); + assert_eq!(c.dropped(), 1); + drop(q); + assert_eq!(a.dropped(), 0); + assert_eq!(b.dropped(), 0); + assert_eq!(c.dropped(), 1); +} + +#[test] +fn test_drain_sorted_forget() { + let a = CrashTestDummy::new(0); + let b = CrashTestDummy::new(1); + let c = CrashTestDummy::new(2); + let mut q = + BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]); + + catch_unwind(AssertUnwindSafe(|| { + let mut it = q.drain_sorted(); + it.next(); + mem::forget(it); + })) + .unwrap(); + // Behaviour after leaking is explicitly unspecified, + // so it's fine if these start failing, but probably worth knowing. + assert_eq!(q.len(), 2); + assert_eq!(a.dropped(), 0); + assert_eq!(b.dropped(), 0); + assert_eq!(c.dropped(), 1); + drop(q); + assert_eq!(a.dropped(), 1); + assert_eq!(b.dropped(), 1); + assert_eq!(c.dropped(), 1); +} + +#[test] fn test_extend_ref() { let mut a = BinaryHeap::new(); a.push(1); |
