dgraph: new module for directed graphs.
[ccan] / ccan / dgraph / test / run.c
1 #include <ccan/dgraph/dgraph.h>
2 /* Include the C files directly. */
3 #include <ccan/dgraph/dgraph.c>
4 #include <ccan/tap/tap.h>
5
6 static bool count_nodes(struct dgraph_node *n, unsigned int *count)
7 {
8         (*count)++;
9         return true;
10 }
11
12 static bool stop_traverse(struct dgraph_node *n, unsigned int *count)
13 {
14         if (--(*count) == 0)
15                 return false;
16         return true;
17 }
18
19 int main(void)
20 {
21         struct dgraph_node n1, n2, n3;
22         unsigned int count = 0;
23
24         /* This is how many tests you plan to run */
25         plan_tests(25);
26
27         dgraph_init_node(&n1);
28         count = 0;
29         dgraph_traverse_from(&n1, count_nodes, &count);
30         ok1(count == 0);
31         count = 0;
32         dgraph_traverse_to(&n1, count_nodes, &count);
33         ok1(count == 0);
34
35         dgraph_init_node(&n2);
36         dgraph_add_edge(&n1, &n2);
37         count = 0;
38         dgraph_traverse_from(&n1, count_nodes, &count);
39         ok1(count == 1);
40         count = 0;
41         dgraph_traverse_to(&n1, count_nodes, &count);
42         ok1(count == 0);
43         count = 0;
44         dgraph_traverse_from(&n2, count_nodes, &count);
45         ok1(count == 0);
46         count = 0;
47         dgraph_traverse_to(&n2, count_nodes, &count);
48         ok1(count == 1);
49
50         dgraph_init_node(&n3);
51         dgraph_add_edge(&n2, &n3);
52         count = 0;
53         dgraph_traverse_from(&n1, count_nodes, &count);
54         ok1(count == 2);
55         count = 0;
56         dgraph_traverse_to(&n1, count_nodes, &count);
57         ok1(count == 0);
58         count = 0;
59         dgraph_traverse_from(&n2, count_nodes, &count);
60         ok1(count == 1);
61         count = 0;
62         dgraph_traverse_to(&n2, count_nodes, &count);
63         ok1(count == 1);
64         count = 0;
65         dgraph_traverse_from(&n3, count_nodes, &count);
66         ok1(count == 0);
67         count = 0;
68         dgraph_traverse_to(&n3, count_nodes, &count);
69         ok1(count == 2);
70
71         /* Check stopping traverse. */
72         count = 1;
73         dgraph_traverse_from(&n1, stop_traverse, &count);
74         ok1(count == 0);
75         count = 2;
76         dgraph_traverse_from(&n1, stop_traverse, &count);
77         ok1(count == 0);
78         count = 3;
79         dgraph_traverse_from(&n1, stop_traverse, &count);
80         ok1(count == 1);
81
82         dgraph_clear_node(&n1);
83
84         count = 0;
85         dgraph_traverse_from(&n2, count_nodes, &count);
86         ok1(count == 1);
87         count = 0;
88         dgraph_traverse_to(&n2, count_nodes, &count);
89         ok1(count == 0);
90         count = 0;
91         dgraph_traverse_from(&n3, count_nodes, &count);
92         ok1(count == 0);
93         count = 0;
94         dgraph_traverse_to(&n3, count_nodes, &count);
95         ok1(count == 1);
96
97         ok1(dgraph_del_edge(&n2, &n3));
98         count = 0;
99         dgraph_traverse_from(&n2, count_nodes, &count);
100         ok1(count == 0);
101         count = 0;
102         dgraph_traverse_to(&n2, count_nodes, &count);
103         ok1(count == 0);
104         count = 0;
105         dgraph_traverse_from(&n3, count_nodes, &count);
106         ok1(count == 0);
107         count = 0;
108         dgraph_traverse_to(&n3, count_nodes, &count);
109         ok1(count == 0);
110
111         ok1(!dgraph_del_edge(&n2, &n3));
112         dgraph_clear_node(&n2);
113
114         /* This exits depending on whether all tests passed */
115         return exit_status();
116 }