Beispiel #1
0
    def test_get_no_alive_cells(self):
        """
        Tests the ability of the grid to retrieve the number of living cells.

        This method tests the ability of the GoLGrid to retrieve the number of living GoLCells it currently has.
        """
        test_grid = GolGrid()
        alive_cells = test_grid.get_no_alive_cells()
        # At this point, none of the GoLCells should be alive. Assert this is true.
        assert alive_cells == 0

        # Create another set of cells, half of which are alive.
        s = 10
        cells = []
        for x in range(0, s):
            cells.append([])
            for y in range(0, s):
                c = GolCell()
                if y % 2 == 0:
                    # Make every second cell alive.
                    c.set_state(Alive())
                cells[x].append(c)

        test_grid = GolGrid(cells)
        alive_cells = test_grid.get_no_alive_cells()
        # Assert that the correct number of cells have been retrieved.
        assert alive_cells == (s * s) / 2
Beispiel #2
0
    def test_set_cells(self):
        """
        This method tests the ability of the GoLGrid to store a collection of GoLCells. The expected result of this
        tests is that a collection of GoLCells can be correctly set.
        """
        test_grid = GolGrid()

        # Create a collection of GolCell objects
        # Hard coding the expectation of a 10 x 10 2D array as it will fail
        # if someone changes the keyword parameters
        s = 10
        cells = []
        for x in range(0, s):
            cells.append([])
            for y in range(0, s):
                c = GolCell()
                if y % 2 == 0:
                    c.set_state(Alive())
                cells[x].append(c)

        test_grid.set_cells(cells)
        # Assert that the collection of GoLCells has been set correctly.
        assert test_grid.get_cells()

        recieved_cells = test_grid.get_cells()
        for x, row in enumerate(recieved_cells):
            # For each row of cells.
            for y, _column in enumerate(row):
                # Assert that the GoLCell at these 'coordinates' is the correct GoLCell.
                assert recieved_cells[x][y] == cells[x][y]
    def test_is_alive(self):
        '''
        Test whether the cell knows it is alive.
        '''
        c = GolCell(Alive())
        assert c.is_alive()

        c = initialise_cell()
        assert not c.is_alive()
Beispiel #4
0
    def __init__(self, **kwargs):
        """
        Constructor

        root - reference to the widget containing this widget
        """
        Canvas.__init__(self, **kwargs)
        self["bg"] = DEAD_COLOUR
        GolCell.__init__(self)
Beispiel #5
0
    def test_is_alive(self):
        """
        This method tests the ability of the GolCell to evaluate whether or not it is alive. The expected result of this
        test is for the evaluation to be successful.
        """
        c = GolCell(Alive())
        # Assert that the GolCell can evaluate itself as alive.
        assert c.is_alive()

        c = initialise_cell()
        # Assert that the GolCell can evaulate itself as dead.
        assert not c.is_alive()
    def test_next_state(self):
        """
        This method tests the ability of the calculator to calculate the next state of a given cell. The expected
        result of this test is for the cell's next state to be correctly calculated.
        """
        rs = get_rule_set()
        calc = calculator.Calculator(rs)

        checked_cell = GolCell()
        #======================================================================
        # 1. Collection of 8 neighbours and one being checked. Test Starvation
        #     - Should result in dead
        #======================================================================
        checked_cell.set_state(Alive())
        neighbours = [GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(),
                      GolCell(),
                      GolCell(),
                      GolCell(),
                      GolCell()]

        next_state = calc._next_state(checked_cell, neighbours)
        # Assert that the cell's next state is dead.
        assert isinstance(next_state, Dead)

        #======================================================================
        # 2. Collection of 8 neighbours and one being checked. Test stay alive
        #     - Should result in alive
        #======================================================================
        checked_cell.set_state(Alive())
        neighbours = [GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(),
                      GolCell(),
                      GolCell()]

        next_state = calc._next_state(checked_cell, neighbours)
        # Assert that the cell's next state is alive.
        assert isinstance(next_state, Alive)

        #======================================================================
        # 3. Collection of 8 neighbours and one being checked.
        # Test over-population
        #    - Should result in dead
        #======================================================================
        checked_cell.set_state(Alive())
        neighbours = [GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(Alive()),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(Alive()),
                      GolCell()]

        next_state = calc._next_state(checked_cell, neighbours)
        # Assert that the cell's next state is dead.
        assert isinstance(next_state, Dead)

        #======================================================================
        # 4. Collection of 8 neighbours and one being checked. Test born
        #     - Should result in alive
        #======================================================================
        checked_cell.set_state(Dead())
        neighbours = [GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(),
                      GolCell(Alive()),
                      GolCell(),
                      GolCell(Alive()),
                      GolCell()]

        next_state = calc._next_state(checked_cell, neighbours)
        # Assert that the cell's next state is alive.
        assert isinstance(next_state, Alive)
    def test_next_state(self):
        """
        Tests the ability of the calculator to calculate the next state of a
        given cell.
        """
        # Finds the state of the cell to be tested.
        # Finds the no. of alive cells within its neighbours.
        # Checks the rule set to see how it reacts.
        # Change state accordingly.
        rs = get_rule_set()
        calc = calculator.Calculator(rs)

        checked_cell = GolCell()
        # ======================================================================
        # 1. Collection of 8 neighbours and one being checked. Test Starvation
        #     - Should result in dead
        # ======================================================================
        checked_cell.set_state(Alive())
        neighbours = [GolCell(), GolCell(Alive()), GolCell(), GolCell(), GolCell(), GolCell(), GolCell(), GolCell()]

        next_state = calc._next_state(checked_cell, neighbours)
        assert isinstance(next_state, Dead)

        # ======================================================================
        # 2. Collection of 8 neighbours and one being checked. Test stay alive
        #     - Should result in alive
        # ======================================================================
        checked_cell.set_state(Alive())
        neighbours = [
            GolCell(),
            GolCell(Alive()),
            GolCell(),
            GolCell(Alive()),
            GolCell(),
            GolCell(),
            GolCell(),
            GolCell(),
        ]

        next_state = calc._next_state(checked_cell, neighbours)
        assert isinstance(next_state, Alive)

        # ======================================================================
        # 3. Collection of 8 neighbours and one being checked.
        # Test over-population
        #    - Should result in dead
        # ======================================================================
        checked_cell.set_state(Alive())
        neighbours = [
            GolCell(),
            GolCell(Alive()),
            GolCell(),
            GolCell(Alive()),
            GolCell(Alive()),
            GolCell(),
            GolCell(Alive()),
            GolCell(),
        ]

        next_state = calc._next_state(checked_cell, neighbours)
        assert isinstance(next_state, Dead)

        # ======================================================================
        # 4. Collection of 8 neighbours and one being checked. Test born
        #     - Should result in alive
        # ======================================================================
        checked_cell.set_state(Dead())
        neighbours = [
            GolCell(),
            GolCell(Alive()),
            GolCell(),
            GolCell(),
            GolCell(Alive()),
            GolCell(),
            GolCell(Alive()),
            GolCell(),
        ]

        next_state = calc._next_state(checked_cell, neighbours)
        assert isinstance(next_state, Alive)