From 0d3eaa848ce6b44a83502174eae16524c893dd28 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 15 Jan 2023 13:08:20 -0800 Subject: [PATCH] Add test showing broken behavior of BinaryHeap::retain --- .../alloc/src/collections/binary_heap/tests.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/library/alloc/src/collections/binary_heap/tests.rs b/library/alloc/src/collections/binary_heap/tests.rs index ffbb6c80ac0..835b2e5b7e9 100644 --- a/library/alloc/src/collections/binary_heap/tests.rs +++ b/library/alloc/src/collections/binary_heap/tests.rs @@ -474,6 +474,23 @@ fn test_retain() { assert!(a.is_empty()); } +#[test] +fn test_retain_catch_unwind() { + let mut heap = BinaryHeap::from(vec![3, 1, 2]); + + // Removes the 3, then unwinds out of retain. + let _ = catch_unwind(AssertUnwindSafe(|| { + heap.retain(|e| { + if *e == 1 { + panic!(); + } + false + }); + })); + + assert_eq!(heap.into_vec(), [1, 2]); // BAD!! +} + // old binaryheap failed this test // // Integrity means that all elements are present after a comparison panics,