def test_get_available_units(self): """Test that I can get the available units.""" self.assertEqual(set(PhysicalQuantity(Dimension()).get_available_units()), set(["1"])) # test only whether it's a subset, so it doesn't fail as I add more units self.assertTrue(set(["kg", "kilogram", "g", "gram"]) <= set(PhysicalQuantity(Dimension(M = 1)).get_available_units())) self.assertTrue(set(["m/s", "meters/second", "miles/hour", "mi/hr"]) <= set(Speed().get_available_units()))
def test_dimensionless_quantity(self): """Test dimensionless quantities.""" d = Dimension() # creating p = PhysicalQuantity(d) # assigning & getting the value. The "unit" is "1": need a better interface. p["1"] = 4 self.assertEqual(p["1"], 4) # creating from a string p = PhysicalQuantity(d, "7") self.assertEqual(p["1"], 7) # creating from a string: trying to use "1" as unit in the string fails self.assertRaises(BadInputError, PhysicalQuantity, d, "7 1")
def test_str(self): """str() prints a reasonable form for the quantity.""" # dimensionless case p = PhysicalQuantity(Dimension(), "2.1e2") self.assertEqual(str(p), "210") # For quantities that are NOT dimensionless we use the "basic unit" (whatever has a unit conversion # factor, so it's SI in our case) with the shortest string representation. # Also, in both the numerator and denominator the order followed is M L T Q Theta p = Speed("60 km/min") self.assertEqual(str(p), "1000 m/s") p = PhysicalQuantity(Dimension(Q = 1), "4 coulomb") self.assertEqual(str(p), "4 C") p = Temperature("4 kelvin") self.assertEqual(str(p), "4 K") p = Speed("30m/s")/Time("2s")/PhysicalQuantity(Dimension(M = 1), "3kg") self.assertEqual(str(p), "5 m/kgs^2")
def test_comparisons(self): """All comparisons should be available between quantities of the same type.""" p1 = PhysicalQuantity(Dimension(L = 1), "2m") p2 = PhysicalQuantity(Dimension(L = 1), "2m") self.assertTrue(p1 == p2) self.assertTrue(p1 >= p2) self.assertTrue(p1 <= p2) self.assertFalse(p1 != p2) self.assertFalse(p1 < p2) self.assertFalse(p1 > p2) p2['km'] = 1 self.assertFalse(p1 == p2) self.assertFalse(p1 >= p2) self.assertTrue(p1 <= p2) self.assertTrue(p1 != p2) self.assertFalse(p1 > p2) self.assertTrue(p1 < p2)
def test_create_simple_distances(self): """Simple distances.""" # Check consistency for unit,meters in self.meters_in.iteritems(): d = Distance('1' + unit) # create "1x" where x is the unit self.assertEqual(d['m'], meters) # the meters should be correct # Check creating from other distances d1 = Distance("1 m") d2 = Distance(d1) self.assertEqual(d1['m'], d2['m']) # Check creating from another quantity with same dimensions d1 = PhysicalQuantity(Dimension(L = 1), "1 m") d2 = Distance(d1) self.assertEqual(d1['m'], d2['m']) # Check creating from another quantity with different dimensions d1 = PhysicalQuantity(Dimension(T = 1), "1 s") self.assertRaises(IncompatibleUnitsError, Distance, d1)
def test_create_simple_masses(self): """Simple masses.""" # Check consistency for unit,kilograms in self.kilograms_in.iteritems(): m = Mass('1' + unit) # create "1x" where x is the unit self.assertEqual(m['kg'], kilograms) # the kilograms should be correct # Check creating from other distances m1 = Mass("1 kg") m2 = Mass(m1) self.assertEqual(m1['kg'], m2['kg']) # Check creating from another quantity with same dimensions m1 = PhysicalQuantity(Dimension(M = 1), "1 kg") m2 = Mass(m1) self.assertEqual(m1['kg'], m2['kg']) # Check creating from another quantity with different dimensions t = PhysicalQuantity(Dimension(T = 1), "1 s") self.assertRaises(IncompatibleUnitsError, Mass, t)
def test_repr(self): """repr() should give something that can be used to recreate the object.""" p1 = PhysicalQuantity(Dimension(L = 1), "2m") p2 = eval(repr(p1)) self.assertEqual(p1, p2) # special case: dimensionless quantities p1 = PhysicalQuantity(Dimension(), "2") p2 = eval(repr(p1)) self.assertEqual(p1, p2) # derived quantities should also work t1 = Time("3 min") t2 = eval(repr(t1)) self.assertEqual(t1, t2) # a more complicated case p1 = Speed("30m/s")/Time("2s")/PhysicalQuantity(Dimension(M = 1), "3kg") p2 = eval(repr(p1)) self.assertEqual(p1, p2)
def test_type_guessing_in_general(self): """The library should find the proper type depending on dimensions.""" d = Distance("10m") t = Time("5s") self.assertEqual(type(d/t), Speed) v = Speed("10mi/hr") self.assertEqual(type(v*t), Distance) # charge density rho = PhysicalQuantity(Dimension(L = -3, Q = 1), "4C/m^3") q = rho*d*d*d self.assertEqual(type(q), Charge) self.assertEqual(q['C'], 4000) # Note: this doesn't work for a quantity explicitly defined as a PhysicalQuantity T1 = Temperature("3 K") T2 = PhysicalQuantity(Dimension(Theta = 1), "3 K") self.assertEqual(T1, T2) self.assertEqual(type(T1), Temperature) self.assertEqual(type(T2), PhysicalQuantity) # But a multiplication or division by a dimensionless quantity should fix that T3 = T2/PhysicalQuantity(Dimension(), "1") self.assertEqual(type(T3), Temperature)
def test_create_simply_physical_quantity(self): """Simple physical quantities.""" d = Dimension(L = 1) p = PhysicalQuantity(d, "3m") self.assertEqual(p['m'], 3) self.assertEqual(p['meters'], 3) self.assertEqual(p['km'], 0.003) self.assertEqual(p['kilometers'], 0.003) p['km'] = 2 self.assertEqual(p['m'], 2000) self.assertEqual(p['meters'], 2000) self.assertEqual(p['km'], 2) self.assertEqual(p['kilometers'], 2)
def test_type_coercion_on_addition_and_subtraction(self): """A PhysicalQuantity, when added/subtracted to/from a Time becomes a Time.""" t1 = Time("5s") t2 = PhysicalQuantity(Dimension(T = 1), "1 min") self.assertTrue(type(t1) != type(t2)) # sanity check before the real check # coercion on the left & right self.assertEqual(type(t1 + t2), type(t1)) self.assertEqual(type(t2 + t1), type(t1)) self.assertEqual(type(t1 - t2), type(t1)) self.assertEqual(type(t2 - t1), type(t1)) # A more complex example s = Speed("3 m/s") d = Distance("4 m") t = Time("4 s") self.assertEqual(type(s + d/t), Speed) self.assertEqual(type(d/t + s), Speed)