Debian Codegraph @

hypothesis-python/tests/quality/test_zig_zagging.py

raw
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 math import log
12 from random import Random
13
14 from hypothesis import (
15 HealthCheck,
16 Phase,
17 Verbosity,
18 assume,
19 example,
20 given,
21 settings,
22 strategies as st,
23 )
24 from hypothesis.internal.compat import ceil
25 from hypothesis.internal.conjecture.data import ConjectureData
26 from hypothesis.internal.conjecture.engine import ConjectureRunner
27
28 from tests.conjecture.common import interesting_origin
29
30
31 @st.composite
32 def problems(draw):
33 m = draw(st.integers(256, 2**64 - 1))
34 assume(m > 0)
35 marker = draw(st.binary(max_size=8))
36 bound = draw(st.integers(0, m - 1))
37 return (m, marker, bound)
38
39
40 base_settings = settings(
41 database=None,
42 deadline=None,
43 suppress_health_check=list(HealthCheck),
44 max_examples=10,
45 verbosity=Verbosity.normal,
46 phases=(Phase.explicit, Phase.generate),
47 )
48
49
50 @example((4503599627370496, b"", 2861143707951135))
51 @example((88305152, b"%\x1b\xa0\xfa", 12394667))
52 @example((99742672384, b"\xf5|", 24300326997))
53 @example((1454610481571840, b"", 1076887621690235))
54 @example((15616, b"", 2508))
55 @example((65536, b"", 20048))
56 @example((256, b"", 0))
57 @example((512, b"", 258))
58 @example((2048, b"", 1792))
59 @example((3072, b"", 0))
60 @example((256, b"", 1))
61 @settings(
62 base_settings,
63 verbosity=Verbosity.normal,
64 phases=(
65 # We disable shrinking for this test because when it fails it's a sign
66 # that the shrinker is working really badly, so it ends up very slow!
67 Phase.explicit,
68 Phase.generate,
69 ),
70 max_examples=20,
71 )
72 @given(problems())
73 def test_avoids_zig_zag_trap(p):
74 m, marker, lower_bound = p
75 n_bits = m.bit_length() + 1
76
77 def test_function(data):
78 m = data.draw_integer(0, 2**n_bits - 1)
79 if m < lower_bound:
80 data.mark_invalid()
81 n = data.draw_integer(0, 2**n_bits - 1)
82 if data.draw_bytes(len(marker), len(marker)) != marker:
83 data.mark_invalid()
84 if abs(m - n) == 1:
85 data.mark_interesting(interesting_origin())
86
87 runner = ConjectureRunner(
88 test_function,
89 database_key=None,
90 settings=settings(base_settings, phases=(Phase.generate, Phase.shrink)),
91 random=Random(0),
92 )
93
94 runner.cached_test_function((m, m + 1, marker))
95 assert runner.interesting_examples
96 runner.run()
97 (v,) = runner.interesting_examples.values()
98 data = ConjectureData.for_choices(v.choices)
99
100 m = data.draw_integer(0, 2**n_bits - 1)
101 n = data.draw_integer(0, 2**n_bits - 1)
102 assert m == lower_bound
103 if m == 0:
104 assert n == 1
105 else:
106 assert n == m - 1
107
108 budget = 2 * n_bits * ceil(log(n_bits, 2)) + 2
109 assert runner.shrinks <= budget