class BasicTest(unittest.TestCase): def setUp(self): self.game = GameOfLife(10, 10) def test_set_alive(self): self.game.set_alive((0, 0)) self.assertTrue(self.game.state[0][0]) self.assertIn((0, 0), self.game.living_cells) def test_set_dead(self): self.game.set_alive((0, 0)) self.assertTrue(self.game.state[0][0]) self.assertIn((0, 0), self.game.living_cells) self.game.set_dead((0, 0)) self.assertFalse(self.game.state[0][0]) self.assertNotIn((0, 0), self.game.living_cells) def test_get_neighbours(self): neighbours = self.game.get_neighbours((0, 0)) self.assertSetEqual(neighbours, set([(0, 1), (1, 0), (1, 1)])) neighbours = self.game.get_neighbours((0, 5)) self.assertSetEqual(neighbours, set( [(0, 6), (0, 4), (1, 4), (1, 5), (1, 6)])) neighbours = self.game.get_neighbours((5, 0)) self.assertSetEqual(neighbours, set( [(5, 1), (4, 0), (4, 1), (6, 0), (6, 1)])) neighbours = self.game.get_neighbours((5, 5)) self.assertSetEqual(neighbours, set( [(5, 6), (5, 4), (4, 4), (4, 5), (4, 6), (6, 4), (6, 5), (6, 6)])) def test_life_over(self): self.game.set_alive((0, 1)) self.game.set_alive((1, 0)) self.game.set_alive((2, 0)) self.game.update() self.assertEqual(len(self.game.living_cells), 2) self.assertIn((1, 0), self.game.living_cells) self.assertIn((1, 1), self.game.living_cells) self.game.update() self.assertTrue(self.game.life_over()) def test_update(self): self.game.set_alive((0, 1)) self.game.set_alive((1, 0)) self.game.set_alive((2, 0)) self.game.update() self.assertEqual(len(self.game.living_cells), 2) self.assertIn((1, 0), self.game.living_cells) self.assertIn((1, 1), self.game.living_cells) def test_reset(self): self.game.set_alive((0, 1)) self.game.set_alive((1, 1)) self.game.set_alive((2, 1)) self.game.update() self.assertEqual(len(self.game.living_cells), 3) self.assertSetEqual( set([(1, 0), (1, 1), (1, 2)]), self.game.living_cells) self.game.reset() self.assertEqual(len(self.game.living_cells), 0) self.assertFalse(any(any(row) for row in self.game.state))
class ShapeTest(unittest.TestCase): '''Test for various transitions Taken from http://pi.math.cornell.edu/~lipa/mec/lesson6.html ''' def setUp(self): self.game = GameOfLife(10, 10) def test_triomino_1(self): '''Test for following transition □ □ □ => □ □ □ => □ => so on □ □ ''' self.game.set_alive((0, 1)) self.game.set_alive((1, 1)) self.game.set_alive((2, 1)) self.game.update() self.assertEqual(len(self.game.living_cells), 3) self.assertSetEqual( set([(1, 0), (1, 1), (1, 2)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 3) self.assertSetEqual( set([(0, 1), (1, 1), (2, 1)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 3) self.assertSetEqual( set([(1, 0), (1, 1), (1, 2)]), self.game.living_cells) def test_triomino_2(self): '''Test for following transitions □ □ => □ => Nothing □ ''' self.game.set_alive((0, 2)) self.game.set_alive((1, 1)) self.game.set_alive((2, 0)) self.game.update() self.assertEqual(len(self.game.living_cells), 1) self.assertSetEqual(set([(1, 1)]), self.game.living_cells) self.game.update() self.assertTrue(self.game.life_over()) def test_tetromino_1(self): '''Test for following transitions □ □ => □ □ => Stable □ □ □ □ ''' self.game.set_alive((0, 0)) self.game.set_alive((0, 1)) self.game.set_alive((1, 0)) self.game.set_alive((1, 1)) self.game.update() self.assertEqual(len(self.game.living_cells), 4) self.assertSetEqual( set([(0, 0), (0, 1), (1, 0), (1, 1)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 4) self.assertSetEqual( set([(0, 0), (0, 1), (1, 0), (1, 1)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 4) self.assertSetEqual( set([(0, 0), (0, 1), (1, 0), (1, 1)]), self.game.living_cells) def test_tetromino_2(self): '''Test for following transitions □ □ □ => □ □ □ => □ □ => stable □ □ □ □ □ □ □ □ ''' self.game.set_alive((0, 1)) self.game.set_alive((1, 1)) self.game.set_alive((2, 1)) self.game.set_alive((3, 1)) self.game.update() self.assertEqual(len(self.game.living_cells), 6) self.assertSetEqual( set([(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 6) self.assertSetEqual( set([(1, 0), (2, 0), (0, 1), (3, 1), (1, 2), (2, 2)]), self.game.living_cells) self.game.update() self.assertEqual(len(self.game.living_cells), 6) self.assertSetEqual( set([(1, 0), (2, 0), (0, 1), (3, 1), (1, 2), (2, 2)]), self.game.living_cells)