mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -05:00
some tests for functional hashtables
This commit is contained in:
parent
6532906f1b
commit
7bde68ae75
3 changed files with 97 additions and 1 deletions
95
tests/test_fHashtbl.ml
Normal file
95
tests/test_fHashtbl.ml
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
|
||||||
|
open OUnit
|
||||||
|
|
||||||
|
module IFHashtbl = FHashtbl.Make(struct
|
||||||
|
type t = int
|
||||||
|
let equal i j = i = j
|
||||||
|
let hash i = i
|
||||||
|
end)
|
||||||
|
|
||||||
|
let test_add () =
|
||||||
|
let h = IFHashtbl.empty 32 in
|
||||||
|
let h = IFHashtbl.replace h 42 "foo" in
|
||||||
|
OUnit.assert_equal (IFHashtbl.find h 42) "foo"
|
||||||
|
|
||||||
|
(*
|
||||||
|
let my_list =
|
||||||
|
[ 1, "a";
|
||||||
|
2, "b";
|
||||||
|
3, "c";
|
||||||
|
4, "d";
|
||||||
|
]
|
||||||
|
|
||||||
|
let my_seq = Sequence.of_list my_list
|
||||||
|
|
||||||
|
let test_of_seq () =
|
||||||
|
let h = IHashtbl.create 5 in
|
||||||
|
IHashtbl.of_seq h my_seq;
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 2) "b";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 1) "a";
|
||||||
|
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 42);
|
||||||
|
()
|
||||||
|
|
||||||
|
let test_to_seq () =
|
||||||
|
let h = IHashtbl.create 5 in
|
||||||
|
IHashtbl.of_seq h my_seq;
|
||||||
|
let l = Sequence.to_list (IHashtbl.to_seq h) in
|
||||||
|
OUnit.assert_equal my_list (List.sort compare l)
|
||||||
|
|
||||||
|
let test_resize () =
|
||||||
|
let h = IHashtbl.create 5 in
|
||||||
|
for i = 0 to 10 do
|
||||||
|
IHashtbl.replace h i (string_of_int i);
|
||||||
|
done;
|
||||||
|
OUnit.assert_bool "must have been resized" (IHashtbl.length h > 5);
|
||||||
|
()
|
||||||
|
|
||||||
|
let test_eq () =
|
||||||
|
let h = IHashtbl.create 3 in
|
||||||
|
IHashtbl.replace h 1 "odd";
|
||||||
|
IHashtbl.replace h 2 "even";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 1) "odd";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 2) "even";
|
||||||
|
()
|
||||||
|
|
||||||
|
let test_copy () =
|
||||||
|
let h = IHashtbl.create 2 in
|
||||||
|
IHashtbl.replace h 1 "one";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 1) "one";
|
||||||
|
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 2);
|
||||||
|
let h' = IHashtbl.copy h in
|
||||||
|
IHashtbl.replace h' 2 "two";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h' 1) "one";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h' 2) "two";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 1) "one";
|
||||||
|
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 2);
|
||||||
|
()
|
||||||
|
|
||||||
|
let test_remove () =
|
||||||
|
let h = IHashtbl.create 3 in
|
||||||
|
IHashtbl.of_seq h my_seq;
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 2) "b";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 3) "c";
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 4) "d";
|
||||||
|
OUnit.assert_equal (IHashtbl.length h) 4;
|
||||||
|
IHashtbl.remove h 2;
|
||||||
|
OUnit.assert_equal (IHashtbl.find h 3) "c";
|
||||||
|
OUnit.assert_equal (IHashtbl.length h) 3;
|
||||||
|
(* test that 2 has been removed *)
|
||||||
|
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 2)
|
||||||
|
*)
|
||||||
|
|
||||||
|
let suite =
|
||||||
|
"test_pHashtbl" >:::
|
||||||
|
[ "test_add" >:: test_add;
|
||||||
|
(*
|
||||||
|
"test_of_seq" >:: test_of_seq;
|
||||||
|
"test_to_seq" >:: test_to_seq;
|
||||||
|
"test_resize" >:: test_resize;
|
||||||
|
"test_eq" >:: test_eq;
|
||||||
|
"test_copy" >:: test_copy;
|
||||||
|
"test_remove" >:: test_remove;
|
||||||
|
*)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@ let test_remove () =
|
||||||
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 2)
|
OUnit.assert_raises Not_found (fun () -> IHashtbl.find h 2)
|
||||||
|
|
||||||
let suite =
|
let suite =
|
||||||
"test_pHashtbl" >:::
|
"test_flatHashtbl" >:::
|
||||||
[ "test_add" >:: test_add;
|
[ "test_add" >:: test_add;
|
||||||
"test_of_seq" >:: test_of_seq;
|
"test_of_seq" >:: test_of_seq;
|
||||||
"test_to_seq" >:: test_to_seq;
|
"test_to_seq" >:: test_to_seq;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ open OUnit
|
||||||
let suite =
|
let suite =
|
||||||
"all_tests" >:::
|
"all_tests" >:::
|
||||||
[ Test_pHashtbl.suite;
|
[ Test_pHashtbl.suite;
|
||||||
|
Test_fHashtbl.suite;
|
||||||
Test_flatHashtbl.suite;
|
Test_flatHashtbl.suite;
|
||||||
Test_heap.suite;
|
Test_heap.suite;
|
||||||
Test_graph.suite;
|
Test_graph.suite;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue