예제 #1
0
 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
예제 #2
0
 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'])
예제 #3
0
 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")
예제 #4
0
 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()))
예제 #5
0
 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)
예제 #6
0
 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)
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
0
 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)