add tests for integer and fractional division
authorJacob Lifshay <programmerjake@gmail.com>
Sun, 7 Jul 2019 08:00:10 +0000 (01:00 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Sun, 7 Jul 2019 08:00:10 +0000 (01:00 -0700)
src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py

index 7d6b201350cd95f3f74ae20b0d16864c975880e1..5760dbabee6382a3063ab87c15a58e6f4b277681 100644 (file)
@@ -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)