Graph
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include "c_GraphDFS.h"
|
||||
#include "c_Test.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Struct context to keep track of discovered traversal orders inside the callback
|
||||
typedef struct {
|
||||
c_size_t order_buffer[10];
|
||||
c_size_t count;
|
||||
} DFS_Tracker;
|
||||
|
||||
// Dummy callback processor mapping discovery steps
|
||||
static c_bool_t mock_dfs_visitor(c_size_t vertex, void* context) {
|
||||
DFS_Tracker* tracker = (DFS_Tracker*)context;
|
||||
if (tracker->count < 10) {
|
||||
tracker->order_buffer[tracker->count++] = vertex;
|
||||
}
|
||||
return C_TRUE; // Keep traversing
|
||||
}
|
||||
|
||||
TEST_CASE(test_graph_dfs_traversal) {
|
||||
c_Graph_t graph;
|
||||
c_Graph_Init(&graph, 4, 0);
|
||||
|
||||
// Build a simple line-graph with a branch: 0-1, 1-2, 1-3
|
||||
c_Graph_AddEdge(&graph, 0, 1);
|
||||
c_Graph_AddEdge(&graph, 1, 2);
|
||||
c_Graph_AddEdge(&graph, 1, 3);
|
||||
|
||||
DFS_Tracker tracker = { .count = 0 };
|
||||
|
||||
// Execute Depth First Scan starting from Root node 0
|
||||
c_err_t err = c_Graph_DFS(&graph, 0, mock_dfs_visitor, &tracker, 0);
|
||||
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
ASSERT_LL_EQ(4, tracker.count); // All nodes should be found
|
||||
|
||||
// The first node discovered must be the start node (0)
|
||||
ASSERT_LL_EQ(0, tracker.order_buffer[0]);
|
||||
// The second node discovered must be 1, since it's 0's only neighbor
|
||||
ASSERT_LL_EQ(1, tracker.order_buffer[1]);
|
||||
|
||||
// Nodes 2 and 3 are structural branches out of 1.
|
||||
// DFS will traverse one completely before popping back to process the other.
|
||||
// Hence, positions 2 and 3 must contain some permutation of nodes 2 and 3.
|
||||
c_size_t pos2 = tracker.order_buffer[2];
|
||||
c_size_t pos3 = tracker.order_buffer[3];
|
||||
ASSERT_TRUE((pos2 == 2 && pos3 == 3) || (pos2 == 3 && pos3 == 2));
|
||||
|
||||
c_Graph_Destroy(&graph);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
|
||||
TEST_START(Unit Tests);
|
||||
|
||||
// 运行普通无环境要求的用例
|
||||
RUN_TEST(test_graph_dfs_traversal);
|
||||
|
||||
// 打印最终统计报告
|
||||
TEST_REPORT();
|
||||
|
||||
RETURN_TEST_STATUS;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user