parametrize tests

pull/217/head
Alden 1 year ago
parent 4c52aec1cd
commit 0f36747c05

@ -5,8 +5,10 @@ import hypothesis
from hypothesis import given, settings from hypothesis import given, settings
from hypothesis.strategies import decimals, integers, tuples from hypothesis.strategies import decimals, integers, tuples
from mpmath import mp from mpmath import mp
import pytest
mp.prec = 500 mp.prec = 500
getcontext().prec = 14
node = subprocess.Popen( node = subprocess.Popen(
["node", "evaluate.mjs"], stdout=subprocess.PIPE, stdin=subprocess.PIPE ["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()) return Decimal(node.stdout.readline().strip().decode())
def to_precision(x, precision=14): def assert_matches(x, mpfunc, jsfunc=None):
getcontext().prec = precision if jsfunc is None:
return Decimal(str(x)) * Decimal("1.0") jsfunc = mpfunc
y = Decimal(str(getattr(mp, mpfunc)(x))) * Decimal("1.0")
z = get_decimal(jsfunc, [str(x)], {"precision": 14})
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})
assert y == z assert y == z
@given(decimal_fixed_precision) @pytest.mark.parametrize("fn", "sin cos tan atan".split())
@settings(max_examples=100) @given(
def test_sine(x): x=tuples(
y = to_precision(mp.sin(x)) decimals(
z = get_decimal("sin", [str(x)], {"precision": 14}) allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
assert y == z ),
integers(min_value=-99, max_value=99),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
@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
) )
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) @pytest.mark.parametrize("fn", "asin acos".split())
@settings(max_examples=100) @given(
def test_acos(x): x=decimals(
y = to_precision(mp.acos(x)) allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
z = get_decimal("acos", [str(x)], {"precision": 14}) )
assert y == z )
def test_inverse_trig(x, fn):
assert_matches(x, fn)
@given(inverse_trig_strat)
@settings(max_examples=100)
def test_asin(x): @pytest.mark.parametrize("fn", "sinh cosh tanh".split())
y = to_precision(mp.asin(x)) @given(
z = get_decimal("asin", [str(x)], {"precision": 14}) x=tuples(
assert y == z decimals(
allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
),
@given(decimal_fixed_precision) integers(min_value=-99, max_value=3),
@settings(max_examples=100) ).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
def test_atan(x): )
y = to_precision(mp.atan(x)) def test_small_domain(x, fn):
z = get_decimal("atan", [str(x)], {"precision": 14}) assert_matches(x, fn)
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

Loading…
Cancel
Save