This commit is contained in:
2026-09-07 18:48:16 +08:00
parent 1564716731
commit c945aa211f
138 changed files with 10337 additions and 150 deletions
+71
View File
@@ -0,0 +1,71 @@
#include "c_Test.h"
#include "c_Digraph.h"
#include "c_DirectedCycle.h"
#include "c_VertexIdList.h"
TEST_CASE(test_directed_cycle_detection_with_vertex_list) {
c_Digraph_t g;
/* 1. Initialize a directed graph with 4 vertices using the default allocator */
c_err_t err = c_Digraph_Init(&g, 4, NULL);
ASSERT_INT_EQ(C_SUCCESS, err);
/*
* 2. Construct an explicit cyclical loop topology:
* 0 -> 1 -> 2 -> 0 (Cyclic core group)
* 2 -> 3 (Dead-end branch link)
*/
c_Digraph_AddEdge(&g, 0, 1);
c_Digraph_AddEdge(&g, 1, 2);
c_Digraph_AddEdge(&g, 2, 0); /* Creates back edge link closing the loop */
c_Digraph_AddEdge(&g, 2, 3);
/* 3. Initialize the cycle processing engine (explicitly passing graph's allocator) */
c_DirectedCycle_t detector;
err = c_DirectedCycle_Init(&detector, &g, &g.allocator);
ASSERT_INT_EQ(C_SUCCESS, err);
/* 4. Assert that a directed cycle was successfully intercepted */
ASSERT_TRUE(c_DirectedCycle_HasCycle(&detector));
/* 5. Extract the tracked cycle loop vertices into an external c_VertexIdList_t container */
c_VertexIdList_t extracted_cycle;
c_VertexIdList_Init(&extracted_cycle, 0, NULL);
err = c_DirectedCycle_GetCycle(&detector, &extracted_cycle);
ASSERT_INT_EQ(C_SUCCESS, err);
/* The cyclic sequence path count should be 4 elements long (e.g., 0 -> 1 -> 2 -> 0) */
c_size_t cycle_size = (c_size_t)c_VertexIdList_GetSize(&extracted_cycle);
ASSERT_LL_EQ(4, cycle_size);
/* 6. Verify the sequential order of vertices inside the cycle using safe c_VertexIdList_Get specs */
c_uint_t v0 = 0, v1 = 0, v2 = 0, v3 = 0;
ASSERT_INT_EQ(C_SUCCESS, c_VertexIdList_Get(&extracted_cycle, 0, &v0));
ASSERT_INT_EQ(C_SUCCESS, c_VertexIdList_Get(&extracted_cycle, 1, &v1));
ASSERT_INT_EQ(C_SUCCESS, c_VertexIdList_Get(&extracted_cycle, 2, &v2));
ASSERT_INT_EQ(C_SUCCESS, c_VertexIdList_Get(&extracted_cycle, 3, &v3));
/* A valid cycle validation check ensures the sequence loops cleanly back to its origin vertex */
ASSERT_LL_EQ(v0, v3);
/* 7. Clean up and reclaim all tracked buffer allocations safely */
c_VertexIdList_Destroy(&extracted_cycle);
c_DirectedCycle_Destroy(&detector);
c_Digraph_Destroy(&g);
}
int main(void) {
/* Bootstrapping the testing test suite environment */
TEST_START(DirectedCycle_Verification_Suite);
/* Run specified structural topology edge cases */
RUN_TEST(test_directed_cycle_detection_with_vertex_list);
/* Generate the total reporting tracking sheet metrics summary */
TEST_REPORT();
/* Unwind back with correct failure signal flag checking patterns */
RETURN_TEST_STATUS;
}