summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_time.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-28 00:26:47 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-28 00:26:47 (GMT)
commit95e9cef6f023a1cf365f2f02775badb3a6ac0d82 (patch)
tree721cb2ab9fbba707888bc7e90b49758ffd5156ed /Lib/test/test_time.py
parentb7df3144ef14ec50650dfd47da4ba09ee0bc674c (diff)
downloadcpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.zip
cpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.tar.gz
cpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.tar.bz2
Issue #22117: Write unit tests for _PyTime_AsTimeval()
* _PyTime_AsTimeval() now ensures that tv_usec is always positive * _PyTime_AsTimespec() now ensures that tv_nsec is always positive * _PyTime_AsTimeval() now returns an integer on overflow instead of raising an exception
Diffstat (limited to 'Lib/test/test_time.py')
-rw-r--r--Lib/test/test_time.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index cfec329..78314a7 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -902,6 +902,44 @@ class TestPyTime_t(unittest.TestCase):
self.assertEqual(PyTime_AsSecondsDouble(nanoseconds),
seconds)
+ def test_timeval(self):
+ from _testcapi import PyTime_AsTimeval
+ for rnd in ALL_ROUNDING_METHODS:
+ for ns, tv in (
+ # microseconds
+ (0, (0, 0)),
+ (1000, (0, 1)),
+ (-1000, (-1, 999999)),
+
+ # seconds
+ (2 * SEC_TO_NS, (2, 0)),
+ (-3 * SEC_TO_NS, (-3, 0)),
+
+ # seconds + nanoseconds
+ (1234567000, (1, 234567)),
+ (-1234567000, (-2, 765433)),
+ ):
+ with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
+ self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
+
+ UP = _PyTime.ROUND_UP
+ DOWN = _PyTime.ROUND_DOWN
+ for ns, tv, rnd in (
+ # nanoseconds
+ (1, (0, 1), UP),
+ (1, (0, 0), DOWN),
+ (-1, (0, 0), DOWN),
+ (-1, (-1, 999999), UP),
+
+ # seconds + nanoseconds
+ (1234567001, (1, 234568), UP),
+ (1234567001, (1, 234567), DOWN),
+ (-1234567001, (-2, 765433), DOWN),
+ (-1234567001, (-2, 765432), UP),
+ ):
+ with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
+ self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
+
@unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'),
'need _testcapi.PyTime_AsTimespec')
def test_timespec(self):