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)
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)