Debian Codegraph @

hypothesis-python/tests/nocover/test_floating.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 """Tests for being able to generate weird and wonderful floating point numbers."""
12
13 import math
14 import sys
15
16 import pytest
17
18 from hypothesis import HealthCheck, assume, given, settings
19 from hypothesis.internal.floats import float_to_int
20 from hypothesis.strategies import data, floats, lists
21
22 from tests.common.debug import find_any
23 from tests.common.utils import fails
24
25 TRY_HARDER = settings(
26 max_examples=1000, suppress_health_check=[HealthCheck.filter_too_much]
27 )
28
29
30 @given(floats())
31 @TRY_HARDER
32 def test_is_float(x):
33 assert isinstance(x, float)
34
35
36 @fails
37 @given(floats())
38 @TRY_HARDER
39 def test_inversion_is_imperfect(x):
40 assume(x != 0.0)
41 y = 1.0 / x
42 assert x * y == 1.0
43
44
45 @given(floats(-sys.float_info.max, sys.float_info.max))
46 def test_largest_range(x):
47 assert not math.isinf(x)
48
49
50 @given(floats())
51 @TRY_HARDER
52 def test_negation_is_self_inverse(x):
53 assume(not math.isnan(x))
54 y = -x
55 assert -y == x
56
57
58 @fails
59 @given(lists(floats()))
60 @TRY_HARDER
61 def test_is_not_nan(xs):
62 assert not any(math.isnan(x) for x in xs)
63
64
65 @fails
66 @given(floats())
67 @TRY_HARDER
68 def test_is_not_positive_infinite(x):
69 assume(x > 0)
70 assert not math.isinf(x)
71
72
73 @fails
74 @given(floats())
75 @TRY_HARDER
76 def test_is_not_negative_infinite(x):
77 assume(x < 0)
78 assert not math.isinf(x)
79
80
81 @fails
82 @given(floats())
83 @TRY_HARDER
84 def test_is_int(x):
85 assume(math.isfinite(x))
86 assert x == int(x)
87
88
89 @fails
90 @given(floats())
91 @TRY_HARDER
92 def test_is_not_int(x):
93 assume(math.isfinite(x))
94 assert x != int(x)
95
96
97 @fails
98 @given(floats())
99 @TRY_HARDER
100 def test_is_in_exact_int_range(x):
101 assume(math.isfinite(x))
102 assert x + 1 != x
103
104
105 @fails
106 @given(floats())
107 @TRY_HARDER
108 def test_can_find_floats_that_do_not_round_trip_through_strings(x):
109 assert float(str(x)) == x
110
111
112 @fails
113 @given(floats())
114 @TRY_HARDER
115 def test_can_find_floats_that_do_not_round_trip_through_reprs(x):
116 assert float(repr(x)) == x
117
118
119 finite_floats = floats(allow_infinity=False, allow_nan=False)
120
121
122 @settings(deadline=None)
123 @given(finite_floats, finite_floats, data())
124 def test_floats_are_in_range(x, y, data):
125 x, y = sorted((x, y))
126 assume(x < y)
127
128 t = data.draw(floats(x, y))
129 assert x <= t <= y
130
131
132 @pytest.mark.parametrize("neg", [False, True])
133 @pytest.mark.parametrize("snan", [False, True])
134 def test_can_find_negative_and_signaling_nans(neg, snan):
135 find_any(
136 floats().filter(math.isnan),
137 lambda x: (
138 snan is (float_to_int(abs(x)) != float_to_int(float("nan")))
139 and neg is (math.copysign(1, x) == -1)
140 ),
141 settings=TRY_HARDER,
142 )