def test_power_to_dimensionless_quantities(self): """I can raise quantities to dimensionless quantities.""" L1 = Distance("3m") L2 = Distance("6m") A = L1*L2 L3 = A**(L1/L2) self.assertEqual(L3['m'], (3*6)**(1/2))
def test_distance_subtracting(self): """Test subtracting distances.""" d1 = Distance("10 m") d2 = Distance("3 km") d3 = d2 - d1 self.assertEqual(d1.dimension, d3.dimension) # type is the same self.assertEqual(d3['m'], 2990)
def test_distance_adding(self): """Test adding distances.""" d1 = Distance("10 m") d2 = Distance("3 km") d3 = d1 + d2 self.assertEqual(d1.dimension, d3.dimension) # type is the same self.assertEqual(d3['m'], 3010)
def test_creating_from_other_distance(self): """I can create a distance from another.""" d1 = Distance("10 m") d2 = Distance(d1) self.assertEqual(d1, d2) d2['m'] = 2 self.assertEqual(d2['m'], 2) self.assertEqual(d1['m'], 10)
def test_simple_speeds(self): """Create a few speeds and check the value.""" s = Speed('1 mi/hr') self.assertEqual(s['mi/hr'], 1) s['miles/hr'] = 2.5 self.assertEqual(s['mi/hr'], 2.5) self.assertEqual(s['m/s'], 2.5*Distance('1mi')['m']/Time('1hr')['s'])
def test_comparison_of_combined_units(self): d = Distance("10m") t = Time("5s") self.assertFalse(d.dimension == t.dimension) self.assertRaises(IncompatibleUnitsError, d.__lt__, t) self.assertRaises(IncompatibleUnitsError, d.__gt__, t) self.assertRaises(IncompatibleUnitsError, d.__le__, t) self.assertRaises(IncompatibleUnitsError, d.__ge__, t)
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_multiplication_and_division_of_combined_units(self): d = Distance("10m") t = Time("5s") s1 = d/t # division s2 = Speed("2m/s") self.assertEqual(s1.dimension, s2.dimension) self.assertEqual(s1, s2) d2 = s2*t # multiplication self.assertEqual(d2, d)
def test_check_known_pace(self): """Check pace for some speeds.""" # speed, distance for pace, pace known_values = [ ['1 m/s', '1 km', '1000s'], ['1 meters/s', '1 km', '1000s'], ['1 mi/hr', '1 mi', '1 hr'] ] for speed, distance, pace in known_values: s, d, t = Speed(speed), Distance(distance), Time(pace) self.assertEqual(s.pace(d)['s'], t['s']) # the seconds should be correct
def test_power(self): """I can raise quantities to integer or fractional powers.""" L = Distance("3m") A = L**2 self.assertEqual(A, L*L) V = L**3 self.assertEqual(V, L*L*L) L2 = A**0.5 self.assertEqual(L2, L) # type guessing works m = Mass("7 kg") v = Speed("11 m/s") E = 1/2*m*v**2 self.assertEqual(E['J'], 1/2*7*11*11)
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)
def test_multiplication_and_division_involving_scalars(self): d1 = Distance("10m") d2 = d1/2 self.assertEqual(type(d2), Distance) self.assertEqual(d2['m'], 5) d3 = d1*2 # multiply on the right self.assertEqual(type(d3), Distance) self.assertEqual(d3['m'], 20) d4 = 2*d1 # multiply on the left self.assertEqual(type(d4), Distance) self.assertEqual(d4['m'], 20) t1 = Time("4hr") rate = 8/t1 self.assertEqual(rate["1/hr"], 2) t2 = 8/rate self.assertEqual(type(t2), Time) self.assertEqual(t2, t1)
def test_addition_and_subtraction_involving_scalars(self): v1 = Dimensionless("1") v2 = v1 + 2 self.assertEqual(type(v2), type(v1)) self.assertEqual(v2['1'], 3) v3 = 2 + v1 self.assertEqual(type(v3), type(v1)) self.assertEqual(v3['1'], 3) v4 = v1 - 3 self.assertEqual(type(v4), type(v1)) self.assertEqual(v4['1'], -2) v5 = 3 - v1 self.assertEqual(type(v5), type(v1)) self.assertEqual(v5['1'], 2) # this won't work with other dimensions d = Distance("3m") self.assertRaises(IncompatibleUnitsError, d.__add__, 4) self.assertRaises(IncompatibleUnitsError, d.__radd__, 4) self.assertRaises(IncompatibleUnitsError, d.__sub__, 4) self.assertRaises(IncompatibleUnitsError, d.__rsub__, 4)
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_for_distance_equality(self): """Test that distances are only compared by length.""" d1 = Distance("1m") d2 = Distance("0.001km") self.assertEqual(d1['m'], d2['m']) # sanity check before the real test self.assertEqual(d1, d2)
def test_addition_and_subtraction_of_combined_units(self): d = Distance("10m") t = Time("5s") self.assertRaises(IncompatibleUnitsError, d.__add__, t) self.assertRaises(IncompatibleUnitsError, d.__sub__, t)
def test_consistency(self): """In its own units, the value should be 1.""" for unit in self.meters_in.keys(): d = Distance('1' + unit) # create "1x" where x is the unit self.assertEqual(d[unit], 1)