@@ -53,5 +53,106 @@ class GraphTests: XCTestCase {
53
53
XCTAssertEqual ( graph. vertices. count, 1 , " Graph should only contain one vertex after trying to create two vertices with identical data " )
54
54
}
55
55
}
56
-
56
+
57
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeDirecedGraphWithType( graphType: AbstractGraph < Int > . Type ) {
58
+ let graph = graphType. init ( )
59
+
60
+ let a = graph. createVertex ( 1 )
61
+ let b = graph. createVertex ( 2 )
62
+
63
+ graph. addDirectedEdge ( a, to: b, withWeight: 1.0 )
64
+
65
+ let edgesFromA = graph. edgesFrom ( a)
66
+ let edgesFromB = graph. edgesFrom ( b)
67
+
68
+ XCTAssertEqual ( edgesFromA. count, 1 )
69
+ XCTAssertEqual ( edgesFromB. count, 0 )
70
+
71
+ XCTAssertEqual ( edgesFromA. first? . to, b)
72
+ }
73
+
74
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeUndirectedGraphWithType( graphType: AbstractGraph < Int > . Type ) {
75
+ let graph = graphType. init ( )
76
+
77
+ let a = graph. createVertex ( 1 )
78
+ let b = graph. createVertex ( 2 )
79
+
80
+ graph. addUndirectedEdge ( ( a, b) , withWeight: 1.0 )
81
+
82
+ let edgesFromA = graph. edgesFrom ( a)
83
+ let edgesFromB = graph. edgesFrom ( b)
84
+
85
+ XCTAssertEqual ( edgesFromA. count, 1 )
86
+ XCTAssertEqual ( edgesFromB. count, 1 )
87
+
88
+ XCTAssertEqual ( edgesFromA. first? . to, b)
89
+ XCTAssertEqual ( edgesFromB. first? . to, a)
90
+ }
91
+
92
+ func testEdgesFromReturnsNoEdgesInNoEdgeGraphWithType( graphType: AbstractGraph < Int > . Type ) {
93
+ let graph = graphType. init ( )
94
+
95
+ let a = graph. createVertex ( 1 )
96
+ let b = graph. createVertex ( 2 )
97
+
98
+ XCTAssertEqual ( graph. edgesFrom ( a) . count, 0 )
99
+ XCTAssertEqual ( graph. edgesFrom ( b) . count, 0 )
100
+ }
101
+
102
+ func testEdgesFromReturnsCorrectEdgesInBiggerGraphInDirectedGraphWithType( graphType: AbstractGraph < Int > . Type ) {
103
+ let graph = graphType. init ( )
104
+ let verticesCount = 100
105
+ var vertices : [ Vertex < Int > ] = [ ]
106
+
107
+ for i in 0 ..< verticesCount {
108
+ vertices. append ( graph. createVertex ( i) )
109
+ }
110
+
111
+ for i in 0 ..< verticesCount {
112
+ for j in i+ 1 ..< verticesCount {
113
+ graph. addDirectedEdge ( vertices [ i] , to: vertices [ j] , withWeight: 1 )
114
+ }
115
+ }
116
+
117
+ for i in 0 ..< verticesCount {
118
+ let outEdges = graph. edgesFrom ( vertices [ i] )
119
+ let toVertices = outEdges. map { return $0. to}
120
+ XCTAssertEqual ( outEdges. count, verticesCount - i - 1 )
121
+ for j in i+ 1 ..< verticesCount {
122
+ XCTAssertTrue ( toVertices. contains ( vertices [ j] ) )
123
+ }
124
+ }
125
+ }
126
+
127
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeDirecedMatrixGraph( ) {
128
+ testEdgesFromReturnsCorrectEdgeInSingleEdgeDirecedGraphWithType ( AdjacencyMatrixGraph < Int > )
129
+ }
130
+
131
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeUndirectedMatrixGraph( ) {
132
+ testEdgesFromReturnsCorrectEdgeInSingleEdgeUndirectedGraphWithType ( AdjacencyMatrixGraph < Int > )
133
+ }
134
+
135
+ func testEdgesFromReturnsNoInNoEdgeMatrixGraph( ) {
136
+ testEdgesFromReturnsNoEdgesInNoEdgeGraphWithType ( AdjacencyMatrixGraph < Int > )
137
+ }
138
+
139
+ func testEdgesFromReturnsCorrectEdgesInBiggerGraphInDirectedMatrixGraph( ) {
140
+ testEdgesFromReturnsCorrectEdgesInBiggerGraphInDirectedGraphWithType ( AdjacencyMatrixGraph < Int > )
141
+ }
142
+
143
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeDirecedListGraph( ) {
144
+ testEdgesFromReturnsCorrectEdgeInSingleEdgeDirecedGraphWithType ( AdjacencyListGraph < Int > )
145
+ }
146
+
147
+ func testEdgesFromReturnsCorrectEdgeInSingleEdgeUndirectedListGraph( ) {
148
+ testEdgesFromReturnsCorrectEdgeInSingleEdgeUndirectedGraphWithType ( AdjacencyListGraph < Int > )
149
+ }
150
+
151
+ func testEdgesFromReturnsNoInNoEdgeListGraph( ) {
152
+ testEdgesFromReturnsNoEdgesInNoEdgeGraphWithType ( AdjacencyListGraph < Int > )
153
+ }
154
+
155
+ func testEdgesFromReturnsCorrectEdgesInBiggerGraphInDirectedListGraph( ) {
156
+ testEdgesFromReturnsCorrectEdgesInBiggerGraphInDirectedGraphWithType ( AdjacencyListGraph < Int > )
157
+ }
57
158
}
0 commit comments