5d72104bb933676e9067ac26cfb51ba6933988ed
[tinc] / test / unit / test_graph.c
1 #include "unittest.h"
2 #include "../../src/connection.h"
3 #include "../../src/node.h"
4 #include "../../src/xalloc.h"
5
6 extern void sssp_bfs(void);
7
8 static void connect_nodes(node_t *from, node_t *to, int weight) {
9         edge_t *direct = new_edge();
10         direct->from = from;
11         direct->to = to;
12         direct->weight = weight;
13         edge_add(direct);
14
15         edge_t *reverse = new_edge();
16         reverse->from = to;
17         reverse->to = from;
18         reverse->weight = weight;
19         edge_add(reverse);
20 }
21
22 static node_t *make_node(const char *name) {
23         node_t *node = new_node();
24         node->name = xstrdup(name);
25         node->status.reachable = true;
26         node_add(node);
27         return node;
28 }
29
30 static void test_sssp_bfs(void **state) {
31         (void)state;
32
33         node_t *mars = make_node("mars");
34         node_t *saturn = make_node("saturn");
35         node_t *neptune = make_node("neptune");
36
37         //          50            1000
38         // myself ------ mars ------------- neptune
39         //      \                             /
40         //       ----------------- saturn ----
41         //              500                10
42
43         // Upper route
44         connect_nodes(myself, mars, 50);
45         connect_nodes(mars, neptune, 1000);
46
47         // Lower route
48         connect_nodes(myself, saturn, 500);
49         connect_nodes(saturn, neptune, 10);
50
51         sssp_bfs();
52
53         assert_true(mars->status.visited);
54         assert_true(saturn->status.visited);
55         assert_true(neptune->status.visited);
56
57         assert_false(mars->status.indirect);
58         assert_false(saturn->status.indirect);
59         assert_false(neptune->status.indirect);
60
61         assert_int_equal(1, mars->distance);
62         assert_int_equal(1, saturn->distance);
63         assert_int_equal(2, neptune->distance);
64
65         assert_ptr_equal(mars, mars->nexthop);
66         assert_ptr_equal(saturn, saturn->nexthop);
67         assert_ptr_equal(saturn, neptune->nexthop);
68
69         assert_ptr_equal(lookup_edge(myself, mars), mars->prevedge);
70         assert_ptr_equal(lookup_edge(myself, saturn), saturn->prevedge);
71         assert_ptr_equal(lookup_edge(saturn, neptune), neptune->prevedge);
72
73         assert_ptr_equal(mars, mars->via);
74         assert_ptr_equal(saturn, saturn->via);
75         assert_ptr_equal(neptune, neptune->via);
76 }
77
78 static int setup(void **state) {
79         (void)state;
80         myself = new_node();
81         myself->name = xstrdup("myself");
82         return 0;
83 }
84
85 static int teardown(void **state) {
86         (void)state;
87         free_node(myself);
88         exit_nodes();
89         exit_edges();
90         return 0;
91 }
92
93 int main(void) {
94         const struct CMUnitTest tests[] = {
95                 cmocka_unit_test_setup_teardown(test_sssp_bfs, setup, teardown),
96         };
97         return cmocka_run_group_tests(tests, NULL, NULL);
98 }