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