From cb5f211cee53439340930a6f2b7bf45c7a9062af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yanis=20Mah=C3=A9?= Date: Tue, 19 May 2026 15:59:15 +0200 Subject: [PATCH] Test for A* --- .../algorithm/utils/ShortestPathTest.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTest.java index 8ca1d79..b26c67d 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTest.java @@ -12,6 +12,7 @@ import java.util.List; import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm; @@ -84,18 +85,23 @@ public class ShortestPathTest { // System.out.println("" + insaBikiniCanal.getOrigin().getId() +" "+ insaBikiniCanal.getDestination().getId()); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(insaBikiniCanalPathData); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(insaBikiniCanalPathData); + ShortestPathAlgorithm aStar = new AStarAlgorithm(insaBikiniCanalPathData); ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionAStar = aStar.run(); assertEquals(Status.OPTIMAL, solutionBellmanFord.getStatus()); assertEquals(Status.OPTIMAL, solutionDijkstra.getStatus()); + assertEquals(Status.OPTIMAL, solutionAStar.getStatus()); assertEquals(insaBikiniCanal.getLength(), solutionBellmanFord.getPath().getLength(),1.0); assertEquals(insaBikiniCanal.getLength(), solutionDijkstra.getPath().getLength(), 1.0); + assertEquals(insaBikiniCanal.getLength(), solutionAStar.getPath().getLength(), 1.0); assertEquals(insaBikiniCanal.getTravelTime(120), solutionBellmanFord.getPath().getTravelTime(120),1.0); assertEquals(insaBikiniCanal.getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0); + assertEquals(insaBikiniCanal.getTravelTime(120), solutionAStar.getPath().getTravelTime(120), 1.0); } @Test @@ -103,41 +109,54 @@ public class ShortestPathTest { ShortestPathData cheminInexistant = new ShortestPathData(insa, insa.get(940), insa.get(702), onlyCarsByLengthArcInspector); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); + ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminInexistant); ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionAStar = aStar.run(); assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus()); assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus()); + assertEquals(Status.INFEASIBLE, solutionAStar.getStatus()); } @Test public void testCheminLongueurNulle() { - ShortestPathData cheminInexistant = new ShortestPathData(insa, insa.get(940), insa.get(940), noFilterByLengthArcInspector); - ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); - ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); + ShortestPathData cheminNul = new ShortestPathData(insa, insa.get(940), insa.get(940), noFilterByLengthArcInspector); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminNul); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminNul); + ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminNul); ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionAStar = aStar.run(); assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus()); assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus()); + assertEquals(Status.INFEASIBLE, solutionAStar.getStatus()); assertNull(solutionBellmanFord.getPath()); assertNull(solutionDijkstra.getPath()); + assertNull(solutionAStar.getPath()); } @Test public void testCheminLong() { // En comparant les deux algos - ShortestPathData cheminInexistant = new ShortestPathData(belgique, belgique.get(956754), belgique.get(842938), noFilterByLengthArcInspector); - ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); - ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); + ShortestPathData cheminLong = new ShortestPathData(belgique, belgique.get(956754), belgique.get(842938), noFilterByLengthArcInspector); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminLong); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminLong); + ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminLong); ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionAStar = aStar.run(); assertEquals(solutionBellmanFord.getStatus(), solutionDijkstra.getStatus()); assertEquals(solutionBellmanFord.getPath().getLength(), solutionDijkstra.getPath().getLength(), 1.0); assertEquals(solutionBellmanFord.getPath().getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0); + + assertEquals(solutionAStar.getStatus(), solutionDijkstra.getStatus()); + assertEquals(solutionAStar.getPath().getLength(), solutionDijkstra.getPath().getLength(), 1.0); + assertEquals(solutionAStar.getPath().getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0); } }