| 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 fractions import Fraction |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import ( |
| 16 |
HealthCheck, |
| 17 |
Phase, |
| 18 |
assume, |
| 19 |
example, |
| 20 |
given, |
| 21 |
settings, |
| 22 |
strategies as st, |
| 23 |
) |
| 24 |
from hypothesis.errors import InvalidArgument |
| 25 |
from hypothesis.internal.conjecture import utils as cu |
| 26 |
from hypothesis.internal.conjecture.data import ConjectureData, Status, StopTest |
| 27 |
from hypothesis.internal.coverage import IN_COVERAGE_TESTS |
| 28 |
|
| 29 |
try: |
| 30 |
import numpy as np |
| 31 |
except ImportError: |
| 32 |
np = None |
| 33 |
|
| 34 |
|
| 35 |
def test_drawing_certain_coin_still_writes(): |
| 36 |
data = ConjectureData.for_choices([True]) |
| 37 |
assert data.draw_boolean(1) |
| 38 |
assert data.choices == (True,) |
| 39 |
|
| 40 |
|
| 41 |
def test_drawing_impossible_coin_still_writes(): |
| 42 |
data = ConjectureData.for_choices([False]) |
| 43 |
assert not data.draw_boolean(0) |
| 44 |
assert data.choices == (False,) |
| 45 |
|
| 46 |
|
| 47 |
def test_draws_extremely_small_p(): |
| 48 |
data = ConjectureData.for_choices((True,)) |
| 49 |
assert data.draw_boolean(0.5**65) |
| 50 |
|
| 51 |
|
| 52 |
@example([Fraction(1, 3), Fraction(1, 3), Fraction(1, 3)]) |
| 53 |
@example([Fraction(1, 1), Fraction(1, 2)]) |
| 54 |
@example([Fraction(1, 2), Fraction(4, 10)]) |
| 55 |
@example([Fraction(1, 1), Fraction(3, 5), Fraction(1, 1)]) |
| 56 |
@example([Fraction(2, 257), Fraction(2, 5), Fraction(1, 11)]) |
| 57 |
@example([0, 2, 47]) |
| 58 |
@settings( |
| 59 |
deadline=None, |
| 60 |
suppress_health_check=list(HealthCheck), |
| 61 |
phases=[Phase.explicit] if IN_COVERAGE_TESTS else settings.default.phases, |
| 62 |
) |
| 63 |
@given(st.lists(st.fractions(min_value=0, max_value=1), min_size=1)) |
| 64 |
def test_sampler_distribution(weights): |
| 65 |
total = sum(weights) |
| 66 |
n = len(weights) |
| 67 |
|
| 68 |
assume(total > 0) |
| 69 |
|
| 70 |
probabilities = [w / total for w in weights] |
| 71 |
|
| 72 |
sampler = cu.Sampler(weights) |
| 73 |
|
| 74 |
calculated = [Fraction(0)] * n |
| 75 |
for base, alternate, p_alternate in sampler.table: |
| 76 |
calculated[base] += (1 - p_alternate) / n |
| 77 |
calculated[alternate] += p_alternate / n |
| 78 |
|
| 79 |
for expected, actual in zip(probabilities, calculated, strict=True): |
| 80 |
if isinstance(actual, Fraction): |
| 81 |
assert expected == actual |
| 82 |
else: |
| 83 |
assert abs(expected - actual) < 0.001 |
| 84 |
|
| 85 |
|
| 86 |
def test_sampler_does_not_draw_minimum_if_zero(): |
| 87 |
sampler = cu.Sampler([0, 2, 47]) |
| 88 |
assert sampler.sample(ConjectureData.for_choices([0, 0])) != 0 |
| 89 |
|
| 90 |
|
| 91 |
def test_sampler_shrinks(): |
| 92 |
sampler = cu.Sampler([4.0, 8.0, 1.0, 1.0, 0.5]) |
| 93 |
assert sampler.sample(ConjectureData.for_choices([0] * 3)) == 0 |
| 94 |
|
| 95 |
|
| 96 |
def test_can_force_sampler(): |
| 97 |
sampler = cu.Sampler([0.5, 0.5]) |
| 98 |
cd = ConjectureData.for_choices([0] * 100) |
| 99 |
assert sampler.sample(cd, forced=0) == 0 |
| 100 |
assert sampler.sample(cd, forced=1) == 1 |
| 101 |
|
| 102 |
|
| 103 |
def test_combine_labels_is_distinct(): |
| 104 |
x = 10 |
| 105 |
y = 100 |
| 106 |
assert cu.combine_labels(x, y) not in (x, y) |
| 107 |
|
| 108 |
|
| 109 |
@given(st.integers()) |
| 110 |
def test_combine_labels_is_identity_for_single_argument(n): |
| 111 |
assert cu.combine_labels(n) == n |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.skipif(np is None, reason="requires Numpy") |
| 115 |
def test_invalid_numpy_sample(): |
| 116 |
with pytest.raises(InvalidArgument): |
| 117 |
cu.check_sample(np.array([[1, 1], [1, 1]]), "array") |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.skipif(np is None, reason="requires Numpy") |
| 121 |
def test_valid_numpy_sample(): |
| 122 |
cu.check_sample(np.array([1, 2, 3]), "array") |
| 123 |
|
| 124 |
|
| 125 |
def test_invalid_set_sample(): |
| 126 |
with pytest.raises(InvalidArgument): |
| 127 |
cu.check_sample({1, 2, 3}, "array") |
| 128 |
|
| 129 |
|
| 130 |
def test_valid_list_sample(): |
| 131 |
cu.check_sample([1, 2, 3], "array") |
| 132 |
|
| 133 |
|
| 134 |
def test_choice(): |
| 135 |
assert ConjectureData.for_choices([1]).choice([1, 2, 3]) == 2 |
| 136 |
|
| 137 |
|
| 138 |
def test_fixed_size_draw_many(): |
| 139 |
many = cu.many( |
| 140 |
ConjectureData.for_choices([]), min_size=3, max_size=3, average_size=3 |
| 141 |
) |
| 142 |
assert many.more() |
| 143 |
assert many.more() |
| 144 |
assert many.more() |
| 145 |
assert not many.more() |
| 146 |
|
| 147 |
|
| 148 |
def test_astronomically_unlikely_draw_many(): |
| 149 |
# Our internal helper doesn't underflow to zero or negative, but nor |
| 150 |
# will we ever generate an element for such a low average size. |
| 151 |
data = ConjectureData.for_choices((True,) * 1000) |
| 152 |
many = cu.many(data, min_size=0, max_size=10, average_size=1e-5) |
| 153 |
assert many.more() |
| 154 |
|
| 155 |
|
| 156 |
def test_rejection_eventually_terminates_many(): |
| 157 |
many = cu.many( |
| 158 |
ConjectureData.for_choices((True,) * 1000), |
| 159 |
min_size=0, |
| 160 |
max_size=1000, |
| 161 |
average_size=100, |
| 162 |
) |
| 163 |
count = 0 |
| 164 |
|
| 165 |
while many.more(): |
| 166 |
count += 1 |
| 167 |
many.reject() |
| 168 |
|
| 169 |
assert count <= 100 |
| 170 |
|
| 171 |
|
| 172 |
def test_rejection_eventually_terminates_many_invalid_for_min_size(): |
| 173 |
data = ConjectureData.for_choices((True,) * 1000) |
| 174 |
many = cu.many(data, min_size=1, max_size=1000, average_size=100) |
| 175 |
|
| 176 |
with pytest.raises(StopTest): |
| 177 |
while many.more(): |
| 178 |
many.reject() |
| 179 |
|
| 180 |
assert data.status == Status.INVALID |
| 181 |
|
| 182 |
|
| 183 |
def test_many_with_min_size(): |
| 184 |
many = cu.many( |
| 185 |
ConjectureData.for_choices((False,) * 5), |
| 186 |
min_size=2, |
| 187 |
average_size=10, |
| 188 |
max_size=1000, |
| 189 |
) |
| 190 |
assert many.more() |
| 191 |
assert many.more() |
| 192 |
assert not many.more() |
| 193 |
|
| 194 |
|
| 195 |
def test_many_with_max_size(): |
| 196 |
many = cu.many( |
| 197 |
ConjectureData.for_choices((True,) * 5), min_size=0, average_size=1, max_size=2 |
| 198 |
) |
| 199 |
assert many.more() |
| 200 |
assert many.more() |
| 201 |
assert not many.more() |
| 202 |
|
| 203 |
|
| 204 |
def test_samples_from_a_range_directly(): |
| 205 |
s = cu.check_sample(range(10**1000), "") |
| 206 |
assert isinstance(s, range) |
| 207 |
|
| 208 |
|
| 209 |
def test_p_continue_to_average_saturates(): |
| 210 |
assert cu._p_continue_to_avg(1.1, 100) == 100 |
| 211 |
|
| 212 |
|
| 213 |
def test_unhashable_calc_label(): |
| 214 |
|
| 215 |
class Unhashable: |
| 216 |
def __call__(self): |
| 217 |
return None |
| 218 |
|
| 219 |
def __hash__(self): |
| 220 |
raise TypeError |
| 221 |
|
| 222 |
c1 = Unhashable() |
| 223 |
c2 = Unhashable() |
| 224 |
|
| 225 |
with pytest.raises(TypeError): |
| 226 |
assert cu.calc_label_from_hash(c1) == cu.calc_label_from_hash(c2) |
| 227 |
assert cu.calc_label_from_callable(c1) == cu.calc_label_from_callable(c2) |