mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
more graph tests
This commit is contained in:
parent
33d8ac6253
commit
151156fb43
2 changed files with 41 additions and 4 deletions
|
|
@ -130,11 +130,11 @@ val min_path_full : ('v, 'e) t ->
|
||||||
(** Find the minimal path, from the given ['v], that does not contain
|
(** Find the minimal path, from the given ['v], that does not contain
|
||||||
any 'v satisfying [ignore], and that reaches a 'v
|
any 'v satisfying [ignore], and that reaches a 'v
|
||||||
that satisfies [goal]. It raises Not_found if no reachable node
|
that satisfies [goal]. It raises Not_found if no reachable node
|
||||||
satisfies [goal]. *)
|
satisfies [goal]. The path is reversed. *)
|
||||||
|
|
||||||
val min_path : ('v, 'e) t -> cost:('e -> int) -> 'v -> 'v -> ('v,'e) path
|
val min_path : ('v, 'e) t -> cost:('e -> int) -> 'v -> 'v -> ('v,'e) path
|
||||||
(** Minimal path from first 'v to second, given the cost function,
|
(** Minimal path from first 'v to second, given the cost function,
|
||||||
or raises Not_found *)
|
or raises Not_found. The path is reversed. *)
|
||||||
|
|
||||||
val diameter : ('v, 'e) t -> 'v -> int
|
val diameter : ('v, 'e) t -> 'v -> int
|
||||||
(** Maximal distance between the given 'v, and any other 'v
|
(** Maximal distance between the given 'v, and any other 'v
|
||||||
|
|
|
||||||
|
|
@ -33,11 +33,47 @@ let test_leaves () =
|
||||||
OUnit.assert_equal (List.sort compare leaves) [5;7]
|
OUnit.assert_equal (List.sort compare leaves) [5;7]
|
||||||
|
|
||||||
let test_dfs () =
|
let test_dfs () =
|
||||||
let g = mk_graph [0,1; 1,2; 2,3; 3,0] in
|
let g = mk_graph [0,1; 1,2; 2,3; 3,0; 1,4; 1,5; 5,6; 4,6; 6,0] in
|
||||||
|
let l = ref [] in
|
||||||
|
Graph.dfs g 0 (fun (v,i) -> l := (v,i) :: !l);
|
||||||
|
(* get index of vertex [v] in DFS traversal *)
|
||||||
|
let get_idx v = List.assoc v !l in
|
||||||
|
OUnit.assert_bool "order" (get_idx 0 < get_idx 1);
|
||||||
|
OUnit.assert_bool "order" (get_idx 1 < get_idx 2);
|
||||||
|
OUnit.assert_bool "order" (get_idx 2 < get_idx 3);
|
||||||
|
OUnit.assert_bool "order" (get_idx 1 < get_idx 4);
|
||||||
|
OUnit.assert_bool "order" (get_idx 1 < get_idx 5);
|
||||||
|
OUnit.assert_bool "order" (get_idx 4 < get_idx 6 || get_idx 5 < get_idx 6);
|
||||||
()
|
()
|
||||||
|
|
||||||
|
let test_bfs () =
|
||||||
|
let g = mk_graph [0,1; 1,2; 2,3; 2,4; 3,0; 1,4; 1,5; 5,6; 4,6; 6,0] in
|
||||||
|
let l = Sequence.to_list
|
||||||
|
(Sequence.mapi (fun i v -> (v,i)) (Graph.bfs_seq g 0)) in
|
||||||
|
(* get index of vertex [v] in DFS traversal *)
|
||||||
|
let get_idx v = List.assoc v l in
|
||||||
|
OUnit.assert_bool "order" (get_idx 0 < get_idx 1);
|
||||||
|
OUnit.assert_bool "order" (get_idx 0 < get_idx 2);
|
||||||
|
OUnit.assert_bool "order" (get_idx 0 < get_idx 4);
|
||||||
|
OUnit.assert_bool "order" (get_idx 1 < get_idx 3);
|
||||||
|
OUnit.assert_bool "order" (get_idx 2 < get_idx 3);
|
||||||
|
OUnit.assert_bool "order" (get_idx 4 < get_idx 6);
|
||||||
|
OUnit.assert_bool "order" (get_idx 5 < get_idx 6);
|
||||||
|
()
|
||||||
|
|
||||||
|
let rec pp_path p =
|
||||||
|
let buf = Buffer.create 10 in
|
||||||
|
Format.bprintf buf "%a" (Sequence.pp_seq ~sep:"; " pp_edge)
|
||||||
|
(Sequence.of_list p);
|
||||||
|
Buffer.contents buf
|
||||||
|
and pp_edge formatter (v1,e,v2) =
|
||||||
|
Format.fprintf formatter "%d -> %d" v1 v2
|
||||||
|
|
||||||
let test_dijkstra () =
|
let test_dijkstra () =
|
||||||
|
let g = mk_graph [0,1; 1,2; 2,3; 3,4; 3,0; 4,5; 1,5; 5,6; 4,6; 6,0] in
|
||||||
|
let path = Graph.min_path g ~cost:(fun x -> x) 0 6 in
|
||||||
|
let path = Graph.rev_path path in
|
||||||
|
OUnit.assert_equal ~printer:pp_path [0,1,1; 1,1,5; 5,1,6] path;
|
||||||
()
|
()
|
||||||
|
|
||||||
let suite =
|
let suite =
|
||||||
|
|
@ -46,5 +82,6 @@ let suite =
|
||||||
"test_leaves" >:: test_leaves;
|
"test_leaves" >:: test_leaves;
|
||||||
"test_roots" >:: test_roots;
|
"test_roots" >:: test_roots;
|
||||||
"test_dfs" >:: test_dfs;
|
"test_dfs" >:: test_dfs;
|
||||||
|
"test_bfs" >:: test_bfs;
|
||||||
"test_dijkstra" >:: test_dijkstra;
|
"test_dijkstra" >:: test_dijkstra;
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue