@@ -13,7 +13,32 @@ namespace Algorithms.Dijktra.Tests
1313 public class DijkstraUTests
1414 {
1515 [ Test ]
16- public void DijkstraTest1_BasicGraph ( )
16+ public void DijkstraTest1_GraphIsNull ( )
17+ {
18+ var graph = new DirectedWeightedGraph < char > ( 5 ) ;
19+ var a = graph . AddVertex ( 'A' ) ;
20+
21+ Func < DistanceModel < char > [ ] > action = ( ) => DijkstraAlgorithm . GenerateShortestPath ( null ! , a ) ;
22+
23+ action . Should ( ) . Throw < ArgumentNullException > ( )
24+ . WithMessage ( $ "Value cannot be null. (Parameter '{ nameof ( graph ) } ')") ;
25+ }
26+
27+ [ Test ]
28+ public void DijkstraTest2_VertexDoesntBelongToGraph ( )
29+ {
30+ var graph = new DirectedWeightedGraph < char > ( 5 ) ;
31+ var startVertex = graph . AddVertex ( 'A' ) ;
32+
33+ Func < DistanceModel < char > [ ] > action = ( ) => DijkstraAlgorithm . GenerateShortestPath (
34+ new DirectedWeightedGraph < char > ( 5 ) , startVertex ) ;
35+
36+ action . Should ( ) . Throw < ArgumentNullException > ( )
37+ . WithMessage ( $ "Value cannot be null. (Parameter '{ nameof ( graph ) } ')") ;
38+ }
39+
40+ [ Test ]
41+ public void DijkstraTest3_BasicGraph ( )
1742 {
1843 var graph = new DirectedWeightedGraph < char > ( 4 ) ;
1944 var a = graph . AddVertex ( 'A' ) ;
@@ -34,26 +59,143 @@ public void DijkstraTest1_BasicGraph()
3459 shortestPathList [ 0 ] . PreviousVertex . Should ( ) . Be ( a ) ;
3560 shortestPathList [ 0 ] . Vertex . Should ( ) . Be ( a ) ;
3661 shortestPathList [ 0 ] . Distance . Should ( ) . Be ( 0 ) ;
37- shortestPathList [ 0 ] . ToString ( ) . Should ( )
38- . Be ( $ "From Previous Vertex: { a } to Vertex { a } is Distance: { 0 } ") ;
62+ shortestPathList [ 0 ] . PrintDistance ( ) . Should ( )
63+ . Be ( $ "Start from Vertex: { a } to Vertex { a } is Distance: { 0 } ") ;
3964
4065 shortestPathList [ 1 ] . PreviousVertex . Should ( ) . Be ( a ) ;
4166 shortestPathList [ 1 ] . Vertex . Should ( ) . Be ( b ) ;
4267 shortestPathList [ 1 ] . Distance . Should ( ) . Be ( 4 ) ;
43- shortestPathList [ 1 ] . ToString ( ) . Should ( )
44- . Be ( $ "From Previous Vertex: { a } to Vertex { b } is Distance: { 4 } ") ;
68+ shortestPathList [ 1 ] . PrintDistance ( ) . Should ( )
69+ . Be ( $ "Start from Vertex: { a } to Vertex { b } is Distance: { 4 } ") ;
4570
4671 shortestPathList [ 2 ] . PreviousVertex . Should ( ) . Be ( b ) ;
4772 shortestPathList [ 2 ] . Vertex . Should ( ) . Be ( c ) ;
4873 shortestPathList [ 2 ] . Distance . Should ( ) . Be ( 6 ) ;
49- shortestPathList [ 2 ] . ToString ( ) . Should ( )
50- . Be ( $ "From Previous Vertex: { b } to Vertex { c } is Distance: { 6 } ") ;
74+ shortestPathList [ 2 ] . PrintDistance ( ) . Should ( )
75+ . Be ( $ "Start from Vertex: { a } to Vertex { c } is Distance: { 6 } ") ;
5176
5277 shortestPathList [ 3 ] . PreviousVertex . Should ( ) . Be ( b ) ;
5378 shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
5479 shortestPathList [ 3 ] . Distance . Should ( ) . Be ( 7 ) ;
55- shortestPathList [ 3 ] . ToString ( ) . Should ( )
56- . Be ( $ "From Previous Vertex: { b } to Vertex { d } is Distance: { 7 } ") ;
80+ shortestPathList [ 3 ] . PrintDistance ( ) . Should ( )
81+ . Be ( $ "Start from Vertex: { a } to Vertex { d } is Distance: { 7 } ") ;
82+ }
83+
84+ [ Test ]
85+ public void DijkstraTest4_SparseGraph ( )
86+ {
87+ var graph = new DirectedWeightedGraph < char > ( 6 ) ;
88+ var a = graph . AddVertex ( 'A' ) ;
89+ var b = graph . AddVertex ( 'B' ) ;
90+ var c = graph . AddVertex ( 'C' ) ;
91+ var d = graph . AddVertex ( 'D' ) ;
92+ var e = graph . AddVertex ( 'E' ) ;
93+ var f = graph . AddVertex ( 'F' ) ;
94+
95+ graph . AddEdge ( a , f , 4 ) ;
96+
97+
98+ var shortestPathList = DijkstraAlgorithm . GenerateShortestPath ( graph , a ) ;
99+ shortestPathList . Length . Should ( ) . Be ( 6 ) ;
100+
101+ shortestPathList [ 0 ] . PreviousVertex . Should ( ) . Be ( a ) ;
102+ shortestPathList [ 0 ] . Vertex . Should ( ) . Be ( a ) ;
103+ shortestPathList [ 0 ] . Distance . Should ( ) . Be ( 0 ) ;
104+ shortestPathList [ 0 ] . PrintDistance ( ) . Should ( )
105+ . Be ( $ "Start from Vertex: { a } to Vertex { a } is Distance: { 0 } ") ;
106+
107+ shortestPathList [ 1 ] . PreviousVertex . Should ( ) . Be ( null ) ;
108+ shortestPathList [ 1 ] . Vertex . Should ( ) . Be ( b ) ;
109+ shortestPathList [ 1 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
110+ shortestPathList [ 1 ] . PrintDistance ( ) . Should ( )
111+ . Be ( $ "Start from Vertex: { a } to Vertex { b } is Distance: { double . MaxValue } ") ;
112+
113+ shortestPathList [ 2 ] . PreviousVertex . Should ( ) . Be ( null ) ;
114+ shortestPathList [ 2 ] . Vertex . Should ( ) . Be ( c ) ;
115+ shortestPathList [ 2 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
116+ shortestPathList [ 2 ] . PrintDistance ( ) . Should ( )
117+ . Be ( $ "Start from Vertex: { a } to Vertex { c } is Distance: { double . MaxValue } ") ;
118+
119+ shortestPathList [ 3 ] . PreviousVertex . Should ( ) . Be ( null ) ;
120+ shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
121+ shortestPathList [ 3 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
122+ shortestPathList [ 3 ] . PrintDistance ( ) . Should ( )
123+ . Be ( $ "Start from Vertex: { a } to Vertex { d } is Distance: { double . MaxValue } ") ;
124+
125+ shortestPathList [ 4 ] . PreviousVertex . Should ( ) . Be ( null ) ;
126+ shortestPathList [ 4 ] . Vertex . Should ( ) . Be ( e ) ;
127+ shortestPathList [ 4 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
128+ shortestPathList [ 4 ] . PrintDistance ( ) . Should ( )
129+ . Be ( $ "Start from Vertex: { a } to Vertex { e } is Distance: { double . MaxValue } ") ;
130+
131+ shortestPathList [ 5 ] . PreviousVertex . Should ( ) . Be ( a ) ;
132+ shortestPathList [ 5 ] . Vertex . Should ( ) . Be ( f ) ;
133+ shortestPathList [ 5 ] . Distance . Should ( ) . Be ( 4 ) ;
134+ shortestPathList [ 5 ] . PrintDistance ( ) . Should ( )
135+ . Be ( $ "Start from Vertex: { a } to Vertex { f } is Distance: { 4 } ") ;
136+ }
137+
138+ [ Test ]
139+ public void DijkstraTest5_DenseGraph ( )
140+ {
141+ var graph = new DirectedWeightedGraph < char > ( 6 ) ;
142+ var a = graph . AddVertex ( 'A' ) ;
143+ var b = graph . AddVertex ( 'B' ) ;
144+ var c = graph . AddVertex ( 'C' ) ;
145+ var d = graph . AddVertex ( 'D' ) ;
146+ var e = graph . AddVertex ( 'E' ) ;
147+ var f = graph . AddVertex ( 'F' ) ;
148+
149+ graph . AddEdge ( a , b , 1 ) ;
150+ graph . AddEdge ( a , d , 2 ) ;
151+ graph . AddEdge ( b , c , 1 ) ;
152+ graph . AddEdge ( b , d , 2 ) ;
153+ graph . AddEdge ( b , e , 3 ) ;
154+ graph . AddEdge ( c , e , 2 ) ;
155+ graph . AddEdge ( c , f , 1 ) ;
156+ graph . AddEdge ( d , e , 1 ) ;
157+ graph . AddEdge ( e , f , 1 ) ;
158+
159+ var shortestPathList = DijkstraAlgorithm . GenerateShortestPath ( graph , a ) ;
160+ shortestPathList . Length . Should ( ) . Be ( 6 ) ;
161+
162+ shortestPathList [ 0 ] . PreviousVertex . Should ( ) . Be ( a ) ;
163+ shortestPathList [ 0 ] . Vertex . Should ( ) . Be ( a ) ;
164+ shortestPathList [ 0 ] . Distance . Should ( ) . Be ( 0 ) ;
165+ shortestPathList [ 0 ] . PrintDistance ( ) . Should ( )
166+ . Be ( $ "Start from Vertex: { a } to Vertex { a } is Distance: { 0 } ") ;
167+
168+ shortestPathList [ 1 ] . PreviousVertex . Should ( ) . Be ( a ) ;
169+ shortestPathList [ 1 ] . Vertex . Should ( ) . Be ( b ) ;
170+ shortestPathList [ 1 ] . Distance . Should ( ) . Be ( 1 ) ;
171+ shortestPathList [ 1 ] . PrintDistance ( ) . Should ( )
172+ . Be ( $ "Start from Vertex: { a } to Vertex { b } is Distance: { 1 } ") ;
173+
174+ shortestPathList [ 2 ] . PreviousVertex . Should ( ) . Be ( b ) ;
175+ shortestPathList [ 2 ] . Vertex . Should ( ) . Be ( c ) ;
176+ shortestPathList [ 2 ] . Distance . Should ( ) . Be ( 2 ) ;
177+ shortestPathList [ 2 ] . PrintDistance ( ) . Should ( )
178+ . Be ( $ "Start from Vertex: { a } to Vertex { c } is Distance: { 2 } ") ;
179+
180+ shortestPathList [ 3 ] . PreviousVertex . Should ( ) . Be ( a ) ;
181+ shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
182+ shortestPathList [ 3 ] . Distance . Should ( ) . Be ( 2 ) ;
183+ shortestPathList [ 3 ] . PrintDistance ( ) . Should ( )
184+ . Be ( $ "Start from Vertex: { a } to Vertex { d } is Distance: { 2 } ") ;
185+
186+ shortestPathList [ 4 ] . PreviousVertex . Should ( ) . Be ( d ) ;
187+ shortestPathList [ 4 ] . Vertex . Should ( ) . Be ( e ) ;
188+ shortestPathList [ 4 ] . Distance . Should ( ) . Be ( 3 ) ;
189+ shortestPathList [ 4 ] . PrintDistance ( ) . Should ( )
190+ . Be ( $ "Start from Vertex: { a } to Vertex { e } is Distance: { 3 } ") ;
191+
192+ shortestPathList [ 5 ] . PreviousVertex . Should ( ) . Be ( c ) ;
193+ shortestPathList [ 5 ] . Vertex . Should ( ) . Be ( f ) ;
194+ shortestPathList [ 5 ] . Distance . Should ( ) . Be ( 3 ) ;
195+ shortestPathList [ 5 ] . PrintDistance ( ) . Should ( )
196+ . Be ( $ "Start from Vertex: { a } to Vertex { f } is Distance: { 3 } ") ;
197+
57198 }
58199 }
200+
59201}
0 commit comments