class TestSingleGrid(unittest.TestCase): ''' Test the SingleGrid object. Since it inherits from Grid, all the functionality tested above should work here too. Instead, this tests the enforcement. ''' def setUp(self): ''' Create a test non-toroidal grid and populate it with Mock Agents ''' width = 3 height = 5 self.grid = SingleGrid(width, height, True) self.agents = [] counter = 0 for x in range(width): for y in range(height): if TEST_GRID[x][y] == 0: continue counter += 1 # Create and place the mock agent a = MockAgent(counter, None) self.agents.append(a) self.grid.place_agent(a, (x, y)) def test_enforcement(self): ''' Test the SingleGrid empty count and enforcement. ''' assert len(self.grid.empties) == 9 a = MockAgent(100, None) with self.assertRaises(Exception): self.grid._place_agent((0, 1), a) # Place the agent in an empty cell self.grid.position_agent(a) # Test whether after placing, the empty cells are reduced by 1 assert a.pos not in self.grid.empties assert len(self.grid.empties) == 8 for i in range(10): self.grid.move_to_empty(a) assert len(self.grid.empties) == 8 # Place agents until the grid is full empty_cells = len(self.grid.empties) for i in range(empty_cells): a = MockAgent(101 + i, None) self.grid.position_agent(a) assert len(self.grid.empties) == 0 a = MockAgent(110, None) with self.assertRaises(Exception): self.grid.position_agent(a) with self.assertRaises(Exception): self.move_to_empty(self.agents[0])
class TestSingleGrid(unittest.TestCase): ''' Test the SingleGrid object. Since it inherits from Grid, all the functionality tested above should work here too. Instead, this tests the enforcement. ''' def setUp(self): ''' Create a test non-toroidal grid and populate it with Mock Agents ''' self.grid = SingleGrid(3, 5, True) self.agents = [] counter = 0 for y in range(3): for x in range(5): if TEST_GRID[y][x] == 0: continue counter += 1 # Create and place the mock agent a = MockAgent(counter, None) self.agents.append(a) self.grid.place_agent(a, (x, y)) def test_enforcement(self): ''' Test the SingleGrid empty count and enforcement. ''' assert len(self.grid.empties) == 10 a = MockAgent(100, None) with self.assertRaises(Exception): self.grid._place_agent((1, 0), a) # Place the agent in an empty cell self.grid.position_agent(a) assert a.pos not in self.grid.empties assert len(self.grid.empties) == 9 for i in range(10): self.grid.move_to_empty(a) assert len(self.grid.empties) == 9 # Place agents until the grid is full for i in range(9): a = MockAgent(101 + i, None) self.grid.position_agent(a) assert len(self.grid.empties) == 0 a = MockAgent(110, None) with self.assertRaises(Exception): self.grid.position_agent(a) with self.assertRaises(Exception): self.move_to_empty(self.agents[0])