test_xyz.py 6.5KB

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