123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- # coding: utf-8
- import collections
- from sandbox.life_game.simulation import Cell
- from sandbox.life_game.simulation import Empty
- from sandbox.life_game.utils import get_subjects_from_str_representation
- from synergine2.cycle import CycleManager
- from synergine2.simulation import Simulation
- from synergine2.xyz import XYZSubjects
- from synergine2.xyz_utils import get_str_representation_from_positions
- from tests import BaseTest
- from tests import str_kwargs
-
-
- class LifeGameBaseTest(BaseTest):
- def _get_str_representation_of_subjects(self, subjects: list):
- items_positions = collections.defaultdict(list)
-
- for subject in subjects:
- if type(subject) == Cell:
- items_positions['1'].append(subject.position)
- if type(subject) == Empty:
- items_positions['0'].append(subject.position)
-
- return get_str_representation_from_positions(
- items_positions,
- complete_lines_with='0',
- **str_kwargs
- )
-
-
- class TestSimpleSimulation(LifeGameBaseTest):
- def test_cycles_evolution(self):
- simulation = Simulation()
- subjects = self._get_subjects(simulation)
- simulation.subjects = subjects
-
- cycle_manager = CycleManager(
- simulation=simulation,
- )
-
- assert """
- 0 0 0 0 0
- 0 1 1 1 0
- 0 0 0 0 0
- """ == self._get_str_representation_of_subjects(
- subjects,
- )
-
- cycle_manager.next()
-
- assert """
- 0 0 0 0 0
- 0 0 1 0 0
- 0 0 1 0 0
- 0 0 1 0 0
- 0 0 0 0 0
- """ == self._get_str_representation_of_subjects(
- subjects,
- )
-
- cycle_manager.next()
-
- assert """
- 0 0 0 0 0
- 0 0 0 0 0
- 0 1 1 1 0
- 0 0 0 0 0
- 0 0 0 0 0
- """ == self._get_str_representation_of_subjects(
- subjects,
- )
-
- def _get_subjects(self, simulation: Simulation):
- cells = XYZSubjects(simulation=simulation)
-
- for position in [
- (-1, 0, 0),
- (0, 0, 0),
- (1, 0, 0),
- ]:
- cells.append(Cell(
- simulation=simulation,
- position=position,
- ))
-
- for position in [
- (-2, -1, 0),
- (-1, -1, 0),
- (0, -1, 0),
- (1, -1, 0),
- (2, -1, 0),
- (-2, 0, 0),
- (2, 0, 0),
- (-2, 1, 0),
- (-1, 1, 0),
- (0, 1, 0),
- (1, 1, 0),
- (2, 1, 0),
- ]:
- cells.append(Empty(
- simulation=simulation,
- position=position,
- ))
- return cells
-
-
- class TestMultipleSimulations(LifeGameBaseTest):
- def test_cross(self):
- str_representations = [
- """
- 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 1 0 0 1 0 0 0 0
- 0 1 1 1 0 0 1 1 1 0 0
- 0 1 0 0 0 0 0 0 1 0 0
- 0 1 0 0 0 0 0 0 1 0 0
- 0 1 1 1 0 0 1 1 1 0 0
- 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0
- """,
- """
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 1 1 0 0 0 0 0
- 0 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 1 0 1 0 0 1 0 1 0 0
- 0 1 1 0 0 0 0 0 0 1 1 0
- 0 1 1 0 0 0 0 0 0 1 1 0
- 0 0 1 0 1 0 0 1 0 1 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 0 0 1 1 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- """,
- """
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 1 1 0 0 1 1 0 0 0
- 0 1 1 1 0 0 0 0 1 1 1 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 1 1 1 0 0 0 0 1 1 1 0
- 0 0 0 1 1 0 0 1 1 0 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- """,
- """
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 1 1 1 0 0 1 1 1 0 0
- 0 0 1 0 0 0 0 0 0 1 0 0
- 0 0 1 0 0 0 0 0 0 1 0 0
- 0 0 1 1 1 0 0 1 1 1 0 0
- 0 0 0 0 1 0 0 1 0 0 0 0
- 0 0 0 0 1 1 1 1 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0
- """,
- ]
-
- simulation = Simulation()
- subjects = get_subjects_from_str_representation(
- str_representations[0],
- simulation,
- )
- simulation.subjects = subjects
-
- cycle_manager = CycleManager(
- simulation=simulation,
- )
-
- for str_representation in str_representations:
- assert str_representation == \
- self._get_str_representation_of_subjects(
- subjects,
- )
- cycle_manager.next()
|