Add additional tests for other rounding types

This commit is contained in:
RunasSudo 2021-01-13 22:38:53 +11:00
parent db98a3c733
commit 5f9d18cb04
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 12 additions and 4 deletions

View File

@ -30,11 +30,11 @@ class Native(BasePyNum):
factor = 10 ** dps
if mode == Native.ROUND_DOWN:
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')
elif mode == Native.ROUND_HALF_EVEN:
return Native._from_impl(round(self.impl * factor) / factor)
elif mode == Native.ROUND_UP:
return Native._from_impl(math.ceil(self.impl * factor) / factor)
else:
else: # pragma: no cover
raise ValueError('Invalid rounding mode')

View File

@ -36,11 +36,11 @@ class Rational(BasePyNum):
factor = Fraction(10) ** dps
if mode == Rational.ROUND_DOWN:
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')
elif mode == Rational.ROUND_HALF_EVEN:
return Rational._from_impl(round(self.impl * factor) / factor)
elif mode == Rational.ROUND_UP:
return Rational._from_impl(math.ceil(self.impl * factor) / factor)
else:
else: # pragma: no cover
raise ValueError('Invalid rounding mode')

View File

@ -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_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_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_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')