test_life_game.py 5.4KB

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