Debian Codegraph @

hypothesis-python/tests/cover/test_simple_collections.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 collections import OrderedDict
12 from random import Random
13
14 import pytest
15
16 from hypothesis import given, settings
17 from hypothesis.strategies import (
18 booleans,
19 dictionaries,
20 fixed_dictionaries,
21 frozensets,
22 integers,
23 lists,
24 none,
25 nothing,
26 sets,
27 text,
28 tuples,
29 )
30
31 from tests.common.debug import assert_simple_property, find_any, minimal
32 from tests.common.utils import flaky
33
34
35 @pytest.mark.parametrize(
36 ("col", "strat"),
37 [
38 ((), tuples()),
39 ([], lists(none(), max_size=0)),
40 (set(), sets(none(), max_size=0)),
41 (frozenset(), frozensets(none(), max_size=0)),
42 ({}, fixed_dictionaries({})),
43 ({}, fixed_dictionaries({}, optional={})),
44 (OrderedDict(), fixed_dictionaries(OrderedDict(), optional=OrderedDict())),
45 ({}, fixed_dictionaries({}, optional={1: booleans()})),
46 ({0: False}, fixed_dictionaries({0: booleans()}, optional={1: booleans()})),
47 ({}, fixed_dictionaries({}, optional={(): booleans(), 0: booleans()})),
48 ([], lists(nothing())),
49 ([], lists(nothing(), unique=True)),
50 ],
51 )
52 def test_find_empty_collection_gives_empty(col, strat):
53 assert minimal(strat) == col
54
55
56 @pytest.mark.parametrize(
57 ("coltype", "strat"), [(list, lists), (set, sets), (frozenset, frozensets)]
58 )
59 def test_find_non_empty_collection_gives_single_zero(coltype, strat):
60 assert minimal(strat(integers()), bool) == coltype((0,))
61
62
63 @pytest.mark.parametrize(
64 ("coltype", "strat"), [(list, lists), (set, sets), (frozenset, frozensets)]
65 )
66 def test_minimizes_to_empty(coltype, strat):
67 assert minimal(strat(integers())) == coltype()
68
69
70 def test_minimizes_list_of_lists():
71 xs = minimal(lists(lists(booleans())), lambda x: any(x) and not all(x))
72 xs.sort()
73 assert xs == [[], [False]]
74
75
76 @given(sets(integers(0, 100), min_size=2, max_size=10))
77 @settings(max_examples=100)
78 def test_sets_are_size_bounded(xs):
79 assert 2 <= len(xs) <= 10
80
81
82 def test_ordered_dictionaries_preserve_keys():
83 r = Random()
84 keys = list(range(100))
85 r.shuffle(keys)
86 assert_simple_property(
87 fixed_dictionaries(OrderedDict([(k, booleans()) for k in keys])),
88 lambda x: list(x.keys()) == keys,
89 )
90
91
92 @given(fixed_dictionaries({}, optional={0: booleans(), 1: nothing(), 2: booleans()}))
93 def test_fixed_dictionaries_with_optional_and_empty_keys(d):
94 assert 1 not in d
95
96
97 @pytest.mark.parametrize("n", range(10))
98 def test_lists_of_fixed_length(n):
99 assert minimal(lists(integers(), min_size=n, max_size=n)) == [0] * n
100
101
102 @pytest.mark.parametrize("n", range(10))
103 def test_sets_of_fixed_length(n):
104 x = minimal(sets(integers(), min_size=n, max_size=n))
105 assert len(x) == n
106
107 if n == 0:
108 assert x == set()
109 else:
110 assert x == set(range(min(x), min(x) + n))
111
112
113 @pytest.mark.parametrize("n", range(10))
114 def test_dictionaries_of_fixed_length(n):
115 x = set(minimal(dictionaries(integers(), booleans(), min_size=n, max_size=n)))
116
117 if not n:
118 assert x == set()
119 else:
120 assert x == set(range(min(x), min(x) + n))
121
122
123 @pytest.mark.parametrize("n", range(10))
124 def test_lists_of_lower_bounded_length(n):
125 l = minimal(lists(integers(), min_size=n), lambda x: sum(x) >= 2 * n)
126 assert l == [] if n == 0 else [0] * (n - 1) + [n * 2]
127
128
129 @flaky(min_passes=1, max_runs=3)
130 def test_can_find_unique_lists_of_non_set_order():
131 # This test checks that our strategy for unique lists doesn't accidentally
132 # depend on the iteration order of sets.
133 #
134 # Unfortunately, that means that *this* test has to rely on set iteration
135 # order. That makes it tricky to debug on CPython, because set iteration
136 # order changes every time the process is launched.
137 #
138 # To get around this, define the PYTHONHASHSEED environment variable to
139 # a consistent value. This could be 0, or it could be the PYTHONHASHSEED
140 # value listed in a failure log from CI.
141
142 ls = minimal(
143 lists(text(), min_size=2, unique=True),
144 lambda x: list(set(reversed(x))) != x, # noqa: C414 # yes, reverse inside set
145 )
146 assert len(set(ls)) == len(ls)
147 assert len(ls) == 2
148
149
150 def test_can_draw_empty_list_from_unsatisfiable_strategy():
151 assert find_any(lists(integers().filter(lambda s: False))) == []
152
153
154 def test_can_draw_empty_set_from_unsatisfiable_strategy():
155 assert find_any(sets(integers().filter(lambda s: False))) == set()
156
157
158 @given(lists(sets(none()), min_size=10))
159 def test_small_sized_sets(x):
160 pass
161
162
163 def test_minimize_dicts_with_incompatible_keys():
164 strat = fixed_dictionaries({1: booleans(), "hi": lists(booleans())})
165 assert minimal(strat) == {1: False, "hi": []}
166
167
168 @given(
169 lists(
170 tuples(integers(), integers()),
171 min_size=2,
172 unique_by=(lambda x: x[0], lambda x: x[1]),
173 )
174 )
175 def test_lists_unique_by_tuple_funcs(ls):
176 firstitems, seconditems = zip(*ls, strict=True)
177 assert len(set(firstitems)) == len(firstitems)
178 assert len(set(seconditems)) == len(seconditems)