From 82dba49362932967c0dda8b0f4baf5dc7174b836 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 17 Dec 2024 09:26:26 +0000 Subject: [PATCH] test: rfc: refactor: run_float_fn -> run_float_fn_approx (#507) Uses `pytest.approx`, see https://docs.pytest.org/en/stable/reference/reference.html#pytest-approx, with optional configurable `rel` and `abs`. --- tests/integration/conftest.py | 20 ++++++++++++++++++-- tests/integration/test_arithmetic.py | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 282221fd..b7da6da1 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -97,6 +97,22 @@ def f(module: ModulePointer, expected: Any, fn_name: str = "main"): def run_int_fn(): return _run_fn("run_int_function") + @pytest.fixture -def run_float_fn(): - return _run_fn("run_float_function") +def run_float_fn_approx(): + """Like run_int_fn, but takes optional additional parameters `rel`, `abs` and `nan_ok` + as per `pytest.approx`.""" + run_fn = _run_fn("run_float_function") + + def run_approx( + hugr: Package, + expected: float, + fn_name: str = "main", + *, + rel: float | None = None, + abs: float | None = None, + nan_ok: bool = False, + ): + return run_fn(hugr, pytest.approx(expected, rel=rel, abs=abs, nan_ok=nan_ok)) + + return run_approx diff --git a/tests/integration/test_arithmetic.py b/tests/integration/test_arithmetic.py index d8bbc348..5a8448aa 100644 --- a/tests/integration/test_arithmetic.py +++ b/tests/integration/test_arithmetic.py @@ -251,7 +251,7 @@ def run_rem() -> int: run_int_fn(hugr, expected=2, fn_name="run_rem") -def test_angle_exec(validate, run_float_fn): +def test_angle_exec(validate, run_float_fn_approx): module = GuppyModule("test_angle_exec") module.load_all(angles) @@ -266,4 +266,4 @@ def main() -> float: hugr = module.compile() validate(hugr) import math - run_float_fn(hugr, expected=pytest.approx(-6 * math.pi)) + run_float_fn_approx(hugr, expected=-6 * math.pi)