Skip to content

Commit

Permalink
test: rfc: refactor: run_float_fn -> run_float_fn_approx (#507)
Browse files Browse the repository at this point in the history
Uses `pytest.approx`, see
https://docs.pytest.org/en/stable/reference/reference.html#pytest-approx,
with optional configurable `rel` and `abs`.
  • Loading branch information
acl-cqc authored Dec 17, 2024
1 parent 02b6ab0 commit 82dba49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/integration/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

0 comments on commit 82dba49

Please sign in to comment.