Skip to content

Commit

Permalink
Add test showing broken behavior of BinaryHeap::retain
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 15, 2023
1 parent ae4d89d commit 0d3eaa8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions library/alloc/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0d3eaa8

Please sign in to comment.