#include #include #include #include #include /* Test : graph creation + free */ START_TEST(test_creation) { graph_t *g = graph_new(); fail_if(g == NULL, "pointer while creating graph"); fail_if(g->node_count != 0, "strange node count"); fail_if(g->edge_count != 0, "strange node count"); graph_free(g); } END_TEST /* Test : graph_node_add */ START_TEST(test_node_add) { graph_t *g = graph_new(); int a = 2; node_t *n = NULL; n = graph_node_add(g, &a); fail_unless(g->node_count == 1, "Wrong node count"); fail_unless(g->edge_count == 0, "Wrong edge count"); /* Check node */ fail_if(n == NULL, "New node has null pointer"); fail_if(memcmp(n->data, &a, sizeof(int)), "Data is different"); graph_free(g); } END_TEST /* Test : graph_edge_add */ START_TEST(test_edge_add) { graph_t *g = graph_new(); int a=3, b=10, c=1; node_t *n1 = graph_node_add(g, &a); node_t *n2 = graph_node_add(g, &b); edge_t *e = graph_edge_new(n1, n2, &c); fail_unless(e != NULL, "Null edge pointer"); graph_edge_add(g, e); /* Check graph consistency */ fail_unless(g->node_count == 2, "Wrong node count"); fail_unless(g->edge_count == 1, "Wrong edge count"); /* Check edge */ fail_if(e == NULL, "New edge has null pointer"); fail_if(memcmp(e->data, &c, sizeof(int)), "Data is different"); fail_if(e->n1 != n1, "Connection to first node failed"); fail_if(e->n2 != n2, "Connection to second node failed"); graph_free(g); } END_TEST int count; void test_visit_node(graph_t *g, node_t *n) { count++; } void test_visit_edge(graph_t *g, edge_t *e) { count++; } /* Test : graph_node_visit AND graph_edge_visit */ START_TEST(test_visit) { graph_t *g = graph_new(); int a=2, b=3, c=1; node_t *n1 = graph_node_add(g, &a); node_t *n2 = graph_node_add(g, &b); edge_t *e = graph_edge_new(n1, n2, &c); graph_edge_add(g, e); /* Test node visitation */ count = 0; graph_node_visit(g, test_visit_node); fail_unless(count == g->node_count, "Doesn't visit every node (we visited %d/%d nodes)", count, g->node_count); /* Test edge visitation */ count = 0; graph_edge_visit(g, test_visit_edge); fail_unless(count == g->edge_count, "Doesn't visit every edge (we visited %d/%d edges)", count, g->edge_count); graph_free(g); } END_TEST int node_comparator(node_t *n, void *data) { int node_val = *(int*)(n->data); int val = *(int*)data; count++; return node_val == val; } /* Test : graph_node_find */ START_TEST(test_node_find) { graph_t *g = graph_new(); int a=2, b=3, c=1; node_t *n1 = graph_node_add(g, &a); node_t *n2 = graph_node_add(g, &b); edge_t *e = graph_edge_new(n1, n2, &c); graph_edge_add(g, e); /* Test find node */ count = 0; int want; node_t *found; /* Existing node */ want = 2; found = graph_node_find(g, &want, node_comparator); fail_unless(found != NULL, "Should have found this node"); fail_unless(want == *(int*)found->data, "Found wrong node"); /* Non-existing node */ count = 0; want = 42; found = NULL; found = graph_node_find(g, &want, node_comparator); fail_if(found != NULL, "Should NOT have found this node"); fail_unless(count == g->node_count, "Doesn't explore every node"); } END_TEST /* Test : graph_edge_find */ int edge_comparator(edge_t *n, void *data) { int edge_val = *(int*)(n->data); int val = *(int*)data; count++; return edge_val == val; } START_TEST(test_edge_find) { graph_t *g = graph_new(); int a=2, b=3, c=1; node_t *n1 = graph_node_add(g, &a); node_t *n2 = graph_node_add(g, &b); edge_t *e = graph_edge_new(n1, n2, &c); graph_edge_add(g, e); /* Test find node */ count = 0; int want; edge_t *found; /* Existing node */ want = 1; found = graph_edge_find(g, &want, edge_comparator); fail_unless(found != NULL, "Should have found this edge"); fail_unless(want == *(int*)found->data, "Found wrong edge"); /* Non-existing node */ count = 0; want = 42; found = NULL; found = graph_edge_find(g, &want, edge_comparator); fail_if(found != NULL, "Should NOT have found this edge"); fail_unless(count == g->edge_count, "Doesn't explore every node"); } END_TEST /* Test suite for "Check" */ Suite *graph_suite() { Suite *s = suite_create("graph"); TCase *tc_core = tcase_create("Core"); /* Add tests here */ tcase_add_test(tc_core, test_creation); tcase_add_test(tc_core, test_node_add); tcase_add_test(tc_core, test_edge_add); tcase_add_test(tc_core, test_visit); tcase_add_test(tc_core, test_node_find); tcase_add_test(tc_core, test_edge_find); suite_add_tcase(s, tc_core); } int main() { int number_failed; Suite *s = graph_suite (); SRunner *sr = srunner_create (s); srunner_run_all (sr, CK_NORMAL); number_failed = srunner_ntests_failed (sr); srunner_free (sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }