Better Dijkstra tests
This commit is contained in:
parent
7776e34f92
commit
3076698958
2 changed files with 82 additions and 43 deletions
|
@ -11,7 +11,7 @@ import java.io.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
public class DijkstraAlgorithmTest {
|
public class DijkstraAlgorithmTest {
|
||||||
|
@ -20,6 +20,20 @@ public class DijkstraAlgorithmTest {
|
||||||
"/home/yohan/Documents/etudes/be-graphes/maps/carre.mapgr",
|
"/home/yohan/Documents/etudes/be-graphes/maps/carre.mapgr",
|
||||||
"/home/yohan/Documents/etudes/be-graphes/maps/insa.mapgr",
|
"/home/yohan/Documents/etudes/be-graphes/maps/insa.mapgr",
|
||||||
"/home/yohan/Documents/etudes/be-graphes/maps/toulouse.mapgr",
|
"/home/yohan/Documents/etudes/be-graphes/maps/toulouse.mapgr",
|
||||||
|
"/home/yohan/Documents/etudes/be-graphes/maps/guadeloupe.mapgr",
|
||||||
|
};
|
||||||
|
|
||||||
|
int[] origIndexes = {
|
||||||
|
22,
|
||||||
|
553,
|
||||||
|
25643,
|
||||||
|
6187,
|
||||||
|
};
|
||||||
|
int[] destIndexes = {
|
||||||
|
15,
|
||||||
|
111,
|
||||||
|
17402,
|
||||||
|
15025,
|
||||||
};
|
};
|
||||||
|
|
||||||
ArcInspector[] arcInspectors = {
|
ArcInspector[] arcInspectors = {
|
||||||
|
@ -27,48 +41,49 @@ public class DijkstraAlgorithmTest {
|
||||||
ArcInspectorFactory.getAllFilters().get(2),
|
ArcInspectorFactory.getAllFilters().get(2),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private ShortestPathSolution runDijkstra(String map, ArcInspector arcInspector, int origIndex, int destIndex) throws IOException {
|
||||||
@Test
|
try (GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(map))))) {
|
||||||
public void testDoRun() {
|
Graph graph = reader.read();
|
||||||
Random random = new Random();
|
List<Node> nodes = graph.getNodes();
|
||||||
for (String map : maps) {
|
Node orig = nodes.get(origIndex);
|
||||||
try (GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(
|
Node dest = nodes.get(destIndex);
|
||||||
new FileInputStream(map))))) {
|
ShortestPathData data = new ShortestPathData(graph, orig, dest, arcInspector);
|
||||||
Graph graph = reader.read();
|
return new DijkstraAlgorithm(data).doRun();
|
||||||
List<Node> nodes = graph.getNodes();
|
|
||||||
|
|
||||||
Node orig;
|
|
||||||
Node dest;
|
|
||||||
|
|
||||||
// Test random paths
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
orig = nodes.get(random.nextInt(nodes.size()));
|
|
||||||
do {
|
|
||||||
dest = nodes.get(random.nextInt(nodes.size()));
|
|
||||||
} while (orig == dest);
|
|
||||||
for (ArcInspector arcInspector : arcInspectors) {
|
|
||||||
ShortestPathData data = new ShortestPathData(graph, orig, dest, arcInspector);
|
|
||||||
ShortestPathSolution sol = new DijkstraAlgorithm(data).doRun();
|
|
||||||
if (sol.isFeasible())
|
|
||||||
assertTrue(sol.getPath().isValid());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test zero-length path
|
|
||||||
orig = nodes.get(random.nextInt(nodes.size()));
|
|
||||||
dest = orig;
|
|
||||||
for (ArcInspector arcInspector : arcInspectors) {
|
|
||||||
ShortestPathData data = new ShortestPathData(graph, orig, dest, arcInspector);
|
|
||||||
ShortestPathSolution sol = new DijkstraAlgorithm(data).doRun();
|
|
||||||
if (sol.isFeasible())
|
|
||||||
assertTrue(sol.getPath().isValid());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDoRun() throws IOException {
|
||||||
|
// test feasible paths
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (ArcInspector arcInspector : arcInspectors) {
|
||||||
|
ShortestPathSolution sol = runDijkstra(maps[i], arcInspector, origIndexes[i], destIndexes[i]);
|
||||||
|
assertTrue(sol.isFeasible());
|
||||||
|
Path path = sol.getPath();
|
||||||
|
assertTrue(path.isValid());
|
||||||
|
if (i == 0)
|
||||||
|
assertEquals(path, Path.createShortestPathFromNodes(sol.getInputData().getGraph(), path.getNodes()));
|
||||||
|
if (i == 1)
|
||||||
|
assertEquals(path, Path.createFastestPathFromNodes(sol.getInputData().getGraph(), path.getNodes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test 0-length paths
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (ArcInspector arcInspector : arcInspectors) {
|
||||||
|
ShortestPathSolution sol = runDijkstra(maps[i], arcInspector, origIndexes[i], origIndexes[i]);
|
||||||
|
assertTrue(sol.isFeasible());
|
||||||
|
assertTrue(sol.getPath().isValid());
|
||||||
|
assertEquals(sol.getPath().getLength(), 0, 1e-5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test impossible path
|
||||||
|
for (ArcInspector arcInspector : arcInspectors) {
|
||||||
|
ShortestPathSolution sol = runDijkstra(maps[3], arcInspector, origIndexes[3], destIndexes[3]);
|
||||||
|
assertFalse(sol.isFeasible());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -288,4 +288,28 @@ public class Path {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Node> getNodes() {
|
||||||
|
List<Node> nodes = new ArrayList<>();
|
||||||
|
if (origin != null)
|
||||||
|
nodes.add(origin);
|
||||||
|
for (Arc arc : arcs) {
|
||||||
|
nodes.add(arc.getDestination());
|
||||||
|
}
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the arcs in this path are the same as arcs in obj
|
||||||
|
* Warning: shallow comparison for the arcs
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj.getClass() != Path.class)
|
||||||
|
return false;
|
||||||
|
for (int i = 0; i < arcs.size(); i++) {
|
||||||
|
if (arcs.get(i) != ((Path)obj).arcs.get(i))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue