Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes | Private Member Functions
test.datetimetester.TestTimeDelta Class Reference
Inheritance diagram for test.datetimetester.TestTimeDelta:
Inheritance graph
[legend]
Collaboration diagram for test.datetimetester.TestTimeDelta:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_constructor
def test_computations
def test_disallowed_computations
def test_disallowed_special
def test_basic_attributes
def test_total_seconds
def test_carries
def test_hash_equality
def test_pickling
def test_compare
def test_str
def test_repr
def test_roundtrip
def test_resolution_info
def test_overflow
def test_microsecond_rounding
def test_massive_normalization
def test_bool
def test_subclass_timedelta
def test_division
def test_remainder
def test_divmod
def test_harmless_mixed_comparison
def test_harmful_mixed_comparison

Static Public Attributes

 theclass = timedelta

Private Member Functions

def _test_overflow_special

Detailed Description

Definition at line 287 of file datetimetester.py.


Member Function Documentation

Definition at line 594 of file datetimetester.py.

00594 
00595     def _test_overflow_special(self):
00596         day = timedelta(1)
00597         self.assertRaises(OverflowError, day.__mul__, INF)
00598         self.assertRaises(OverflowError, day.__mul__, -INF)

Here is the call graph for this function:

Definition at line 417 of file datetimetester.py.

00417 
00418     def test_basic_attributes(self):
00419         days, seconds, us = 1, 7, 31
00420         td = timedelta(days, seconds, us)
00421         self.assertEqual(td.days, days)
00422         self.assertEqual(td.seconds, seconds)
00423         self.assertEqual(td.microseconds, us)

Here is the call graph for this function:

Definition at line 625 of file datetimetester.py.

00625 
00626     def test_bool(self):
00627         self.assertTrue(timedelta(1))
00628         self.assertTrue(timedelta(0, 1))
00629         self.assertTrue(timedelta(0, 0, 1))
00630         self.assertTrue(timedelta(microseconds=1))
00631         self.assertTrue(not timedelta(0))

Here is the call graph for this function:

Definition at line 436 of file datetimetester.py.

00436 
00437     def test_carries(self):
00438         t1 = timedelta(days=100,
00439                        weeks=-7,
00440                        hours=-24*(100-49),
00441                        minutes=-3,
00442                        seconds=12,
00443                        microseconds=(3*60 - 12) * 1e6 + 1)
00444         t2 = timedelta(microseconds=1)
00445         self.assertEqual(t1, t2)

Here is the call graph for this function:

Definition at line 474 of file datetimetester.py.

00474 
00475     def test_compare(self):
00476         t1 = timedelta(2, 3, 4)
00477         t2 = timedelta(2, 3, 4)
00478         self.assertEqual(t1, t2)
00479         self.assertTrue(t1 <= t2)
00480         self.assertTrue(t1 >= t2)
00481         self.assertTrue(not t1 != t2)
00482         self.assertTrue(not t1 < t2)
00483         self.assertTrue(not t1 > t2)
00484 
00485         for args in (3, 3, 3), (2, 4, 4), (2, 3, 5):
00486             t2 = timedelta(*args)   # this is larger than t1
00487             self.assertTrue(t1 < t2)
00488             self.assertTrue(t2 > t1)
00489             self.assertTrue(t1 <= t2)
00490             self.assertTrue(t2 >= t1)
00491             self.assertTrue(t1 != t2)
00492             self.assertTrue(t2 != t1)
00493             self.assertTrue(not t1 == t2)
00494             self.assertTrue(not t2 == t1)
00495             self.assertTrue(not t1 > t2)
00496             self.assertTrue(not t2 < t1)
00497             self.assertTrue(not t1 >= t2)
00498             self.assertTrue(not t2 <= t1)
00499 
00500         for badarg in OTHERSTUFF:
00501             self.assertEqual(t1 == badarg, False)
00502             self.assertEqual(t1 != badarg, True)
00503             self.assertEqual(badarg == t1, False)
00504             self.assertEqual(badarg != t1, True)
00505 
00506             self.assertRaises(TypeError, lambda: t1 <= badarg)
00507             self.assertRaises(TypeError, lambda: t1 < badarg)
00508             self.assertRaises(TypeError, lambda: t1 > badarg)
00509             self.assertRaises(TypeError, lambda: t1 >= badarg)
00510             self.assertRaises(TypeError, lambda: badarg <= t1)
00511             self.assertRaises(TypeError, lambda: badarg < t1)
00512             self.assertRaises(TypeError, lambda: badarg > t1)
00513             self.assertRaises(TypeError, lambda: badarg >= t1)

Here is the call graph for this function:

Definition at line 316 of file datetimetester.py.

00316 
00317     def test_computations(self):
00318         eq = self.assertEqual
00319         td = timedelta
00320 
00321         a = td(7) # One week
00322         b = td(0, 60) # One minute
00323         c = td(0, 0, 1000) # One millisecond
00324         eq(a+b+c, td(7, 60, 1000))
00325         eq(a-b, td(6, 24*3600 - 60))
00326         eq(b.__rsub__(a), td(6, 24*3600 - 60))
00327         eq(-a, td(-7))
00328         eq(+a, td(7))
00329         eq(-b, td(-1, 24*3600 - 60))
00330         eq(-c, td(-1, 24*3600 - 1, 999000))
00331         eq(abs(a), a)
00332         eq(abs(-a), a)
00333         eq(td(6, 24*3600), a)
00334         eq(td(0, 0, 60*1000000), b)
00335         eq(a*10, td(70))
00336         eq(a*10, 10*a)
00337         eq(a*10, 10*a)
00338         eq(b*10, td(0, 600))
00339         eq(10*b, td(0, 600))
00340         eq(b*10, td(0, 600))
00341         eq(c*10, td(0, 0, 10000))
00342         eq(10*c, td(0, 0, 10000))
00343         eq(c*10, td(0, 0, 10000))
00344         eq(a*-1, -a)
00345         eq(b*-2, -b-b)
00346         eq(c*-2, -c+-c)
00347         eq(b*(60*24), (b*60)*24)
00348         eq(b*(60*24), (60*b)*24)
00349         eq(c*1000, td(0, 1))
00350         eq(1000*c, td(0, 1))
00351         eq(a//7, td(1))
00352         eq(b//10, td(0, 6))
00353         eq(c//1000, td(0, 0, 1))
00354         eq(a//10, td(0, 7*24*360))
00355         eq(a//3600000, td(0, 0, 7*24*1000))
00356         eq(a/0.5, td(14))
00357         eq(b/0.5, td(0, 120))
00358         eq(a/7, td(1))
00359         eq(b/10, td(0, 6))
00360         eq(c/1000, td(0, 0, 1))
00361         eq(a/10, td(0, 7*24*360))
00362         eq(a/3600000, td(0, 0, 7*24*1000))
00363 
00364         # Multiplication by float
00365         us = td(microseconds=1)
00366         eq((3*us) * 0.5, 2*us)
00367         eq((5*us) * 0.5, 2*us)
00368         eq(0.5 * (3*us), 2*us)
00369         eq(0.5 * (5*us), 2*us)
00370         eq((-3*us) * 0.5, -2*us)
00371         eq((-5*us) * 0.5, -2*us)
00372 
00373         # Division by int and float
00374         eq((3*us) / 2, 2*us)
00375         eq((5*us) / 2, 2*us)
00376         eq((-3*us) / 2.0, -2*us)
00377         eq((-5*us) / 2.0, -2*us)
00378         eq((3*us) / -2, -2*us)
00379         eq((5*us) / -2, -2*us)
00380         eq((3*us) / -2.0, -2*us)
00381         eq((5*us) / -2.0, -2*us)
00382         for i in range(-10, 10):
00383             eq((i*us/3)//us, round(i/3))
00384         for i in range(-10, 10):
00385             eq((i*us/-3)//us, round(i/-3))
00386 
00387         # Issue #11576
00388         eq(td(999999999, 86399, 999999) - td(999999999, 86399, 999998),
00389            td(0, 0, 1))
00390         eq(td(999999999, 1, 1) - td(999999999, 1, 0),
00391            td(0, 0, 1))

Here is the call graph for this function:

Definition at line 291 of file datetimetester.py.

00291 
00292     def test_constructor(self):
00293         eq = self.assertEqual
00294         td = timedelta
00295 
00296         # Check keyword args to constructor
00297         eq(td(), td(weeks=0, days=0, hours=0, minutes=0, seconds=0,
00298                     milliseconds=0, microseconds=0))
00299         eq(td(1), td(days=1))
00300         eq(td(0, 1), td(seconds=1))
00301         eq(td(0, 0, 1), td(microseconds=1))
00302         eq(td(weeks=1), td(days=7))
00303         eq(td(days=1), td(hours=24))
00304         eq(td(hours=1), td(minutes=60))
00305         eq(td(minutes=1), td(seconds=60))
00306         eq(td(seconds=1), td(milliseconds=1000))
00307         eq(td(milliseconds=1), td(microseconds=1000))
00308 
00309         # Check float args to constructor
00310         eq(td(weeks=1.0/7), td(days=1))
00311         eq(td(days=1.0/24), td(hours=1))
00312         eq(td(hours=1.0/60), td(minutes=1))
00313         eq(td(minutes=1.0/60), td(seconds=1))
00314         eq(td(seconds=0.001), td(milliseconds=1))
00315         eq(td(milliseconds=0.001), td(microseconds=1))

Here is the call graph for this function:

Definition at line 392 of file datetimetester.py.

00392 
00393     def test_disallowed_computations(self):
00394         a = timedelta(42)
00395 
00396         # Add/sub ints or floats should be illegal
00397         for i in 1, 1.0:
00398             self.assertRaises(TypeError, lambda: a+i)
00399             self.assertRaises(TypeError, lambda: a-i)
00400             self.assertRaises(TypeError, lambda: i+a)
00401             self.assertRaises(TypeError, lambda: i-a)
00402 
00403         # Division of int by timedelta doesn't make sense.
00404         # Division by zero doesn't make sense.
00405         zero = 0
00406         self.assertRaises(TypeError, lambda: zero // a)
00407         self.assertRaises(ZeroDivisionError, lambda: a // zero)
00408         self.assertRaises(ZeroDivisionError, lambda: a / zero)
00409         self.assertRaises(ZeroDivisionError, lambda: a / 0.0)
00410         self.assertRaises(TypeError, lambda: a / '')

Here is the call graph for this function:

Definition at line 412 of file datetimetester.py.

00412 
00413     def test_disallowed_special(self):
00414         a = timedelta(42)
00415         self.assertRaises(ValueError, a.__mul__, NAN)
00416         self.assertRaises(ValueError, a.__truediv__, NAN)

Here is the call graph for this function:

Definition at line 663 of file datetimetester.py.

00663 
00664     def test_division(self):
00665         t = timedelta(hours=1, minutes=24, seconds=19)
00666         second = timedelta(seconds=1)
00667         self.assertEqual(t / second, 5059.0)
00668         self.assertEqual(t // second, 5059)
00669 
00670         t = timedelta(minutes=2, seconds=30)
00671         minute = timedelta(minutes=1)
00672         self.assertEqual(t / minute, 2.5)
00673         self.assertEqual(t // minute, 2)
00674 
00675         zerotd = timedelta(0)
00676         self.assertRaises(ZeroDivisionError, truediv, t, zerotd)
00677         self.assertRaises(ZeroDivisionError, floordiv, t, zerotd)
00678 
00679         # self.assertRaises(TypeError, truediv, t, 2)
00680         # note: floor division of a timedelta by an integer *is*
00681         # currently permitted.

Here is the call graph for this function:

Definition at line 697 of file datetimetester.py.

00697 
00698     def test_divmod(self):
00699         t = timedelta(minutes=2, seconds=30)
00700         minute = timedelta(minutes=1)
00701         q, r = divmod(t, minute)
00702         self.assertEqual(q, 2)
00703         self.assertEqual(r, timedelta(seconds=30))
00704 
00705         t = timedelta(minutes=-2, seconds=30)
00706         q, r = divmod(t, minute)
00707         self.assertEqual(q, -2)
00708         self.assertEqual(r, timedelta(seconds=30))
00709 
00710         zerotd = timedelta(0)
00711         self.assertRaises(ZeroDivisionError, divmod, t, zerotd)
00712 
00713         self.assertRaises(TypeError, divmod, t, 10)
00714 

Here is the call graph for this function:

Definition at line 271 of file datetimetester.py.

00271 
00272     def test_harmful_mixed_comparison(self):
00273         me = self.theclass(1, 1, 1)
00274 
00275         self.assertRaises(TypeError, lambda: me < ())
00276         self.assertRaises(TypeError, lambda: me <= ())
00277         self.assertRaises(TypeError, lambda: me > ())
00278         self.assertRaises(TypeError, lambda: me >= ())
00279 
00280         self.assertRaises(TypeError, lambda: () < me)
00281         self.assertRaises(TypeError, lambda: () <= me)
00282         self.assertRaises(TypeError, lambda: () > me)
00283         self.assertRaises(TypeError, lambda: () >= me)

Here is the call graph for this function:

Definition at line 260 of file datetimetester.py.

00260 
00261     def test_harmless_mixed_comparison(self):
00262         me = self.theclass(1, 1, 1)
00263 
00264         self.assertFalse(me == ())
00265         self.assertTrue(me != ())
00266         self.assertFalse(() == me)
00267         self.assertTrue(() != me)
00268 
00269         self.assertIn(me, [1, 20, [], me])
00270         self.assertIn([], [me, 1, 20, []])

Here is the call graph for this function:

Definition at line 446 of file datetimetester.py.

00446 
00447     def test_hash_equality(self):
00448         t1 = timedelta(days=100,
00449                        weeks=-7,
00450                        hours=-24*(100-49),
00451                        minutes=-3,
00452                        seconds=12,
00453                        microseconds=(3*60 - 12) * 1000000)
00454         t2 = timedelta()
00455         self.assertEqual(hash(t1), hash(t2))
00456 
00457         t1 += timedelta(weeks=7)
00458         t2 += timedelta(days=7*7)
00459         self.assertEqual(t1, t2)
00460         self.assertEqual(hash(t1), hash(t2))
00461 
00462         d = {t1: 1}
00463         d[t2] = 2
00464         self.assertEqual(len(d), 1)
00465         self.assertEqual(d[t1], 2)

Here is the call graph for this function:

Definition at line 620 of file datetimetester.py.

00620 
00621     def test_massive_normalization(self):
00622         td = timedelta(microseconds=-1)
00623         self.assertEqual((td.days, td.seconds, td.microseconds),
00624                          (-1, 24*3600-1, 999999))

Here is the call graph for this function:

Definition at line 599 of file datetimetester.py.

00599 
00600     def test_microsecond_rounding(self):
00601         td = timedelta
00602         eq = self.assertEqual
00603 
00604         # Single-field rounding.
00605         eq(td(milliseconds=0.4/1000), td(0))    # rounds to 0
00606         eq(td(milliseconds=-0.4/1000), td(0))    # rounds to 0
00607         eq(td(milliseconds=0.6/1000), td(microseconds=1))
00608         eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
00609 
00610         # Rounding due to contributions from more than one field.
00611         us_per_hour = 3600e6
00612         us_per_day = us_per_hour * 24
00613         eq(td(days=.4/us_per_day), td(0))
00614         eq(td(hours=.2/us_per_hour), td(0))
00615         eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1))
00616 
00617         eq(td(days=-.4/us_per_day), td(0))
00618         eq(td(hours=-.2/us_per_hour), td(0))
00619         eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1))

Here is the call graph for this function:

Definition at line 571 of file datetimetester.py.

00571 
00572     def test_overflow(self):
00573         tiny = timedelta.resolution
00574 
00575         td = timedelta.min + tiny
00576         td -= tiny  # no problem
00577         self.assertRaises(OverflowError, td.__sub__, tiny)
00578         self.assertRaises(OverflowError, td.__add__, -tiny)
00579 
00580         td = timedelta.max - tiny
00581         td += tiny  # no problem
00582         self.assertRaises(OverflowError, td.__add__, tiny)
00583         self.assertRaises(OverflowError, td.__sub__, -tiny)
00584 
00585         self.assertRaises(OverflowError, lambda: -timedelta.max)
00586 
00587         day = timedelta(1)
00588         self.assertRaises(OverflowError, day.__mul__, 10**9)
00589         self.assertRaises(OverflowError, day.__mul__, 1e9)
00590         self.assertRaises(OverflowError, day.__truediv__, 1e-20)
00591         self.assertRaises(OverflowError, day.__truediv__, 1e-10)
00592         self.assertRaises(OverflowError, day.__truediv__, 9e-10)

Here is the call graph for this function:

Definition at line 466 of file datetimetester.py.

00466 
00467     def test_pickling(self):
00468         args = 12, 34, 56
00469         orig = timedelta(*args)
00470         for pickler, unpickler, proto in pickle_choices:
00471             green = pickler.dumps(orig, proto)
00472             derived = unpickler.loads(green)
00473             self.assertEqual(orig, derived)

Here is the call graph for this function:

Definition at line 682 of file datetimetester.py.

00682 
00683     def test_remainder(self):
00684         t = timedelta(minutes=2, seconds=30)
00685         minute = timedelta(minutes=1)
00686         r = t % minute
00687         self.assertEqual(r, timedelta(seconds=30))
00688 
00689         t = timedelta(minutes=-2, seconds=30)
00690         r = t %  minute
00691         self.assertEqual(r, timedelta(seconds=30))
00692 
00693         zerotd = timedelta(0)
00694         self.assertRaises(ZeroDivisionError, mod, t, zerotd)
00695 
00696         self.assertRaises(TypeError, mod, t, 10)

Here is the call graph for this function:

Definition at line 535 of file datetimetester.py.

00535 
00536     def test_repr(self):
00537         name = 'datetime.' + self.theclass.__name__
00538         self.assertEqual(repr(self.theclass(1)),
00539                          "%s(1)" % name)
00540         self.assertEqual(repr(self.theclass(10, 2)),
00541                          "%s(10, 2)" % name)
00542         self.assertEqual(repr(self.theclass(-10, 2, 400000)),
00543                          "%s(-10, 2, 400000)" % name)

Here is the call graph for this function:

Definition at line 562 of file datetimetester.py.

00562 
00563     def test_resolution_info(self):
00564         self.assertIsInstance(timedelta.min, timedelta)
00565         self.assertIsInstance(timedelta.max, timedelta)
00566         self.assertIsInstance(timedelta.resolution, timedelta)
00567         self.assertTrue(timedelta.max > timedelta.min)
00568         self.assertEqual(timedelta.min, timedelta(-999999999))
00569         self.assertEqual(timedelta.max, timedelta(999999999, 24*3600-1, 1e6-1))
00570         self.assertEqual(timedelta.resolution, timedelta(0, 0, 1))

Here is the call graph for this function:

Definition at line 544 of file datetimetester.py.

00544 
00545     def test_roundtrip(self):
00546         for td in (timedelta(days=999999999, hours=23, minutes=59,
00547                              seconds=59, microseconds=999999),
00548                    timedelta(days=-999999999),
00549                    timedelta(days=-999999999, seconds=1),
00550                    timedelta(days=1, seconds=2, microseconds=3)):
00551 
00552             # Verify td -> string -> td identity.
00553             s = repr(td)
00554             self.assertTrue(s.startswith('datetime.'))
00555             s = s[9:]
00556             td2 = eval(s)
00557             self.assertEqual(td, td2)
00558 
00559             # Verify identity via reconstructing from pieces.
00560             td2 = timedelta(td.days, td.seconds, td.microseconds)
00561             self.assertEqual(td, td2)

Here is the call graph for this function:

Definition at line 514 of file datetimetester.py.

00514 
00515     def test_str(self):
00516         td = timedelta
00517         eq = self.assertEqual
00518 
00519         eq(str(td(1)), "1 day, 0:00:00")
00520         eq(str(td(-1)), "-1 day, 0:00:00")
00521         eq(str(td(2)), "2 days, 0:00:00")
00522         eq(str(td(-2)), "-2 days, 0:00:00")
00523 
00524         eq(str(td(hours=12, minutes=58, seconds=59)), "12:58:59")
00525         eq(str(td(hours=2, minutes=3, seconds=4)), "2:03:04")
00526         eq(str(td(weeks=-30, hours=23, minutes=12, seconds=34)),
00527            "-210 days, 23:12:34")
00528 
00529         eq(str(td(milliseconds=1)), "0:00:00.001000")
00530         eq(str(td(microseconds=3)), "0:00:00.000003")
00531 
00532         eq(str(td(days=999999999, hours=23, minutes=59, seconds=59,
00533                    microseconds=999999)),
00534            "999999999 days, 23:59:59.999999")

Here is the call graph for this function:

Definition at line 632 of file datetimetester.py.

00632 
00633     def test_subclass_timedelta(self):
00634 
00635         class T(timedelta):
00636             @staticmethod
00637             def from_td(td):
00638                 return T(td.days, td.seconds, td.microseconds)
00639 
00640             def as_hours(self):
00641                 sum = (self.days * 24 +
00642                        self.seconds / 3600.0 +
00643                        self.microseconds / 3600e6)
00644                 return round(sum)
00645 
00646         t1 = T(days=1)
00647         self.assertTrue(type(t1) is T)
00648         self.assertEqual(t1.as_hours(), 24)
00649 
00650         t2 = T(days=-1, seconds=-3600)
00651         self.assertTrue(type(t2) is T)
00652         self.assertEqual(t2.as_hours(), -25)
00653 
00654         t3 = t1 + t2
00655         self.assertTrue(type(t3) is timedelta)
00656         t4 = T.from_td(t3)
00657         self.assertTrue(type(t4) is T)
00658         self.assertEqual(t3.days, t4.days)
00659         self.assertEqual(t3.seconds, t4.seconds)
00660         self.assertEqual(t3.microseconds, t4.microseconds)
00661         self.assertEqual(str(t3), str(t4))
00662         self.assertEqual(t4.as_hours(), -1)

Here is the call graph for this function:

Definition at line 424 of file datetimetester.py.

00424 
00425     def test_total_seconds(self):
00426         td = timedelta(days=365)
00427         self.assertEqual(td.total_seconds(), 31536000.0)
00428         for total_seconds in [123456.789012, -123456.789012, 0.123456, 0, 1e6]:
00429             td = timedelta(seconds=total_seconds)
00430             self.assertEqual(td.total_seconds(), total_seconds)
00431         # Issue8644: Test that td.total_seconds() has the same
00432         # accuracy as td / timedelta(seconds=1).
00433         for ms in [-1, -2, -123]:
00434             td = timedelta(microseconds=ms)
00435             self.assertEqual(td.total_seconds(), td / timedelta(seconds=1))

Here is the call graph for this function:


Member Data Documentation

Definition at line 289 of file datetimetester.py.


The documentation for this class was generated from the following file: