2024-02-13 14:21:35 +00:00
|
|
|
import unittest
|
2024-02-15 19:13:31 +00:00
|
|
|
from cell import Cell, CellWallLabels
|
2024-02-15 14:58:51 +00:00
|
|
|
import maze
|
2024-02-14 22:50:46 +00:00
|
|
|
import errors
|
2024-02-13 14:21:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Tests(unittest.TestCase):
|
2024-02-15 16:36:18 +00:00
|
|
|
"""
|
|
|
|
Tests runs the test suite.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 14:21:35 +00:00
|
|
|
def test_maze_create_cell_grid(self):
|
2024-02-13 15:52:28 +00:00
|
|
|
"""
|
|
|
|
test_maze_create_cell_grid tests that the maze is constructed properly.
|
|
|
|
"""
|
2024-02-13 14:21:35 +00:00
|
|
|
cases = [
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"height": 6,
|
|
|
|
"width": 9,
|
2024-02-13 14:21:35 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"height": 3,
|
|
|
|
"width": 12,
|
2024-02-13 14:21:35 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"height": 4,
|
|
|
|
"width": 4,
|
2024-02-13 14:21:35 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
for case in cases:
|
2024-02-15 14:58:51 +00:00
|
|
|
m = maze.Maze(
|
2024-02-13 15:52:28 +00:00
|
|
|
0,
|
|
|
|
0,
|
2024-02-16 23:27:09 +00:00
|
|
|
case["height"],
|
|
|
|
case["width"],
|
2024-02-13 15:52:28 +00:00
|
|
|
2,
|
|
|
|
2,
|
2024-02-15 03:30:30 +00:00
|
|
|
None,
|
|
|
|
None,
|
2024-02-13 15:52:28 +00:00
|
|
|
)
|
2024-02-18 09:10:34 +00:00
|
|
|
m.generate()
|
2024-02-13 15:52:28 +00:00
|
|
|
self.assertEqual(
|
2024-02-16 23:27:09 +00:00
|
|
|
len(m._cell_grid),
|
|
|
|
case["height"],
|
2024-02-13 15:52:28 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2024-02-16 23:27:09 +00:00
|
|
|
len(m._cell_grid[0]),
|
|
|
|
case["width"],
|
2024-02-13 15:52:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_break_entrance_and_exit(self):
|
|
|
|
"""
|
|
|
|
test_break_entrance_and_exit tests to ensure that the Maze's entry and
|
|
|
|
exit Cells are open.
|
|
|
|
"""
|
|
|
|
number_of_cell_rows = 5
|
|
|
|
number_of_cells_per_row = 20
|
2024-02-15 14:58:51 +00:00
|
|
|
m = maze.Maze(
|
2024-02-13 15:52:28 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
number_of_cell_rows,
|
|
|
|
number_of_cells_per_row,
|
|
|
|
2,
|
|
|
|
2,
|
2024-02-15 03:30:30 +00:00
|
|
|
None,
|
|
|
|
None,
|
2024-02-13 15:52:28 +00:00
|
|
|
)
|
2024-02-18 08:53:36 +00:00
|
|
|
m.generate()
|
2024-02-16 23:27:09 +00:00
|
|
|
self.assertFalse(m._cell_grid[0][0].wall_exists(CellWallLabels.TOP))
|
2024-02-13 15:52:28 +00:00
|
|
|
self.assertFalse(
|
2024-02-16 23:27:09 +00:00
|
|
|
m._cell_grid[number_of_cell_rows - 1]
|
2024-02-15 19:13:31 +00:00
|
|
|
[number_of_cells_per_row - 1].wall_exists(CellWallLabels.BOTTOM)
|
2024-02-14 22:50:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_invalid_cell_exception(self):
|
|
|
|
"""
|
|
|
|
test_invalid_cell_exception tests the exception for when an attempt
|
|
|
|
is made to create an invalid Cell.
|
|
|
|
"""
|
|
|
|
cases = [
|
|
|
|
{"x1": 30, "y1": 50, "x2": 20, "y2": 100},
|
|
|
|
{"x1": 30, "y1": 50, "x2": 40, "y2": 25},
|
|
|
|
]
|
|
|
|
|
|
|
|
for case in cases:
|
|
|
|
with self.assertRaises(errors.CellInvalidError):
|
|
|
|
_ = Cell(
|
|
|
|
x1=case["x1"],
|
|
|
|
y1=case["y1"],
|
|
|
|
x2=case["x2"],
|
|
|
|
y2=case["y2"]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_cell_too_small_exception(self):
|
|
|
|
"""
|
2024-02-15 14:58:51 +00:00
|
|
|
test_cell_too_small_exception tests the exception for when an attempt
|
2024-02-14 22:50:46 +00:00
|
|
|
is made to create a Cell that's too small.
|
|
|
|
"""
|
|
|
|
cases = [
|
|
|
|
{"x1": 1, "y1": 50, "x2": 2, "y2": 100},
|
|
|
|
{"x1": 30, "y1": 25, "x2": 40, "y2": 25},
|
|
|
|
]
|
|
|
|
|
|
|
|
for case in cases:
|
|
|
|
with self.assertRaises(errors.CellTooSmallError):
|
|
|
|
_ = Cell(
|
|
|
|
x1=case["x1"],
|
|
|
|
y1=case["y1"],
|
|
|
|
x2=case["x2"],
|
|
|
|
y2=case["y2"]
|
|
|
|
)
|
2024-02-13 14:21:35 +00:00
|
|
|
|
2024-02-15 14:58:51 +00:00
|
|
|
def test_maze_position_equality(self):
|
|
|
|
cases = [
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"m1": maze.MazePosition(i=1, j=3, last_i=10, last_j=100),
|
|
|
|
"m2": maze.MazePosition(i=1, j=3, last_i=10, last_j=100),
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": True,
|
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"m1": maze.MazePosition(i=1, j=3, last_i=10, last_j=100),
|
|
|
|
"m2": maze.MazePosition(i=100, j=30, last_i=200, last_j=100),
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": False,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
for case in cases:
|
|
|
|
result = case["m1"] == case["m2"]
|
|
|
|
self.assertEqual(result, case["expected"])
|
|
|
|
|
|
|
|
def test_maze_position_adjacent_positition(self):
|
|
|
|
cases = [
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=3, j=4, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.ABOVE,
|
|
|
|
"expected": maze.MazePosition(i=2, j=4, last_i=10, last_j=10),
|
2024-02-15 14:58:51 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=9, j=4, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.BELOW,
|
|
|
|
"expected": maze.MazePosition(i=10, j=4, last_i=10, last_j=10),
|
2024-02-15 14:58:51 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=1, j=1, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.LEFT,
|
|
|
|
"expected": maze.MazePosition(i=1, j=0, last_i=10, last_j=10),
|
2024-02-15 14:58:51 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=3, j=9, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.RIGHT,
|
|
|
|
"expected": maze.MazePosition(i=3, j=10, last_i=10, last_j=10),
|
2024-02-15 14:58:51 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=0, j=4, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.ABOVE,
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": None,
|
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=10, j=4, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.BELOW,
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": None,
|
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=1, j=0, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.LEFT,
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": None,
|
|
|
|
},
|
|
|
|
{
|
2024-02-16 23:27:09 +00:00
|
|
|
"position": maze.MazePosition(i=3, j=10, last_i=10, last_j=10),
|
|
|
|
"direction": maze.MazeDirection.RIGHT,
|
2024-02-15 14:58:51 +00:00
|
|
|
"expected": None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
for case in cases:
|
|
|
|
result = case["position"].get_adjacent_position(case["direction"])
|
|
|
|
self.assertEqual(result, case["expected"])
|
|
|
|
|
2024-02-13 14:21:35 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|