| 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 math |
| 12 |
import sys |
| 13 |
import warnings |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import assume, given, strategies as st |
| 18 |
from hypothesis.errors import InvalidArgument |
| 19 |
from hypothesis.internal.floats import ( |
| 20 |
float_of, |
| 21 |
float_to_int, |
| 22 |
int_to_float, |
| 23 |
is_negative, |
| 24 |
next_down, |
| 25 |
next_up, |
| 26 |
) |
| 27 |
|
| 28 |
from tests.common.debug import find_any, minimal |
| 29 |
|
| 30 |
try: |
| 31 |
import numpy |
| 32 |
except ImportError: |
| 33 |
numpy = None |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize( |
| 37 |
("lower", "upper"), |
| 38 |
[ |
| 39 |
# Exact values don't matter, but they're large enough so that x + y = inf. |
| 40 |
(9.9792015476736e291, 1.7976931348623157e308), |
| 41 |
(-sys.float_info.max, sys.float_info.max), |
| 42 |
], |
| 43 |
) |
| 44 |
@given(data=st.data()) |
| 45 |
def test_floats_are_in_range(data, lower, upper): |
| 46 |
t = data.draw(st.floats(lower, upper)) |
| 47 |
assert lower <= t <= upper |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize("sign", [-1, 1]) |
| 51 |
def test_can_generate_both_zeros(sign): |
| 52 |
assert minimal(st.floats(), lambda x: math.copysign(1, x) == sign) == sign * 0.0 |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize( |
| 56 |
("l", "r"), |
| 57 |
[(-1.0, 1.0), (-0.0, 1.0), (-1.0, 0.0), (-sys.float_info.min, sys.float_info.min)], |
| 58 |
) |
| 59 |
@pytest.mark.parametrize("sign", [-1, 1]) |
| 60 |
def test_can_generate_both_zeros_when_in_interval(l, r, sign): |
| 61 |
assert minimal(st.floats(l, r), lambda x: math.copysign(1, x) == sign) == sign * 0.0 |
| 62 |
|
| 63 |
|
| 64 |
@given(st.floats(0.0, 1.0)) |
| 65 |
def test_does_not_generate_negative_if_right_boundary_is_positive(x): |
| 66 |
assert math.copysign(1, x) == 1 |
| 67 |
|
| 68 |
|
| 69 |
@given(st.floats(-1.0, -0.0)) |
| 70 |
def test_does_not_generate_positive_if_right_boundary_is_negative(x): |
| 71 |
assert math.copysign(1, x) == -1 |
| 72 |
|
| 73 |
|
| 74 |
def test_half_bounded_generates_zero(): |
| 75 |
find_any(st.floats(min_value=-1.0), lambda x: x == 0.0) |
| 76 |
find_any(st.floats(max_value=1.0), lambda x: x == 0.0) |
| 77 |
|
| 78 |
|
| 79 |
@given(st.floats(max_value=-0.0)) |
| 80 |
def test_half_bounded_respects_sign_of_upper_bound(x): |
| 81 |
assert math.copysign(1, x) == -1 |
| 82 |
|
| 83 |
|
| 84 |
@given(st.floats(min_value=0.0)) |
| 85 |
def test_half_bounded_respects_sign_of_lower_bound(x): |
| 86 |
assert math.copysign(1, x) == 1 |
| 87 |
|
| 88 |
|
| 89 |
@given(st.floats(allow_nan=False)) |
| 90 |
def test_filter_nan(x): |
| 91 |
assert not math.isnan(x) |
| 92 |
|
| 93 |
|
| 94 |
@given(st.floats(allow_infinity=False)) |
| 95 |
def test_filter_infinity(x): |
| 96 |
assert not math.isinf(x) |
| 97 |
|
| 98 |
|
| 99 |
def test_can_guard_against_draws_of_nan(): |
| 100 |
"""In this test we create a NaN value that naturally "tries" to shrink into |
| 101 |
the first strategy, where it is not permitted. This tests a case that is |
| 102 |
very unlikely to happen in random generation: When the unconstrained first |
| 103 |
branch of generating a float just happens to produce a NaN value. |
| 104 |
|
| 105 |
Here what happens is that we get a NaN from the *second* strategy, |
| 106 |
but this then shrinks into its unconstrained branch. The natural |
| 107 |
thing to happen is then to try to zero the branch parameter of the |
| 108 |
one_of, but that will put an illegal value there, so it's not |
| 109 |
allowed to happen. |
| 110 |
""" |
| 111 |
tagged_floats = st.one_of( |
| 112 |
st.tuples(st.just(0), st.floats(allow_nan=False)), |
| 113 |
st.tuples(st.just(1), st.floats(allow_nan=True)), |
| 114 |
) |
| 115 |
|
| 116 |
tag, _f = minimal(tagged_floats, lambda x: math.isnan(x[1])) |
| 117 |
assert tag == 1 |
| 118 |
|
| 119 |
|
| 120 |
def test_very_narrow_interval(): |
| 121 |
upper_bound = -1.0 |
| 122 |
lower_bound = int_to_float(float_to_int(upper_bound) + 10) |
| 123 |
assert lower_bound < upper_bound |
| 124 |
|
| 125 |
@given(st.floats(lower_bound, upper_bound)) |
| 126 |
def test(f): |
| 127 |
assert lower_bound <= f <= upper_bound |
| 128 |
|
| 129 |
test() |
| 130 |
|
| 131 |
|
| 132 |
@given(st.floats()) |
| 133 |
def test_up_means_greater(x): |
| 134 |
hi = next_up(x) |
| 135 |
if not x < hi: |
| 136 |
assert ( |
| 137 |
(math.isnan(x) and math.isnan(hi)) |
| 138 |
or (x > 0 and math.isinf(x)) |
| 139 |
or (x == hi == 0 and is_negative(x) and not is_negative(hi)) |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
@given(st.floats()) |
| 144 |
def test_down_means_lesser(x): |
| 145 |
lo = next_down(x) |
| 146 |
if not x > lo: |
| 147 |
assert ( |
| 148 |
(math.isnan(x) and math.isnan(lo)) |
| 149 |
or (x < 0 and math.isinf(x)) |
| 150 |
or (x == lo == 0 and is_negative(lo) and not is_negative(x)) |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
@given(st.floats(allow_nan=False, allow_infinity=False)) |
| 155 |
def test_updown_roundtrip(val): |
| 156 |
assert val == next_up(next_down(val)) |
| 157 |
assert val == next_down(next_up(val)) |
| 158 |
|
| 159 |
|
| 160 |
@given(st.floats(width=32, allow_infinity=False)) |
| 161 |
def test_float32_can_exclude_infinity(x): |
| 162 |
assert not math.isinf(x) |
| 163 |
|
| 164 |
|
| 165 |
@given(st.floats(width=16, allow_infinity=False)) |
| 166 |
def test_float16_can_exclude_infinity(x): |
| 167 |
assert not math.isinf(x) |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.parametrize( |
| 171 |
"kwargs", |
| 172 |
[ |
| 173 |
{"min_value": 10**5, "width": 16}, |
| 174 |
{"max_value": 10**5, "width": 16}, |
| 175 |
{"min_value": 10**40, "width": 32}, |
| 176 |
{"max_value": 10**40, "width": 32}, |
| 177 |
{"min_value": 10**400, "width": 64}, |
| 178 |
{"max_value": 10**400, "width": 64}, |
| 179 |
{"min_value": 10**400}, |
| 180 |
{"max_value": 10**400}, |
| 181 |
], |
| 182 |
) |
| 183 |
def test_out_of_range(kwargs): |
| 184 |
with pytest.raises(OverflowError): |
| 185 |
st.floats(**kwargs).validate() |
| 186 |
|
| 187 |
|
| 188 |
def test_disallowed_width(): |
| 189 |
with pytest.raises(InvalidArgument): |
| 190 |
st.floats(width=128).validate() |
| 191 |
|
| 192 |
|
| 193 |
def test_no_single_floats_in_range(): |
| 194 |
low = 2.0**25 + 1 |
| 195 |
high = low + 2 |
| 196 |
st.floats(low, high).validate() # Note: OK for 64bit floats |
| 197 |
with warnings.catch_warnings(): |
| 198 |
# Unrepresentable bounds are deprecated, but we're not testing that here |
| 199 |
warnings.simplefilter("ignore") |
| 200 |
with pytest.raises(InvalidArgument): |
| 201 |
st.floats(low, high, width=32).validate() |
| 202 |
|
| 203 |
|
| 204 |
# If the floats() strategy adds random floats to a value as large as 10^304 |
| 205 |
# without handling overflow, we are very likely to generate infinity. |
| 206 |
@given(st.floats(min_value=1e304, allow_infinity=False)) |
| 207 |
def test_finite_min_bound_does_not_overflow(x): |
| 208 |
assert not math.isinf(x) |
| 209 |
|
| 210 |
|
| 211 |
@given(st.floats(max_value=-1e304, allow_infinity=False)) |
| 212 |
def test_finite_max_bound_does_not_overflow(x): |
| 213 |
assert not math.isinf(x) |
| 214 |
|
| 215 |
|
| 216 |
@given(st.floats(0, 1, exclude_min=True, exclude_max=True)) |
| 217 |
def test_can_exclude_endpoints(x): |
| 218 |
assert 0 < x < 1 |
| 219 |
|
| 220 |
|
| 221 |
@given(st.floats(-math.inf, -1e307, exclude_min=True)) |
| 222 |
def test_can_exclude_neg_infinite_endpoint(x): |
| 223 |
assert not math.isinf(x) |
| 224 |
|
| 225 |
|
| 226 |
@given(st.floats(1e307, math.inf, exclude_max=True)) |
| 227 |
def test_can_exclude_pos_infinite_endpoint(x): |
| 228 |
assert not math.isinf(x) |
| 229 |
|
| 230 |
|
| 231 |
def test_exclude_infinite_endpoint_is_invalid(): |
| 232 |
with pytest.raises(InvalidArgument): |
| 233 |
st.floats(min_value=math.inf, exclude_min=True).validate() |
| 234 |
with pytest.raises(InvalidArgument): |
| 235 |
st.floats(max_value=-math.inf, exclude_max=True).validate() |
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.parametrize("lo,hi", [(True, False), (False, True), (True, True)]) |
| 239 |
@given(bound=st.floats(allow_nan=False, allow_infinity=False).filter(bool)) |
| 240 |
def test_exclude_entire_interval(lo, hi, bound): |
| 241 |
with pytest.raises(InvalidArgument, match=r"exclude_min=.+ and exclude_max="): |
| 242 |
st.floats(bound, bound, exclude_min=lo, exclude_max=hi).validate() |
| 243 |
|
| 244 |
|
| 245 |
def test_zero_intervals_are_OK(): |
| 246 |
st.floats(0.0, 0.0).validate() |
| 247 |
st.floats(-0.0, 0.0).validate() |
| 248 |
st.floats(-0.0, -0.0).validate() |
| 249 |
|
| 250 |
|
| 251 |
@pytest.mark.parametrize("lo", [0.0, -0.0]) |
| 252 |
@pytest.mark.parametrize("hi", [0.0, -0.0]) |
| 253 |
@pytest.mark.parametrize("exmin,exmax", [(True, False), (False, True), (True, True)]) |
| 254 |
def test_cannot_exclude_endpoint_with_zero_interval(lo, hi, exmin, exmax): |
| 255 |
with pytest.raises(InvalidArgument): |
| 256 |
st.floats(lo, hi, exclude_min=exmin, exclude_max=exmax).validate() |
| 257 |
|
| 258 |
|
| 259 |
WIDTHS = (64, 32, 16) |
| 260 |
|
| 261 |
|
| 262 |
@pytest.mark.parametrize("nonfloat", [st.nothing(), st.none()]) |
| 263 |
@given(data=st.data(), width=st.sampled_from(WIDTHS)) |
| 264 |
def test_fuzzing_floats_bounds(data, width, nonfloat): |
| 265 |
lo = data.draw(nonfloat | st.floats(allow_nan=False, width=width), label="lo") |
| 266 |
hi = data.draw(nonfloat | st.floats(allow_nan=False, width=width), label="hi") |
| 267 |
if lo is not None and hi is not None and lo > hi: |
| 268 |
lo, hi = hi, lo |
| 269 |
assume(lo != 0 or hi != 0) |
| 270 |
value = data.draw( |
| 271 |
st.floats(min_value=lo, max_value=hi, width=width, allow_nan=False), |
| 272 |
label="value", |
| 273 |
) |
| 274 |
assert value == float_of(value, width=width) |
| 275 |
assert lo is None or lo <= value |
| 276 |
assert hi is None or value <= hi |