def test_pow_create_new_instance_dimensions(): q0 = DQ(1, dimensions=D({'a': -1})) q1_exp = DQ(1, D({'a': -2})) q1_real = q0**2 assert (q1_exp.dimensions == q1_real.dimensions) assert (id(q0.dimensions) != id(q1_real.dimensions))
def test_sub_basics(): q0 = D({'a': 1, 'b': 0, 'c': 0}) q1 = D({'a': 0, 'b': 1, 'd': 0}) q2_exp = D({'a': 1, 'b': -1, 'c': 0, 'd': 0}) q2_real = q0 - q1 assert (q2_exp == q2_real)
def test_mul_numeric(): # __mul__ does make sense if the other is a number q0 = D({'a': 1, 'b': 0, 'c': 0}) q1 = -1 * q0 q0_ = D({'a': -1, 'b': 0, 'c': 0}) assert (q1 == q0_) q2 = 2 * q0 assert (q2['a'] == 2)
def test_add_basics(): q0 = D({'a': 1, 'b': 0, 'c': 0}) q1 = D({'a': 0, 'b': 1, 'd': 0}) q2_exp = D({'a': 1, 'b': 1, 'c': 0, 'd': 0}) q2_real = q0 + q1 assert (q2_exp == q2_real)
def test_ne(): q0 = DQ(1, D({'a':1, 'b':0, 'c':0})) q1 = DQ(2, D({'a':1, 'b':0, 'c':0})) assert(q0 != q1) with pytest.raises(TypeError): a = (q1!=2) q2 = DQ(3, D({'a':1})) with pytest.raises(NotImplementedError): a = (q0!=q2)
def test_mul_create_new_instance_dimensions(): q0 = DQ(2, dimensions=D({'a': -1})) q1 = DQ(3, dimensions=D({'a': -1})) q2_exp = DQ(6, D({'a': -2})) q2_real = q0 * q1 assert (q2_exp.dimensions == q2_real.dimensions) assert(id(q0.dimensions)!=id(q2_real.dimensions) \ and id(q1.dimensions)!=id(q2_real.dimensions))
def test_div_create_new_instance_dimensions(): q0 = DQ(1, dimensions=D({'a': -1})) q1 = DQ(2, dimensions=D({'a': -1})) q2_exp = DQ(0.5, D({'a': 0})) q2_real = q0 / q1 assert (q2_exp.dimensions == q2_real.dimensions) assert(id(q0.dimensions)!=id(q2_real.dimensions) \ and id(q1.dimensions)!=id(q2_real.dimensions))
def test_add_create_new_instance_dimensions(): q0 = DQ(dimensions=D({'a': -1})) q1 = DQ(dimensions=D({'a': -1})) q2 = q0 + q1 assert(q0.dimensions == q2.dimensions \ and q1.dimensions == q2.dimensions) assert(id(q0.dimensions)!=id(q2.dimensions) \ and id(q1.dimensions)!=id(q2.dimensions))
def test_numeric_value_settings(): with pytest.raises(TypeError): test = DQ(numeric='1', dimensions=D()) with pytest.raises(TypeError): test = DQ(numeric=(1, 1), dimensions=D()) with pytest.raises(TypeError): test = DQ(numeric=True, dimensions=D()) valid = DQ(numeric=1) valid = DQ(numeric=1.0) valid = DQ(numeric=1 + 2j)
def test_add_nomal_dict(): q0 = D({'a': 1, 'b': 0, 'c': 0}) q_wrong = { 'd': 1 } # this is a regular dict, not an instance of Dimensional q_exp = D({'a': 1, 'b': 0, 'c': 0, 'd': 1}) assert (q_exp == q0 + q_wrong ) # this is True because Dimensional can handle the wrong type assert (q_exp == q_wrong + q0) # same for __radd__
def test_rpow_basics(): q = DQ(2, D({'a': 1, 'b': -1})) # each dimension declared in q.dimensions has to be 0 in order for pow to make sense with pytest.raises(NotImplementedError): q_ = 2**q q1 = DQ(1, D({'a': 1, 'b': -1})) q2 = q / q1 dim_null = D({'a': 0, 'b': 0}) assert (q2.dimensions == dim_null) base = 2 value = base**q2 assert (value == 4)
def test_decorator_wrapping(): q0 = D({'a': 1, 'b': 0, 'c': 0}) assert (q0.__add__.__name__ == '__add__') assert (q0.__sub__.__name__ == '__sub__') assert (q0.__mul__.__name__ == '__mul__') assert (q0.__radd__.__name__ == '__radd__') assert (q0.__rsub__.__name__ == '__rsub__') assert (q0.__rmul__.__name__ == '__rmul__')
def multiplying_Q_many_times(): dq_1m = DQ(1, D({'L': 1})) pint_1m = 1 * ureg.meter nu_1m = 1 * nu.m normal_int = 1 for i in range(1000): dq_1m *= dq_1m pint_1m *= pint_1m nu_1m *= nu_1m normal_int *= normal_int
def test_ge(): q0 = DQ(2) q1 = DQ(1) assert(q0>=q1) assert(q0>=1) q2 = DQ(1, {'a':1}) with pytest.raises(TypeError): a = (q2>=0) with pytest.raises(NotImplementedError): a = (q0>=q2) q0.dimensions = D({'a':1}) assert(q0>=q2)
def test_le(): q0 = DQ(2) q1 = DQ(1) assert(q1<=q0) assert(q1<=2) q2 = DQ(1, {'a':1}) with pytest.raises(TypeError): a = (q2<=2) with pytest.raises(NotImplementedError): a = (q2<=q0) q0.dimensions = D({'a':1}) assert(q2<=q0)
def test_dimensions_settings(): valid = DQ(dimensions={'a': 1}) valid = DQ(dimensions=OrderedDict({'a': 1})) valid = DQ(dimensions=D({'a': 1})) with pytest.raises(TypeError): test = DQ(dimensions=1) with pytest.raises(TypeError): test = DQ(dimensions='1') with pytest.raises(TypeError): test = DQ(dimensions=(1, 2)) with pytest.raises(TypeError): test = DQ(dimensions=[1, 2])
def test_SI_dimensions(translator): # http://www.bipm.org/en/publications/si-brochure/section1-3.html assert (translator.translate('m').dimensions == D({'L': 1})) assert (translator.translate('s').dimensions == D({'t': 1}) ) # 't' instead of suggested (by SI) 'T' because assert (translator.translate('K').dimensions == D({'T': 1}) ) # the suggested \theta doesn't quite work, hence 'T' here. assert ( translator.translate('A').dimensions == D({'i': 1}) ) # lower case 'i' instead of upper 'I' to avoid confusion between (1, l, I) (one, lower L, upper i) assert (translator.translate('mol').dimensions == D({'N': 1})) assert (translator.translate('cd').dimensions == D({'J': 1})) assert (translator.translate('kg').dimensions == D({'M': 1}))
def test_mul(): # __mul__ doesn't make sense hence it isn't implemented q0 = D({'a': 1, 'b': 0, 'c': 0}) q1 = D({'a': 0, 'b': 1, 'd': 0}) with pytest.raises(TypeError): q2 = q0 * q1
# http://www.bipm.org/en/publications/si-brochure/section1-3.html assert (translator.translate('m').dimensions == D({'L': 1})) assert (translator.translate('s').dimensions == D({'t': 1}) ) # 't' instead of suggested (by SI) 'T' because assert (translator.translate('K').dimensions == D({'T': 1}) ) # the suggested \theta doesn't quite work, hence 'T' here. assert ( translator.translate('A').dimensions == D({'i': 1}) ) # lower case 'i' instead of upper 'I' to avoid confusion between (1, l, I) (one, lower L, upper i) assert (translator.translate('mol').dimensions == D({'N': 1})) assert (translator.translate('cd').dimensions == D({'J': 1})) assert (translator.translate('kg').dimensions == D({'M': 1})) @pytest.mark.parametrize('dim_string,expected_dimension', ( ('m', D({'L': 1})), ('m2', D({'L': 2})), ('m-2', D({'L': -2})), )) def test_SI_dimension_m_multiples(translator, dim_string, expected_dimension): q0 = translator.translate(dim_string).dimensions assert (q0 == expected_dimension) @pytest.mark.parametrize('string, expected_dimension', ( ('(kg.s)-2/((m3/K)-3/A2)', D({ 'M': -2.0, 't': -2.0, 'L': 9.0, 'T': -3.0,
def test_dimensions_settings_new_id(): q0 = DQ() d0 = D({'a': 1}) q0.dimensions = d0 assert (id(q0.dimensions) != id(d0))
def test_eq(): q0 = DQ(1, D({'a':1, 'b':0, 'c':0})) q1 = DQ(1, D({'a':1, 'b':0, 'c':0})) assert(q0 == q1)
if method(dimquant)==possibility: passed = True break return passed def test_dimquant_with_str_init(): q0 = DQ('1 m') q1 = DQ('1 s') q2 = DQ('1 m/s') assert( q2==(q0/q1) ) q2_ = DQ('1 m.s-1') assert( q2==q2_ ) @pytest.mark.parametrize('expected_strs,dq',( ( ('1 m.s-1','1 s-1.m'), DQ(1, D({'L': 1, 't': -1}))), ( ('2 m.s-1','2 s-1.m'), DQ(2, D({'L': 1, 't': -1}))), ( ('1 m2.s-1','1 s-1.m2'), DQ(1, D({'L': 2, 't': -1}))), )) def test_dimquant_str_out(expected_strs,dq): assert( accept_multiple_possibilities(expected_strs, dq, str) ) @pytest.mark.parametrize('expected_reprs,dq',( ( ('BaseDimQuant(1, Dimensional({\'L\': 1, \'t\': -1}))', 'BaseDimQuant(1, Dimensional({\'t\': -1, \'L\': 1}))'), BDQ(1, D({'L': 1, 't': -1}))), ( ('BaseDimQuant(2, Dimensional({\'L\': 1, \'t\': -1}))', 'BaseDimQuant(2, Dimensional({\'t\': -1, \'L\': 1}))'), BDQ(2, D({'L': 1, 't': -1}))),
def test_add_nondict(): q0 = D({'a': 1, 'b': 0, 'c': 0}) with pytest.raises(TypeError): q2 = q0 + 1 with pytest.raises(TypeError): q2 = 1 + q0
def test_sub_create_new_instances(): q0 = D({'a': 1, 'b': 0, 'c': 0}) q1 = D({'a': 0, 'b': 1, 'd': 0}) q2 = q0 - q1 assert (id(q0) != id(q2) and id(q1) != id(q2))
def test_sub_nondict(): q0 = D({'a': 1, 'b': 0, 'c': 0}) with pytest.raises(TypeError): q2 = q0 - 1 with pytest.raises(TypeError): q2 = 1 - q0
def test_DQ_default_init(): default = DQ() assert (default.numeric == 0) assert (default.dimensions == D())
def test_add_create_new_instance_dimensions(): q0 = DQ(dimensions=D({'a': -1})) q1 = DQ(dimensions=D({'a': -1})) q2 = q0 + q1 assert(q0.dimensions == q2.dimensions \ and q1.dimensions == q2.dimensions) assert(id(q0.dimensions)!=id(q2.dimensions) \ and id(q1.dimensions)!=id(q2.dimensions)) @pytest.mark.parametrize('summand0, summand1, expectation', ( (DQ(1, D({ 'a': 1, 'b': 0, 'c': 0 })), DQ(2, D({ 'a': 1, 'b': 0, 'c': 0 })), DQ(3, D({ 'a': 1, 'b': 0, 'c': 0 }))), (DQ(4, D({ 'a': 1, 'b': 1, 'c': 0 })), DQ(5, D({