def test_good_named_add_w_mult(): """A quantity with a named composed unit that carries a multiplier should add to a composed unit that has a multiplier""" mile = unit('mi').composed_unit kilometre = unit('km') assert within_epsilon(Quantity(1, mile) + Quantity(1, kilometre), Quantity(1, kilometre) + Quantity(1, mile)) assert within_epsilon(Quantity(2609.344, unit('m')), Quantity(1, kilometre) + Quantity(1, mile))
def test_good_named_add_w_mults(): """Two quantities with compatible but differently-named and differently-multiplied units should add together.""" mile = unit('mi') kilometre = unit('km') assert within_epsilon(Quantity(1, mile) + Quantity(1, kilometre), Quantity(1, kilometre) + Quantity(1, mile)) assert within_epsilon(Quantity(2609.344, unit('m')), Quantity(1, kilometre) + Quantity(1, mile))
def test_good_named_add_w_mult(): """A quantity with a named composed unit that carries a multiplier should add to a composed unit that has a multiplier""" mile = unit('mi').composed_unit kilometre = unit('km') assert within_epsilon( Quantity(1, mile) + Quantity(1, kilometre), Quantity(1, kilometre) + Quantity(1, mile)) assert within_epsilon(Quantity(2609.344, unit('m')), Quantity(1, kilometre) + Quantity(1, mile))
def test_good_named_add_w_mults(): """Two quantities with compatible but differently-named and differently-multiplied units should add together.""" mile = unit('mi') kilometre = unit('km') assert within_epsilon( Quantity(1, mile) + Quantity(1, kilometre), Quantity(1, kilometre) + Quantity(1, mile)) assert within_epsilon(Quantity(2609.344, unit('m')), Quantity(1, kilometre) + Quantity(1, mile))
def test_good_sub_w_mults(): """Two quantities with compatible units should sub together even when they have different multipliers""" mile = unit('mi').composed_unit kilometre = unit('km').composed_unit m_on_left = Quantity(1, mile) - Quantity(1, kilometre) km_on_left = Quantity(1, kilometre) - Quantity(1, mile) m_on_left_diff = Quantity(609.344, unit('m')) km_on_left_diff = Quantity(-609.344, unit('m')) assert within_epsilon(m_on_left, m_on_left_diff) assert within_epsilon(km_on_left, km_on_left_diff) assert within_epsilon(m_on_left_diff, -km_on_left_diff)
def test_pow(): """Exponentiation of quantities.""" REGISTRY.clear() define_units() m_unit = unit('m') m_quant = Quantity(2, m_unit) assert (m_quant ** 2 == m_quant * m_quant == pow(m_quant, 2)) cm_unit = unit('cm') cm_quant = Quantity(2, cm_unit) assert within_epsilon(cm_quant ** 2, cm_quant * cm_quant) assert within_epsilon(cm_quant ** 2, pow(cm_quant, 2))
def test_named_pickle(): """Pickling is reversible on quantities backed by named units""" named = named_unit('S', 'N', 'D') arbitrary_quantity = named(3.0) pickled = dumps(arbitrary_quantity) unpickled = loads(pickled) assert within_epsilon(arbitrary_quantity, unpickled)
def test_composed_pickle(): """Pickling is reversible on quantities backed by composed units""" arbitrary = unit('arbitrary') arbitrary_quantity = arbitrary(3.0) * arbitrary(3.0) pickled = dumps(arbitrary_quantity) unpickled = loads(pickled) assert within_epsilon(arbitrary_quantity, unpickled)
def test_valid_composed_to_composed(): """Valid composed units in terms of others.""" metric_vel = unit('km') / unit('h') imp_vel = unit('mi') / unit('h') highway_kph = Quantity(100, metric_vel) highway_mph = Quantity(62.1371192237334, imp_vel) assert str(highway_kph) == '100 km / h' assert str(highway_mph) == '62.1371192237 mi / h' assert within_epsilon(imp_vel(highway_kph), highway_mph) assert str(imp_vel(highway_kph)) == '62.1371192237 mi / h'
def test_valid_composed_to_composed(): """Valid composed units in terms of others.""" metric_vel = unit('km') / unit('h') imp_vel = unit('mi') / unit('h') highway_kph = Quantity(100, metric_vel) highway_mph = Quantity(62.1371192237334, imp_vel) assert str(highway_kph) == '100.00 km / h' assert str(highway_mph) == '62.14 mi / h' assert within_epsilon(imp_vel(highway_kph), highway_mph) assert str(imp_vel(highway_kph)) == '62.14 mi / h'
def test_good_add_w_mults(): """Two quantities with compatible units should add together even when they have different multipliers""" mile = ComposedUnit([unit('m')], [], multiplier=1609.344) kilometre = ComposedUnit([unit('m')], [], multiplier=1000) m_on_left = Quantity(1, mile) + Quantity(1, kilometre) km_on_left = Quantity(1, kilometre) + Quantity(1, mile) manual_sum = Quantity(2609.344, unit('m')) assert within_epsilon(m_on_left, km_on_left) assert within_epsilon(km_on_left, m_on_left) assert within_epsilon(manual_sum, m_on_left) assert within_epsilon(manual_sum, km_on_left) assert within_epsilon(m_on_left, manual_sum) assert within_epsilon(km_on_left, manual_sum)