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

61 lines
2.5 KiB
C

#include "c_Test.h"
#include "c_EdgeWeightedDigraph.h"
#include "c_DijkstraAllPairsSP.h"
#include "c_VertexIdList.h"
TEST_CASE(test_dijkstra_all_pairs_sp_matrix) {
c_EdgeWeightedDigraph_t g;
c_err_t err = c_EdgeWeightedDigraph_Init(&g, 4, NULL);
ASSERT_INT_EQ(C_SUCCESS, err);
/* Construct an evaluation network topology:
* 0 -> 1 (Weight: 5.0) [Edge 0]
* 0 -> 2 (Weight: 1.0) [Edge 1]
* 2 -> 1 (Weight: 2.0) [Edge 2] -> Path 0->2->1 total is 3.0
* 1 -> 3 (Weight: 1.0) [Edge 3] -> Path 2->1->3 total is 3.0, Path 0->2->1->3 total is 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_DijkstraAllPairsSP_t all_pairs;
err = c_DijkstraAllPairsSP_Init(&all_pairs, &g, NULL);
ASSERT_INT_EQ(C_SUCCESS, err);
/* Verify random multi-source/multi-target query pairs */
ASSERT_TRUE(c_DijkstraAllPairsSP_HasPath(&all_pairs, 0, 3));
ASSERT_DOUBLE_EQ_MSG(4.0, c_DijkstraAllPairsSP_Dist(&all_pairs, 0, 3), "0 -> 3 shortest path wrong");
ASSERT_TRUE(c_DijkstraAllPairsSP_HasPath(&all_pairs, 2, 3));
ASSERT_DOUBLE_EQ_MSG(3.0, c_DijkstraAllPairsSP_Dist(&all_pairs, 2, 3), "2 -> 3 shortest path wrong");
/* Backward direction from 3 to 0 must remain unlinked/unreachable */
ASSERT_FALSE(c_DijkstraAllPairsSP_HasPath(&all_pairs, 3, 0));
ASSERT_DOUBLE_EQ_MSG(DBL_MAX, c_DijkstraAllPairsSP_Dist(&all_pairs, 3, 0), "Unreachable distance check failed");
/* Verify path extraction trace */
c_VertexIdList_t extracted_path;
c_VertexIdList_Init(&extracted_path, 0, 0);
err = c_DijkstraAllPairsSP_Path(&all_pairs, 0, 3, &extracted_path);
ASSERT_INT_EQ(C_SUCCESS, err);
ASSERT_LL_EQ(3, (c_size_t)c_VertexIdList_GetSize(&extracted_path));
c_uint_t edge_token = 0;
c_VertexIdList_Get(&extracted_path, 0, &edge_token); ASSERT_LL_EQ(1, edge_token); /* Edge 1 (0->2) */
c_VertexIdList_Get(&extracted_path, 1, &edge_token); ASSERT_LL_EQ(2, edge_token); /* Edge 2 (2->1) */
c_VertexIdList_Get(&extracted_path, 2, &edge_token); ASSERT_LL_EQ(3, edge_token); /* Edge 3 (1->3) */
c_VertexIdList_Destroy(&extracted_path);
c_DijkstraAllPairsSP_Destroy(&all_pairs);
c_EdgeWeightedDigraph_Destroy(&g);
}
int main(void) {
TEST_START(DijkstraAllPairsSP_Matrix_Suite);
RUN_TEST(test_dijkstra_all_pairs_sp_matrix);
TEST_REPORT();
RETURN_TEST_STATUS;
}