test_life_game.py 5.1KB

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