-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinding_test.go
177 lines (162 loc) · 4.47 KB
/
pathfinding_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
package main
import (
"fmt"
pathfinding "github.com/xarg/gopathfinding"
"math/rand"
"strings"
"testing"
)
const MAP0 = `s....
.....
##.##
.....
....e`
const MAP1 = `............................
............................
.............#..............
.............#..............
.......e.....#..............
.............#..............
.............#..............
.............#..............
.............#..............
.............#.........s....
............................`
const MAP2 = `............................
............................
.............#..............
.............#..............
.......e.....#..............
.............#..............
.............#..............
.............#..............
.............#.......#######
.............#.......#.s....
.....................#......`
const MAP3 = `............................
............................
............................
............................
............................
.........#..................
.......s#...................
.........#..................
..........##................
............##...e...##.....
..............##...##.......
................###.........
............................`
func read_map(map_str string) *pathfinding.MapData {
rows := strings.Split(map_str, "\n")
if len(rows) == 0 {
panic("The map needs to have at least 1 row")
}
row_count := len(rows)
col_count := len(rows[0])
result := *pathfinding.NewMapData(row_count, col_count)
for i := 0; i < row_count; i++ {
for j := 0; j < col_count; j++ {
char := rows[i][j]
switch char {
case '.':
result[i][j] = pathfinding.LAND
case '#':
result[i][j] = pathfinding.WALL
case 's':
result[i][j] = pathfinding.START
case 'e':
result[i][j] = pathfinding.STOP
}
}
}
return &result
}
func str_map(data *pathfinding.MapData, nodes []*pathfinding.Node) string {
var result string
for i, row := range *data {
for j, cell := range row {
added := false
for _, node := range nodes {
if node.X == i && node.Y == j {
result += "+"
added = true
break
}
}
if !added {
switch cell {
case pathfinding.LAND:
result += "."
case pathfinding.WALL:
result += "#"
case pathfinding.START:
result += "s"
case pathfinding.STOP:
result += "e"
default: //Unknown
result += "?"
}
}
}
result += "\n"
}
return result
}
//Generate a random pathfinding.MapData given some dimensions
func generate_map(n int) *pathfinding.MapData {
map_data := *pathfinding.NewMapData(n, n)
map_data[0][0] = pathfinding.START
map_data[n-1][n-1] = pathfinding.STOP
xs := rand.Perm(n - 1)
ys := rand.Perm(n - 1)
for i := 1; i < len(xs); i += rand.Intn(4) + 1 {
for j := 1; j < len(ys); j++ {
map_data[xs[i]][ys[j]] = pathfinding.WALL
}
}
return &map_data
}
func TestAstar0(t *testing.T) {
map_data := read_map(MAP0) //Read map data and create the map_data
graph := pathfinding.NewGraph(map_data) //Create a new graph
nodes_path := pathfinding.Astar(graph) //Get the shortest path
fmt.Println(str_map(map_data, nodes_path))
if len(nodes_path) != 9 {
t.Errorf("Expected 9. Got %d", len(nodes_path))
}
}
func TestAstar1(t *testing.T) {
map_data := read_map(MAP1) //Read map data and create the map_data
graph := pathfinding.NewGraph(map_data) //Create a new graph
nodes_path := pathfinding.Astar(graph) //Get the shortest path
fmt.Println(str_map(map_data, nodes_path))
if len(nodes_path) != 24 {
t.Errorf("Expected 24. Got %d", len(nodes_path))
}
}
func TestAstar2(t *testing.T) {
map_data := read_map(MAP2) //Read map data and create the map_data
graph := pathfinding.NewGraph(map_data) //Create a new graph
nodes_path := pathfinding.Astar(graph) //Get the shortest path
fmt.Println(str_map(map_data, nodes_path))
if len(nodes_path) != 0 {
t.Errorf("Expected 0. Got %d", len(nodes_path))
}
}
func TestAstar3(t *testing.T) {
map_data := read_map(MAP3) //Read map data and create the map_data
graph := pathfinding.NewGraph(map_data) //Create a new graph
nodes_path := pathfinding.Astar(graph) //Get the shortest path
fmt.Println(str_map(map_data, nodes_path))
if len(nodes_path) != 18 {
t.Errorf("Expected 18. Got %d", len(nodes_path))
}
}
func BenchmarkAstar1(b *testing.B) {
b.StopTimer()
fmt.Printf("Benchmarking with a %dx%d map\n", b.N, b.N)
map_data := generate_map(b.N + 5)
graph := pathfinding.NewGraph(map_data)
b.StartTimer()
pathfinding.Astar(graph)
}