Esempio n. 1
0
    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**9, (-1, 998046875)),
            ):
                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.9999999999, (0, 999999999), FLOOR),
            (0.9999999999, (1, 0), CEILING),
            (0.9999999999, (1, 0), HALF_UP),

            (-0.6e-9, (-1, 999999999), HALF_UP),
            # skipped, 0.5e-6 is inexact in base 2
            #(-0.5e-9, (-1, 999999999), HALF_UP),
            (-0.4e-9, (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),
        ):
            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)
Esempio n. 2
0
    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.9999999999, (0, 999999999), FLOOR),
            (0.9999999999, (1, 0), CEILING),
            (0.9999999999, (1, 0), HALF_UP),
            (-0.6e-9, (-1, 999999999), HALF_UP),
                # skipped, 0.5e-6 is inexact in base 2
                #(-0.5e-9, (-1, 999999999), HALF_UP),
            (-0.4e-9, (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),
        ):
            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)
Esempio n. 3
0
    def test_object_to_timespec(self):
        from _testcapi import pytime_object_to_timespec

        self.check_int_rounding(pytime_object_to_timespec,
                                lambda secs: (secs, 0),
                                value_filter=self.time_t_filter)

        self.check_float_rounding(pytime_object_to_timespec,
                                  self.create_converter(SEC_TO_NS),
                                  value_filter=self.time_t_filter)

        # test nan
        for time_rnd, _ in ROUNDING_MODES:
            with self.assertRaises(ValueError):
                pytime_object_to_timespec(float('nan'), time_rnd)
Esempio n. 4
0
    def test_object_to_timespec(self):
        from _testcapi import pytime_object_to_timespec

        self.check_int_rounding(pytime_object_to_timespec,
                                lambda secs: (secs, 0),
                                value_filter=self.time_t_filter)

        self.check_float_rounding(pytime_object_to_timespec,
                                  self.create_converter(SEC_TO_NS),
                                  value_filter=self.time_t_filter)

        # test nan
        for time_rnd, _ in ROUNDING_MODES:
            with self.assertRaises(ValueError):
                pytime_object_to_timespec(float('nan'), time_rnd)
Esempio n. 5
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec in (
            (0, (0, 0)),
            (-1, (-1, 0)),
            (-1.0, (-1, 0)),
            (1e-9, (0, 1)),
            (-1e-9, (-1, 999999999)),
            (-1.2, (-2, 800000000)),
            (1.1234567890, (1, 123456789)),
            (1.1234567899, (1, 123456789)),
            (-1.1234567890, (-2, 876543211)),
            (-1.1234567891, (-2, 876543210)),
        ):
            self.assertEqual(pytime_object_to_timespec(obj), timespec)

        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timespec, invalid)
Esempio n. 6
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec in (
            (0, (0, 0)),
            (-1, (-1, 0)),
            (-1.0, (-1, 0)),
            (1e-9, (0, 1)),
            (-1e-9, (-1, 999999999)),
            (-1.2, (-2, 800000000)),
            (1.1234567890, (1, 123456789)),
            (1.1234567899, (1, 123456789)),
            (-1.1234567890, (-2, 876543211)),
            (-1.1234567891, (-2, 876543210)),
        ):
            self.assertEqual(pytime_object_to_timespec(obj), timespec)

        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timespec, invalid)
Esempio n. 7
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec, rnd in (
                # 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 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_FLOOR
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timespec,
                              invalid, rnd)
Esempio n. 8
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec, rnd in (
            # Round towards zero
            (0, (0, 0), _PyTime_ROUND_DOWN),
            (-1, (-1, 0), _PyTime_ROUND_DOWN),
            (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
            (1e-9, (0, 1), _PyTime_ROUND_DOWN),
            (1e-10, (0, 0), _PyTime_ROUND_DOWN),
            (-1e-9, (-1, 999999999), _PyTime_ROUND_DOWN),
            (-1e-10, (-1, 999999999), _PyTime_ROUND_DOWN),
            (-1.2, (-2, 800000000), _PyTime_ROUND_DOWN),
            (0.9999999999, (0, 999999999), _PyTime_ROUND_DOWN),
            (1.1234567890, (1, 123456789), _PyTime_ROUND_DOWN),
            (1.1234567899, (1, 123456789), _PyTime_ROUND_DOWN),
            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_DOWN),
            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_DOWN),
            # Round away from zero
            (0, (0, 0), _PyTime_ROUND_UP),
            (-1, (-1, 0), _PyTime_ROUND_UP),
            (-1.0, (-1, 0), _PyTime_ROUND_UP),
            (1e-9, (0, 1), _PyTime_ROUND_UP),
            (1e-10, (0, 1), _PyTime_ROUND_UP),
            (-1e-9, (-1, 999999999), _PyTime_ROUND_UP),
            (-1e-10, (-1, 999999999), _PyTime_ROUND_UP),
            (-1.2, (-2, 800000000), _PyTime_ROUND_UP),
            (0.9999999999, (1, 0), _PyTime_ROUND_UP),
            (1.1234567890, (1, 123456790), _PyTime_ROUND_UP),
            (1.1234567899, (1, 123456790), _PyTime_ROUND_UP),
            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_UP),
            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_UP),
        ):
            with self.subTest(obj=obj, round=rnd, timespec=timespec):
                self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)

        rnd = _PyTime_ROUND_DOWN
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError,
                              pytime_object_to_timespec, invalid, rnd)
Esempio n. 9
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec, rnd in (
            # 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 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_FLOOR
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError,
                              pytime_object_to_timespec, invalid, rnd)
Esempio n. 10
0
    def test_timespec(self):
        from _testcapi import pytime_object_to_timespec
        for obj, timespec, rnd in (
                # Round towards zero
            (0, (0, 0), _PyTime_ROUND_DOWN),
            (-1, (-1, 0), _PyTime_ROUND_DOWN),
            (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
            (1e-9, (0, 1), _PyTime_ROUND_DOWN),
            (1e-10, (0, 0), _PyTime_ROUND_DOWN),
            (-1e-9, (-1, 999999999), _PyTime_ROUND_DOWN),
            (-1e-10, (-1, 999999999), _PyTime_ROUND_DOWN),
            (-1.2, (-2, 800000000), _PyTime_ROUND_DOWN),
            (0.9999999999, (0, 999999999), _PyTime_ROUND_DOWN),
            (1.1234567890, (1, 123456789), _PyTime_ROUND_DOWN),
            (1.1234567899, (1, 123456789), _PyTime_ROUND_DOWN),
            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_DOWN),
            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_DOWN),
                # Round away from zero
            (0, (0, 0), _PyTime_ROUND_UP),
            (-1, (-1, 0), _PyTime_ROUND_UP),
            (-1.0, (-1, 0), _PyTime_ROUND_UP),
            (1e-9, (0, 1), _PyTime_ROUND_UP),
            (1e-10, (0, 1), _PyTime_ROUND_UP),
            (-1e-9, (-1, 999999999), _PyTime_ROUND_UP),
            (-1e-10, (-1, 999999999), _PyTime_ROUND_UP),
            (-1.2, (-2, 800000000), _PyTime_ROUND_UP),
            (0.9999999999, (1, 0), _PyTime_ROUND_UP),
            (1.1234567890, (1, 123456790), _PyTime_ROUND_UP),
            (1.1234567899, (1, 123456790), _PyTime_ROUND_UP),
            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_UP),
            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_UP),
        ):
            with self.subTest(obj=obj, round=rnd, timespec=timespec):
                self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)

        rnd = _PyTime_ROUND_DOWN
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timespec,
                              invalid, rnd)
Esempio n. 11
0
    def test_object_to_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/2**7, (-1, 992187500)),
                (-1.0, (-1, 0)),
                (-1e-9, (-1, 999999999)),
                (1e-9, (0, 1)),
                (1.0, (1, 0)),
            ):
                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_EVEN =  _PyTime.ROUND_HALF_EVEN
        for obj, timespec, rnd in (
            # Round towards minus infinity (-inf)
            (-1e-10, (0, 0), CEILING),
            (-1e-10, (-1, 999999999), FLOOR),
            (-1e-10, (0, 0), HALF_EVEN),
            (1e-10, (0, 0), FLOOR),
            (1e-10, (0, 1), CEILING),
            (1e-10, (0, 0), HALF_EVEN),

            (0.9999999999, (0, 999999999), FLOOR),
            (0.9999999999, (1, 0), CEILING),

            (1.1234567890, (1, 123456789), FLOOR),
            (1.1234567899, (1, 123456789), FLOOR),
            (-1.1234567890, (-2, 876543210), FLOOR),
            (-1.1234567891, (-2, 876543210), FLOOR),
            # Round towards infinity (+inf)
            (1.1234567890, (1, 123456790), CEILING),
            (1.1234567899, (1, 123456790), CEILING),
            (-1.1234567890, (-2, 876543211), CEILING),
            (-1.1234567891, (-2, 876543211), CEILING),

            # half even
            (-1.5e-9, (-1, 999999998), HALF_EVEN),
            (-0.9e-9, (-1, 999999999), HALF_EVEN),
            (-0.5e-9, (0, 0), HALF_EVEN),
            (0.5e-9, (0, 0), HALF_EVEN),
            (0.9e-9, (0, 1), HALF_EVEN),
            (1.5e-9, (0, 2), HALF_EVEN),
        ):
            with self.subTest(obj=obj, round=rnd, timespec=timespec):
                self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)

        # Test OverflowError
        rnd = _PyTime.ROUND_FLOOR
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError,
                              pytime_object_to_timespec, invalid, rnd)