test_fgFDM.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. """
  3. Unit tests for the fgFDM library
  4. """
  5. from __future__ import print_function
  6. import unittest
  7. from pymavlink.fgFDM import fgFDMError, fgFDMVariable, fgFDMVariableList, fgFDM
  8. class fgFDMErrorTest(unittest.TestCase):
  9. """
  10. Class to test fgFDMError
  11. """
  12. def __init__(self, *args, **kwargs):
  13. """Constructor, set up some data that is reused in many tests"""
  14. super(fgFDMErrorTest, self).__init__(*args, **kwargs)
  15. def test_constructor(self):
  16. ex = fgFDMError("Test Exception {0}".format(1))
  17. assert ex.message == "fgFDMError: Test Exception 1"
  18. class fgFDMVariableTest(unittest.TestCase):
  19. """
  20. Class to test fgFDMVariable
  21. """
  22. def __init__(self, *args, **kwargs):
  23. """Constructor, set up some data that is reused in many tests"""
  24. super(fgFDMVariableTest, self).__init__(*args, **kwargs)
  25. def test_constructor(self):
  26. """Test the constructor"""
  27. varry = fgFDMVariable(0, 3, 'radians')
  28. assert varry.index == 0
  29. assert varry.arraylength == 3
  30. assert varry.units == 'radians'
  31. class fgFDMVariableListTest(unittest.TestCase):
  32. """
  33. Class to test fgFDMVariableList
  34. """
  35. def __init__(self, *args, **kwargs):
  36. """Constructor, set up some data that is reused in many tests"""
  37. super(fgFDMVariableListTest, self).__init__(*args, **kwargs)
  38. def test_constructor(self):
  39. """Test the constructor and adding variables"""
  40. mapping = fgFDMVariableList()
  41. mapping.add('longitude', units='radians')
  42. mapping.add('stall_warning')
  43. mapping.add('rpm', 4)
  44. assert mapping._nextidx == 6
  45. assert mapping.vars['longitude'].index == 0
  46. assert mapping.vars['longitude'].units == 'radians'
  47. assert mapping.vars['rpm'].index == 2
  48. assert mapping.vars['rpm'].units is None
  49. class fgFDMTest(unittest.TestCase):
  50. """
  51. Class to test fgFDM
  52. """
  53. def __init__(self, *args, **kwargs):
  54. """Constructor, set up some data that is reused in many tests"""
  55. super(fgFDMTest, self).__init__(*args, **kwargs)
  56. def test_constructor(self):
  57. """Test the constructor"""
  58. fdm = fgFDM()
  59. assert fdm.FG_NET_FDM_VERSION == 24
  60. def test_getset(self):
  61. """Test the getting and setting and unit conversion of variables"""
  62. fdm = fgFDM()
  63. fdm.set('latitude', 67.4, units='degrees')
  64. fdm.set('longitude', 120.6, units='degrees')
  65. fdm.set('num_engines', 1)
  66. fdm.set('vcas', 44, units='mps')
  67. assert fdm.get('latitude', units='degrees') == 67.4
  68. assert round(fdm.get('vcas', units='knots'), 2) == 85.53
  69. def test_packparse(self):
  70. """Test the packing and parsing of an fgFDM packet"""
  71. fdm = fgFDM()
  72. fdm.set('latitude', 67.4, units='degrees')
  73. fdm.set('longitude', 120.6, units='degrees')
  74. fdm.set('num_engines', 1)
  75. fdm.set('vcas', 44, units='mps')
  76. packedBytes = fdm.pack()
  77. parsedObj = fgFDM()
  78. parsedObj.parse(packedBytes)
  79. assert parsedObj.get('latitude', units='degrees') == 67.4
  80. assert round(parsedObj.get('vcas', units='knots'), 2) == 85.53
  81. if __name__ == '__main__':
  82. unittest.main()