Skip to content

Commit

Permalink
tests: Prepare testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Aug 13, 2021
1 parent dd1cb73 commit f1e3502
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/runtime/pureFortran/tests/cmake.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project({{ test_name }} Fortran C)

add_executable({{ test_name }}
{{ test_files|join('\n ') }}
)
10 changes: 10 additions & 0 deletions src/runtime/pureFortran/tests/gen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[trig]] # [name of lfortran_MODULE.f90]
fname = "sin" # Function to generate test for; must be the same as for gfortran
only = "dsin" # if not testing same function
range = 20 # [-range, +range]
stepsize = 0.01

[[trig]]
fname = "cos" # Function to generate test for
range = 20
stepsize = 0.01
60 changes: 60 additions & 0 deletions src/runtime/pureFortran/tests/gen_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

import shutil, os
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory
import subprocess

import click
import tomli

from jinja2 import Environment, FileSystemLoader

@click.command()
@click.option('--compiler', default ='lfortran', prompt='Compiler',
help='The compiler used')
@click.option('--execute/--no-execute', default=False)
@click.option('--plot/--no-plot', default=False)
@click.option('--build/--no-build', default=False)
@click.option('--genfiles', is_flag=True)
# TODO: Make groups
# TODO: Use temporary files

def mkfunc(compiler, genfiles, execute, plot, build):
"""Generate tests"""
template_loader = FileSystemLoader('./')
env = Environment(loader=template_loader)
buildtemplate = env.get_template("cmake.jinja")
tests_dict = tomli.loads(Path("gen.toml").read_bytes().decode())
execenv = os.environ.copy()
execenv["FC"]=compiler
for mod, ftests in tests_dict.items():
testtemplate = env.get_template(f"{mod}.f90.jinja")
lfmn = f"lfortran_intrinsic_{mod}.f90"
lfmod = Path(Path.cwd().parent / lfmn ).absolute()
moddirname = Path(Path.cwd() / f"gentests/{mod}" ).absolute()
Path.mkdir(moddirname, parents=True, exist_ok=True)
for func in ftests:
funcdirname = moddirname / func['fname']
Path.mkdir(funcdirname, exist_ok=True)
shutil.copy(lfmod, funcdirname)
fn = f"{func['fname']}_test.f90"
test_data = {
'test_name': f"{func['fname']}_test",
'test_files': [lfmn, fn ]
}
func['compiler'] = compiler
Path.write_text(funcdirname/fn, testtemplate.render(func))
Path.write_text(funcdirname/'CMakeLists.txt', buildtemplate.render(test_data))
subprocess.Popen(['cmake', '.'], env=execenv, cwd=funcdirname).wait()
if build==True:
subprocess.Popen(['cmake', '--build', '.'], env=execenv, cwd=funcdirname).wait()
if execute==True:
with open(str(funcdirname.absolute())+f"/{compiler}_{func['fname']}_output.dat", "w") as res:
subprocess.Popen([f"./{test_data['test_name']}"], env=execenv, cwd=funcdirname, stdout=res).wait()
if plot==True:
pass


if __name__ == '__main__':
mkfunc()
16 changes: 16 additions & 0 deletions src/runtime/pureFortran/tests/trig.f90.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
program {{ fname }}_test
use, intrinsic :: iso_fortran_env, only: sp => real32, dp => real64
use :: lfortran_intrinsic_trig, only: {{ fname }}2 => {{ fname }}
real(dp), parameter :: pi = 3.1415926535897932384626433832795_dp
real(dp) :: ir, gf, lf
print*, "Compiled with {{ compiler }}"
print*, "x ", "gfortran_{{fname}}(x) ", &
"lfortan_{{fname}}(x) ", "abs_error "
ir = -{{ range }}
do while (ir<={{ range }})
gf = {{ fname }}(ir)
lf = {{ fname }}2(ir)
print*, ir, gf, lf, abs(gf-lf)
ir = ir + {{ stepsize }}
end do
end program

0 comments on commit f1e3502

Please sign in to comment.