| 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 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given |
| 17 |
from hypothesis.strategies import floats, integers, lists |
| 18 |
|
| 19 |
from tests.common.debug import minimal |
| 20 |
from tests.common.utils import Why, xfail_on_crosshair |
| 21 |
|
| 22 |
|
| 23 |
def test_minimize_negative_int(): |
| 24 |
assert minimal(integers(), lambda x: x < 0) == -1 |
| 25 |
assert minimal(integers(), lambda x: x < -1) == -2 |
| 26 |
|
| 27 |
|
| 28 |
def test_positive_negative_int(): |
| 29 |
assert minimal(integers(), lambda x: x > 0) == 1 |
| 30 |
assert minimal(integers(), lambda x: x > 1) == 2 |
| 31 |
|
| 32 |
|
| 33 |
boundaries = pytest.mark.parametrize( |
| 34 |
"boundary", |
| 35 |
sorted( |
| 36 |
[2**i for i in range(10)] |
| 37 |
+ [2**i - 1 for i in range(10)] |
| 38 |
+ [2**i + 1 for i in range(10)] |
| 39 |
+ [10**i for i in range(6)] |
| 40 |
), |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
@boundaries |
| 45 |
def test_minimizes_int_down_to_boundary(boundary): |
| 46 |
assert minimal(integers(), lambda x: x >= boundary) == boundary |
| 47 |
|
| 48 |
|
| 49 |
@boundaries |
| 50 |
def test_minimizes_int_up_to_boundary(boundary): |
| 51 |
assert minimal(integers(), lambda x: x <= -boundary) == -boundary |
| 52 |
|
| 53 |
|
| 54 |
@boundaries |
| 55 |
def test_minimizes_ints_from_down_to_boundary(boundary): |
| 56 |
def is_good(x): |
| 57 |
assert x >= boundary - 10 |
| 58 |
return x >= boundary |
| 59 |
|
| 60 |
assert minimal(integers(min_value=boundary - 10), is_good) == boundary |
| 61 |
|
| 62 |
assert minimal(integers(min_value=boundary)) == boundary |
| 63 |
|
| 64 |
|
| 65 |
def test_minimizes_negative_integer_range_upwards(): |
| 66 |
assert minimal(integers(min_value=-10, max_value=-1)) == -1 |
| 67 |
|
| 68 |
|
| 69 |
@boundaries |
| 70 |
def test_minimizes_integer_range_to_boundary(boundary): |
| 71 |
assert minimal(integers(boundary, boundary + 100)) == boundary |
| 72 |
|
| 73 |
|
| 74 |
def test_single_integer_range_is_range(): |
| 75 |
assert minimal(integers(1, 1)) == 1 |
| 76 |
|
| 77 |
|
| 78 |
def test_minimal_small_number_in_large_range(): |
| 79 |
assert minimal(integers((-(2**32)), 2**32), lambda x: x >= 101) == 101 |
| 80 |
|
| 81 |
|
| 82 |
def test_minimal_small_sum_float_list(): |
| 83 |
xs = minimal(lists(floats(), min_size=5), lambda x: sum(x) >= 1.0) |
| 84 |
assert xs == [0.0, 0.0, 0.0, 0.0, 1.0] |
| 85 |
|
| 86 |
|
| 87 |
def test_minimals_boundary_floats(): |
| 88 |
def f(x): |
| 89 |
print(x) |
| 90 |
return True |
| 91 |
|
| 92 |
assert minimal(floats(min_value=-1, max_value=1), f) == 0 |
| 93 |
|
| 94 |
|
| 95 |
def test_minimal_non_boundary_float(): |
| 96 |
x = minimal(floats(min_value=1, max_value=9), lambda x: x > 2) |
| 97 |
assert x == 3 # (the smallest integer > 2) |
| 98 |
|
| 99 |
|
| 100 |
def test_minimal_float_is_zero(): |
| 101 |
assert minimal(floats()) == 0.0 |
| 102 |
|
| 103 |
|
| 104 |
def test_minimal_asymetric_bounded_float(): |
| 105 |
assert minimal(floats(min_value=1.1, max_value=1.6)) == 1.5 |
| 106 |
|
| 107 |
|
| 108 |
def test_negative_floats_simplify_to_zero(): |
| 109 |
assert minimal(floats(), lambda x: x <= -1.0) == -1.0 |
| 110 |
|
| 111 |
|
| 112 |
def test_minimal_infinite_float_is_positive(): |
| 113 |
assert minimal(floats(), math.isinf) == math.inf |
| 114 |
|
| 115 |
|
| 116 |
def test_can_minimal_infinite_negative_float(): |
| 117 |
assert minimal(floats(), lambda x: x < -sys.float_info.max) |
| 118 |
|
| 119 |
|
| 120 |
# Flakey under CrossHair; see https://github.com/pschanely/hypothesis-crosshair/issues/28 |
| 121 |
@xfail_on_crosshair(Why.undiscovered, strict=False) |
| 122 |
def test_can_minimal_float_on_boundary_of_representable(): |
| 123 |
minimal(floats(), lambda x: x + 1 == x and not math.isinf(x)) |
| 124 |
|
| 125 |
|
| 126 |
def test_minimize_nan(): |
| 127 |
assert math.isnan(minimal(floats(), math.isnan)) |
| 128 |
|
| 129 |
|
| 130 |
def test_minimize_very_large_float(): |
| 131 |
t = sys.float_info.max / 2 |
| 132 |
assert minimal(floats(), lambda x: x >= t) == t |
| 133 |
|
| 134 |
|
| 135 |
def is_integral(value): |
| 136 |
try: |
| 137 |
return int(value) == value |
| 138 |
except (OverflowError, ValueError): |
| 139 |
return False |
| 140 |
|
| 141 |
|
| 142 |
def test_can_minimal_float_far_from_integral(): |
| 143 |
minimal(floats(), lambda x: math.isfinite(x) and not is_integral(x * (2**32))) |
| 144 |
|
| 145 |
|
| 146 |
def test_list_of_fractional_float(): |
| 147 |
assert set( |
| 148 |
minimal( |
| 149 |
lists(floats(), min_size=5), |
| 150 |
lambda x: len([t for t in x if t >= 1.5]) >= 5, |
| 151 |
) |
| 152 |
) == {2} |
| 153 |
|
| 154 |
|
| 155 |
def test_minimal_fractional_float(): |
| 156 |
assert minimal(floats(), lambda x: x >= 1.5) == 2 |
| 157 |
|
| 158 |
|
| 159 |
@xfail_on_crosshair(Why.undiscovered) # Ineffective CrossHair decision heuristics here |
| 160 |
def test_minimizes_lists_of_negative_ints_up_to_boundary(): |
| 161 |
result = minimal( |
| 162 |
lists(integers(), min_size=10), |
| 163 |
lambda x: len([t for t in x if t <= -1]) >= 10, |
| 164 |
) |
| 165 |
assert result == [-1] * 10 |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.parametrize( |
| 169 |
("left", "right"), |
| 170 |
[(0.0, 5e-324), (-5e-324, 0.0), (-5e-324, 5e-324), (5e-324, 1e-323)], |
| 171 |
) |
| 172 |
def test_floats_in_constrained_range(left, right): |
| 173 |
@given(floats(left, right)) |
| 174 |
def test_in_range(r): |
| 175 |
assert left <= r <= right |
| 176 |
|
| 177 |
test_in_range() |
| 178 |
|
| 179 |
|
| 180 |
def test_bounds_are_respected(): |
| 181 |
assert minimal(floats(min_value=1.0)) == 1.0 |
| 182 |
assert minimal(floats(max_value=-1.0)) == -1.0 |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.parametrize("k", range(10)) |
| 186 |
def test_floats_from_zero_have_reasonable_range(k): |
| 187 |
n = 10**k |
| 188 |
assert minimal(floats(min_value=0.0), lambda x: x >= n) == float(n) |
| 189 |
assert minimal(floats(max_value=0.0), lambda x: x <= -n) == float(-n) |
| 190 |
|
| 191 |
|
| 192 |
def test_explicit_allow_nan(): |
| 193 |
minimal(floats(allow_nan=True), math.isnan) |
| 194 |
|
| 195 |
|
| 196 |
def test_one_sided_contains_infinity(): |
| 197 |
minimal(floats(min_value=1.0), math.isinf) |
| 198 |
minimal(floats(max_value=1.0), math.isinf) |
| 199 |
|
| 200 |
|
| 201 |
@given(floats(min_value=0.0, allow_infinity=False)) |
| 202 |
def test_no_allow_infinity_upper(x): |
| 203 |
assert not math.isinf(x) |
| 204 |
|
| 205 |
|
| 206 |
@given(floats(max_value=0.0, allow_infinity=False)) |
| 207 |
def test_no_allow_infinity_lower(x): |
| 208 |
assert not math.isinf(x) |
| 209 |
|
| 210 |
|
| 211 |
class TestFloatsAreFloats: |
| 212 |
@given(floats()) |
| 213 |
def test_unbounded(self, arg): |
| 214 |
assert isinstance(arg, float) |
| 215 |
|
| 216 |
@given(floats(min_value=0, max_value=float(2**64 - 1))) |
| 217 |
def test_int_float(self, arg): |
| 218 |
assert isinstance(arg, float) |
| 219 |
|
| 220 |
@given(floats(min_value=float(0), max_value=float(2**64 - 1))) |
| 221 |
def test_float_float(self, arg): |
| 222 |
assert isinstance(arg, float) |