From ee9d439485ee8d517937dcbac94126b26f391212 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 5 Aug 2023 12:02:42 +0200 Subject: [PATCH] Added isWall and isPath to RawMaze with tests --- maze/raw_maze.go | 8 ++ maze/raw_maze_test.go | 225 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 maze/raw_maze_test.go diff --git a/maze/raw_maze.go b/maze/raw_maze.go index 78d57c8..889494d 100644 --- a/maze/raw_maze.go +++ b/maze/raw_maze.go @@ -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 +} diff --git a/maze/raw_maze_test.go b/maze/raw_maze_test.go new file mode 100644 index 0000000..c8ade78 --- /dev/null +++ b/maze/raw_maze_test.go @@ -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) + } + } + } + } +}