Debian Codegraph @

hypothesis-python/tests/nocover/test_conjecture_engine.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 from hypothesis import given, settings, strategies as st
12 from hypothesis.database import InMemoryExampleDatabase, choices_from_bytes
13 from hypothesis.internal.conjecture.data import ConjectureData
14 from hypothesis.internal.conjecture.engine import ConjectureRunner
15 from hypothesis.internal.conjecture.shrinker import Shrinker
16
17 from tests.common.utils import (
18 Why,
19 counts_calls,
20 non_covering_examples,
21 xfail_on_crosshair,
22 )
23 from tests.conjecture.common import interesting_origin, run_to_nodes, shrinking_from
24
25
26 @xfail_on_crosshair(Why.nested_given)
27 def test_lot_of_dead_nodes():
28 @run_to_nodes
29 def nodes(data):
30 for i in range(4):
31 if data.draw_integer(0, 2**7 - 1) != i:
32 data.mark_invalid()
33 data.mark_interesting(interesting_origin())
34
35 assert tuple(n.value for n in nodes) == (0, 1, 2, 3)
36
37
38 def test_saves_data_while_shrinking(monkeypatch):
39 key = b"hi there"
40 n = 5
41 db = InMemoryExampleDatabase()
42 assert list(db.fetch(key)) == []
43 seen = set()
44
45 monkeypatch.setattr(
46 ConjectureRunner,
47 "generate_new_examples",
48 lambda runner: runner.cached_test_function([bytes([255] * 10)]),
49 )
50
51 def f(data):
52 x = data.draw_bytes(10, 10)
53 if sum(x) >= 2000 and len(seen) < n:
54 seen.add(x)
55 if x in seen:
56 data.mark_interesting(interesting_origin())
57
58 runner = ConjectureRunner(f, settings=settings(database=db), database_key=key)
59 runner.run()
60 assert runner.interesting_examples
61 assert len(seen) == n
62
63 in_db = {choices_from_bytes(b)[0] for b in non_covering_examples(db)}
64 assert in_db.issubset(seen)
65 assert in_db == seen
66
67
68 def test_can_discard(monkeypatch):
69 n = 8
70
71 monkeypatch.setattr(
72 ConjectureRunner,
73 "generate_new_examples",
74 lambda runner: runner.cached_test_function(
75 tuple(bytes(v) for i in range(n) for v in [i, i])
76 ),
77 )
78
79 @run_to_nodes
80 def nodes(data):
81 seen = set()
82 while len(seen) < n:
83 seen.add(data.draw_bytes())
84 data.mark_interesting(interesting_origin())
85
86 assert len(nodes) == n
87
88
89 @xfail_on_crosshair(Why.nested_given)
90 @given(st.integers(0, 255), st.integers(0, 255))
91 def test_cached_with_masked_byte_agrees_with_results(a, b):
92 def f(data):
93 data.draw_integer(0, 3)
94
95 runner = ConjectureRunner(f)
96
97 cached_a = runner.cached_test_function([a])
98 cached_b = runner.cached_test_function([b])
99
100 data_b = ConjectureData.for_choices([b], observer=runner.tree.new_observer())
101 runner.test_function(data_b)
102
103 # If the cache found an old result, then it should match the real result.
104 # If it did not, then it must be because A and B were different.
105 assert (cached_a is cached_b) == (cached_a.nodes == data_b.nodes)
106
107
108 def test_node_programs_fail_efficiently(monkeypatch):
109 # Create 256 byte-sized nodes. None of the nodes can be deleted, and
110 # every deletion attempt produces a different buffer.
111 @shrinking_from(range(256))
112 def shrinker(data: ConjectureData):
113 values = set()
114 for _ in range(256):
115 v = data.draw_integer(0, 2**8 - 1)
116 values.add(v)
117 if len(values) == 256:
118 data.mark_interesting(interesting_origin())
119
120 monkeypatch.setattr(
121 Shrinker, "run_node_program", counts_calls(Shrinker.run_node_program)
122 )
123 shrinker.max_stall = 500
124 shrinker.fixate_shrink_passes([shrinker.node_program("XX")])
125
126 assert shrinker.shrinks == 0
127 assert 250 <= shrinker.calls <= 260
128 # The node program should have been run roughly 255 times, with a little
129 # bit of wiggle room for implementation details.
130 # - Too many calls mean that failing steps are doing too much work.
131 # - Too few calls mean that this test is probably miscounting and buggy.
132 assert 250 <= Shrinker.run_node_program.calls <= 260