Example #1
0
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_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_ )
Example #3
0
def test_add_wrong_dimensions():
    q0 = DQ(1, {'a': 2})
    q1 = DQ(1, {'a': 1})
    q2 = DQ(1, {'b': 1})
    with pytest.raises(ValueError):
        q3 = q0 + q1
    with pytest.raises(ValueError):
        q3 = q0 + q2
Example #4
0
def test_non_dimensional():
    q0 = DQ()
    assert (q0.is_non_dimensional())
    assert (DQ.is_non_dimensional(q0))
    q1 = DQ(1, {'a': 0, 'b': 0})
    assert (q1.is_non_dimensional())
    q2 = DQ(1, {'a': 1, 'b': 0})
    assert (not q2.is_non_dimensional())
Example #5
0
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)
Example #6
0
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))
Example #7
0
def test_eq_non_dimensional_DQ():
    q0 = DQ(1)
    assert( q0 == 1 )
    q1 = DQ(1, {'a':0})
    assert( q1==1 )
    q2 = DQ(1, {'a':1})
    with pytest.raises(TypeError):
        a = (q2==1)
    with pytest.raises(NotImplementedError):
        a = (q1==q2)
Example #8
0
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))
Example #9
0
def test_register_prefix_LUT(setup_and_clean_DQ_with_basic_Translator):
    with pytest.raises(KeyError):
        q_fanta = DQ('1 qm')  # `m` is known, but prefix `q` not

    fantasy_prefix_LUT = {'q': 3.14}
    translator = Translator()
    translator.register_prefix_LUT(fantasy_prefix_LUT)
    DQ.register_translator(translator)

    assert (DQ('1 qm') == DQ('314 cm'))  # `c` prefix is still known
Example #10
0
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))
Example #11
0
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)
Example #12
0
def test_register_unit_LUT(setup_and_clean_DQ_with_basic_Translator):
    with pytest.raises(KeyError):
        q_fanta = DQ('1 fanta')  # this unit doesn't exist hence the KeyError

    fantasy_unit_LUT = {'fanta': DQ('2.54 cm')}
    translator = Translator()
    translator.register_unit_LUT(fantasy_unit_LUT)
    DQ.register_translator(translator)

    q_si = 2 * DQ('2.54 cm')
    q_fanta = DQ('2 fanta')  # now it does exist
    assert (q_si == q_fanta)
Example #13
0
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)
Example #14
0
def test_div_other_types():
    q0 = DQ(1)
    q1 = q0 / 2
    assert (q1.numeric == 0.5)
    q2 = DQ(4)
    q3 = 1 / q2
    q3_ = DQ(1 / 4)
    assert (q3.numeric == 0.25)
    assert (q3 == q3_)
    q4 = DQ(1, {'a': 2})
    q5 = 1 / q4
    q5_ = DQ(1, {'a': -2})
    assert (q5 == q5_)
Example #15
0
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)
Example #16
0
def test_register_prefix_LUT_override(
        setup_and_clean_DQ_with_basic_Translator):
    with pytest.raises(KeyError):
        q_fanta = DQ('1 qm')  # `m` is known, but prefix `q` not

    q_before_override = DQ('314 cm')

    fantasy_prefix_LUT = {'q': 3.14}
    translator = Translator()
    translator.register_prefix_LUT(fantasy_prefix_LUT, override=True)
    DQ.register_translator(translator)

    assert (DQ('1 qm') == q_before_override)

    with pytest.raises(KeyError):
        q_after_override = DQ('314 cm')
Example #17
0
def test_decorator_wrapping_cmp():
    q0 = DQ()

    assert (q0.__eq__.__name__ == '__eq__')
    assert (q0.__gt__.__name__ == '__gt__')
    assert (q0.__ge__.__name__ == '__ge__')
    assert (q0.__lt__.__name__ == '__lt__')
    assert (q0.__le__.__name__ == '__le__')
Example #18
0
def test_div_other_incompat_types():
    q0 = DQ(1)
    with pytest.raises(TypeError):
        q1 = q0 / [1]
    with pytest.raises(TypeError):
        q1 = (1, 2) / q0
    with pytest.raises(TypeError):
        q1 = q0 / {'a': 1}
Example #19
0
def test_mul_other_incompat_types():
    q0 = DQ(1)
    with pytest.raises(TypeError):
        q1 = q0 * [1]
    with pytest.raises(TypeError):
        q1 = (1, 2) * q0
    with pytest.raises(TypeError):
        q1 = q0 * {'a': 1}
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 create_many_instances():
    bigN = 1000
    for j in range(bigN):
        pint_m = Q_('{} m'.format(j))
    for j in range(bigN):
        pint_kg = Q_('{} kg'.format(j))
    for j in range(bigN):
        nu_m = j * nu.m
    for j in range(bigN):
        nu_kg = j * nu.kg
    for j in range(bigN):
        dq_m = DQ('{} m'.format(j))
    for j in range(bigN):
        dq_kg = DQ('{} kg'.format(j))
    for j in range(bigN):
        normal_int = int(j)
    for j in range(bigN):
        normal_string = '{} m'.format(j)
    for j in range(bigN):
        normal_dict = dict(j=j)
Example #22
0
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])
Example #23
0
def test_register_unit_LUT_override(setup_and_clean_DQ_with_basic_Translator):
    # the `setup_and_clean_DQ_with_basic_Translator` ensures
    # DQ doesn't suddenly come with weird LUTs preregistered
    # despite the following having been registered in a prev test;
    # in other words, this is supposed to test whether the teardown was executed correctly
    with pytest.raises(KeyError):
        q_fanta = DQ('1 fanta')  # this unit doesn't exist hence the KeyError

    pre_T = DQ._T
    q_si_before_overwritten = 2 * DQ('2.54 cm')

    fantasy_unit_LUT = {'fanta': DQ('2.54 cm')}
    translator = Translator()
    translator.register_unit_LUT(fantasy_unit_LUT, override=True)
    DQ.register_translator(translator)

    assert (id(pre_T) != DQ._T)  # since overwritten

    q_fanta = DQ('2 fanta')  # now it does exist
    assert (q_si_before_overwritten == q_fanta)

    with pytest.raises(KeyError):
        q_si_after_overwritten = DQ(
            '2.54 cm')  # no longer recognized because LUT is overwritten
Example #24
0
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)
Example #25
0
def test_dimensions_settings_new_id():
    q0 = DQ()
    d0 = D({'a': 1})
    q0.dimensions = d0
    assert (id(q0.dimensions) != id(d0))
Example #26
0
def test_register_unit_LUT_with_already_existing(
        setup_and_clean_DQ_with_basic_Translator):
    redundant_unit_LUT = {'m': DQ('100 cm')}
    translator = Translator()
    with pytest.raises(ValueError):
        translator.register_unit_LUT(redundant_unit_LUT)
Example #27
0
def test_pow_other_incompat_types(other):
    q0 = DQ(1)
    with pytest.raises(TypeError):
        q1 = q0**other
Example #28
0
def test_decorator_wrapping_linop():
    q0 = DQ()

    assert (q0.__add__.__name__ == '__add__')
    assert (q0.__sub__.__name__ == '__sub__')
Example #29
0
def test_add_wrong_type():
    q0 = DQ()
    with pytest.raises(TypeError):
        q1 = 1 + q0
    with pytest.raises(TypeError):
        q1 = q0 + 1
Example #30
0
def test_add_create_new_instances():
    q0 = DQ()
    q1 = DQ()
    q2 = q0 + q1

    assert (id(q0) != id(q2) and id(q1) != id(q2))