Debian Codegraph @

hypothesis-python/tests/conjecture/test_minimizer.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 Counter
12
13 import pytest
14
15 from hypothesis.internal.conjecture.shrinking import (
16 Bytes,
17 Collection,
18 Integer,
19 Ordering,
20 String,
21 )
22 from hypothesis.internal.intervalsets import IntervalSet
23
24
25 def test_shrink_to_zero():
26 assert Integer.shrink(2**16, lambda n: True) == 0
27
28
29 def test_shrink_to_smallest():
30 assert Integer.shrink(2**16, lambda n: n > 10) == 11
31
32
33 def test_can_sort_bytes_by_reordering():
34 start = bytes([5, 4, 3, 2, 1, 0])
35 finish = Ordering.shrink(start, lambda x: set(x) == set(start))
36 assert bytes(finish) == bytes([0, 1, 2, 3, 4, 5])
37
38
39 def test_can_sort_bytes_by_reordering_partially():
40 start = bytes([5, 4, 3, 2, 1, 0])
41 finish = Ordering.shrink(start, lambda x: set(x) == set(start) and x[0] > x[-1])
42 assert bytes(finish) == bytes([1, 2, 3, 4, 5, 0])
43
44
45 def test_can_sort_bytes_by_reordering_partially2():
46 start = bytes([5, 4, 3, 2, 1, 0])
47 finish = Ordering.shrink(
48 start,
49 lambda x: Counter(x) == Counter(start) and x[0] > x[2],
50 full=True,
51 )
52 assert bytes(finish) == bytes([1, 2, 0, 3, 4, 5])
53
54
55 def test_can_sort_bytes_by_reordering_partially_not_cross_stationary_element():
56 start = bytes([5, 3, 0, 2, 1, 4])
57 finish = Ordering.shrink(start, lambda x: set(x) == set(start) and x[3] == 2)
58 assert bytes(finish) == bytes([0, 1, 3, 2, 4, 5])
59
60
61 @pytest.mark.parametrize(
62 "initial, predicate, intervals, expected",
63 [
64 ("f" * 10, lambda s: True, IntervalSet.from_string("abcdefg"), ""),
65 ("f" * 10, lambda s: len(s) >= 3, IntervalSet.from_string("abcdefg"), "aaa"),
66 (
67 "f" * 10,
68 lambda s: len(s) >= 3 and "a" not in s,
69 IntervalSet.from_string("abcdefg"),
70 "bbb",
71 ),
72 ],
73 )
74 def test_shrink_strings(initial, predicate, intervals, expected):
75 assert String.shrink(
76 initial, predicate, intervals=intervals, min_size=len(expected)
77 ) == tuple(expected)
78
79
80 @pytest.mark.parametrize(
81 "initial, predicate, expected",
82 [
83 (b"\x18\x12", lambda v: len(v) == 2, b"\x00\x00"),
84 (b"\x18\x12", lambda v: True, b""),
85 (b"\x01\x10", lambda v: len(v) > 0 and v[0] == 1, b"\x01"),
86 (b"\x01\x10\x01\x92", lambda v: sum(v) >= 9, b"\x09"),
87 ],
88 )
89 def test_shrink_bytes(initial, predicate, expected):
90 assert bytes(Bytes.shrink(initial, predicate, min_size=len(expected))) == expected
91
92
93 def test_collection_left_is_better():
94 shrinker = Collection(
95 [1, 2, 3], lambda v: True, ElementShrinker=Integer, min_size=3
96 )
97 assert not shrinker.left_is_better([1, 2, 3], [1, 2, 3])