1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-09-28 22:40:48 +00:00

parametrize tests

This commit is contained in:
Alden 2023-02-13 01:44:14 -05:00
parent 4c52aec1cd
commit 0f36747c05

View File

@ -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})
assert y == z
decimal_fixed_precision = tuples( @pytest.mark.parametrize("fn", "sin cos tan atan".split())
@given(
x=tuples(
decimals( decimals(
allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14 allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
), ),
integers(min_value=-99, max_value=99), integers(min_value=-99, max_value=99),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1]) ).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
@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
) )
def test_matches(x, fn):
assert_matches(x, fn)
@given(inverse_trig_strat) @pytest.mark.parametrize("fn", "ln log10".split())
@settings(max_examples=100) @given(
def test_acos(x): x=tuples(
y = to_precision(mp.acos(x)) decimals(
z = get_decimal("acos", [str(x)], {"precision": 14}) allow_nan=False,
assert y == z 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_asin(x): x=decimals(
y = to_precision(mp.asin(x)) allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
z = get_decimal("asin", [str(x)], {"precision": 14}) )
assert y == z )
def test_inverse_trig(x, fn):
assert_matches(x, fn)
@given(decimal_fixed_precision) @pytest.mark.parametrize("fn", "sinh cosh tanh".split())
@settings(max_examples=100) @given(
def test_atan(x): x=tuples(
y = to_precision(mp.atan(x)) decimals(
z = get_decimal("atan", [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=3),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
@given(decimal_fixed_precision) )
@settings(max_examples=10) def test_small_domain(x, fn):
def test_cosh(x): assert_matches(x, fn)
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