Files
cKit/Graph/c_AcyclicLP.t.c
T
2026-09-07 18:48:16 +08:00

56 lines
2.1 KiB
C

#include "c_Test.h"
#include "c_EdgeWeightedDigraph.h"
#include "c_AcyclicLP.h"
#include "c_VertexIdList.h"
TEST_CASE(test_acyclic_longest_paths_dag) {
c_EdgeWeightedDigraph_t g;
c_err_t err = c_EdgeWeightedDigraph_Init(&g, 4, NULL);
ASSERT_INT_EQ(C_SUCCESS, err);
/*
* Construct a valid DAG layout to measure longest path selection capabilities:
* 0 -> 1 (Weight: 2.0) [Edge 0]
* 0 -> 2 (Weight: 1.0) [Edge 1]
* 1 -> 2 (Weight: 5.0) [Edge 2] -> Path 0->1->2 total is 2.0 + 5.0 = 7.0 (Longer than direct 0->2 edge)
* 2 -> 3 (Weight: 3.0) [Edge 3] -> Path 0->1->2->3 total is 7.0 + 3.0 = 10.0
*/
c_EdgeWeightedDigraph_AddEdge(&g, 0, 1, 2.0);
c_EdgeWeightedDigraph_AddEdge(&g, 0, 2, 1.0);
c_EdgeWeightedDigraph_AddEdge(&g, 1, 2, 5.0);
c_EdgeWeightedDigraph_AddEdge(&g, 2, 3, 3.0);
c_AcyclicLP_t lp;
err = c_AcyclicLP_Init(&lp, &g, 0, &g.allocator);
ASSERT_INT_EQ(C_SUCCESS, err);
/* Verify longest path evaluation weights values */
ASSERT_TRUE(c_AcyclicLP_HasPathTo(&lp, 3));
ASSERT_DOUBLE_EQ_MSG(7.0, c_AcyclicLP_DistTo(&lp, 2), "Critical intermediate length calculation mismatch");
ASSERT_DOUBLE_EQ_MSG(10.0, c_AcyclicLP_DistTo(&lp, 3), "DAG critical terminal timeline calculation wrong");
/* Reconstruct edge sequence arrays elements */
c_VertexIdList_t edge_path;
c_VertexIdList_Init(&edge_path, 0, 0);
err = c_AcyclicLP_PathTo(&lp, 3, &edge_path);
ASSERT_INT_EQ(C_SUCCESS, err);
ASSERT_LL_EQ(3, (c_size_t)c_VertexIdList_GetSize(&edge_path));
c_uint_t e_id = 0;
c_VertexIdList_Get(&edge_path, 0, &e_id); ASSERT_LL_EQ(0, e_id); /* Edge 0 (0->1) */
c_VertexIdList_Get(&edge_path, 1, &e_id); ASSERT_LL_EQ(2, e_id); /* Edge 2 (1->2) */
c_VertexIdList_Get(&edge_path, 2, &e_id); ASSERT_LL_EQ(3, e_id); /* Edge 3 (2->3) */
c_VertexIdList_Destroy(&edge_path);
c_AcyclicLP_Destroy(&lp);
c_EdgeWeightedDigraph_Destroy(&g);
}
int main(void) {
TEST_START(AcyclicLP_DAG_Maximization_Suite);
RUN_TEST(test_acyclic_longest_paths_dag);
TEST_REPORT();
RETURN_TEST_STATUS;
}