@@ -5,29 +5,72 @@ import spock.lang.Subject
55import spock.lang.Unroll
66
77class DijkstrasSpec extends Specification {
8-
9- static final SAMPLE_ONE = [
10- A : [B : 7 , C : 2 ],
11- B : [A : 3 , C : 5 ],
12- C : [A : 1 , B : 3 ]
13- ] as Graph<String >
14-
158 @Subject
169 def algorithm = new Dijkstras<String > ()
1710
18- @Unroll (" from #source to #target the time is #time and the path is #expected " )
19- def ' should find a route for sample one ' () {
11+ @Unroll (" from #source to #target the time is #time and the path is #fastest " )
12+ def ' should find a route for a simple graph ' () {
2013 given :
21- def graph = SAMPLE_ONE
14+ def graph = new Graph ([
15+ A : [B : 7 , C : 2 ],
16+ B : [A : 3 , C : 5 ],
17+ C : [A : 1 , B : 3 ]
18+ ])
2219
2320 when :
2421 def path = algorithm. findPath(graph, source, target)
2522
2623 then :
27- path == expected
24+ path == fastest
25+
26+ and :
27+ graph. getDistance(path) == time as double
2828
2929 where :
30- source | target || time | expected
31- ' A' | ' B' || 7 | [' A' , ' B' ]
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' ]
3235 }
36+
37+ @Unroll (" from #source to #target the time is #time and the path is #fastest" )
38+ def ' should find a route for a complex graph' () {
39+ given :
40+ def graph = new Graph ([
41+ A : [B : 5 , H : 2 ],
42+ B : [A : 5 , C : 7 ],
43+ C : [B : 7 , D : 3 , G : 4 ],
44+ D : [C : 20 , E : 4 ],
45+ E : [F : 5 ],
46+ F : [G : 6 ],
47+ G : [C : 4 ],
48+ H : [G : 3 ]
49+ ])
50+
51+ when :
52+ def path = algorithm. findPath(graph, source, target)
53+
54+ then :
55+ path == fastest
56+
57+ and :
58+ graph. getDistance(path) == time as double
59+
60+ where :
61+ source | target || time | fastest
62+ ' A' | ' A' || 0 | [' A' ]
63+ ' B' | ' B' || 0 | [' B' ]
64+ ' A' | ' B' || 5 | [' A' , ' B' ]
65+ ' B' | ' A' || 5 | [' B' , ' A' ]
66+ ' A' | ' C' || 9 | [' A' , ' H' , ' G' , ' C' ]
67+ ' C' | ' A' || 12 | [' C' , ' B' , ' A' ]
68+ ' A' | ' G' || 5 | [' A' , ' H' , ' G' ]
69+ ' C' | ' D' || 3 | [' C' , ' D' ]
70+ ' D' | ' C' || 19 | [' D' , ' E' , ' F' , ' G' , ' C' ]
71+ ' B' | ' D' || 10 | [' B' , ' C' , ' D' ]
72+ ' D' | ' B' || 26 | [' D' , ' E' , ' F' , ' G' , ' C' , ' B' ]
73+ ' D' | ' H' || 33 | [' D' , ' E' , ' F' , ' G' , ' C' , ' B' , ' A' , ' H' ]
74+ }
75+
3376}
0 commit comments