def gol(): data = [0b00010, 0b01000, 0b00110, 0b00010, 0b00111] return life.GameOfLife(size=5, data=data)
def test_count_neighbours(): game = life.GameOfLife(max_x=3, max_y=3, population=0, template="") game.live(0, 1) game.live(1, 0) game.live(1, 2) game.live(2, 1) game.flip() # assert game.count_neighbours(0, 0) == 2 # assert game.count_neighbours(0, 1) == 2 # assert game.count_neighbours(0, 2) == 2 # assert game.count_neighbours(1, 0) == 2 # assert game.count_neighbours(1, 1) == 4 # assert game.count_neighbours(1, 2) == 2 # assert game.count_neighbours(2, 0) == 2 # assert game.count_neighbours(2, 1) == 2 # assert game.count_neighbours(2, 2) == 2 assert game.count_neighbours(0, 0) == 4 assert game.count_neighbours(0, 1) == 3 assert game.count_neighbours(0, 2) == 4 assert game.count_neighbours(1, 0) == 3 assert game.count_neighbours(1, 1) == 4 assert game.count_neighbours(1, 2) == 3 assert game.count_neighbours(2, 0) == 4 assert game.count_neighbours(2, 1) == 3 assert game.count_neighbours(2, 2) == 4
def test_step_1(): game = life.GameOfLife(max_x=3, max_y=3, population=0, template="") game.live(0, 1) game.live(1, 0) game.live(1, 2) game.flip() done = game.step() game.flip() assert not done # assert not game.alive(0, 0) # assert game.alive(0, 1) # assert not game.alive(0, 2) # assert not game.alive(1, 0) # assert game.alive(1, 1) # assert not game.alive(1, 2) # assert not game.alive(2, 0) # assert not game.alive(2, 1) # assert not game.alive(2, 2) assert game.alive(0, 0) assert game.alive(0, 1) assert game.alive(0, 2) assert game.alive(1, 0) assert game.alive(1, 1) assert game.alive(1, 2) assert game.alive(2, 0) assert game.alive(2, 1) assert game.alive(2, 2)
def test_live(): game = life.GameOfLife(max_x=2, max_y=2, population=0, template="") game.live(1, 1) game.flip() assert not game.alive(0, 0) assert not game.alive(0, 1) assert not game.alive(1, 0) assert game.alive(1, 1)
def test_is_max_generations_exceeded(self): max_generations = 4 game = life.GameOfLife((self.rows, self.cols), max_generations=max_generations) game.curr_generation = self.grid for _ in range(max_generations - 1): game.step() self.assertEqual(game.generations, max_generations) self.assertTrue(game.is_max_generations_exceeded)
def test_die(): game = life.GameOfLife(max_x=2, max_y=2, population=0, template="OOOO") game.flip( ) # need to flip because the next_world isn't initialized with the template yet game.die(1, 1) game.flip() assert game.alive(0, 0) assert game.alive(0, 1) assert game.alive(1, 0) assert not game.alive(1, 1)
def test_can_update(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid with open("steps.txt") as f: steps = json.load(f) num_updates = 0 for step in sorted(steps.keys(), key=int): with self.subTest(step=step): for _ in range(int(step) - num_updates): game.curr_generation = game.get_next_generation() num_updates += 1 self.assertEqual(steps[step], game.curr_generation)
def test_can_update(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid tests_dir = os.path.dirname(__file__) steps_path = os.path.join(tests_dir, "steps.json") with open(steps_path) as f: steps = json.load(f) num_updates = 0 for step in sorted(steps.keys(), key=int): with self.subTest(step=step): for _ in range(int(step) - num_updates): game.curr_generation = game.get_next_generation() num_updates += 1 self.assertEqual(steps[step], game.curr_generation)
def test_check_livelihood(): game = life.GameOfLife(max_x=3, max_y=3, population=0, template="") game.live(0, 1) game.live(1, 0) game.live(1, 2) game.flip() # assert not game.check_livelihood(0, 0) # assert game.check_livelihood(0, 1) # assert not game.check_livelihood(0, 2) # assert not game.check_livelihood(1, 0) # assert game.check_livelihood(1, 1) # assert not game.check_livelihood(1, 2) # assert not game.check_livelihood(2, 0) # assert not game.check_livelihood(2, 1) # assert not game.check_livelihood(2, 2) assert game.check_livelihood(0, 0) assert game.check_livelihood(0, 1) assert game.check_livelihood(0, 2) assert game.check_livelihood(1, 0) assert game.check_livelihood(1, 1) assert game.check_livelihood(1, 2) assert game.check_livelihood(2, 0) assert game.check_livelihood(2, 1) assert game.check_livelihood(2, 2)
def test_get_neighbours_for_right_side(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid neighbours = game.get_neighbours((2, 7)) self.assertEqual(5, len(neighbours)) self.assertEqual(2, sum(neighbours))
def test_get_neighbours_for_lower_right_corner(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid neighbours = game.get_neighbours((5, 7)) self.assertEqual(3, len(neighbours)) self.assertEqual(1, sum(neighbours))
def test_get_neighbours(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid neighbours = game.get_neighbours((2, 3)) self.assertEqual(8, len(neighbours)) self.assertEqual(4, sum(neighbours))
def test_can_create_a_random_grid(self): game = life.GameOfLife((3, 3)) random.seed(12345) grid = game.create_grid(randomize=True) self.assertEqual([[1, 0, 1], [1, 0, 1], [1, 0, 1]], grid)
def test_can_create_an_empty_grid(self): game = life.GameOfLife((3, 3)) grid = game.create_grid(randomize=False) self.assertEqual([[0, 0, 0], [0, 0, 0], [0, 0, 0]], grid)
def test_is_not_changing(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid for _ in range(self.max_generations + 1): game.step() self.assertFalse(game.is_changing)
def test_is_changing(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid game.step() self.assertTrue(game.is_changing)
def test_prev_generation_is_correct(self): game = life.GameOfLife((self.rows, self.cols)) game.curr_generation = self.grid game.step() self.assertEqual(game.prev_generation, self.grid)