forked from dominikbraun/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
135 lines (114 loc) · 2.8 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
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 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 {
properties := EdgeProperties{
Attributes: make(map[string]string),
}
EdgeAttribute(test.key, test.value)(&properties)
value, ok := properties.Attributes[test.key]
if !ok {
t.Errorf("%s: attribute expectaton doesn't match: key %v doesn't exist", name, test.key)
}
expectedValue := test.expected.Attributes[test.key]
if value != expectedValue {
t.Errorf("%s: value expectation doesn't match: expected %v, got %v", name, expectedValue, value)
}
}
}