Пример #1
0
    def test_object_to_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**6, (0, 15625)),
                (-1/2**6, (-1, 984375)),
                (-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.9999999, (0, 999999), FLOOR),
            (0.9999999, (1, 0), CEILING),
            (0.9999999, (1, 0), HALF_UP),

            (-0.6e-6, (-1, 999999), HALF_UP),
            # skipped, -0.5e-6 is inexact in base 2
            #(-0.5e-6, (-1, 999999), HALF_UP),
            (-0.4e-6, (0, 0), HALF_UP),

            (0.4e-6, (0, 0), HALF_UP),
            # skipped, 0.5e-6 is inexact in base 2
            #(0.5e-6, (0, 1), HALF_UP),
            (0.6e-6, (0, 1), 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)
Пример #2
0
    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.9999999, (0, 999999), FLOOR),
            (0.9999999, (1, 0), CEILING),
            (0.9999999, (1, 0), HALF_UP),
            (-0.6e-6, (-1, 999999), HALF_UP),
                # skipped, -0.5e-6 is inexact in base 2
                #(-0.5e-6, (-1, 999999), HALF_UP),
            (-0.4e-6, (0, 0), HALF_UP),
            (0.4e-6, (0, 0), HALF_UP),
                # skipped, 0.5e-6 is inexact in base 2
                #(0.5e-6, (0, 1), HALF_UP),
            (0.6e-6, (0, 1), 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)
Пример #3
0
    def test_object_to_timeval(self):
        from _testcapi import pytime_object_to_timeval

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

        self.check_float_rounding(pytime_object_to_timeval,
                                  self.create_converter(SEC_TO_US),
                                  value_filter=self.time_t_filter)

        # test nan
        for time_rnd, _ in ROUNDING_MODES:
            with self.assertRaises(ValueError):
                pytime_object_to_timeval(float('nan'), time_rnd)
Пример #4
0
    def test_object_to_timeval(self):
        from _testcapi import pytime_object_to_timeval

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

        self.check_float_rounding(pytime_object_to_timeval,
                                  self.create_converter(SEC_TO_US),
                                  value_filter=self.time_t_filter)

         # test nan
        for time_rnd, _ in ROUNDING_MODES:
            with self.assertRaises(ValueError):
                pytime_object_to_timeval(float('nan'), time_rnd)
Пример #5
0
    def test_timeval(self):
        from _testcapi import pytime_object_to_timeval
        for obj, timeval, 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-6, (0, 1), _PyTime_ROUND_DOWN),
            (1e-7, (0, 0), _PyTime_ROUND_DOWN),
            (-1e-6, (-1, 999999), _PyTime_ROUND_DOWN),
            (-1e-7, (-1, 999999), _PyTime_ROUND_DOWN),
            (-1.2, (-2, 800000), _PyTime_ROUND_DOWN),
            (0.9999999, (0, 999999), _PyTime_ROUND_DOWN),
            (0.0000041, (0, 4), _PyTime_ROUND_DOWN),
            (1.1234560, (1, 123456), _PyTime_ROUND_DOWN),
            (1.1234569, (1, 123456), _PyTime_ROUND_DOWN),
            (-0.0000040, (-1, 999996), _PyTime_ROUND_DOWN),
            (-0.0000041, (-1, 999995), _PyTime_ROUND_DOWN),
            (-1.1234560, (-2, 876544), _PyTime_ROUND_DOWN),
            (-1.1234561, (-2, 876543), _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-6, (0, 1), _PyTime_ROUND_UP),
            (1e-7, (0, 1), _PyTime_ROUND_UP),
            (-1e-6, (-1, 999999), _PyTime_ROUND_UP),
            (-1e-7, (-1, 999999), _PyTime_ROUND_UP),
            (-1.2, (-2, 800000), _PyTime_ROUND_UP),
            (0.9999999, (1, 0), _PyTime_ROUND_UP),
            (0.0000041, (0, 5), _PyTime_ROUND_UP),
            (1.1234560, (1, 123457), _PyTime_ROUND_UP),
            (1.1234569, (1, 123457), _PyTime_ROUND_UP),
            (-0.0000040, (-1, 999996), _PyTime_ROUND_UP),
            (-0.0000041, (-1, 999995), _PyTime_ROUND_UP),
            (-1.1234560, (-2, 876544), _PyTime_ROUND_UP),
            (-1.1234561, (-2, 876543), _PyTime_ROUND_UP),
        ):
            with self.subTest(obj=obj, round=rnd, timeval=timeval):
                self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)

        rnd = _PyTime_ROUND_DOWN
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError,
                              pytime_object_to_timeval, invalid, rnd)
Пример #6
0
    def test_timeval(self):
        from _testcapi import pytime_object_to_timeval
        for obj, timeval, 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-6, (0, 1), _PyTime_ROUND_DOWN),
            (1e-7, (0, 0), _PyTime_ROUND_DOWN),
            (-1e-6, (-1, 999999), _PyTime_ROUND_DOWN),
            (-1e-7, (-1, 999999), _PyTime_ROUND_DOWN),
            (-1.2, (-2, 800000), _PyTime_ROUND_DOWN),
            (0.9999999, (0, 999999), _PyTime_ROUND_DOWN),
            (0.0000041, (0, 4), _PyTime_ROUND_DOWN),
            (1.1234560, (1, 123456), _PyTime_ROUND_DOWN),
            (1.1234569, (1, 123456), _PyTime_ROUND_DOWN),
            (-0.0000040, (-1, 999996), _PyTime_ROUND_DOWN),
            (-0.0000041, (-1, 999995), _PyTime_ROUND_DOWN),
            (-1.1234560, (-2, 876544), _PyTime_ROUND_DOWN),
            (-1.1234561, (-2, 876543), _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-6, (0, 1), _PyTime_ROUND_UP),
            (1e-7, (0, 1), _PyTime_ROUND_UP),
            (-1e-6, (-1, 999999), _PyTime_ROUND_UP),
            (-1e-7, (-1, 999999), _PyTime_ROUND_UP),
            (-1.2, (-2, 800000), _PyTime_ROUND_UP),
            (0.9999999, (1, 0), _PyTime_ROUND_UP),
            (0.0000041, (0, 5), _PyTime_ROUND_UP),
            (1.1234560, (1, 123457), _PyTime_ROUND_UP),
            (1.1234569, (1, 123457), _PyTime_ROUND_UP),
            (-0.0000040, (-1, 999996), _PyTime_ROUND_UP),
            (-0.0000041, (-1, 999995), _PyTime_ROUND_UP),
            (-1.1234560, (-2, 876544), _PyTime_ROUND_UP),
            (-1.1234561, (-2, 876543), _PyTime_ROUND_UP),
        ):
            with self.subTest(obj=obj, round=rnd, timeval=timeval):
                self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)

        rnd = _PyTime_ROUND_DOWN
        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timeval, invalid,
                              rnd)
Пример #7
0
    def test_timeval(self):
        from _testcapi import pytime_object_to_timeval
        for obj, timeval in (
            (0, (0, 0)),
            (-1, (-1, 0)),
            (-1.0, (-1, 0)),
            (1e-6, (0, 1)),
            (-1e-6, (-1, 999999)),
            (-1.2, (-2, 800000)),
            (1.1234560, (1, 123456)),
            (1.1234569, (1, 123456)),
            (-1.1234560, (-2, 876544)),
            (-1.1234561, (-2, 876543)),
        ):
            self.assertEqual(pytime_object_to_timeval(obj), timeval)

        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
Пример #8
0
    def test_timeval(self):
        from _testcapi import pytime_object_to_timeval
        for obj, timeval in (
            (0, (0, 0)),
            (-1, (-1, 0)),
            (-1.0, (-1, 0)),
            (1e-6, (0, 1)),
            (-1e-6, (-1, 999999)),
            (-1.2, (-2, 800000)),
            (1.1234560, (1, 123456)),
            (1.1234569, (1, 123456)),
            (-1.1234560, (-2, 876544)),
            (-1.1234561, (-2, 876543)),
        ):
            self.assertEqual(pytime_object_to_timeval(obj), timeval)

        for invalid in self.invalid_values:
            self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)