Add additional tests for other rounding types
This commit is contained in:
parent
db98a3c733
commit
5f9d18cb04
@ -30,11 +30,11 @@ class Native(BasePyNum):
|
|||||||
factor = 10 ** dps
|
factor = 10 ** dps
|
||||||
if mode == Native.ROUND_DOWN:
|
if mode == Native.ROUND_DOWN:
|
||||||
return Native._from_impl(math.floor(self.impl * factor) / factor)
|
return Native._from_impl(math.floor(self.impl * factor) / factor)
|
||||||
elif mode == Native.ROUND_HALF_UP:
|
elif mode == Native.ROUND_HALF_UP: # pragma: no cover
|
||||||
raise NotImplementedError('ROUND_HALF_UP is not implemented in Python Native context')
|
raise NotImplementedError('ROUND_HALF_UP is not implemented in Python Native context')
|
||||||
elif mode == Native.ROUND_HALF_EVEN:
|
elif mode == Native.ROUND_HALF_EVEN:
|
||||||
return Native._from_impl(round(self.impl * factor) / factor)
|
return Native._from_impl(round(self.impl * factor) / factor)
|
||||||
elif mode == Native.ROUND_UP:
|
elif mode == Native.ROUND_UP:
|
||||||
return Native._from_impl(math.ceil(self.impl * factor) / factor)
|
return Native._from_impl(math.ceil(self.impl * factor) / factor)
|
||||||
else:
|
else: # pragma: no cover
|
||||||
raise ValueError('Invalid rounding mode')
|
raise ValueError('Invalid rounding mode')
|
||||||
|
@ -36,11 +36,11 @@ class Rational(BasePyNum):
|
|||||||
factor = Fraction(10) ** dps
|
factor = Fraction(10) ** dps
|
||||||
if mode == Rational.ROUND_DOWN:
|
if mode == Rational.ROUND_DOWN:
|
||||||
return Rational._from_impl(math.floor(self.impl * factor) / factor)
|
return Rational._from_impl(math.floor(self.impl * factor) / factor)
|
||||||
elif mode == Rational.ROUND_HALF_UP:
|
elif mode == Rational.ROUND_HALF_UP: # pragma: no cover
|
||||||
raise NotImplementedError('ROUND_HALF_UP is not implemented in Python Rational context')
|
raise NotImplementedError('ROUND_HALF_UP is not implemented in Python Rational context')
|
||||||
elif mode == Rational.ROUND_HALF_EVEN:
|
elif mode == Rational.ROUND_HALF_EVEN:
|
||||||
return Rational._from_impl(round(self.impl * factor) / factor)
|
return Rational._from_impl(round(self.impl * factor) / factor)
|
||||||
elif mode == Rational.ROUND_UP:
|
elif mode == Rational.ROUND_UP:
|
||||||
return Rational._from_impl(math.ceil(self.impl * factor) / factor)
|
return Rational._from_impl(math.ceil(self.impl * factor) / factor)
|
||||||
else:
|
else: # pragma: no cover
|
||||||
raise ValueError('Invalid rounding mode')
|
raise ValueError('Invalid rounding mode')
|
||||||
|
@ -67,7 +67,15 @@ def maketst_round(numbers, dps, num, dps_round, mode_round, result):
|
|||||||
|
|
||||||
test_fixed_round1_py, test_fixed_round1_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
test_fixed_round1_py, test_fixed_round1_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
||||||
test_fixed_round2_py, test_fixed_round2_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_UP', '3141.6')
|
test_fixed_round2_py, test_fixed_round2_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_UP', '3141.6')
|
||||||
|
test_fixed_round3_py, test_fixed_round3_js = maketst_round('Fixed', 5, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
|
||||||
|
test_fixed_round4_py, test_fixed_round4_js = maketst_round('Fixed', 5, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
|
||||||
|
|
||||||
test_native_round1_py, test_native_round1_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
test_native_round1_py, test_native_round1_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
||||||
test_native_round2_py, test_native_round2_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|
test_native_round2_py, test_native_round2_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|
||||||
|
test_native_round3_py, _ = maketst_round('Native', 0, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
|
||||||
|
_, test_native_round4_js = maketst_round('Native', 0, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
|
||||||
|
|
||||||
test_rational_round1_py, test_rational_round1_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
test_rational_round1_py, test_rational_round1_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
||||||
test_rational_round2_py, test_rational_round2_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|
test_rational_round2_py, test_rational_round2_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|
||||||
|
test_rational_round3_py, _ = maketst_round('Rational', 0, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
|
||||||
|
_, test_rational_round4_js = maketst_round('Rational', 0, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
|
||||||
|
Reference in New Issue
Block a user