示例#1
0
    def testRoundTo(self, nat, precision, method):
        """
        Test proper functioning of Nats.roundTo().
        """
        (subject, base) = nat
        (result, relation) = Nats.roundTo(subject, base, precision, method)

        if precision is None or precision >= 0:
            self.assertEqual(result, subject)
            self.assertEqual(relation, 0)
            return

        num_digits = -precision
        padding = num_digits * [0]

        if result != []:
            self.assertEqual(result[precision:], padding)

        if num_digits > len(subject):
            self.assertIn(result, ([], [1] + padding))

        int_subject = Nats.convert_to_int(subject, base)
        int_result = Nats.convert_to_int(result, base)

        self.assertEqual(
           relation,
           Fraction(int_result - int_subject, base ** num_digits)
        )

        self.assertGreater(relation, -1)
        self.assertLess(relation, 1)
示例#2
0
    def test_inverses(self, strategy):
        """
        Test that division and undivision are inverses.
        """
        (divisor, dividend, base) = strategy
        (
            integer_part,
            non_repeating_part,
            repeating_part,
            relation,
        ) = NatDivision.division(divisor, dividend, base)
        self.assertEqual(relation, 0)

        (denominator,
         numerator) = NatDivision.undivision(integer_part, non_repeating_part,
                                             repeating_part, base)

        self.assertTrue(
            isinstance(numerator, list)
            and (not numerator or numerator[0] != 0))
        self.assertNotEqual(denominator, [])
        self.assertNotEqual(denominator[0], 0)

        original = fractions.Fraction(Nats.convert_to_int(dividend, base),
                                      Nats.convert_to_int(divisor, base))
        result = fractions.Fraction(Nats.convert_to_int(numerator, base),
                                    Nats.convert_to_int(denominator, base))

        self.assertEqual(original, result)
示例#3
0
 def testUlp(self, radix):
     """
     Verify that ulp has correct relation to ``radix``.
     """
     result = radix.ulp
     if radix.repeating_part != []:
         self.assertIsNone(result)
     else:
         (carry_out, non_repeating_part) = \
            Nats.carry_in(radix.non_repeating_part, 1, radix.base)
         (carry_out, integer_part) = \
            Nats.carry_in(radix.integer_part, carry_out, radix.base)
         if carry_out != 0:
             integer_part = [carry_out] + integer_part
         new = Radix(
            radix.sign if radix.sign != 0 else 1,
            integer_part,
            non_repeating_part,
            [],
            radix.base
         )
         difference = new.as_rational() - radix.as_rational()
         if radix.sign < 0:
             self.assertEqual(difference, -result)
         else:
             self.assertEqual(difference, result)
示例#4
0
 def testFromInt(self, value, to_base):
     """
     convert_to_int(convert_from_int(value, to_base), 10) == value
     No leading zeros in convert_from_int(value, to_base)
     """
     result = Nats.convert_from_int(value, to_base)
     assert result[:1] != [0]
     assert Nats.convert_to_int(result, to_base) == value
示例#5
0
 def test_from_other(self, nat, to_base):
     """Test roundtrip from number in arbitrary base."""
     (subject, from_base) = nat
     result = Nats.convert(subject, from_base, to_base)
     self.assertEqual(
         Nats.convert_to_int(result, to_base),
         Nats.convert_to_int(subject, from_base),
     )
示例#6
0
 def test_from_int(self, value, to_base):
     """
     convert_to_int(convert_from_int(value, to_base), 10) == value
     No leading zeros in convert_from_int(value, to_base)
     """
     result = Nats.convert_from_int(value, to_base)
     self.assertNotEqual(result[:1], [0])
     self.assertEqual(Nats.convert_to_int(result, to_base), value)
示例#7
0
    def testCarryIn(self, strategy):
        """
        Test carry_in.

        :param strategy: the strategy (tuple of value, carry, base)
        """
        (value, base, carry) = strategy
        (carry_out, result) = Nats.carry_in(value, carry, base)
        assert len(result) == len(value)

        result2 = Nats.convert_from_int(
           Nats.convert_to_int(value, base) + carry,
           base
        )

        assert len(result2) >= len(result)

        assert (len(result2) == len(result)) or \
           result2[0] == carry_out and result2[1:] == result

        assert not (len(result2) == len(result)) or result2 == result
示例#8
0
    def test_carry_in(self, strategy):
        """
        Test carry_in.

        :param strategy: the strategy (tuple of value, carry, base)
        """
        (value, carry, base) = strategy
        (carry_out, result) = Nats.carry_in(value, carry, base)
        self.assertEqual(len(result), len(value))

        result2 = Nats.convert_from_int(Nats.convert_to_int(value, base) + carry, base)

        self.assertGreaterEqual(len(result2), len(result))

        self.assertTrue(
            (len(result2) == len(result))
            or result2[0] == carry_out
            and result2[1:] == result
        )

        self.assertTrue(not (len(result2) == len(result)) or result2 == result)
示例#9
0
    def testUpDown(self, divisor, dividend, base, precision):
        """
        Test that rounding up and rounding down have the right relationship.
        """
        # pylint: disable=too-many-locals
        (integer_part, non_repeating_part, repeating_part, rel) = \
           NatDivision.division(
              divisor,
              dividend,
              base,
              precision=precision,
              method=RoundingMethods.ROUND_UP
           )
        (integer_part_2, non_repeating_part_2, repeating_part_2, rel_2) = \
           NatDivision.division(
              divisor,
              dividend,
              base,
              precision=precision,
              method=RoundingMethods.ROUND_DOWN
           )
        (integer_part_3, non_repeating_part_3, repeating_part_3, rel_3) = \
           NatDivision.division(
              divisor,
              dividend,
              base,
              precision=precision,
              method=RoundingMethods.ROUND_TO_ZERO
           )

        assert integer_part_2 == integer_part_3 and \
           non_repeating_part_2 == non_repeating_part_3 and \
           repeating_part_2 == repeating_part_3

        assert repeating_part != [] or repeating_part_2 == []
        assert rel >= rel_2 and rel_2 == rel_3

        round_up_int = \
           Nats.convert_to_int(integer_part + non_repeating_part, base)
        round_down_int = \
           Nats.convert_to_int(integer_part_2 + non_repeating_part_2, base)

        if repeating_part == []:
            assert round_up_int - round_down_int in (0, 1)

        if rel == 0:
            assert round_up_int == round_down_int
            assert rel_2 == 0
            assert rel_3 == 0


        for method in RoundingMethods.CONDITIONAL_METHODS():
            (integer_part_c, non_repeating_part_c, _, rel) = \
               NatDivision.division(
                  divisor,
                  dividend,
                  base,
                  precision=precision,
                  method=method
               )
            rounded_int = \
               Nats.convert_to_int(integer_part_c + non_repeating_part_c, base)
            if repeating_part == []:
                assert rounded_int <= round_up_int and \
                   rounded_int >= round_down_int
                if rel == 0:
                    assert round_up_int == round_down_int
                elif rel < 0:
                    assert rounded_int == round_down_int
                else:
                    assert rounded_int == round_up_int
示例#10
0
 def testExceptions(self):
     """ Test throwing exception. """
     with self.assertRaises(BasesError):
         Nats.convert_from_int(-32, 2)
     with self.assertRaises(BasesError):
         Nats.convert_from_int(32, -2)
     with self.assertRaises(BasesError):
         Nats.convert([1], 1, 2)
     with self.assertRaises(BasesError):
         Nats.convert([1], 2, 1)
     with self.assertRaises(BasesError):
         Nats.convert_to_int([1], 1)
     with self.assertRaises(BasesError):
         Nats.convert_to_int([-1], 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([-1], 1, 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([1], -1, 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([1], 1, 1)
     with self.assertRaises(BasesError):
         Nats.roundTo([1], 0, 2, RoundingMethods.ROUND_DOWN)
     with self.assertRaises(BasesError):
         Nats.roundTo([3], 2, 2, RoundingMethods.ROUND_DOWN)
     with self.assertRaises(BasesError):
         Nats.roundTo([1], 2, 2, None)
示例#11
0
 def testFromOther(self, nat, to_base):
     """ Test roundtrip from number in arbitrary base. """
     (subject, from_base) = nat
     result = Nats.convert(subject, from_base, to_base)
     assert Nats.convert_to_int(result, to_base) == \
         Nats.convert_to_int(subject, from_base)
示例#12
0
 def test_exceptions(self):
     """Test throwing exception."""
     with self.assertRaises(BasesError):
         Nats.convert_from_int(-32, 2)
     with self.assertRaises(BasesError):
         Nats.convert_from_int(32, -2)
     with self.assertRaises(BasesError):
         Nats.convert([1], 1, 2)
     with self.assertRaises(BasesError):
         Nats.convert([1], 2, 1)
     with self.assertRaises(BasesError):
         Nats.convert_to_int([1], 1)
     with self.assertRaises(BasesError):
         Nats.convert_to_int([-1], 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([-1], 1, 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([1], -1, 2)
     with self.assertRaises(BasesError):
         Nats.carry_in([1], 1, 1)
示例#13
0
    def test_up_down(self, divisor, dividend, base, precision):
        """
        Test that rounding up and rounding down have the right relationship.
        """
        # pylint: disable=too-many-locals
        divisor = Nats.convert_from_int(divisor, base)
        dividend = Nats.convert_from_int(dividend, base)
        (integer_part, non_repeating_part, repeating_part,
         rel) = NatDivision.division(divisor, dividend, base, precision,
                                     RoundingMethods.ROUND_UP)
        (
            integer_part_2,
            non_repeating_part_2,
            repeating_part_2,
            rel_2,
        ) = NatDivision.division(divisor, dividend, base, precision,
                                 RoundingMethods.ROUND_DOWN)
        (
            integer_part_3,
            non_repeating_part_3,
            repeating_part_3,
            rel_3,
        ) = NatDivision.division(divisor, dividend, base, precision,
                                 RoundingMethods.ROUND_TO_ZERO)

        self.assertEqual(integer_part_2, integer_part_3)
        self.assertEqual(non_repeating_part_2, non_repeating_part_3)
        self.assertEqual(repeating_part_2, repeating_part_3)

        self.assertTrue(repeating_part != [] or repeating_part_2 == [])
        self.assertGreaterEqual(rel, rel_2)
        self.assertEqual(rel_2, rel_3)

        round_up_int = Nats.convert_to_int(integer_part + non_repeating_part,
                                           base)
        round_down_int = Nats.convert_to_int(
            integer_part_2 + non_repeating_part_2, base)

        if repeating_part == []:
            self.assertIn(round_up_int - round_down_int, (0, 1))

        if rel == 0:
            self.assertEqual(round_up_int, round_down_int)
            self.assertEqual(rel_2, 0)
            self.assertEqual(rel_3, 0)

        for method in RoundingMethods.CONDITIONAL_METHODS():
            (integer_part_c, non_repeating_part_c, _,
             rel) = NatDivision.division(divisor, dividend, base, precision,
                                         method)
            rounded_int = Nats.convert_to_int(
                integer_part_c + non_repeating_part_c, base)
            if repeating_part == []:
                self.assertLessEqual(round_down_int, rounded_int)
                self.assertLessEqual(rounded_int, round_up_int)
                if rel == 0:
                    self.assertEqual(round_up_int, round_down_int)
                elif rel == -1:
                    self.assertEqual(rounded_int, round_down_int)
                else:
                    self.assertEqual(rounded_int, round_up_int)