Skip to content

Commit 7ce3224

Browse files
author
Jegors Čemisovs
committed
Add DijkstrasAlgorithmTest
1 parent 1c691c3 commit 7ce3224

11 files changed

Lines changed: 426 additions & 134 deletions

algorithm/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ publishing {
4646
fromResolutionOf('runtimeClasspath')
4747
}
4848
usage('java-runtime') {
49-
fromResolutionResult()
49+
fromResolutionResult(null)
5050
}
5151
}
5252
}
@@ -65,6 +65,9 @@ dependencies {
6565
// Required for spock-reports
6666
testImplementation 'org.slf4j:slf4j-api:2.0.1'
6767
testRuntimeClasspath 'org.slf4j:slf4j-simple:2.0.1'
68+
69+
// JUnit 5 Parameterized Tests
70+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
6871
}
6972

7073
test {

algorithm/src/test/groovy/lv/id/jc/algorithm/graph/DijkstrasAlgorithmSpec.groovy

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import spock.lang.*
44

55
@Title("Dijkstra's Algorithm")
66
@See("https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm")
7-
@Narrative("Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph")
7+
@Narrative("""
8+
Dijkstra's algorithm is an algorithm for finding
9+
the fastest paths between nodes in a graph
10+
""")
811
class DijkstrasAlgorithmSpec extends Specification {
12+
913
@Subject
1014
def algorithm = new DijkstrasAlgorithm()
1115

@@ -19,19 +23,17 @@ class DijkstrasAlgorithmSpec extends Specification {
1923

2024
when:
2125
def path = algorithm.findPath(graph, source, target)
26+
def time = graph.getDistance(path)
2227

2328
then:
24-
path == fastest
25-
26-
and:
27-
graph.getDistance(path) == time as double
29+
path == fastestPath
30+
time == fastestTime
2831

2932
where:
30-
source | target || time | fastest
31-
'A' | 'A' || 0 | ['A']
32-
'B' | 'B' || 0 | ['B']
33-
'C' | 'C' || 0 | ['C']
34-
'A' | 'B' || 5 | ['A', 'C', 'B']
33+
source | target || fastestPath | fastestTime
34+
'A' | 'A' || ['A'] | 0
35+
'B' | 'A' || ['B', 'A'] | 3
36+
'A' | 'B' || ['A', 'C', 'B'] | 5
3537
}
3638

3739
def 'should find a route for a medium graph'() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package lv.id.jc.algorithm.graph;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.*;
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
// Java 8 + JUnit 5 + Parameterized Test
14+
class DijkstrasAlgorithmTest {
15+
16+
// Subject under test
17+
SearchAlgorithm<String> algorithm = new DijkstrasAlgorithm<>();
18+
19+
// Sample data
20+
private static Stream<Arguments> providePathTime() {
21+
return Stream.of(
22+
Arguments.of("A", "A", Collections.singletonList("A"), 0),
23+
Arguments.of("B", "A", Arrays.asList("B", "A"), 3),
24+
Arguments.of("A", "B", Arrays.asList("A", "C", "B"), 5)
25+
);
26+
}
27+
28+
@ParameterizedTest
29+
@MethodSource("providePathTime")
30+
@DisplayName("should find a route for a simple graph")
31+
void testRouteForSimpleGraph(String source, String target, List<String> fastestPath, double fastestTime) {
32+
// given
33+
Map<String, Number> fromA = new HashMap<>();
34+
fromA.put("B", 7);
35+
fromA.put("C", 2);
36+
Map<String, Number> fromB = new HashMap<>();
37+
fromB.put("A", 3);
38+
fromB.put("C", 5);
39+
Map<String, Number> fromC = new HashMap<>();
40+
fromC.put("A", 1);
41+
fromC.put("B", 3);
42+
Map<String, Map<String, Number>> nodes = new HashMap<>();
43+
nodes.put("A", fromA);
44+
nodes.put("B", fromB);
45+
nodes.put("C", fromC);
46+
Graph<String> graph = Graph.of(nodes);
47+
48+
// when
49+
List<String> path = algorithm.findPath(graph, source, target);
50+
double time = graph.getDistance(path);
51+
52+
// then
53+
assertEquals(fastestPath, path);
54+
assertEquals(fastestTime, time);
55+
}
56+
}

algorithm/src/test/java/lv/id/jc/algorithm/graph/SearchAlgorithmsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void tearDown() {
6767
void testFindPathAA() {
6868
// Java 8
6969

70+
// given
7071
String source = "A";
7172
String target = "A";
7273

@@ -76,9 +77,11 @@ void testFindPathAA() {
7677
List<String> fastest = new ArrayList<>();
7778
fastest.add("A");
7879

80+
// when
7981
List<String> routeOne = bfsAlgorithm.findPath(graph, source, target);
8082
List<String> routeTwo = dijkstras.findPath(graph, source, target);
8183

84+
// then
8285
assertEquals(shortest, routeOne);
8386
assertEquals(fastest, routeTwo);
8487

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"lv.id.jc.algorithm.graph.SearchAlgorithmsSpec":{"executedFeatures":["should find a route for a complex graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":1,"totalFeatures":1,"passed":1,"successRate":1.0,"time":46},"title":"Comparison of two algorithms","narrative":""},"lv.id.jc.algorithm.graph.BreadthFirstSearchSpec":{"executedFeatures":["should find a route for complex graph","should find a route for simple graph","should return an empty path if can't find a route","should thrown NPE path for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":4,"totalFeatures":4,"passed":4,"successRate":1.0,"time":16},"title":"Breadth First Search Algorithm","narrative":"Breadth First Search algorithm for finding the shortest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":45},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.GraphSpec":{"executedFeatures":["should be zero distance for an empty path","should be zero distance for any one node path","should calculate distance for a path","should return edges for a given node","should throw NPE for incorrect path"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":47},"title":"Generic Graph","narrative":"A generic implementation of Graph structure"}}
1+
{"lv.id.jc.algorithm.graph.SearchAlgorithmsSpec":{"executedFeatures":["should find a route for a complex graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":1,"totalFeatures":1,"passed":1,"successRate":1.0,"time":46},"title":"Comparison of two algorithms","narrative":""},"lv.id.jc.algorithm.graph.BreadthFirstSearchSpec":{"executedFeatures":["should find a route for complex graph","should find a route for simple graph","should return an empty path if can't find a route","should thrown NPE path for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":4,"totalFeatures":4,"passed":4,"successRate":1.0,"time":16},"title":"Breadth First Search Algorithm","narrative":"Breadth First Search algorithm for finding the shortest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":45},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.GraphSpec":{"executedFeatures":["should be zero distance for an empty path","should be zero distance for any one node path","should calculate distance for a path","should return edges for a given node","should throw NPE for incorrect path"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":47},"title":"Generic Graph","narrative":"A generic implementation of Graph structure"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":46},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":1,"totalFeatures":5,"passed":1,"successRate":1.0,"time":29},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":1,"totalFeatures":5,"passed":1,"successRate":1.0,"time":32},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.BreadthFirstSearchSpec":{"executedFeatures":["should find a route for complex graph","should find a route for simple graph","should return an empty path if can't find a route","should thrown an exception for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":4,"totalFeatures":4,"passed":4,"successRate":1.0,"time":37},"title":"Breadth First Search Algorithm","narrative":"Breadth First Search algorithm for finding the shortest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":15},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding the fastest paths between nodes in a graph"},"lv.id.jc.algorithm.graph.GraphSpec":{"executedFeatures":["should be zero distance for an empty path","should be zero distance for any one node path","should calculate distance for a path","should return edges for a given node","should throw NPE for incorrect path"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":32},"title":"Generic Graph","narrative":"A generic implementation of Graph structure"},"lv.id.jc.algorithm.graph.SearchAlgorithmsSpec":{"executedFeatures":["should find a route for a complex graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":1,"totalFeatures":1,"passed":1,"successRate":1.0,"time":5},"title":"Comparison of two algorithms","narrative":""},"lv.id.jc.algorithm.graph.DijkstrasAlgorithmSpec":{"executedFeatures":["should find a route for a complex graph","should find a route for a medium graph","should find a route for a simple graph","should return an empty path if can't find a route","should thrown NPE for an empty graph"],"ignoredFeatures":[],"stats":{"failures":0,"errors":0,"skipped":0,"totalRuns":5,"totalFeatures":5,"passed":5,"successRate":1.0,"time":44},"title":"Dijkstra's Algorithm","narrative":"Dijkstra's algorithm is an algorithm for finding \nthe fastest paths between nodes in a graph"}}

docs/spock-reports/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ <h2>Specification run results</h2>
8484
</div>
8585
<div class='summary-report'>
8686
<h3>Specifications summary:</h3>
87-
<div class='date-test-ran'>Created on Tue Jan 11 09:12:51 EET 2022 by jegors.cemisovs</div>
87+
<div class='date-test-ran'>Created on Sun Oct 16 13:46:14 EEST 2022 by jegors</div>
8888
<table class='summary-table'>
8989
<thead>
9090
<tr>
@@ -111,7 +111,7 @@ <h3>Specifications summary:</h3>
111111
<td>0</td>
112112
<td>0</td>
113113
<td>100.0%</td>
114-
<td>0.154 seconds</td>
114+
<td>0.118 seconds</td>
115115
</tr>
116116
</tbody>
117117
</table>
@@ -142,7 +142,7 @@ <h3>Specifications:</h3>
142142
<td>0</td>
143143
<td>0</td>
144144
<td>100.0%</td>
145-
<td>0.016 seconds</td>
145+
<td>0.037 seconds</td>
146146
</tr>
147147
<tr>
148148
<td>
@@ -155,7 +155,7 @@ <h3>Specifications:</h3>
155155
<td>0</td>
156156
<td>0</td>
157157
<td>100.0%</td>
158-
<td>0.045 seconds</td>
158+
<td>0.044 seconds</td>
159159
</tr>
160160
<tr>
161161
<td>
@@ -168,7 +168,7 @@ <h3>Specifications:</h3>
168168
<td>0</td>
169169
<td>0</td>
170170
<td>100.0%</td>
171-
<td>0.047 seconds</td>
171+
<td>0.032 seconds</td>
172172
</tr>
173173
<tr>
174174
<td>
@@ -181,7 +181,7 @@ <h3>Specifications:</h3>
181181
<td>0</td>
182182
<td>0</td>
183183
<td>100.0%</td>
184-
<td>0.046 seconds</td>
184+
<td>0.005 seconds</td>
185185
</tr>
186186
</tbody>
187187
</table>

0 commit comments

Comments
 (0)