test_xyz.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # -*- coding: utf-8 -*-
  2. from synergine2.simulation import Subject
  3. from synergine2.simulation import Subjects
  4. from synergine2.simulation import Simulation
  5. from synergine2.xyz import ProximitySubjectMechanism
  6. from synergine2.xyz import XYZSubjectMixin
  7. from synergine2.xyz_utils import get_positions_from_str_representation
  8. from synergine2.xyz_utils import get_str_representation_from_positions
  9. from tests import BaseTest
  10. from tests import str_kwargs
  11. class MySubject(XYZSubjectMixin, Subject):
  12. pass
  13. class MyProximityMechanism(ProximitySubjectMechanism):
  14. distance = 10
  15. class TestXYZ(BaseTest):
  16. def test_proximity_mechanism_with_one(self):
  17. simulation = Simulation()
  18. subject = MySubject(simulation, position=(0, 0, 0))
  19. other_subject = MySubject(simulation, position=(5, 0, 0))
  20. simulation.subjects = Subjects(
  21. [subject, other_subject],
  22. simulation=simulation,
  23. )
  24. proximity_mechanism = MyProximityMechanism(
  25. simulation=simulation,
  26. subject=subject,
  27. )
  28. assert 5 == proximity_mechanism.get_distance_of(
  29. position=subject.position,
  30. subject=other_subject,
  31. )
  32. assert [{
  33. 'subject': other_subject,
  34. 'direction': 90.0,
  35. 'distance': 5.0,
  36. }] == proximity_mechanism.run()
  37. def test_proximity_mechanism_excluding(self):
  38. simulation = Simulation()
  39. subject = MySubject(simulation, position=(0, 0, 0))
  40. other_subject = MySubject(simulation, position=(11, 0, 0))
  41. simulation.subjects = Subjects(
  42. [subject, other_subject],
  43. simulation=simulation,
  44. )
  45. proximity_mechanism = MyProximityMechanism(
  46. simulation=simulation,
  47. subject=subject,
  48. )
  49. assert 11 == proximity_mechanism.get_distance_of(
  50. position=subject.position,
  51. subject=other_subject,
  52. )
  53. # other_subject is to far away
  54. assert [] == proximity_mechanism.run()
  55. def test_proximity_mechanism_with_multiple(self):
  56. simulation = Simulation()
  57. subject = MySubject(simulation, position=(0, 0, 0))
  58. other_subjects = []
  59. for i in range(3):
  60. other_subjects.append(MySubject(simulation, position=(i, i, 0)))
  61. simulation.subjects = Subjects([subject], simulation=simulation)
  62. simulation.subjects.extend(other_subjects)
  63. proximity_mechanism = MyProximityMechanism(
  64. simulation=simulation,
  65. subject=subject,
  66. )
  67. data = proximity_mechanism.run()
  68. assert [
  69. {
  70. 'direction': 0,
  71. 'subject': other_subjects[0],
  72. 'distance': 0.0,
  73. },
  74. {
  75. 'direction': 135.0,
  76. 'subject': other_subjects[1],
  77. 'distance': 1.41
  78. },
  79. {
  80. 'direction': 135.0,
  81. 'subject': other_subjects[2],
  82. 'distance': 2.83
  83. },
  84. ] == data
  85. def test_str_representation_from_str(self):
  86. str_ = """
  87. 0 0 1 0 0
  88. 0 1 1 1 0
  89. 0 0 1 0 0
  90. """
  91. items_positions = {
  92. '0': [
  93. (-2, -1, 0),
  94. (-1, -1, 0),
  95. (1, -1, 0),
  96. (2, -1, 0),
  97. (-2, 0, 0),
  98. (2, 0, 0),
  99. (-2, 1, 0),
  100. (-1, 1, 0),
  101. (1, 1, 0),
  102. (2, 1, 0),
  103. ],
  104. '1': [
  105. (0, -1, 0),
  106. (-1, 0, 0),
  107. (0, 0, 0),
  108. (1, 0, 0),
  109. (0, 1, 0),
  110. ],
  111. }
  112. assert items_positions == get_positions_from_str_representation(str_)
  113. def test_str_representation_to_str(self):
  114. expected = """
  115. 0 0 1 0 0
  116. 0 1 1 1 0
  117. 0 0 1 0 0
  118. """
  119. items_positions = {
  120. '0': [
  121. (-2, -1, 0),
  122. (-1, -1, 0),
  123. (1, -1, 0),
  124. (2, -1, 0),
  125. (-2, 0, 0),
  126. (2, 0, 0),
  127. (-2, 1, 0),
  128. (-1, 1, 0),
  129. (1, 1, 0),
  130. (2, 1, 0),
  131. ],
  132. '1': [
  133. (0, -1, 0),
  134. (-1, 0, 0),
  135. (0, 0, 0),
  136. (1, 0, 0),
  137. (0, 1, 0),
  138. ],
  139. }
  140. assert expected == \
  141. get_str_representation_from_positions(
  142. items_positions,
  143. **str_kwargs
  144. )
  145. # def test_str_representation_to_str_multi_levels(self):
  146. # expected = """
  147. # 0 0 1 0 0
  148. # 0 1 1 1 0
  149. # 0 0 1 0 0
  150. # ----
  151. # 0 0 0 0 0
  152. # 0 0 1 0 0
  153. # 0 0 0 0 0
  154. # """
  155. # items_positions = {
  156. # '0': [
  157. # (-2, -1, 0),
  158. # (-1, -1, 0),
  159. # (1, -1, 0),
  160. # (2, -1, 0),
  161. # (-2, 0, 0),
  162. # (2, 0, 0),
  163. # (-2, 1, 0),
  164. # (-1, 1, 0),
  165. # (1, 1, 0),
  166. # (2, 1, 0),
  167. # (-2, -1, 1),
  168. # (-1, -1, 1),
  169. # (1, -1, 1),
  170. # (2, -1, 1),
  171. # (-1, 0, 1),
  172. # (-2, 0, 1),
  173. # (2, 0, 1),
  174. # (-2, 1, 1),
  175. # (-1, 1, 1),
  176. # (1, 1, 1),
  177. # (2, 1, 1),
  178. # (2, -1, 1),
  179. # (1, 0, 1),
  180. # (2, 1, 1),
  181. # ],
  182. # '1': [
  183. # (0, -1, 0),
  184. # (-1, 0, 0),
  185. # (0, 0, 0),
  186. # (1, 0, 0),
  187. # (0, 1, 0),
  188. # (0, 0, 1),
  189. # ],
  190. # }
  191. #
  192. # assert expected == \
  193. # get_str_representation_from_positions(
  194. # items_positions,
  195. # **str_kwargs
  196. # )