From 4a52b4ac601dca228fe797676ca4d3c40d0bdf7b Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Tue, 8 Aug 2023 00:39:56 +0000 Subject: [PATCH 1/3] TST: Add basic pytest logic --- environment.yml | 3 ++ pytests/test_gjp_rec.py | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 pytests/test_gjp_rec.py diff --git a/environment.yml b/environment.yml index 0937659..07edbb1 100644 --- a/environment.yml +++ b/environment.yml @@ -17,6 +17,9 @@ dependencies: # Python, Sympy - ipython - sympy + - numpy - jupytext - papermill - ipykernel + # More testing + - pytest diff --git a/pytests/test_gjp_rec.py b/pytests/test_gjp_rec.py new file mode 100644 index 0000000..2542708 --- /dev/null +++ b/pytests/test_gjp_rec.py @@ -0,0 +1,62 @@ +import subprocess + +import numpy as np +import pytest + + +def extract_values(output_str): + lines = output_str.strip().split("\n") + roots = [] + weights = [] + for line in lines: + _, root, _, weight = line.split() + roots.append(float(root)) + weights.append(float(weight)) + return np.array(roots), np.array(weights) + + +def run_fortran(n, alpha, beta): + result = subprocess.run( + ["fpm", "run", "gjp_quad_rec", "--", str(n), str(alpha), str(beta)], + stdout=subprocess.PIPE, + ) + return extract_values(result.stdout.decode()) + + +def run_python(n, alpha, beta): + result = subprocess.run( + [ + "python", + "scripts/gen_analytic_vals.py", + "--npts", + str(n), + "--alpha", + str(alpha), + "--beta", + str(beta), + ], + stdout=subprocess.PIPE, + ) + return extract_values(result.stdout.decode()) + + +@pytest.mark.parametrize( + "n, alpha, beta", + [ + (3, 1.0, 5.0), + (5, 2.0, 3.0), + pytest.param( + 10, + 0.0, + 30.0, + marks=pytest.mark.xfail(reason="High beta values diverge"), + id="highbeta_fail", + ), + ], +) +def test_gjp_quad_rec(n, alpha, beta): + fortran_roots, fortran_weights = run_fortran(n, alpha, beta) + python_roots, python_weights = run_python(n, alpha, beta) + + assert np.allclose(fortran_roots, python_roots, atol=1e-14) + assert np.allclose(fortran_weights, python_weights, atol=1e-14) From beaf0453b96570629c5d5e5587143159caf3c8a8 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Tue, 8 Aug 2023 00:43:16 +0000 Subject: [PATCH 2/3] CI: Add basic testing to CI --- .github/workflows/build_test.yml | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/build_test.yml diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml new file mode 100644 index 0000000..189ed0e --- /dev/null +++ b/.github/workflows/build_test.yml @@ -0,0 +1,38 @@ +name: Basic consistency tests +on: + push: + branches: + - main + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + buildmamba: + runs-on: ${{ matrix.config.os }} + name: ${{ matrix.config.os }} + strategy: + fail-fast: false + matrix: + config: + - {os: macOS-latest} + - {os: ubuntu-latest} + steps: + - uses: actions/checkout@v3 + with: + submodules: "recursive" + fetch-depth: 0 + - name: Install Conda environment + uses: mamba-org/setup-micromamba@v1 + with: + environment-file: environment.yml + cache-environment: true + - name: Build and Test + shell: bash -l {0} + run: | + fpm build + pytest -vvv From 81bb33b302416e0871fc0d0ce8799ffaa32fe27a Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Tue, 8 Aug 2023 00:59:15 +0000 Subject: [PATCH 3/3] MAINT: Cleanup and format arguments to fortran --- pytests/test_gjp_rec.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pytests/test_gjp_rec.py b/pytests/test_gjp_rec.py index 2542708..90b1c74 100644 --- a/pytests/test_gjp_rec.py +++ b/pytests/test_gjp_rec.py @@ -17,7 +17,15 @@ def extract_values(output_str): def run_fortran(n, alpha, beta): result = subprocess.run( - ["fpm", "run", "gjp_quad_rec", "--", str(n), str(alpha), str(beta)], + [ + "fpm", + "run", + "gjp_quad_rec", + "--", + str(n), + "{:.1f}".format(alpha), + "{:.1f}".format(beta), + ], stdout=subprocess.PIPE, ) return extract_values(result.stdout.decode()) @@ -43,8 +51,8 @@ def run_python(n, alpha, beta): @pytest.mark.parametrize( "n, alpha, beta", [ - (3, 1.0, 5.0), - (5, 2.0, 3.0), + (3, 1, 5), + (5, 2, 3), pytest.param( 10, 0.0,