test_xyz.py 6.0KB

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