Files
cKit/Graph/c_DepthFirstSearch.t.c
T

97 lines
3.2 KiB
C
Raw Normal View History

2026-09-07 18:48:16 +08:00
#include "c_DepthFirstSearch.h"
#include "c_Test.h"
#include <stdlib.h>
#include <stdio.h>
TEST_CASE(test_depth_first_search_object) {
c_Graph_t graph;
c_Graph_Init(&graph, 6, 0);
// Connected Cluster 1 (Triangle component + stem): 0-1, 1-2, 2-0, 2-3
c_Graph_AddEdge(&graph, 0, 1);
c_Graph_AddEdge(&graph, 1, 2);
c_Graph_AddEdge(&graph, 2, 0);
c_Graph_AddEdge(&graph, 2, 3);
// Connected Cluster 2 (Isolated pairing component): 4-5
c_Graph_AddEdge(&graph, 4, 5);
// Initialize the Search framework tracking from Source root 0
c_DepthFirstSearch_t dfs;
c_err_t err = c_DepthFirstSearch_Init(&dfs, &graph, 0, 0);
ASSERT_INT_EQ(C_SUCCESS, err);
// Connected metrics checking rules validation: Source 0 can discover 0, 1, 2, 3 -> count is 4
ASSERT_LL_EQ(4, c_DepthFirstSearch_Count(&dfs));
// Verify component connectivity mappings
ASSERT_TRUE(c_DepthFirstSearch_HasPathTo(&dfs, 3));
ASSERT_TRUE(c_DepthFirstSearch_HasPathTo(&dfs, 1));
// Nodes 4 and 5 are completely unreachable from Source 0
ASSERT_FALSE(c_DepthFirstSearch_HasPathTo(&dfs, 4));
ASSERT_FALSE(c_DepthFirstSearch_HasPathTo(&dfs, 5));
// Cleanup resources
c_DepthFirstSearch_Destroy(&dfs);
c_Graph_Destroy(&graph);
}
TEST_CASE(test_dfs_path_to_extraction) {
c_Graph_t graph;
c_Graph_Init(&graph, 5, 0);
// Create a snake graph path network with a tail: 0-1, 1-2, 2-3, 4 (isolated)
c_Graph_AddEdge(&graph, 0, 1);
c_Graph_AddEdge(&graph, 1, 2);
c_Graph_AddEdge(&graph, 2, 3);
// Initialize depth tracing calculation from root node zero (0)
c_DepthFirstSearch_t dfs;
c_DepthFirstSearch_Init(&dfs, &graph, 0, 0);
// Instantiate our destination identifier list vector
c_VertexIdList_t route;
c_VertexIdList_Init(&route, 4, 0);
// Test 1: Extract valid route trajectory tracking out to vertex 3
c_err_t err = c_DepthFirstSearch_PathTo(&dfs, 3, &route);
ASSERT_INT_EQ(C_SUCCESS, err);
ASSERT_LL_EQ(4, route.size); // Path length should be exactly 4 hops: [0, 1, 2, 3]
// Verify correct forward sequence mapping layout elements
c_uint_t node;
c_VertexIdList_Get(&route, 0, &node); ASSERT_LL_EQ(0, node);
c_VertexIdList_Get(&route, 1, &node); ASSERT_LL_EQ(1, node);
c_VertexIdList_Get(&route, 2, &node); ASSERT_LL_EQ(2, node);
c_VertexIdList_Get(&route, 3, &node); ASSERT_LL_EQ(3, node);
// Test 2: Unreachable destination error boundary isolation checks
c_VertexIdList_t bad_route;
c_VertexIdList_Init(&bad_route, 4, 0);
c_err_t fail_err = c_DepthFirstSearch_PathTo(&dfs, 4, &bad_route);
ASSERT_INT_EQ_MSG(C_ERR_FAIL, fail_err, "Isolated un-routed paths must return failure status mapping");
ASSERT_LL_EQ(0, bad_route.size);
// Clean structural tracker allocations
c_VertexIdList_Destroy(&route);
c_VertexIdList_Destroy(&bad_route);
c_DepthFirstSearch_Destroy(&dfs);
c_Graph_Destroy(&graph);
}
int main(int argc, char** argv){
TEST_START(Component Tests);
// Execution list configurations
RUN_TEST(test_depth_first_search_object);
RUN_TEST(test_dfs_path_to_extraction);
TEST_REPORT();
RETURN_TEST_STATUS;
}