-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph_test.go
253 lines (213 loc) · 4.21 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
package dag
import (
"fmt"
"strings"
"testing"
)
func TestGraph_empty(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphEmptyStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraph_basic(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 3))
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphBasicStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraph_remove(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 3))
g.Remove(3)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphRemoveStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraph_replace(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 3))
g.Replace(2, 42)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphReplaceStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraph_replaceSelf(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 3))
g.Replace(2, 2)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphReplaceSelfStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
// This tests that connecting edges works based on custom Hashcode
// implementations for uniqueness.
func TestGraph_hashcode(t *testing.T) {
var g Graph
g.Add(&hashVertex{code: 1})
g.Add(&hashVertex{code: 2})
g.Add(&hashVertex{code: 3})
g.Connect(BasicEdge(
&hashVertex{code: 1},
&hashVertex{code: 3}))
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphBasicStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphHasVertex(t *testing.T) {
var g Graph
g.Add(1)
if !g.HasVertex(1) {
t.Fatal("should have 1")
}
if g.HasVertex(2) {
t.Fatal("should not have 2")
}
}
func TestGraphHasEdge(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Connect(BasicEdge(1, 2))
if !g.HasEdge(BasicEdge(1, 2)) {
t.Fatal("should have 1,2")
}
if g.HasVertex(BasicEdge(2, 3)) {
t.Fatal("should not have 2,3")
}
}
func TestGraphEdgesFrom(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 3))
g.Connect(BasicEdge(2, 3))
edges := g.EdgesFrom(1)
expected := make(Set)
expected.Add(BasicEdge(1, 3))
s := make(Set)
for _, e := range edges {
s.Add(e)
}
if s.Intersection(expected).Len() != expected.Len() {
t.Fatalf("bad: %#v", edges)
}
}
func TestGraphEdgesTo(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 3))
g.Connect(BasicEdge(1, 2))
edges := g.EdgesTo(3)
expected := make(Set)
expected.Add(BasicEdge(1, 3))
s := make(Set)
for _, e := range edges {
s.Add(e)
}
if s.Intersection(expected).Len() != expected.Len() {
t.Fatalf("bad: %#v", edges)
}
}
func TestGraphUpdownEdges(t *testing.T) {
// Verify that we can't inadvertently modify the internal graph sets
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 3))
up := g.UpEdges(2)
if up.Len() != 1 || !up.Include(1) {
t.Fatalf("expected only an up edge of '1', got %#v", up)
}
// modify the up set
up.Add(9)
orig := g.UpEdges(2)
diff := up.Difference(orig)
if diff.Len() != 1 || !diff.Include(9) {
t.Fatalf("expected a diff of only '9', got %#v", diff)
}
down := g.DownEdges(2)
if down.Len() != 1 || !down.Include(3) {
t.Fatalf("expected only a down edge of '3', got %#v", down)
}
// modify the down set
down.Add(8)
orig = g.DownEdges(2)
diff = down.Difference(orig)
if diff.Len() != 1 || !diff.Include(8) {
t.Fatalf("expected a diff of only '8', got %#v", diff)
}
}
type hashVertex struct {
code interface{}
}
func (v *hashVertex) Hashcode() interface{} {
return v.code
}
func (v *hashVertex) Name() string {
return fmt.Sprintf("%#v", v.code)
}
const testGraphBasicStr = `
1
3
2
3
`
const testGraphEmptyStr = `
1
2
3
`
const testGraphRemoveStr = `
1
2
`
const testGraphReplaceStr = `
1
42
3
42
3
`
const testGraphReplaceSelfStr = `
1
2
2
3
3
`