-
Notifications
You must be signed in to change notification settings - Fork 10
/
graph_test.go
120 lines (92 loc) · 2.15 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
package graphs
import "testing"
func TestGraphNVertices(t *testing.T) {
graph := NewGraph[string]()
if graph.NVertices() != 0 {
t.Error("empty graph should not have any vertices")
}
graph.AddEdge("a", "b", 0)
if graph.NVertices() != 2 {
t.Error("graph should have two vertices")
}
}
func TestEachVertex(t *testing.T) {
graph := NewGraph[string]()
graph.AddEdge("a", "b", 0)
graph.AddEdge("b", "c", 0)
vertices := 0
graph.EachVertex(func(_ string, _ func()) {
vertices++
})
if vertices != graph.NVertices() {
t.Error("wrong number of vertices")
}
}
func TestEachVertexStop(t *testing.T) {
graph := NewGraph[string]()
graph.AddEdge("a", "b", 0)
graph.AddEdge("b", "c", 0)
vertices := 0
graph.EachVertex(func(_ string, stop func()) {
vertices++
stop()
})
if vertices != 1 {
t.Error("wrong number of vertices")
}
}
func TestEachEdge(t *testing.T) {
graph := NewGraph[string]()
graph.AddEdge("a", "b", 0)
graph.AddEdge("b", "c", 0)
edges := 0
graph.EachEdge(func(_ Edge[string], _ func()) {
edges++
})
if edges != graph.NEdges()*2 {
t.Errorf("wrong number of edges: %d", edges)
}
}
func TestEachEdgeStop(t *testing.T) {
graph := NewGraph[string]()
graph.AddEdge("a", "b", 0)
graph.AddEdge("b", "c", 0)
edges := 0
graph.EachEdge(func(_ Edge[string], stop func()) {
edges++
stop()
})
if edges != 1 {
t.Errorf("wrong number of edges: %d", edges)
}
}
func TestGraphNEdges(t *testing.T) {
graph := NewGraph[string]()
if graph.NEdges() != 0 {
t.Error("empty graph should not have any edges")
}
graph.AddEdge("a", "b", 0)
if graph.NEdges() != 1 {
t.Error("graph should have one edge")
}
}
func TestGraphEquals(t *testing.T) {
g1 := NewGraph[string]()
g2 := NewGraph[string]()
if !g1.Equals(g2) {
t.Error("two empty graphs should be equal")
}
g1.AddEdge("a", "b", 0)
if g1.Equals(g2) {
t.Error("two graphs with different number of edges should not be equal")
}
g2.AddEdge("a", "b", 0)
if !g1.Equals(g2) {
t.Error("two graphs with same edges should be equal")
}
g1.AddEdge("b", "c", 0)
g2.AddEdge("a", "c", 0)
if g1.Equals(g2) {
t.Error("two graphs with different edges should not be equal")
}
}