diff --git a/Dijkstra/TestFile3 b/Dijkstra/TestFile3 new file mode 100644 index 0000000..dae3f16 --- /dev/null +++ b/Dijkstra/TestFile3 @@ -0,0 +1,124 @@ +Digraph { +Siegen -> Osnabrueck [label="22,100"]; +FrankfurtanderOder -> Kassel [label="68,80"]; +FrankfurtanderOder -> Potsdam [label="89,120"]; +Goeteborg -> Aachen [label="30,200"]; +Dresden -> Duisburg [label="84,120"]; +Bremen -> Duisburg [label="8,80"]; +Bremen -> Chemnitz [label="52,80"]; +Bremen -> Bochum [label="49,50"]; +Duesseldorf -> Dresden [label="76,130"]; +Duesseldorf -> Mannheim [label="60,50"]; +Duesseldorf -> Bochum [label="45,50"]; +Aachen -> Giessen [label="41,100"]; +Mainz -> Siegen [label="85,80"]; +Mainz -> Luebeck [label="80,100"]; +FrankfurtamMain -> Potsdam [label="57,80"]; +Mannheim -> Aachen [label="69,120"]; +Mannheim -> Chemnitz [label="24,100"]; +Essen -> FrankfurtanderOder [label="99,50"]; +Passau -> Cologne [label="73,120"]; +Heidelberg -> Bremen [label="57,120"]; +Saarbruecken -> FrankfurtamMain [label="56,100"]; +Bochum -> Aachen [label="58,50"]; +Bochum -> Munich [label="6,130"]; +Bochum -> Bremen [label="49,50"]; +Bochum -> Schwerin [label="42,120"]; +Bochum -> Osnabrueck [label="62,120"]; +Bochum -> Duesseldorf [label="45,50"]; +Schwerin -> Bamberg [label="48,50"]; +Schwerin -> Cologne [label="30,120"]; +Schwerin -> Kassel [label="56,120"]; +Osnabrueck -> Brunswick [label="17,80"]; +Potsdam -> Mainz [label="40,120"]; +Hanover -> Schwerin [label="90,50"]; +Hanover -> Duesseldorf [label="9,80"]; +Hanover -> Leipzig [label="76,50"]; +Berlin -> Dresden [label="70,100"]; +Hamburg -> Berlin [label="87,80"]; +Hamburg -> Augsburg [label="78,120"]; +Hamburg -> Luebeck [label="59,50"]; +Braunschweig -> Hamburg [label="9,50"]; +Goettingen -> Bonn [label="38,50"]; +Goettingen -> Luebeck [label="9,50"]; +Goettingen -> Duesseldorf [label="83,120"]; +Bayreuth -> Hamburg [label="53,100"]; +Chemnitz -> Augsburg [label="5,100"]; +Oldenburg -> Erfurt [label="8,130"]; +Erfurt -> Bonn [label="80,130"]; +Erfurt -> Bayreuth [label="38,120"]; +Leipzig -> Munich [label="54,120"]; +Leipzig -> Hamburg [label="47,50"]; +Leipzig -> Chemnitz [label="77,50"]; +Darmstadt -> Munich [label="25,50"]; +Darmstadt -> Schwerin [label="66,50"]; +Giessen -> Braunschweig [label="59,50"]; +Giessen -> Luebeck [label="75,50"]; +Giessen -> Erfurt [label="23,50"]; +Regensburg -> FrankfurtamMain [label="68,80"]; +Regensburg -> Siegen [label="98,120"]; +Regensburg -> Luebeck [label="64,120"]; +Regensburg -> Osnabrueck [label="61,120"]; +Bonn -> Hanover [label="10,120"]; +Kiel -> Bayreuth [label="89,50"]; +Kiel -> Bochum [label="26,130"]; +Kiel -> Potsdam [label="50,50"]; +Bamberg -> Siegen [label="75,120"]; +Bamberg -> Goettingen [label="47,50"]; +Bamberg -> Schwerin [label="48,50"]; +Duisburg -> Bamberg [label="48,50"]; +Duisburg -> Heidelberg [label="62,80"]; +Munich -> Hanover [label="20,50"]; +Munich -> Heidelberg [label="57,50"]; +Munich -> Saarbruecken [label="18,50"]; +Cologne -> Schwerin [label="30,120"]; +Augsburg -> Hamburg [label="78,120"]; +Augsburg -> Kassel [label="38,50"]; +Kassel -> Bonn [label="85,50"]; +Kassel -> Schwerin [label="72,120"]; +Brunswick -> Hanover [label="97,100"]; +Brunswick -> Bochum [label="9,100"]; +Luebeck -> Mainz [label="80,100"]; +Luebeck -> Brunswick [label="18,130"]; +Siegen [label="Siegen,2"]; +FrankfurtanderOder [label="FrankfurtanderOder,1"]; +Goeteborg [label="Goeteborg,10"]; +Dresden [label="Dresden,2"]; +Bremen [label="Bremen,4"]; +Duesseldorf [label="Duesseldorf,1"]; +Aachen [label="Aachen,3"]; +Mainz [label="Mainz,2"]; +FrankfurtamMain [label="FrankfurtamMain,1"]; +Mannheim [label="Mannheim,1"]; +Essen [label="Essen,3"]; +Passau [label="Passau,1"]; +Heidelberg [label="Heidelberg,2"]; +Saarbruecken [label="Saarbruecken,2"]; +Bochum [label="Bochum,2"]; +Schwerin [label="Schwerin,2"]; +Osnabrueck [label="Osnabrueck,1"]; +Potsdam [label="Potsdam,2"]; +Hanover [label="Hanover,1"]; +Berlin [label="Berlin,2"]; +Hamburg [label="Hamburg,2"]; +Braunschweig [label="Braunschweig,2"]; +Goettingen [label="Goettingen,4"]; +Bayreuth [label="Bayreuth,4"]; +Chemnitz [label="Chemnitz,2"]; +Oldenburg [label="Oldenburg,2"]; +Erfurt [label="Erfurt,5"]; +Leipzig [label="Leipzig,2"]; +Darmstadt [label="Darmstadt,2"]; +Giessen [label="Giessen,2"]; +Regensburg [label="Regensburg,3"]; +Bonn [label="Bonn,2"]; +Kiel [label="Kiel,1"]; +Bamberg [label="Bamberg,4"]; +Duisburg [label="Duisburg,2"]; +Munich [label="Munich,4"]; +Cologne [label="Cologne,1"]; +Augsburg [label="Augsburg,4"]; +Kassel [label="Kassel,1"]; +Brunswick [label="Brunswick,4"]; +Luebeck [label="Luebeck,4"]; +} diff --git a/Dijkstra/TestFile4 b/Dijkstra/TestFile4 new file mode 100644 index 0000000..d357f4b --- /dev/null +++ b/Dijkstra/TestFile4 @@ -0,0 +1,27 @@ +Digraph { +A -> B [label="2,70"]; +A -> F [label="10,90"]; +A -> G [label="9,120"]; +B -> C [label="4,70"]; +B -> G [label="6,130"]; +C -> D [label="2,30"]; +C -> I [label="15,90"]; +D -> E [label="1,15"]; +D -> F [label="1,60"]; +D -> I [label="1,30"]; +E -> H [label="6,100"]; +F -> E [label="6,90"]; +F -> H [label="5,50"]; +G -> I [label="2,55"]; +H -> G [label="4,120"]; +I -> H [label="15,30"]; +A [label="A,3"]; +B [label="B,2"]; +C [label="C,4"]; +D [label="D,0"]; +E [label="E,0"]; +F [label="F,2"]; +G [label="G,2"]; +H [label="H,1"]; +I [label="I,2"]; +} \ No newline at end of file diff --git a/Dijkstra/TestFile5 b/Dijkstra/TestFile5 new file mode 100644 index 0000000..c1a74d4 --- /dev/null +++ b/Dijkstra/TestFile5 @@ -0,0 +1,17 @@ +Digraph { +A -> B [label="69,50"]; +A [label="A,2"]; +B -> A [label="67,80"]; +B -> C [label="63,50"]; +B -> D [label="40,50"]; +C -> D [label="23,80"]; +C [label="C,1"]; +E -> C [label="26,50"]; +E -> D [label="26,130"]; +E [label="E,4"]; +D -> C [label="5,50"]; +D [label="D,1"]; +F -> A [label="30,200"]; +F [label="F,10"]; +B [label="B,2"]; +} diff --git a/Dijkstra/TestFile6 b/Dijkstra/TestFile6 new file mode 100644 index 0000000..9a06494 --- /dev/null +++ b/Dijkstra/TestFile6 @@ -0,0 +1,17 @@ +Digraph { +Aachen -> Augsburg [label="69,50"]; +Aachen [label="Aachen,2"]; +Augsburg -> Aachen [label="67,80"]; +Augsburg -> Bamberg [label="63,50"]; +Augsburg -> Berlin [label="40,50"]; +Bamberg -> Berlin [label="23,80"]; +Bamberg [label="Bamberg,1"]; +Bayreuth -> Bamberg [label="26,50"]; +Bayreuth -> Berlin [label="26,130"]; +Bayreuth [label="Bayreuth,4"]; +Berlin -> Bamberg [label="5,50"]; +Berlin [label="Berlin,1"]; +Goeteborg -> Aachen [label="30,200"]; +Goeteborg [label="Goeteborg,10"]; +Augsburg [label="Augsburg,2"]; +} diff --git a/Dijkstra/src/frame/AllTests.java b/Dijkstra/src/frame/AllTests.java index 74504fb..ec2e05f 100644 --- a/Dijkstra/src/frame/AllTests.java +++ b/Dijkstra/src/frame/AllTests.java @@ -1,160 +1,29 @@ package frame; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; +import java.time.Duration; import java.util.ArrayList; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; +import java.util.Collections; import lab.Navigation; +/** + * TestCases for the second lab. This class contains the static methods used by + * the test cases. + * + * @author Stefan Kropp, Andreas Johansson + */ +// public class NavigationLabTestCase extends TestCase { public class AllTests { + + public static final Duration timeout = Duration.ofMillis(750); - private enum OutputFormat { - Distance, Time + public static enum OutputFormat { + Distance, Time, RouteDistance, RouteTime }; - @Nested - @DisplayName("Dijkstra TestFile1") - class TestFile1 { - - @Test - @DisplayName("Test Distance from A to C") - public final void testFile1_A_C_Distance() { - assertEquals(8, testDistance("TestFile1", "A", "C"), "From A to C: "); - } - - @Test - @DisplayName("Test Distance from A to G") - public final void testFile1_A_G_Distance() { - assertEquals(24, testDistance("TestFile1", "A", "G"), "From A to G: "); - } - - @Test - @DisplayName("Test Time from A to D") - public final void testFile1_A_D_Time() { - assertEquals(14, testTime("TestFile1", "A", "D"), "From A to D: "); - } - - @Test - @DisplayName("Test Time from A to F") - public final void testFile1_A_F_Time() { - assertEquals(18, testTime("TestFile1", "A", "F"), "From A to F: "); - } - - @Test - @DisplayName("Test Shortest Route from A to E") - public final void testfile1_Route_A_E_Distance() { - ArrayList route = new ArrayList(); - Navigation nav = new Navigation("TestFile1"); - // Build the route we expect to find - route.add("A\\s*->\\s*C"); - route.add("C\\s*->\\s*D"); - route.add("D\\s*->\\s*E"); - assertTrue(testRoute(nav.findShortestRoute("A", "E"), route), "Route not correct"); - } - - @Test - @DisplayName("Test Fastest Route from A to F") - public final void testfile1_Route_A_F_Time() { - ArrayList route = new ArrayList(); - Navigation nav = new Navigation("TestFile1"); - // Build the route we expect to find - route.add("A\\s*->\\s*C"); - route.add("C\\s*->\\s*D"); - route.add("D\\s*->\\s*F"); - assertTrue(testRoute(nav.findFastestRoute("A", "F"), route), "Route not correct"); - } - - } - - @Nested - @DisplayName("Dijkstra TestFile2") - class TestFile2 { - - @Test - @DisplayName("Test Distance from A to C") - public final void testFile2_A_C_Distance() { - assertEquals(8, testDistance("TestFile2", "A", "C"), "From A to C: "); - } - - @Test - @DisplayName("Test Distance from A to D") - public final void testFile2_A_D_Distance() { - assertEquals(12, testDistance("TestFile2", "A", "D"), "From A to D: "); - } - - @Test - @DisplayName("Test Distance from A to F") - public final void testFile2_A_F_Distance() { - assertEquals(16, testDistance("TestFile2", "A", "F"), "From A to F: "); - } - - @Test - @DisplayName("Test Time from A to C") - public final void testFile2_A_C_Time() { - assertEquals(8, testTime("TestFile2", "A", "C"), "From A to C: "); - } - - @Test - @DisplayName("Test Time from A to E") - public final void testFile2_A_E_Time() { - assertEquals(15, testTime("TestFile2", "A", "E"), "From A to E: "); - } - - @Test - @DisplayName("Test Time from A to F") - public final void testFile2_A_F_Time() { - assertEquals(12, testTime("TestFile2", "A", "F"), "From A to F: "); - } - - @Test - @DisplayName("Test Number of Vertices on Shortest Route from A to B") - public final void testFile2_Size() { - Navigation nav = new Navigation("TestFile2"); - assertEquals(16, nav.findShortestRoute("A", "B").size(), "Number of entries in output map: "); - } - - @Test - @DisplayName("Test Missing vertex G") - public final void testFile2_Negative() { - Navigation nav = new Navigation("TestFile2"); - assertEquals(-2, nav.findShortestDistance("A", "G"), "Test non-existing vertex: "); - } - - @Test - @DisplayName("Test Shortest Route from A to E") - public final void testfile2_Route_A_E_Distance() { - ArrayList route = new ArrayList(); - Navigation nav = new Navigation("TestFile2"); - // Build the route we expect to find - route.add("A\\s*->\\s*B"); - route.add("B\\s*->\\s*C"); - route.add("C\\s*->\\s*D"); - route.add("D\\s*->\\s*E"); - assertTrue(testRoute(nav.findShortestRoute("A", "E"), route), "Route not correct"); - } - - @Test - @DisplayName("Test Fastest Route from A to E") - public final void testfile2_Route_A_E_Time() { - ArrayList route = new ArrayList<>(); - Navigation nav = new Navigation("TestFile2"); - // Build the route we expect to find - route.add("A\\s*->\\s*B"); - route.add("B\\s*->\\s*C"); - route.add("C\\s*->\\s*E"); - assertTrue(testRoute(nav.findFastestRoute("A", "E"), route), "Route not correct"); - } - - } - /** * This method returns the shortest distance from start to stop on the map * stored in filename. @@ -172,13 +41,13 @@ public class AllTests { * Destination node * @return The shortest distance between start and stop in km */ - private final int testDistance(String filename, String start, String stop) { - Navigation nav = new Navigation(filename); - ArrayList returnMap = new ArrayList(); + public static final int testDistance(String filename, String start, String stop) { + Navigation lab = new Navigation(filename); + // ArrayList < String > returnMap = new ArrayList < String >(); - returnMap = nav.findShortestRoute(start, stop); - writeGraphToFile(returnMap, filename, start, stop, OutputFormat.Distance); - return nav.findShortestDistance(start, stop); + // returnMap = lab.findShortestRoute(start,stop); + // writeGraphToFile(returnMap, filename, start, stop, OutputFormat.Distance); + return lab.findShortestDistance(start, stop); } /** @@ -198,18 +67,19 @@ public class AllTests { * Destination node * @return Fastest route in minutes */ - private final int testTime(String filename, String start, String stop) { - Navigation nav = new Navigation(filename); - ArrayList returnMap = new ArrayList(); + public static final int testTime(String filename, String start, String stop) { + Navigation lab = new Navigation(filename); + // ArrayList < String > returnMap = new ArrayList < String >(); - returnMap = nav.findFastestRoute(start, stop); - writeGraphToFile(returnMap, filename, start, stop, OutputFormat.Time); - return nav.findFastestTime(start, stop); + // returnMap = lab.findFastestRoute(start,stop); + // writeGraphToFile(returnMap, filename, start, stop, OutputFormat.Time); + return lab.findFastestTime(start, stop); } /** * This method tests wether the edges contained in boldEdges are present and - * marked as bold in map + * marked as bold in map. It prints any edges not found bold to standard error. + * It also prints the graph to a file with a fitting name. * * @param map * The map to check, in dot format @@ -217,19 +87,63 @@ public class AllTests { * The edges to find * @return True if all edges in boldEdges are marked bold in map */ - private final boolean testRoute(ArrayList map, ArrayList boldEdges) { + public static final boolean testRoute(boolean positiveTest, ArrayList map, ArrayList boldEdges, + String filename, String start, String stop, OutputFormat format) { + boolean foundEdge = false; + ; boolean correct = true; + boolean tmpCorrect = true; int matches = 0; + for (String edge : boldEdges) { // for all edges we're looking for + foundEdge = false; for (String line : map) { // for all lines in the map if (line.matches(".*" + edge + ".*")) { // if the edge is there - correct = correct && line.matches(".*bold.*"); // check if it is bold + tmpCorrect = line.matches(".*bold.*"); // check if it is bold + if (!tmpCorrect && positiveTest) { + System.err.println("[" + filename + "_route_" + start + "_" + stop + + "] looked for bold for edge: '" + edge + "' but found '" + line + "'"); + } + correct = correct && tmpCorrect; matches++; // Count the number of bold lines correctly found + foundEdge = true; + break; // Let's not continue when we found one edge } } + if (!foundEdge && positiveTest) { + System.err.println("[" + filename + "_route_" + start + "_" + stop + "] Didn't find edge: " + edge); + } } // Check if we found all of them correct = correct && (matches == boldEdges.size()); + //writeGraphToFile(map, filename, start, stop, format); + return correct; + } + + /** + * Tests if any edges are marked as bold. Returns true if no edges are marked as + * bold. + * + * @param map + * @param filename + * @param start + * @param stop + * @param format + * @return + */ + public static final boolean testNoRoute(ArrayList map, String filename, String start, String stop, + OutputFormat format) { + boolean correct = true; + + for (String line : map) { // for all lines in the map + if (line.matches(".*bold.*")) { // if the edge is there + correct = false; + System.err.println( + "[" + filename + "_route_" + start + "_" + stop + "] found unexpected bold edge: " + line); + } + } + + // writeGraphToFile(map,filename,start,stop,format); return correct; } @@ -239,9 +153,8 @@ public class AllTests { * The format of the filename of the file created depends on the last four * parameters: * - * if format = OutputFormat.Time: output_[filename]_from[start]to[stop]Time.txt - * if format = OutputFormat.Distance: - * output_[filename]_from[start]to[stop]Distance.txt + * if format = OutputForma.Time: output_[filename]_from[start]to[stop]Time.txt + * if format = OutputForma.Distance if format = OutputForma.Distance * * @param map * @param filename @@ -249,22 +162,31 @@ public class AllTests { * @param stop * @param format */ - public final void writeGraphToFile(ArrayList map, String filename, String start, String stop, + public static final void writeGraphToFile(ArrayList map, String filename, String start, String stop, OutputFormat format) { try { String typeString = null; switch (format) { case Distance: - typeString = "distance"; + typeString = "_Distance"; break; case Time: - typeString = "time"; + typeString = "_Time"; + break; + case RouteDistance: + typeString = "_RouteDistance"; + break; + case RouteTime: + typeString = "_RouteTime"; break; } FileWriter fw = new FileWriter("output_" + filename + "_from" + start + "to" + stop + typeString + ".txt"); BufferedWriter bw = new BufferedWriter(fw); + if (map.size() > 2) + Collections.sort(map.subList(1, map.size() - 2)); + for (String element : map) { bw.write(element); bw.newLine(); diff --git a/Dijkstra/src/frame/TestFile1TestCases.java b/Dijkstra/src/frame/TestFile1TestCases.java new file mode 100644 index 0000000..01be584 --- /dev/null +++ b/Dijkstra/src/frame/TestFile1TestCases.java @@ -0,0 +1,237 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile1TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void TestFile1_A_C_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(8, AllTests.testDistance("TestFile1", "A", "C"), "From A to C: "); + }); + } + + @Test + public void TestFile1_A_E_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(18, AllTests.testDistance("TestFile1", "A", "E"), "From A to G: "); + }); + } + + @Test + public void TestFile1_A_G_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(24, AllTests.testDistance("TestFile1", "A", "G"), "From A to G: "); + }); + } + + @Test + public void TestFile1_A_H_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(29, AllTests.testDistance("TestFile1", "A", "H"), "From A to G: "); + }); + } + + @Test + public void TestFile1_A_B_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(7, AllTests.testTime("TestFile1", "A", "B"), "From A to D: "); + }); + } + + @Test + public void TestFile1_A_D_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(14, AllTests.testTime("TestFile1", "A", "D"), "From A to D: "); + }); + } + + @Test + public void TestFile1_A_E_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(19, AllTests.testTime("TestFile1", "A", "E"), "From A to D: "); + }); + } + + @Test + public void TestFile1_A_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(18, AllTests.testTime("TestFile1", "A", "F"), "From A to F: "); + }); + } + + @Test + public void TestFile1_A_H_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(37, AllTests.testTime("TestFile1", "A", "H"), "From A to D: "); + }); + } + + @Test + public void TestFile1_Size() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(20, lab.findShortestRoute("A", "B").size(), "Number of entries in output map: "); + }); + } + + @Test + public void TestFile1_Reflexive() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(0, lab.findShortestDistance("A", "A"), "Test reflexitivity (A->A): "); + }); + } + + @Test + public void TestFile1_NoPointA() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(-1, lab.findShortestDistance("R", "A"), "Test non-existing node: "); + }); + } + + @Test + public void TestFile1_NoPointB() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(-2, lab.findShortestDistance("A", "R"), "Test non-existing nodes: "); + }); + } + + @Test + public void TestFile1_NoPointsAtAll() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(-3, lab.findShortestDistance("L", "R"), "Test non-existing path: "); + }); + } + + @Test + public void TestFile1_B_A_NoPath() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + assertEquals(-4, lab.findShortestDistance("B", "A"), "Test non-existing path B->A: "); + }); + } + + @Test + public void TestFile1_Route_B_A_NoPath() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile1"); + ArrayList map = lab.findShortestRoute("B", "A"); + assertEquals(20, map.size(), "Expected more lines in the file B->A"); + assertTrue(AllTests.testNoRoute(map, "TestFile1", "B", "A", AllTests.OutputFormat.RouteDistance), + "Test non-existing path B->A: "); + }); + } + + @Test + public void TestFile1_Route_A_B_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("A\\s*->\\s*C"); + route.add("C\\s*->\\s*B"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("A", "B"), route, "TestFile1", "A", "G", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_E_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("A\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*E"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("A", "E"), route, "TestFile1", "A", "E", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_G_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("A\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*F"); + route.add("F\\s*->\\s*G"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("A", "G"), route, "TestFile1", "A", "G", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("A\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*F"); + assertTrue(AllTests.testRoute(true, lab.findFastestRoute("A", "F"), route, "TestFile1", "A", "F", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_H_Time() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("A\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*F"); + route.add("F\\s*->\\s*G"); + route.add("G\\s*->\\s*H"); + assertTrue(AllTests.testRoute(true, lab.findFastestRoute("A", "H"), route, "TestFile1", "A", "H", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_F_NOPATH_E_F() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("E\\s*->\\s*F"); + assertFalse(AllTests.testRoute(false, lab.findFastestRoute("A", "F"), route, "TestFile1", "A", "F", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + + @Test + public void TestFile1_Route_A_F_NOPATH_B_D() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile1"); + // Build the route we expect to find + route.add("B\\s*->\\s*D"); + assertFalse(AllTests.testRoute(false, lab.findFastestRoute("A", "F"), route, "TestFile1", "A", "F", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + +} diff --git a/Dijkstra/src/frame/TestFile2TestCases.java b/Dijkstra/src/frame/TestFile2TestCases.java new file mode 100644 index 0000000..110001b --- /dev/null +++ b/Dijkstra/src/frame/TestFile2TestCases.java @@ -0,0 +1,151 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile2TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void TestFile2_A_C_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(8, AllTests.testDistance("TestFile2", "A", "C"), "From A to C: "); + }); + } + + @Test + public void TestFile2_A_D_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(12, AllTests.testDistance("TestFile2", "A", "D"), "From A to D: "); + }); + } + + @Test + public void TestFile2_A_F_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(16, AllTests.testDistance("TestFile2", "A", "F"), "From A to F: "); + }); + } + + @Test + public void TestFile2_B_F_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(15, AllTests.testDistance("TestFile2", "B", "F"), "From A to F: "); + }); + } + + @Test + public void TestFile2_A_C_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(8, AllTests.testTime("TestFile2", "A", "C"), "From A to C: "); + }); + } + + @Test + public void TestFile2_A_E_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(15, AllTests.testTime("TestFile2", "A", "E"), "From A to E: "); + }); + } + + @Test + public void TestFile2_A_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(12, AllTests.testTime("TestFile2", "A", "F"), "From A to F: "); + }); + } + + @Test + public void TestFile2_B_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(16, AllTests.testTime("TestFile2", "B", "F"), "From A to F: "); + }); + } + + @Test + public void TestFile2_Size() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile2"); + assertEquals(16, lab.findShortestRoute("A", "B").size(), "Number of entries in output map: "); + }); + } + + @Test + public void TestFile2_NoPointA() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile2"); + assertEquals(-1, lab.findShortestDistance("G", "A"), "Test non-existing node 1: "); + }); + } + + @Test + public void TestFile2_NoPointB() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile2"); + assertEquals(-2, lab.findShortestDistance("A", "G"), "Test non-existing node 2: "); + }); + } + + @Test + public void TestFile2_NoPointsAtAll() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile2"); + assertEquals(-3, lab.findShortestDistance("L", "G"), "Test non-existing nodes: "); + }); + } + + @Test + public void TestFile2_NoPath() { + assertTimeoutPreemptively(timeout, () -> { + Navigation lab = new Navigation("TestFile2"); + assertEquals(-4, lab.findShortestDistance("B", "A"), "Test non-existing path B->A: "); + }); + } + + @Test + public void TestFile2_Route_A_E_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile2"); + // Build the route we expect to find + route.add("A\\s*->\\s*B"); + route.add("B\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*E"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("A", "E"), route, "TestFile2", "A", "E", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile2_Route_A_E_Time() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + ArrayList foundRoute = new ArrayList(); + String source = "A"; + String destination = "E"; + String fileName = "TestFile2"; + + Navigation lab = new Navigation(fileName); + // Build the route we expect to find + route.add("A\\s*->\\s*B"); + route.add("B\\s*->\\s*C"); + route.add("C\\s*->\\s*E"); + + foundRoute = lab.findFastestRoute(source, destination); + + assertTrue(AllTests.testRoute(true, foundRoute, route, fileName, source, destination, + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + +} diff --git a/Dijkstra/src/frame/TestFile3TestCases.java b/Dijkstra/src/frame/TestFile3TestCases.java new file mode 100644 index 0000000..fab94a4 --- /dev/null +++ b/Dijkstra/src/frame/TestFile3TestCases.java @@ -0,0 +1,173 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile3TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void TestFile3_Duisburg_Augsburg_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(176, AllTests.testDistance("TestFile3", "Duisburg", "Augsburg"), + "From Duisburg to Augsburg: "); + }); + } + + @Test + public void TestFile3_Hanover_Giessen_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(153, AllTests.testDistance("TestFile3", "Hanover", "Giessen"), "From Hanover to Giessen: "); + }); + } + + @Test + public void TestFile3_Regensburg_Braunschweig_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(245, AllTests.testDistance("TestFile3", "Regensburg", "Braunschweig"), + "From Regensburg to Braunschweig: "); + }); + } + + @Test + public void TestFile3_Duisburg_Augsburg_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(125, AllTests.testTime("TestFile3", "Duisburg", "Augsburg"), "From Duisburg to Augsburg: "); + }); + } + + @Test + public void TestFile3_Hanover_Giessen_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(143, AllTests.testTime("TestFile3", "Hanover", "Giessen"), "From Hanover to Giessen: "); + }); + } + + @Test + public void TestFile3_Regensburg_Braunschweig_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(226, AllTests.testTime("TestFile3", "Regensburg", "Braunschweig"), + "From Regensburg to Braunschweig: "); + }); + } + + @Test + public void TestFile3_Route_Duisburg_Augsburg_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile3"); + // Build the route we expect to find + route.add("Duisburg\\s*->\\s*Heidelberg"); + route.add("Heidelberg\\s*->\\s*Bremen"); + route.add("Bremen\\s*->\\s*Chemnitz"); + route.add("Chemnitz\\s*->\\s*Augsburg"); + ArrayList map = lab.findShortestRoute("Duisburg", "Augsburg"); + assertTrue(AllTests.testRoute(true, map, route, "TestFile3", "Duisburg", "Augsburg", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile3_Route_Duisburg_Augsburg_Time_Size() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList map; + Navigation lab = new Navigation("TestFile3"); + map = lab.findFastestRoute("Duisburg", "Augsburg"); + assertEquals(124, map.size(), "Expected more lines in output file:"); + }); + } + + @Test + public void TestFile3_Route_Duisburg_Augsburg_Time() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + ArrayList map; + Navigation lab = new Navigation("TestFile3"); + map = lab.findFastestRoute("Duisburg", "Augsburg"); + // Build the route we expect to find + route.add("Duisburg\\s*->\\s*Heidelberg"); + route.add("Heidelberg\\s*->\\s*Bremen"); + route.add("Bremen\\s*->\\s*Chemnitz"); + route.add("Chemnitz\\s*->\\s*Augsburg"); + assertTrue(AllTests.testRoute(true, map, route, "TestFile3", "Duisburg", "Augsburg", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + + @Test + public void TestFile3_Route_Regensburg_Braunschweig_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile3"); + // Build the route we expect to find + route.add("Regensburg\\s*->\\s*Osnabrueck"); + route.add("Osnabrueck\\s*->\\s*Brunswick"); + route.add("Brunswick\\s*->\\s*Bochum"); + route.add("Bochum\\s*->\\s*Aachen"); + route.add("Aachen\\s*->\\s*Giessen"); + route.add("Giessen\\s*->\\s*Braunschweig"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("Regensburg", "Braunschweig"), route, "TestFile3", + "Regensburg", "Braunschweig", AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void TestFile3_Route_Regensburg_Braunschweig_Time() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route1 = new ArrayList(); + ArrayList route2 = new ArrayList(); + Navigation lab = new Navigation("TestFile3"); + // Build the route we expect to find + route1.add("Regensburg\\s*->\\s*Osnabrueck"); + route1.add("Osnabrueck\\s*->\\s*Brunswick"); + route1.add("Brunswick\\s*->\\s*Bochum"); + route1.add("Bochum\\s*->\\s*Aachen"); + route1.add("Aachen\\s*->\\s*Giessen"); + route1.add("Giessen\\s*->\\s*Braunschweig"); + + // alternatively instead + + route2.add("Regensburg\\s*->\\s*Luebeck"); + route2.add("Luebeck\\s*->\\s*Brunswick"); + route2.add("Brunswick\\s*->\\s*Bochum"); + route2.add("Bochum\\s*->\\s*Aachen"); + route2.add("Aachen\\s*->\\s*Giessen"); + route2.add("Giessen\\s*->\\s*Braunschweig"); + + ArrayList res = lab.findFastestRoute("Regensburg", "Braunschweig"); + boolean path1 = AllTests.testRoute(true, res, route1, "TestFile3-path1", "Regensburg", "Braunschweig", + AllTests.OutputFormat.RouteTime); + + ArrayList res2 = lab.findFastestRoute("Regensburg", "Braunschweig"); + boolean path2 = AllTests.testRoute(true, res2, route2, "TestFile3-path2", "Regensburg", "Braunschweig", + AllTests.OutputFormat.RouteTime); + + assertTrue(path1 || path2, "Route not correct"); + }); + } + + @Test + public void TestFile3_Route_FrankfurtAmMain_Brunswick_NOPATH_Goeteborg_Aachen() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile3"); + // Build the route we expect to find + route.add("Goeteborg\\s*->\\s*Aachen"); + assertFalse( + AllTests.testRoute(false, lab.findFastestRoute("FrankfurtamMain", "Brunswick"), route, "TestFile3", + "Goeteborg", "Aachen", AllTests.OutputFormat.RouteTime), + "Route not correct (wrong bold edge present)"); + }); + } + +} diff --git a/Dijkstra/src/frame/TestFile4TestCases.java b/Dijkstra/src/frame/TestFile4TestCases.java new file mode 100644 index 0000000..7c7986c --- /dev/null +++ b/Dijkstra/src/frame/TestFile4TestCases.java @@ -0,0 +1,96 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile4TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void testFile4_A_I_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(9, AllTests.testDistance("TestFile4", "A", "I"), "From A to I: "); + }); + } + + @Test + public void testFile4_B_H_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(12, AllTests.testDistance("TestFile4", "B", "H"), "From B to H: "); + }); + } + + @Test + public void testFile4_C_G_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(12, AllTests.testDistance("TestFile4", "C", "G"), "From C to G: "); + }); + } + + @Test + public void testFile4_A_I_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(9, AllTests.testTime("TestFile4", "A", "I"), "From A to I: "); + }); + } + + @Test + public void testFile4_B_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(13, AllTests.testTime("TestFile4", "B", "F"), "From B to H: "); + }); + } + + @Test + public void testFile4_B_H_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(20, AllTests.testTime("TestFile4", "B", "H"), "From B to H: "); + }); + } + + @Test + public void testFile4_C_G_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(15, AllTests.testTime("TestFile4", "C", "G"), "From C to G: "); + }); + } + + @Test + public void testfile4_Route_A_E_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile4"); + // Build the route we expect to find + route.add("A\\s*->\\s*B"); + route.add("B\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*E"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("A", "E"), route, "testfile1.txt", "A", "E", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + + @Test + public void testfile4_Route_B_F_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile4"); + // Build the route we expect to find + route.add("B\\s*->\\s*C"); + route.add("C\\s*->\\s*D"); + route.add("D\\s*->\\s*F"); + assertTrue(AllTests.testRoute(true, lab.findFastestRoute("B", "F"), route, "TestFile4", "B", "F", + AllTests.OutputFormat.RouteTime), "Route not correct"); + }); + } + +} diff --git a/Dijkstra/src/frame/TestFile5TestCases.java b/Dijkstra/src/frame/TestFile5TestCases.java new file mode 100644 index 0000000..04bb400 --- /dev/null +++ b/Dijkstra/src/frame/TestFile5TestCases.java @@ -0,0 +1,61 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile5TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void testFile5_A_F_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(-4, AllTests.testDistance("TestFile5", "A", "F"), "From A to F: "); + }); + } + + @Test + public void testfile5_F_C_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(144, AllTests.testDistance("TestFile5", "F", "C"), "From F to C: "); + }); + } + + @Test + public void testFile5_A_F_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(-4, AllTests.testTime("TestFile5", "A", "F"), "From A to F: "); + }); + } + + @Test + public void testfile5_F_C_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(151, AllTests.testTime("TestFile5", "F", "C"), "From F to C: "); + }); + } + + @Test + public void testfile5_Route_F_C_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile5"); + // Build the route we expect to find + route.add("F\\s*->\\s*A"); + route.add("A\\s*->\\s*B"); + route.add("B\\s*->\\s*D"); + route.add("D\\s*->\\s*C"); + assertTrue(AllTests.testRoute(true, lab.findShortestRoute("F", "C"), route, "TestFile5", "F", "C", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + +} diff --git a/Dijkstra/src/frame/TestFile6TestCases.java b/Dijkstra/src/frame/TestFile6TestCases.java new file mode 100644 index 0000000..e765a00 --- /dev/null +++ b/Dijkstra/src/frame/TestFile6TestCases.java @@ -0,0 +1,71 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import lab.Navigation; + +public class TestFile6TestCases { + + private Duration timeout = AllTests.timeout; + + @Test + public void TestFile6_Aachen_Goeteborg_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(-4, + AllTests.testDistance("TestFile6", "Aachen", + "Goeteborg"), "From Aachen to Goeteborg: "); + }); + } + + @Test + public void TestFile6_Goeteborg_Bamberg_Distance() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(144, + AllTests.testDistance("TestFile6", + "Goeteborg", "Bamberg"), "From Goeteborg to Bamberg: "); + }); + } + + @Test + public void TestFile6_Aachen_Goeteborg_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(-4, + AllTests.testTime("TestFile6", "Aachen", + "Goeteborg"), "From Aachen to Goeteborg: "); + }); + } + + @Test + public void TestFile6_Goeteborg_Bamberg_Time() { + assertTimeoutPreemptively(timeout, () -> { + assertEquals(151, + AllTests.testTime("TestFile6", "Goeteborg", + "Bamberg"), "From Goeteborg to Bamberg: "); + }); + } + + @Test + public void TestFile6_Route_Goeteborg_Bamberg_Distance() { + assertTimeoutPreemptively(timeout, () -> { + ArrayList route = new ArrayList(); + Navigation lab = new Navigation("TestFile6"); + // Build the route we expect to find + route.add("Goeteborg\\s*->\\s*Aachen"); + route.add("Aachen\\s*->\\s*Augsburg"); + route.add("Augsburg\\s*->\\s*Berlin"); + route.add("Berlin\\s*->\\s*Bamberg"); + assertTrue(AllTests.testRoute(true, + lab.findShortestRoute("Goeteborg", "Bamberg"), route, + "TestFile6", "Goeteborg", "Bamberg", + AllTests.OutputFormat.RouteDistance), "Route not correct"); + }); + } + +}