forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ') }} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |