Exemplo n.º 1
0
def test_array_real_2d_F_div():

    f1 = arrays.array_real_2d_F_div
    f2 = epyccel(f1)

    x1 = np.array([[1., 2., 3.], [4., 5., 6.]])
    x2 = x1.copy()
    a = np.array([[-1., -2., -3.], [-4., -5., -6.]])

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 2
0
def test_array_real_2d_F_scalar_mul():

    f1 = arrays.array_real_2d_F_scalar_mul
    f2 = epyccel(f1)

    x1 = np.array([[1., 2., 3.], [4., 5., 6.]])
    x2 = x1.copy()
    a = 5.

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 3
0
def test_array_int_1d_add():

    f1 = arrays.array_int_1d_add
    f2 = epyccel(f1)

    x1 = np.array([1, 2, 3], dtype=np.int32)
    x2 = x1.copy()
    a = np.array([1, 2, 3], dtype=np.int32)

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 4
0
def test_array_real_1d_scalar_sub():

    f1 = arrays.array_real_1d_scalar_sub
    f2 = epyccel(f1)

    x1 = np.array([1., 2., 3.])
    x2 = x1.copy()
    a = 5.

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 5
0
def test_array_real_1d_div():

    f1 = arrays.array_real_1d_div
    f2 = epyccel(f1)

    x1 = np.array([1., 2., 3.])
    x2 = x1.copy()
    a = np.array([1., 2., 3.])

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 6
0
def test_array_int_2d_F_idiv():

    f1 = arrays.array_int_2d_F_idiv
    f2 = epyccel(f1)

    x1 = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
    x2 = x1.copy()
    a = np.array([[-1, -2, -3], [-4, -5, -6]], dtype=np.int32)

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 7
0
def test_array_int_2d_F_scalar_mul():

    f1 = arrays.array_int_2d_F_scalar_mul
    f2 = epyccel(f1)

    x1 = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
    x2 = x1.copy()
    a = 5

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 8
0
def test_call_fdiv_i_i(language):
    @types(int, int)
    def fdiv_i_i(x, y):
        return x // y

    f = epyccel(fdiv_i_i, language=language)
    x = randint(1e9)
    y = randint(low=1, high=1e3)

    assert (f(x, y) == fdiv_i_i(x, y))
    assert (f(-x, y) == fdiv_i_i(-x, y))
    assert (f(x, -y) == fdiv_i_i(x, -y))
    assert (f(-x, -y) == fdiv_i_i(-x, -y))
Exemplo n.º 9
0
def test_array_real_2d_C_scalar_div():

    f1 = arrays.array_real_2d_C_scalar_div
    f2 = epyccel(f1)

    x1 = np.array([[1., 2., 3.], [4., 5., 6.]])
    x2 = np.copy(x1)
    a = 5.

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 10
0
def test_array_int32_1d_sub():

    f1 = arrays.array_int32_1d_sub
    f2 = epyccel(f1)

    x1 = np.array([1, 2, 3], dtype=np.int32)
    x2 = np.copy(x1)
    a = np.array([1, 2, 3], dtype=np.int32)

    f1(x1, a)
    f2(x2, a)

    assert np.array_equal(x1, x2)
Exemplo n.º 11
0
def test_arguments_f10():
    @types('int64[:]')
    def f10(x):
        x[:] += 1

    f = epyccel(f10)

    x = np.zeros(10, dtype='int64')
    x_expected = x.copy()

    f10(x)
    f(x_expected)
    assert np.array_equal(x, x_expected)
Exemplo n.º 12
0
def test_call_div_i_i(language):
    @types(int, int)
    def div_i_i(x, y):
        return x / y

    f = epyccel(div_i_i, language=language)
    x = randint(1e9)
    y = randint(low=1, high=1e3)

    assert isclose(f(x, y), div_i_i(x, y), rtol=1e-14, atol=1e-15)
    assert isclose(f(-x, y), div_i_i(-x, y), rtol=1e-14, atol=1e-15)
    assert isclose(f(x, -y), div_i_i(x, -y), rtol=1e-14, atol=1e-15)
    assert isclose(f(-x, -y), div_i_i(-x, -y), rtol=1e-14, atol=1e-15)
Exemplo n.º 13
0
def test_pow_int_int(language):
    @types(int, int)
    def f_call(x, y):
        return x**y

    f = epyccel(f_call, language=language)
    x = randint(50)
    y = randint(5)

    assert f(x, y) == f_call(x, y)
    # negative base
    assert f(-x, y) == f_call(-x, y)

    assert isinstance(f(x, y), type(f_call(x, y)))
Exemplo n.º 14
0
def test_loop_on_real_array():

    f1 = loops.product_loop_on_real_array
    f2 = epyccel(f1)

    z1 = np.ones(11)
    out1 = np.empty_like(z1)
    z2 = z1.copy()
    out2 = out1.copy()

    f1(z1, out1)
    f2(z2, out2)

    assert np.array_equal(out1, out2)
Exemplo n.º 15
0
def test_trunc_call_int(language):  # trunc
    @types('int')
    def trunc_call(x):
        from math import trunc
        return trunc((x))

    f1 = epyccel(trunc_call, language=language)
    high = 10000
    x = randint(high)

    # positive number
    assert (trunc_call(x) == f1(x))
    # Negative number
    assert (trunc_call(-x) == f1(-x))
Exemplo n.º 16
0
def test_erfc_phrase(language):
    @types('real', 'real')
    def erfc_phrase(x, y):
        from math import erfc
        a = erfc(x) + erfc(y)
        return a

    f2 = epyccel(erfc_phrase, language=language)

    # Domain ]-inf, +inf[
    x = uniform(high=max_float)
    y = uniform(high=max_float)
    assert (isclose(f2(x, y), erfc_phrase(x, y), rtol=1e-14, atol=1e-15))
    assert (isclose(f2(-x, -y), erfc_phrase(-x, -y), rtol=1e-14, atol=1e-15))
Exemplo n.º 17
0
def test_asinh_call(language):
    @types('real')
    def asinh_call(x):
        from math import asinh
        return asinh(x)

    f1 = epyccel(asinh_call, language=language)

    x = uniform(high=max_float)
    assert (isclose(f1(x), asinh_call(x), rtol=1e-14, atol=1e-15))
    assert isinstance(f1(x), type(asinh_call(x)))

    # Negative value
    assert (isclose(f1(-x), asinh_call(-x), rtol=1e-14, atol=1e-15))
def test_reallocation_stack(language):
    @stack_array('x')
    def f():
        import numpy as np
        x = np.zeros((3, 7), dtype=int)
        x = np.ones((4, 5), dtype=int)
        return x.sum()

    # Initialize singleton that stores Pyccel errors
    errors = Errors()

    # epyccel should raise an Exception
    with pytest.raises(PyccelSemanticError):
        epyccel(f, language=language)

    # Check that we got exactly 1 Pyccel error
    assert errors.has_errors()
    assert errors.num_messages() == 1

    # Check that the error is correct
    error_info = [*errors.error_info_map.values()][0][0]
    assert error_info.symbol == 'x'
    assert error_info.message == INCOMPATIBLE_REDEFINITION_STACK_ARRAY
def test_creation_in_loop_stack(language):
    @stack_array('x')
    def f():
        import numpy as np
        for i in range(3):
            x = np.ones(i, dtype=int)
        return x.sum()

    # Initialize singleton that stores Pyccel errors
    errors = Errors()

    # epyccel should raise an Exception
    with pytest.raises(PyccelSemanticError):
        epyccel(f, language=language)

    # Check that we got exactly 1 Pyccel error
    assert errors.has_errors()
    assert errors.num_messages() == 1

    # Check that the error is correct
    error_info = [*errors.error_info_map.values()][0][0]
    assert error_info.symbol == 'x'
    assert error_info.message == STACK_ARRAY_DEFINITION_IN_LOOP
Exemplo n.º 20
0
def test_array_int32_in_bool_out_2d_F_complex_3d_expr():

    f1 = arrays.array_int32_in_bool_out_2d_F_complex_3d_expr
    f2 = epyccel(f1)

    x = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32, order='F')
    a = np.array([[-1, -2, -3], [-4, -5, -6]], dtype=np.int32, order='F')
    r1 = np.empty((2, 3), dtype=bool, order='F')
    r2 = np.copy(r1)

    f1(x, a, r1)
    f2(x, a, r2)

    assert np.array_equal(r1, r2)
Exemplo n.º 21
0
def test_array_int32_in_bool_out_1d_complex_3d_expr():

    f1 = arrays.array_int32_in_bool_out_1d_complex_3d_expr
    f2 = epyccel(f1)

    x = np.array([1, 2, 3], dtype=np.int32)
    a = np.array([-1, -2, -3], dtype=np.int32)
    r1 = np.empty(3, dtype=bool)
    r2 = np.copy(r1)

    f1(x, a, r1)
    f2(x, a, r2)

    assert np.array_equal(r1, r2)
Exemplo n.º 22
0
def test_input_output_matching_types(language):
    @types('float32', 'float32')
    def add_real(a, b):
        c = a + b
        return c

    fflags = "-Werror -Wconversion"
    if language == "fortran":
        fflags = fflags + "-extra"
    if platform.system() == 'Darwin' and language == 'c':  # If macosx
        fflags = fflags + " -Wno-error=unused-command-line-argument"
    epyc_add_real = epyccel(add_real, fflags=fflags, language=language)

    assert (add_real(1.0, 2.0) == epyc_add_real(1.0, 2.0))
Exemplo n.º 23
0
def test_f1(language):
    @types('int')
    def f1(x = None):
        if x is None :
            return 5
        return x + 5

    f = epyccel(f1, language = language)

    # ...
    assert f(2) == f1(2)
    assert f() == f1()
    assert f(None) == f1(None)
    assert f(0) == f1(0)
Exemplo n.º 24
0
def test_f4(language):
    @types('bool')
    def f4(x = None):
        if x is None :
            return True
        return False

    f = epyccel(f4, language = language)

    # ...
    assert f(True) == f4(True)
    assert f() == f4()
    assert f(None) == f4(None)
    assert f(False) == f4(False)
def test_Reassign_to_Target():
    def f():
        import numpy as np
        x = np.zeros((3, 7), dtype=int)
        c = x
        x = np.ones((4, 5), dtype=int)
        return c.sum()

    # Initialize singleton that stores Pyccel errors
    errors = Errors()

    # epyccel should raise an Exception
    with pytest.raises(PyccelSemanticError):
        epyccel(f)

    # Check that we got exactly 1 Pyccel error
    assert errors.has_errors() == 1
    assert errors.num_messages() == 1

    # Check that the error is correct
    error_info = [*errors.error_info_map.values()][0][0]
    assert error_info.symbol == 'x'
    assert error_info.message == ARRAY_ALREADY_IN_USE
Exemplo n.º 26
0
def test_f2(language):
    @types('real')
    def f2(x = None):
        if x is None :
            return 2.5
        return x + 2.5

    f = epyccel(f2, language = language)

    # ...
    assert f(2.0) == f2(2.0)
    assert f() == f2()
    assert f(None) == f2(None)
    assert f(0.0) == f2(0.0)
Exemplo n.º 27
0
def test_mix_types_1(language):
    f1 = epyccel(mod2.mix_types_1, language=language, verbose=True)
    f2 = mod2.mix_types_1

    assert f1(complex(1, 2), 15, np.int16(5)) == f2(complex(1, 2), 15,
                                                    np.int16(5))
    assert f1(complex(1, 2), 15, True) == f2(complex(1, 2), 15, True)
    assert f1(complex(1, 2), 7.0, np.int16(5)) == f2(complex(1, 2), 7.0,
                                                     np.int16(5))
    assert f1(complex(1, 2), 7.0, False) == f2(complex(1, 2), 7.0, False)
    assert f1(15, 14, np.int16(2012)) == f2(15, 14, np.int16(2012))
    assert f1(15, 14, True) == f2(15, 14, True)
    assert f1(15, 7.0, np.int16(2012)) == f2(15, 7.0, np.int16(2012))
    assert f1(15, 14, False) == f2(15, 14, False)
Exemplo n.º 28
0
def test_f4(language):
    @types('bool')
    def f4(x = True):
        if x:
            return 1
        else:
            return 2

    f = epyccel(f4, language = language)

    # ...
    assert f(True)  == f4(True)
    assert f(False) == f4(False)
    assert f()      == f4()
Exemplo n.º 29
0
def test_complex_types(language):
    f1 = epyccel(mod2.complex_types, language=language, verbose=True)
    f2 = mod2.complex_types

    assert f1(complex(1, 2.2), complex(1, 2.2)) == f2(complex(1, 2.2),
                                                      complex(1, 2.2))
    assert f1(np.complex(15.5, 2.0),
              np.complex(10.5, 3.4)) == f2(np.complex(15.5, 2.0),
                                           np.complex(10.5, 3.4))
    assert f1(np.complex64(15.5 + 2.0j),
              np.complex64(10.5 + 3.4j)) == f2(np.complex64(15.5 + 2.0j),
                                               np.complex64(10.5 + 3.4j))
    assert f1(np.complex128(15.5 + 2.0j),
              np.complex(10.5 + 3.4j)) == f2(np.complex128(15.5 + 2.0j),
                                             np.complex(10.5 + 3.4j))
Exemplo n.º 30
0
def test_or_boolean(language):
    @types('bool', 'bool')
    def or_bool(a, b):
        c = False
        if (a):
            c = True
        if (b):
            c = True
        return c

    epyc_or_bool = epyccel(or_bool, language=language)

    assert (epyc_or_bool(True, True) == or_bool(True, True))
    assert (epyc_or_bool(True, False) == or_bool(True, False))
    assert (epyc_or_bool(False, False) == or_bool(False, False))