Skip to content

Commit a80e685

Browse files
committed
Test leaking of BinaryHeap Drain iterators
1 parent 204f854 commit a80e685

File tree

1 file changed

+53
-0
lines changed
  • library/alloc/src/collections/binary_heap

1 file changed

+53
-0
lines changed

library/alloc/src/collections/binary_heap/tests.rs

+53
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,59 @@ fn test_drain_sorted_leak() {
317317
assert!(q.is_empty());
318318
}
319319

320+
#[test]
321+
fn test_drain_forget() {
322+
let a = CrashTestDummy::new(0);
323+
let b = CrashTestDummy::new(1);
324+
let c = CrashTestDummy::new(2);
325+
let mut q =
326+
BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]);
327+
328+
catch_unwind(AssertUnwindSafe(|| {
329+
let mut it = q.drain();
330+
it.next();
331+
mem::forget(it);
332+
}))
333+
.unwrap();
334+
// Behaviour after leaking is explicitly unspecified and order is arbitrary,
335+
// so it's fine if these start failing, but probably worth knowing.
336+
assert!(q.is_empty());
337+
assert_eq!(a.dropped() + b.dropped() + c.dropped(), 1);
338+
assert_eq!(a.dropped(), 0);
339+
assert_eq!(b.dropped(), 0);
340+
assert_eq!(c.dropped(), 1);
341+
drop(q);
342+
assert_eq!(a.dropped(), 0);
343+
assert_eq!(b.dropped(), 0);
344+
assert_eq!(c.dropped(), 1);
345+
}
346+
347+
#[test]
348+
fn test_drain_sorted_forget() {
349+
let a = CrashTestDummy::new(0);
350+
let b = CrashTestDummy::new(1);
351+
let c = CrashTestDummy::new(2);
352+
let mut q =
353+
BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]);
354+
355+
catch_unwind(AssertUnwindSafe(|| {
356+
let mut it = q.drain_sorted();
357+
it.next();
358+
mem::forget(it);
359+
}))
360+
.unwrap();
361+
// Behaviour after leaking is explicitly unspecified,
362+
// so it's fine if these start failing, but probably worth knowing.
363+
assert_eq!(q.len(), 2);
364+
assert_eq!(a.dropped(), 0);
365+
assert_eq!(b.dropped(), 0);
366+
assert_eq!(c.dropped(), 1);
367+
drop(q);
368+
assert_eq!(a.dropped(), 1);
369+
assert_eq!(b.dropped(), 1);
370+
assert_eq!(c.dropped(), 1);
371+
}
372+
320373
#[test]
321374
fn test_extend_ref() {
322375
let mut a = BinaryHeap::new();

0 commit comments

Comments
 (0)