Added isWall and isPath to RawMaze with tests

This commit is contained in:
Karma Riuk 2023-08-05 12:02:42 +02:00
parent 130deb40d8
commit ee9d439485
2 changed files with 233 additions and 0 deletions

View File

@ -26,3 +26,11 @@ func (m *RawMaze) String() string {
return ret.String()
}
func (m *RawMaze) isPath(x int, y int) bool {
return m.Data[y][x] == m.PathChar
}
func (m *RawMaze) isWall(x int, y int) bool {
return m.Data[y][x] == m.WallChar
}

225
maze/raw_maze_test.go Normal file
View File

@ -0,0 +1,225 @@
package maze
import "testing"
func TestRawMazeWall(t *testing.T) {
tests := []struct {
name string
pathChar byte
wallChar byte
data []string
expected [][]bool
}{
{
"Trivial",
' ',
'#',
[]string{
"## ##",
"# #",
"### #",
},
[][]bool{
{true, true, false, true, true},
{true, false, false, false, true},
{true, true, true, false, true},
},
},
{
"Trivial Bigger",
' ',
'#',
[]string{
"### ###",
"### ###",
"# #",
"##### #",
"##### #",
},
[][]bool{
{true, true, true, false, true, true, true},
{true, true, true, false, true, true, true},
{true, false, false, false, false, false, true},
{true, true, true, true, true, false, true},
{true, true, true, true, true, false, true},
},
},
{
"Bigger Staggered",
' ',
'#',
[]string{
"### ###",
"### ###",
"# #",
"#### ##",
"#### ##",
},
[][]bool{
{true, true, true, false, true, true, true},
{true, true, true, false, true, true, true},
{true, false, false, false, false, false, true},
{true, true, true, true, false, true, true},
{true, true, true, true, false, true, true},
},
},
{
"Normal",
' ',
'#',
[]string{
"##### #####",
"# # #",
"##### ### #",
"# # #",
"# # ##### #",
"# # #",
"### ### # #",
"# # # #",
"# ####### #",
"# # #",
"##### #####",
},
[][]bool{
{true, true, true, true, true, false, true, true, true, true, true},
{true, false, false, false, false, false, true, false, false, false, true},
{true, true, true, true, true, false, true, true, true, false, true},
{true, false, false, false, true, false, false, false, false, false, true},
{true, false, true, false, true, true, true, true, true, false, true},
{true, false, true, false, false, false, false, false, false, false, true},
{true, true, true, false, true, true, true, false, true, false, true},
{true, false, false, false, true, false, false, false, true, false, true},
{true, false, true, true, true, true, true, true, true, false, true},
{true, false, false, false, false, false, true, false, false, false, true},
{true, true, true, true, true, false, true, true, true, true, true},
},
},
}
for _, test := range tests {
rawMaze := RawMaze{
PathChar: test.pathChar,
WallChar: test.wallChar,
Data: test.data,
}
for y, row := range test.expected {
for x, expected := range row {
if rawMaze.isWall(x, y) != expected {
t.Fatalf("Wanted wall at (%v, %v), apparently it isn't", x, y)
}
}
}
}
}
func TestRawMazePath(t *testing.T) {
tests := []struct {
name string
pathChar byte
wallChar byte
data []string
expected [][]bool
}{
{
"Trivial",
' ',
'#',
[]string{
"## ##",
"# #",
"### #",
},
[][]bool{
{false, false, true, false, false},
{false, true, true, true, false},
{false, false, false, true, false},
},
},
{
"Trivial Bigger",
' ',
'#',
[]string{
"### ###",
"### ###",
"# #",
"##### #",
"##### #",
},
[][]bool{
{false, false, false, true, false, false, false},
{false, false, false, true, false, false, false},
{false, true, true, true, true, true, false},
{false, false, false, false, false, true, false},
{false, false, false, false, false, true, false},
},
},
{
"Bigger Staggered",
' ',
'#',
[]string{
"### ###",
"### ###",
"# #",
"#### ##",
"#### ##",
},
[][]bool{
{false, false, false, true, false, false, false},
{false, false, false, true, false, false, false},
{false, true, true, true, true, true, false},
{false, false, false, false, true, false, false},
{false, false, false, false, true, false, false},
},
},
{
"Normal",
' ',
'#',
[]string{
"##### #####",
"# # #",
"##### ### #",
"# # #",
"# # ##### #",
"# # #",
"### ### # #",
"# # # #",
"# ####### #",
"# # #",
"##### #####",
},
[][]bool{
{false, false, false, false, false, true, false, false, false, false, false},
{false, true, true, true, true, true, false, true, true, true, false},
{false, false, false, false, false, true, false, false, false, true, false},
{false, true, true, true, false, true, true, true, true, true, false},
{false, true, false, true, false, false, false, false, false, true, false},
{false, true, false, true, true, true, true, true, true, true, false},
{false, false, false, true, false, false, false, true, false, true, false},
{false, true, true, true, false, true, true, true, false, true, false},
{false, true, false, false, false, false, false, false, false, true, false},
{false, true, true, true, true, true, false, true, true, true, false},
{false, false, false, false, false, true, false, false, false, false, false},
},
},
}
for _, test := range tests {
rawMaze := RawMaze{
PathChar: test.pathChar,
WallChar: test.wallChar,
Data: test.data,
}
for y, row := range test.expected {
for x, expected := range row {
if rawMaze.isPath(x, y) != expected {
t.Fatalf("Wanted path at (%v, %v), apparently it isn't", x, y)
}
}
}
}
}