test_life_game.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.simulation import Subjects
  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. **str_kwargs
  22. )
  23. class TestSimpleSimulation(LifeGameBaseTest):
  24. def test_cycles_evolution(self):
  25. simulation = Simulation()
  26. subjects = self._get_subjects(simulation)
  27. simulation.subjects = subjects
  28. cycle_manager = CycleManager(
  29. subjects=subjects,
  30. )
  31. assert """
  32. 0 0 0 0 0
  33. 0 1 1 1 0
  34. 0 0 0 0 0
  35. """ == self._get_str_representation_of_subjects(
  36. subjects,
  37. )
  38. cycle_manager.next()
  39. assert """
  40. 0 0 1 0 0
  41. 0 0 1 0 0
  42. 0 0 1 0 0
  43. """ == self._get_str_representation_of_subjects(
  44. subjects,
  45. )
  46. cycle_manager.next()
  47. assert """
  48. 0 0 0 0 0
  49. 0 1 1 1 0
  50. 0 0 0 0 0
  51. """ == self._get_str_representation_of_subjects(
  52. subjects,
  53. )
  54. def _get_subjects(self, simulation: Simulation):
  55. cells = Subjects(simulation=simulation)
  56. for position in [
  57. (-1, 0, 0),
  58. (0, 0, 0),
  59. (1, 0, 0),
  60. ]:
  61. cells.append(Cell(
  62. simulation=simulation,
  63. position=position,
  64. ))
  65. for position in [
  66. (-2, -1, 0),
  67. (-1, -1, 0),
  68. (0, -1, 0),
  69. (1, -1, 0),
  70. (2, -1, 0),
  71. (-2, 0, 0),
  72. (2, 0, 0),
  73. (-2, 1, 0),
  74. (-1, 1, 0),
  75. (0, 1, 0),
  76. (1, 1, 0),
  77. (2, 1, 0),
  78. ]:
  79. cells.append(Empty(
  80. simulation=simulation,
  81. position=position,
  82. ))
  83. return cells
  84. class TestMultipleSimulations(LifeGameBaseTest):
  85. def test_cross(self):
  86. str_representations = [
  87. """
  88. 0 0 0 0 0 0 0 0 0 0 0
  89. 0 0 0 1 1 1 1 0 0 0 0
  90. 0 0 0 1 0 0 1 0 0 0 0
  91. 0 1 1 1 0 0 1 1 1 0 0
  92. 0 1 0 0 0 0 0 0 1 0 0
  93. 0 1 0 0 0 0 0 0 1 0 0
  94. 0 1 1 1 0 0 1 1 1 0 0
  95. 0 0 0 1 0 0 1 0 0 0 0
  96. 0 0 0 1 1 1 1 0 0 0 0
  97. 0 0 0 0 0 0 0 0 0 0 0
  98. 0 0 0 0 0 0 0 0 0 0 0
  99. """,
  100. """
  101. 0 0 0 0 1 1 0 0 0 0 0
  102. 0 0 0 1 1 1 1 0 0 0 0
  103. 0 0 0 0 0 0 0 0 0 0 0
  104. 0 1 0 1 0 0 1 0 1 0 0
  105. 1 1 0 0 0 0 0 0 1 1 0
  106. 1 1 0 0 0 0 0 0 1 1 0
  107. 0 1 0 1 0 0 1 0 1 0 0
  108. 0 0 0 0 0 0 0 0 0 0 0
  109. 0 0 0 1 1 1 1 0 0 0 0
  110. 0 0 0 0 1 1 0 0 0 0 0
  111. 0 0 0 0 0 0 0 0 0 0 0
  112. """,
  113. """
  114. 0 0 0 1 0 0 1 0 0 0 0
  115. 0 0 0 1 0 0 1 0 0 0 0
  116. 0 0 1 1 0 0 1 1 0 0 0
  117. 1 1 1 0 0 0 0 1 1 1 0
  118. 0 0 0 0 0 0 0 0 0 0 0
  119. 0 0 0 0 0 0 0 0 0 0 0
  120. 1 1 1 0 0 0 0 1 1 1 0
  121. 0 0 1 1 0 0 1 1 0 0 0
  122. 0 0 0 1 0 0 1 0 0 0 0
  123. 0 0 0 1 0 0 1 0 0 0 0
  124. 0 0 0 0 0 0 0 0 0 0 0
  125. """,
  126. """
  127. 0 0 0 0 0 0 0 0 0 0 0
  128. 0 0 0 1 1 1 1 0 0 0 0
  129. 0 0 0 1 0 0 1 0 0 0 0
  130. 0 1 1 1 0 0 1 1 1 0 0
  131. 0 1 0 0 0 0 0 0 1 0 0
  132. 0 1 0 0 0 0 0 0 1 0 0
  133. 0 1 1 1 0 0 1 1 1 0 0
  134. 0 0 0 1 0 0 1 0 0 0 0
  135. 0 0 0 1 1 1 1 0 0 0 0
  136. 0 0 0 0 0 0 0 0 0 0 0
  137. 0 0 0 0 0 0 0 0 0 0 0
  138. """,
  139. """
  140. 0 0 0 0 1 1 0 0 0 0 0
  141. 0 0 0 1 1 1 1 0 0 0 0
  142. 0 0 0 0 0 0 0 0 0 0 0
  143. 0 1 0 1 0 0 1 0 1 0 0
  144. 1 1 0 0 0 0 0 0 1 1 0
  145. 1 1 0 0 0 0 0 0 1 1 0
  146. 0 1 0 1 0 0 1 0 1 0 0
  147. 0 0 0 0 0 0 0 0 0 0 0
  148. 0 0 0 1 1 1 1 0 0 0 0
  149. 0 0 0 0 1 1 0 0 0 0 0
  150. 0 0 0 0 0 0 0 0 0 0 0
  151. """,
  152. """
  153. 0 0 0 1 0 0 1 0 0 0 0
  154. 0 0 0 1 0 0 1 0 0 0 0
  155. 0 0 1 1 0 0 1 1 0 0 0
  156. 1 1 1 0 0 0 0 1 1 1 0
  157. 0 0 0 0 0 0 0 0 0 0 0
  158. 0 0 0 0 0 0 0 0 0 0 0
  159. 1 1 1 0 0 0 0 1 1 1 0
  160. 0 0 1 1 0 0 1 1 0 0 0
  161. 0 0 0 1 0 0 1 0 0 0 0
  162. 0 0 0 1 0 0 1 0 0 0 0
  163. 0 0 0 0 0 0 0 0 0 0 0
  164. """
  165. ]
  166. simulation = Simulation()
  167. subjects = get_subjects_from_str_representation(
  168. str_representations[0],
  169. simulation,
  170. )
  171. simulation.subjects = subjects
  172. cycle_manager = CycleManager(
  173. subjects=subjects,
  174. )
  175. for str_representation in str_representations:
  176. assert str_representation == \
  177. self._get_str_representation_of_subjects(
  178. subjects,
  179. )
  180. cycle_manager.next()