@Test public void testaddRoad_simple() { MapGraph mapGraph = new MapGraph(); mapGraph.addRoad("Road 1", new Point2D.Double(5, 0), new Point2D.Double(5, 10), 35); mapGraph.addRoad("Road 2", new Point2D.Double(0, 5), new Point2D.Double(10, 5), 35); mapGraph.addRoad("Road 3", new Point2D.Double(0, 7), new Point2D.Double(10, 7), 35); }
@Test public void testIntersection() { Point2D origin = new Point2D.Double(0, 0); Point2D p02 = new Point2D.Double(0, 2); Point2D p20 = new Point2D.Double(2, 0); Point2D p11 = new Point2D.Double(1, 1); Point2D p1_1 = new Point2D.Double(1, -1); assertEquals(MapGraph.intersection(origin, p02, p11, p1_1), null); assertEquals(MapGraph.intersection(origin, p20, p11, p1_1), new Point2D.Double(1, 0)); }
@Test public void testAStar_distance_slantedRoad() { MapGraph mapGraph = new MapGraph(); mapGraph.addRoad("Road 1", new Point2D.Double(0, 20), new Point2D.Double(100, 20), 35); mapGraph.addRoad("Road 2", new Point2D.Double(20, 0), new Point2D.Double(20, 100), 35); mapGraph.addRoad("Road 3", new Point2D.Double(0, 80), new Point2D.Double(100, 80), 35); mapGraph.addRoad("Road 4", new Point2D.Double(80, 0), new Point2D.Double(80, 100), 35); mapGraph.addRoad("Slanted road", new Point2D.Double(25, 15), new Point2D.Double(75, 85), 35); GraphVisualizer gv = new GraphVisualizer(mapGraph); LinkedList<Intersection> shortestPath1 = mapGraph.shortestPath_distance( mapGraph.getIntersectionByName("Road 1 + Road 2"), mapGraph.getIntersectionByName("Road 1 + Slanted road")); if (shortestPath1 == null) fail(); Iterator<Intersection> test = shortestPath1.iterator(); assertEquals("Road 1 + Road 2", test.next().toString()); assertEquals("Road 1 + Slanted road", test.next().toString()); assertFalse(test.hasNext()); LinkedList<Intersection> shortestPath2 = mapGraph.shortestPath_distance( mapGraph.getIntersectionByName("Road 1 + Road 2"), mapGraph.getIntersectionByName("Road 3 + Road 4")); if (shortestPath2 == null) fail(); test = shortestPath2.iterator(); assertEquals("Road 1 + Road 2", test.next().toString()); assertEquals("Road 1 + Slanted road", test.next().toString()); assertEquals("Road 3 + Slanted road", test.next().toString()); assertEquals("Road 3 + Road 4", test.next().toString()); assertFalse(test.hasNext()); }
@Test public void testAStar_distance_tictac() { MapGraph mapGraph = new MapGraph(); mapGraph.addRoad("Road 1", new Point2D.Double(0, 20), new Point2D.Double(100, 20), 35); mapGraph.addRoad("Road 2", new Point2D.Double(20, 0), new Point2D.Double(20, 100), 35); mapGraph.addRoad("Road 3", new Point2D.Double(0, 80), new Point2D.Double(100, 80), 35); LinkedList<Intersection> shortestPath1 = mapGraph.shortestPath_distance( mapGraph.getIntersectionByName("Road 1 + Road 2"), mapGraph.getIntersectionByName("Road 2 + Road 3")); if (shortestPath1 == null) fail(); Iterator<Intersection> test = shortestPath1.iterator(); assertEquals("Road 1 + Road 2", test.next().toString()); assertEquals("Road 2 + Road 3", test.next().toString()); assertFalse(test.hasNext()); mapGraph.addRoad("Road 4", new Point2D.Double(80, 0), new Point2D.Double(80, 100), 35); LinkedList<Intersection> shortestPath1b = mapGraph.shortestPath_distance( mapGraph.getIntersectionByName("Road 1 + Road 2"), mapGraph.getIntersectionByName("Road 3 + Road 4")); if (shortestPath1b == null) fail(); // System.out.println(shortestPath1b); // test = shortestPath1b.iterator(); // assertEquals("Road 1 + Road 2",test.next().toString()); // assertEquals("Road 2 + Road 3",test.next().toString()); // assertFalse(test.hasNext()); }
public static MapGraph createMapGraphFromText(List<String> fileLines) { if (fileLines.size() < 1) { throw new IllegalArgumentException("The file needs some content!"); } MapGraph newMap = new MapGraph(getName(fileLines.get(0))); for (int i = 1; i < fileLines.size(); i++) { String line = fileLines.get(i).replaceAll("[\\s]", ""); String[] stuff = line.split("--"); if (stuff.length >= 2) { String id1 = stuff[0]; String id2 = stuff[1]; newMap.addNeighbors(id1, id2); } } return newMap; }
/** * Helper function, used for printing the error messages when the test function finds the mismatch * between the expected result and the returned result. * * @param originCity * @param destCity * @param returnedPath * @param expectedPath * @param MapGraph gr */ public static void printInfo( String originCity, String destCity, ArrayList<Integer> returnedPath, String[] expectedPath, MapGraph gr) { System.out.println("MISMATCH FOUND: For cities " + originCity + " -> " + destCity); System.out.print("The expected path is: "); for (int l = 0; l < expectedPath.length; l++) { System.out.print(expectedPath[l] + " "); } System.out.print("\nYour program returned the following path: "); for (int l = 0; l < returnedPath.size(); l++) { String city = gr.getNode(returnedPath.get(l)).getCity(); System.out.print(city + " "); } }
@Test public void testAStar_time() { MapGraph mapGraph = new MapGraph(); mapGraph.addRoad("Road 1", new Point2D.Double(0, 20), new Point2D.Double(100, 20), 35); mapGraph.addRoad("Road 2", new Point2D.Double(20, 0), new Point2D.Double(20, 100), 5); mapGraph.addRoad("Road 3", new Point2D.Double(0, 80), new Point2D.Double(100, 80), 35); mapGraph.addRoad("Road 4", new Point2D.Double(80, 0), new Point2D.Double(80, 100), 35); mapGraph.addRoad("Slanted road", new Point2D.Double(25, 15), new Point2D.Double(75, 85), 5); LinkedList<Intersection> shortestPath1 = mapGraph.shortestPath_time( mapGraph.getIntersectionByName("Road 3 + Road 4"), mapGraph.getIntersectionByName("Road 1 + Road 2")); if (shortestPath1 == null) fail(); System.out.println(shortestPath1); Iterator<Intersection> test = shortestPath1.iterator(); assertEquals("Road 3 + Road 4", test.next().toString()); assertEquals("Road 1 + Road 4", test.next().toString()); assertEquals("Road 1 + Slanted road", test.next().toString()); assertEquals("Road 1 + Road 2", test.next().toString()); assertFalse(test.hasNext()); }
/** * Loads the graph from the input file. Runs Dijktra's algorithm on every origin node and for each * destination node, compares the returned path with the correct path in the resultsFile. * * @param inputFile * @param resultsFile * @return * @throws IOException */ public static boolean testDijkstra(String inputFile, String resultsFile) throws IOException { Dijkstra dijkstra = new Dijkstra(inputFile); MapGraph gr = dijkstra.getGraph(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(resultsFile)); String s; for (int i = 0; i < gr.numNodes(); i++) { CityNode originNode = gr.getNode(i); String originCity = originNode.getCity(); s = br.readLine(); if (s == null) { System.out.println("Error reading from the resultsFile"); return false; } assert (s.equals(originCity)); // Run Dijkstra's algorithm to compute the paths from originCity // to all other nodes dijkstra.computePaths(originNode); // Go along all the destinations for (int j = 0; j < gr.numNodes(); j++) { if (j != i) { CityNode destNode = gr.getNode(j); String destCity = destNode.getCity(); // get the path between originCity and destCity ArrayList<Integer> path = dijkstra.shortestPath(destNode); String pathString = br.readLine(); if (pathString == null) { System.out.println("Error reading from the resultsFile"); return false; } String[] expectedPath = pathString.split(" "); if (path.size() != expectedPath.length) { System.out.println("Your path's size = " + path.size()); System.out.println("Expected the path of size: " + expectedPath.length); DijkstraTest.printInfo(originCity, destCity, path, expectedPath, gr); return false; } assert (expectedPath[expectedPath.length - 1].equals(destCity)); // comparing the cities on the returned path and the expected path for (int k = 0; k < path.size(); k++) { Integer num = path.get(k); CityNode nodeOnPath = gr.getNode(num); String cityString = nodeOnPath.getCity(); if (!cityString.equals(expectedPath[k])) { DijkstraTest.printInfo(originCity, destCity, path, expectedPath, gr); return false; } } // for loop on nodes on the path } // if i!=j } // for destinations } // for origins } catch (IOException e) { e.getMessage(); } return true; }