Kaydet (Commit) acea9f62 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #23517: Reintroduce unit tests for the old PyTime API since it's still

used.
üst 67edcc90
......@@ -727,6 +727,9 @@ class TestPytime(unittest.TestCase):
@unittest.skipUnless(_testcapi is not None,
'need the _testcapi module')
class TestPyTime_t(unittest.TestCase):
"""
Test the _PyTime_t API.
"""
def test_FromSeconds(self):
from _testcapi import PyTime_FromSeconds
for seconds in (0, 3, -456, _testcapi.INT_MAX, _testcapi.INT_MIN):
......@@ -1041,5 +1044,156 @@ class TestPyTime_t(unittest.TestCase):
self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms)
@unittest.skipUnless(_testcapi is not None,
'need the _testcapi module')
class TestOldPyTime(unittest.TestCase):
"""
Test the old _PyTime_t API: _PyTime_ObjectToXXX() functions.
"""
def setUp(self):
self.invalid_values = (
-(2 ** 100), 2 ** 100,
-(2.0 ** 100.0), 2.0 ** 100.0,
)
@support.cpython_only
def test_time_t(self):
from _testcapi import pytime_object_to_time_t
# Conversion giving the same result for all rounding methods
for rnd in ALL_ROUNDING_METHODS:
for obj, time_t in (
# int
(0, 0),
(-1, -1),
# float
(1.0, 1),
(-1.0, -1),
):
self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
# Conversion giving different results depending on the rounding method
FLOOR = _PyTime.ROUND_FLOOR
CEILING = _PyTime.ROUND_CEILING
HALF_UP = _PyTime.ROUND_HALF_UP
for obj, time_t, rnd in (
(-1.9, -2, FLOOR),
(-1.9, -2, HALF_UP),
(-1.9, -1, CEILING),
(1.9, 1, FLOOR),
(1.9, 2, HALF_UP),
(1.9, 2, CEILING),
):
self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
# Test OverflowError
rnd = _PyTime.ROUND_FLOOR
for invalid in self.invalid_values:
self.assertRaises(OverflowError,
pytime_object_to_time_t, invalid, rnd)
def test_timeval(self):
from _testcapi import pytime_object_to_timeval
# Conversion giving the same result for all rounding methods
for rnd in ALL_ROUNDING_METHODS:
for obj, timeval in (
# int
(0, (0, 0)),
(-1, (-1, 0)),
# float
(-1.0, (-1, 0)),
(-1.2, (-2, 800000)),
(-1e-6, (-1, 999999)),
(1e-6, (0, 1)),
):
with self.subTest(obj=obj, round=rnd, timeval=timeval):
self.assertEqual(pytime_object_to_timeval(obj, rnd),
timeval)
# Conversion giving different results depending on the rounding method
FLOOR = _PyTime.ROUND_FLOOR
CEILING = _PyTime.ROUND_CEILING
HALF_UP = _PyTime.ROUND_HALF_UP
for obj, timeval, rnd in (
(-1e-7, (-1, 999999), FLOOR),
(-1e-7, (0, 0), CEILING),
(-1e-7, (0, 0), HALF_UP),
(1e-7, (0, 0), FLOOR),
(1e-7, (0, 1), CEILING),
(1e-7, (0, 0), HALF_UP),
(0.4e-6, (0, 0), HALF_UP),
(0.5e-6, (0, 1), HALF_UP),
(0.6e-6, (0, 1), HALF_UP),
(0.9999999, (0, 999999), FLOOR),
(0.9999999, (1, 0), CEILING),
(0.9999999, (1, 0), HALF_UP),
):
with self.subTest(obj=obj, round=rnd, timeval=timeval):
self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)
rnd = _PyTime.ROUND_FLOOR
for invalid in self.invalid_values:
self.assertRaises(OverflowError,
pytime_object_to_timeval, invalid, rnd)
@support.cpython_only
def test_timespec(self):
from _testcapi import pytime_object_to_timespec
# Conversion giving the same result for all rounding methods
for rnd in ALL_ROUNDING_METHODS:
for obj, timespec in (
# int
(0, (0, 0)),
(-1, (-1, 0)),
# float
(-1.0, (-1, 0)),
(-1e-9, (-1, 999999999)),
(1e-9, (0, 1)),
(-1.2, (-2, 800000000)),
):
with self.subTest(obj=obj, round=rnd, timespec=timespec):
self.assertEqual(pytime_object_to_timespec(obj, rnd),
timespec)
# Conversion giving different results depending on the rounding method
FLOOR = _PyTime.ROUND_FLOOR
CEILING = _PyTime.ROUND_CEILING
HALF_UP = _PyTime.ROUND_HALF_UP
for obj, timespec, rnd in (
(-1e-10, (-1, 999999999), FLOOR),
(-1e-10, (0, 0), CEILING),
(-1e-10, (0, 0), HALF_UP),
(1e-10, (0, 0), FLOOR),
(1e-10, (0, 1), CEILING),
(1e-10, (0, 0), HALF_UP),
(0.4e-9, (0, 0), HALF_UP),
(0.5e-9, (0, 1), HALF_UP),
(0.6e-9, (0, 1), HALF_UP),
(0.9999999999, (0, 999999999), FLOOR),
(0.9999999999, (1, 0), CEILING),
(0.9999999999, (1, 0), HALF_UP),
):
with self.subTest(obj=obj, round=rnd, timespec=timespec):
self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
# Test OverflowError
rnd = FLOOR
for invalid in self.invalid_values:
self.assertRaises(OverflowError,
pytime_object_to_timespec, invalid, rnd)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment