Skip to content

Commit

Permalink
Add better test for BinaryHeap::retain.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Apr 22, 2021
1 parent 62226ee commit f5d72ab
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions library/alloc/tests/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,23 @@ fn assert_covariance() {

#[test]
fn test_retain() {
let mut a = BinaryHeap::from(vec![-10, -5, 1, 2, 4, 13]);
a.retain(|x| x % 2 == 0);
let mut a = BinaryHeap::from(vec![100, 10, 50, 1, 2, 20, 30]);
a.retain(|&x| x != 2);

assert_eq!(a.into_sorted_vec(), [-10, 2, 4])
// Check that 20 moved into 10's place.
assert_eq!(a.clone().into_vec(), [100, 20, 50, 1, 10, 30]);

a.retain(|_| true);

assert_eq!(a.clone().into_vec(), [100, 20, 50, 1, 10, 30]);

a.retain(|&x| x < 50);

assert_eq!(a.clone().into_vec(), [30, 20, 10, 1]);

a.retain(|_| false);

assert!(a.is_empty());
}

// old binaryheap failed this test
Expand Down

0 comments on commit f5d72ab

Please sign in to comment.