def test_known_values(self): t1 = Temperature() t2 = Temperature() t3 = Temperature() t4 = Temperature() for K, R, C, F in self.known_values: t1['K'] = K self.assertAlmostEqual(t1['K'], K) self.assertAlmostEqual(t1['R'], R) self.assertAlmostEqual(t1['C'], C) self.assertAlmostEqual(t1['F'], F) t2['R'] = R self.assertAlmostEqual(t2['K'], K) self.assertAlmostEqual(t2['R'], R) self.assertAlmostEqual(t2['C'], C) self.assertAlmostEqual(t2['F'], F) t3['C'] = C self.assertAlmostEqual(t3['K'], K) self.assertAlmostEqual(t3['R'], R) self.assertAlmostEqual(t3['C'], C) self.assertAlmostEqual(t3['F'], F) t4['F'] = F self.assertAlmostEqual(t4['K'], K) self.assertAlmostEqual(t4['R'], R) self.assertAlmostEqual(t4['C'], C) self.assertAlmostEqual(t4['F'], F)
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_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_consistency(self): """In its own units, the value should be 1.""" for unit in self.kelvins_in.keys(): t = Temperature('1' + unit) # create "1x" where x is the unit self.assertEqual(t[unit], 1)
def test_create_simple_temperatures(self): """Simple temperatures.""" for unit,kelvins in self.kelvins_in.iteritems(): t = Temperature('1' + unit) # create "1x" where x is the unit self.assertEqual(t['K'], kelvins)