Debian Codegraph @

hypothesis-python/tests/conjecture/test_forced.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 math
12
13 import pytest
14
15 import hypothesis.strategies as st
16 from hypothesis import HealthCheck, example, given, settings
17 from hypothesis.internal.conjecture import utils as cu
18 from hypothesis.internal.conjecture.choice import choice_equal
19 from hypothesis.internal.conjecture.data import ConjectureData
20 from hypothesis.internal.floats import SIGNALING_NAN, SMALLEST_SUBNORMAL
21
22 from tests.conjecture.common import choice_types_constraints, fresh_data
23
24
25 @given(st.data())
26 @settings(
27 database=None,
28 suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow],
29 )
30 def test_forced_many(data):
31 forced = data.draw(st.integers(0, 100))
32 min_size = data.draw(st.integers(0, forced))
33 max_size = data.draw(st.integers(forced, 100))
34 assert min_size <= forced <= max_size # by construction
35
36 data = fresh_data()
37 many = cu.many(
38 data,
39 min_size=min_size,
40 average_size=(min_size + max_size) / 2,
41 max_size=max_size,
42 forced=forced,
43 )
44 for _ in range(forced):
45 assert many.more()
46
47 assert not many.more()
48
49 # ensure values written to the buffer do in fact generate the forced value
50 data = ConjectureData.for_choices(data.choices)
51 many = cu.many(
52 data,
53 min_size=min_size,
54 average_size=(min_size + max_size) / 2,
55 max_size=max_size,
56 )
57 for _ in range(forced):
58 assert many.more()
59
60 assert not many.more()
61
62
63 @example(("boolean", {"p": 1e-19, "forced": True})) # 64 bit p
64 @example(("boolean", {"p": 3e-19, "forced": True})) # 62 bit p
65 @example(
66 (
67 "integer",
68 {
69 "min_value": -1,
70 "max_value": 1,
71 "shrink_towards": 1,
72 "weights": {-1: 0.2, 0: 0.2, 1: 0.2},
73 "forced": 0,
74 },
75 )
76 )
77 @example(
78 (
79 "integer",
80 {
81 "min_value": -1,
82 "max_value": 1,
83 "shrink_towards": -1,
84 "weights": {-1: 0.2, 0: 0.2, 1: 0.2},
85 "forced": 0,
86 },
87 )
88 )
89 @example(
90 (
91 "integer",
92 {
93 "min_value": 10,
94 "max_value": 1_000,
95 "shrink_towards": 17,
96 "weights": {20: 0.1},
97 "forced": 15,
98 },
99 )
100 )
101 @example(
102 (
103 "integer",
104 {
105 "min_value": -1_000,
106 "max_value": -10,
107 "shrink_towards": -17,
108 "weights": {-20: 0.1},
109 "forced": -15,
110 },
111 )
112 )
113 @example(("float", {"forced": 0.0}))
114 @example(("float", {"forced": -0.0}))
115 @example(("float", {"forced": 1.0}))
116 @example(("float", {"forced": 1.2345}))
117 @example(("float", {"forced": SMALLEST_SUBNORMAL}))
118 @example(("float", {"forced": -SMALLEST_SUBNORMAL}))
119 @example(("float", {"forced": 100 * SMALLEST_SUBNORMAL}))
120 @example(("float", {"forced": math.nan}))
121 @example(("float", {"forced": -math.nan}))
122 @example(("float", {"forced": SIGNALING_NAN}))
123 @example(("float", {"forced": -SIGNALING_NAN}))
124 @example(("float", {"forced": 1e999}))
125 @example(("float", {"forced": -1e999}))
126 # previously errored on our {pos, neg}_clamper logic not considering nans.
127 @example(
128 (
129 "float",
130 {"min_value": -1 * math.inf, "max_value": -1 * math.inf, "forced": math.nan},
131 )
132 )
133 @given(choice_types_constraints(use_forced=True))
134 def test_forced_values(choice_type_and_constraints):
135 (choice_type, constraints) = choice_type_and_constraints
136 constraints = constraints.copy()
137 forced = constraints["forced"]
138 data = fresh_data()
139 assert choice_equal(getattr(data, f"draw_{choice_type}")(**constraints), forced)
140
141 # now make sure the written buffer reproduces the forced value, even without
142 # specifying forced=.
143 del constraints["forced"]
144 data = ConjectureData.for_choices(data.choices)
145 assert choice_equal(getattr(data, f"draw_{choice_type}")(**constraints), forced)
146
147
148 @pytest.mark.parametrize("sign", [1, -1])
149 @pytest.mark.parametrize(
150 "min_value, max_value",
151 [
152 (0.0, 0.0),
153 (-0.0, -0.0),
154 (0.0, 100.0),
155 (-100.0, -0.0),
156 (5.0, 10.0),
157 (-10.0, -5.0),
158 ],
159 )
160 @given(random=st.randoms())
161 def test_forced_floats_with_nan(random, sign, min_value, max_value):
162 # nans with a sign opposite of both bounds previously gave us trouble
163 # trying to use float clampers that didn't exist when drawing.
164 data = fresh_data(random=random)
165 data.draw_float(min_value=min_value, max_value=max_value, forced=sign * math.nan)
166
167
168 @given(st.data())
169 def test_forced_with_large_magnitude_integers(data):
170 bound_offset = data.draw(st.integers(min_value=0))
171 # forced_offset = bound_offset + st.integers(min_value=0) may look cleaner, but
172 # has subtly different maximum value semantics as it is twice the range of a
173 # single draw
174 forced_offset = data.draw(st.integers(min_value=bound_offset))
175
176 half_range = 2**127 + 1
177 cd = fresh_data()
178 cd.draw_integer(
179 min_value=half_range + bound_offset, forced=half_range + forced_offset
180 )
181
182 cd = fresh_data()
183 cd.draw_integer(
184 max_value=-(half_range + bound_offset), forced=-(half_range + forced_offset)
185 )