def test_reallocation_heap(language):

    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()

    # TODO: check if we get the correct Pyccel warning
    g = epyccel(f, language=language)

    # Check result of pyccelized function
    assert f() == g()

    # Check that we got exactly 1 Pyccel warning
    assert errors.has_warnings()
    assert errors.num_messages() == 1

    # Check that we the warning is correct
    warning_info = [*errors.error_info_map.values()][0][0]
    assert warning_info.symbol  == 'x'
    assert warning_info.message == ARRAY_REALLOCATION
def test_creation_in_loop_heap(language):

    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()

    # TODO: check if we get the correct Pyccel warning
    g = epyccel(f, language=language)

    # Check result of pyccelized function
    assert f() == g()

    # Check that we got exactly 1 Pyccel warning
    assert errors.has_warnings()
    assert errors.num_messages() == 1

    # Check that we the warning is correct
    warning_info = [*errors.error_info_map.values()][0][0]
    assert warning_info.symbol  == 'x'
    assert warning_info.message == ARRAY_DEFINITION_IN_LOOP
Exemple #3
0
def test_semantic_warnings(f):

    pyccel = Parser(f, show_traceback=False)
    pyccel.parse()

    settings = {}
    pyccel.annotate(**settings)

    # reset Errors singleton
    errors = Errors()
    assert(errors.num_messages()!=0)
    errors.reset()
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_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
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
def test_Assign_Between_Allocatables():
    def f():
        import numpy as np
        x = np.zeros((3, 7), dtype=int)
        y = np.ones((4, 5), dtype=int)
        x = y
        x[0][0] = 1
        return y.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 == ASSIGN_ARRAYS_ONE_ANOTHER