import graphlib.edges.DirectedEdge; import graphlib.graphs.DirectedGraph; import graphlib.nodes.Node; import graphlib.util.Graphs; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.util.Arrays; public class SimpleTest { private static KortstePaden kp; @BeforeClass public static void initClass() { kp = new BellmanFordKortstePaden(); } private DirectedGraph graph; @Before public void init() { graph = new DirectedGraph(); for(int i = 0; i < 4; i++) { graph.addNode(new Node("n" + i)); } DirectedEdge e01 = new DirectedEdge(graph.getNode("n0"), graph.getNode("n1"), 50); DirectedEdge e03 = new DirectedEdge(graph.getNode("n0"), graph.getNode("n3"), -34); DirectedEdge e02 = new DirectedEdge(graph.getNode("n0"), graph.getNode("n2"), 20); DirectedEdge e13 = new DirectedEdge(graph.getNode("n1"), graph.getNode("n3"), 10); DirectedEdge e23 = new DirectedEdge(graph.getNode("n2"), graph.getNode("n3"), 15); graph.addEdge(e01); graph.addEdge(e02); graph.addEdge(e03); graph.addEdge(e13); graph.addEdge(e23); } @Test public void testSameNode() { Assert.assertEquals(Arrays.asList(graph.getNode("n0")), kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n0"), graph.getNode("n0"))); } @Test public void testPath01() { Assert.assertEquals(Arrays.asList(graph.getNode("n0"), graph.getNode("n1")), kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n0"), graph.getNode("n1"))); } @Test public void testPath02() { Assert.assertEquals(Arrays.asList(graph.getNode("n0"), graph.getNode("n2")), kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n0"), graph.getNode("n2"))); } @Test public void testPath03() { Assert.assertEquals(Arrays.asList(graph.getNode("n0"), graph.getNode("n3")), kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n0"), graph.getNode("n3"))); } @Test public void testPath13() { graph.addEdge(new DirectedEdge(graph.getNode("n1"), graph.getNode("n0"), 40)); Assert.assertEquals(Arrays.asList(graph.getNode("n1"), graph.getNode("n0"), graph.getNode("n3")), kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n1"), graph.getNode("n3"))); } @Test(expected = KortstePaden.UnreachableDestinationException.class) public void testUnreachableNode() { kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n1"), graph.getNode("n2")); } @Test(expected = KortstePaden.NegativeCycleException.class) public void testNegativeCycle() { graph.addEdge(new DirectedEdge(graph.getNode("n3"), graph.getNode("n0"), -34)); kp.kortstePad(Graphs.unmodifiableDirectedGraph(graph), graph.getNode("n0"), graph.getNode("n1")); } }