dgraph: add dgraph_check and CCAN_DGRAPH_DEBUG
[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(42);
26
27         dgraph_init_node(&n1);
28         ok1(dgraph_check(&n1, NULL) == &n1);
29         count = 0;
30         dgraph_traverse_from(&n1, count_nodes, &count);
31         ok1(count == 0);
32         count = 0;
33         dgraph_traverse_to(&n1, count_nodes, &count);
34         ok1(count == 0);
35
36         dgraph_init_node(&n2);
37         ok1(dgraph_check(&n2, NULL) == &n2);
38         dgraph_add_edge(&n1, &n2);
39         ok1(dgraph_check(&n1, NULL) == &n1);
40         ok1(dgraph_check(&n2, NULL) == &n2);
41         count = 0;
42         dgraph_traverse_from(&n1, count_nodes, &count);
43         ok1(count == 1);
44         count = 0;
45         dgraph_traverse_to(&n1, count_nodes, &count);
46         ok1(count == 0);
47         count = 0;
48         dgraph_traverse_from(&n2, count_nodes, &count);
49         ok1(count == 0);
50         count = 0;
51         dgraph_traverse_to(&n2, count_nodes, &count);
52         ok1(count == 1);
53
54         dgraph_init_node(&n3);
55         ok1(dgraph_check(&n3, NULL) == &n3);
56         dgraph_add_edge(&n2, &n3);
57         ok1(dgraph_check(&n1, NULL) == &n1);
58         ok1(dgraph_check(&n2, NULL) == &n2);
59         ok1(dgraph_check(&n3, NULL) == &n3);
60         count = 0;
61         dgraph_traverse_from(&n1, count_nodes, &count);
62         ok1(count == 2);
63         count = 0;
64         dgraph_traverse_to(&n1, count_nodes, &count);
65         ok1(count == 0);
66         count = 0;
67         dgraph_traverse_from(&n2, count_nodes, &count);
68         ok1(count == 1);
69         count = 0;
70         dgraph_traverse_to(&n2, count_nodes, &count);
71         ok1(count == 1);
72         count = 0;
73         dgraph_traverse_from(&n3, count_nodes, &count);
74         ok1(count == 0);
75         count = 0;
76         dgraph_traverse_to(&n3, count_nodes, &count);
77         ok1(count == 2);
78
79         /* Check stopping traverse. */
80         count = 1;
81         dgraph_traverse_from(&n1, stop_traverse, &count);
82         ok1(count == 0);
83         count = 2;
84         dgraph_traverse_from(&n1, stop_traverse, &count);
85         ok1(count == 0);
86         count = 3;
87         dgraph_traverse_from(&n1, stop_traverse, &count);
88         ok1(count == 1);
89
90         dgraph_clear_node(&n1);
91         ok1(dgraph_check(&n1, NULL) == &n1);
92         ok1(dgraph_check(&n2, NULL) == &n2);
93         ok1(dgraph_check(&n3, NULL) == &n3);
94
95         count = 0;
96         dgraph_traverse_from(&n2, count_nodes, &count);
97         ok1(count == 1);
98         count = 0;
99         dgraph_traverse_to(&n2, count_nodes, &count);
100         ok1(count == 0);
101         count = 0;
102         dgraph_traverse_from(&n3, count_nodes, &count);
103         ok1(count == 0);
104         count = 0;
105         dgraph_traverse_to(&n3, count_nodes, &count);
106         ok1(count == 1);
107
108         ok1(dgraph_del_edge(&n2, &n3));
109         count = 0;
110         dgraph_traverse_from(&n2, count_nodes, &count);
111         ok1(count == 0);
112         count = 0;
113         dgraph_traverse_to(&n2, count_nodes, &count);
114         ok1(count == 0);
115         count = 0;
116         dgraph_traverse_from(&n3, count_nodes, &count);
117         ok1(count == 0);
118         count = 0;
119         dgraph_traverse_to(&n3, count_nodes, &count);
120         ok1(count == 0);
121         ok1(dgraph_check(&n1, NULL) == &n1);
122         ok1(dgraph_check(&n2, NULL) == &n2);
123         ok1(dgraph_check(&n3, NULL) == &n3);
124
125         ok1(!dgraph_del_edge(&n2, &n3));
126         dgraph_clear_node(&n2);
127
128         ok1(dgraph_check(&n1, NULL) == &n1);
129         ok1(dgraph_check(&n2, NULL) == &n2);
130         ok1(dgraph_check(&n3, NULL) == &n3);
131
132         /* This exits depending on whether all tests passed */
133         return exit_status();
134 }