diff --git a/tests/test_heap.ml b/tests/test_heap.ml index 0f222066..3b10cd94 100644 --- a/tests/test_heap.ml +++ b/tests/test_heap.ml @@ -2,11 +2,41 @@ open OUnit +let print_int_list l = + let b = Buffer.create 20 in + Format.bprintf b "@[[%a]@]" + (Sequence.pp_seq ~sep:", " Format.pp_print_int) + (Sequence.of_list l); + Buffer.contents b + let test_empty () = - let h = Heap.empty ~compare:(fun x y -> x - y) in + let h = Heap.empty ~cmp:(fun x y -> x - y) in OUnit.assert_bool "is_empty empty" (Heap.is_empty h) +let test_sort () = + let h = Heap.empty ~cmp:(fun x y -> x - y) in + (* Heap sort *) + let l = [3;4;2;1;6;5;0;7;10;9;8] in + Heap.of_seq h (Sequence.of_list l); + let l' = Sequence.to_list (Heap.to_seq h) in + OUnit.assert_equal ~printer:print_int_list l' [0;1;2;3;4;5;6;7;8;9;10] + +let test_remove () = + let h = Heap.empty ~cmp:(fun x y -> x - y) in + let l = [3;4;2;1;6;5;0;7;10;9;8] in + Heap.of_seq h (Sequence.of_list l); + (* check pop *) + OUnit.assert_equal (Heap.pop h) 0; + OUnit.assert_equal (Heap.pop h) 1; + OUnit.assert_equal (Heap.pop h) 2; + OUnit.assert_equal (Heap.pop h) 3; + (* check that elements have been removed *) + let l' = Sequence.to_list (Heap.to_seq h) in + OUnit.assert_equal ~printer:print_int_list l' [4;5;6;7;8;9;10] + let suite = "test_heaps" >::: [ "test_empty" >:: test_empty; + "test_sort" >:: test_sort; + "test_remove" >:: test_remove; ] diff --git a/tests/tests.ml b/tests/tests.ml index ce5f78ca..09e6a200 100644 --- a/tests/tests.ml +++ b/tests/tests.ml @@ -5,7 +5,7 @@ open OUnit let suite = "all_tests" >::: [ Test_pHashtbl.suite; - (* Test_heap.suite; *) + Test_heap.suite; ] let _ =