#include "c_Digraph.h" #include "c_Test.h" #include #include /* 使用编写的 TEST_CASE 声明测试函数 */ TEST_CASE(test_digraph_basic_and_degree) { c_Digraph_t g; c_err_t err = c_Digraph_Init(&g, 4, NULL); /* 4个顶点的图 */ ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_INT_EQ(4, c_Digraph_GetV(&g)); ASSERT_INT_EQ(0, c_Digraph_GetE(&g)); /* 添加有向边: 0->1, 0->2, 1->2 */ c_Digraph_AddEdge(&g, 0, 1); c_Digraph_AddEdge(&g, 0, 2); c_Digraph_AddEdge(&g, 1, 2); ASSERT_INT_EQ(3, c_Digraph_GetE(&g)); /* 测试度数计算:0号节点的出度应为2,入度应为0 */ ASSERT_INT_EQ(2, c_Digraph_GetDegree(&g, 0, C_TRUE)); /* 出度 */ ASSERT_INT_EQ(0, c_Digraph_GetDegree(&g, 0, C_FALSE)); /* 入度 */ /* 测试度数计算:2号节点的出度应为0,入度应为2 (由0和1指向它) */ ASSERT_INT_EQ(0, c_Digraph_GetDegree(&g, 2, C_TRUE)); ASSERT_INT_EQ(2, c_Digraph_GetDegree(&g, 2, C_FALSE)); c_Digraph_Destroy(&g); } TEST_CASE(test_digraph_reverse) { c_Digraph_t g; c_Digraph_Init(&g, 3, NULL); c_Digraph_AddEdge(&g, 0, 1); /* 0 -> 1 */ c_Digraph_AddEdge(&g, 1, 2); /* 1 -> 2 */ c_Digraph_t rev; c_err_t err = c_Digraph_Reverse(&g, &rev); ASSERT_INT_EQ(C_SUCCESS, err); /* 反向图验证:原图 0->1,反向图应为 1->0 ;即0号节点入度应变为1,出度变为0 */ ASSERT_INT_EQ(0, c_Digraph_GetDegree(&rev, 0, C_TRUE)); ASSERT_INT_EQ(1, c_Digraph_GetDegree(&rev, 0, C_FALSE)); /* 反向图 2->1 ;即2号节点出度应变为1 */ ASSERT_INT_EQ(1, c_Digraph_GetDegree(&rev, 2, C_TRUE)); c_Digraph_Destroy(&g); c_Digraph_Destroy(&rev); } TEST_CASE(test_unweighted_digraph_edge_existence) { c_Digraph_t g; c_err_t err = c_Digraph_Init(&g, 3, NULL); ASSERT_INT_EQ(C_SUCCESS, err); /* Add edge 0 -> 1 */ c_Digraph_AddEdge(&g, 0, 1); /* 1. Verify existence of the added edge */ ASSERT_TRUE(c_Digraph_HasEdge(&g, 0, 1)); /* 2. Verify non-existence of reverse edge (1 -> 0) since it's a directed graph */ ASSERT_FALSE(c_Digraph_HasEdge(&g, 1, 0)); /* 3. Verify non-existence of an unadded edge (0 -> 2) */ ASSERT_FALSE(c_Digraph_HasEdge(&g, 0, 2)); /* 4. Verify that out-of-bounds inputs safely return C_FALSE instead of crashing */ ASSERT_FALSE(c_Digraph_HasEdge(&g, 0, 99)); ASSERT_FALSE(c_Digraph_HasEdge(&g, 99, 1)); c_Digraph_Destroy(&g); } TEST_CASE(test_unweighted_digraph_get_edge_index) { c_Digraph_t g; c_err_t err = c_Digraph_Init(&g, 4, NULL); ASSERT_INT_EQ(C_SUCCESS, err); /* Construct directed edges out from vertex 0: * 0 -> 2 (inserted first, expected index 0) * 0 -> 3 (inserted second, expected index 1) */ c_Digraph_AddEdge(&g, 0, 2); c_Digraph_AddEdge(&g, 0, 3); c_size_t resolved_idx = 0; /* 1. Verify retrieval of the first inserted edge */ err = c_Digraph_GetEdge(&g, 0, 2, &resolved_idx); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(0, resolved_idx); /* 2. Verify retrieval of the second inserted edge */ err = c_Digraph_GetEdge(&g, 0, 3, &resolved_idx); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(1, resolved_idx); /* 3. Verify that non-existent edges return C_ERR_NOTFOUND */ err = c_Digraph_GetEdge(&g, 0, 1, &resolved_idx); ASSERT_INT_EQ(C_ERR_NOTFOUND, err); /* 4. Verify that reverse paths in directed graphs are correctly treated as non-existent */ err = c_Digraph_GetEdge(&g, 2, 0, &resolved_idx); ASSERT_INT_EQ(C_ERR_NOTFOUND, err); /* 5. Verify that out-of-bounds parameters are safely caught */ err = c_Digraph_GetEdge(&g, 0, 99, &resolved_idx); ASSERT_INT_EQ(C_ERR_OUTOFBOUND, err); err = c_Digraph_GetEdge(&g, 99, 2, &resolved_idx); ASSERT_INT_EQ(C_ERR_OUTOFBOUND, err); c_Digraph_Destroy(&g); } int main(void) { TEST_START(Digraph_DataStructure_Tests); /* 运行测试用例 */ RUN_TEST(test_digraph_basic_and_degree); RUN_TEST(test_digraph_reverse); RUN_TEST(test_unweighted_digraph_edge_existence); RUN_TEST(test_unweighted_digraph_get_edge_index); /* 输出总报告 */ TEST_REPORT(); /* 阻断并返回状态代码 */ RETURN_TEST_STATUS; }