| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import decimal |
| 12 |
from math import copysign, inf |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import HealthCheck, assume, given, reject, settings |
| 17 |
from hypothesis.errors import InvalidArgument |
| 18 |
from hypothesis.internal.floats import next_down, next_up |
| 19 |
from hypothesis.strategies import ( |
| 20 |
booleans, |
| 21 |
data, |
| 22 |
decimals, |
| 23 |
floats, |
| 24 |
fractions, |
| 25 |
integers, |
| 26 |
none, |
| 27 |
sampled_from, |
| 28 |
tuples, |
| 29 |
) |
| 30 |
|
| 31 |
from tests.common.debug import check_can_generate_examples, find_any, minimal |
| 32 |
from tests.common.utils import checks_deprecated_behaviour, skipif_threading |
| 33 |
|
| 34 |
|
| 35 |
@settings(suppress_health_check=list(HealthCheck)) |
| 36 |
@given(data()) |
| 37 |
def test_fuzz_floats_bounds(data): |
| 38 |
width = data.draw(sampled_from([64, 32, 16])) |
| 39 |
bound = none() | floats(allow_nan=False, width=width) |
| 40 |
low, high = data.draw(tuples(bound, bound), label="low, high") |
| 41 |
if low is not None and high is not None and low > high: |
| 42 |
low, high = high, low |
| 43 |
if low is not None and high is not None and low > high: |
| 44 |
low, high = high, low |
| 45 |
exmin = low is not None and low != inf and data.draw(booleans(), label="ex_min") |
| 46 |
exmax = high is not None and high != -inf and data.draw(booleans(), label="ex_max") |
| 47 |
|
| 48 |
if low is not None and high is not None: |
| 49 |
lo = next_up(low, width) if exmin else low |
| 50 |
hi = next_down(high, width) if exmax else high |
| 51 |
# There must actually be floats between these bounds |
| 52 |
assume(lo <= hi) |
| 53 |
if lo == hi == 0: |
| 54 |
assume(not exmin and not exmax and copysign(1.0, lo) <= copysign(1.0, hi)) |
| 55 |
|
| 56 |
s = floats(low, high, exclude_min=exmin, exclude_max=exmax, width=width) |
| 57 |
val = data.draw(s, label="value") |
| 58 |
assume(val) # positive/negative zero is an issue |
| 59 |
|
| 60 |
if low is not None: |
| 61 |
assert low <= val |
| 62 |
if high is not None: |
| 63 |
assert val <= high |
| 64 |
if exmin: |
| 65 |
assert low != val |
| 66 |
if exmax: |
| 67 |
assert high != val |
| 68 |
|
| 69 |
|
| 70 |
@given(data()) |
| 71 |
def test_fuzz_fractions_bounds(data): |
| 72 |
denom = data.draw(none() | integers(1, 100), label="denominator") |
| 73 |
fracs = none() | fractions(max_denominator=denom) |
| 74 |
low, high = data.draw(tuples(fracs, fracs), label="low, high") |
| 75 |
if low is not None and high is not None and low > high: |
| 76 |
low, high = high, low |
| 77 |
try: |
| 78 |
val = data.draw(fractions(low, high, max_denominator=denom), label="value") |
| 79 |
except InvalidArgument: |
| 80 |
reject() # fractions too close for given max_denominator |
| 81 |
if low is not None: |
| 82 |
assert low <= val |
| 83 |
if high is not None: |
| 84 |
assert val <= high |
| 85 |
if denom is not None: |
| 86 |
assert 1 <= val.denominator <= denom |
| 87 |
|
| 88 |
|
| 89 |
@given(data()) |
| 90 |
def test_fuzz_decimals_bounds(data): |
| 91 |
places = data.draw(none() | integers(0, 20), label="places") |
| 92 |
finite_decs = ( |
| 93 |
decimals(allow_nan=False, allow_infinity=False, places=places) | none() |
| 94 |
) |
| 95 |
low, high = data.draw(tuples(finite_decs, finite_decs), label="low, high") |
| 96 |
if low is not None and high is not None and low > high: |
| 97 |
low, high = high, low |
| 98 |
ctx = decimal.Context(prec=data.draw(integers(1, 100), label="precision")) |
| 99 |
try: |
| 100 |
with decimal.localcontext(ctx): |
| 101 |
strat = decimals( |
| 102 |
low, high, allow_nan=False, allow_infinity=False, places=places |
| 103 |
) |
| 104 |
val = data.draw(strat, label="value") |
| 105 |
except InvalidArgument: |
| 106 |
reject() # decimals too close for given places |
| 107 |
if low is not None: |
| 108 |
assert low <= val |
| 109 |
if high is not None: |
| 110 |
assert val <= high |
| 111 |
if places is not None: |
| 112 |
assert val.as_tuple().exponent == -places |
| 113 |
|
| 114 |
|
| 115 |
def test_all_decimals_can_be_exact_floats(): |
| 116 |
find_any( |
| 117 |
decimals(), lambda x: assume(x.is_finite()) and decimal.Decimal(float(x)) == x |
| 118 |
) |
| 119 |
|
| 120 |
|
| 121 |
@given(fractions(), fractions(), fractions()) |
| 122 |
def test_fraction_addition_is_well_behaved(x, y, z): |
| 123 |
assert x + y + z == y + x + z |
| 124 |
|
| 125 |
|
| 126 |
def test_decimals_include_nan(): |
| 127 |
find_any(decimals(), lambda x: x.is_nan()) |
| 128 |
|
| 129 |
|
| 130 |
def test_decimals_include_inf(): |
| 131 |
find_any(decimals(), lambda x: x.is_infinite(), settings(max_examples=10**6)) |
| 132 |
|
| 133 |
|
| 134 |
@given(decimals(allow_nan=False)) |
| 135 |
def test_decimals_can_disallow_nan(x): |
| 136 |
assert not x.is_nan() |
| 137 |
|
| 138 |
|
| 139 |
@given(decimals(allow_infinity=False)) |
| 140 |
def test_decimals_can_disallow_inf(x): |
| 141 |
assert not x.is_infinite() |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.parametrize("places", range(10)) |
| 145 |
def test_decimals_have_correct_places(places): |
| 146 |
@given(decimals(0, 10, allow_nan=False, places=places)) |
| 147 |
def inner_tst(n): |
| 148 |
assert n.as_tuple().exponent == -places |
| 149 |
|
| 150 |
inner_tst() |
| 151 |
|
| 152 |
|
| 153 |
@given(decimals(min_value="0.1", max_value="0.2", allow_nan=False, places=1)) |
| 154 |
def test_works_with_few_values(dec): |
| 155 |
assert dec in (decimal.Decimal("0.1"), decimal.Decimal("0.2")) |
| 156 |
|
| 157 |
|
| 158 |
@given(decimals(places=3, allow_nan=False, allow_infinity=False)) |
| 159 |
def test_issue_725_regression(x): |
| 160 |
pass |
| 161 |
|
| 162 |
|
| 163 |
@given(decimals(min_value="0.1", max_value="0.3")) |
| 164 |
def test_issue_739_regression(x): |
| 165 |
pass |
| 166 |
|
| 167 |
|
| 168 |
def test_consistent_decimal_error(): |
| 169 |
bad = "invalid argument to Decimal" |
| 170 |
with pytest.raises(InvalidArgument) as excinfo: |
| 171 |
check_can_generate_examples(decimals(bad)) |
| 172 |
with ( |
| 173 |
pytest.raises(InvalidArgument) as excinfo2, |
| 174 |
decimal.localcontext(decimal.Context(traps=[])), |
| 175 |
): |
| 176 |
check_can_generate_examples(decimals(bad)) |
| 177 |
assert str(excinfo.value) == str(excinfo2.value) |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
"s, msg", |
| 182 |
[ |
| 183 |
( |
| 184 |
floats(min_value=inf, allow_infinity=False), |
| 185 |
"allow_infinity=False excludes min_value=inf", |
| 186 |
), |
| 187 |
( |
| 188 |
floats(min_value=next_down(inf), exclude_min=True, allow_infinity=False), |
| 189 |
"exclude_min=True turns min_value=.+? into inf, but allow_infinity=False", |
| 190 |
), |
| 191 |
( |
| 192 |
floats(max_value=-inf, allow_infinity=False), |
| 193 |
"allow_infinity=False excludes max_value=-inf", |
| 194 |
), |
| 195 |
( |
| 196 |
floats(max_value=next_up(-inf), exclude_max=True, allow_infinity=False), |
| 197 |
"exclude_max=True turns max_value=.+? into -inf, but allow_infinity=False", |
| 198 |
), |
| 199 |
], |
| 200 |
) |
| 201 |
@skipif_threading # strategy instances are shared, which persists .validate_called |
| 202 |
def test_floats_message(s, msg): |
| 203 |
# https://github.com/HypothesisWorks/hypothesis/issues/3207 |
| 204 |
with pytest.raises(InvalidArgument, match=msg): |
| 205 |
s.validate() |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.parametrize("s", [decimals(), decimals(places=10)], ids=repr) |
| 209 |
def test_minimal_nonfinite_decimal_is_inf(s): |
| 210 |
assert minimal(s.filter(lambda x: not x.is_finite())) == decimal.Decimal("Infinity") |
| 211 |
|
| 212 |
|
| 213 |
@checks_deprecated_behaviour |
| 214 |
def test_decimals_warns_for_inexact_numeric_bounds(): |
| 215 |
check_can_generate_examples(decimals(min_value=1e-100)) |