-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_test.go
174 lines (143 loc) · 3.74 KB
/
search_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
package grapho
import (
"testing"
)
// equalPath compares the given path with the expected path
func equalPath(s1, s2 []uint64) bool {
if len(s1) != len(s2) {
return false
}
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
return false
}
}
return true
}
// TestSearch tests all 4 search algorithms, with both directed and undirected graphs
func TestSearch(t *testing.T) {
// Dijkstra
testDijkstra(t, sampleGraph())
testDijkstra(t, sampleDiGraph())
// A*
testAstar(t, sampleGraph())
testAstar(t, sampleDiGraph())
// BFS
testBreadthFirstSearch(t, sampleGraph())
testBreadthFirstSearch(t, sampleDiGraph())
// DFS
testDepthFirstSearch(t, sampleGraph())
testDepthFirstSearch(t, sampleDiGraph())
}
// testDijkstra tests the Dijkstra algorithm with the given graph
func testDijkstra(t *testing.T, g *Graph) {
expected := []uint64{1, 2, 5, 8}
path, err := Search(g, 1, 8, Dijkstra, nil)
if err != nil {
t.Fatalf("Dijkstra: %v", err)
} else if !equalPath(path, expected) {
t.Errorf("Path: %v. Expected: %v", path, expected)
}
path, err = Search(g, 1, 10, Dijkstra, nil)
if err == nil {
t.Error("Dijkstra: Did not get expected error")
}
}
// testAstar tests the A* algorithm with an heuristic that
// assigns more priority to numbers closer to 9
func testAstar(t *testing.T, g *Graph) {
h := func(node, goal uint64) int {
return (int)(goal - node)
}
path, err := Search(g, 1, 9, Astar, h)
if err != nil {
t.Fatalf("Astar: %v", err)
}
expected := []uint64{1, 3, 6, 8, 9}
if !equalPath(path, expected) {
t.Errorf("Path: %v. Expected: %v", path, expected)
}
}
// testBreadthFirstSearch tests BreadthFirstSearch algorithm with the given graph
func testBreadthFirstSearch(t *testing.T, g *Graph) {
path, err := Search(g, 1, 9, BreadthFirstSearch, nil)
if err != nil {
t.Fatalf("BreadthFirstSearch: %v", err)
}
expected := []uint64{1, 2, 4, 7, 9}
if !equalPath(path, expected) {
t.Errorf("Path: %v. Expected: %v", path, expected)
}
}
// testDepthFirstSearch tests DepthFirstSearch algorithm with the given graph
func testDepthFirstSearch(t *testing.T, g *Graph) {
path, err := Search(g, 1, 9, DepthFirstSearch, nil)
if err != nil {
t.Fatalf("DepthFirstSearch: %v", err)
}
expected := []uint64{1, 2, 4, 7, 5, 3, 6, 8, 9}
if !equalPath(path, expected) {
t.Errorf("Path: %v. Expected: %v", path, expected)
}
}
// sampleGraph creates a simple Graph for testing purposes
func sampleGraph() *Graph {
// 1 2 4
// 3 5 7
// 6 8 9
g := NewGraph(false) // Directed Graph
g.AddEdge(1, 2, 1, nil)
g.AddEdge(1, 3, 1, nil)
g.AddEdge(2, 4, 1, nil)
g.AddEdge(2, 5, 1, nil)
g.AddEdge(3, 5, 1, nil)
g.AddEdge(3, 6, 1, nil)
g.AddEdge(4, 7, 1, nil)
g.AddEdge(5, 7, 1, nil)
g.AddEdge(5, 8, 1, nil)
g.AddEdge(6, 8, 1, nil)
g.AddEdge(7, 9, 1, nil)
g.AddEdge(8, 9, 1, nil)
return g
}
// sampleDiGraph creates a simple DiGraph for testing purposes
func sampleDiGraph() *Graph {
// 1 2 4
// 3 5 7
// 6 8 9
g := NewGraph(true) // Directed Graph
g.AddNode(1, nil)
g.AddNode(2, nil)
g.AddNode(3, nil)
g.AddNode(4, nil)
g.AddNode(5, nil)
g.AddNode(6, nil)
g.AddNode(7, nil)
g.AddNode(8, nil)
g.AddNode(9, nil)
g.AddEdge(1, 2, 1, nil)
g.AddEdge(1, 3, 1, nil)
g.AddEdge(2, 1, 1, nil)
g.AddEdge(2, 4, 1, nil)
g.AddEdge(2, 5, 1, nil)
g.AddEdge(3, 1, 1, nil)
g.AddEdge(3, 5, 1, nil)
g.AddEdge(3, 6, 1, nil)
g.AddEdge(4, 2, 1, nil)
g.AddEdge(4, 7, 1, nil)
g.AddEdge(5, 2, 1, nil)
g.AddEdge(5, 3, 1, nil)
g.AddEdge(5, 7, 1, nil)
g.AddEdge(5, 8, 1, nil)
g.AddEdge(6, 3, 1, nil)
g.AddEdge(6, 8, 1, nil)
g.AddEdge(7, 4, 1, nil)
g.AddEdge(7, 5, 1, nil)
g.AddEdge(7, 9, 1, nil)
g.AddEdge(8, 5, 1, nil)
g.AddEdge(8, 6, 1, nil)
g.AddEdge(8, 9, 1, nil)
g.AddEdge(9, 7, 1, nil)
g.AddEdge(9, 8, 1, nil)
return g
}