Debian Codegraph @

hypothesis-python/tests/nocover/test_strategy_state.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 hashlib
12 import math
13 from random import Random
14
15 from hypothesis import Verbosity, assume, settings
16 from hypothesis.database import InMemoryExampleDatabase
17 from hypothesis.internal.compat import PYPY
18 from hypothesis.internal.floats import clamp, float_to_int, int_to_float, is_negative
19 from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule
20 from hypothesis.strategies import (
21 binary,
22 booleans,
23 complex_numbers,
24 data,
25 decimals,
26 floats,
27 fractions,
28 integers,
29 just,
30 lists,
31 none,
32 sampled_from,
33 text,
34 tuples,
35 )
36
37
38 class HypothesisSpec(RuleBasedStateMachine):
39 def __init__(self):
40 super().__init__()
41 self.database = None
42
43 strategies = Bundle("strategy")
44 strategy_tuples = Bundle("tuples")
45 objects = Bundle("objects")
46 basic_data = Bundle("basic")
47 varied_floats = Bundle("varied_floats")
48
49 def teardown(self):
50 self.clear_database()
51
52 @rule()
53 def clear_database(self):
54 if self.database is not None:
55 self.database = None
56
57 @rule()
58 def set_database(self):
59 self.teardown()
60 self.database = InMemoryExampleDatabase()
61
62 @rule(
63 target=strategies,
64 spec=sampled_from(
65 (
66 integers(),
67 booleans(),
68 floats(),
69 complex_numbers(),
70 fractions(),
71 decimals(),
72 text(),
73 binary(),
74 none(),
75 tuples(),
76 )
77 ),
78 )
79 def strategy(self, spec):
80 return spec
81
82 @rule(target=strategies, values=lists(integers() | text(), min_size=1))
83 def sampled_from_strategy(self, values):
84 return sampled_from(values)
85
86 @rule(target=strategies, spec=strategy_tuples)
87 def strategy_for_tupes(self, spec):
88 return tuples(*spec)
89
90 @rule(target=strategies, source=strategies, level=integers(1, 10), mixer=text())
91 def filtered_strategy(self, source, level, mixer):
92 def is_good(x):
93 seed = hashlib.sha384((mixer + repr(x)).encode()).digest()
94 return bool(Random(seed).randint(0, level))
95
96 return source.filter(is_good)
97
98 @rule(target=strategies, elements=strategies)
99 def list_strategy(self, elements):
100 return lists(elements)
101
102 @rule(target=strategies, left=strategies, right=strategies)
103 def or_strategy(self, left, right):
104 return left | right
105
106 @rule(target=varied_floats, source=floats())
107 def float(self, source):
108 return source
109
110 @rule(target=varied_floats, source=varied_floats, offset=integers(-100, 100))
111 def adjust_float(self, source, offset):
112 return int_to_float(clamp(0, float_to_int(source) + offset, 2**64 - 1))
113
114 @rule(target=strategies, left=varied_floats, right=varied_floats)
115 def float_range(self, left, right):
116 assume(math.isfinite(left) and math.isfinite(right))
117 left, right = sorted((left, right))
118 assert left <= right
119 # exclude deprecated case where left = 0.0 and right = -0.0
120 assume(left or right or not (is_negative(right) and not is_negative(left)))
121 return floats(left, right)
122
123 @rule(
124 target=strategies,
125 source=strategies,
126 result1=strategies,
127 result2=strategies,
128 mixer=text(),
129 p=floats(0, 1),
130 )
131 def flatmapped_strategy(self, source, result1, result2, mixer, p):
132 assume(result1 is not result2)
133
134 def do_map(value):
135 rep = repr(value)
136 random = Random(hashlib.sha384((mixer + rep).encode()).digest())
137 if random.random() <= p:
138 return result1
139 else:
140 return result2
141
142 return source.flatmap(do_map)
143
144 @rule(target=strategies, value=objects)
145 def just_strategy(self, value):
146 return just(value)
147
148 @rule(target=strategy_tuples, source=strategies)
149 def single_tuple(self, source):
150 return (source,)
151
152 @rule(target=strategy_tuples, left=strategy_tuples, right=strategy_tuples)
153 def cat_tuples(self, left, right):
154 return left + right
155
156 @rule(target=objects, strat=strategies, data=data())
157 def get_example(self, strat, data):
158 data.draw(strat)
159
160 @rule(target=strategies, left=integers(), right=integers())
161 def integer_range(self, left, right):
162 left, right = sorted((left, right))
163 return integers(left, right)
164
165 @rule(strat=strategies)
166 def repr_is_good(self, strat):
167 assert " at 0x" not in repr(strat)
168
169
170 MAIN = __name__ == "__main__"
171
172 TestHypothesis = HypothesisSpec.TestCase
173
174 TestHypothesis.settings = settings(
175 TestHypothesis.settings,
176 stateful_step_count=10 if PYPY else 50,
177 verbosity=max(TestHypothesis.settings.verbosity, Verbosity.verbose),
178 max_examples=10000 if MAIN else 200,
179 )
180
181 if MAIN:
182 TestHypothesis().runTest()