| 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 contextlib |
| 12 |
import itertools |
| 13 |
from random import Random |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import HealthCheck, Phase, settings, strategies as st |
| 18 |
from hypothesis.database import ( |
| 19 |
InMemoryExampleDatabase, |
| 20 |
choices_from_bytes, |
| 21 |
choices_to_bytes, |
| 22 |
) |
| 23 |
from hypothesis.errors import StopTest |
| 24 |
from hypothesis.internal.conjecture.data import ConjectureData, Status |
| 25 |
from hypothesis.internal.conjecture.engine import ConjectureRunner, RunIsComplete |
| 26 |
from hypothesis.internal.conjecture.pareto import ParetoFront |
| 27 |
|
| 28 |
from tests.conjecture.common import interesting_origin |
| 29 |
|
| 30 |
|
| 31 |
def test_pareto_front_contains_different_interesting_reasons(): |
| 32 |
def test(data): |
| 33 |
data.target_observations[""] = 1 |
| 34 |
n = data.draw_integer(0, 2**4 - 1) |
| 35 |
data.mark_interesting(interesting_origin(n)) |
| 36 |
|
| 37 |
runner = ConjectureRunner( |
| 38 |
test, |
| 39 |
settings=settings( |
| 40 |
max_examples=5000, |
| 41 |
database=InMemoryExampleDatabase(), |
| 42 |
suppress_health_check=list(HealthCheck), |
| 43 |
), |
| 44 |
database_key=b"stuff", |
| 45 |
random=Random(0), |
| 46 |
) |
| 47 |
|
| 48 |
runner.run() |
| 49 |
assert len(runner.pareto_front) == 2**4 |
| 50 |
|
| 51 |
|
| 52 |
def test_pareto_front_omits_invalid_examples(): |
| 53 |
def test(data): |
| 54 |
x = data.draw_integer(0, 2**4 - 1) |
| 55 |
if x % 2: |
| 56 |
data.target_observations[""] = 1 |
| 57 |
data.mark_invalid() |
| 58 |
|
| 59 |
runner = ConjectureRunner( |
| 60 |
test, |
| 61 |
settings=settings( |
| 62 |
max_examples=5000, |
| 63 |
database=InMemoryExampleDatabase(), |
| 64 |
suppress_health_check=list(HealthCheck), |
| 65 |
), |
| 66 |
database_key=b"stuff", |
| 67 |
random=Random(0), |
| 68 |
) |
| 69 |
|
| 70 |
runner.run() |
| 71 |
assert len(runner.pareto_front) == 0 |
| 72 |
|
| 73 |
|
| 74 |
def test_database_contains_only_pareto_front(): |
| 75 |
def test(data): |
| 76 |
data.target_observations["1"] = data.draw(st.integers(0, 2**4)) |
| 77 |
data.draw(st.integers(0, 2**64)) |
| 78 |
data.target_observations["2"] = data.draw(st.integers(0, 2**8)) |
| 79 |
|
| 80 |
assert len(set(db.fetch(b"stuff.pareto"))) == len(runner.pareto_front) |
| 81 |
|
| 82 |
db = InMemoryExampleDatabase() |
| 83 |
runner = ConjectureRunner( |
| 84 |
test, |
| 85 |
settings=settings( |
| 86 |
max_examples=500, database=db, suppress_health_check=list(HealthCheck) |
| 87 |
), |
| 88 |
database_key=b"stuff", |
| 89 |
random=Random(0), |
| 90 |
) |
| 91 |
runner.run() |
| 92 |
|
| 93 |
assert len(runner.pareto_front) <= 500 |
| 94 |
for v in runner.pareto_front: |
| 95 |
assert v.status >= Status.VALID |
| 96 |
|
| 97 |
values = set(db.fetch(b"stuff.pareto")) |
| 98 |
assert len(values) == len(runner.pareto_front), { |
| 99 |
choices_to_bytes(data.choices) for data in runner.pareto_front |
| 100 |
}.symmetric_difference(values) |
| 101 |
|
| 102 |
for data in runner.pareto_front: |
| 103 |
assert choices_to_bytes(data.choices) in values |
| 104 |
assert data in runner.pareto_front |
| 105 |
|
| 106 |
for b in values: |
| 107 |
choices = choices_from_bytes(b) |
| 108 |
assert runner.cached_test_function(choices) in runner.pareto_front |
| 109 |
|
| 110 |
|
| 111 |
def test_clears_defunct_pareto_front(): |
| 112 |
def test(data): |
| 113 |
data.target_observations[""] = 1 |
| 114 |
data.draw_integer(0, 2**8 - 1) |
| 115 |
data.draw_integer(0, 2**8 - 1) |
| 116 |
|
| 117 |
db = InMemoryExampleDatabase() |
| 118 |
|
| 119 |
runner = ConjectureRunner( |
| 120 |
test, |
| 121 |
settings=settings( |
| 122 |
max_examples=10000, |
| 123 |
database=db, |
| 124 |
suppress_health_check=list(HealthCheck), |
| 125 |
phases=[Phase.reuse], |
| 126 |
), |
| 127 |
database_key=b"stuff", |
| 128 |
random=Random(0), |
| 129 |
) |
| 130 |
|
| 131 |
for i in range(256): |
| 132 |
db.save(runner.pareto_key, choices_to_bytes((i, 0))) |
| 133 |
|
| 134 |
runner.run() |
| 135 |
assert len(list(db.fetch(runner.pareto_key))) == 1 |
| 136 |
|
| 137 |
|
| 138 |
def test_down_samples_the_pareto_front(): |
| 139 |
def test(data): |
| 140 |
data.draw_integer(0, 2**8 - 1) |
| 141 |
data.draw_integer(0, 2**8 - 1) |
| 142 |
|
| 143 |
db = InMemoryExampleDatabase() |
| 144 |
|
| 145 |
runner = ConjectureRunner( |
| 146 |
test, |
| 147 |
settings=settings( |
| 148 |
max_examples=1000, |
| 149 |
database=db, |
| 150 |
suppress_health_check=list(HealthCheck), |
| 151 |
phases=[Phase.reuse], |
| 152 |
), |
| 153 |
database_key=b"stuff", |
| 154 |
random=Random(0), |
| 155 |
) |
| 156 |
|
| 157 |
for n1, n2 in itertools.product(range(256), range(256)): |
| 158 |
db.save(runner.pareto_key, choices_to_bytes((n1, n2))) |
| 159 |
|
| 160 |
with pytest.raises(RunIsComplete): |
| 161 |
runner.reuse_existing_examples() |
| 162 |
|
| 163 |
assert runner.valid_examples == 1000 |
| 164 |
|
| 165 |
|
| 166 |
def test_stops_loading_pareto_front_if_interesting(): |
| 167 |
def test(data): |
| 168 |
data.draw_integer() |
| 169 |
data.draw_integer() |
| 170 |
data.mark_interesting(interesting_origin()) |
| 171 |
|
| 172 |
db = InMemoryExampleDatabase() |
| 173 |
|
| 174 |
runner = ConjectureRunner( |
| 175 |
test, |
| 176 |
settings=settings( |
| 177 |
max_examples=1000, |
| 178 |
database=db, |
| 179 |
suppress_health_check=list(HealthCheck), |
| 180 |
phases=[Phase.reuse], |
| 181 |
), |
| 182 |
database_key=b"stuff", |
| 183 |
random=Random(0), |
| 184 |
) |
| 185 |
|
| 186 |
for n1, n2 in itertools.product(range(256), range(256)): |
| 187 |
db.save(runner.pareto_key, choices_to_bytes((n1, n2))) |
| 188 |
|
| 189 |
runner.reuse_existing_examples() |
| 190 |
assert runner.call_count == 1 |
| 191 |
|
| 192 |
|
| 193 |
def test_uses_tags_in_calculating_pareto_front(): |
| 194 |
def test(data): |
| 195 |
data.target_observations[""] = 1 |
| 196 |
if data.draw_boolean(): |
| 197 |
data.start_span(11) |
| 198 |
data.draw_integer(0, 2**8 - 1) |
| 199 |
data.stop_span() |
| 200 |
|
| 201 |
runner = ConjectureRunner( |
| 202 |
test, |
| 203 |
settings=settings(max_examples=10, database=InMemoryExampleDatabase()), |
| 204 |
database_key=b"stuff", |
| 205 |
random=Random(0), |
| 206 |
) |
| 207 |
|
| 208 |
runner.run() |
| 209 |
assert len(runner.pareto_front) == 2 |
| 210 |
|
| 211 |
|
| 212 |
def test_optimises_the_pareto_front(): |
| 213 |
def test(data): |
| 214 |
count = 0 |
| 215 |
while data.draw_integer(0, 2**8 - 1): |
| 216 |
count += 1 |
| 217 |
|
| 218 |
data.target_observations[""] = min(count, 5) |
| 219 |
|
| 220 |
runner = ConjectureRunner( |
| 221 |
test, |
| 222 |
settings=settings(max_examples=10000, database=InMemoryExampleDatabase()), |
| 223 |
database_key=b"stuff", |
| 224 |
) |
| 225 |
runner.cached_test_function([255] * 20 + [0]) |
| 226 |
runner.pareto_optimise() |
| 227 |
|
| 228 |
assert len(runner.pareto_front) == 6 |
| 229 |
for i, data in enumerate(runner.pareto_front): |
| 230 |
assert data.choices == (1,) * i + (0,) |
| 231 |
|
| 232 |
|
| 233 |
def test_does_not_optimise_the_pareto_front_if_interesting(): |
| 234 |
def test(data): |
| 235 |
n = data.draw_integer(0, 2**8 - 1) |
| 236 |
data.target_observations[""] = n |
| 237 |
if n == 255: |
| 238 |
data.mark_interesting(interesting_origin()) |
| 239 |
|
| 240 |
runner = ConjectureRunner( |
| 241 |
test, |
| 242 |
settings=settings(max_examples=10000, database=InMemoryExampleDatabase()), |
| 243 |
database_key=b"stuff", |
| 244 |
) |
| 245 |
|
| 246 |
runner.cached_test_function([0]) |
| 247 |
runner.pareto_optimise = None |
| 248 |
runner.optimise_targets() |
| 249 |
|
| 250 |
assert runner.interesting_examples |
| 251 |
|
| 252 |
|
| 253 |
def test_stops_optimising_once_interesting(): |
| 254 |
hi = 2**16 - 1 |
| 255 |
|
| 256 |
def test(data): |
| 257 |
n = data.draw_integer(0, 2**16 - 1) |
| 258 |
data.target_observations[""] = n |
| 259 |
if n < hi: |
| 260 |
data.mark_interesting(interesting_origin()) |
| 261 |
|
| 262 |
runner = ConjectureRunner( |
| 263 |
test, |
| 264 |
settings=settings(max_examples=10000, database=InMemoryExampleDatabase()), |
| 265 |
database_key=b"stuff", |
| 266 |
) |
| 267 |
|
| 268 |
data = runner.cached_test_function([hi]) |
| 269 |
assert data.status == Status.VALID |
| 270 |
runner.pareto_optimise() |
| 271 |
assert runner.call_count <= 20 |
| 272 |
assert runner.interesting_examples |
| 273 |
|
| 274 |
|
| 275 |
def test_pareto_contains(): |
| 276 |
front = ParetoFront(random=Random()) |
| 277 |
assert "not a data" not in front |
| 278 |
|
| 279 |
data = ConjectureData.for_choices([]) |
| 280 |
with contextlib.suppress(StopTest): |
| 281 |
data.mark_overrun() |
| 282 |
# check a data which turns into an Overrun when .as_result is called |
| 283 |
assert data not in front |