Skip to content

Commit

Permalink
Expression: Add new test sub-module for finders and expr_visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange05 committed Aug 26, 2024
1 parent 6edebd6 commit 90e9fe8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
54 changes: 54 additions & 0 deletions loki/expression/tests/test_expr_visitors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# (C) Copyright 2018- ECMWF.
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

import pytest

from loki import Sourcefile
from loki.expression import FindVariables, FindTypedSymbols
from loki.frontend import available_frontends


@pytest.mark.parametrize('frontend', available_frontends())
def test_expression_finder_retrieval_function(frontend, tmp_path):
"""
Verify that expression finder visitors work as intended and remain
functional if re-used
"""
fcode = """
module some_mod
implicit none
contains
function some_func() result(ret)
integer :: ret
ret = 1
end function some_func
subroutine other_routine
integer :: var, tmp
var = 5 + some_func()
end subroutine other_routine
end module some_mod
""".strip()

source = Sourcefile.from_source(fcode, frontend=frontend, xmods=[tmp_path])

expected_ts = {'var', 'some_func'}
expected_vars = ('var',)

# Instantiate the first expression finder and make sure it works as expected
find_ts = FindTypedSymbols()
assert find_ts.visit(source['other_routine'].body) == expected_ts

# Verify that it works also on a repeated invocation
assert find_ts.visit(source['other_routine'].body) == expected_ts

# Instantiate the second expression finder and make sure it works as expected
find_vars = FindVariables(unique=False)
assert find_vars.visit(source['other_routine'].body) == expected_vars

# Make sure the first expression finder still works
assert find_ts.visit(source['other_routine'].body) == expected_ts
46 changes: 2 additions & 44 deletions loki/expression/tests/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from loki.backend import cgen, fgen
from loki.build import jit_compile, clean_test
from loki.expression import (
symbols as sym, FindVariables, FindExpressions, FindTypedSymbols,
FindInlineCalls, SubstituteExpressions, AttachScopesMapper, parse_expr
symbols as sym, FindVariables, FindExpressions, FindInlineCalls,
SubstituteExpressions, AttachScopesMapper, parse_expr
)
from loki.frontend import (
available_frontends, OFP, OMNI, FP, HAVE_FP, parse_fparser_expression
Expand Down Expand Up @@ -1450,48 +1450,6 @@ def test_expression_container_matching():
assert 'b(i)' in defaultdict(list, ((b, [a]),))


@pytest.mark.parametrize('frontend', available_frontends())
def test_expression_finder_retrieval_function(frontend, tmp_path):
"""
Verify that expression finder visitors work as intended and remain
functional if re-used
"""
fcode = """
module some_mod
implicit none
contains
function some_func() result(ret)
integer :: ret
ret = 1
end function some_func
subroutine other_routine
integer :: var, tmp
var = 5 + some_func()
end subroutine other_routine
end module some_mod
""".strip()

source = Sourcefile.from_source(fcode, frontend=frontend, xmods=[tmp_path])

expected_ts = {'var', 'some_func'}
expected_vars = ('var',)

# Instantiate the first expression finder and make sure it works as expected
find_ts = FindTypedSymbols()
assert find_ts.visit(source['other_routine'].body) == expected_ts

# Verify that it works also on a repeated invocation
assert find_ts.visit(source['other_routine'].body) == expected_ts

# Instantiate the second expression finder and make sure it works as expected
find_vars = FindVariables(unique=False)
assert find_vars.visit(source['other_routine'].body) == expected_vars

# Make sure the first expression finder still works
assert find_ts.visit(source['other_routine'].body) == expected_ts


@pytest.mark.parametrize('frontend', available_frontends())
def test_expression_c_de_reference(frontend):
"""
Expand Down

0 comments on commit 90e9fe8

Please sign in to comment.