1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
from collections import Counter
import pytest
from hypothesis.internal.conjecture.shrinking import (
Bytes,
Collection,
Integer,
Ordering,
String,
)
from hypothesis.internal.intervalsets import IntervalSet
def test_shrink_to_zero():
assert Integer.shrink(2**16, lambda n: True) == 0
def test_shrink_to_smallest():
assert Integer.shrink(2**16, lambda n: n > 10) == 11
def test_can_sort_bytes_by_reordering():
start = bytes([5, 4, 3, 2, 1, 0])
finish = Ordering.shrink(start, lambda x: set(x) == set(start))
assert bytes(finish) == bytes([0, 1, 2, 3, 4, 5])
def test_can_sort_bytes_by_reordering_partially():
start = bytes([5, 4, 3, 2, 1, 0])
finish = Ordering.shrink(start, lambda x: set(x) == set(start) and x[0] > x[-1])
assert bytes(finish) == bytes([1, 2, 3, 4, 5, 0])
def test_can_sort_bytes_by_reordering_partially2():
start = bytes([5, 4, 3, 2, 1, 0])
finish = Ordering.shrink(
start,
lambda x: Counter(x) == Counter(start) and x[0] > x[2],
full=True,
)
assert bytes(finish) == bytes([1, 2, 0, 3, 4, 5])
def test_can_sort_bytes_by_reordering_partially_not_cross_stationary_element():
start = bytes([5, 3, 0, 2, 1, 4])
finish = Ordering.shrink(start, lambda x: set(x) == set(start) and x[3] == 2)
assert bytes(finish) == bytes([0, 1, 3, 2, 4, 5])
@pytest.mark.parametrize(
"initial, predicate, intervals, expected",
[
("f" * 10, lambda s: True, IntervalSet.from_string("abcdefg"), ""),
("f" * 10, lambda s: len(s) >= 3, IntervalSet.from_string("abcdefg"), "aaa"),
(
"f" * 10,
lambda s: len(s) >= 3 and "a" not in s,
IntervalSet.from_string("abcdefg"),
"bbb",
),
],
)
def test_shrink_strings(initial, predicate, intervals, expected):
assert String.shrink(
initial, predicate, intervals=intervals, min_size=len(expected)
) == tuple(expected)
@pytest.mark.parametrize(
"initial, predicate, expected",
[
(b"\x18\x12", lambda v: len(v) == 2, b"\x00\x00"),
(b"\x18\x12", lambda v: True, b""),
(b"\x01\x10", lambda v: len(v) > 0 and v[0] == 1, b"\x01"),
(b"\x01\x10\x01\x92", lambda v: sum(v) >= 9, b"\x09"),
],
)
def test_shrink_bytes(initial, predicate, expected):
assert bytes(Bytes.shrink(initial, predicate, min_size=len(expected))) == expected
def test_collection_left_is_better():
shrinker = Collection(
[1, 2, 3], lambda v: True, ElementShrinker=Integer, min_size=3
)
assert not shrinker.left_is_better([1, 2, 3], [1, 2, 3])
|