-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
graph_test.go
298 lines (251 loc) · 6.39 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
package graph
import (
"reflect"
"testing"
)
func TestNew(t *testing.T) {
directedType := reflect.TypeOf(&directed[int, int]{})
undirectedType := reflect.TypeOf(&undirected[int, int]{})
tests := map[string]struct {
expectedType reflect.Type
options []func(*Traits)
}{
"no options": {
options: []func(*Traits){},
expectedType: undirectedType,
},
"directed option": {
options: []func(*Traits){Directed()},
expectedType: directedType,
},
}
for name, test := range tests {
graph := New(IntHash, test.options...)
actualType := reflect.TypeOf(graph)
if actualType != test.expectedType {
t.Errorf("%s: graph type expectancy doesn't match: expected %v, got %v", name, test.expectedType, actualType)
}
}
}
func TestNewLike(t *testing.T) {
tests := map[string]struct {
g Graph[int, int]
vertices []int
}{
"new directed graph of integers": {
g: New(IntHash, Directed()),
vertices: []int{1, 2, 3},
},
"new undirected weighted graph of integers": {
g: New(IntHash, Weighted()),
vertices: []int{1, 2, 3},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
for _, vertex := range test.vertices {
_ = test.g.AddVertex(vertex)
}
h := NewLike(test.g)
if len(test.vertices) > 0 {
if _, err := h.Vertex(test.vertices[0]); err == nil {
t.Errorf("expected vertex %v not to exist in h", test.vertices[0])
}
}
if test.g.Traits().IsDirected {
actual, ok := h.(*directed[int, int])
if !ok {
t.Fatalf("type assertion to *directed failed")
}
expected := test.g.(*directed[int, int])
if actual.hash(42) != expected.hash(42) {
t.Errorf("expected hash %v, got %v", expected.hash, actual.hash)
}
} else {
actual, ok := h.(*undirected[int, int])
if !ok {
t.Fatalf("type assertion to *directed failed")
}
expected := test.g.(*undirected[int, int])
if actual.hash(42) != expected.hash(42) {
t.Errorf("expected hash %v, got %v", expected.hash, actual.hash)
}
}
if !traitsAreEqual(h.Traits(), test.g.Traits()) {
t.Errorf("expected traits %+v, got %+v", test.g.Traits(), h.Traits())
}
})
}
}
func TestStringHash(t *testing.T) {
tests := map[string]struct {
value string
expectedHash string
}{
"string value": {
value: "London",
expectedHash: "London",
},
}
for name, test := range tests {
hash := StringHash(test.value)
if hash != test.expectedHash {
t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash)
}
}
}
func TestIntHash(t *testing.T) {
tests := map[string]struct {
value int
expectedHash int
}{
"int value": {
value: 3,
expectedHash: 3,
},
}
for name, test := range tests {
hash := IntHash(test.value)
if hash != test.expectedHash {
t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash)
}
}
}
func TestEdgeWeight(t *testing.T) {
tests := map[string]struct {
expected EdgeProperties
weight int
}{
"weight 4": {
weight: 4,
expected: EdgeProperties{
Weight: 4,
},
},
}
for name, test := range tests {
properties := EdgeProperties{}
EdgeWeight(test.weight)(&properties)
if properties.Weight != test.expected.Weight {
t.Errorf("%s: weight expectation doesn't match: expected %v, got %v", name, test.expected.Weight, properties.Weight)
}
}
}
func TestEdgeAttribute(t *testing.T) {
tests := map[string]struct {
key string
value string
expected EdgeProperties
}{
"attribute label=my-label": {
key: "label",
value: "my-label",
expected: EdgeProperties{
Attributes: map[string]string{
"label": "my-label",
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
properties := EdgeProperties{
Attributes: make(map[string]string),
}
EdgeAttribute(test.key, test.value)(&properties)
value, ok := properties.Attributes[test.key]
if !ok {
t.Errorf("attribute expectaton doesn't match: key %v doesn't exist", test.key)
}
expectedValue := test.expected.Attributes[test.key]
if value != expectedValue {
t.Errorf("value expectation doesn't match: expected %v, got %v", expectedValue, value)
}
})
}
}
func TestEdgeAttributes(t *testing.T) {
tests := map[string]struct {
attributes map[string]string
expected map[string]string
}{
"attribute label=my-label": {
attributes: map[string]string{
"label": "my-label",
},
expected: map[string]string{
"label": "my-label",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
properties := EdgeProperties{
Attributes: make(map[string]string),
}
EdgeAttributes(test.attributes)(&properties)
if !mapsAreEqual(test.expected, properties.Attributes) {
t.Errorf("expected %v, got %v", test.expected, properties.Attributes)
}
})
}
}
func TestVertexAttribute(t *testing.T) {
tests := map[string]struct {
key string
value string
expected VertexProperties
}{
"attribute label=my-label": {
key: "label",
value: "my-label",
expected: VertexProperties{
Attributes: map[string]string{
"label": "my-label",
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
properties := VertexProperties{
Attributes: make(map[string]string),
}
VertexAttribute(test.key, test.value)(&properties)
value, ok := properties.Attributes[test.key]
if !ok {
t.Errorf("attribute expectaton doesn't match: key %v doesn't exist", test.key)
}
expectedValue := test.expected.Attributes[test.key]
if value != expectedValue {
t.Errorf("value expectation doesn't match: expected %v, got %v", expectedValue, value)
}
})
}
}
func TestVertexAttributes(t *testing.T) {
tests := map[string]struct {
attributes map[string]string
expected map[string]string
}{
"attribute label=my-label": {
attributes: map[string]string{
"label": "my-label",
},
expected: map[string]string{
"label": "my-label",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
properties := VertexProperties{
Attributes: make(map[string]string),
}
VertexAttributes(test.attributes)(&properties)
if !mapsAreEqual(test.expected, properties.Attributes) {
t.Errorf("expected %v, got %v", test.expected, properties.Attributes)
}
})
}
}