updated tests , with tests on heaps

This commit is contained in:
Simon Cruanes 2013-03-05 11:05:34 +01:00
parent fc590bfb27
commit 2e19b5eb1a
2 changed files with 32 additions and 2 deletions

View file

@ -2,11 +2,41 @@
open OUnit
let print_int_list l =
let b = Buffer.create 20 in
Format.bprintf b "@[<h>[%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;
]

View file

@ -5,7 +5,7 @@ open OUnit
let suite =
"all_tests" >:::
[ Test_pHashtbl.suite;
(* Test_heap.suite; *)
Test_heap.suite;
]
let _ =