test_duration.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import unittest
  2. from sccd.util.duration import *
  3. class TestDuration(unittest.TestCase):
  4. def test_equal(self):
  5. # The same amount of time, but objects not considered equal.
  6. x = duration(1000, Millisecond)
  7. y = duration(1, Second)
  8. z = duration(3, Day)
  9. self.assertEqual(x, y)
  10. self.assertEqual(y, x)
  11. self.assertNotEqual(x, z)
  12. def test_gcd(self):
  13. x = duration(20, Second)
  14. y = duration(100, Microsecond)
  15. u = gcd(x, y)
  16. v = gcd(y, x)
  17. self.assertEqual(u, duration(100, Microsecond))
  18. self.assertEqual(v, duration(100, Microsecond))
  19. def test_gcd_zero(self):
  20. x = duration(0)
  21. y = duration(100, Microsecond)
  22. u = gcd(x, y)
  23. v = gcd(y, x)
  24. self.assertEqual(u, duration(100, Microsecond))
  25. self.assertEqual(v, duration(100, Microsecond))
  26. def test_gcd_many(self):
  27. l = [duration(3, Microsecond), duration(0), duration(10, Millisecond)]
  28. u = gcd(*l)
  29. self.assertEqual(u, duration(1, Microsecond))
  30. def test_gcd_few(self):
  31. l = [duration(3, Microsecond)]
  32. u = gcd(*l)
  33. self.assertEqual(u, duration(3, Microsecond))
  34. def test_gcd_none(self):
  35. l = []
  36. u = gcd(*l)
  37. self.assertEqual(u, duration(0))
  38. def test_mult(self):
  39. x = duration(10, Millisecond)
  40. self.assertEqual(x * 10, duration(100, Millisecond))
  41. self.assertEqual(10 * x, duration(100, Millisecond))
  42. def test_floordiv(self):
  43. x = duration(100, Millisecond)
  44. y = duration(10, Millisecond)
  45. z = duration(3, Millisecond)
  46. # Duration divided by duration is factor
  47. self.assertEqual(x // y, 10)
  48. self.assertEqual(y // x, 0)
  49. self.assertEqual(x // z, 33)
  50. self.assertRaises(ZeroDivisionError, lambda: x // duration(0))
  51. def test_mod(self):
  52. x = duration(100, Millisecond)
  53. y = duration(10, Microsecond)
  54. z = duration(1, Second)
  55. i = duration(3, Millisecond)
  56. self.assertEqual(x % y, duration(0))
  57. self.assertEqual(x % z, duration(100, Millisecond))
  58. self.assertEqual(x % i, duration(1, Millisecond))