forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHierholzerAlgorithmTest.java
More file actions
60 lines (53 loc) · 2.43 KB
/
HierholzerAlgorithmTest.java
File metadata and controls
60 lines (53 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.thealgorithms.graph;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
public class HierholzerAlgorithmTest {
@Test
public void testFindsEulerianCircuitInSimpleTriangleGraph() {
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
graph.put(0, new LinkedList<>(Arrays.asList(1, 2)));
graph.put(1, new LinkedList<>(Arrays.asList(0, 2)));
graph.put(2, new LinkedList<>(Arrays.asList(0, 1)));
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
assertTrue(algorithm.hasEulerianCircuit());
List<Integer> circuit = algorithm.findEulerianCircuit();
assertEquals(4, circuit.size());
assertEquals(circuit.get(0), circuit.get(circuit.size() - 1));
}
@Test
public void testFailsForGraphWithOddDegreeVertices() {
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
graph.put(0, new LinkedList<>(Collections.singletonList(1)));
graph.put(1, new LinkedList<>(Collections.singletonList(0)));
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
assertFalse(algorithm.hasEulerianCircuit());
assertTrue(algorithm.findEulerianCircuit().isEmpty());
}
@Test
public void testFailsForDisconnectedGraph() {
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
graph.put(0, new LinkedList<>(Arrays.asList(1, 2)));
graph.put(1, new LinkedList<>(Arrays.asList(0, 2)));
graph.put(2, new LinkedList<>(Arrays.asList(0, 1)));
graph.put(3, new LinkedList<>(Arrays.asList(4, 5)));
graph.put(4, new LinkedList<>(Arrays.asList(3, 5)));
graph.put(5, new LinkedList<>(Arrays.asList(3, 4)));
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
assertFalse(algorithm.hasEulerianCircuit());
}
@Test
public void testHandlesEmptyGraph() {
Map<Integer, LinkedList<Integer>> graph = new HashMap<>();
HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph);
assertTrue(algorithm.hasEulerianCircuit());
assertTrue(algorithm.findEulerianCircuit().isEmpty());
}
}