From be0cd90c69f968c9084c4293019363026e637192 Mon Sep 17 00:00:00 2001 From: Adrien Barbanson Date: Tue, 18 May 2021 18:43:22 +0200 Subject: [PATCH] Added Junit tests --- .../algorithm/utils/AstarAlgorithmTests.java | 20 +++ .../utils/DijkstraAlgorithmTests.java | 22 +++ .../algorithm/utils/ShortestPathTests.java | 147 ++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarAlgorithmTests.java create mode 100644 be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraAlgorithmTests.java create mode 100644 be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTests.java diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarAlgorithmTests.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarAlgorithmTests.java new file mode 100644 index 0000000..93bc4fc --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarAlgorithmTests.java @@ -0,0 +1,20 @@ +package org.insa.graphs.algorithm.utils; + +import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; +import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; + +public class AstarAlgorithmTests extends ShortestPathTests{ + + @Override + protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) { + return new BellmanFordAlgorithm(data); + } + + @Override + protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) { + return new AStarAlgorithm(data); + } + +} diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraAlgorithmTests.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraAlgorithmTests.java new file mode 100644 index 0000000..f3dbebb --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraAlgorithmTests.java @@ -0,0 +1,22 @@ +package org.insa.graphs.algorithm.utils; + +import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; +import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; + +public class DijkstraAlgorithmTests extends ShortestPathTests{ + + @Override + protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) { + return new BellmanFordAlgorithm(data); + } + + @Override + protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) { + return new DijkstraAlgorithm(data); + } + + + +} diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTests.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTests.java new file mode 100644 index 0000000..577687d --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTests.java @@ -0,0 +1,147 @@ +package org.insa.graphs.algorithm.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Random; + +import org.insa.graphs.algorithm.ArcInspector; +import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; +import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.io.BinaryGraphReader; +import org.insa.graphs.model.io.GraphReader; +import org.junit.Before; +import org.junit.Test; + + +public abstract class ShortestPathTests { + + + Graph testGraph; + + //oracle is the algorithm that has authority to determine the shortestPath + protected abstract ShortestPathAlgorithm getOracleAlgo(ShortestPathData data); + + protected abstract ShortestPathAlgorithm getTesterAlgo(ShortestPathData data); + + + class Journey { + Node start, end; + + Journey(Node start, Node end){ + this.start = start; + this.end = end; + } + + public Node getStart() { + return this.start; + } + + public Node getEnd() { + return this.end; + } + } + + @Before + public void init() throws IOException { + // we read a graph + String testMap = "/home/adrien/git/be-graphes/maps/haute-garonne.mapgr"; + GraphReader readerMap = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(testMap)))); + this.testGraph = readerMap.read(); + readerMap.close(); + } + + /** + * Method to get some pair of random point in the graph in order to find a path between them. + * @param quantity the number of random pair to generate in the graph + * @return Journey a Journey array object that contain start and end Node. + */ + protected Journey[] getRandomJourneys(int quantity) { + + Random rand = new Random(); + + Journey [] allJourneys = new Journey[quantity]; + + List nodes = testGraph.getNodes(); + int size = nodes.size(); + + for(int i=0; i < quantity; i++) { + Node start = nodes.get(rand.nextInt(size)); + Node end; + + do { + end = nodes.get(rand.nextInt(size)); + } while(start.equals(end)); + + allJourneys[i] = new Journey(start, end); + + } + + return allJourneys; + + } + + protected void compareAlgorithm(int numberOfRandomTests, ArcInspector mode) { + // We are determining 10 random start/end points on the graph + Journey[] allJourneys = getRandomJourneys(numberOfRandomTests); + + // We now ask to oracle the best path time based + ShortestPathSolution oracleSolutions[] = new ShortestPathSolution[numberOfRandomTests]; + + ShortestPathSolution testAlgoSolutions[] = new ShortestPathSolution[numberOfRandomTests]; + + + for(int i = 0 ; i < numberOfRandomTests ; i++) { + System.out.println("Running test n°" + i + " mode = " + mode.toString()); + ShortestPathData parameters = new ShortestPathData(testGraph, + allJourneys[i].getStart(), allJourneys[i].getEnd(), mode); + oracleSolutions[i] = getOracleAlgo(parameters).run(); + testAlgoSolutions[i] = getTesterAlgo(parameters).run(); + } + + // Now we have all the journey, let's compare. + for(int i = 0 ; i < numberOfRandomTests ; i++) { + /**System.out.println("Comparing solution " + i + "..."); + System.out.println("==================="); + System.out.println("Oracle says : "); + System.out.println(oracleSolutions[i]); + System.out.println("==================="); + System.out.println("Test algo says : "); + System.out.println(testAlgoSolutions[i]); + System.out.println("===================");**/ + + if(!oracleSolutions[i].isFeasible()) { + assertTrue(!testAlgoSolutions[i].isFeasible()); + } + else { + assertTrue(oracleSolutions[i].getPath().isSamePath(testAlgoSolutions[i].getPath())); + } + + } + } + + + @Test + public void testShortestPathAlgorithmTimeBasedRandom() { + compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(1)); + } + + @Test + public void testShortestPathAlgorithmLengthBasedRandom() { + compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(2)); + } + + + + +}