From 0f36747c05c15f734e86f920a099ffbc0fa8b5a9 Mon Sep 17 00:00:00 2001 From: Alden Date: Mon, 13 Feb 2023 01:44:14 -0500 Subject: [PATCH] parametrize tests --- test/hypothesis/error_hunt.py | 152 ++++++++++++---------------------- 1 file changed, 55 insertions(+), 97 deletions(-) diff --git a/test/hypothesis/error_hunt.py b/test/hypothesis/error_hunt.py index 7d5e952..7523551 100644 --- a/test/hypothesis/error_hunt.py +++ b/test/hypothesis/error_hunt.py @@ -5,8 +5,10 @@ import hypothesis from hypothesis import given, settings from hypothesis.strategies import decimals, integers, tuples from mpmath import mp +import pytest mp.prec = 500 +getcontext().prec = 14 node = subprocess.Popen( ["node", "evaluate.mjs"], stdout=subprocess.PIPE, stdin=subprocess.PIPE @@ -20,106 +22,62 @@ def get_decimal(func: str, args: list, config: dict): return Decimal(node.stdout.readline().strip().decode()) -def to_precision(x, precision=14): - getcontext().prec = precision - return Decimal(str(x)) * Decimal("1.0") - - -decimal_fixed_precision = tuples( - decimals( - allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 - ), - integers(min_value=-99, max_value=99), -).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) - -decimal_positive = tuples( - decimals( - allow_nan=False, allow_infinity=False, min_value=1e-13, max_value=1, places=14 - ), - integers(min_value=-99, max_value=99), -).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) - - -@given(decimal_fixed_precision) -@settings(max_examples=100) -def test_tangent(x): - y = to_precision(mp.tan(x)) - z = get_decimal("tan", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_fixed_precision) -@settings(max_examples=100) -def test_cosine(x): - y = to_precision(mp.cos(x)) - z = get_decimal("cos", [str(x)], {"precision": 14}) +def assert_matches(x, mpfunc, jsfunc=None): + if jsfunc is None: + jsfunc = mpfunc + y = Decimal(str(getattr(mp, mpfunc)(x))) * Decimal("1.0") + z = get_decimal(jsfunc, [str(x)], {"precision": 14}) assert y == z -@given(decimal_fixed_precision) -@settings(max_examples=100) -def test_sine(x): - y = to_precision(mp.sin(x)) - z = get_decimal("sin", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_positive) -@settings(max_examples=100) -def test_log(x): - y = to_precision(mp.log10(x)) - z = get_decimal("log", [str(x)], {"precision": 14}) - assert y == z - - -inverse_trig_strat = decimals( - allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 +@pytest.mark.parametrize("fn", "sin cos tan atan".split()) +@given( + x=tuples( + decimals( + allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 + ), + integers(min_value=-99, max_value=99), + ).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) ) +def test_matches(x, fn): + assert_matches(x, fn) + + +@pytest.mark.parametrize("fn", "ln log10".split()) +@given( + x=tuples( + decimals( + allow_nan=False, + allow_infinity=False, + min_value=1e-13, + max_value=1, + places=14, + ), + integers(min_value=-99, max_value=99), + ).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) +) +def test_positive_domain(x, fn): + assert_matches(x, fn) -@given(inverse_trig_strat) -@settings(max_examples=100) -def test_acos(x): - y = to_precision(mp.acos(x)) - z = get_decimal("acos", [str(x)], {"precision": 14}) - assert y == z - - -@given(inverse_trig_strat) -@settings(max_examples=100) -def test_asin(x): - y = to_precision(mp.asin(x)) - z = get_decimal("asin", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_fixed_precision) -@settings(max_examples=100) -def test_atan(x): - y = to_precision(mp.atan(x)) - z = get_decimal("atan", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_fixed_precision) -@settings(max_examples=10) -def test_cosh(x): - y = to_precision(mp.cosh(x)) - z = get_decimal("cosh", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_fixed_precision) -@settings(max_examples=4) -def test_sinh(x): - y = to_precision(mp.sinh(x)) - z = get_decimal("sinh", [str(x)], {"precision": 14}) - assert y == z - - -@given(decimal_fixed_precision) -@settings(max_examples=4) -def test_tanh(x): - y = to_precision(mp.tanh(x)) - z = get_decimal("tanh", [str(x)], {"precision": 14}) - assert y == z +@pytest.mark.parametrize("fn", "asin acos".split()) +@given( + x=decimals( + allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 + ) +) +def test_inverse_trig(x, fn): + assert_matches(x, fn) + + +@pytest.mark.parametrize("fn", "sinh cosh tanh".split()) +@given( + x=tuples( + decimals( + allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 + ), + integers(min_value=-99, max_value=3), + ).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) +) +def test_small_domain(x, fn): + assert_matches(x, fn)