import java.util.List; import java.util.Arrays; import org.junit.Test; import org.junit.Assert; import graphlib.edges.DirectedEdge; import graphlib.graphs.DirectedGraph; import graphlib.nodes.Node; public class SimpleTest { PathFinder pathFinder = new ASter(); private DirectedEdge naturalEdge(Node c1, Node c2) { double dx = c1.key().x() - c2.key().x(); double dy = c1.key().y() - c2.key().y(); return new DirectedEdge<>(c1, c2, Math.sqrt(dx * dx + dy * dy)); } @Test public void testSingleton() { DirectedGraph graph = new DirectedGraph<>(); Node a = graph.addNode(new Coordinate(0, 0)); Assert.assertEquals( Arrays.asList(a), pathFinder.shortestPath(graph, a, a) ); } @Test public void testSingleEdge() { DirectedGraph graph = new DirectedGraph<>(); Node a = graph.addNode(new Coordinate(0, 0)), b = graph.addNode(new Coordinate(1, 1)); graph.addEdge(naturalEdge(a, b)); Assert.assertEquals(Arrays.asList(a, b), pathFinder.shortestPath(graph, a, b)); Assert.assertNull(pathFinder.shortestPath(graph, b, a)); } @Test public void testVShape() { DirectedGraph graph = new DirectedGraph<>(); Node a = graph.addNode(new Coordinate(0, 0)), b = graph.addNode(new Coordinate(1, 0)), m = graph.addNode(new Coordinate(0, 1)); graph.addEdge(naturalEdge(a, m)); graph.addEdge(naturalEdge(m, b)); Assert.assertEquals(Arrays.asList(a), pathFinder.shortestPath(graph, a, a)); Assert.assertEquals(Arrays.asList(a, m), pathFinder.shortestPath(graph, a, m)); Assert.assertEquals(Arrays.asList(a, m, b), pathFinder.shortestPath(graph, a, b)); } @Test public void testTriangle() { DirectedGraph graph = new DirectedGraph<>(); Node a = graph.addNode(new Coordinate(0, 0)), b = graph.addNode(new Coordinate(1, 0)), c = graph.addNode(new Coordinate(0, 1)); graph.addEdge(naturalEdge(a, b)); graph.addEdge(naturalEdge(b, c)); graph.addEdge(naturalEdge(c, a)); Assert.assertEquals(Arrays.asList(a, b, c), pathFinder.shortestPath(graph, a, c)); Assert.assertEquals(Arrays.asList(b, c, a), pathFinder.shortestPath(graph, b, a)); Assert.assertEquals(Arrays.asList(c, a, b), pathFinder.shortestPath(graph, c, b)); } @Test public void testShortestPath() { DirectedGraph graph = new DirectedGraph<>(); Node a = graph.addNode(new Coordinate(0, 0)), b = graph.addNode(new Coordinate(1, 0)), c = graph.addNode(new Coordinate(1, 1)), d = graph.addNode(new Coordinate(2, 1)), e = graph.addNode(new Coordinate(1, 2)); graph.addEdge(naturalEdge(a, b)); graph.addEdge(naturalEdge(b, c)); graph.addEdge(naturalEdge(c, d)); graph.addEdge(naturalEdge(a, e)); graph.addEdge(naturalEdge(e, d)); graph.addEdge(naturalEdge(e, c)); graph.addEdge(naturalEdge(c, e)); Assert.assertEquals(Arrays.asList(a, b, c, d), pathFinder.shortestPath(graph, a, d)); } }