-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgraph_test.go
400 lines (345 loc) · 14.5 KB
/
graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
package goraph
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math"
)
var _ = Describe("Tests of Graph structure", func() {
var (
graph *Graph
)
Context("add/get vertex methods with Vertex interface tests", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Given an empty graph, when get an vertex, then get a nil and error", func() {
vertex, err := graph.GetVertex("S")
Expect(err).Should(HaveOccurred())
Expect(vertex).Should(BeNil())
})
It("Given an empty graph, when add a vertex, then can get the vertex by its id later", func() {
myVertex := &myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}}
err := graph.AddVertexWithEdges(myVertex)
Expect(err).ShouldNot(HaveOccurred())
vertex, err := graph.GetVertex("S")
Expect(err).ShouldNot(HaveOccurred())
Expect(vertex).ShouldNot(BeNil())
Expect(vertex).Should(BeEquivalentTo(myVertex))
})
It("Given a graph, when add vertex with same ID, then get error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}})
err := graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{}, map[ID]float64{"S": 10, "B": 5}})
Expect(err).Should(HaveOccurred())
})
It("Given a graph, when add a vertex with -inf weight edge, then get error", func() {
err := graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"T": math.Inf(-1)}, map[ID]float64{}})
Expect(err).Should(HaveOccurred())
err = graph.AddVertexWithEdges(&myVertex{"T", map[ID]float64{}, map[ID]float64{"S": math.Inf(-1)}})
Expect(err).Should(HaveOccurred())
})
It("Given a graph, when add a vertex with unrelated edge, then get error", func() {
err := graph.AddVertexWithEdges(&testVertex{"X", &myVertex{"S", map[ID]float64{"T": 10}, map[ID]float64{}}})
Expect(err).Should(HaveOccurred())
})
})
Context("generic add/get vertex methods tests", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Given an empty graph, when get a vertex, then get a nil and error", func() {
vertex, err := graph.GetVertex("S")
Expect(err).Should(HaveOccurred())
Expect(vertex).Should(BeNil())
})
It("Given an empty graph, when add a vertex, then cat get the vertex by its id later", func() {
err := graph.AddVertex("S", "I am vertex S")
Expect(err).ShouldNot(HaveOccurred())
vertex, err := graph.GetVertex("S")
Expect(err).ShouldNot(HaveOccurred())
Expect(vertex).Should(BeEquivalentTo("I am vertex S"))
})
It("Given a graph with vertex S, when add vertex S again, then get error", func() {
err := graph.AddVertex("S", "I am vertex S")
Expect(err).ShouldNot(HaveOccurred())
err = graph.AddVertex("S", "I am vertex S too")
Expect(err).Should(HaveOccurred())
})
})
Context("add/get/update edge methods tests", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Given a graph without vertex S, when get the edge(weight) from S, then get +inf and an error", func() {
graph.AddVertexWithEdges(&myVertex{"A", map[ID]float64{}, map[ID]float64{"T": 10}})
graph.AddVertexWithEdges(&myVertex{"T", map[ID]float64{"T": 10}, map[ID]float64{}})
weight, err := graph.GetEdgeWeight("S", "T")
Expect(err).Should(HaveOccurred())
Expect(weight).Should(BeEquivalentTo(math.Inf(1)))
edge, err := graph.GetEdge("S", "T")
Expect(err).Should(HaveOccurred())
Expect(edge).Should(BeNil())
})
It("Given a graph without vertex T, when get the edge(weight) to T, then get +inf and an error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"T": 10}, map[ID]float64{}})
graph.AddVertexWithEdges(&myVertex{"B", map[ID]float64{}, map[ID]float64{"T": 10}})
weight, err := graph.GetEdgeWeight("S", "T")
Expect(err).Should(HaveOccurred())
Expect(weight).Should(BeEquivalentTo(math.Inf(1)))
edge, err := graph.GetEdge("S", "T")
Expect(err).Should(HaveOccurred())
Expect(edge).Should(BeNil())
})
It("Given a graph with S and T disconnected, when get the edge(weight) from S to T, then get +inf without error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"A": 10}, map[ID]float64{"A": 10}})
graph.AddVertexWithEdges(&myVertex{"A", map[ID]float64{"S": 10}, map[ID]float64{"S": 10}})
graph.AddVertexWithEdges(&myVertex{"B", map[ID]float64{"T": 10}, map[ID]float64{"T": 10}})
graph.AddVertexWithEdges(&myVertex{"T", map[ID]float64{"B": 10}, map[ID]float64{"B": 10}})
weight, err := graph.GetEdgeWeight("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(weight).Should(BeEquivalentTo(math.Inf(1)))
edge, err := graph.GetEdge("S", "T")
Expect(err).Should(HaveOccurred())
Expect(edge).Should(BeNil())
})
It("Given a graph with S and T connected, when get the edge from S to T, then get its weight without error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"T": 10}, map[ID]float64{"T": 10}})
graph.AddVertexWithEdges(&myVertex{"T", map[ID]float64{"S": 10}, map[ID]float64{"S": 10}})
edge, err := graph.GetEdgeWeight("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(edge).Should(BeEquivalentTo(10))
})
It("Given a graph with S and T connected, when get the edge from S to T, then get its weight without error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"T": 10}, map[ID]float64{"T": 10}})
graph.AddVertexWithEdges(&myVertex{"T", map[ID]float64{"S": 10}, map[ID]float64{"S": 10}})
edge, err := graph.GetEdgeWeight("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(edge).Should(BeEquivalentTo(10))
})
It("Given a graph without S, when add an edge from S, then get an error", func() {
graph.AddVertex("T", "I am vertex T")
err := graph.AddEdge("S", "T", 10, nil)
Expect(err).Should(HaveOccurred())
})
It("Given a graph without T, when add an edge to T, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
err := graph.AddEdge("S", "T", 10, nil)
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T disconnected, when add an edge weight from S to T with -inf weight, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
err := graph.AddEdge("S", "T", math.Inf(-1), nil)
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T disconnected, when add an edge weight from S to T, then get nil error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
err := graph.AddEdge("S", "T", 10, nil)
Expect(err).ShouldNot(HaveOccurred())
edge, err := graph.GetEdgeWeight("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(edge).Should(BeEquivalentTo(10))
})
It("Given a graph with S and T already connected, when add an edge weight from S to T again, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
err := graph.AddEdge("S", "T", 10, nil)
Expect(err).ShouldNot(HaveOccurred())
err = graph.AddEdge("S", "T", 20, nil)
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T disconnected, when add an edge from S to T, then get nil error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
err := graph.AddEdge("S", "T", 10, "I am an edge")
Expect(err).ShouldNot(HaveOccurred())
edge, err := graph.GetEdge("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(edge.(string)).Should(BeEquivalentTo("I am an edge"))
})
})
Context("update methods tests", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Given a graph without S, when update an edge from S, then get an error", func() {
graph.AddVertex("T", "I am vertex T")
err := graph.UpdateEdgeWeight("S", "T", 10)
Expect(err).Should(HaveOccurred())
})
It("Given a graph without T, when update an edge to T, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
err := graph.UpdateEdgeWeight("S", "T", 10)
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T disconnected, when update an edge from S to T, then get nil error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
err := graph.UpdateEdgeWeight("S", "T", 10)
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T connected, when add an edge from S to T with -inf weight, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
graph.AddEdge("S", "T", 10, nil)
err := graph.UpdateEdgeWeight("S", "T", math.Inf(-1))
Expect(err).Should(HaveOccurred())
})
It("Given a graph with S and T connected, when add an edge from S to T again, then get an error", func() {
graph.AddVertex("S", "I am vertex S")
graph.AddVertex("T", "I am vertex T")
graph.AddEdge("S", "T", 10, nil)
err := graph.UpdateEdgeWeight("S", "T", 20)
Expect(err).ShouldNot(HaveOccurred())
edge, err := graph.GetEdgeWeight("S", "T")
Expect(err).ShouldNot(HaveOccurred())
Expect(edge).Should(BeEquivalentTo(20))
})
})
Context("IntegrityCheck tests", func() {
BeforeEach(func() {
graph = NewGraph()
})
AfterEach(func() {
graph = nil
})
It("Given a graph with an edge to an unknown vertex, when check integrity, then get error", func() {
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}})
err := graph.CheckIntegrity()
Expect(err).Should(HaveOccurred())
graph.AddVertexWithEdges(&myVertex{"A", map[ID]float64{}, map[ID]float64{"S": 10, "B": 5}})
err = graph.CheckIntegrity()
Expect(err).Should(HaveOccurred())
graph.AddVertexWithEdges(&myVertex{"B", map[ID]float64{"A": 5}, map[ID]float64{"S": 10}})
err = graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
graph.egress["C"] = map[ID]*edge{"B": {nil, 15, true, false}}
err = graph.CheckIntegrity()
Expect(err).Should(HaveOccurred())
delete(graph.egress, "C")
graph.ingress["S"]["T"] = &edge{nil, 20, true, false}
err = graph.CheckIntegrity()
Expect(err).Should(HaveOccurred())
delete(graph.ingress["S"], "T")
graph.ingress["T"] = map[ID]*edge{"S": {nil, 20, true, false}}
err = graph.CheckIntegrity()
Expect(err).Should(HaveOccurred())
})
})
Context("delete methods tests", func() {
BeforeEach(func() {
graph = NewGraph()
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}})
graph.AddVertexWithEdges(&myVertex{"A", map[ID]float64{}, map[ID]float64{"S": 10, "B": 5}})
graph.AddVertexWithEdges(&myVertex{"B", map[ID]float64{"A": 5}, map[ID]float64{"S": 10}})
})
AfterEach(func() {
graph = nil
})
It("Given a graph without vertex T, when delete vertex T, then get an nil", func() {
v := graph.DeleteVertex("T")
Expect(v).Should(BeNil())
})
It("Given a graph with vertex S, when delete vertex S, then get S and can not get S later", func() {
v := graph.DeleteVertex("S")
Expect(v).Should(BeEquivalentTo(&myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}}))
_, err := graph.GetVertex("S")
Expect(err).Should(HaveOccurred())
err = graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
v = graph.DeleteVertex("A")
Expect(v).Should(BeEquivalentTo(&myVertex{"A", map[ID]float64{}, map[ID]float64{"S": 10, "B": 5}}))
_, err = graph.GetVertex("A")
Expect(err).Should(HaveOccurred())
err = graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
})
It("Given a graph without vertex T, when delete edge from/to vertex T, then nothing happens", func() {
graph.DeleteEdge("T", "S")
_, err := graph.GetVertex("S")
Expect(err).ShouldNot(HaveOccurred())
err = graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
_, err = graph.GetVertex("S")
Expect(err).ShouldNot(HaveOccurred())
graph.DeleteEdge("S", "T")
_, err = graph.GetVertex("S")
Expect(err).ShouldNot(HaveOccurred())
err = graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
})
It("Given a graph without edge from vertex A to S, when delete edge from A to S, then nothing happens", func() {
weight, _ := graph.GetEdgeWeight("A", "S")
Expect(weight).Should(BeEquivalentTo(math.Inf(1)))
edge := graph.DeleteEdge("A", "S")
Expect(edge).Should(BeNil())
err := graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
})
It("Given a graph with edge from vertex A to B, when delete edge from A to B, then the weight between A and B will be +inf", func() {
weight, _ := graph.GetEdgeWeight("B", "A")
Expect(weight).Should(BeEquivalentTo(5))
graph.DeleteEdge("B", "A")
weight, _ = graph.GetEdgeWeight("B", "A")
Expect(weight).Should(BeEquivalentTo(math.Inf(1)))
err := graph.CheckIntegrity()
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("get total weight of path tests", func() {
BeforeEach(func() {
graph = NewGraph()
graph.AddVertexWithEdges(&myVertex{"S", map[ID]float64{"A": 10, "B": 10}, map[ID]float64{}})
graph.AddVertexWithEdges(&myVertex{"A", map[ID]float64{}, map[ID]float64{"S": 10, "B": 5}})
graph.AddVertexWithEdges(&myVertex{"B", map[ID]float64{"A": 5}, map[ID]float64{"S": 10}})
})
AfterEach(func() {
graph = nil
})
It("Given an empty path, when get its weight, then get -inf", func() {
path := []ID{}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(math.Inf(-1)))
})
It("Given a path contains unexisted vertex, when get its weight, then get -inf", func() {
path := []ID{"T"}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(math.Inf(-1)))
path = []ID{"S", "A", "T"}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(math.Inf(-1)))
})
It("Given a path only have one vertex, when get its weight, then get 0", func() {
path := []ID{"S"}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(0))
})
It("Given a path with vertex disconnected, when get its weight, then get +inf", func() {
path := []ID{"B", "A", "S"}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(math.Inf(1)))
})
It("Given a path with all vertex connected, when get its weight, then get the end to end weight of the path", func() {
path := []ID{"S", "B", "A"}
Expect(graph.GetPathWeight(path)).Should(BeEquivalentTo(15))
})
})
})
type testVertex struct {
id ID
vertex Vertex
}
func (test *testVertex) ID() ID {
return test.id
}
func (test *testVertex) Edges() (edges []Edge) {
return test.vertex.Edges()
}