testHelpers.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 *
  17. class TestHelpers(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. setLogger('None', ('localhost', 514), logging.WARN)
  22. def test_helper_add_dict(self):
  23. a = {"a": 3, "b": 6, "c": 2}
  24. b = {"a": 4, "b": 1, "c": 7}
  25. addDict(a, b)
  26. self.assertTrue(a == {"a": 7, "b": 7, "c": 9})
  27. b = {"a": 4, "b": 1, "c": 7}
  28. addDict(a, b)
  29. self.assertTrue(a == {"a": 11, "b": 8, "c": 16})
  30. b = {"c": 1}
  31. addDict(a, b)
  32. self.assertTrue(a == {"a": 11, "b": 8, "c": 17})
  33. b = {"a": -21}
  34. addDict(a, b)
  35. self.assertTrue(a == {"a": -10, "b": 8, "c": 17})
  36. b = {"a": 0, "d": 9}
  37. addDict(a, b)
  38. self.assertTrue(a == {"a": -10, "b": 8, "c": 17, "d": 9})
  39. b = {}
  40. addDict(a, b)
  41. self.assertTrue(a == {"a": -10, "b": 8, "c": 17, "d": 9})
  42. b = {"a": 10, "b": -8, "c": -17, "d": -9, "def": 3}
  43. addDict(a, b)
  44. self.assertTrue(a == {"a": 0, "b": 0, "c": 0, "d": 0, "def": 3})
  45. b = {"d": -9, "def": 2}
  46. addDict(a, b)
  47. self.assertTrue(a == {"a": 0, "b": 0, "c": 0, "d": -9, "def": 5})
  48. def test_helper_all_zero_dict(self):
  49. a = {"a": 0}
  50. self.assertTrue(allZeroDict(a))
  51. a = {}
  52. self.assertTrue(allZeroDict(a))
  53. a = {"a": 0, "b": 0, "c": 0}
  54. self.assertTrue(allZeroDict(a))
  55. a = {"a": 1, "b": 0}
  56. self.assertFalse(allZeroDict(a))
  57. a = {"a": 1, "b": -1}
  58. self.assertFalse(allZeroDict(a))
  59. a = {"a": 0, "b": 1}
  60. self.assertFalse(allZeroDict(a))
  61. a = {"a": -1}
  62. self.assertFalse(allZeroDict(a))
  63. def test_toStr(self):
  64. self.assertTrue(toStr("abc") == "'abc'")
  65. self.assertTrue(toStr("'abc") == "''abc'")
  66. self.assertTrue(toStr(5) == "'5'")
  67. self.assertTrue(toStr([1, 2, 3]) == "'[1, 2, 3]'")
  68. def test_DEVSException(self):
  69. self.assertTrue(str(DEVSException("ABC")) == "DEVS Exception: ABC")
  70. def test_easyCommand(self):
  71. self.assertTrue(easyCommand("abc", []) == "abc()")
  72. self.assertTrue(easyCommand("abc", ["1", "def"]) == "abc(1, def)")
  73. self.assertTrue(easyCommand("abc", ["'def'"]) == "abc('def')")