testActions.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from testutils import *
  16. from pypdevs.util import DEVSException
  17. class TestActions(unittest.TestCase):
  18. # Tests the externalInput function, which takes messages of the form:
  19. # [[time, age], content, anti-message, UUID, color]
  20. def setUp(self):
  21. self.sim = basicSim()
  22. def tearDown(self):
  23. self.sim.run_gvt = False
  24. def test_actions_delayed_action_normal(self):
  25. self.sim.gvt = 0
  26. plist = []
  27. plist.append([(1, 1), "model1", "ABC"])
  28. plist.append([(2, 1), "model3", "ABC"])
  29. plist.append([(3, 1), "model1", "ABE"])
  30. plist.append([(2, 6), "model1", "DBC"])
  31. plist.append([(0, 1), "model2", "ABZ"])
  32. plist.append([(7, 1), "model1", "A5C"])
  33. # Messages should not be sorted in the actions list
  34. for i in plist:
  35. self.sim.delayedAction(i[0], i[1], i[2])
  36. self.assertTrue(self.sim.actions == plist)
  37. def test_actions_text_revertion(self):
  38. self.sim.gvt = 1
  39. plist = []
  40. plist.append([(1, 1), "model1", "ABC"])
  41. plist.append([(2, 1), "model3", "ABC"])
  42. plist.append([(3, 1), "model1", "ABE"])
  43. plist.append([(2, 6), "model1", "DBC"])
  44. # This one should be filtered out
  45. plist.append([(0, 1), "model2", "ABZ"])
  46. plist.append([(7, 1), "model1", "A5C"])
  47. # Messages should not be sorted in the toPrint list
  48. rlist = []
  49. for i in plist:
  50. if i[0][0] < self.sim.gvt:
  51. try:
  52. self.sim.delayedAction(i[0], i[1], i[2])
  53. # Should throw an exception
  54. self.fail() #pragma: nocover
  55. except DEVSException:
  56. # OK
  57. pass
  58. else:
  59. # This one should work fine
  60. self.sim.delayedAction(i[0], i[1], i[2])
  61. rlist.append(i)
  62. self.assertTrue(self.sim.actions == rlist)
  63. def test_actions_perform(self):
  64. self.sim.gvt = 0
  65. plist = []
  66. # Should not need to be added in correct order!
  67. # Those that should not be executed contain faulty code
  68. # (a division by zero)
  69. plist.append([(0, 0), "model1", "pass"])
  70. plist.append([(3, 1), "model2", "pass"])
  71. plist.append([(2, 1), "model3", "pass"])
  72. plist.append([(2, 1), "model1", "pass"])
  73. plist.append([(6, 4), "model2", "1/0"])
  74. plist.append([(4, 3), "model1", "pass"])
  75. plist.append([(8, 2), "model1", "1/0"])
  76. plist.append([(9, 9), "model3", "1/0"])
  77. plist2 = []
  78. # Should be in this exact order, otherwise this part was sorted too
  79. plist2.append(plist[4])
  80. plist2.append(plist[6])
  81. plist2.append(plist[7])
  82. self.sim.actions = list(plist)
  83. # Perform all actions up to time 5
  84. try:
  85. self.sim.performActions(5)
  86. except ZeroDivisionError: #pragma: nocover
  87. # executed a bad piece of code!
  88. self.fail("Executed too much code")
  89. self.assertTrue(self.sim.actions == plist2)
  90. # Now execute a command that should crash, to make sure that
  91. # this code is executed and not just deleted
  92. try:
  93. self.sim.performActions(7)
  94. self.fail("Didn't execute desired code") #pragma: nocover
  95. except ZeroDivisionError:
  96. pass
  97. def test_actions_remove_revert(self):
  98. self.sim.gvt = 5
  99. plist = []
  100. # Should not need to be added in correct order!
  101. plist.append([(0, 0), "model1", "ABC"])
  102. plist.append([(3, 1), "model2", "ABC"])
  103. plist.append([(2, 1), "model3", "ABC"])
  104. plist.append([(2, 1), "model1", "ABC"])
  105. plist.append([(8, 4), "model2", "ABC"])
  106. plist.append([(4, 3), "model1", "ABC"])
  107. plist.append([(8, 2), "model1", "ABC"])
  108. plist.append([(9, 9), "model3", "ABC"])
  109. # Make a copy...
  110. self.sim.actions = list(plist)
  111. self.sim.removeActions("model1", (9, 1))
  112. # No messages from this time on
  113. self.assertTrue(self.sim.actions == plist)
  114. try:
  115. self.sim.removeActions("model1", (4, 3))
  116. # Revertion from before the GVT, should be denied!
  117. self.fail() #pragma: nocover
  118. except DEVSException:
  119. pass
  120. # Nothing should have happend to the print list
  121. self.assertTrue(self.sim.actions == plist)
  122. self.sim.removeActions("model2", (5,1))
  123. # One message from model2 should be removed, as it is just at the GVT
  124. clist = []
  125. clist.append([(0, 0), "model1", "ABC"])
  126. clist.append([(3, 1), "model2", "ABC"])
  127. clist.append([(2, 1), "model3", "ABC"])
  128. clist.append([(2, 1), "model1", "ABC"])
  129. clist.append([(4, 3), "model1", "ABC"])
  130. clist.append([(8, 2), "model1", "ABC"])
  131. clist.append([(9, 9), "model3", "ABC"])
  132. try:
  133. self.sim.removeActions("QDFQF", (0,0))
  134. # Should still cause an error, even if there is nothing < GVT
  135. self.fail() #pragma: nocover
  136. except DEVSException:
  137. pass
  138. self.assertTrue(self.sim.actions == clist)
  139. def test_actions_remove_normal(self):
  140. self.sim.gvt = 0
  141. plist = []
  142. # Should not need to be added in correct order!
  143. plist.append([(0, 0), "model1", "ABC"])
  144. plist.append([(3, 1), "model2", "ABC"])
  145. plist.append([(2, 1), "model3", "ABC"])
  146. plist.append([(2, 1), "model1", "ABC"])
  147. plist.append([(8, 4), "model2", "ABC"])
  148. plist.append([(4, 3), "model1", "ABC"])
  149. plist.append([(8, 2), "model1", "ABC"])
  150. plist.append([(9, 9), "model3", "ABC"])
  151. # Make a copy...
  152. self.sim.actions = list(plist)
  153. self.sim.removeActions("model1", (9, 1))
  154. # No messages from this time on
  155. self.assertTrue(self.sim.actions == plist)
  156. self.sim.removeActions("model1", (4, 3))
  157. # The message at [4,3] itself should also be removed
  158. clist = []
  159. clist.append([(0, 0), "model1", "ABC"])
  160. clist.append([(3, 1), "model2", "ABC"])
  161. clist.append([(2, 1), "model3", "ABC"])
  162. clist.append([(2, 1), "model1", "ABC"])
  163. clist.append([(8, 4), "model2", "ABC"])
  164. clist.append([(9, 9), "model3", "ABC"])
  165. self.assertTrue(self.sim.actions == clist)
  166. self.sim.removeActions("model2", (0,0))
  167. # All messages from model2 should be removed
  168. clist = []
  169. clist.append([(0, 0), "model1", "ABC"])
  170. clist.append([(2, 1), "model3", "ABC"])
  171. clist.append([(2, 1), "model1", "ABC"])
  172. clist.append([(9, 9), "model3", "ABC"])
  173. self.assertTrue(self.sim.actions == clist)
  174. self.sim.removeActions("QDFQF", (0,0))
  175. # Shouldn't make a difference as the model does not exist
  176. self.assertTrue(self.sim.actions == clist)