From 41b364cd804d637c7624072bb724085b600e56b5 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Sun, 7 Jul 2019 01:00:10 -0700 Subject: [PATCH] add tests for integer and fractional division --- .../div_rem_sqrt_rsqrt/test_algorithm.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py b/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py index 7d6b2013..5760dbab 100644 --- a/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py +++ b/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py @@ -1077,3 +1077,55 @@ class TestFixedUDivRemSqrtRSqrt(unittest.TestCase): def test_radix_16_RSqrt(self): self.helper(4, Operation.RSqrtRem) + + def test_int_div(self): + bit_width = 8 + fract_width = 4 + log2_radix = 3 + for dividend in range(1 << bit_width): + for divisor in range(1, 1 << bit_width): + obj = FixedUDivRemSqrtRSqrt(dividend, + divisor, + Operation.UDivRem, + bit_width, + fract_width, + log2_radix) + obj.calculate() + quotient, remainder = div_rem(dividend, + divisor, + bit_width, + False) + shifted_remainder = remainder << fract_width + with self.subTest(dividend=dividend, + divisor=divisor, + quotient=quotient, + remainder=remainder, + shifted_remainder=shifted_remainder): + self.assertEqual(obj.quotient_root, quotient) + self.assertEqual(obj.remainder, shifted_remainder) + + def test_fract_div(self): + bit_width = 8 + fract_width = 4 + log2_radix = 3 + for dividend in range(1 << bit_width): + for divisor in range(1, 1 << bit_width): + obj = FixedUDivRemSqrtRSqrt(dividend << fract_width, + divisor, + Operation.UDivRem, + bit_width, + fract_width, + log2_radix) + obj.calculate() + quotient = (dividend << fract_width) // divisor + if quotient >= (1 << bit_width): + continue + remainder = (dividend << fract_width) % divisor + shifted_remainder = remainder << fract_width + with self.subTest(dividend=dividend, + divisor=divisor, + quotient=quotient, + remainder=remainder, + shifted_remainder=shifted_remainder): + self.assertEqual(obj.quotient_root, quotient) + self.assertEqual(obj.remainder, shifted_remainder) -- 2.30.2