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

71 lines
2.1 KiB
C

#include "c_Test.h"
#include "c_Digraph.h"
#include "c_DirectedEulerianPath.h"
#include "c_VertexIdList.h"
TEST_CASE(test_directed_eulerian_path_open) {
c_Digraph_t g;
c_Digraph_Init(&g, 4, NULL);
/* Construct an open Eulerian path: 0 -> 1 -> 2 -> 3 -> 1
* Start vertex: 0 (Out=1, In=0)
* End vertex: 1 (Out=1, In=2) -- wait, let's fix the loop to be valid:
* 0 -> 1
* 1 -> 2
* 2 -> 3
* 3 -> 1
* Degrees calculation:
* v=0: Out=1, In=0 (Start node!)
* v=1: Out=1, In=2
* v=2: Out=1, In=1
* v=3: Out=1, In=1
* Wait, v=1 has Out=1, In=2, which means delta is 1 (In > Out), so v=1 is the unique end node.
* Let's trace edges: (0,1), (1,2), (2,3), (3,1). Total edges = 4.
* Every single edge visited exactly once. Valid path!
*/
c_Digraph_AddEdge(&g, 0, 1);
c_Digraph_AddEdge(&g, 1, 2);
c_Digraph_AddEdge(&g, 2, 3);
c_Digraph_AddEdge(&g, 3, 1);
c_DirectedEulerianPath_t eulerian;
c_err_t err = c_DirectedEulerianPath_Init(&eulerian, &g, 0);
ASSERT_INT_EQ(C_SUCCESS, err);
ASSERT_TRUE(c_DirectedEulerianPath_HasPath(&eulerian));
c_VertexIdList_t path_output;
c_VertexIdList_Init(&path_output, 0, NULL);
err = c_DirectedEulerianPath_GetPath(&eulerian, &path_output);
ASSERT_INT_EQ(C_SUCCESS, err);
/* Total path sequence elements count must equal exact E + 1 (4 edges + 1 = 5 steps) */
c_size_t path_steps = (c_size_t)c_VertexIdList_GetSize(&path_output);
ASSERT_LL_EQ(5, path_steps);
/* Confirm correct endpoints sequence values */
c_uint_t start_v = 0;
c_uint_t end_v = 0;
c_VertexIdList_Get(&path_output, 0, &start_v);
c_VertexIdList_Get(&path_output, path_steps - 1, &end_v);
ASSERT_LL_EQ(0, start_v); /* Should start at 0 */
ASSERT_LL_EQ(1, end_v); /* Should terminate at 1 */
c_VertexIdList_Destroy(&path_output);
c_DirectedEulerianPath_Destroy(&eulerian);
c_Digraph_Destroy(&g);
}
int main(int argc, char** argv){
TEST_START(Component Tests);
// Execution list configurations
RUN_TEST(test_directed_eulerian_path_open);
TEST_REPORT();
RETURN_TEST_STATUS;
}