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 98 99 100 101 102 103 104 105 106 107 108 109
|
# 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 math import log
from random import Random
from hypothesis import (
HealthCheck,
Phase,
Verbosity,
assume,
example,
given,
settings,
strategies as st,
)
from hypothesis.internal.compat import ceil
from hypothesis.internal.conjecture.data import ConjectureData
from hypothesis.internal.conjecture.engine import ConjectureRunner
from tests.conjecture.common import interesting_origin
@st.composite
def problems(draw):
m = draw(st.integers(256, 2**64 - 1))
assume(m > 0)
marker = draw(st.binary(max_size=8))
bound = draw(st.integers(0, m - 1))
return (m, marker, bound)
base_settings = settings(
database=None,
deadline=None,
suppress_health_check=list(HealthCheck),
max_examples=10,
verbosity=Verbosity.normal,
phases=(Phase.explicit, Phase.generate),
)
@example((4503599627370496, b"", 2861143707951135))
@example((88305152, b"%\x1b\xa0\xfa", 12394667))
@example((99742672384, b"\xf5|", 24300326997))
@example((1454610481571840, b"", 1076887621690235))
@example((15616, b"", 2508))
@example((65536, b"", 20048))
@example((256, b"", 0))
@example((512, b"", 258))
@example((2048, b"", 1792))
@example((3072, b"", 0))
@example((256, b"", 1))
@settings(
base_settings,
verbosity=Verbosity.normal,
phases=(
# We disable shrinking for this test because when it fails it's a sign
# that the shrinker is working really badly, so it ends up very slow!
Phase.explicit,
Phase.generate,
),
max_examples=20,
)
@given(problems())
def test_avoids_zig_zag_trap(p):
m, marker, lower_bound = p
n_bits = m.bit_length() + 1
def test_function(data):
m = data.draw_integer(0, 2**n_bits - 1)
if m < lower_bound:
data.mark_invalid()
n = data.draw_integer(0, 2**n_bits - 1)
if data.draw_bytes(len(marker), len(marker)) != marker:
data.mark_invalid()
if abs(m - n) == 1:
data.mark_interesting(interesting_origin())
runner = ConjectureRunner(
test_function,
database_key=None,
settings=settings(base_settings, phases=(Phase.generate, Phase.shrink)),
random=Random(0),
)
runner.cached_test_function((m, m + 1, marker))
assert runner.interesting_examples
runner.run()
(v,) = runner.interesting_examples.values()
data = ConjectureData.for_choices(v.choices)
m = data.draw_integer(0, 2**n_bits - 1)
n = data.draw_integer(0, 2**n_bits - 1)
assert m == lower_bound
if m == 0:
assert n == 1
else:
assert n == m - 1
budget = 2 * n_bits * ceil(log(n_bits, 2)) + 2
assert runner.shrinks <= budget
|