| 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 HealthCheck, assume, example, given, settings, strategies as st |
| 17 |
from hypothesis.internal.compat import ceil, extract_bits, floor |
| 18 |
from hypothesis.internal.conjecture import floats as flt |
| 19 |
from hypothesis.internal.conjecture.engine import ConjectureRunner |
| 20 |
from hypothesis.internal.floats import SIGNALING_NAN, float_to_int |
| 21 |
|
| 22 |
from tests.conjecture.common import interesting_origin |
| 23 |
|
| 24 |
EXPONENTS = list(range(flt.MAX_EXPONENT + 1)) |
| 25 |
assert len(EXPONENTS) == 2**11 |
| 26 |
|
| 27 |
|
| 28 |
def assert_reordered_exponents(res): |
| 29 |
res = list(res) |
| 30 |
assert len(res) == len(EXPONENTS) |
| 31 |
for x in res: |
| 32 |
assert res.count(x) == 1 |
| 33 |
assert 0 <= x <= flt.MAX_EXPONENT |
| 34 |
|
| 35 |
|
| 36 |
def test_encode_permutes_elements(): |
| 37 |
assert_reordered_exponents(map(flt.encode_exponent, EXPONENTS)) |
| 38 |
|
| 39 |
|
| 40 |
def test_decode_permutes_elements(): |
| 41 |
assert_reordered_exponents(map(flt.decode_exponent, EXPONENTS)) |
| 42 |
|
| 43 |
|
| 44 |
def test_decode_encode(): |
| 45 |
for e in EXPONENTS: |
| 46 |
assert flt.decode_exponent(flt.encode_exponent(e)) == e |
| 47 |
|
| 48 |
|
| 49 |
def test_encode_decode(): |
| 50 |
for e in EXPONENTS: |
| 51 |
assert flt.decode_exponent(flt.encode_exponent(e)) == e |
| 52 |
|
| 53 |
|
| 54 |
@given(st.data()) |
| 55 |
def test_double_reverse_bounded(data): |
| 56 |
n = data.draw(st.integers(1, 64)) |
| 57 |
i = data.draw(st.integers(0, 2**n - 1)) |
| 58 |
j = flt.reverse_bits(i, n) |
| 59 |
assert flt.reverse_bits(j, n) == i |
| 60 |
|
| 61 |
|
| 62 |
@given(st.integers(0, 2**64 - 1)) |
| 63 |
def test_double_reverse(i): |
| 64 |
j = flt.reverse64(i) |
| 65 |
assert flt.reverse64(j) == i |
| 66 |
|
| 67 |
|
| 68 |
@example(0.0) |
| 69 |
@example(2.5) |
| 70 |
@example(8.000000000000007) |
| 71 |
@example(3.0) |
| 72 |
@example(2.0) |
| 73 |
@example(1.9999999999999998) |
| 74 |
@example(1.0) |
| 75 |
@given(st.floats(min_value=0.0)) |
| 76 |
def test_floats_round_trip(f): |
| 77 |
i = flt.float_to_lex(f) |
| 78 |
g = flt.lex_to_float(i) |
| 79 |
|
| 80 |
assert float_to_int(f) == float_to_int(g) |
| 81 |
|
| 82 |
|
| 83 |
@settings(suppress_health_check=[HealthCheck.too_slow, HealthCheck.filter_too_much]) |
| 84 |
@example(1, 0.5) |
| 85 |
@given(st.integers(1, 2**53), st.floats(0, 1).filter(lambda x: x not in (0, 1))) |
| 86 |
def test_floats_order_worse_than_their_integral_part(n, g): |
| 87 |
f = n + g |
| 88 |
assume(int(f) != f) |
| 89 |
assume(int(f) != 0) |
| 90 |
i = flt.float_to_lex(f) |
| 91 |
if f < 0: |
| 92 |
g = ceil(f) |
| 93 |
else: |
| 94 |
g = floor(f) |
| 95 |
|
| 96 |
assert flt.float_to_lex(float(g)) < i |
| 97 |
|
| 98 |
|
| 99 |
integral_floats = st.floats(allow_infinity=False, allow_nan=False, min_value=0.0).map( |
| 100 |
lambda x: abs(float(int(x))) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
@given(integral_floats, integral_floats) |
| 105 |
def test_integral_floats_order_as_integers(x, y): |
| 106 |
assume(x != y) |
| 107 |
x, y = sorted((x, y)) |
| 108 |
assert flt.float_to_lex(x) < flt.float_to_lex(y) |
| 109 |
|
| 110 |
|
| 111 |
@given(st.floats(0, 1)) |
| 112 |
def test_fractional_floats_are_worse_than_one(f): |
| 113 |
assume(0 < f < 1) |
| 114 |
assert flt.float_to_lex(f) > flt.float_to_lex(1) |
| 115 |
|
| 116 |
|
| 117 |
def test_reverse_bits_table_reverses_bits(): |
| 118 |
for i, b in enumerate(flt.REVERSE_BITS_TABLE): |
| 119 |
assert extract_bits(i, width=8) == list(reversed(extract_bits(b, width=8))) |
| 120 |
|
| 121 |
|
| 122 |
def test_reverse_bits_table_has_right_elements(): |
| 123 |
assert sorted(flt.REVERSE_BITS_TABLE) == list(range(256)) |
| 124 |
|
| 125 |
|
| 126 |
def float_runner(start, condition, *, constraints=None): |
| 127 |
constraints = {} if constraints is None else constraints |
| 128 |
|
| 129 |
def test_function(data): |
| 130 |
f = data.draw_float(**constraints) |
| 131 |
if condition(f): |
| 132 |
data.mark_interesting(interesting_origin()) |
| 133 |
|
| 134 |
runner = ConjectureRunner(test_function) |
| 135 |
runner.cached_test_function((float(start),)) |
| 136 |
assert runner.interesting_examples |
| 137 |
return runner |
| 138 |
|
| 139 |
|
| 140 |
def minimal_from(start, condition, *, constraints=None): |
| 141 |
runner = float_runner(start, condition, constraints=constraints) |
| 142 |
runner.shrink_interesting_examples() |
| 143 |
(v,) = runner.interesting_examples.values() |
| 144 |
f = v.choices[0] |
| 145 |
assert condition(f) |
| 146 |
return f |
| 147 |
|
| 148 |
|
| 149 |
INTERESTING_FLOATS = [0.0, 1.0, 2.0, sys.float_info.max, float("inf"), float("nan")] |
| 150 |
|
| 151 |
|
| 152 |
@pytest.mark.parametrize( |
| 153 |
("start", "end"), |
| 154 |
[ |
| 155 |
(a, b) |
| 156 |
for a in INTERESTING_FLOATS |
| 157 |
for b in INTERESTING_FLOATS |
| 158 |
if flt.float_to_lex(a) > flt.float_to_lex(b) |
| 159 |
], |
| 160 |
) |
| 161 |
def test_can_shrink_downwards(start, end): |
| 162 |
assert minimal_from(start, lambda x: not (x < end)) == end |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.parametrize( |
| 166 |
"f", [1, 2, 4, 8, 10, 16, 32, 64, 100, 128, 256, 500, 512, 1000, 1024] |
| 167 |
) |
| 168 |
@pytest.mark.parametrize("mul", [1.1, 1.5, 9.99, 10]) |
| 169 |
def test_shrinks_downwards_to_integers(f, mul): |
| 170 |
g = minimal_from(f * mul, lambda x: x >= f) |
| 171 |
assert g == f |
| 172 |
|
| 173 |
|
| 174 |
def test_shrink_to_integer_upper_bound(): |
| 175 |
assert minimal_from(1.1, lambda x: 1 < x <= 2) == 2 |
| 176 |
|
| 177 |
|
| 178 |
def test_shrink_up_to_one(): |
| 179 |
assert minimal_from(0.5, lambda x: 0.5 <= x <= 1.5) == 1 |
| 180 |
|
| 181 |
|
| 182 |
def test_shrink_down_to_half(): |
| 183 |
assert minimal_from(0.75, lambda x: 0 < x < 1) == 0.5 |
| 184 |
|
| 185 |
|
| 186 |
def test_shrink_fractional_part(): |
| 187 |
assert minimal_from(2.5, lambda x: divmod(x, 1)[1] == 0.5) == 1.5 |
| 188 |
|
| 189 |
|
| 190 |
def test_does_not_shrink_across_one(): |
| 191 |
# This is something of an odd special case. Because of our encoding we |
| 192 |
# prefer all numbers >= 1 to all numbers in 0 < x < 1. For the most part |
| 193 |
# this is the correct thing to do, but there are some low negative exponent |
| 194 |
# cases where we get odd behaviour like this. |
| 195 |
|
| 196 |
# This test primarily exists to validate that we don't try to subtract one |
| 197 |
# from the starting point and trigger an internal exception. |
| 198 |
assert minimal_from(1.1, lambda x: x == 1.1 or 0 < x < 1) == 1.1 |
| 199 |
|
| 200 |
|
| 201 |
def test_reject_out_of_bounds_floats_while_shrinking(): |
| 202 |
# coverage test for rejecting out of bounds floats while shrinking |
| 203 |
constraints = {"min_value": 103.0} |
| 204 |
g = minimal_from(103.1, lambda x: x >= 100, constraints=constraints) |
| 205 |
assert g == 103.0 |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.parametrize("nan", [-math.nan, SIGNALING_NAN, -SIGNALING_NAN]) |
| 209 |
def test_shrinks_to_canonical_nan(nan): |
| 210 |
shrunk = minimal_from(nan, math.isnan) |
| 211 |
assert float_to_int(shrunk) == float_to_int(math.nan) |