Beispiel #1
0
    def test_grid_updateCell(self):
        for n_neigh in range(9):
            grid = gol.Grid(3, 3)
            cell = grid[1, 1]
            neighs = grid.getNeighbours(cell)
            for n_cell in neighs[:n_neigh]:
                n_cell.birth()

            if grid.updateCell(cell):
                cell.birth()
            else:
                cell.death()

            if n_neigh in gol.Cell.STATE[
                    "Starving"] or n_neigh in gol.Cell.STATE["Crowded"]:
                self.assertTrue(cell.isDead())
                self.assertFalse(cell.isAlive())
            elif n_neigh in gol.Cell.STATE["Reproducing"]:
                self.assertTrue(cell.isAlive())
                self.assertFalse(cell.isDead())
            elif n_neigh in gol.Cell.STATE["Stable"]:
                if cell.isAlive():
                    self.assertTrue(cell.isAlive())
                    self.assertFalse(cell.isDead())
                if cell.isDead():
                    self.assertTrue(cell.isDead())
                    self.assertFalse(cell.isAlive())
            else:
                raise AssertionError(
                    "State not registered for {} neighbours".format(n_neigh))
Beispiel #2
0
    def test_grid_init(self):
        grid = gol.Grid(9, 9)

        self.assertEqual((9, 9), grid.getDimension())

        # Check that all objects in grid are type Cell
        for cell in grid:
            self.assertIsInstance(cell, gol.Cell)
Beispiel #3
0
 def test_grid_generateInitialState(self):
     grid = gol.Grid(100, 100)
     self.assertAlmostEqual(
         0.0,
         sum([x.isAlive() for x in grid]) / float(len(grid)), 1)
     grid.generateInitialState()
     self.assertAlmostEqual(
         0.5,
         sum([x.isAlive() for x in grid]) / float(len(grid)), 1)
Beispiel #4
0
	def _test_mc_chance_n_neighbours(self, prob_map, n_neigh, total_samples=10000):
		how_many_times_survived = 0
		grid = gol.Grid(6, 6, prob_map)
		neigh_list = (grid[2, 2], grid[2, 3], grid[2, 4], grid[3, 2], grid[3, 4], grid[4, 2], grid[4, 3], grid[4, 4])

		for i in range(total_samples):
			for n in range(n_neigh):
				neigh_list[n].birth()
			grid[3, 3].birth()
			grid.update()
			how_many_times_survived += int(grid[3, 3].isDead())
			grid.killAll()
		self.assertAlmostEqual(prob_map[n_neigh], how_many_times_survived / float(total_samples), places=2)
Beispiel #5
0
    def test_grid_cloneState(self):
        test_matrix = ((gol.Cell(0, 0), gol.Cell(0, 1), gol.Cell(0, 2)),
                       (gol.Cell(1, 0), gol.Cell(1, 1), gol.Cell(1, 2)),
                       (gol.Cell(2, 0), gol.Cell(2, 1), gol.Cell(2, 2)))
        grid = gol.Grid(3, 3)
        original_grid_ref = grid._grid
        self.assertTrue(grid._grid is original_grid_ref)
        self.assertFalse(grid._grid is test_matrix)
        test_matrix[1][1].birth()
        grid.cloneState(test_matrix)

        self.assertTrue(grid[1, 1].isAlive())
        self.assertTrue(grid._grid is original_grid_ref)
        self.assertFalse(grid._grid is test_matrix)
Beispiel #6
0
    def test_grid_getCellState(self):
        for n_neigh in range(9):
            grid = gol.Grid(3, 3)
            cell = grid[1, 1]
            neighs = grid.getNeighbours(cell)
            for n_cell in neighs[:n_neigh]:
                n_cell.birth()

            for state in gol.Cell.STATE:
                if n_neigh in gol.Cell.STATE[state]:
                    expected_state = state
                    break

            self.assertEqual(expected_state, grid.getCellState(cell))
Beispiel #7
0
    def test_grid_getAliveNeighbours(self):
        grid = gol.Grid(6, 6)
        neighs = (grid[1, 1], grid[2, 3], grid[1, 3])
        map(lambda c: c.birth(), neighs)

        # No boundary conditions
        cell = grid[2, 2]
        self.assertEqual(3, grid.getAliveNeighbours(cell))
        self.assertEqual(3, grid.getAliveNeighbours(neighs))

        # With periodic boundary conditions
        neighs = (grid[4, 5], grid[0, 0])
        map(lambda c: c.birth(), neighs)
        cell = grid[5, 5]
        self.assertEqual(2, grid.getAliveNeighbours(cell))
        self.assertEqual(2, grid.getAliveNeighbours(neighs))
Beispiel #8
0
    def test_grid_getNeighbours(self):
        grid = gol.Grid(9, 9)
        cell = grid[4, 4]
        neighs = grid.getNeighbours(cell)

        # Check the total number of neighbours
        self.assertEqual(
            len(neighs),
            8,
            msg="Number of cell neighbours different from 8 (In a 2D grid)")

        # Check its coordinates
        # No boundary
        expected_coords = [(3, 3), (3, 4), (3, 5), (4, 3), (4, 5), (5, 3),
                           (5, 4), (5, 5)]
        for neigh_cell in neighs:
            try:
                expected_coords.remove(neigh_cell.getCoords())
            except ValueError as e:
                print "{} not a valid neighbour coordinate".format(
                    neigh_cell.getCoords())

        self.assertEqual(
            len(expected_coords),
            0,
            msg="No boundary conditions failed retrieving neighbours")

        # With boundary
        cell = grid[8, 8]
        neighs = grid.getNeighbours(cell)
        expected_coords = [(7, 7), (7, 8), (7, 0), (8, 7), (8, 0), (0, 7),
                           (0, 8), (0, 0)]
        for neigh_cell in neighs:
            try:
                expected_coords.remove(neigh_cell.getCoords())
            except ValueError as e:
                print "{} not a valid neighbour coordinate".format(
                    neigh_cell.getCoords())

        self.assertEqual(
            len(expected_coords),
            0,
            msg="Periodic boundary conditions failed retrieveing neighbours")
Beispiel #9
0
 def test_mc_well_stability(self):
     grid = gol.Grid(6, 6, mc_life2death_probs)
     block_cells = (grid[1, 2], grid[1, 3], grid[2, 1], grid[3, 1],
                    grid[2, 4], grid[3, 4], grid[4, 2], grid[4, 3])
     self._test_stability(grid, block_cells)
Beispiel #10
0
 def test_mc_block_stability(self):
     grid = gol.Grid(6, 6, mc_life2death_probs)
     block_cells = (grid[2, 2], grid[2, 3], grid[3, 2], grid[3, 3])
     self._test_stability(grid, block_cells)
Beispiel #11
0
 def test_well_stability(self):
     grid = gol.Grid(6, 6)
     block_cells = (grid[1, 2], grid[1, 3], grid[2, 1], grid[3, 1],
                    grid[2, 4], grid[3, 4], grid[4, 2], grid[4, 3])
     self._test_stability(grid, block_cells)
Beispiel #12
0
 def test_beehive_stability(self):
     grid = gol.Grid(6, 6)
     block_cells = (grid[1, 2], grid[1, 3], grid[2, 1], grid[2, 4],
                    grid[3, 2], grid[3, 3])
     self._test_stability(grid, block_cells)
Beispiel #13
0
 def test_block_stability(self):
     grid = gol.Grid(6, 6)
     block_cells = (grid[2, 2], grid[2, 3], grid[3, 2], grid[3, 3])
     self._test_stability(grid, block_cells)
Beispiel #14
0
 def test_grid_getCell(self):
     grid = gol.Grid(3, 3)
     cell = grid[1, 1]
     self.assertIsInstance(cell, gol.Cell)
     self.assertEqual((1, 1), cell.getCoords())