summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_datetime.py
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-05-31 17:33:47 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-05-31 17:33:47 (GMT)
commit1790bc43bf8430b219116f4af0423c3a732c9eb8 (patch)
tree57771b07db60bebd3fb1d038fb7c5c541f3dcb6e /Lib/test/test_datetime.py
parent9103597ee7e318f5c7183a1b1c97e5322f1f77aa (diff)
downloadcpython-1790bc43bf8430b219116f4af0423c3a732c9eb8.zip
cpython-1790bc43bf8430b219116f4af0423c3a732c9eb8.tar.gz
cpython-1790bc43bf8430b219116f4af0423c3a732c9eb8.tar.bz2
Issue #1289118: datetime.timedelta objects can now be multiplied by float
and divided by float and int objects.
Diffstat (limited to 'Lib/test/test_datetime.py')
-rw-r--r--Lib/test/test_datetime.py72
1 files changed, 62 insertions, 10 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index b751820..7c5fa9f 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -25,6 +25,16 @@ assert len(pickle_choices) == 3
OTHERSTUFF = (10, 34.5, "abc", {}, [], ())
+# XXX Copied from test_float.
+INF = float("inf")
+NAN = float("nan")
+
+# decorator for skipping tests on non-IEEE 754 platforms
+requires_IEEE_754 = unittest.skipUnless(
+ float.__getformat__("double").startswith("IEEE"),
+ "test requires IEEE 754 doubles")
+
+
#############################################################################
# module tests
@@ -225,6 +235,36 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
eq(c//1000, td(0, 0, 1))
eq(a//10, td(0, 7*24*360))
eq(a//3600000, td(0, 0, 7*24*1000))
+ eq(a/0.5, td(14))
+ eq(b/0.5, td(0, 120))
+ eq(a/7, td(1))
+ eq(b/10, td(0, 6))
+ eq(c/1000, td(0, 0, 1))
+ eq(a/10, td(0, 7*24*360))
+ eq(a/3600000, td(0, 0, 7*24*1000))
+
+ # Multiplication by float
+ us = td(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)
+
+ # 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))
def test_disallowed_computations(self):
a = timedelta(42)
@@ -236,20 +276,19 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(TypeError, lambda: i+a)
self.assertRaises(TypeError, lambda: i-a)
- # Mul/div by float isn't supported.
- x = 2.3
- self.assertRaises(TypeError, lambda: a*x)
- self.assertRaises(TypeError, lambda: x*a)
- self.assertRaises(TypeError, lambda: a/x)
- self.assertRaises(TypeError, lambda: x/a)
- self.assertRaises(TypeError, lambda: a // x)
- self.assertRaises(TypeError, lambda: x // a)
-
# Division of int by timedelta doesn't make sense.
# Division by zero doesn't make sense.
zero = 0
self.assertRaises(TypeError, lambda: zero // a)
self.assertRaises(ZeroDivisionError, lambda: a // zero)
+ self.assertRaises(ZeroDivisionError, lambda: a / zero)
+ self.assertRaises(ZeroDivisionError, lambda: a / 0.0)
+
+ @requires_IEEE_754
+ def test_disallowed_special(self):
+ a = timedelta(42)
+ self.assertRaises(ValueError, a.__mul__, NAN)
+ self.assertRaises(ValueError, a.__truediv__, NAN)
def test_basic_attributes(self):
days, seconds, us = 1, 7, 31
@@ -410,6 +449,19 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(OverflowError, lambda: -timedelta.max)
+ day = timedelta(1)
+ self.assertRaises(OverflowError, day.__mul__, 10**9)
+ self.assertRaises(OverflowError, day.__mul__, 1e9)
+ self.assertRaises(OverflowError, day.__truediv__, 1e-20)
+ self.assertRaises(OverflowError, day.__truediv__, 1e-10)
+ self.assertRaises(OverflowError, day.__truediv__, 9e-10)
+
+ @requires_IEEE_754
+ def _test_overflow_special(self):
+ day = timedelta(1)
+ self.assertRaises(OverflowError, day.__mul__, INF)
+ self.assertRaises(OverflowError, day.__mul__, -INF)
+
def test_microsecond_rounding(self):
td = timedelta
eq = self.assertEqual
@@ -489,7 +541,7 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(ZeroDivisionError, truediv, t, zerotd)
self.assertRaises(ZeroDivisionError, floordiv, t, zerotd)
- self.assertRaises(TypeError, truediv, t, 2)
+ # self.assertRaises(TypeError, truediv, t, 2)
# note: floor division of a timedelta by an integer *is*
# currently permitted.