Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fortran serialization codegen tests #198

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
511ae43
No dependency test created
ChristopherBignamini Apr 19, 2023
6d5034e
Missing dependency test added
ChristopherBignamini Apr 19, 2023
853c7bc
Added test for non existing granule input file
ChristopherBignamini Apr 20, 2023
d480745
Fix for source with no external dependencies
ChristopherBignamini Apr 27, 2023
173f2de
Serialization directives generation test created
ChristopherBignamini Apr 28, 2023
55d6591
Add small fixes
samkellerhals May 2, 2023
48ee72b
Serialization directives generation test created for diffusion granule
ChristopherBignamini May 3, 2023
43d4ce2
Useless import removed
ChristopherBignamini May 3, 2023
7e87416
Useless import removed
ChristopherBignamini May 3, 2023
9e60086
Merge branch 'fortran-serialisation' into fortran-serialization-codeg…
ChristopherBignamini May 3, 2023
6e9f16c
New interface adopted
ChristopherBignamini May 3, 2023
f3596a9
Codegen fixtures created
ChristopherBignamini May 4, 2023
6f106b5
Serialization directives reference file created
ChristopherBignamini May 4, 2023
1e71448
Fix tests
samkellerhals May 4, 2023
ba12eb5
Merge branch 'fortran-serialisation' into fortran-serialisation-cli-t…
ChristopherBignamini May 4, 2023
34fc8dc
Merge branch 'fortran-serialisation-cli-tests' of https://github.com/…
ChristopherBignamini May 4, 2023
adb2d4b
Merge branch 'fortran-serialisation-cli-tests' into fortran-serializa…
ChristopherBignamini May 5, 2023
645f312
Merge branch 'fortran-serialisation' into fortran-serialization-codeg…
ChristopherBignamini May 9, 2023
66952a0
Fix tests
samkellerhals May 9, 2023
e1859cb
Remove print statement
samkellerhals May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pyutils/src/icon4py/f2ser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ def _parse_derived_types(self, derived_types: dict) -> dict:
MissingDerivedTypeError: If the type definition for a derived type could not be found in any of the dependency files.
"""
derived_type_defs = {}
for dep in self.dependencies:
parsed = crack(dep)
for block in parsed["body"]:
if block["block"] == "type":
derived_type_defs[block["name"]] = block["vars"]
if self.dependencies:
for dep in self.dependencies:
parsed = crack(dep)
for block in parsed["body"]:
if block["block"] == "type":
derived_type_defs[block["name"]] = block["vars"]

for _, subroutine_vars in derived_types.items():
for _, intent_vars in subroutine_vars.items():
Expand Down
19 changes: 17 additions & 2 deletions pyutils/tests/f2ser/test_f2ser_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,32 @@ def test_cli_no_deps(no_deps_source_file, outfile, cli):
assert result.exit_code == 0


def test_cli_wrong_deps(diffusion_granule, samples_path, outfile, cli):
inp = str(diffusion_granule)
deps = [str(samples_path / "wrong_derived_types_example.f90")]
args = [inp, outfile, "-d", *deps]
result = cli.invoke(main, args)
assert result.exit_code == 2
assert "Invalid value for '--dependencies' / '-d'" in result.output


def test_cli_missing_deps(diffusion_granule, outfile, cli):
inp = str(diffusion_granule)
args = [inp, outfile]
result = cli.invoke(main, args)
assert isinstance(result.exception, MissingDerivedTypeError)


def test_cli_wrong_source(outfile, cli):
inp = str("foo.90")
args = [inp, outfile]
result = cli.invoke(main, args)
assert "Invalid value for 'GRANULE_PATH'" in result.output

def test_cli_missing_source(not_existing_diffusion_granule, outfile, cli):
inp = str(not_existing_diffusion_granule)
args = [inp, outfile]
result = cli.invoke(main, args)
error_search = result.stdout.find("Invalid value for 'GRANULE_PATH'")
assert error_search != -1
print(result.exception)
assert isinstance(result.exception, SystemExit)
assert "Invalid value for 'GRANULE_PATH'" in result.output
94 changes: 88 additions & 6 deletions pyutils/tests/f2ser/test_f2ser_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,100 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

import pytest

from icon4py.f2ser.deserialise import ParsedGranuleDeserialiser
from icon4py.f2ser.parse import GranuleParser
from icon4py.liskov.codegen.serialisation.generate import (
SerialisationCodeGenerator,
)
from icon4py.liskov.codegen.shared.types import GeneratedCode


def test_deserialiser_diffusion_codegen(diffusion_granule, diffusion_granule_deps):
parser = GranuleParser(diffusion_granule, diffusion_granule_deps)
parsed = parser()
deserialiser = ParsedGranuleDeserialiser(parsed, directory=".", prefix="test")
interface = deserialiser()
generator = SerialisationCodeGenerator(interface)
generated = generator()
parsed = GranuleParser(diffusion_granule, diffusion_granule_deps)()
interface = ParsedGranuleDeserialiser(parsed, directory=".", prefix="test")()
generated = SerialisationCodeGenerator(interface)()
assert len(generated) == 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add one test case for a fortran file which does have dependencies and where the dependencies include derived types so we can test they are being generated correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test_deserialiser_directives_diffusion_codegen test where I check if the serialization directives are correctly generated for the diffusion case, so the derived types serialization are included. I'm not sure this is what you expected, maybe I can create a smaller more specific test...

I have a question concerning the reference code fixtures: in the shorter case of the test_deserialiser_directives_no_deps_codegen test, where I check the generation of the serialization directives for a no-deps small source code, I create the GeneratedCode items and save them in a list, exactly as we do in the serialization code. In the diffusion case, instead, I saved the reference code in a file and then compare the file content to the generated code, after a conversion into a string of the GeneratedCode list (because creating the GeneratedCode instances is a bit complex giving the source code size). Should I unify the two approaches?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the small reference case it makes sense to define the generated code fixture like you did it. I agree that for more complex cases such as when defining the expected code for the diffusion granule, a better approach would be to define the expected code in an external file. Here a compromise would be to for example only check the first savepoint since otherwise the file would be quite large. I've modified the test to check for only the first savepoint, since anyhow that includes all the parts we want to test.


@pytest.fixture
def expected_no_deps_serialization_directives():
serialization_directives = [
GeneratedCode(
startln=12,
source="\n"
' !$ser init directory="." prefix="test"\n'
"\n"
" !$ser savepoint no_deps_init_in\n"
"\n"
" PRINT *, 'Serializing a=a'\n"
"\n"
" !$ser data a=a\n"
"\n"
" PRINT *, 'Serializing b=b'\n"
"\n"
" !$ser data b=b",
),
GeneratedCode(
startln=14,
source="\n"
" !$ser savepoint no_deps_init_out\n"
"\n"
" PRINT *, 'Serializing c=c'\n"
"\n"
" !$ser data c=c\n"
"\n"
" PRINT *, 'Serializing b=b'\n"
"\n"
" !$ser data b=b",
),
GeneratedCode(
startln=20,
source="\n"
" !$ser savepoint no_deps_run_in\n"
"\n"
" PRINT *, 'Serializing a=a'\n"
"\n"
" !$ser data a=a\n"
"\n"
" PRINT *, 'Serializing b=b'\n"
"\n"
" !$ser data b=b",
),
GeneratedCode(
startln=22,
source="\n"
" !$ser savepoint no_deps_run_out\n"
"\n"
" PRINT *, 'Serializing c=c'\n"
"\n"
" !$ser data c=c\n"
"\n"
" PRINT *, 'Serializing b=b'\n"
"\n"
" !$ser data b=b",
),
]
return serialization_directives


def test_deserialiser_directives_no_deps_codegen(
no_deps_source_file, expected_no_deps_serialization_directives
):
parsed = GranuleParser(no_deps_source_file)()
interface = ParsedGranuleDeserialiser(parsed, directory=".", prefix="test")()
generated = SerialisationCodeGenerator(interface)()
assert generated == expected_no_deps_serialization_directives


def test_deserialiser_directives_diffusion_codegen(
diffusion_granule, diffusion_granule_deps, samples_path
):
parsed = GranuleParser(diffusion_granule, diffusion_granule_deps)()
interface = ParsedGranuleDeserialiser(parsed, directory=".", prefix="test")()
generated = SerialisationCodeGenerator(interface)()
reference_savepoint = (
samples_path / "expected_diffusion_granule_savepoint.f90"
).read_text()
assert generated[0].source == reference_savepoint.rstrip()
Loading