Debian Codegraph @

hypothesis-python/tests/conjecture/test_choice_tree.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 random import Random
12
13 from hypothesis import given, strategies as st
14 from hypothesis.internal.conjecture.shrinking.choicetree import (
15 ChoiceTree,
16 prefix_selection_order,
17 random_selection_order,
18 )
19
20
21 def select(*args):
22 return prefix_selection_order(args)
23
24
25 def exhaust(f):
26 tree = ChoiceTree()
27
28 results = []
29
30 prefix = ()
31
32 while not tree.exhausted:
33 prefix = tree.step(
34 prefix_selection_order(prefix), lambda chooser: results.append(f(chooser))
35 )
36 return results
37
38
39 @given(st.lists(st.integers()))
40 def test_can_enumerate_a_shallow_set(ls):
41 results = exhaust(lambda chooser: chooser.choose(ls))
42
43 assert sorted(results) == sorted(ls)
44
45
46 def test_can_enumerate_a_nested_set():
47 @exhaust
48 def nested(chooser):
49 i = chooser.choose(range(10))
50 j = chooser.choose(range(10), condition=lambda j: j > i)
51 return (i, j)
52
53 assert sorted(nested) == [(i, j) for i in range(10) for j in range(i + 1, 10)]
54
55
56 def test_can_enumerate_empty():
57 @exhaust
58 def empty(chooser):
59 return 1
60
61 assert empty == [1]
62
63
64 def test_all_filtered_child():
65 @exhaust
66 def all_filtered(chooser):
67 chooser.choose(range(10), condition=lambda j: False)
68
69 assert all_filtered == []
70
71
72 def test_skips_over_exhausted_children():
73 results = []
74
75 def f(chooser):
76 results.append(
77 (
78 chooser.choose(range(3), condition=lambda x: x > 0),
79 chooser.choose(range(2)),
80 )
81 )
82
83 tree = ChoiceTree()
84
85 tree.step(select(1, 0), f)
86 tree.step(select(1, 1), f)
87 tree.step(select(0, 0), f)
88
89 assert results == [(1, 0), (1, 1), (2, 0)]
90
91
92 def test_extends_prefix_from_right():
93 def f(chooser):
94 chooser.choose(range(4))
95
96 tree = ChoiceTree()
97 result = tree.step(select(), f)
98 assert result == (3,)
99
100
101 def test_starts_from_the_end():
102 def f(chooser):
103 chooser.choose(range(3))
104
105 tree = ChoiceTree()
106 assert tree.step(select(), f) == (2,)
107
108
109 def test_skips_over_exhausted_subtree():
110 def f(chooser):
111 chooser.choose(range(10))
112
113 tree = ChoiceTree()
114 assert tree.step(select(8), f) == (8,)
115 assert tree.step(select(8), f) == (7,)
116
117
118 def test_exhausts_randomly():
119 def f(chooser):
120 chooser.choose(range(10))
121
122 tree = ChoiceTree()
123
124 random = Random()
125
126 seen = set()
127
128 for _ in range(10):
129 seen.add(tree.step(random_selection_order(random), f))
130
131 assert len(seen) == 10
132 assert tree.exhausted
133
134
135 def test_exhausts_randomly_when_filtering():
136 def f(chooser):
137 chooser.choose(range(10), lambda x: False)
138
139 tree = ChoiceTree()
140
141 random = Random()
142
143 tree.step(random_selection_order(random), f)
144
145 assert tree.exhausted