movingavg.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from __future__ import division, absolute_import, print_function, unicode_literals
  2. from awlsim_tstlib import *
  3. initTest(__file__)
  4. from awlsim.common.movingavg import * #+cimport
  5. from awlsim.common.exceptions import *
  6. class Test_MovingAvg(TestCase):
  7. def test_case_0(self):
  8. m = MovingAvg(3)
  9. for i in range(10):
  10. self.assertEqual(m.calculate(0.0), 0.0)
  11. def test_case_1(self):
  12. m = MovingAvg(1)
  13. for i in range(10):
  14. self.assertAlmostEqual(m.calculate(float(i)), float(i))
  15. def test_case_2(self):
  16. m = MovingAvg(1)
  17. for i in range(0, -10, -1):
  18. self.assertAlmostEqual(m.calculate(float(i)), float(i))
  19. def test_case_3(self):
  20. self.assertRaises(AwlSimError, lambda: MovingAvg(0))
  21. self.assertRaises(AwlSimError, lambda: MovingAvg(-1))
  22. def test_case_4(self):
  23. m = MovingAvg(3)
  24. self.assertAlmostEqual(m.calculate(1.0), 1.0, places=3)
  25. self.assertAlmostEqual(m.calculate(2.0), 1.5, places=3)
  26. self.assertAlmostEqual(m.calculate(2.0), 1.6666, places=3)
  27. self.assertAlmostEqual(m.calculate(2.0), 2.0, places=3)
  28. self.assertAlmostEqual(m.calculate(3.0), 2.3333, places=3)
  29. self.assertAlmostEqual(m.calculate(10.0), 5.0, places=3)
  30. self.assertAlmostEqual(m.calculate(20.0), 11.0, places=3)
  31. self.assertAlmostEqual(m.calculate(300.0), 110.0, places=3)
  32. self.assertAlmostEqual(m.calculate(1.2), 107.0666, places=3)
  33. self.assertAlmostEqual(m.calculate(1.2), 100.8, places=3)
  34. self.assertAlmostEqual(m.calculate(1.2), 1.2, places=3)
  35. def test_case_5(self):
  36. m = MovingAvg(5)
  37. self.assertAlmostEqual(m.calculate(-1.0), -1.0, places=3)
  38. self.assertAlmostEqual(m.calculate(-1.0), -1.0, places=3)
  39. self.assertAlmostEqual(m.calculate(-7.7), -3.2333, places=3)
  40. self.assertAlmostEqual(m.calculate(-6.1), -3.95, places=3)
  41. self.assertAlmostEqual(m.calculate(-6.1), -4.38, places=3)
  42. self.assertAlmostEqual(m.calculate(-11.5), -6.48, places=3)
  43. self.assertAlmostEqual(m.calculate(-12.2), -8.72, places=3)
  44. self.assertAlmostEqual(m.calculate(1000.0), 192.82, places=3)