test_life_game.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import collections
  2. from sandbox.life_game.simulation import Cell
  3. from sandbox.life_game.simulation import Empty
  4. from sandbox.life_game.utils import get_subjects_from_str_representation
  5. from synergine2.cycle import CycleManager
  6. from synergine2.simulation import Simulation
  7. from synergine2.xyz import XYZSubjects
  8. from synergine2.xyz_utils import get_str_representation_from_positions
  9. from tests import BaseTest
  10. from tests import str_kwargs
  11. class LifeGameBaseTest(BaseTest):
  12. def _get_str_representation_of_subjects(self, subjects: list):
  13. items_positions = collections.defaultdict(list)
  14. for subject in subjects:
  15. if type(subject) == Cell:
  16. items_positions['1'].append(subject.position)
  17. if type(subject) == Empty:
  18. items_positions['0'].append(subject.position)
  19. return get_str_representation_from_positions(
  20. items_positions,
  21. complete_lines_with='0',
  22. **str_kwargs
  23. )
  24. class TestSimpleSimulation(LifeGameBaseTest):
  25. def test_cycles_evolution(self):
  26. simulation = Simulation()
  27. subjects = self._get_subjects(simulation)
  28. simulation.subjects = subjects
  29. cycle_manager = CycleManager(
  30. simulation=simulation,
  31. )
  32. assert """
  33. 0 0 0 0 0
  34. 0 1 1 1 0
  35. 0 0 0 0 0
  36. """ == self._get_str_representation_of_subjects(
  37. subjects,
  38. )
  39. cycle_manager.next()
  40. assert """
  41. 0 0 0 0 0
  42. 0 0 1 0 0
  43. 0 0 1 0 0
  44. 0 0 1 0 0
  45. 0 0 0 0 0
  46. """ == self._get_str_representation_of_subjects(
  47. subjects,
  48. )
  49. cycle_manager.next()
  50. assert """
  51. 0 0 0 0 0
  52. 0 0 0 0 0
  53. 0 1 1 1 0
  54. 0 0 0 0 0
  55. 0 0 0 0 0
  56. """ == self._get_str_representation_of_subjects(
  57. subjects,
  58. )
  59. def _get_subjects(self, simulation: Simulation):
  60. cells = XYZSubjects(simulation=simulation)
  61. for position in [
  62. (-1, 0, 0),
  63. (0, 0, 0),
  64. (1, 0, 0),
  65. ]:
  66. cells.append(Cell(
  67. simulation=simulation,
  68. position=position,
  69. ))
  70. for position in [
  71. (-2, -1, 0),
  72. (-1, -1, 0),
  73. (0, -1, 0),
  74. (1, -1, 0),
  75. (2, -1, 0),
  76. (-2, 0, 0),
  77. (2, 0, 0),
  78. (-2, 1, 0),
  79. (-1, 1, 0),
  80. (0, 1, 0),
  81. (1, 1, 0),
  82. (2, 1, 0),
  83. ]:
  84. cells.append(Empty(
  85. simulation=simulation,
  86. position=position,
  87. ))
  88. return cells
  89. class TestMultipleSimulations(LifeGameBaseTest):
  90. def test_cross(self):
  91. str_representations = [
  92. """
  93. 0 0 0 0 0 0 0 0 0 0 0
  94. 0 0 0 1 1 1 1 0 0 0 0
  95. 0 0 0 1 0 0 1 0 0 0 0
  96. 0 1 1 1 0 0 1 1 1 0 0
  97. 0 1 0 0 0 0 0 0 1 0 0
  98. 0 1 0 0 0 0 0 0 1 0 0
  99. 0 1 1 1 0 0 1 1 1 0 0
  100. 0 0 0 1 0 0 1 0 0 0 0
  101. 0 0 0 1 1 1 1 0 0 0 0
  102. 0 0 0 0 0 0 0 0 0 0 0
  103. 0 0 0 0 0 0 0 0 0 0 0
  104. """,
  105. """
  106. 0 0 0 0 0 0 0 0 0 0 0 0
  107. 0 0 0 0 0 1 1 0 0 0 0 0
  108. 0 0 0 0 1 1 1 1 0 0 0 0
  109. 0 0 0 0 0 0 0 0 0 0 0 0
  110. 0 0 1 0 1 0 0 1 0 1 0 0
  111. 0 1 1 0 0 0 0 0 0 1 1 0
  112. 0 1 1 0 0 0 0 0 0 1 1 0
  113. 0 0 1 0 1 0 0 1 0 1 0 0
  114. 0 0 0 0 0 0 0 0 0 0 0 0
  115. 0 0 0 0 1 1 1 1 0 0 0 0
  116. 0 0 0 0 0 1 1 0 0 0 0 0
  117. 0 0 0 0 0 0 0 0 0 0 0 0
  118. """,
  119. """
  120. 0 0 0 0 0 0 0 0 0 0 0 0
  121. 0 0 0 0 1 0 0 1 0 0 0 0
  122. 0 0 0 0 1 0 0 1 0 0 0 0
  123. 0 0 0 1 1 0 0 1 1 0 0 0
  124. 0 1 1 1 0 0 0 0 1 1 1 0
  125. 0 0 0 0 0 0 0 0 0 0 0 0
  126. 0 0 0 0 0 0 0 0 0 0 0 0
  127. 0 1 1 1 0 0 0 0 1 1 1 0
  128. 0 0 0 1 1 0 0 1 1 0 0 0
  129. 0 0 0 0 1 0 0 1 0 0 0 0
  130. 0 0 0 0 1 0 0 1 0 0 0 0
  131. 0 0 0 0 0 0 0 0 0 0 0 0
  132. """,
  133. """
  134. 0 0 0 0 0 0 0 0 0 0 0 0
  135. 0 0 0 0 0 0 0 0 0 0 0 0
  136. 0 0 0 0 1 1 1 1 0 0 0 0
  137. 0 0 0 0 1 0 0 1 0 0 0 0
  138. 0 0 1 1 1 0 0 1 1 1 0 0
  139. 0 0 1 0 0 0 0 0 0 1 0 0
  140. 0 0 1 0 0 0 0 0 0 1 0 0
  141. 0 0 1 1 1 0 0 1 1 1 0 0
  142. 0 0 0 0 1 0 0 1 0 0 0 0
  143. 0 0 0 0 1 1 1 1 0 0 0 0
  144. 0 0 0 0 0 0 0 0 0 0 0 0
  145. 0 0 0 0 0 0 0 0 0 0 0 0
  146. """,
  147. ]
  148. simulation = Simulation()
  149. subjects = get_subjects_from_str_representation(
  150. str_representations[0],
  151. simulation,
  152. )
  153. simulation.subjects = subjects
  154. cycle_manager = CycleManager(
  155. simulation=simulation,
  156. )
  157. for str_representation in str_representations:
  158. assert str_representation == \
  159. self._get_str_representation_of_subjects(
  160. subjects,
  161. )
  162. cycle_manager.next()