def test_subtract(self): a = Quantity(1, 0.2, {'Newton': 1}, si_system) b = Quantity(3, 0.4, {'Kilogram': 1, 'Meter': 1, 'Second': -2}, si_system) c = Quantity(-2, 1 / math.sqrt(5), {'Newton': 1}, si_system) d = Quantity(1, 0.2, {'Kilogram': 1}, si_system) self.assert_quantity_equal(a - b, c.expand()) with self.assertRaises(TypeError): a - d with self.assertRaises(TypeError): a - 1
def test_almost_equals(self): a = Quantity(1, 0.5, {'Kilogram': 1}, si_system) b = Quantity(2, 0.7, {'Kilogram': 1}, si_system) c = Quantity(3, 0.9, {'Kilogram': 1}, si_system) d = Quantity(1, 0.5, {'Meter': 1}, si_system) e = Quantity(1, 0.5, {}, si_system) f = Quantity(2, 0.7, {}, si_system) self.assertTrue(a.almost_equals(b)) self.assertFalse(a.almost_equals(c)) self.assertRaises(TypeError, a.almost_equals, d) for x in [a, b, c, d]: self.assertRaises(TypeError, x.almost_equals, 1) self.assertTrue(e.almost_equals(1)) self.assertTrue(f.almost_equals(2)) self.assertFalse(e.almost_equals(2)) self.assertFalse(f.almost_equals(1)) self.assertTrue(e.almost_equals(f))
def test_expand(self): # Lorentz force a = Quantity(1, 0, {'Coulomb': 1, 'Meter': 1, 'Second': -1, 'Tesla': 1}, si_system) b = Quantity(1, 0, {'Newton': 1}, si_system) self.assert_quantity_equal(a.expand(), b.expand()) # Faraday's law a = Quantity(1, 0, {'Weber': 1, 'Second': -1}, si_system) b = Quantity(1, 0, {'Volt': 1}, si_system) self.assert_quantity_equal(a.expand(), b.expand()) # torque of a motor a = Quantity(1, 0, {'Ampere': 1, 'Tesla': 1, 'Meter': 2}, si_system) b = Quantity(1, 0, {'Newton': 1, 'Meter': 1}, si_system) self.assert_quantity_equal(a.expand(), b.expand()) # resonance frequency of an RLC circuit a = Quantity(1, 0, {'Henry': -1/2, 'Farad': -1/2}, si_system) b = Quantity(1, 0, {'Hertz': 1}, si_system) self.assert_quantity_equal(a.expand(), b.expand())