-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
197 lines (160 loc) · 4.06 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
package graph
import (
"testing"
)
func TestGraph_AddEdge(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
b := Vertex[string]{"B"}
g.AddVertex(&a)
g.AddVertex(&b)
tag := "tag"
pTag := &tag
err := g.AddEdge(&a, &b, 1, pTag)
if err != nil {
t.Error(err)
}
}
func TestGraph_ContainsNode(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
g.AddVertex(&a)
result := g.ContainsVertex(&a)
if !result {
t.Error("could not find vertex in graph")
}
}
func TestGraph_ContainsEdge(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
b := Vertex[string]{"B"}
g.AddVertex(&a)
g.AddVertex(&b)
tag := "tag"
pTag := &tag
err := g.AddEdge(&a, &b, 1, pTag)
if err != nil {
t.Error(err)
}
result := g.containsEdge(&a, &b, pTag)
if !result {
t.Error("could not find edge in graph")
}
}
func TestGraph_ContainsSymmetricEdge(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
b := Vertex[string]{"B"}
g.AddVertex(&a)
g.AddVertex(&b)
tag := "tag"
pTag := &tag
// no edges
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
// A->B
_ = g.AddEdge(&a, &b, 1, pTag)
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
g.RemoveEdge(&a, &b, pTag)
// B->A
_ = g.AddEdge(&b, &a, 1, pTag)
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); contains {
t.Error("should not contain A<->B")
}
g.RemoveEdge(&b, &a, pTag)
// A<->B
if err := g.AddSymmetricEdge(&a, &b, 5, pTag); err != nil {
t.Error("unable to add symmetric edge A<->B")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); !contains {
t.Error("should contain A<->B, but does not!")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); !contains {
t.Error("should contain A<->B, but does not!")
}
g.RemoveSymmetricEdge(&a, &b, pTag)
// B<->A
if err := g.AddSymmetricEdge(&b, &a, 5, pTag); err != nil {
t.Error("unable to add symmetric edge A<->B")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); !contains {
t.Error("should contain A<->B, but does not!")
}
if contains := g.ContainsSymmetricEdge(&a, &b, pTag); !contains {
t.Error("should contain A<->B, but does not!")
}
}
func TestGraph_RemoveSymmetricEdge(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
b := Vertex[string]{"B"}
g.AddVertex(&a)
g.AddVertex(&b)
tag := "tag"
pTag := &tag
err := g.AddSymmetricEdge(&a, &b, 5, pTag)
if err != nil {
t.Error("unable to add symmetric edge")
t.Fail()
}
g.RemoveSymmetricEdge(&a, &b, pTag)
if g.ContainsEdge(&a, &b, pTag) {
t.Error("graph still contains symmetric edge A -> B")
t.Fail()
}
}
func TestGraph_AddSymmetricEdge(t *testing.T) {
g := Graph[string]{}
a := Vertex[string]{"A"}
b := Vertex[string]{"B"}
g.AddVertex(&a)
g.AddVertex(&b)
tag := "tag"
pTag := &tag
err := g.AddSymmetricEdge(&a, &b, 5, pTag)
if err != nil {
t.Error("unable to add symmetric edge")
t.Fail()
}
if !g.ContainsSymmetricEdge(&a, &b, pTag) {
t.Error("graph does not contains symmetric edge A -> B")
t.Fail()
}
}
func TestGraph_ShortestPath(t *testing.T) {
g := Graph[string]{}
a := &(Vertex[string]{"A"})
b := &(Vertex[string]{"B"})
c := &(Vertex[string]{"C"})
d := &(Vertex[string]{"D"})
g.AddVertex(a)
g.AddVertex(b)
g.AddVertex(c)
g.AddVertex(d)
_ = g.AddEdge(a, b, 1, nil)
_ = g.AddEdge(b, c, 10, nil)
_ = g.addEdge(a, d, 5, nil)
_ = g.addEdge(d, c, 5, nil)
path, err := g.ShortestPath(a, c)
if err != nil {
t.Error(err)
}
if path[0].Source.String() != "A" || path[0].Destination.String() != "D" {
t.Error("Path does not contain A -> D")
}
if path[1].Source.String() != "D" || path[1].Destination.String() != "C" {
t.Error("Path does not contain D -> C")
}
}