2019-02-09 17:16:19 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append('../src')
|
|
|
|
|
|
|
|
from cellular_automaton import *
|
|
|
|
import unittest
|
|
|
|
import mock
|
|
|
|
|
|
|
|
|
|
|
|
class TestFac(CAFactory):
|
|
|
|
@staticmethod
|
|
|
|
def make_cells(dimension, state_class):
|
|
|
|
return CAFactory._make_cells(dimension, state_class)
|
|
|
|
|
|
|
|
@staticmethod
|
2019-02-16 17:05:26 +00:00
|
|
|
def apply_neighborhood(cells, neighborhood, dimension):
|
|
|
|
return CAFactory._apply_neighborhood_to_cells(cells, neighborhood, dimension)
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCAFactory(unittest.TestCase):
|
2019-02-16 17:05:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
self._neighborhood = MooreNeighborhood(EdgeRule.IGNORE_EDGE_CELLS)
|
|
|
|
|
2019-02-09 17:16:19 +00:00
|
|
|
def test_make_ca_calls_correct_methods(self):
|
|
|
|
with mock.patch.object(CAFactory, '_make_cells', return_value={1: True}) as m1:
|
2019-02-16 17:05:26 +00:00
|
|
|
with mock.patch.object(CAFactory, '_apply_neighborhood_to_cells') as m2:
|
|
|
|
CAFactory.make_cellular_automaton([10], self._neighborhood, CellState, Rule())
|
2019-02-09 17:16:19 +00:00
|
|
|
m1.assert_called_once_with([10], CellState)
|
2019-02-16 17:05:26 +00:00
|
|
|
m2.assert_called_once_with({1: True}, self._neighborhood, [10])
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
def test_make_ca_returns_correct_values(self):
|
|
|
|
with mock.patch.object(CAFactory, '_make_cells', return_value={1: True}):
|
2019-02-16 17:05:26 +00:00
|
|
|
with mock.patch.object(CAFactory, '_apply_neighborhood_to_cells'):
|
|
|
|
ca = CAFactory.make_cellular_automaton([10], self._neighborhood, CellState, Rule())
|
|
|
|
self.assertIsInstance(ca, CellularAutomatonState)
|
|
|
|
self.assertEqual(tuple(ca.cells.values()), (True, ))
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
def test_1dimension_coordinates(self):
|
2019-02-16 17:05:26 +00:00
|
|
|
c = TestFac.make_cells([3], CellState)
|
2019-02-15 19:02:57 +00:00
|
|
|
self.assertEqual(list(c.keys()), [(0,), (1,), (2,)])
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
def test_2dimension_coordinates(self):
|
2019-02-16 17:05:26 +00:00
|
|
|
c = TestFac.make_cells([2, 2], CellState)
|
2019-02-15 19:02:57 +00:00
|
|
|
self.assertEqual(list(c.keys()), [(0, 0), (0, 1), (1, 0), (1, 1)])
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
def test_3dimension_coordinates(self):
|
2019-02-16 17:05:26 +00:00
|
|
|
c = TestFac.make_cells([2, 2, 2], CellState)
|
2019-02-15 19:02:57 +00:00
|
|
|
self.assertEqual(list(c.keys()), [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
|
|
|
|
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)])
|
2019-02-09 17:16:19 +00:00
|
|
|
|
2019-02-16 17:05:26 +00:00
|
|
|
def test_apply_neighborhood(self):
|
|
|
|
cells = TestFac.make_cells([3, 3], CellState)
|
|
|
|
TestFac.apply_neighborhood(cells, self._neighborhood, [3, 3])
|
2019-02-15 18:33:15 +00:00
|
|
|
|
2019-02-16 17:05:26 +00:00
|
|
|
neighbors = self.__create_neighbor_list_of_cell((1, 1), cells)
|
2019-02-15 18:33:15 +00:00
|
|
|
|
2019-02-16 17:05:26 +00:00
|
|
|
self.assertEqual(set(neighbors), set(cells[(1, 1)].neighbor_states))
|
2019-02-15 18:33:15 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2019-02-16 17:05:26 +00:00
|
|
|
def __create_neighbor_list_of_cell(cell_id, cells):
|
|
|
|
neighbors = []
|
2019-02-15 18:33:15 +00:00
|
|
|
for c in cells.values():
|
|
|
|
if c != cells[cell_id]:
|
2019-02-16 17:05:26 +00:00
|
|
|
neighbors.append(c.state)
|
|
|
|
return neighbors
|
2019-02-15 18:33:15 +00:00
|
|
|
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|