示例#1
0
    def testResults(self, s, config):
        """ Test component results. """
        (m, u) = s.components(config)
        self.assertEqual(m * int(u), s.magnitude)
        if u == B:
            return

        if config.unit is None:
            if config.binary_units:
                self.assertIn(u, BinaryUnits.UNITS())
            else:
                self.assertIn(u, DecimalUnits.UNITS())
            self.assertTrue(abs(m) >= config.min_value)
        else:
            self.assertEqual(u, config.unit)


        (exact, sign, left, right) = get_string_info(
           m,
           places=config.max_places
        )
        value = sign * Fraction("%s.%s" % (left, right))
        if config.exact_value and config.unit is None:
            self.assertTrue(exact)
            self.assertTrue(Fraction(value) * int(u) == s.magnitude)
        if not exact:
            self.assertFalse(Fraction(value) * int(u) == s.magnitude)
示例#2
0
 def testExactness(self, p, q, n, m):
     """ When max_places is not specified and the denominator of
         the value is 2^n * 5^m the result is exact.
     """
     x = Fraction(p * q, p * (2**n * 5**m))
     (exact, sign, left, right) = get_string_info(x, places=None)
     self.assertEqual(sign * Fraction("%s.%s" % (left, right)), x)
     self.assertTrue(exact)
示例#3
0
 def testException(self):
     """ Raises exception on bad input. """
     with self.assertRaises(SizeValueError):
         get_string_info(Decimal(200), places=-1)
     with self.assertRaises(SizeValueError):
         get_string_info(0.1, places=0)