MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_computations

Method test_computations

Lib/test/datetimetester.py:550–631  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

548 ra(TypeError, lambda: td(microseconds='1'))
549
550 def test_computations(self):
551 eq = self.assertEqual
552 td = timedelta
553
554 a = td(7) # One week
555 b = td(0, 60) # One minute
556 c = td(0, 0, 1000) # One millisecond
557 eq(a+b+c, td(7, 60, 1000))
558 eq(a-b, td(6, 24*3600 - 60))
559 eq(b.__rsub__(a), td(6, 24*3600 - 60))
560 eq(-a, td(-7))
561 eq(+a, td(7))
562 eq(-b, td(-1, 24*3600 - 60))
563 eq(-c, td(-1, 24*3600 - 1, 999000))
564 eq(abs(a), a)
565 eq(abs(-a), a)
566 eq(td(6, 24*3600), a)
567 eq(td(0, 0, 60*1000000), b)
568 eq(a*10, td(70))
569 eq(a*10, 10*a)
570 eq(a*10, 10*a)
571 eq(b*10, td(0, 600))
572 eq(10*b, td(0, 600))
573 eq(b*10, td(0, 600))
574 eq(c*10, td(0, 0, 10000))
575 eq(10*c, td(0, 0, 10000))
576 eq(c*10, td(0, 0, 10000))
577 eq(a*-1, -a)
578 eq(b*-2, -b-b)
579 eq(c*-2, -c+-c)
580 eq(b*(60*24), (b*60)*24)
581 eq(b*(60*24), (60*b)*24)
582 eq(c*1000, td(0, 1))
583 eq(1000*c, td(0, 1))
584 eq(a//7, td(1))
585 eq(b//10, td(0, 6))
586 eq(c//1000, td(0, 0, 1))
587 eq(a//10, td(0, 7*24*360))
588 eq(a//3600000, td(0, 0, 7*24*1000))
589 eq(a/0.5, td(14))
590 eq(b/0.5, td(0, 120))
591 eq(a/7, td(1))
592 eq(b/10, td(0, 6))
593 eq(c/1000, td(0, 0, 1))
594 eq(a/10, td(0, 7*24*360))
595 eq(a/3600000, td(0, 0, 7*24*1000))
596
597 # Multiplication by float
598 us = td(microseconds=1)
599 eq((3*us) * 0.5, 2*us)
600 eq((5*us) * 0.5, 2*us)
601 eq(0.5 * (3*us), 2*us)
602 eq(0.5 * (5*us), 2*us)
603 eq((-3*us) * 0.5, -2*us)
604 eq((-5*us) * 0.5, -2*us)
605
606 # Issue #23521
607 eq(td(seconds=1) * 0.123456, td(microseconds=123456))

Callers

nothing calls this directly

Calls 4

eqFunction · 0.90
roundFunction · 0.85
absFunction · 0.50
__rsub__Method · 0.45

Tested by

no test coverage detected