-
Notifications
You must be signed in to change notification settings - Fork 1
/
giraffe_test.go
118 lines (84 loc) · 2.44 KB
/
giraffe_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
package giraffe
import (
"testing"
)
func TestAddVertex(t *testing.T) {
var g Graph
v0 := &Vertex{ Index: 0 }
g.AddVertex(v0)
err := g.AddVertex(v0)
if err == nil {
t.Errorf("Expected adding of duplicate vertex to throw an error")
}
}
func TestAddEdge(t *testing.T) {
var g Graph
v0 := &Vertex{ Index: 0 }
v1 := &Vertex{ Index: 1 }
v2 := &Vertex{ Index: 2 }
g.AddVertex(v0)
g.AddVertex(v1)
g.AddVertex(v2)
err := g.AddEdge(&Edge{Start: v0, End: v0})
g.AddEdge(&Edge{Start: v0, End: v1})
if err == nil {
t.Errorf("Expected cyclic edge creation to throw an error")
}
err = g.AddEdge(&Edge{Start: v1, End: v0})
if err == nil {
t.Errorf("Expected duplicate edge creation to throw an error")
}
}
func MakeTestGraph() *Graph {
var g Graph
v0 := &Vertex{ Index: 0 }
v1 := &Vertex{ Index: 1 }
v2 := &Vertex{ Index: 2 }
v3 := &Vertex{ Index: 3 }
g.AddVertex(v0)
g.AddVertex(v1)
g.AddVertex(v2)
g.AddVertex(v3)
g.AddEdge(&Edge{Start: v0, End: v3})
g.AddEdge(&Edge{Start: v0, End: v2})
g.AddEdge(&Edge{Start: v0, End: v1})
v0.SortSiblings()
g.AddEdge(&Edge{Start: v2, End: v1})
g.AddEdge(&Edge{Start: v2, End: v3})
v2.SortSiblings()
return &g
}
func TestFindVertex(t *testing.T) {
g := MakeTestGraph()
found, visited := g.FindVertexBFS(0, 3)
if found == nil {
t.Errorf("Expected to find vertex with index 3, but found nil")
}
const nodes_to_visit int = 4
if len(visited) != nodes_to_visit {
t.Errorf("Expected %d visited vertices, but got %d", nodes_to_visit, len(visited))
}
expectedIndices := map[int]bool{0: true, 1: true, 2: true, 3: true}
for _, vtx := range visited {
if !expectedIndices[vtx.Index] {
t.Errorf("Unexpected visited vertex index: %d", vtx.Index)
}
}
}
func TestFindVertexDFS(t *testing.T) {
g := MakeTestGraph()
found, visited := g.FindVertexDFS(2)
if found == nil {
t.Errorf("Expected to find vertex with index 2, but found nil")
}
const nodes_to_visit int = 3
if len(visited) != nodes_to_visit {
t.Errorf("Expected %d visited vertices, but got %d", nodes_to_visit, len(visited))
}
expectedIndices := map[int]bool{0: true, 3: true, 2: true}
for _, vtx := range visited {
if !expectedIndices[vtx.Index] {
t.Errorf("Unexpected visited vertex index: %d", vtx.Index)
}
}
}