import graphlib.edges.DirectedEdge; import graphlib.graphs.DirectedMultiGraph; import graphlib.nodes.Node; import graphlib.paths.Walk; import graphlib.util.Graphs; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class SimpleTest { public static Cycle c; @BeforeClass public static void init() { c = new EulerianCycle(); } @Test public void test1() { DirectedMultiGraph graph = new DirectedMultiGraph<>(); graph.addNode(new Node<>("n1")); graph.addNode(new Node<>("n2")); graph.addNode(new Node<>("n3")); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n2"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n1"))); Walk euler = c.createCircuit(Graphs.unmodifiableDirectedMultiGraph(graph)); check(euler, graph); } @Test public void test2() { DirectedMultiGraph graph = new DirectedMultiGraph<>(); graph.addNode(new Node<>("n1")); graph.addNode(new Node<>("n2")); graph.addNode(new Node<>("n3")); graph.addNode(new Node<>("n4")); graph.addNode(new Node<>("n5")); graph.addNode(new Node<>("n6")); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n2"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n1"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n5"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n5"), graph.getNode("n4"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n4"), graph.getNode("n2"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n6"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n6"), graph.getNode("n5"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n5"), graph.getNode("n3"))); Walk euler = c.createCircuit(Graphs.unmodifiableDirectedMultiGraph(graph)); check(euler, graph); } @Test public void test3() { DirectedMultiGraph graph = new DirectedMultiGraph<>(); graph.addNode(new Node<>("n1")); graph.addNode(new Node<>("n2")); graph.addNode(new Node<>("n3")); graph.addNode(new Node<>("n4")); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n2"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n4"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n1"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n4"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n4"), graph.getNode("n1"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n4"), graph.getNode("n2"))); Walk euler = c.createCircuit(Graphs.unmodifiableDirectedMultiGraph(graph)); check(euler, graph); } @Test public void test4() { DirectedMultiGraph graph = new DirectedMultiGraph<>(); graph.addNode(new Node<>("n1")); graph.addNode(new Node<>("n2")); graph.addNode(new Node<>("n3")); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n2"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n1"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n2"), graph.getNode("n3"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n1"))); graph.addEdge(new DirectedEdge<>(graph.getNode("n3"), graph.getNode("n2"))); Walk euler = c.createCircuit(Graphs.unmodifiableDirectedMultiGraph(graph)); check(euler, graph); } @Test public void test5() { DirectedMultiGraph graph = new DirectedMultiGraph<>(); graph.addNode(new Node<>("n1")); graph.addEdge(new DirectedEdge<>(graph.getNode("n1"), graph.getNode("n1"))); Walk euler = c.createCircuit(Graphs.unmodifiableDirectedMultiGraph(graph)); check(euler, graph); } public void check(Walk walk, DirectedMultiGraph graph) { // lijst waaruit de gebruikte bogen worden verwijderd Set> usedEdges = new HashSet<>(graph.getAllEdges()); Iterator> it = walk.iterator(); Node first = null, second; if(it.hasNext()) { first = it.next(); } while(it.hasNext()) { second = it.next(); List> edges = graph.getEdges(first, second); // controleert of de gegeven boog wel in de graaf aanwezig is Assert.assertFalse("Je gebruikt een boog (" + first + "," + second + "), maar deze is niet in de graaf aanwezig.", edges.isEmpty()); int i = 0; boolean found = false; // zoekt de juiste boog (indien een boog meerdere keren aanwezig is) while(!found && i < edges.size()) { if (usedEdges.contains(graph.getEdges(first, second).get(i))) { found = true; } else { i++; } } // indien i==edges.size(), dan is de boog al eens gebruikt en al verwijderd uit de lijst usedEdges Assert.assertTrue("Je gebruikt de boog (" + first + "," + second + ") meerdere keren.",i < edges.size()); usedEdges.remove(graph.getEdges(first, second).get(i)); first = second; } // de bogen die nog aanwezig zijn in usedEdges zijn nog niet gebruikt in het pad Assert.assertTrue("De bogen " + usedEdges + " zijn niet gebruikt in jouw pad.", usedEdges.isEmpty()); } }