test_xyz.py 6.2KB

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