Graph
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#include "c_Test.h"
|
||||
#include "c_EdgeWeightedDigraph.h"
|
||||
#include "c_EdgeWeightedDirectedCycle.h"
|
||||
#include "c_VertexIdList.h"
|
||||
|
||||
TEST_CASE(test_edge_weighted_directed_cycle_detection) {
|
||||
c_EdgeWeightedDigraph_t g;
|
||||
c_err_t err = c_EdgeWeightedDigraph_Init(&g, 3, NULL);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* Construct a simple cyclic network loop topology:
|
||||
* 0 -> 1 (Weight: 1.5) [Edge ID: 0]
|
||||
* 1 -> 2 (Weight: 2.3) [Edge ID: 1]
|
||||
* 2 -> 0 (Weight: 0.8) [Edge ID: 2] -> Closes the cycle
|
||||
*/
|
||||
c_EdgeWeightedDigraph_AddEdge(&g, 0, 1, 1.5);
|
||||
c_EdgeWeightedDigraph_AddEdge(&g, 1, 2, 2.3);
|
||||
c_EdgeWeightedDigraph_AddEdge(&g, 2, 0, 0.8);
|
||||
|
||||
c_EdgeWeightedDirectedCycle_t detector;
|
||||
err = c_EdgeWeightedDirectedCycle_Init(&detector, &g, &g.allocator);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* Assert that a cycle is captured */
|
||||
ASSERT_TRUE(c_EdgeWeightedDirectedCycle_HasCycle(&detector));
|
||||
|
||||
c_VertexIdList_t extracted;
|
||||
c_VertexIdList_Init(&extracted, 0, 0);
|
||||
|
||||
err = c_EdgeWeightedDirectedCycle_GetCycle(&detector, &extracted);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* The loop cycle size must match exactly 3 edge elements */
|
||||
c_size_t cycle_size = (c_size_t)c_VertexIdList_GetSize(&extracted);
|
||||
ASSERT_LL_EQ(3, cycle_size);
|
||||
|
||||
/* Verify the extracted sequential edge tokens order matches forward travel steps */
|
||||
c_uint_t e0 = 0, e1 = 0, e2 = 0;
|
||||
c_VertexIdList_Get(&extracted, 0, &e0); ASSERT_LL_EQ(0, e0); /* Edge 0 (0->1) */
|
||||
c_VertexIdList_Get(&extracted, 1, &e1); ASSERT_LL_EQ(1, e1); /* Edge 1 (1->2) */
|
||||
c_VertexIdList_Get(&extracted, 2, &e2); ASSERT_LL_EQ(2, e2); /* Edge 2 (2->0) */
|
||||
|
||||
c_VertexIdList_Destroy(&extracted);
|
||||
c_EdgeWeightedDirectedCycle_Destroy(&detector);
|
||||
c_EdgeWeightedDigraph_Destroy(&g);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
TEST_START(EdgeWeightedDirectedCycle_Suite);
|
||||
RUN_TEST(test_edge_weighted_directed_cycle_detection);
|
||||
TEST_REPORT();
|
||||
RETURN_TEST_STATUS;
|
||||
}
|
||||
Reference in New Issue
Block a user