| 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 enum |
| 12 |
import functools |
| 13 |
import itertools |
| 14 |
import operator |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from hypothesis import given, settings, strategies as st |
| 19 |
from hypothesis.errors import InvalidArgument |
| 20 |
from hypothesis.internal.compat import bit_count |
| 21 |
from hypothesis.strategies._internal.strategies import SampledFromStrategy |
| 22 |
|
| 23 |
from tests.common.debug import find_any, minimal |
| 24 |
from tests.common.utils import fails_with |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize("size", [100, 10**5, 10**6, 2**25]) |
| 28 |
@given(data=st.data()) |
| 29 |
def test_filter_large_lists(data, size): |
| 30 |
n_calls = 0 |
| 31 |
|
| 32 |
def cond(x): |
| 33 |
nonlocal n_calls |
| 34 |
n_calls += 1 |
| 35 |
return x % 2 != 0 |
| 36 |
|
| 37 |
s = data.draw(st.sampled_from(range(size)).filter(cond)) |
| 38 |
|
| 39 |
assert s % 2 != 0 |
| 40 |
assert n_calls <= SampledFromStrategy._MAX_FILTER_CALLS |
| 41 |
|
| 42 |
|
| 43 |
def rare_value_strategy(n, target): |
| 44 |
def forbid(s, forbidden): |
| 45 |
"""Helper function to avoid Python variable scoping issues.""" |
| 46 |
return s.filter(lambda x: x != forbidden) |
| 47 |
|
| 48 |
s = st.sampled_from(range(n)) |
| 49 |
for i in range(n): |
| 50 |
if i != target: |
| 51 |
s = forbid(s, i) |
| 52 |
|
| 53 |
return s |
| 54 |
|
| 55 |
|
| 56 |
@given(rare_value_strategy(n=128, target=80)) |
| 57 |
def test_chained_filters_find_rare_value(x): |
| 58 |
assert x == 80 |
| 59 |
|
| 60 |
|
| 61 |
@fails_with(InvalidArgument) |
| 62 |
@given(st.sets(st.sampled_from(range(10)), min_size=11)) |
| 63 |
def test_unsat_sets_of_samples(x): |
| 64 |
raise AssertionError |
| 65 |
|
| 66 |
|
| 67 |
@given(st.sets(st.sampled_from(range(50)), min_size=50)) |
| 68 |
def test_efficient_sets_of_samples(x): |
| 69 |
assert x == set(range(50)) |
| 70 |
|
| 71 |
|
| 72 |
class AnEnum(enum.Enum): |
| 73 |
a = enum.auto() |
| 74 |
b = enum.auto() |
| 75 |
|
| 76 |
|
| 77 |
def test_enum_repr_uses_class_not_a_list(): |
| 78 |
# The repr should have enough detail to find the class again |
| 79 |
# (which is very useful for the ghostwriter logic we're working on) |
| 80 |
lazy_repr = repr(st.sampled_from(AnEnum)) |
| 81 |
assert lazy_repr == "sampled_from(tests.nocover.test_sampled_from.AnEnum)" |
| 82 |
|
| 83 |
|
| 84 |
def test_repr_truncates_with_many_elements(): |
| 85 |
s = st.sampled_from(list(range(10_000))) |
| 86 |
repr_limit = 512 |
| 87 |
assert repr(s) == f"sampled_from([{', '.join(map(str, range(repr_limit)))}, ...])" |
| 88 |
|
| 89 |
|
| 90 |
class AFlag(enum.Flag): |
| 91 |
a = enum.auto() |
| 92 |
b = enum.auto() |
| 93 |
c = enum.auto() |
| 94 |
|
| 95 |
|
| 96 |
LargeFlag = enum.Flag("LargeFlag", {f"bit{i}": enum.auto() for i in range(64)}) |
| 97 |
|
| 98 |
|
| 99 |
class UnnamedFlag(enum.Flag): |
| 100 |
# Would fail under EnumCheck.NAMED_FLAGS |
| 101 |
a = 0 |
| 102 |
b = 7 |
| 103 |
|
| 104 |
|
| 105 |
def test_flag_enum_repr_uses_class_not_a_list(): |
| 106 |
lazy_repr = repr(st.sampled_from(AFlag)) |
| 107 |
assert lazy_repr == "sampled_from(tests.nocover.test_sampled_from.AFlag)" |
| 108 |
|
| 109 |
|
| 110 |
def test_exhaustive_flags(): |
| 111 |
# Generate powerset of flag combinations. There are only 2^3 of them, so |
| 112 |
# we can reasonably expect that they are all are found. |
| 113 |
unseen_flags = { |
| 114 |
functools.reduce(operator.or_, flaglist, AFlag(0)) |
| 115 |
for r in range(len(AFlag) + 1) |
| 116 |
for flaglist in itertools.combinations(AFlag, r) |
| 117 |
} |
| 118 |
|
| 119 |
@given(st.sampled_from(AFlag)) |
| 120 |
def accept(flag): |
| 121 |
unseen_flags.discard(flag) |
| 122 |
|
| 123 |
accept() |
| 124 |
|
| 125 |
assert not unseen_flags |
| 126 |
|
| 127 |
|
| 128 |
def test_flags_minimize_to_first_named_flag(): |
| 129 |
assert minimal(st.sampled_from(LargeFlag)) == LargeFlag.bit0 |
| 130 |
|
| 131 |
|
| 132 |
def test_flags_minimizes_bit_count(): |
| 133 |
assert ( |
| 134 |
minimal(st.sampled_from(LargeFlag), lambda f: bit_count(f.value) > 1) |
| 135 |
== LargeFlag.bit0 | LargeFlag.bit1 |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.skipif( |
| 140 |
settings.get_current_profile_name() == "crosshair", |
| 141 |
reason="takes ~10 mins; path tree is too large", |
| 142 |
) |
| 143 |
def test_flags_finds_all_bits_set(): |
| 144 |
assert find_any(st.sampled_from(LargeFlag), lambda f: f == ~LargeFlag(0)) |
| 145 |
|
| 146 |
|
| 147 |
def test_sample_unnamed_alias(): |
| 148 |
assert find_any(st.sampled_from(UnnamedFlag), lambda f: f == UnnamedFlag.b) |
| 149 |
|
| 150 |
|
| 151 |
def test_shrink_to_named_empty(): |
| 152 |
assert minimal(st.sampled_from(UnnamedFlag)) == UnnamedFlag(0) |