Browse Source

Added Junit tests

Adrien Barbanson 2 months ago
parent
commit
be0cd90c69

+ 20
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarAlgorithmTests.java View File

@@ -0,0 +1,20 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
4
+import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
5
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
6
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
7
+
8
+public class AstarAlgorithmTests extends ShortestPathTests{
9
+	
10
+	@Override
11
+	protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) {
12
+		return new BellmanFordAlgorithm(data);
13
+	}
14
+
15
+	@Override
16
+	protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) {
17
+		return new AStarAlgorithm(data);
18
+	}
19
+	
20
+}

+ 22
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraAlgorithmTests.java View File

@@ -0,0 +1,22 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
4
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
5
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
6
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
7
+
8
+public class DijkstraAlgorithmTests extends ShortestPathTests{
9
+
10
+	@Override
11
+	protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) {
12
+		return new BellmanFordAlgorithm(data);
13
+	}
14
+
15
+	@Override
16
+	protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) {
17
+		return new DijkstraAlgorithm(data);
18
+	}
19
+	
20
+	
21
+
22
+}

+ 147
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/ShortestPathTests.java View File

@@ -0,0 +1,147 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+import static org.junit.Assert.assertTrue;
5
+
6
+import java.io.BufferedInputStream;
7
+import java.io.DataInputStream;
8
+import java.io.FileInputStream;
9
+import java.io.FileNotFoundException;
10
+import java.io.IOException;
11
+import java.util.List;
12
+import java.util.Random;
13
+
14
+import org.insa.graphs.algorithm.ArcInspector;
15
+import org.insa.graphs.algorithm.ArcInspectorFactory;
16
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
17
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
18
+import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
19
+import org.insa.graphs.model.Graph;
20
+import org.insa.graphs.model.Node;
21
+import org.insa.graphs.model.io.BinaryGraphReader;
22
+import org.insa.graphs.model.io.GraphReader;
23
+import org.junit.Before;
24
+import org.junit.Test;
25
+
26
+
27
+public abstract class ShortestPathTests {
28
+	
29
+	
30
+	Graph testGraph;
31
+	
32
+	//oracle is the algorithm that has authority to determine the shortestPath
33
+	protected abstract ShortestPathAlgorithm getOracleAlgo(ShortestPathData data);
34
+	
35
+	protected abstract ShortestPathAlgorithm  getTesterAlgo(ShortestPathData data);
36
+	
37
+
38
+	class Journey {
39
+		Node start, end;
40
+		
41
+		Journey(Node start, Node end){
42
+			this.start = start;
43
+			this.end = end;
44
+		}
45
+		
46
+		public Node getStart() {
47
+			return this.start;
48
+		}
49
+		
50
+		public Node getEnd() {
51
+			return this.end;
52
+		}
53
+	}
54
+	
55
+    @Before
56
+    public void init() throws IOException {
57
+    	// we read a graph
58
+    	String testMap = "/home/adrien/git/be-graphes/maps/haute-garonne.mapgr";
59
+    	GraphReader readerMap = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(testMap))));
60
+    	this.testGraph = readerMap.read();
61
+    	readerMap.close();
62
+    }
63
+    
64
+    /**
65
+     * Method to get some pair of random point in the graph in order to find a path between them.
66
+     * @param quantity the number of random pair to generate in the graph
67
+     * @return Journey a Journey array object that contain start and end Node.
68
+     */
69
+    protected Journey[] getRandomJourneys(int quantity) {
70
+    	
71
+    	Random rand = new Random();
72
+    	
73
+    	Journey [] allJourneys = new Journey[quantity];
74
+    	
75
+    	List<Node> nodes = testGraph.getNodes();
76
+    	int size = nodes.size(); 
77
+    	
78
+    	for(int i=0; i < quantity; i++) {
79
+    		Node start = nodes.get(rand.nextInt(size));
80
+    		Node end;
81
+    		
82
+    		do {
83
+    			end   = nodes.get(rand.nextInt(size));
84
+    		} while(start.equals(end));
85
+    		
86
+    		allJourneys[i] = new Journey(start, end);
87
+    		
88
+    	}
89
+    	
90
+    	return allJourneys;
91
+    	
92
+    }
93
+    
94
+    protected void compareAlgorithm(int numberOfRandomTests, ArcInspector mode) {    	
95
+    	// We are determining 10 random start/end points on the graph
96
+    	Journey[] allJourneys = getRandomJourneys(numberOfRandomTests);
97
+    	 
98
+    	// We now ask to oracle the best path time based
99
+    	ShortestPathSolution oracleSolutions[] = new ShortestPathSolution[numberOfRandomTests];
100
+    	
101
+    	ShortestPathSolution testAlgoSolutions[] = new ShortestPathSolution[numberOfRandomTests];
102
+
103
+    	
104
+    	for(int i = 0 ; i < numberOfRandomTests ; i++) {
105
+    		System.out.println("Running test n°" + i + " mode = " + mode.toString());
106
+    		ShortestPathData parameters = new ShortestPathData(testGraph, 
107
+    				allJourneys[i].getStart(), allJourneys[i].getEnd(), mode);
108
+    		oracleSolutions[i] = getOracleAlgo(parameters).run();
109
+    		testAlgoSolutions[i] = getTesterAlgo(parameters).run();
110
+    	}
111
+    	
112
+    	// Now we have all the journey, let's compare.
113
+    	for(int i = 0 ; i < numberOfRandomTests ; i++) {
114
+    		/**System.out.println("Comparing solution " + i + "...");
115
+    		System.out.println("===================");
116
+    		System.out.println("Oracle says : ");
117
+    		System.out.println(oracleSolutions[i]);
118
+    		System.out.println("===================");
119
+    		System.out.println("Test algo says : ");
120
+    		System.out.println(testAlgoSolutions[i]);
121
+    		System.out.println("===================");**/
122
+    		
123
+    		if(!oracleSolutions[i].isFeasible()) {
124
+    			assertTrue(!testAlgoSolutions[i].isFeasible());
125
+    		}
126
+    		else {
127
+        		assertTrue(oracleSolutions[i].getPath().isSamePath(testAlgoSolutions[i].getPath()));
128
+    		}
129
+    		
130
+    	}
131
+    }
132
+    
133
+    
134
+    @Test
135
+    public void testShortestPathAlgorithmTimeBasedRandom() {
136
+    	compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(1));
137
+    }
138
+    
139
+    @Test
140
+    public void testShortestPathAlgorithmLengthBasedRandom() {
141
+    	compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(2));
142
+    }
143
+    
144
+    
145
+
146
+	
147
+}

Loading…
Cancel
Save