mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
test file for Graph
This commit is contained in:
parent
009afc575b
commit
33d8ac6253
5 changed files with 62 additions and 8 deletions
2
graph.ml
2
graph.ml
|
|
@ -92,7 +92,7 @@ let prev t v =
|
|||
Sequence.of_list (PHashtbl.find t v).n_prev
|
||||
|
||||
let between t v1 v2 =
|
||||
let edges = Sequence.of_list (PHashtbl.find t v1).n_prev in
|
||||
let edges = Sequence.of_list (PHashtbl.find t v1).n_next in
|
||||
let edges = Sequence.filter (fun (e, v2') -> (PHashtbl.get_eq t) v2 v2') edges in
|
||||
Sequence.map fst edges
|
||||
|
||||
|
|
|
|||
9
tests/helpers.ml
Normal file
9
tests/helpers.ml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
(** Some helpers for tests *)
|
||||
|
||||
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
|
||||
50
tests/test_graph.ml
Normal file
50
tests/test_graph.ml
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
(** Tests on graphs *)
|
||||
|
||||
open OUnit
|
||||
open Helpers
|
||||
|
||||
(* build a graph from a list of pairs of ints *)
|
||||
let mk_graph l =
|
||||
let g = Graph.empty 5 in
|
||||
Graph.add_seq g
|
||||
(Sequence.map (fun (x,y) -> x,1,y)
|
||||
(Sequence.of_list l));
|
||||
g
|
||||
|
||||
let test_copy () =
|
||||
let g = mk_graph [0,1; 1,2; 2,3; 3,0] in
|
||||
let g' = Graph.copy g in
|
||||
Graph.add g 1 1 3;
|
||||
Graph.add g 1 2 3;
|
||||
OUnit.assert_equal ~printer:print_int_list
|
||||
[1;2] (List.sort compare (Sequence.to_list (Graph.between g 1 3)));
|
||||
OUnit.assert_bool "copy" (Sequence.is_empty (Graph.between g' 1 3));
|
||||
()
|
||||
|
||||
let test_roots () =
|
||||
let g = mk_graph [0,1; 1,2; 2,3; 4,1; 5,1; 6,5; 3,5] in
|
||||
let roots = Sequence.to_list (Graph.roots g) in
|
||||
OUnit.assert_equal (List.sort compare roots) [0;4;6]
|
||||
|
||||
let test_leaves () =
|
||||
let g = mk_graph [0,1; 1,2; 2,3; 4,1; 6,5; 3,5; 3,7] in
|
||||
let leaves = Sequence.to_list (Graph.leaves g) in
|
||||
OUnit.assert_equal (List.sort compare leaves) [5;7]
|
||||
|
||||
let test_dfs () =
|
||||
let g = mk_graph [0,1; 1,2; 2,3; 3,0] in
|
||||
|
||||
()
|
||||
|
||||
let test_dijkstra () =
|
||||
()
|
||||
|
||||
let suite =
|
||||
"test_graph" >:::
|
||||
[ "test_copy" >:: test_copy;
|
||||
"test_leaves" >:: test_leaves;
|
||||
"test_roots" >:: test_roots;
|
||||
"test_dfs" >:: test_dfs;
|
||||
"test_dijkstra" >:: test_dijkstra;
|
||||
]
|
||||
|
|
@ -1,13 +1,7 @@
|
|||
(** Test heaps *)
|
||||
|
||||
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
|
||||
open Helpers
|
||||
|
||||
let test_empty () =
|
||||
let h = Heap.empty ~cmp:(fun x y -> x - y) in
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ let suite =
|
|||
"all_tests" >:::
|
||||
[ Test_pHashtbl.suite;
|
||||
Test_heap.suite;
|
||||
Test_graph.suite;
|
||||
]
|
||||
|
||||
let _ =
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue