def test_determine_number_of_living_two_neighbor_edges(self): """ For any cell, determine the number of living neighbors when one edge neighbor is living """ b = board.Board(width=3, height=3) b.setCellAt(1, 1, cell.LiveCell()) b.setCellAt(1, 0, cell.LiveCell()) b.setCellAt(0, 1, cell.LiveCell()) self.assertEquals(b.livingNeighbors(1, 1), 2) b = board.Board(width=3, height=3) b.setCellAt(1, 1, cell.LiveCell()) b.setCellAt(2, 1, cell.LiveCell()) b.setCellAt(1, 2, cell.LiveCell()) self.assertEquals(b.livingNeighbors(1, 1), 2)
def test_determine_number_of_living_two_neighbor_corner(self): """ For any cell, dtermine the number of living neighbors when two corner neighbors are living """ b = board.Board(width=3, height=3) b.setCellAt(1, 1, cell.LiveCell()) b.setCellAt(0, 0, cell.LiveCell()) b.setCellAt(2, 2, cell.LiveCell()) self.assertEquals(b.livingNeighbors(1, 1), 2) b = board.Board(width=3, height=3) b.setCellAt(1, 1, cell.LiveCell()) b.setCellAt(2, 0, cell.LiveCell()) b.setCellAt(0, 2, cell.LiveCell()) self.assertEquals(b.livingNeighbors(1, 1), 2)
def test_cell_is_born_with_exactly_three_neighbors(self): """ A cell is born if it has exactly three living neighbors """ b = board.Board(width=5, height=5) b.setCellAt(2, 1, cell.LiveCell()) b.setCellAt(2, 3, cell.LiveCell()) b.setCellAt(1, 2, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_alive()) b = board.Board(width=5, height=5) b.setCellAt(1, 1, cell.LiveCell()) b.setCellAt(1, 3, cell.LiveCell()) b.setCellAt(3, 3, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_alive())
def test_living_cell_dies_with_zero_neighbors(self): """ A living cell dies if it has 0 living neighbors """ b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead())
def test_iteration_count_can_be_incremented(self): """ Iteration count can be incremented """ b = board.Board(width=10, height=10) self.assertEquals(b.iteration, 0) b.step() self.assertEquals(b.iteration, 1) b.step() self.assertEquals(b.iteration, 2)
def test_row_serialization_collapsed(self): b = board.Board(width=3, height=1) b.set_cell_at(1, 0, cell.LiveCell()) b.set_cell_at(2, 0, cell.LiveCell()) self.assertEquals(RleRowFormat(b, 0).serialize(), "b2o$") b.setCellAt(0, 0, cell.LiveCell()) b.setCellAt(1, 0, cell.LiveCell()) b.setCellAt(2, 0, cell.LiveCell()) self.assertEquals(RleRowFormat(b, 0).serialize(), "3o$")
def test_board_serialization(self): b = board.Board(width=3, height=3) b.set_cell_at(1, 0, cell.LiveCell()) b.set_cell_at(2, 1, cell.LiveCell()) b.set_cell_at(0, 2, cell.LiveCell()) b.set_cell_at(1, 2, cell.LiveCell()) b.set_cell_at(2, 2, cell.LiveCell()) self.assertEquals( RleBoardFormat(b).serialize(), "x = 3, y = 3, rule = B3/S23\nbo$2bo$3o!")
def test_out_of_range_cell(self): """ Attempt to access a cell outside the board throws an exception """ b = board.Board(width=10, height=10) with self.assertRaises(IndexError): b.setCellAt(-1, -1, cell.LiveCell()) with self.assertRaises(IndexError): b.cellAt(-1, -1) with self.assertRaises(IndexError): b.cellAt(b.width, b.height)
def test_board_serialization(self): """ A board is serialized as a header followed by visually-representative cells """ b = board.Board(width=3, height=3) b.setCellAt(1, 0, cell.LiveCell()) b.setCellAt(2, 1, cell.LiveCell()) b.setCellAt(0, 2, cell.LiveCell()) b.setCellAt(1, 2, cell.LiveCell()) b.setCellAt(2, 2, cell.LiveCell()) self.assertEquals( BF.PlaintextBoardFormat(b, "name").serialize(), "!Name: name\n!\n.O.\n..O\nOOO\n")
def test_can_retrieve_cell_by_coordinate(self): """ Any cell can be retrieved from the board by specifying an x and y coordinate """ c = cell.LiveCell() b = board.Board(width=10, height=10) # Living cell b.setCellAt(x=3, y=3, cell=c) self.assertEquals(b.cellAt(3, 3), c) # Dead cell self.assertEquals(b.cellAt(7, 7).mortality, cell.Dead)
def test_living_cell_dies_with_one_neighbor(self): """ A living cell dies it it has 1 living neighbors """ b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(2, 1, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(1, 1, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(1, 2, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(1, 3, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(2, 3, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(3, 1, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(3, 2, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead()) b = board.Board(width=5, height=5) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(3, 3, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_dead())
def test_spinner(self): """ Both phases of a three-cell spinner are born and die appropriately """ b = board.Board(width=5, height=5) b.setCellAt(1, 2, cell.LiveCell()) b.setCellAt(2, 2, cell.LiveCell()) b.setCellAt(3, 2, cell.LiveCell()) b.step() self.assertTrue(b.cellAt(2, 2).is_alive()) self.assertTrue(b.cellAt(2, 1).is_alive()) self.assertTrue(b.cellAt(2, 3).is_alive()) self.assertTrue(b.cellAt(1, 2).is_dead()) self.assertTrue(b.cellAt(3, 2).is_dead()) b.step() self.assertTrue(b.cellAt(2, 2).is_alive()) self.assertTrue(b.cellAt(2, 1).is_dead()) self.assertTrue(b.cellAt(2, 3).is_dead()) self.assertTrue(b.cellAt(1, 2).is_alive()) self.assertTrue(b.cellAt(3, 2).is_alive())
def test_last_row_serialization_direct(self): b = board.Board(width=3, height=1) b.set_cell_at(0, 0, cell.LiveCell()) b.set_cell_at(2, 0, cell.LiveCell()) self.assertEqual(RleRowFormat(b, 0, last=True).serialize(), "obo!")
def test_can_be_applied(self): """ Rules can be applied """ r = rules.ConwayRules() r.apply_to(board.Board(0, 0))
def test_iteration_count_starts_at_zero(self): """ Iteration count starts at 0 """ b = board.Board(width=10, height=10) self.assertEquals(b.iteration, 0)
def test_has_iteration_count(self): """ Board has an iteration count """ b = board.Board(width=10, height=10, iteration=4) self.assertEquals(b.iteration, 4)
def test_iteration_count_can_be_reset(self): """ Iteration count can be reset """ b = board.Board(width=10, height=10, iteration=4) b.reset() self.assertEquals(b.iteration, 0)
def test_row_serialization_drops_trailing_dead_cells(self): b = board.Board(width=3, height=1) b.set_cell_at(0, 0, cell.LiveCell()) self.assertEquals(RleRowFormat(b, 0).serialize(), "o$")
def test_determine_number_of_living_zero_neighbors(self): """ For any cell, determine the number of living neighbors when no neighbors are living """ b = board.Board(width=3, height=3) b.setCellAt(1, 1, cell.LiveCell()) self.assertEquals(b.livingNeighbors(1, 1), 0)
def test_has_width_and_height(self): """ A board is primarily specified by its width and height """ b = board.Board(width=10, height=10) self.assertEquals(b.width, 10) self.assertEquals(b.height, 10)