From 5f9d18cb04ca4ccbff28d782b4a64541afe18f5a Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 13 Jan 2021 22:38:53 +1100 Subject: [PATCH] Add additional tests for other rounding types --- pyRCV2/numbers/native_py.py | 4 ++-- pyRCV2/numbers/rational_py.py | 4 ++-- tests/test_numbers.py | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pyRCV2/numbers/native_py.py b/pyRCV2/numbers/native_py.py index 155b050..c6908ee 100644 --- a/pyRCV2/numbers/native_py.py +++ b/pyRCV2/numbers/native_py.py @@ -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') diff --git a/pyRCV2/numbers/rational_py.py b/pyRCV2/numbers/rational_py.py index cfc5b6c..9b4de4c 100644 --- a/pyRCV2/numbers/rational_py.py +++ b/pyRCV2/numbers/rational_py.py @@ -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') diff --git a/tests/test_numbers.py b/tests/test_numbers.py index b76b6c4..f2cc405 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -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')