-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_test.go
197 lines (167 loc) · 3.86 KB
/
maze_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
package ggworkshop
import (
"errors"
"fmt"
"strings"
"testing"
)
func CompareMaze(m *Maze, bs [][]bool) error {
var err error
m.Range(func(x, y int) bool {
switch m.Map[y][x].Type {
case Road, Exit:
if !bs[y][x] {
err = errors.New(fmt.Sprintf("(%d, %d) => (%v) is not a wall", x, y, m.Map[y][x].Type))
}
case Wall:
if bs[y][x] {
err = errors.New(fmt.Sprintf("(%d, %d) => (%v) is a wall", x, y, m.Map[y][x].Type))
}
case Duplicate:
err = errors.New(fmt.Sprintf("Duplicate (%d, %d)\r\n", x, y))
}
return false
})
return err
}
func PrintBools(bs [][]bool) {
builder := strings.Builder{}
for y := range bs {
for x := range bs[y] {
if bs[y][x] {
builder.WriteString(".")
} else {
builder.WriteString("x")
}
}
builder.WriteString("\r\n")
}
fmt.Println(builder.String())
}
func TestParse4x4(t *testing.T) {
m, err := FromFile("./mazes/4x4.maze")
if err != nil {
t.Error(err)
return
}
bs := [][]bool{
{false, true, false, false},
{false, true, true, false},
{false, false, true, false},
{false, false, true, false},
}
if err := CompareMaze(m, bs); err != nil {
PrintBools(bs)
fmt.Println(m)
t.Error(err)
}
}
func TestParse5x5(t *testing.T) {
m, err := FromFile("./mazes/5x5.maze")
if err != nil {
t.Error(err)
return
}
bs := [][]bool{
{false, true, false, false, false},
{false, true, true, true, false},
{false, false, false, true, false},
{false, true, true, true, false},
{false, false, false, true, false},
}
if err := CompareMaze(m, bs); err != nil {
fmt.Println(m)
t.Error(err)
}
}
func TestParse6x6(t *testing.T) {
m, err := FromFile("./mazes/6x6.maze")
if err != nil {
t.Error(err)
return
}
bs := [][]bool{
{false, true, false, false, false, false},
{false, true, false, false, false, false},
{false, true, true, true, false, false},
{false, false, true, false, true, false},
{false, false, true, true, true, false},
{false, false, false, true, false, false},
}
if err := CompareMaze(m, bs); err != nil {
fmt.Println(m)
t.Error(err)
}
}
func TestExplore4x4(t *testing.T) {
m, err := FromFile("./mazes/4x4.maze")
if err != nil {
t.Error(err)
}
out := make(chan []Path)
go m.Explore(m.Map[0][1], Path{}, out)
paths := <-out
p := paths[1]
if len(p) != 5 {
fmt.Println(len(p))
fmt.Println(p)
t.Errorf("The length of the route should be 5, not %d", len(p))
}
}
func TestRange(t *testing.T) {
m, err := FromFile("./mazes/4x4.maze")
if err != nil {
t.Error(err)
return
}
var count int
m.Range(func(x, y int) bool {
count++
return true
})
if count != 16 {
t.Errorf("Ranges counts to 16 not %d", count)
}
count = 0
m.Range(func(x, y int) bool {
count++
return count != 10
})
if count != 10 {
t.Errorf("The range is terminated when the count is 10 not %d", count)
}
}
func TestFindExits(t *testing.T) {
m, err := FromFile("./mazes/4x4.maze")
if err != nil {
t.Error(err)
return
}
if len(m.Exits) != 2 {
t.Errorf("There are 2 exits, not %d", len(m.Exits))
}
if m.Exits[0].Coordinate.X != 1 || m.Exits[0].Coordinate.Y != 0 {
t.Errorf("The first exit has (X,Y)(1,0), not (%d, %d)", m.Exits[0].Coordinate.X, m.Exits[0].Coordinate.Y)
}
if m.Exits[1].Coordinate.X != 2 || m.Exits[1].Coordinate.Y != 3 {
t.Errorf("The first exit has (X,Y)(2,3), not (%d, %d)", m.Exits[1].Coordinate.X, m.Exits[1].Coordinate.Y)
}
}
func TestLook(t *testing.T) {
m, err := FromFile("./mazes/4x4.maze")
if err != nil {
t.Error(err)
return
}
tileType, tile := m.Look(South, m.Map[0][1], Path{})
if tileType != Wall {
t.Errorf("We are looking at a wall, not %s", tileType)
}
if tile.Y != 1 {
t.Errorf("We moved south. The tile should be at Y=1 not %d", tile.Y)
}
tileType, tile = m.Look(South, m.Map[0][1], Path{&m.Map[1][1].Coordinate})
if tileType != Duplicate {
t.Errorf("We are looking at a wall, not %s", tileType)
}
}