#include "c_DepthFirstDirectedPaths.h" #include "c_Test.h" #include #include TEST_CASE(test_depth_first_directed_paths_complete) { c_Digraph_t g; /* 1. 初始化一个拥有 6 个顶点的有向图,使用默认分配器 */ c_err_t err = c_Digraph_Init(&g, 6, NULL); ASSERT_INT_EQ(C_SUCCESS, err); /* * 2. 构建测试图拓扑结构: * 0 -> 1 -> 2 -> 5 * 0 -> 3 -> 4 * 5 是孤立终点,4 是另一个路径的终点 */ c_Digraph_AddEdge(&g, 0, 1); c_Digraph_AddEdge(&g, 1, 2); c_Digraph_AddEdge(&g, 2, 5); c_Digraph_AddEdge(&g, 0, 3); c_Digraph_AddEdge(&g, 3, 4); /* 3. 初始化 DFS 路径搜索引擎(起点设为 0,并显式传入图的分配器) */ c_DepthFirstDirectedPaths_t paths; err = c_DepthFirstDirectedPaths_Init(&paths, &g, 0, &g.allocator); ASSERT_INT_EQ(C_SUCCESS, err); /* 4. 测试连通性判定 (HasPathTo) */ ASSERT_TRUE(c_DepthFirstDirectedPaths_HasPathTo(&paths, 5, g.V)); ASSERT_TRUE(c_DepthFirstDirectedPaths_HasPathTo(&paths, 4, g.V)); /* 假设存在一个没有被任何边指向的独立节点(比如把图扩大或测试不连通) */ /* 由于我们只初始化了 6 个节点,若查询范围外的节点或已知断开的路径: */ // 假设不存在 0 到某个未连接节点的情况,这里测试起点本身 ASSERT_TRUE(c_DepthFirstDirectedPaths_HasPathTo(&paths, 0, g.V)); /* 5. 提取并验证从 0 到 5 的完整路径 */ c_VertexIdList_t path_to_5; c_VertexIdList_Init(&path_to_5, 0, NULL); /* 这里映射的是 c_UIntArray_Init */ err = c_DepthFirstDirectedPaths_PathTo(&paths, 5, g.V, &path_to_5); ASSERT_INT_EQ(C_SUCCESS, err); /* 预期路径长度为 4: [0, 1, 2, 5] */ c_size_t path_size = (c_size_t)c_VertexIdList_GetSize(&path_to_5); ASSERT_LL_EQ(4, path_size); /* 使用最新的安全 c_VertexIdList_Get 接口验证路径节点顺序 */ c_uint_t val = 0; err = c_VertexIdList_Get(&path_to_5, 0, &val); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(0, val); err = c_VertexIdList_Get(&path_to_5, 1, &val); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(1, val); err = c_VertexIdList_Get(&path_to_5, 2, &val); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(2, val); err = c_VertexIdList_Get(&path_to_5, 3, &val); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_LL_EQ(5, val); /* 6. 清理所有分配的资源 */ c_VertexIdList_Destroy(&path_to_5); c_DepthFirstDirectedPaths_Destroy(&paths); c_Digraph_Destroy(&g); } int main(void) { /* 启动测试集 */ TEST_START(DepthFirstDirectedPaths_Tests); /* 运行具体的测试用例 */ RUN_TEST(test_depth_first_directed_paths_complete); /* 打印测试汇总报告 */ TEST_REPORT(); /* 返回测试状态代码码(0 代表全部通过,1 代表有失败) */ RETURN_TEST_STATUS; }