| 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 |
from sys import float_info |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import example, given, strategies as st |
| 17 |
from hypothesis.internal.conjecture.choice import choice_equal, choice_permitted |
| 18 |
from hypothesis.internal.conjecture.provider_conformance import float_constraints |
| 19 |
from hypothesis.internal.floats import ( |
| 20 |
count_between_floats, |
| 21 |
make_float_clamper, |
| 22 |
next_down, |
| 23 |
next_up, |
| 24 |
sign_aware_lte, |
| 25 |
) |
| 26 |
|
| 27 |
from tests.conjecture.common import float_constr |
| 28 |
|
| 29 |
|
| 30 |
def test_can_handle_straddling_zero(): |
| 31 |
assert count_between_floats(-0.0, 0.0) == 2 |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.parametrize( |
| 35 |
"func,val", |
| 36 |
[ |
| 37 |
(next_up, math.nan), |
| 38 |
(next_up, math.inf), |
| 39 |
(next_up, -0.0), |
| 40 |
(next_down, math.nan), |
| 41 |
(next_down, -math.inf), |
| 42 |
(next_down, 0.0), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_next_float_equal(func, val): |
| 46 |
if math.isnan(val): |
| 47 |
assert math.isnan(func(val)) |
| 48 |
else: |
| 49 |
assert func(val) == val |
| 50 |
|
| 51 |
|
| 52 |
# exponent comparisons: |
| 53 |
@example(float_constr(1, float_info.max), 0.0) |
| 54 |
@example(float_constr(1, float_info.max), 1.0) |
| 55 |
@example(float_constr(1, float_info.max), 10.0) |
| 56 |
@example(float_constr(1, float_info.max), float_info.max) |
| 57 |
@example(float_constr(1, float_info.max), math.inf) |
| 58 |
# mantissa comparisons: |
| 59 |
@example(float_constr(100.0001, 100.0003), 100.0001) |
| 60 |
@example(float_constr(100.0001, 100.0003), 100.0002) |
| 61 |
@example(float_constr(100.0001, 100.0003), 100.0003) |
| 62 |
@example(float_constr(100.0001, 100.0003, allow_nan=False), math.nan) |
| 63 |
@example(float_constr(0, 10, allow_nan=False), math.nan) |
| 64 |
@example(float_constr(0, 10, allow_nan=True), math.nan) |
| 65 |
# the branch coverage of resampling in the "out of range of smallest magnitude" case |
| 66 |
# relies on randomness from the mantissa. try a few different values. |
| 67 |
@example(float_constr(-4, -1, smallest_nonzero_magnitude=4), 4.0) |
| 68 |
@example(float_constr(-4, -1, smallest_nonzero_magnitude=4), 5.0) |
| 69 |
@example(float_constr(-4, -1, smallest_nonzero_magnitude=4), 6.0) |
| 70 |
@example(float_constr(1, 4, smallest_nonzero_magnitude=4), -4.0) |
| 71 |
@example(float_constr(1, 4, smallest_nonzero_magnitude=4), -5.0) |
| 72 |
@example(float_constr(1, 4, smallest_nonzero_magnitude=4), -6.0) |
| 73 |
@example(float_constr(-5e-324, -0.0), 3.0) |
| 74 |
@example(float_constr(0.0, 0.0), -0.0) |
| 75 |
@example(float_constr(-0.0, -0.0), 0.0) |
| 76 |
@given(float_constraints(), st.floats()) |
| 77 |
def test_float_clamper(constraints, input_value): |
| 78 |
min_value = constraints["min_value"] |
| 79 |
max_value = constraints["max_value"] |
| 80 |
allow_nan = constraints["allow_nan"] |
| 81 |
smallest_nonzero_magnitude = constraints["smallest_nonzero_magnitude"] |
| 82 |
clamper = make_float_clamper( |
| 83 |
min_value, |
| 84 |
max_value, |
| 85 |
smallest_nonzero_magnitude=smallest_nonzero_magnitude, |
| 86 |
allow_nan=allow_nan, |
| 87 |
) |
| 88 |
clamped = clamper(input_value) |
| 89 |
if math.isnan(clamped): |
| 90 |
# we should only clamp to nan if nans are allowed. |
| 91 |
assert allow_nan |
| 92 |
else: |
| 93 |
# otherwise, we should have clamped to something in the permitted range. |
| 94 |
assert sign_aware_lte(min_value, clamped) |
| 95 |
assert sign_aware_lte(clamped, max_value) |
| 96 |
|
| 97 |
# if input_value was permitted in the first place, then the clamped value should |
| 98 |
# be the same as the input value. |
| 99 |
if choice_permitted( |
| 100 |
input_value, |
| 101 |
{ |
| 102 |
"min_value": min_value, |
| 103 |
"max_value": max_value, |
| 104 |
"allow_nan": allow_nan, |
| 105 |
"smallest_nonzero_magnitude": smallest_nonzero_magnitude, |
| 106 |
}, |
| 107 |
): |
| 108 |
assert choice_equal(input_value, clamped) |