mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
test case finder
This commit is contained in:
parent
075521e8a0
commit
05c3fd3fae
3
test/hypothesis/.gitignore
vendored
Normal file
3
test/hypothesis/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
__pycache__
|
||||
.hypothesis
|
||||
.ipynb_checkpoints
|
125
test/hypothesis/error_hunt.py
Normal file
125
test/hypothesis/error_hunt.py
Normal file
@ -0,0 +1,125 @@
|
||||
from decimal import Decimal, getcontext
|
||||
import json
|
||||
import subprocess
|
||||
import hypothesis
|
||||
from hypothesis import given, settings
|
||||
from hypothesis.strategies import decimals, integers, tuples
|
||||
from mpmath import mp
|
||||
|
||||
mp.prec = 500
|
||||
|
||||
node = subprocess.Popen(
|
||||
["node", "evaluate.mjs"], stdout=subprocess.PIPE, stdin=subprocess.PIPE
|
||||
)
|
||||
|
||||
|
||||
def get_decimal(func: str, args: list, config: dict):
|
||||
arg = json.dumps({"func": func, "args": args, "config": config}).encode() + b"\r"
|
||||
node.stdin.write(arg)
|
||||
node.stdin.flush()
|
||||
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})
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
@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
|
26
test/hypothesis/evaluate.mjs
Normal file
26
test/hypothesis/evaluate.mjs
Normal file
@ -0,0 +1,26 @@
|
||||
// listen for test cases, and provide the results.
|
||||
// give JSON of the test case, receive the result
|
||||
// Example:
|
||||
// > {"func":"tan", "args":["12.5"], "config":{"precision":8}}
|
||||
// -0.066468242
|
||||
|
||||
|
||||
import {Decimal} from '../../decimal.mjs';
|
||||
import {createInterface} from 'readline';
|
||||
|
||||
const readline = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
readline.on("close", () => {console.log('\n'); process.exit(0);});
|
||||
|
||||
readline.on("line", (line) => {
|
||||
if (line) {
|
||||
const {func, args, config} = JSON.parse(line);
|
||||
config.defaults = true;
|
||||
Decimal.set(config);
|
||||
const result = Decimal[func](...args);
|
||||
console.log(result);
|
||||
}
|
||||
});
|
3
test/hypothesis/requirements.txt
Normal file
3
test/hypothesis/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
hypothesis
|
||||
mpmath
|
||||
node
|
Loading…
Reference in New Issue
Block a user