def test_computations(self): eq = self.assertEqual a = Duration(7) # One week b = Duration(0, 60) # One minute c = Duration(0, 0, 1000) # One millisecond eq(a + b + c, Duration(7, 60, 1000)) eq(a - b, Duration(6, 24 * 3600 - 60)) eq(b.__rsub__(a), Duration(6, 24 * 3600 - 60)) eq(-a, Duration(-7)) eq(+a, Duration(7)) eq(-b, Duration(-1, 24 * 3600 - 60)) eq(-c, Duration(-1, 24 * 3600 - 1, 999000)) eq(abs(a), a) eq(abs(-a), a) eq(Duration(6, 24 * 3600), a) eq(Duration(0, 0, 60 * 1000000), b) eq(a * 10, Duration(70)) eq(a * 10, 10 * a) eq(a * 10, 10 * a) eq(b * 10, Duration(0, 600)) eq(10 * b, Duration(0, 600)) eq(b * 10, Duration(0, 600)) eq(c * 10, Duration(0, 0, 10000)) eq(10 * c, Duration(0, 0, 10000)) eq(c * 10, Duration(0, 0, 10000)) eq(a * -1, -a) eq(b * -2, -b - b) eq(c * -2, -c + -c) eq(b * (60 * 24), (b * 60) * 24) eq(b * (60 * 24), (60 * b) * 24) eq(c * 1000, Duration(0, 1)) eq(1000 * c, Duration(0, 1)) eq(a // 7, Duration(1)) eq(b // 10, Duration(0, 6)) eq(c // 1000, Duration(0, 0, 1)) eq(a // 10, Duration(0, 7 * 24 * 360)) eq(a // 3600000, Duration(0, 0, 7 * 24 * 1000)) eq(a / 0.5, Duration(14)) eq(b / 0.5, Duration(0, 120)) eq(a / 7, Duration(1)) eq(b / 10, Duration(0, 6)) eq(c / 1000, Duration(0, 0, 1)) eq(a / 10, Duration(0, 7 * 24 * 360)) eq(a / 3600000, Duration(0, 0, 7 * 24 * 1000)) # Multiplication by float us = Duration(microseconds=1) eq((3 * us) * 0.5, 2 * us) eq((5 * us) * 0.5, 2 * us) eq(0.5 * (3 * us), 2 * us) eq(0.5 * (5 * us), 2 * us) eq((-3 * us) * 0.5, -2 * us) eq((-5 * us) * 0.5, -2 * us) # See http://bugs.python.org/review/23521 eq(Duration(seconds=1) * 0.123456, Duration(microseconds=123456)) eq(Duration(seconds=1) * 0.6112295, Duration(microseconds=611230)) # Division by int and float eq((3 * us) / 2, 2 * us) eq((5 * us) / 2, 2 * us) eq((-3 * us) / 2.0, -2 * us) eq((-5 * us) / 2.0, -2 * us) eq((3 * us) / -2, -2 * us) eq((5 * us) / -2, -2 * us) eq((3 * us) / -2.0, -2 * us) eq((5 * us) / -2.0, -2 * us) for i in range(-10, 10): eq((i * us / 3) // us, round(i / 3)) for i in range(-10, 10): eq((i * us / -3) // us, round(i / -3)) # See http://bugs.python.org/review/23521 eq(Duration(seconds=1) / (1 / 0.6112295), Duration(microseconds=611230)) # http://bugs.python.org/review/11576 eq(Duration(999999999, 86399, 999999) - Duration(999999999, 86399, 999998), Duration(0, 0, 1)) eq(Duration(999999999, 1, 1) - Duration(999999999, 1, 0), Duration(0, 0, 1))