Example #1
0
 def test_complex(self):
     """
     Test the ComplexWithUncertainties for proper usage
     """
     f1 = float(3.5)
     f2 = float(12)
     lu1 = 1
     uu1 = 2
     lu2 = 4.1
     uu2 = 7.2
     fu1 = FloatWithUncertainties(f1,
                                  lower_uncertainty=lu1,
                                  upper_uncertainty=uu1)
     fu2 = FloatWithUncertainties(f2,
                                  lower_uncertainty=lu2,
                                  upper_uncertainty=uu2)
     c1 = ComplexWithUncertainties()
     c2 = ComplexWithUncertainties(f1, f2)
     c3 = ComplexWithUncertainties(f1,
                                   f2,
                                   lower_uncertainty=complex(lu1, lu2),
                                   upper_uncertainty=complex(uu1, uu2))
     c4 = ComplexWithUncertainties(fu1, fu2)
     # c1 should be 0+0j with uncertainties of None
     self._check_complex_with_u(c1, 0, None, None, 0, None, None)
     # c2 should return the floats
     self._check_complex_with_u(c2, f1, None, None, f2, None, None)
     # c3 and c4 should be the same.
     self._check_complex_with_u(c3, f1, lu1, uu1, f2, lu2, uu2)
     self._check_complex_with_u(c4, f1, lu1, uu1, f2, lu2, uu2)
     assert c4.real == fu1
     assert c4.imag == fu2
Example #2
0
    def test_floating_point_types_are_indempotent(self):
        """
        Applying the constructor multiple times should not change the values.
        """
        f = FloatWithUncertainties(1.0,
                                   lower_uncertainty=0.5,
                                   upper_uncertainty=1.5)
        assert f == 1.0
        assert f.lower_uncertainty == 0.5
        assert f.upper_uncertainty == 1.5
        f = FloatWithUncertainties(f)
        assert f == 1.0
        assert f.lower_uncertainty == 0.5
        assert f.upper_uncertainty == 1.5

        f = FloatWithUncertaintiesAndUnit(1.0,
                                          lower_uncertainty=0.5,
                                          upper_uncertainty=1.5,
                                          unit="AB")
        assert f == 1.0
        assert f.lower_uncertainty == 0.5
        assert f.upper_uncertainty == 1.5
        assert f.unit == "AB"
        f = FloatWithUncertaintiesAndUnit(f)
        assert f == 1.0
        assert f.lower_uncertainty == 0.5
        assert f.upper_uncertainty == 1.5
        assert f.unit == "AB"
Example #3
0
    def test_floating_point_types_are_indempotent(self):
        """
        Applying the constructor multiple times should not change the values.
        """
        f = FloatWithUncertainties(1.0,
                                   lower_uncertainty=0.5,
                                   upper_uncertainty=1.5)
        self.assertEqual(f, 1.0)
        self.assertEqual(f.lower_uncertainty, 0.5)
        self.assertEqual(f.upper_uncertainty, 1.5)
        f = FloatWithUncertainties(f)
        self.assertEqual(f, 1.0)
        self.assertEqual(f.lower_uncertainty, 0.5)
        self.assertEqual(f.upper_uncertainty, 1.5)

        f = FloatWithUncertaintiesAndUnit(1.0,
                                          lower_uncertainty=0.5,
                                          upper_uncertainty=1.5,
                                          unit="AB")
        self.assertEqual(f, 1.0)
        self.assertEqual(f.lower_uncertainty, 0.5)
        self.assertEqual(f.upper_uncertainty, 1.5)
        self.assertEqual(f.unit, "AB")
        f = FloatWithUncertaintiesAndUnit(f)
        self.assertEqual(f, 1.0)
        self.assertEqual(f.lower_uncertainty, 0.5)
        self.assertEqual(f.upper_uncertainty, 1.5)
        self.assertEqual(f.unit, "AB")