2 #include "../../src/connection.h"
3 #include "../../src/node.h"
4 #include "../../src/xalloc.h"
6 extern void sssp_bfs(void);
8 static void connect_nodes(node_t *from, node_t *to, int weight) {
9 edge_t *direct = new_edge();
12 direct->weight = weight;
15 edge_t *reverse = new_edge();
18 reverse->weight = weight;
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;
30 static void test_sssp_bfs(void **state) {
33 node_t *mars = make_node("mars");
34 node_t *saturn = make_node("saturn");
35 node_t *neptune = make_node("neptune");
38 // myself ------ mars ------------- neptune
40 // ----------------- saturn ----
44 connect_nodes(myself, mars, 50);
45 connect_nodes(mars, neptune, 1000);
48 connect_nodes(myself, saturn, 500);
49 connect_nodes(saturn, neptune, 10);
53 assert_true(mars->status.visited);
54 assert_true(saturn->status.visited);
55 assert_true(neptune->status.visited);
57 assert_false(mars->status.indirect);
58 assert_false(saturn->status.indirect);
59 assert_false(neptune->status.indirect);
61 assert_int_equal(1, mars->distance);
62 assert_int_equal(1, saturn->distance);
63 assert_int_equal(2, neptune->distance);
65 assert_ptr_equal(mars, mars->nexthop);
66 assert_ptr_equal(saturn, saturn->nexthop);
67 assert_ptr_equal(saturn, neptune->nexthop);
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);
73 assert_ptr_equal(mars, mars->via);
74 assert_ptr_equal(saturn, saturn->via);
75 assert_ptr_equal(neptune, neptune->via);
78 static int setup(void **state) {
81 myself->name = xstrdup("myself");
85 static int teardown(void **state) {
94 const struct CMUnitTest tests[] = {
95 cmocka_unit_test_setup_teardown(test_sssp_bfs, setup, teardown),
97 return cmocka_run_group_tests(tests, NULL, NULL);