From 5c248f26dcb3d18868017d240170d269e71e1671 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Mon, 20 May 2024 22:21:34 +0200 Subject: [PATCH] refactor(test): dijkstra bellman assertion --- .../shortestpath/DijkstraAlgorithmTest.java | 71 +++++++------------ 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java index bd9586f..e218a2b 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java @@ -53,7 +53,7 @@ public class DijkstraAlgorithmTest { // Create a graph reader final GraphReader reader = new BinaryGraphReader( new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); - // Read the graph. X + // Read the graph. graph.add(reader.read()); // free resources reader.close(); @@ -74,6 +74,24 @@ public class DijkstraAlgorithmTest { * Chemins courts testés par comparaison avec Bellman. * Chemin longs testés par comparaison avec chemins déjà construits. Bellman trop long. */ + + /* + * Verifies that path is valid and mathes the one found by the Bellman algo + */ + private void assertBellmanHasSameResult(Graph graph, Node origin, Node destination, ArcInspector arcFilter) { + final ShortestPathData data = new ShortestPathData(graph, origin, destination, arcFilter); + + final DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + final ShortestPathSolution dijk_path = dijkstra.doRun(); + + final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); + final ShortestPathSolution bell_path = bellman.doRun(); + + assert(dijk_path.getPath().isValid()); + assert(dijk_path.isFeasible()); + assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0); + assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + } @Test /* @@ -84,22 +102,12 @@ public class DijkstraAlgorithmTest { * PATH UTILISE : ../Paths/custom_paths/short_path_carre.path */ public void chemin_court_CARRE_length() { - ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs Graph myGraph = graph.get(0); Node origin = myGraph.get(19); Node destination = myGraph.get(4); - ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); - - DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); - ShortestPathSolution dijk_path = dijkstra.doRun(); - BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); - ShortestPathSolution bell_path = bellman.doRun(); - - assert(dijk_path.getPath().isValid()); - assert(dijk_path.isFeasible()); - assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0); - assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); } @Test @@ -115,18 +123,7 @@ public class DijkstraAlgorithmTest { Node origin = myGraph.get(15); Node destination = myGraph.get(9); - ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); - - DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); - ShortestPathSolution dijk_path = dijkstra.doRun(); - - BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); - ShortestPathSolution bell_path = bellman.doRun(); - - assert(dijk_path.getPath().isValid()); - assert(dijk_path.isFeasible()); - assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0); - assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); } @@ -184,18 +181,8 @@ public class DijkstraAlgorithmTest { Graph myGraph = graph.get(2); Node origin = myGraph.get(8423); Node destination = myGraph.get(8435); - ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); - - DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); - ShortestPathSolution dijk_path = dijkstra.doRun(); - BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); - ShortestPathSolution bell_path = bellman.doRun(); - - assert(dijk_path.getPath().isValid()); - assert(dijk_path.isFeasible()); - assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0); - assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); } @Test @@ -215,18 +202,8 @@ public class DijkstraAlgorithmTest { Graph myGraph = graph.get(2); Node origin = myGraph.get(16644); Node destination = myGraph.get(39229); - ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); - - DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); - ShortestPathSolution dijk_path = dijkstra.doRun(); - BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); - ShortestPathSolution bell_path = bellman.doRun(); - - assert(dijk_path.getPath().isValid()); - assert(dijk_path.isFeasible()); - assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0)); - assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 10.0); + assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); }