| 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 pytest |
| 12 |
|
| 13 |
from hypothesis import Phase, given, seed, settings, strategies as st, target |
| 14 |
|
| 15 |
from tests.common.utils import Why, xfail_on_crosshair |
| 16 |
|
| 17 |
pytest_plugins = "pytester" |
| 18 |
|
| 19 |
TESTSUITE = """ |
| 20 |
from hypothesis import given, strategies as st, target |
| 21 |
|
| 22 |
@given(st.integers(min_value=0)) |
| 23 |
def test_threshold_problem(x): |
| 24 |
target(float(x)) |
| 25 |
{0}target(float(x * 2), label="double") |
| 26 |
{0}assert x <= 100000 |
| 27 |
assert x <= 100 |
| 28 |
""" |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize("multiple", [False, True]) |
| 32 |
def test_reports_target_results(testdir, multiple): |
| 33 |
script = testdir.makepyfile(TESTSUITE.format("" if multiple else "# ")) |
| 34 |
result = testdir.runpytest(script, "--tb=native", "-rN") |
| 35 |
out = "\n".join(result.stdout.lines) |
| 36 |
assert "Falsifying example" in out |
| 37 |
assert "x=101" in out, out |
| 38 |
assert out.count("Highest target score") == 1 |
| 39 |
assert result.ret != 0 |
| 40 |
|
| 41 |
|
| 42 |
def test_targeting_increases_max_length(): |
| 43 |
strat = st.lists(st.booleans()) |
| 44 |
|
| 45 |
@settings(database=None, max_examples=200, phases=[Phase.generate, Phase.target]) |
| 46 |
@given(strat) |
| 47 |
def test_with_targeting(ls): |
| 48 |
target(float(len(ls))) |
| 49 |
assert len(ls) <= 80 |
| 50 |
|
| 51 |
with pytest.raises(AssertionError): |
| 52 |
test_with_targeting() |
| 53 |
|
| 54 |
|
| 55 |
@given(st.integers(), st.integers()) |
| 56 |
def test_target_returns_value(a, b): |
| 57 |
difference = target(abs(a - b)) |
| 58 |
assert difference == abs(a - b) |
| 59 |
assert isinstance(difference, int) |
| 60 |
|
| 61 |
|
| 62 |
@xfail_on_crosshair(Why.symbolic_outside_context) |
| 63 |
def test_targeting_can_be_disabled(): |
| 64 |
strat = st.lists(st.integers(0, 255)) |
| 65 |
|
| 66 |
def score(enabled): |
| 67 |
result = 0 |
| 68 |
phases = [Phase.generate] |
| 69 |
if enabled: |
| 70 |
phases.append(Phase.target) |
| 71 |
|
| 72 |
@seed(0) |
| 73 |
@settings(database=None, max_examples=100, phases=phases) |
| 74 |
@given(strat) |
| 75 |
def test(ls): |
| 76 |
nonlocal result |
| 77 |
# cap the score to avoid long test times by unbounded driving of list |
| 78 |
# length upwards |
| 79 |
score = min(sum(ls), 10_000) |
| 80 |
result = max(result, score) |
| 81 |
target(score) |
| 82 |
|
| 83 |
test() |
| 84 |
return result |
| 85 |
|
| 86 |
assert score(enabled=True) > score(enabled=False) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.skipif( |
| 90 |
settings.get_current_profile_name() == "crosshair", |
| 91 |
reason="takes ~15 minutes, mostly just unrolling the rejection sampling loop", |
| 92 |
) |
| 93 |
def test_issue_2395_regression(): |
| 94 |
@given(d=st.floats().filter(lambda x: abs(x) < 1000)) |
| 95 |
@settings(max_examples=1000, database=None) |
| 96 |
@seed(93962505385993024185959759429298090872) |
| 97 |
def test_targeting_square_loss(d): |
| 98 |
target(-((d - 42.5) ** 2.0)) |
| 99 |
|
| 100 |
test_targeting_square_loss() |