#include "c_Test.h" #include "c_EdgeWeightedDigraph.h" #include "c_DijkstraSP.h" TEST_CASE(test_dijkstra_path_extraction) { c_EdgeWeightedDigraph_t g; c_err_t err = c_EdgeWeightedDigraph_Init(&g, 4, NULL); ASSERT_INT_EQ(C_SUCCESS, err); /* Setup paths: * 0 -> 1 (Weight: 5.0) [Edge ID: 0] * 0 -> 2 (Weight: 1.0) [Edge ID: 1] * 2 -> 1 (Weight: 2.0) [Edge ID: 2] * 1 -> 3 (Weight: 1.0) [Edge ID: 3] * True Shortest path from 0 to 3 is: 0 -> 2 -> 1 -> 3 (Total Weight = 4.0) */ c_EdgeWeightedDigraph_AddEdge(&g, 0, 1, 5.0); c_EdgeWeightedDigraph_AddEdge(&g, 0, 2, 1.0); c_EdgeWeightedDigraph_AddEdge(&g, 2, 1, 2.0); c_EdgeWeightedDigraph_AddEdge(&g, 1, 3, 1.0); c_DijkstraSP_t sp; err = c_DijkstraSP_Init(&sp, &g, 0, &g.allocator); ASSERT_INT_EQ(C_SUCCESS, err); ASSERT_TRUE(c_DijkstraSP_HasPathTo(&sp, 3)); ASSERT_DOUBLE_EQ_MSG(4.0, c_DijkstraSP_DistTo(&sp, 3), "Shortest distance mapping check failed"); /* Create list to intercept path edge tokens */ c_VertexIdList_t edge_path; c_VertexIdList_Init(&edge_path, 0, 0); /* Run the re-implemented signature format */ err = c_DijkstraSP_PathTo(&sp, 3, &edge_path); ASSERT_INT_EQ(C_SUCCESS, err); /* Expected edge sequence size should be 3 components long */ c_size_t path_len = (c_size_t)c_VertexIdList_GetSize(&edge_path); ASSERT_LL_EQ(3, path_len); /* Extract and verify raw edge pool identity markers sequentially */ c_uint_t e_id = 0; c_VertexIdList_Get(&edge_path, 0, &e_id); ASSERT_LL_EQ(1, e_id); /* Edge ID 1 (0->2) */ c_VertexIdList_Get(&edge_path, 1, &e_id); ASSERT_LL_EQ(2, e_id); /* Edge ID 2 (2->1) */ c_VertexIdList_Get(&edge_path, 2, &e_id); ASSERT_LL_EQ(3, e_id); /* Edge ID 3 (1->3) */ c_VertexIdList_Destroy(&edge_path); c_DijkstraSP_Destroy(&sp); c_EdgeWeightedDigraph_Destroy(&g); } int main(void) { TEST_START(DijkstraSP_ShortestPath_Suite); RUN_TEST(test_dijkstra_path_extraction); TEST_REPORT(); RETURN_TEST_STATUS; }