forked from ZenGround0/go-dot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_test.go
241 lines (204 loc) · 4.28 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
package dot
import (
"bytes"
"fmt"
"strings"
"testing"
)
var emptyGraph = `digraph testGraph {
}`
func TestMakeWriteEmptyGraph(t *testing.T) {
buf := new(bytes.Buffer)
g := NewGraph("testGraph")
err := g.WriteDot(buf)
if err != nil {
t.Fatal(err)
}
s := buf.String()
if s != emptyGraph {
t.Errorf("unexpected output: \n%s", s)
}
}
var edgeGraph = `digraph testGraph {
vTo -> vFrom
}`
func ExampleGraph_AddEdge() {
buf := new(bytes.Buffer)
g := NewGraph("testGraph")
vTo := &VertexDescription {
ID: "vTo",
}
vFrom := &VertexDescription {
ID: "vFrom",
}
g.AddEdge(vTo, vFrom, true)
g.WriteDot(buf)
s := buf.String()
lines := strings.Split(s, "\n")
fmt.Printf("%d\n", len(lines))
if len(lines) < 3 {
return
}
fmt.Println(lines[2])
// Output:
// 5
// vTo -> vFrom
}
var vertexGraph = `digraph testGraph {
v [label="vertex"]
}`
func TestAddVertex(t *testing.T) {
buf := new(bytes.Buffer)
g := NewGraph("testGraph")
v := &VertexDescription {
ID: "v",
Label: "vertex",
}
g.AddVertex(v)
err := g.WriteDot(buf)
if err != nil{
t.Fatal(err)
}
s := buf.String()
if s != vertexGraph {
t.Errorf("unexpected output: \n%s\n",s)
t.Errorf("expected outpuf: \n%s", vertexGraph)
}
}
var commentGraph = `digraph testGraph {
/* This is a comment */
}`
func TestAddComment(t *testing.T) {
buf := new(bytes.Buffer)
g := NewGraph("testGraph")
g.AddComment("This is a comment")
err := g.WriteDot(buf)
if err != nil {
t.Fatal(err)
}
s := buf.String()
if s!= commentGraph {
t.Errorf("unexpeted output: \n%s\n",s)
}
}
var newlineGraph = `digraph testGraph {
}`
func TestAddNewLine(t *testing.T) {
buf := new(bytes.Buffer)
g := NewGraph("testGraph")
g.AddNewLine()
err := g.WriteDot(buf)
if err != nil {
t.Fatal(err)
}
s := buf.String()
if s!= newlineGraph {
t.Errorf("unexpeted output: \n%s\n",s)
t.Errorf("expected ouput: \n%s\n", newlineGraph)
}
}
var basicGraph = `digraph cluster {
/* The nodes of the connectivity graph */
/* The cluster-service peers */
C0 [label="EhD" color="blue2"]
C1 [label="DQJ" color="blue2"]
C2 [label="mJu" color="blue2"]
/* The ipfs peers */
I0 [label="Ssq" color="goldenrod"]
I1 [label="ZDV" color="goldenrod"]
I2 [label="suL" color="goldenrod"]
/* Edges representing active connections in the cluster */
/* The connections among cluster-service peers */
C0 -> C1
C0 -> C2
C1 -> C0
C1 -> C2
C2 -> C0
C2 -> C1
/* The connections between cluster peers and their ipfs daemons */
C0 -> I1
C1 -> I0
C2 -> I2
/* The swarm peer connections among ipfs daemons in the cluster */
I0 -> I1
I0 -> I2
I1 -> I0
I1 -> I2
I2 -> I0
I2 -> I1
}`
func TestPrintBasicGraph(t *testing.T) {
buf := new(bytes.Buffer)
g := NewGraph("cluster")
g.AddComment("The nodes of the connectivity graph")
g.AddComment("The cluster-service peers")
c0 := &VertexDescription{
ID: "C0",
Label: "EhD",
Color: "blue2",
}
c1 := &VertexDescription{
ID: "C1",
Label: "DQJ",
Color: "blue2",
}
c2 := &VertexDescription{
ID: "C2",
Label: "mJu",
Color: "blue2",
}
g.AddVertex(c0)
g.AddVertex(c1)
g.AddVertex(c2)
g.AddNewLine()
g.AddComment("The ipfs peers")
i0 := &VertexDescription{
ID: "I0",
Label: "Ssq",
Color: "goldenrod",
}
i1 := &VertexDescription{
ID: "I1",
Label: "ZDV",
Color: "goldenrod",
}
i2 := &VertexDescription{
ID: "I2",
Label: "suL",
Color: "goldenrod",
}
g.AddVertex(i0)
g.AddVertex(i1)
g.AddVertex(i2)
g.AddNewLine()
g.AddComment("Edges representing active connections in the cluster")
g.AddComment("The connections among cluster-service peers")
g.AddEdge(c0, c1, true)
g.AddEdge(c0, c2, true)
g.AddEdge(c1, c0, true)
g.AddEdge(c1, c2, true)
g.AddEdge(c2, c0, true)
g.AddEdge(c2, c1, true)
g.AddNewLine()
g.AddComment("The connections between cluster peers and their ipfs daemons")
g.AddEdge(c0, i1, true)
g.AddEdge(c1, i0, true)
g.AddEdge(c2, i2, true)
g.AddNewLine()
g.AddComment("The swarm peer connections among ipfs daemons in the cluster")
g.AddEdge(i0, i1, true)
g.AddEdge(i0, i2, true)
g.AddEdge(i1, i0, true)
g.AddEdge(i1, i2, true)
g.AddEdge(i2, i0, true)
g.AddEdge(i2, i1, true)
err := g.WriteDot(buf)
if err != nil {
t.Fatal(err)
}
s := buf.String()
if s != basicGraph {
t.Errorf("unexpected output \n%s\n", s)
t.Errorf("The expected output \n%s\n", basicGraph)
}
}