添加add_edge单元测试

This commit is contained in:
建峰 2025-04-25 16:04:14 +08:00
parent 49bc8b0cc4
commit 628df4c1a0
2 changed files with 181 additions and 142 deletions

View File

@ -457,23 +457,27 @@ static void graph_print(struct _graph *self)
self->print_obj(cur->obj);
cur = cur->next;
}
printf("\n");
// printf("\n ");
// for(uint32_t i = 0; i < self->_capacity; i++)
// {
// self->print_obj((char *)self->_head->obj + i * self->_obj_size);
// }
// printf("\n");
// for(uint32_t i = 0; i < self->_capacity; i++)
// {
// self->print_obj((char *)self->_head->obj + i * self->_obj_size);
// for(uint32_t j = 0; j < self->_capacity; j++)
// {
// printf(" %2d ", self->_head->edge[i][j]);
// }
// printf("\n");
// }
printf("edge : \n");
cur = self->_head->next;
while (cur != NULL)
{
struct _graph_edge* edge = cur->edge;
while (edge != NULL)
{
struct _graph_node* target = (struct _graph_node*)edge->target;
printf("from ");
self->print_obj(cur->obj);
printf(" to ");
self->print_obj(target->obj);
printf(": %d \n", edge->weight);
edge = edge->next;
}
// self->print_obj(cur->obj);
cur = cur->next;
}
printf("\n");
printf("print done.\n");
}

View File

@ -69,6 +69,40 @@ void test_graph_add_vertex(void)
TEST_ASSERT_NULL(graph);
}
void test_graph_add_edge(void)
{
const int size = 10;
int data[10] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
};
int temp = 11;
uint32_t i = 0;
graph_t graph = graph_new(sizeof(int));
TEST_ASSERT_NOT_NULL(graph);
graph->compare = compare_num;
graph->print_obj = print_num;
// test add_vertex
for(i = 0; i < size; i++)
{
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
}
graph->print(graph);
// test add_edge
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 0));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 0));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 0));
graph->print(graph);
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
graph_free(&graph);
TEST_ASSERT_NULL(graph);
}
#if 0
void test_graph_print(void)
{
@ -116,6 +150,7 @@ void test_graph(void)
RUN_TEST(test_graph_new);
RUN_TEST(test_graph_add_vertex);
RUN_TEST(test_graph_add_edge);
// RUN_TEST(test_graph_print);
// RUN_TEST(test_graph_from_matrix);