File: test_bf.ml

package info (click to toggle)
ocamlgraph 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,624 kB
  • sloc: ml: 19,995; xml: 151; makefile: 14; sh: 1
file content (43 lines) | stat: -rw-r--r-- 1,572 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

(* Test file for Bellman-Ford *)

open Printf
open Graph
open Pack.Digraph

(* TODO: This could be done a tiny bit better, 
  no need for printing, this could use alcotest or equivalent *)

let test ~name has_cycle spec =
  printf "Running test with name: %s\n" name;
  let v = Array.init 5 V.create in
  let g = create () in
  let () = Array.iter (add_vertex g) v in

  let build (s,w,t) = add_edge_e g (E.create v.(s) w v.(t)) in
  List.iter build spec;
  begin try
    let cycle = bellman_ford g v.(1) in
    let print_edge e =
      printf "%d --(%d)--> %d\n" (V.label (E.src e)) (E.label e) (V.label (E.dst e))
    in
    List.iter print_edge cycle;
    assert has_cycle
  with Not_found ->
    printf "No cycle found\n";
    assert (not has_cycle)
  end;
  print_newline ();
  flush stdout

let () =
  test ~name:"cycle_1" true [ 0, (-3), 1; 1, 1, 2; 2, 1, 0; 1, 1, 3; 3, 1, 4; 4, 1, 0 ];
  test ~name:"cycle_2" true  [ 0, (-10), 1; 1, 1, 2; 2, 1, 0; 1, 1, 3; 3, 1, 4; 4, 1, 0 ];
  test ~name:"cycle_3" true  [ 0, (-10), 1;          2, 1, 0; 1, 1, 3; 3, 1, 4; 4, 1, 0 ];
  test ~name:"cycle_4" true  [ 0, (-10), 1; 1, 1, 2;          1, 1, 3; 3, 1, 4; 4, 1, 0 ];
  test ~name:"cycle_5" true  [ 0, (-10), 1; 1, 1, 2; 2, 1, 0;          3, 1, 4; 4, 1, 0 ];
  test ~name:"cycle_6" true  [ 0, (-10), 1; 1, 1, 2; 2, 1, 0; 1, 1, 3;          4, 1, 0 ];
  test ~name:"nocycle_1" false [              1, 1, 2; 2, 1, 0; 1, 1, 3; 3, 1, 4; 4, 1, 0 ];
  test ~name:"nocycle_2" false [ 0, (-10), 1; 1, 1, 2;          1, 1, 3; 3, 1, 4;         ];

  printf "All tests succeeded.\n"