Skip to content

Commit ccb2ee2

Browse files
committed
fix: Add more test cases to improve code coverage
1 parent d3b0e3b commit ccb2ee2

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/test/java/com/thealgorithms/graph/HierholzerAlgorithmTest.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.thealgorithms.graph;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
3+
import static org.junit.jupiter.api.Assertions.*;
64
import java.util.*;
75
import org.junit.jupiter.api.Test;
86

97
public class HierholzerAlgorithmTest {
108

119
@Test
1210
public void testFindsEulerianCircuitInSimpleTriangleGraph() {
13-
// Create a simple triangle graph where a circuit definitely exists: 0-1, 1-2, 2-0
11+
// A simple triangle graph where a circuit definitely exists: 0-1, 1-2, 2-0
1412
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
1513
graph.put(0, new LinkedList<>(Arrays.asList(1, 2)));
1614
graph.put(1, new LinkedList<>(Arrays.asList(0, 2)));
@@ -31,16 +29,42 @@ public void testFindsEulerianCircuitInSimpleTriangleGraph() {
3129
}
3230

3331
@Test
34-
public void testHandlesGraphWithNoEulerianCircuit() {
35-
// Create a graph where a vertex has an odd degree
32+
public void testFailsForGraphWithOddDegreeVertices() {
33+
// Create a graph where vertices 0 and 1 have an odd degree (1)
3634
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
3735
graph.put(0, new LinkedList<>(Collections.singletonList(1)));
3836
graph.put(1, new LinkedList<>(Collections.singletonList(0)));
39-
graph.put(2, new LinkedList<>(Collections.emptyList())); // Vertex 2 is isolated
4037

4138
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
4239

4340
// The algorithm should correctly identify that no circuit exists
44-
assertEquals(false, algorithm.hasEulerianCircuit());
41+
assertFalse(algorithm.hasEulerianCircuit());
42+
// The find method should return an empty list
43+
assertTrue(algorithm.findEulerianCircuit().isEmpty());
44+
}
45+
46+
@Test
47+
public void testFailsForDisconnectedGraph() {
48+
// Create a graph with two separate triangles (0-1-2 and 3-4-5)
49+
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
50+
graph.put(0, new LinkedList<>(Arrays.asList(1, 2)));
51+
graph.put(1, new LinkedList<>(Arrays.asList(0, 2)));
52+
graph.put(2, new LinkedList<>(Arrays.asList(0, 1)));
53+
graph.put(3, new LinkedList<>(Arrays.asList(4, 5)));
54+
graph.put(4, new LinkedList<>(Arrays.asList(3, 5)));
55+
graph.put(5, new LinkedList<>(Arrays.asList(3, 4)));
56+
57+
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
58+
59+
// All degrees are even, but the graph is not connected, so no circuit exists
60+
assertFalse(algorithm.hasEulerianCircuit());
61+
}
62+
63+
@Test
64+
public void testHandlesEmptyGraph() {
65+
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
66+
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
67+
assertTrue(algorithm.hasEulerianCircuit());
68+
assertTrue(algorithm.findEulerianCircuit().isEmpty());
4569
}
46-
}
70+
}

0 commit comments

Comments
 (0)