Graph
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include "c_Test.h"
|
||||
#include "c_EdgeWeightedDigraph.h"
|
||||
|
||||
TEST_CASE(test_edge_weighted_digraph_plumbing) {
|
||||
c_EdgeWeightedDigraph_t graph;
|
||||
c_err_t err = c_EdgeWeightedDigraph_Init(&graph, 3, NULL);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
ASSERT_LL_EQ(3, c_EdgeWeightedDigraph_GetV(&graph));
|
||||
ASSERT_LL_EQ(0, c_EdgeWeightedDigraph_GetE(&graph));
|
||||
|
||||
/* Inject directed edges: 0 -> 1 (W: 1.5) and 0 -> 2 (W: 4.2) */
|
||||
err = c_EdgeWeightedDigraph_AddEdge(&graph, 0, 1, 1.5); ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
err = c_EdgeWeightedDigraph_AddEdge(&graph, 0, 2, 4.2); ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
ASSERT_LL_EQ(2, c_EdgeWeightedDigraph_GetE(&graph));
|
||||
|
||||
/* Validate O(1) degree cache outputs */
|
||||
ASSERT_LL_EQ(2, c_EdgeWeightedDigraph_GetOutDegree(&graph, 0));
|
||||
ASSERT_LL_EQ(0, c_EdgeWeightedDigraph_GetInDegree(&graph, 0));
|
||||
ASSERT_LL_EQ(1, c_EdgeWeightedDigraph_GetInDegree(&graph, 1));
|
||||
ASSERT_LL_EQ(1, c_EdgeWeightedDigraph_GetInDegree(&graph, 2));
|
||||
|
||||
/* Inspect outbound connections from vertex index 0 */
|
||||
c_AdjList_t* zero_adj = &graph.adj_list[0];
|
||||
ASSERT_INT_EQ(2, c_AdjList_GetSize(zero_adj));
|
||||
|
||||
c_uint_t first_edge_id = 0;
|
||||
err = c_AdjList_Get(zero_adj, 0, &first_edge_id);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* Map back to the edge pool item row to verify its integrity */
|
||||
c_DirectedEdge_t* edge1 = &graph.edges_pool[(c_size_t)first_edge_id];
|
||||
ASSERT_LL_EQ(0, c_DirectedEdge_From(edge1));
|
||||
ASSERT_LL_EQ(1, c_DirectedEdge_To(edge1));
|
||||
ASSERT_DOUBLE_EQ_MSG(1.5, c_DirectedEdge_Weight(edge1), "Directed weight match failed");
|
||||
|
||||
c_EdgeWeightedDigraph_Destroy(&graph);
|
||||
}
|
||||
|
||||
TEST_CASE(test_edge_weighted_digraph_soft_remove) {
|
||||
c_EdgeWeightedDigraph_t graph;
|
||||
c_err_t err = c_EdgeWeightedDigraph_Init(&graph, 3, NULL);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* Inject directed edges: 0 -> 1 [Edge ID: 0] and 0 -> 2 [Edge ID: 1] */
|
||||
c_EdgeWeightedDigraph_AddEdge(&graph, 0, 1, 1.5);
|
||||
c_EdgeWeightedDigraph_AddEdge(&graph, 0, 2, 4.2);
|
||||
|
||||
ASSERT_LL_EQ(2, c_EdgeWeightedDigraph_GetE(&graph));
|
||||
ASSERT_LL_EQ(1, c_EdgeWeightedDigraph_GetInDegree(&graph, 1));
|
||||
ASSERT_LL_EQ(2, c_EdgeWeightedDigraph_GetOutDegree(&graph, 0));
|
||||
|
||||
/* Remove edge 0 (0 -> 1) */
|
||||
err = c_EdgeWeightedDigraph_RemoveEdge(&graph, 0);
|
||||
ASSERT_INT_EQ(C_SUCCESS, err);
|
||||
|
||||
/* Verify that the adjacency list and degree caches accurately reflected the change */
|
||||
ASSERT_LL_EQ(1, c_EdgeWeightedDigraph_GetOutDegree(&graph, 0));
|
||||
ASSERT_LL_EQ(0, c_EdgeWeightedDigraph_GetInDegree(&graph, 1)); /* Should drop down to 0 */
|
||||
ASSERT_LL_EQ(1, c_EdgeWeightedDigraph_GetInDegree(&graph, 2)); /* Edge 1 remains intact */
|
||||
|
||||
/* Ensure bounds boundary checks prevent out-of-bounds corruption */
|
||||
err = c_EdgeWeightedDigraph_RemoveEdge(&graph, 99);
|
||||
ASSERT_INT_EQ(C_ERR_OUTOFBOUND, err);
|
||||
|
||||
c_EdgeWeightedDigraph_Destroy(&graph);
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
TEST_START(EdgeWeightedDigraph_Plumbing_Suite);
|
||||
RUN_TEST(test_edge_weighted_digraph_plumbing);
|
||||
RUN_TEST(test_edge_weighted_digraph_soft_remove);
|
||||
TEST_REPORT();
|
||||
RETURN_TEST_STATUS;
|
||||
}
|
||||
Reference in New Issue
Block a user