Skip to content

Commit 9b1ba2a

Browse files
committed
Add test for re-heapify using boxed closure.
1 parent 99bce97 commit 9b1ba2a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/liballoc/tests/binary_heap.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,26 @@ fn test_from_iter() {
259259

260260
#[test]
261261
fn test_from_vec_cmp() {
262-
let vec = vec![9, 8, 7, 6, 5, 4, 3, 2, 1];
263-
let cmp = |a: &i32, b: &i32| b.partial_cmp(a);
262+
// closure
263+
let heap = BinaryHeap::from((vec![2, 3, 89, 5, 8, 1, 13, 21, 34, 55, 1],
264+
|a: &i32, b: &i32| b.partial_cmp(a)));
265+
assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(),
266+
vec![1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]);
267+
}
264268

265-
let heap = BinaryHeap::from((vec, cmp));
266-
assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(),
267-
vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
269+
#[test]
270+
fn test_from_vec_cmp_boxed_closure() {
271+
// boxed closure
272+
let vec = vec![2, 3, 89, 5, 8, 1, 13, 21, 34, 55, 1];
273+
let cmp1: Box<dyn Fn(&i32, &i32) -> Option<core::cmp::Ordering>> = Box::new(|a: &i32, b: &i32| a.partial_cmp(b));
274+
let cmp2: Box<dyn Fn(&i32, &i32) -> Option<core::cmp::Ordering>> = Box::new(|a: &i32, b: &i32| b.partial_cmp(a));
275+
276+
let mut heap = BinaryHeap::from((vec, cmp1));
277+
assert_eq!(heap.pop(), Some(89));
278+
279+
// re-heapify using different comparator
280+
heap = BinaryHeap::from((heap.into(), cmp2));
281+
assert_eq!(heap.pop(), Some(1));
268282
}
269283

270284
#[test]

0 commit comments

Comments
 (0)