test_xyz.py 5.7KB

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