summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_time.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-04-01 15:47:07 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-04-01 15:47:07 (GMT)
commit62d1c70eff9498446d2ce3c564fefee7a7e54770 (patch)
treea0a7b3f54ed7baa339dfaaeefc048101d727c63c /Lib/test/test_time.py
parent067bbba7a49aa8bf455465eb5ee75d561cfb1556 (diff)
downloadcpython-62d1c70eff9498446d2ce3c564fefee7a7e54770.zip
cpython-62d1c70eff9498446d2ce3c564fefee7a7e54770.tar.gz
cpython-62d1c70eff9498446d2ce3c564fefee7a7e54770.tar.bz2
Issue #22117, issue #23485: Fix _PyTime_AsMilliseconds() and
_PyTime_AsMicroseconds() rounding. Add also unit tests.
Diffstat (limited to 'Lib/test/test_time.py')
-rw-r--r--Lib/test/test_time.py76
1 files changed, 72 insertions, 4 deletions
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index 4747cc6..a9f6fd8 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -21,6 +21,8 @@ SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
TIME_MINYEAR = -TIME_MAXYEAR - 1
+US_TO_NS = 10 ** 3
+MS_TO_NS = 10 ** 6
SEC_TO_NS = 10 ** 9
class _PyTime(enum.IntEnum):
@@ -867,10 +869,6 @@ class TestPyTime_t(unittest.TestCase):
# 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)
@@ -914,6 +912,76 @@ class TestPyTime_t(unittest.TestCase):
with self.subTest(nanoseconds=ns, timespec=ts):
self.assertEqual(PyTime_AsTimespec(ns), ts)
+ def test_milliseconds(self):
+ from _testcapi import PyTime_AsMilliseconds
+ for rnd in ALL_ROUNDING_METHODS:
+ for ns, tv in (
+ # milliseconds
+ (1 * MS_TO_NS, 1),
+ (-2 * MS_TO_NS, -2),
+
+ # seconds
+ (2 * SEC_TO_NS, 2000),
+ (-3 * SEC_TO_NS, -3000),
+ ):
+ with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
+ self.assertEqual(PyTime_AsMilliseconds(ns, rnd), tv)
+
+ FLOOR = _PyTime.ROUND_FLOOR
+ CEILING = _PyTime.ROUND_CEILING
+ for ns, ms, rnd in (
+ # nanoseconds
+ (1, 0, FLOOR),
+ (1, 1, CEILING),
+ (-1, 0, FLOOR),
+ (-1, -1, CEILING),
+
+ # seconds + nanoseconds
+ (1234 * MS_TO_NS + 1, 1234, FLOOR),
+ (1234 * MS_TO_NS + 1, 1235, CEILING),
+ (-1234 * MS_TO_NS - 1, -1234, FLOOR),
+ (-1234 * MS_TO_NS - 1, -1235, CEILING),
+ ):
+ with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
+ self.assertEqual(PyTime_AsMilliseconds(ns, rnd), ms)
+
+ def test_microseconds(self):
+ from _testcapi import PyTime_AsMicroseconds
+ for rnd in ALL_ROUNDING_METHODS:
+ for ns, tv in (
+ # microseconds
+ (1 * US_TO_NS, 1),
+ (-2 * US_TO_NS, -2),
+
+ # milliseconds
+ (1 * MS_TO_NS, 1000),
+ (-2 * MS_TO_NS, -2000),
+
+ # seconds
+ (2 * SEC_TO_NS, 2000000),
+ (-3 * SEC_TO_NS, -3000000),
+ ):
+ with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
+ self.assertEqual(PyTime_AsMicroseconds(ns, rnd), tv)
+
+ FLOOR = _PyTime.ROUND_FLOOR
+ CEILING = _PyTime.ROUND_CEILING
+ for ns, ms, rnd in (
+ # nanoseconds
+ (1, 0, FLOOR),
+ (1, 1, CEILING),
+ (-1, 0, FLOOR),
+ (-1, -1, CEILING),
+
+ # seconds + nanoseconds
+ (1234 * US_TO_NS + 1, 1234, FLOOR),
+ (1234 * US_TO_NS + 1, 1235, CEILING),
+ (-1234 * US_TO_NS - 1, -1234, FLOOR),
+ (-1234 * US_TO_NS - 1, -1235, CEILING),
+ ):
+ with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
+ self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms)
+
if __name__ == "__main__":
unittest.main()