Exemple #1
0
 def test_string_output(self):
     """Test time output in string format."""
     t = Time("1 min")
     self.assertEqual(t.str, "01:00")
     t = Time("60 s")
     self.assertEqual(t.str, "01:00")
     t = Time("3661 s")
     self.assertEqual(t.str, "1:01:01")
     t = Time("0.1 s")
     self.assertEqual(t.str, "00:00")
Exemple #2
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)
Exemple #3
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'])
Exemple #4
0
 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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
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
Exemple #8
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")
Exemple #9
0
 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)
Exemple #10
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)
Exemple #11
0
 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)
Exemple #12
0
 def test_consistency(self):
     """In its own units, the value should be 1."""
     for unit in self.seconds_in.keys():
         t = Time('1' + unit) # create "1x" where x is the unit
         self.assertEqual(t[unit], 1)
Exemple #13
0
 def test_create_simple_times(self):
     """Simple times."""
     for unit,seconds in self.seconds_in.iteritems():
         t = Time('1' + unit) # create "1x" where x is the unit
         self.assertEqual(t['s'], seconds) # the seconds should be correct