Exemplo n.º 1
0
    def test_multiply(self):
        # multiplication should error for units with offsets

        x = _find_unit('g')
        y = _find_unit('s')
        z = _find_unit('degC')

        self.assertEqual(
            x * y,
            PhysicalUnit({
                's': 1,
                'kg': 1
            }, .001, _get_powers(mass=1, time=1), 0))
        self.assertEqual(
            y * x,
            PhysicalUnit({
                's': 1,
                'kg': 1
            }, .001, _get_powers(mass=1, time=1), 0))

        try:
            x * z
        except TypeError as err:
            self.assertEqual(str(err),
                             "cannot multiply units with non-zero offset")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 2
0
    def test_division(self):
        # division should error when working with offset units

        w = _find_unit('kg')
        x = _find_unit('g')
        y = _find_unit('s')
        z = _find_unit('degC')

        quo = w / x
        quo2 = x / y

        self.assertEqual(quo, PhysicalUnit({'kg': 1, 'g': -1},
                                           1000.0, _get_powers(), 0))
        self.assertEqual(quo2, PhysicalUnit({'s': -1, 'g': 1},
                                            0.001,
                                            _get_powers(mass=1, time=-1), 0))
        quo = y / 2.0
        self.assertEqual(quo, PhysicalUnit({'s': 1, "2.0": -1},
                                           .5, _get_powers(time=1), 0))
        quo = 2.0 / y
        self.assertEqual(quo, PhysicalUnit({'s': -1, "2.0": 1}, 2,
                                           _get_powers(time=-1), 0))
        try:
            x / z
        except TypeError as err:
            self.assertEqual(
                str(err), "Can't divide units: either 'g' or 'degC' has a non-zero offset.")
        else:
            self.fail("Expecting TypeError")