|
| 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 math |
| 12 | +from typing import Optional |
| 13 | + |
| 14 | +from hypothesis.internal.compat import int_from_bytes |
| 15 | +from hypothesis.internal.conjecture.data import ( |
| 16 | + BYTE_MASKS, |
| 17 | + COLLECTION_DEFAULT_MAX_SIZE, |
| 18 | + ConjectureData, |
| 19 | + PrimitiveProvider, |
| 20 | + bits_to_bytes, |
| 21 | +) |
| 22 | +from hypothesis.internal.conjecture.floats import lex_to_float |
| 23 | +from hypothesis.internal.conjecture.utils import many |
| 24 | +from hypothesis.internal.floats import make_float_clamper |
| 25 | +from hypothesis.internal.intervalsets import IntervalSet |
| 26 | + |
| 27 | + |
| 28 | +class BytestringProvider(PrimitiveProvider): |
| 29 | + lifetime = "test_case" |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, conjecturedata: Optional["ConjectureData"], /, *, bytestring: bytes |
| 33 | + ): |
| 34 | + super().__init__(conjecturedata) |
| 35 | + self.bytestring = bytestring |
| 36 | + self.index = 0 |
| 37 | + self.drawn = bytearray() |
| 38 | + |
| 39 | + def _draw_bits(self, n): |
| 40 | + if n == 0: # pragma: no cover |
| 41 | + return 0 |
| 42 | + n_bytes = bits_to_bytes(n) |
| 43 | + if self.index + n_bytes > len(self.bytestring): |
| 44 | + self._cd.mark_overrun() |
| 45 | + buf = bytearray(self.bytestring[self.index : self.index + n_bytes]) |
| 46 | + self.index += n_bytes |
| 47 | + |
| 48 | + buf[0] &= BYTE_MASKS[n % 8] |
| 49 | + buf = bytes(buf) |
| 50 | + self.drawn += buf |
| 51 | + return int_from_bytes(buf) |
| 52 | + |
| 53 | + def draw_boolean( |
| 54 | + self, |
| 55 | + p: float = 0.5, |
| 56 | + *, |
| 57 | + forced: Optional[bool] = None, |
| 58 | + fake_forced: bool = False, |
| 59 | + ) -> bool: |
| 60 | + if forced is not None: |
| 61 | + return forced |
| 62 | + |
| 63 | + if p <= 0: |
| 64 | + return False |
| 65 | + if p >= 1: |
| 66 | + return True |
| 67 | + |
| 68 | + # always use one byte for booleans to maintain constant draw size. |
| 69 | + # If a probability requires more than 8 bits to represent precisely, |
| 70 | + # the result will be slightly biased, but not badly. |
| 71 | + bits = 8 |
| 72 | + size = 2**bits |
| 73 | + # always leave at least one value that can be true, even for very small |
| 74 | + # p. |
| 75 | + falsey = max(1, math.floor(size * (1 - p))) |
| 76 | + n = self._draw_bits(bits) |
| 77 | + return n >= falsey |
| 78 | + |
| 79 | + def draw_integer( |
| 80 | + self, |
| 81 | + min_value: Optional[int] = None, |
| 82 | + max_value: Optional[int] = None, |
| 83 | + *, |
| 84 | + weights: Optional[dict[int, float]] = None, |
| 85 | + shrink_towards: int = 0, |
| 86 | + forced: Optional[int] = None, |
| 87 | + fake_forced: bool = False, |
| 88 | + ) -> int: |
| 89 | + if forced is not None: |
| 90 | + return forced |
| 91 | + |
| 92 | + assert self._cd is not None |
| 93 | + |
| 94 | + # we explicitly ignore integer weights for now, as they are likely net |
| 95 | + # negative on fuzzer performance. |
| 96 | + |
| 97 | + if min_value is None and max_value is None: |
| 98 | + min_value = -(2**127) |
| 99 | + max_value = 2**127 - 1 |
| 100 | + elif min_value is None: |
| 101 | + assert max_value is not None |
| 102 | + min_value = max_value - 2**64 |
| 103 | + elif max_value is None: |
| 104 | + assert min_value is not None |
| 105 | + max_value = min_value + 2**64 |
| 106 | + |
| 107 | + if min_value == max_value: |
| 108 | + return min_value |
| 109 | + |
| 110 | + bits = (max_value - min_value).bit_length() |
| 111 | + value = self._draw_bits(bits) |
| 112 | + while not (min_value <= value <= max_value): |
| 113 | + value = self._draw_bits(bits) |
| 114 | + return value |
| 115 | + |
| 116 | + def draw_float( |
| 117 | + self, |
| 118 | + *, |
| 119 | + min_value: float = -math.inf, |
| 120 | + max_value: float = math.inf, |
| 121 | + allow_nan: bool = True, |
| 122 | + smallest_nonzero_magnitude: float, |
| 123 | + forced: Optional[float] = None, |
| 124 | + fake_forced: bool = False, |
| 125 | + ) -> float: |
| 126 | + if forced is not None: |
| 127 | + return forced |
| 128 | + |
| 129 | + n = self._draw_bits(64) |
| 130 | + sign = -1 if n >> 64 else 1 |
| 131 | + f = sign * lex_to_float(n & ((1 << 64) - 1)) |
| 132 | + clamper = make_float_clamper( |
| 133 | + min_value, |
| 134 | + max_value, |
| 135 | + smallest_nonzero_magnitude=smallest_nonzero_magnitude, |
| 136 | + allow_nan=allow_nan, |
| 137 | + ) |
| 138 | + return clamper(f) |
| 139 | + |
| 140 | + def _draw_collection(self, min_size, max_size, *, alphabet_size): |
| 141 | + average_size = min( |
| 142 | + max(min_size * 2, min_size + 5), |
| 143 | + 0.5 * (min_size + max_size), |
| 144 | + ) |
| 145 | + elements = many( |
| 146 | + self._cd, |
| 147 | + min_size=min_size, |
| 148 | + max_size=max_size, |
| 149 | + average_size=average_size, |
| 150 | + observe=False, |
| 151 | + ) |
| 152 | + values = [] |
| 153 | + while elements.more(): |
| 154 | + values.append(self.draw_integer(0, alphabet_size - 1)) |
| 155 | + return values |
| 156 | + |
| 157 | + def draw_string( |
| 158 | + self, |
| 159 | + intervals: IntervalSet, |
| 160 | + *, |
| 161 | + min_size: int = 0, |
| 162 | + max_size: int = COLLECTION_DEFAULT_MAX_SIZE, |
| 163 | + forced: Optional[str] = None, |
| 164 | + fake_forced: bool = False, |
| 165 | + ) -> str: |
| 166 | + if forced is not None: |
| 167 | + return forced |
| 168 | + values = self._draw_collection(min_size, max_size, alphabet_size=len(intervals)) |
| 169 | + return "".join(chr(intervals[v]) for v in values) |
| 170 | + |
| 171 | + def draw_bytes( |
| 172 | + self, |
| 173 | + min_size: int = 0, |
| 174 | + max_size: int = COLLECTION_DEFAULT_MAX_SIZE, |
| 175 | + *, |
| 176 | + forced: Optional[bytes] = None, |
| 177 | + fake_forced: bool = False, |
| 178 | + ) -> bytes: |
| 179 | + if forced is not None: |
| 180 | + return forced |
| 181 | + values = self._draw_collection(min_size, max_size, alphabet_size=2**8) |
| 182 | + return bytes(values) |
0 commit comments