1
+ import XCTest
2
+
3
+ class BreadthFirstSearchShortestPathTests : XCTestCase {
4
+
5
+ func testShortestPathWhenGivenTree( ) {
6
+ let tree = Graph ( )
7
+ let nodeA = tree. addNode ( " a " )
8
+ let nodeB = tree. addNode ( " b " )
9
+ let nodeC = tree. addNode ( " c " )
10
+ let nodeD = tree. addNode ( " d " )
11
+ let nodeE = tree. addNode ( " e " )
12
+ let nodeF = tree. addNode ( " f " )
13
+ let nodeG = tree. addNode ( " g " )
14
+ let nodeH = tree. addNode ( " h " )
15
+ tree. addEdge ( nodeA, neighbor: nodeB)
16
+ tree. addEdge ( nodeA, neighbor: nodeC)
17
+ tree. addEdge ( nodeB, neighbor: nodeD)
18
+ tree. addEdge ( nodeB, neighbor: nodeE)
19
+ tree. addEdge ( nodeC, neighbor: nodeF)
20
+ tree. addEdge ( nodeC, neighbor: nodeG)
21
+ tree. addEdge ( nodeE, neighbor: nodeH)
22
+
23
+ let shortestPaths = breadthFirstSearchShortestPath ( tree, source: nodeA)
24
+
25
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeA. label) . distance, 0 )
26
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeB. label) . distance, 1 )
27
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeC. label) . distance, 1 )
28
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeD. label) . distance, 2 )
29
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeE. label) . distance, 2 )
30
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeF. label) . distance, 2 )
31
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeG. label) . distance, 2 )
32
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeH. label) . distance, 3 )
33
+ }
34
+
35
+ func testShortestPathWhenGivenGraph( ) {
36
+ let graph = Graph ( )
37
+
38
+ let nodeA = graph. addNode ( " a " )
39
+ let nodeB = graph. addNode ( " b " )
40
+ let nodeC = graph. addNode ( " c " )
41
+ let nodeD = graph. addNode ( " d " )
42
+ let nodeE = graph. addNode ( " e " )
43
+ let nodeF = graph. addNode ( " f " )
44
+ let nodeG = graph. addNode ( " g " )
45
+ let nodeH = graph. addNode ( " h " )
46
+ let nodeI = graph. addNode ( " i " )
47
+
48
+ graph. addEdge ( nodeA, neighbor: nodeB)
49
+ graph. addEdge ( nodeA, neighbor: nodeH)
50
+ graph. addEdge ( nodeB, neighbor: nodeA)
51
+ graph. addEdge ( nodeB, neighbor: nodeC)
52
+ graph. addEdge ( nodeB, neighbor: nodeH)
53
+ graph. addEdge ( nodeC, neighbor: nodeB)
54
+ graph. addEdge ( nodeC, neighbor: nodeD)
55
+ graph. addEdge ( nodeC, neighbor: nodeF)
56
+ graph. addEdge ( nodeC, neighbor: nodeI)
57
+ graph. addEdge ( nodeD, neighbor: nodeC)
58
+ graph. addEdge ( nodeD, neighbor: nodeE)
59
+ graph. addEdge ( nodeD, neighbor: nodeF)
60
+ graph. addEdge ( nodeE, neighbor: nodeD)
61
+ graph. addEdge ( nodeE, neighbor: nodeF)
62
+ graph. addEdge ( nodeF, neighbor: nodeC)
63
+ graph. addEdge ( nodeF, neighbor: nodeD)
64
+ graph. addEdge ( nodeF, neighbor: nodeE)
65
+ graph. addEdge ( nodeF, neighbor: nodeG)
66
+ graph. addEdge ( nodeG, neighbor: nodeF)
67
+ graph. addEdge ( nodeG, neighbor: nodeH)
68
+ graph. addEdge ( nodeG, neighbor: nodeI)
69
+ graph. addEdge ( nodeH, neighbor: nodeA)
70
+ graph. addEdge ( nodeH, neighbor: nodeB)
71
+ graph. addEdge ( nodeH, neighbor: nodeG)
72
+ graph. addEdge ( nodeH, neighbor: nodeI)
73
+ graph. addEdge ( nodeI, neighbor: nodeC)
74
+ graph. addEdge ( nodeI, neighbor: nodeG)
75
+ graph. addEdge ( nodeI, neighbor: nodeH)
76
+
77
+ let shortestPaths = breadthFirstSearchShortestPath ( graph, source: nodeA)
78
+
79
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeA. label) . distance, 0 )
80
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeB. label) . distance, 1 )
81
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeC. label) . distance, 2 )
82
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeD. label) . distance, 3 )
83
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeE. label) . distance, 4 )
84
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeF. label) . distance, 3 )
85
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeG. label) . distance, 2 )
86
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeH. label) . distance, 1 )
87
+ XCTAssertEqual ( shortestPaths. findNodeWithLabel ( nodeI. label) . distance, 2 )
88
+ }
89
+ }
0 commit comments