summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_time.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-30 01:52:49 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-30 01:52:49 (GMT)
commitbcdd777d3c01a6db3b4357922663624ef617e65a (patch)
tree89c64b79fd8d7d515bcece608063019cfad79bfd /Lib/test/test_time.py
parentc4bb599be091ae92cfbe47e82a6d18f39c11443e (diff)
downloadcpython-bcdd777d3c01a6db3b4357922663624ef617e65a.zip
cpython-bcdd777d3c01a6db3b4357922663624ef617e65a.tar.gz
cpython-bcdd777d3c01a6db3b4357922663624ef617e65a.tar.bz2
Issue #22117: Add _PyTime_ROUND_CEILING rounding method for timestamps
Add also more tests for ROUNd_FLOOR.
Diffstat (limited to 'Lib/test/test_time.py')
-rw-r--r--Lib/test/test_time.py65
1 files changed, 61 insertions, 4 deletions
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index bcbf41d..472110c 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -28,13 +28,16 @@ class _PyTime(enum.IntEnum):
ROUND_DOWN = 0
# Round away from zero
ROUND_UP = 1
- # Round towards -Infinity
+ # Round towards minus infinity (-inf)
ROUND_FLOOR = 2
+ # Round towards infinity (+inf)
+ ROUND_CEILING = 3
ALL_ROUNDING_METHODS = (
_PyTime.ROUND_UP,
_PyTime.ROUND_DOWN,
- _PyTime.ROUND_FLOOR)
+ _PyTime.ROUND_FLOOR,
+ _PyTime.ROUND_CEILING)
class TimeTestCase(unittest.TestCase):
@@ -621,6 +624,13 @@ class TestPytime(unittest.TestCase):
(-1.9, -1, _PyTime.ROUND_DOWN),
(1.0, 1, _PyTime.ROUND_DOWN),
(1.9, 1, _PyTime.ROUND_DOWN),
+ # Round towards minus infinity (-inf)
+ (0, 0, _PyTime.ROUND_FLOOR),
+ (-1, -1, _PyTime.ROUND_FLOOR),
+ (-1.0, -1, _PyTime.ROUND_FLOOR),
+ (-1.9, -2, _PyTime.ROUND_FLOOR),
+ (1.0, 1, _PyTime.ROUND_FLOOR),
+ (1.9, 1, _PyTime.ROUND_FLOOR),
# Round away from zero
(0, 0, _PyTime.ROUND_UP),
(-1, -1, _PyTime.ROUND_UP),
@@ -628,10 +638,17 @@ class TestPytime(unittest.TestCase):
(-1.9, -2, _PyTime.ROUND_UP),
(1.0, 1, _PyTime.ROUND_UP),
(1.9, 2, _PyTime.ROUND_UP),
+ # Round towards infinity (+inf)
+ (0, 0, _PyTime.ROUND_CEILING),
+ (-1, -1, _PyTime.ROUND_CEILING),
+ (-1.0, -1, _PyTime.ROUND_CEILING),
+ (-1.9, -1, _PyTime.ROUND_CEILING),
+ (1.0, 1, _PyTime.ROUND_CEILING),
+ (1.9, 2, _PyTime.ROUND_CEILING),
):
self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
- rnd = _PyTime.ROUND_DOWN
+ rnd = _PyTime.ROUND_FLOOR
for invalid in self.invalid_values:
self.assertRaises(OverflowError,
pytime_object_to_time_t, invalid, rnd)
@@ -654,6 +671,20 @@ class TestPytime(unittest.TestCase):
(1.1234567899, (1, 123456789), _PyTime.ROUND_DOWN),
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_DOWN),
(-1.1234567891, (-2, 876543211), _PyTime.ROUND_DOWN),
+ # Round towards minus infinity (-inf)
+ (0, (0, 0), _PyTime.ROUND_FLOOR),
+ (-1, (-1, 0), _PyTime.ROUND_FLOOR),
+ (-1.0, (-1, 0), _PyTime.ROUND_FLOOR),
+ (1e-9, (0, 1), _PyTime.ROUND_FLOOR),
+ (1e-10, (0, 0), _PyTime.ROUND_FLOOR),
+ (-1e-9, (-1, 999999999), _PyTime.ROUND_FLOOR),
+ (-1e-10, (-1, 999999999), _PyTime.ROUND_FLOOR),
+ (-1.2, (-2, 800000000), _PyTime.ROUND_FLOOR),
+ (0.9999999999, (0, 999999999), _PyTime.ROUND_FLOOR),
+ (1.1234567890, (1, 123456789), _PyTime.ROUND_FLOOR),
+ (1.1234567899, (1, 123456789), _PyTime.ROUND_FLOOR),
+ (-1.1234567890, (-2, 876543211), _PyTime.ROUND_FLOOR),
+ (-1.1234567891, (-2, 876543210), _PyTime.ROUND_FLOOR),
# Round away from zero
(0, (0, 0), _PyTime.ROUND_UP),
(-1, (-1, 0), _PyTime.ROUND_UP),
@@ -668,11 +699,25 @@ class TestPytime(unittest.TestCase):
(1.1234567899, (1, 123456790), _PyTime.ROUND_UP),
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_UP),
(-1.1234567891, (-2, 876543210), _PyTime.ROUND_UP),
+ # Round towards infinity (+inf)
+ (0, (0, 0), _PyTime.ROUND_CEILING),
+ (-1, (-1, 0), _PyTime.ROUND_CEILING),
+ (-1.0, (-1, 0), _PyTime.ROUND_CEILING),
+ (1e-9, (0, 1), _PyTime.ROUND_CEILING),
+ (1e-10, (0, 1), _PyTime.ROUND_CEILING),
+ (-1e-9, (-1, 999999999), _PyTime.ROUND_CEILING),
+ (-1e-10, (0, 0), _PyTime.ROUND_CEILING),
+ (-1.2, (-2, 800000000), _PyTime.ROUND_CEILING),
+ (0.9999999999, (1, 0), _PyTime.ROUND_CEILING),
+ (1.1234567890, (1, 123456790), _PyTime.ROUND_CEILING),
+ (1.1234567899, (1, 123456790), _PyTime.ROUND_CEILING),
+ (-1.1234567890, (-2, 876543211), _PyTime.ROUND_CEILING),
+ (-1.1234567891, (-2, 876543211), _PyTime.ROUND_CEILING),
):
with self.subTest(obj=obj, round=rnd, timespec=timespec):
self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
- rnd = _PyTime.ROUND_DOWN
+ rnd = _PyTime.ROUND_FLOOR
for invalid in self.invalid_values:
self.assertRaises(OverflowError,
pytime_object_to_timespec, invalid, rnd)
@@ -794,27 +839,34 @@ class TestPyTime_t(unittest.TestCase):
UP = _PyTime.ROUND_UP
DOWN = _PyTime.ROUND_DOWN
FLOOR = _PyTime.ROUND_FLOOR
+ CEILING = _PyTime.ROUND_CEILING
for obj, ts, rnd in (
# close to zero
+ ( 1e-10, 1, CEILING),
( 1e-10, 1, UP),
( 1e-10, 0, DOWN),
( 1e-10, 0, FLOOR),
+ (-1e-10, 0, CEILING),
(-1e-10, 0, DOWN),
(-1e-10, -1, UP),
(-1e-10, -1, FLOOR),
# test rounding of the last nanosecond
+ ( 1.1234567899, 1123456790, CEILING),
( 1.1234567899, 1123456790, UP),
( 1.1234567899, 1123456789, DOWN),
( 1.1234567899, 1123456789, FLOOR),
+ (-1.1234567899, -1123456789, CEILING),
(-1.1234567899, -1123456789, DOWN),
(-1.1234567899, -1123456790, UP),
(-1.1234567899, -1123456790, FLOOR),
# close to 1 second
+ ( 0.9999999999, 1000000000, CEILING),
( 0.9999999999, 1000000000, UP),
( 0.9999999999, 999999999, DOWN),
( 0.9999999999, 999999999, FLOOR),
+ (-0.9999999999, -999999999, CEILING),
(-0.9999999999, -999999999, DOWN),
(-0.9999999999, -1000000000, UP),
(-0.9999999999, -1000000000, FLOOR),
@@ -890,19 +942,24 @@ class TestPyTime_t(unittest.TestCase):
UP = _PyTime.ROUND_UP
DOWN = _PyTime.ROUND_DOWN
FLOOR = _PyTime.ROUND_FLOOR
+ CEILING = _PyTime.ROUND_CEILING
for ns, tv, rnd in (
# nanoseconds
+ (1, (0, 1), CEILING),
(1, (0, 1), UP),
(1, (0, 0), DOWN),
(1, (0, 0), FLOOR),
+ (-1, (0, 0), CEILING),
(-1, (0, 0), DOWN),
(-1, (-1, 999999), UP),
(-1, (-1, 999999), FLOOR),
# seconds + nanoseconds
+ (1234567001, (1, 234568), CEILING),
(1234567001, (1, 234568), UP),
(1234567001, (1, 234567), DOWN),
(1234567001, (1, 234567), FLOOR),
+ (-1234567001, (-2, 765433), CEILING),
(-1234567001, (-2, 765433), DOWN),
(-1234567001, (-2, 765432), UP),
(-1234567001, (-2, 765432), FLOOR),