Add test showing broken behavior of BinaryHeap::retain

This commit is contained in:
David Tolnay 2023-01-15 13:08:20 -08:00
parent ae4d89dfb5
commit 0d3eaa848c
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
1 changed files with 17 additions and 0 deletions

View File

@ -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,