MCPcopy Index your code
hub / github.com/python/cpython / _check_rounding

Method _check_rounding

Lib/test/test_time.py:913–956  ·  view source on GitHub ↗
(self, pytime_converter, expected_func,
                        use_float, unit_to_sec, value_filter=None)

Source from the content-addressed store, hash-verified

911 return ns_timestamps
912
913 def _check_rounding(self, pytime_converter, expected_func,
914 use_float, unit_to_sec, value_filter=None):
915
916 def convert_values(ns_timestamps):
917 if use_float:
918 unit_to_ns = SEC_TO_NS / float(unit_to_sec)
919 values = [ns / unit_to_ns for ns in ns_timestamps]
920 else:
921 unit_to_ns = SEC_TO_NS // unit_to_sec
922 values = [ns // unit_to_ns for ns in ns_timestamps]
923
924 if value_filter:
925 values = filter(value_filter, values)
926
927 # remove duplicates and sort
928 return sorted(set(values))
929
930 # test rounding
931 ns_timestamps = self._rounding_values(use_float)
932 valid_values = convert_values(ns_timestamps)
933 for time_rnd, decimal_rnd in ROUNDING_MODES:
934 with decimal.localcontext() as context:
935 context.rounding = decimal_rnd
936
937 for value in valid_values:
938 debug_info = {'value': value, 'rounding': decimal_rnd}
939 try:
940 result = pytime_converter(value, time_rnd)
941 expected = expected_func(value)
942 except Exception:
943 self.fail("Error on timestamp conversion: %s" % debug_info)
944 self.assertEqual(result,
945 expected,
946 debug_info)
947
948 # test overflow
949 ns = self.OVERFLOW_SECONDS * SEC_TO_NS
950 ns_timestamps = (-ns, ns)
951 overflow_values = convert_values(ns_timestamps)
952 for time_rnd, _ in ROUNDING_MODES :
953 for value in overflow_values:
954 debug_info = {'value': value, 'rounding': time_rnd}
955 with self.assertRaises(OverflowError, msg=debug_info):
956 pytime_converter(value, time_rnd)
957
958 def check_int_rounding(self, pytime_converter, expected_func,
959 unit_to_sec=1, value_filter=None):

Callers 2

check_int_roundingMethod · 0.95
check_float_roundingMethod · 0.95

Calls 4

_rounding_valuesMethod · 0.95
failMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected