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
+55
View File
@@ -0,0 +1,55 @@
#include "c_Test.h"
#include "c_EdgeWeightedDigraph.h"
#include "c_AcyclicSP.h"
#include "c_VertexIdList.h"
TEST_CASE(test_acyclic_shortest_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 containing negative weights:
* 0 -> 1 (Weight: 2.0) [Edge 0]
* 0 -> 2 (Weight: 5.0) [Edge 1]
* 1 -> 2 (Weight: -4.0) [Edge 2] -> Path 0->1->2 total is 2.0 + (-4.0) = -2.0
* 2 -> 3 (Weight: 1.0) [Edge 3] -> Path 0->1->2->3 total is -2.0 + 1.0 = -1.0
*/
c_EdgeWeightedDigraph_AddEdge(&g, 0, 1, 2.0);
c_EdgeWeightedDigraph_AddEdge(&g, 0, 2, 5.0);
c_EdgeWeightedDigraph_AddEdge(&g, 1, 2, -4.0);
c_EdgeWeightedDigraph_AddEdge(&g, 2, 3, 1.0);
c_AcyclicSP_t sp;
err = c_AcyclicSP_Init(&sp, &g, 0, &g.allocator);
ASSERT_INT_EQ(C_SUCCESS, err);
/* Distance mappings verification */
ASSERT_TRUE(c_AcyclicSP_HasPathTo(&sp, 3));
ASSERT_DOUBLE_EQ_MSG(-2.0, c_AcyclicSP_DistTo(&sp, 2), "Negative distance processing check failed");
ASSERT_DOUBLE_EQ_MSG(-1.0, c_AcyclicSP_DistTo(&sp, 3), "DAG terminal target computation failed");
/* Reconstruct edge collection paths sequence */
c_VertexIdList_t edge_path;
c_VertexIdList_Init(&edge_path, 0, 0 );
err = c_AcyclicSP_PathTo(&sp, 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_AcyclicSP_Destroy(&sp);
c_EdgeWeightedDigraph_Destroy(&g);
}
int main(void) {
TEST_START(AcyclicSP_DAG_Optimization_Suite);
RUN_TEST(test_acyclic_shortest_paths_dag);
TEST_REPORT();
RETURN_TEST_STATUS;
}