Debian Codegraph @

hypothesis-python/tests/quality/test_float_shrinking.py

raw
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 pytest
12
13 from hypothesis import HealthCheck, example, given, settings, strategies as st
14 from hypothesis.internal.compat import ceil
15
16 from tests.common.debug import minimal
17
18
19 def test_shrinks_to_simple_floats():
20 assert minimal(st.floats(), lambda x: x > 1) == 2.0
21 assert minimal(st.floats(), lambda x: x > 0) == 1.0
22
23
24 @pytest.mark.parametrize("n", [1, 2, 3, 8, 10])
25 def test_can_shrink_in_variable_sized_context(n):
26 x = minimal(st.lists(st.floats(), min_size=n), any)
27 assert len(x) == n
28 assert x.count(0.0) == n - 1
29 assert 1 in x
30
31
32 @example(1.7976931348623157e308)
33 @example(1.5)
34 @given(st.floats(min_value=0, allow_infinity=False, allow_nan=False))
35 @settings(suppress_health_check=[HealthCheck.nested_given])
36 def test_shrinks_downwards_to_integers(f):
37 assert minimal(st.floats(min_value=f)) == ceil(f)
38
39
40 @example(1)
41 @given(st.integers(1, 2**16 - 1))
42 @settings(suppress_health_check=[HealthCheck.nested_given])
43 def test_shrinks_downwards_to_integers_when_fractional(b):
44 g = minimal(
45 st.floats(
46 min_value=b, max_value=2**53, exclude_min=True, exclude_max=True
47 ).filter(lambda x: int(x) != x)
48 )
49 assert g == b + 0.5