-
Notifications
You must be signed in to change notification settings - Fork 7
/
graph_test.go
66 lines (53 loc) · 1.41 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
package gograph
import (
"reflect"
"testing"
)
func TestVertex(t *testing.T) {
g := New[string]()
vA := g.AddVertexByLabel("A")
vB := g.AddVertexByLabel("B")
vC := g.AddVertexByLabel("C")
_, err := g.AddEdge(vA, vB)
if err != nil {
t.Errorf(testErrMsgError, err)
}
_, err = g.AddEdge(vA, vC)
if err != nil {
t.Errorf(testErrMsgError, err)
}
v := vA.NeighborByLabel("B")
if !reflect.DeepEqual(vB, v) {
t.Errorf(testErrMsgNotEqual, vB, v)
}
if !vA.HasNeighbor(vC) {
t.Error(testErrMsgNotTrue)
}
if vA.HasNeighbor(NewVertex("D")) {
t.Error(testErrMsgNotFalse)
}
if vA.OutDegree() != 2 {
t.Errorf(testErrMsgNotEqual, 2, vA.OutDegree())
}
// test cloning neighbors
neighbors := vA.Neighbors()
if len(neighbors) != len(vA.neighbors) {
t.Errorf(testErrMsgNotEqual, len(neighbors), len(vA.neighbors))
}
neighbors[0].label = "D"
if neighbors[0].Label() == vA.neighbors[0].Label() {
t.Errorf(testErrMsgNotFalse)
}
}
func TestEdge_OtherVertex(t *testing.T) {
edge := NewEdge[int](NewVertex(1), NewVertex(2))
if edge.OtherVertex(3) != nil {
t.Errorf("Expect OtherVertex return nil, but get %+v", edge.OtherVertex(3))
}
if edge.OtherVertex(1).label != edge.Destination().Label() {
t.Errorf("Expect OtherVertex return 2, but get %+v", edge.OtherVertex(1))
}
if edge.OtherVertex(2).label != edge.Source().Label() {
t.Errorf("Expect OtherVertex return 1, but get %+v", edge.OtherVertex(2))
}
}