Skip to content

Commit 0d3eaa8

Browse files
committed
Add test showing broken behavior of BinaryHeap::retain
1 parent ae4d89d commit 0d3eaa8

File tree

1 file changed

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

1 file changed

+17
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,23 @@ fn test_retain() {
474474
assert!(a.is_empty());
475475
}
476476

477+
#[test]
478+
fn test_retain_catch_unwind() {
479+
let mut heap = BinaryHeap::from(vec![3, 1, 2]);
480+
481+
// Removes the 3, then unwinds out of retain.
482+
let _ = catch_unwind(AssertUnwindSafe(|| {
483+
heap.retain(|e| {
484+
if *e == 1 {
485+
panic!();
486+
}
487+
false
488+
});
489+
}));
490+
491+
assert_eq!(heap.into_vec(), [1, 2]); // BAD!!
492+
}
493+
477494
// old binaryheap failed this test
478495
//
479496
// Integrity means that all elements are present after a comparison panics,

0 commit comments

Comments
 (0)