Debian Codegraph @

hypothesis-python/tests/conjecture/test_test_data.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 itertools
12
13 import pytest
14
15 from hypothesis import strategies as st
16 from hypothesis.errors import Frozen
17 from hypothesis.internal.conjecture.data import (
18 MAX_DEPTH,
19 ConjectureData,
20 DataObserver,
21 Overrun,
22 Status,
23 StopTest,
24 structural_coverage,
25 )
26 from hypothesis.strategies import SearchStrategy
27
28 from tests.conjecture.common import buffer_size_limit, interesting_origin
29
30
31 def test_cannot_draw_after_freeze():
32 d = ConjectureData.for_choices((True,))
33 d.draw_boolean()
34 d.freeze()
35 with pytest.raises(Frozen):
36 d.draw_boolean()
37
38
39 def test_can_double_freeze():
40 d = ConjectureData.for_choices([])
41 d.freeze()
42 assert d.frozen
43 d.freeze()
44 assert d.frozen
45
46
47 def test_draw_past_end_sets_overflow():
48 d = ConjectureData.for_choices((True,))
49
50 d.draw_boolean()
51 with pytest.raises(StopTest) as e:
52 d.draw_boolean()
53
54 assert e.value.testcounter == d.testcounter
55 assert d.frozen
56 assert d.status == Status.OVERRUN
57
58
59 def test_notes_repr():
60 d = ConjectureData.for_choices([])
61 d.note(b"hi")
62 assert repr(b"hi") in d.output
63
64
65 def test_can_mark_interesting():
66 d = ConjectureData.for_choices([])
67 with pytest.raises(StopTest):
68 d.mark_interesting(interesting_origin())
69 assert d.frozen
70 assert d.status == Status.INTERESTING
71
72
73 def test_can_mark_invalid():
74 d = ConjectureData.for_choices([])
75 with pytest.raises(StopTest):
76 d.mark_invalid()
77 assert d.frozen
78 assert d.status == Status.INVALID
79
80
81 def test_can_mark_invalid_with_why():
82 d = ConjectureData.for_choices([])
83 with pytest.raises(StopTest):
84 d.mark_invalid("some reason")
85 assert d.frozen
86 assert d.status == Status.INVALID
87 assert d.events == {"invalid because": "some reason"}
88
89
90 class BoomStrategy(SearchStrategy):
91 def do_draw(self, data):
92 data.draw_boolean()
93 raise ValueError
94
95
96 def test_closes_interval_on_error_in_strategy():
97 d = ConjectureData.for_choices((True,))
98 with pytest.raises(ValueError):
99 d.draw(BoomStrategy())
100 d.freeze()
101 assert not any(eg.end is None for eg in d.spans)
102
103
104 class BigStrategy(SearchStrategy):
105 def do_draw(self, data):
106 data.draw_bytes(10**6, 10**6)
107
108
109 def test_does_not_double_freeze_in_interval_close():
110 d = ConjectureData.for_choices((b"hi",))
111 with pytest.raises(StopTest):
112 d.draw(BigStrategy())
113 assert d.frozen
114 assert not any(eg.end is None for eg in d.spans)
115
116
117 def test_triviality():
118 d = ConjectureData.for_choices((True, False, b"1"))
119
120 d.start_span(label=1)
121 d.draw(st.booleans())
122 d.draw(st.booleans())
123 d.stop_span()
124
125 d.start_span(label=2)
126 d.draw_bytes(1, 1, forced=bytes([2]))
127 d.stop_span()
128
129 d.freeze()
130
131 def trivial(u, v):
132 ex = next(ex for ex in d.spans if ex.start == u and ex.end == v)
133 return all(node.trivial for node in d.nodes[ex.start : ex.end])
134
135 assert not trivial(0, 2)
136 assert not trivial(0, 1)
137 assert trivial(1, 2)
138 assert trivial(2, 3)
139
140
141 def test_example_depth_marking():
142 d = ConjectureData.for_choices((0,) * 6)
143 d.draw(st.integers()) # v1
144 d.start_span(0)
145 d.draw(st.integers()) # v2
146 d.draw(st.integers()) # v3
147 d.stop_span()
148 d.draw(st.integers()) # v4
149 d.freeze()
150
151 assert len(d.spans) == 6
152
153 depths = [(ex.choice_count, ex.depth) for ex in d.spans]
154 assert depths == [
155 (4, 0), # top
156 (1, 1), # v1
157 (2, 1), # inner
158 (1, 2), # v2
159 (1, 2), # v3
160 (1, 1), # v4
161 ]
162
163
164 def test_has_examples_even_when_empty():
165 d = ConjectureData.for_choices([])
166 d.draw(st.just(False))
167 d.freeze()
168 assert d.spans
169
170
171 def test_has_cached_examples_even_when_overrun():
172 d = ConjectureData.for_choices((False,))
173 d.start_span(3)
174 d.draw_boolean()
175 d.stop_span()
176 try:
177 d.draw_boolean()
178 except StopTest:
179 pass
180 assert d.status == Status.OVERRUN
181 assert any(ex.label == 3 and ex.choice_count == 1 for ex in d.spans)
182 assert d.spans is d.spans
183
184
185 def test_can_observe_draws():
186 class LoggingObserver(DataObserver):
187 def __init__(self):
188 self.log = []
189
190 def draw_boolean(self, value: bool, *, was_forced: bool, constraints: dict):
191 self.log.append(("draw_boolean", value, was_forced))
192
193 def draw_integer(self, value: int, *, was_forced: bool, constraints: dict):
194 self.log.append(("draw_integer", value, was_forced))
195
196 def conclude_test(self, *args):
197 assert d.frozen
198 self.log.append(("concluded", *args))
199
200 observer = LoggingObserver()
201 d = ConjectureData.for_choices((True, 1, 3), observer=observer)
202
203 origin = interesting_origin()
204 d.draw_boolean()
205 d.draw_integer(0, 2**7 - 1, forced=10)
206 d.draw_integer(0, 2**8 - 1)
207 with pytest.raises(StopTest):
208 d.conclude_test(Status.INTERESTING, interesting_origin=origin)
209
210 assert observer.log == [
211 ("draw_boolean", True, False),
212 ("draw_integer", 10, True),
213 ("draw_integer", 3, False),
214 ("concluded", Status.INTERESTING, origin),
215 ]
216
217
218 def test_calls_concluded_implicitly():
219 class NoteConcluded(DataObserver):
220 def conclude_test(self, status, reason):
221 assert d.frozen
222 self.conclusion = (status, reason)
223
224 observer = NoteConcluded()
225
226 d = ConjectureData.for_choices((True,), observer=observer)
227 d.draw_boolean()
228 d.freeze()
229
230 assert observer.conclusion == (Status.VALID, None)
231
232
233 def test_examples_show_up_as_discarded():
234 d = ConjectureData.for_choices((True, False, True))
235
236 d.start_span(1)
237 d.draw_boolean()
238 d.stop_span(discard=True)
239 d.start_span(1)
240 d.draw_boolean()
241 d.stop_span()
242 d.freeze()
243
244 assert len([ex for ex in d.spans if ex.discarded]) == 1
245
246
247 def test_examples_support_negative_indexing():
248 d = ConjectureData.for_choices((True, True))
249 d.draw(st.booleans())
250 d.draw(st.booleans())
251 d.freeze()
252 assert d.spans[-1].choice_count == 1
253
254
255 def test_examples_out_of_bounds_index():
256 d = ConjectureData.for_choices((False,))
257 d.draw(st.booleans())
258 d.freeze()
259 with pytest.raises(IndexError):
260 d.spans[10]
261
262
263 def test_can_override_label():
264 d = ConjectureData.for_choices((False,))
265 d.draw(st.booleans(), label=7)
266 d.freeze()
267 assert any(ex.label == 7 for ex in d.spans)
268
269
270 def test_will_mark_too_deep_examples_as_invalid():
271 d = ConjectureData.for_choices((0,))
272
273 s = st.integers()
274 for _ in range(MAX_DEPTH + 1):
275 s = s.map(lambda x: None)
276
277 with pytest.raises(StopTest):
278 d.draw(s)
279 assert d.status == Status.INVALID
280
281
282 def test_empty_strategy_is_invalid():
283 d = ConjectureData.for_choices([])
284 with pytest.raises(StopTest):
285 d.draw(st.nothing())
286 assert d.status == Status.INVALID
287
288
289 def test_can_note_non_str():
290 d = ConjectureData.for_choices([])
291 x = object()
292 d.note(x)
293 assert repr(x) in d.output
294
295
296 def test_can_note_str_as_non_repr():
297 d = ConjectureData.for_choices([])
298 d.note("foo")
299 assert d.output == "foo"
300
301
302 def test_result_is_overrun():
303 d = ConjectureData.for_choices([])
304 with pytest.raises(StopTest):
305 d.draw_boolean()
306 assert d.as_result() is Overrun
307
308
309 def test_trivial_before_force_agrees_with_trivial_after():
310 d = ConjectureData.for_choices((False, True, True))
311 d.draw_boolean()
312 d.draw_boolean(forced=True)
313 d.draw_boolean()
314
315 t1 = [d.nodes[i].trivial for i in range(3)]
316 d.freeze()
317 r = d.as_result()
318 t2 = [n.trivial for n in r.nodes]
319 t3 = [r.nodes[i].trivial for i in range(3)]
320
321 assert t1 == t2 == t3
322
323
324 def test_events_are_noted():
325 d = ConjectureData.for_choices([])
326 d.events["hello"] = ""
327 assert "hello" in d.events
328
329
330 def test_child_indices():
331 d = ConjectureData.for_choices((True,) * 4)
332
333 d.start_span(0) # examples[1]
334 d.start_span(1) # examples[2]
335 d.draw(st.booleans()) # examples[3] (st.booleans)
336 d.draw(st.booleans()) # examples[4] (st.booleans)
337 d.stop_span()
338 d.stop_span()
339 d.draw(st.booleans()) # examples[5] (st.booleans)
340 d.draw(st.booleans()) # examples[6] (st.booleans)
341 d.freeze()
342
343 assert list(d.spans.children[0]) == [1, 5, 6]
344 assert list(d.spans.children[1]) == [2]
345 assert list(d.spans.children[2]) == [3, 4]
346
347 assert d.spans[0].parent is None
348 for ex in list(d.spans)[1:]:
349 assert ex in d.spans[ex.parent].children
350
351
352 def test_example_equality():
353 d = ConjectureData.for_choices((False, False))
354
355 d.start_span(0)
356 d.draw_boolean()
357 d.stop_span()
358 d.start_span(0)
359 d.draw_boolean()
360 d.stop_span()
361 d.freeze()
362
363 examples = list(d.spans)
364 for ex1, ex2 in itertools.combinations(examples, 2):
365 assert ex1 != ex2
366 assert not (ex1 == ex2) # noqa
367
368 for ex in examples:
369 assert ex == ex
370 assert not (ex != ex) # noqa
371
372 assert not (ex == "hello") # noqa
373 assert ex != "hello"
374
375
376 def test_structural_coverage_is_cached():
377 assert structural_coverage(50) is structural_coverage(50)
378
379
380 def test_examples_create_structural_coverage():
381 data = ConjectureData.for_choices([])
382 data.start_span(42)
383 data.stop_span()
384 data.freeze()
385 assert structural_coverage(42) in data.tags
386
387
388 def test_discarded_examples_do_not_create_structural_coverage():
389 data = ConjectureData.for_choices([])
390 data.start_span(42)
391 data.stop_span(discard=True)
392 data.freeze()
393 assert structural_coverage(42) not in data.tags
394
395
396 def test_children_of_discarded_examples_do_not_create_structural_coverage():
397 data = ConjectureData.for_choices([])
398 data.start_span(10)
399 data.start_span(42)
400 data.stop_span()
401 data.stop_span(discard=True)
402 data.freeze()
403 assert structural_coverage(42) not in data.tags
404 assert structural_coverage(10) not in data.tags
405
406
407 def test_overruns_at_exactly_max_length():
408 with buffer_size_limit(1):
409 data = ConjectureData(prefix=[True], random=None, max_choices=1)
410 data.draw_boolean()
411 try:
412 data.draw_boolean()
413 except StopTest:
414 pass
415 assert data.status is Status.OVERRUN