Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a unit test for sssp_bfs() #448

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/unit/test_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ static node_t *make_node(const char *name) {
return node;
}

static void test_sssp_bfs_2(void **state) {
(void)state;

node_t *mars = make_node("mars");
node_t *saturn = make_node("saturn");
node_t *neptune = make_node("neptune");

// 1000 500
// myself ---------- mars ------------- neptune
// \ /
// ------- saturn --------------
// 50 501

// Upper route
connect_nodes(myself, mars, 1000);
connect_nodes(mars, neptune, 500);

// Lower route
connect_nodes(myself, saturn, 50);
connect_nodes(saturn, neptune, 501);

sssp_bfs();

assert_true(mars->status.visited);
assert_true(saturn->status.visited);
assert_true(neptune->status.visited);

assert_false(mars->status.indirect);
assert_false(saturn->status.indirect);
assert_false(neptune->status.indirect);

assert_int_equal(1, mars->distance);
assert_int_equal(1, saturn->distance);
assert_int_equal(2, neptune->distance);

assert_ptr_equal(mars, mars->nexthop);
assert_ptr_equal(saturn, saturn->nexthop);
assert_ptr_equal(saturn, neptune->nexthop);

assert_ptr_equal(lookup_edge(myself, mars), mars->prevedge);
assert_ptr_equal(lookup_edge(myself, saturn), saturn->prevedge);
assert_ptr_equal(lookup_edge(saturn, neptune), neptune->prevedge);

assert_ptr_equal(mars, mars->via);
assert_ptr_equal(saturn, saturn->via);
assert_ptr_equal(neptune, neptune->via);
}

static void test_sssp_bfs(void **state) {
(void)state;

Expand Down Expand Up @@ -91,6 +139,7 @@ static int teardown(void **state) {
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_sssp_bfs, setup, teardown),
cmocka_unit_test_setup_teardown(test_sssp_bfs_2, setup, teardown)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading