def test_addition(self): pairs = [ (self.p1, self.v1), (self.v1, self.p1), (self.p1, self.v2), (self.v2, self.p1), (self.p1, self.p3), (self.p3, self.p1), (self.p1, self.p4), (self.p4, self.p1), ] for p1, p2 in pairs: p = p1 + p2 p1_value, p1_unit = get_unit_and_value(p1) p2_value, p2_unit = get_unit_and_value(p2) if p1_unit == "1": p1_unit = p2_unit elif p2_unit == "1": p2_unit = p1_unit self.assertEqual(p.value, p1_value + p2_value) self.assertEqual(ureg(p.unit).u, ureg(f"{p1_unit}+{p2_unit}").u) pairs = [(self.p1, self.p2), (self.p2, self.p1)] with self.assertRaises(pint.errors.DimensionalityError): for p1, p2 in pairs: p = p1 + p2
def test_power(self): pairs = [(self.p1, self.v1), (self.p1, self.v2), (self.p1, self.p4)] for p1, p2 in pairs: p = p1**p2 p1_value, p1_unit = get_unit_and_value(p1) p2_value, p2_unit = get_unit_and_value(p2) self.assertEqual(p.value, p1_value**p2_value) self.assertEqual( ureg(p.unit).u, ureg(f"({p1_unit})**{p2_value}").u) with self.assertRaises(AssertionError): p = self.p1**self.p2
def test_multiplication(self): pairs = [ (self.p1, self.p2), (self.p2, self.p1), (self.p1, self.v1), (self.v1, self.p1), (self.p1, self.v2), (self.v2, self.p1), (self.p1, self.p4), (self.p4, self.p1), ] for p1, p2 in pairs: p = p1 * p2 p1_value, p1_unit = get_unit_and_value(p1) p2_value, p2_unit = get_unit_and_value(p2) self.assertEqual(p.value, p1_value * p2_value) self.assertEqual(ureg(p.unit).u, ureg(f"{p1_unit}*{p2_unit}").u)