-
Notifications
You must be signed in to change notification settings - Fork 33
/
graph_test.go
76 lines (73 loc) · 1.97 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
package dijkstra
import "testing"
func TestGetVertex(t *testing.T) {
g := NewGraph()
g.AddEmptyVertex(99)
if _, err := g.GetVertexArcs(99); err != nil {
t.Error("Getting vertex failed (99)")
}
if _, err := g.GetVertexArcs(100); err == nil {
t.Error("Vertex should not be found (100)")
}
}
func TestAddVertex(t *testing.T) {
g := NewGraph()
g.AddEmptyVertex(99)
_, err := g.GetVertexArcs(98)
if err == nil {
t.Error("should not have had ID set and err should not be nil")
}
for i := 0; i <= 10; i++ {
g.AddEmptyVertex(i)
}
v := g.AddNewEmptyVertex()
if v != 11 {
t.Error("Adding self assigned vertex fail")
}
g = NewGraph()
for i := 0; i <= 10; i++ {
g.AddNewEmptyVertex()
}
if v = g.AddNewEmptyVertex(); v != 11 {
t.Error("Adding self assigned vertex fail when extending slice")
}
if _, err := g.GetArc(11, 5); err == nil {
t.Error("Arc should not exist")
}
err = g.AddVertex(11, map[int]uint64{5: 1})
if err != nil {
t.Error("overwritting vertex should have succeeded")
}
if arc, err := g.GetArc(11, 5); err != nil || arc != 1 {
t.Error("Error getting arc", err, arc)
}
err = g.AddVertexAndArcs(11, map[int]uint64{5: 2})
if err != nil {
t.Error("adding vertex should have overwritten successfully")
}
if arc, err := g.GetArc(11, 5); err != nil || arc != 2 {
t.Error("Error getting arc")
}
err = g.AddVertex(100, map[int]uint64{101: 1})
if err == nil {
t.Error("arc is out of range, should have errored")
}
err = g.AddVertexAndArcs(100, map[int]uint64{101: 1})
if err != nil {
t.Error("adding vertex should have succeeded")
}
if len(g.vertexArcs) != 102 {
//as we should have created index 101 with arc
t.Error("vertexArcs should have been extended")
}
if g.vertexArcs[101] == nil {
t.Error("arc should have been added")
}
}
func TestValidateCorrect(t *testing.T) {
for i, test := range testGraphsCorrect {
if test.graph.validate() != nil {
t.Error("Graph ", i, "not valid;\n", test.graph.validate(), " should be nil")
}
}
}