Skip to content

Commit

Permalink
#190 Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergisiso committed Dec 19, 2019
1 parent df49360 commit 4915eee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/psyclone/gocean1p0.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,9 +1466,18 @@ def append(self, name):
''' Append a GOKernelArgument to the Argument list.
:param str name: name of the appended argument.
:raises TypeError: if the given name is not a string.
'''
from psyclone.parse.algorithm import Arg
from psyclone.parse.kernel import Descriptor

if not isinstance(name, str):
raise TypeError(
"The name parameter given to GOKernelArguments.append method "
"should be a string, but found '{0}' instead.".
format(type(name).__name__))

descriptor = Descriptor(None, "") # Create a dummy descriptor
arg = Arg("variable", name, name)
argument = GOKernelArgument(descriptor, arg, self._parent_call)
Expand All @@ -1486,7 +1495,8 @@ def __init__(self, arg, arg_info, call):
@property
def type(self):
''' Return the type of this kernel argument - whether it is a field,
a scalar or a grid_property (to be supplied by the PSy layer) '''
a scalar or a grid_property (to be supplied by the PSy layer).
If it has no type it defaults to scalar.'''
if hasattr(self._arg, 'type'):
return self._arg.type
else:
Expand Down
60 changes: 58 additions & 2 deletions src/psyclone/tests/gocean1p0_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
from psyclone.parse.algorithm import parse
from psyclone.psyGen import PSyFactory, InternalError
from psyclone.generator import GenerationError, ParseError
from psyclone.gocean1p0 import GOKern, GOLoop, GOInvokeSchedule
from psyclone.gocean1p0 import GOKern, GOLoop, GOInvokeSchedule, \
GOKernelArgument, GOKernelArguments
from psyclone.tests.utilities import get_invoke
from psyclone.tests.gocean1p0_build import GOcean1p0Build
from psyclone.psyir.symbols import DataType
Expand Down Expand Up @@ -1479,6 +1480,62 @@ def test14_no_builtins():
assert "Built-ins are not supported for the GOcean" in str(excinfo.value)


def test_gokernelarguments_append():
''' Check the GOcean specialisation of KernelArguments append method'''

# Parse a file to get an initialised GOKernelsArguments object
_, invoke_info = parse(os.path.join(os.path.
dirname(os.path.
abspath(__file__)),
"test_files", "gocean1p0",
"single_invoke.f90"),
api=API)
psy = PSyFactory(API).create(invoke_info)
invoke = psy.invokes.invoke_list[0]
kernelcall = invoke.schedule.coded_kernels()[0]
argument_list = kernelcall.arguments
assert isinstance(argument_list, GOKernelArguments)

# Try append a non-string value
with pytest.raises(TypeError) as err:
argument_list.append(3)
assert "The name parameter given to GOKernelArguments.append method " \
"should be a string, but found 'int' instead." in str(err.value)

# Append strings
argument_list.append("var1")
argument_list.append("var2")

assert isinstance(kernelcall.args[-1], GOKernelArgument)
assert isinstance(kernelcall.args[-2], GOKernelArgument)
assert kernelcall.args[-1].name == "var2"
assert kernelcall.args[-2].name == "var1"

# And the generated code looks as expected
generated_code = str(psy.gen)
assert "CALL compute_cu_code(i, j, cu_fld%data, p_fld%data, u_fld%data," \
" var1, var2)" in generated_code


def test_gokernelargument_type():
''' Check the type property of the GOKernelArgument'''
from psyclone.parse.algorithm import Arg
from psyclone.parse.kernel import Descriptor
from psyclone.psyGen import Node

# Create a dummy GOKernelArgument
descriptor = Descriptor(None, "")
arg = Arg("variable", "arg", "arg")
argument = GOKernelArgument(descriptor, arg, Node())

# If the descriptor does not have a type it defaults to 'scalar'
assert argument.type == "scalar"

# Otherwise it return the descriptor type
argument._arg.type = "descriptor_type" # Mock the descriptor type method
assert argument.type == "descriptor_type"


def test_kernelglobalstoarguments_wrongapi():
''' Check the KernelGlobalsToArguments with an API other than GOcean1p0'''
from psyclone.transformations import KernelGlobalsToArguments, \
Expand Down Expand Up @@ -1515,7 +1572,6 @@ def test_kernelglobalstoarguments(monkeypatch, tmpdir):
" in the kernel call."

# Construct a testing InvokeSchedule

_, invoke_info = parse(os.path.join(os.path.
dirname(os.path.abspath(__file__)),
"test_files", "gocean1p0",
Expand Down

0 comments on commit 4915eee

Please sign in to comment.