def test_subtract_different_unit(self): first = make_unit("first", self.dimension, 1) second = make_unit("second", self.dimension, 10) a = first(10) b = second(1) with self.assertRaises(OperationError): print(a - b)
def test_conversion(self): first = make_unit("first", self.dimension, 1) second = make_unit("second", self.dimension, 10) ten_ones = first(10) one_ten = second(1) # "is" assumes that test_flyweights passed self.assertIs(ten_ones.to_second(), one_ten)
def test_equal_different_dimension(self): dim2 = make_dimension("dim2") first = make_unit("first", self.dimension, 1) a = first(1) second = make_unit("second", dim2, 1) b = second(1) with self.assertRaises(ImplicitConversionError): print(a == b)
def test_unit_name_exponent(self): length = make_unit('Meters', self.dimension, 1) time = make_unit('Seconds', self.alt_dimension, 1) acceleration = make_compound_dimension("Acceleration", ((length, 1), (time, -2))) mpss = make_compound_unit(acceleration, 1) self.assertEqual("MetersPerSecondSquared", mpss.__name__)
def test_unit_name(self): length = make_unit('Meters', self.dimension, 1) time = make_unit('Seconds', self.alt_dimension, 1) velocity = make_compound_dimension("Velocity", ((length, 1), (time, -1))) mps = make_compound_unit(velocity, 1) self.assertEqual("MetersPerSecond", mps.__name__)
def test_multiply_simple_unit(self): compound = make_compound_unit(self.dimension, 1) simple = make_unit("simple", self.simple_b, 1) expected_unit = make_unit('expected', self.simple_a, 1) expected = expected_unit(2) result = compound(2) * simple(1) self.assertTrue(result.instance_of(expected_unit)) self.assertEqual(expected, result)
def test_flyweights(self): unit = make_unit("testunit", self.dimension, Decimal("2")) a = unit(1) b = unit(1) c = unit(2) d = unit(2) self.assertIs(a, b) self.assertIsNot(a, c) self.assertIs(c, d)
def test_isinstance(self): a = make_compound_dimension("baseline", ((self.dimA, 1), (self.dimB, -1))) b = make_compound_dimension("compare", ((self.dimA, 1), (self.dimB, -1))) unit_a = make_unit("fromA", a, 1) self.assertTrue(unit_a(1).instance_of(a)) self.assertTrue(unit_a(1).instance_of(b))
def test_multiply_scalar(self): unit = make_unit("unit", self.dimension, 1) one = unit(1) five = unit(5) self.assertIs(five, one * 5)
def test_equal_same_dimension(self): first = make_unit("first", self.dimension, 1) second = make_unit("second", self.dimension, 10) ten_ones = first(10) one_ten = second(1) self.assertEqual(ten_ones, one_ten)
def test_subtract_same_unit(self): unit = make_unit("unit", self.dimension, 1) one = unit(1) two = unit(2) three = unit(3) self.assertIs(two, three - one)
def setUp(self): self.first_dimension = make_dimension("TEST1") self.second_dimension = make_dimension("TEST2") self.unit_1a = make_unit("unit_1a", self.first_dimension, 1) self.unit_1b = make_unit("unit_1b", self.first_dimension, 10) self.unit_2 = make_unit("unit_2", self.second_dimension, 1)
from decimal import Decimal from base import make_unit, make_dimension Length = make_dimension("Length") meters = make_unit("meters", Length, 1) kilometers = make_unit("kilometers", Length, 1000) feet = make_unit("feet", Length, Decimal("0.3048"))
from decimal import Decimal from base import make_unit, make_dimension Time = make_dimension("Time") seconds = make_unit("seconds", Time, 1) minutes = make_unit("minutes", Time, 60) hours = make_unit("hours", Time, 60 * 60) days = make_unit("days", Time, 60 * 60 * 24) sidereal_days = make_unit("sidereal_days", Time, 60 * 60 * 23 + 60 * 56 + Decimal("4.091"))
def test_divide_scalar(self): unit = make_unit("unit", self.dimension, 1) two = unit(2) four = unit(4) self.assertIs(two, four / 2)
def setUp(self): self.dimA = make_dimension("FIRST") self.dimB = make_dimension("SECOND") self.unitA = make_unit("first", self.dimA, 1) self.unitB = make_unit("second", self.dimB, 1)
def test_add_same_unit(self): unit = make_unit("unit", self.dimension, 1) one = unit(1) two = unit(2) three = unit(3) self.assertIs(three, one + two)
from decimal import Decimal from base import make_dimension, make_unit Mass = make_dimension("Mass") kilograms = make_unit("kilograms", Mass, 1) grams = make_unit("grams", Mass, Decimal("0.001")) pounds = make_unit("pounds", Mass, Decimal("0.3732417216"))
def setUp(self): self.dimension = make_dimension("TEST") self.first = make_unit("first", self.dimension, 1) self.second = make_unit("second", self.dimension, 10)