コード例 #1
0
ファイル: numbers.py プロジェクト: The-Compiler/hypothesis
    def do_draw(self, data):
        size = 16
        sign_mask = 2 ** (size * 8 - 1)

        def distribution(random, n):
            assert n == size
            if random.randint(0, 2) > 0:
                k = 1
            elif random.randint(0, 2) > 0:
                k = 2
            else:
                k = random.randint(1, size)
            r = random.getrandbits(k * 8)
            if random.randint(0, 1):
                r |= sign_mask
            else:
                r &= ~sign_mask
            return int_to_bytes(r, n)

        byt = data.draw_bytes(size, distribution=distribution)
        r = int_from_bytes(byt)
        negative = r & sign_mask
        r &= ~sign_mask
        if negative:
            r = -r
        return int(r)
コード例 #2
0
ファイル: numbers.py プロジェクト: adamtheturtle/hypothesis
    def do_draw(self, data):
        size = 16
        sign_mask = 2 ** (size * 8 - 1)

        def distribution(random, n):
            assert n == size
            k = min(
                random.randint(0, n * 8 - 1),
                random.randint(0, n * 8 - 1),
            )
            if k > 0:
                r = random.getrandbits(k)
            else:
                r = 0
            if random.randint(0, 1):
                r |= sign_mask
            else:
                r &= (~sign_mask)
            return int_to_bytes(r, n)
        byt = data.draw_bytes(size, distribution=distribution)
        r = int_from_bytes(byt)
        negative = r & sign_mask
        r &= (~sign_mask)
        if negative:
            r = -r
        return int(r)
コード例 #3
0
def problem(draw):
    b = hbytes(draw(st.binary(min_size=1, max_size=8)))
    m = int_from_bytes(b) * 256
    assume(m > 0)
    marker = draw(st.binary(max_size=8))
    bound = draw(st.integers(0, m - 1))
    return (b, marker, bound)
コード例 #4
0
def problem(draw):
    b = hbytes(draw(st.binary(min_size=1, max_size=8)))
    m = int_from_bytes(b) * 256
    assume(m > 0)
    marker = draw(st.binary(max_size=8))
    bound = draw(st.integers(0, m - 1))
    return (b, marker, bound)
コード例 #5
0
    def do_draw(self, data):
        size = 16
        sign_mask = 2**(size * 8 - 1)

        def distribution(random, n):
            assert n == size
            k = min(
                random.randint(0, n * 8 - 1),
                random.randint(0, n * 8 - 1),
            )
            if k > 0:
                r = random.getrandbits(k)
            else:
                r = 0
            if random.randint(0, 1):
                r |= sign_mask
            else:
                r &= (~sign_mask)
            return int_to_bytes(r, n)

        byt = data.draw_bytes(size, distribution=distribution)
        r = int_from_bytes(byt)
        negative = r & sign_mask
        r &= (~sign_mask)
        if negative:
            r = -r
        return int(r)
コード例 #6
0
 def write(self, string):
     """Write ``string`` to the output buffer."""
     self.__assert_not_frozen("write")
     string = hbytes(string)
     if not string:
         return
     self.draw_bits(len(string) * 8, forced=int_from_bytes(string))
     return self.buffer[-len(string) :]
コード例 #7
0
ファイル: data.py プロジェクト: MarkAgimba/Mtribune
 def write(self, string):
     """Write ``string`` to the output buffer."""
     self.__assert_not_frozen("write")
     string = hbytes(string)
     if not string:
         return
     self.draw_bits(len(string) * 8, forced=int_from_bytes(string))
     return self.buffer[-len(string):]
コード例 #8
0
ファイル: data.py プロジェクト: doismellburning/hypothesis
 def draw_bits(self, n):
     self.__assert_not_frozen('draw_bits')
     if n == 0:
         result = 0
     elif n % 8 == 0:
         return int_from_bytes(self.draw_bytes(n // 8))
     else:
         n_bytes = (n // 8) + 1
         self.__check_capacity(n_bytes)
         buf = bytearray(self._draw_bytes(self, n_bytes))
         assert len(buf) == n_bytes
         mask = (1 << (n % 8)) - 1
         buf[0] &= mask
         self.capped_indices[self.index] = mask
         buf = hbytes(buf)
         self.__write(buf)
         result = int_from_bytes(buf)
     assert bit_length(result) <= n
     return result
コード例 #9
0
ファイル: data.py プロジェクト: springcoil/hypothesis-python
 def draw_bits(self, n):
     self.__assert_not_frozen('draw_bits')
     if n == 0:
         result = 0
     elif n % 8 == 0:
         return int_from_bytes(self.draw_bytes(n // 8))
     else:
         n_bytes = (n // 8) + 1
         self.__check_capacity(n_bytes)
         buf = bytearray(self._draw_bytes(self, n_bytes))
         assert len(buf) == n_bytes
         mask = (1 << (n % 8)) - 1
         buf[0] &= mask
         self.capped_indices[self.index] = mask
         buf = hbytes(buf)
         self.__write(buf)
         result = int_from_bytes(buf)
     assert bit_length(result) <= n
     return result
コード例 #10
0
ファイル: numbers.py プロジェクト: doismellburning/hypothesis
    def do_draw(self, data):
        size = 16
        sign_mask = 2 ** (size * 8 - 1)

        byt = data.draw_bytes(size)
        r = int_from_bytes(byt)
        negative = r & sign_mask
        r &= (~sign_mask)
        if negative:
            r = -r
        return int(r)
コード例 #11
0
    def do_draw(self, data):
        size = 16
        sign_mask = 2**(size * 8 - 1)

        byt = data.draw_bytes(size)
        r = int_from_bytes(byt)
        negative = r & sign_mask
        r &= (~sign_mask)
        if negative:
            r = -r
        return int(r)
コード例 #12
0
def integer_range(data, lower, upper, center=None, distribution=None):
    assert lower <= upper
    if lower == upper:
        return int(lower)

    if center is None:
        center = lower
    center = min(max(center, lower), upper)
    if distribution is None:
        if lower < center < upper:

            def distribution(random):
                if random.randint(0, 1):
                    return random.randint(center, upper)
                else:
                    return random.randint(lower, center)
        else:

            def distribution(random):
                return random.randint(lower, upper)

    gap = upper - lower
    bits = bit_length(gap)
    nbytes = bits // 8 + int(bits % 8 != 0)
    mask = saturate(gap)

    def byte_distribution(random, n):
        assert n == nbytes
        v = distribution(random)
        if v >= center:
            probe = v - center
        else:
            probe = upper - v
        return int_to_bytes(probe, n)

    probe = gap + 1

    while probe > gap:
        probe = int_from_bytes(data.draw_bytes(nbytes,
                                               byte_distribution)) & mask

    if center == upper:
        result = upper - probe
    elif center == lower:
        result = lower + probe
    else:
        if center + probe <= upper:
            result = center + probe
        else:
            result = upper - probe
    assert lower <= result <= upper
    return int(result)
コード例 #13
0
ファイル: utils.py プロジェクト: AWhetter/hypothesis-python
def integer_range(data, lower, upper, center=None, distribution=None):
    assert lower <= upper
    if lower == upper:
        return int(lower)

    if center is None:
        center = lower
    center = min(max(center, lower), upper)
    if distribution is None:
        if lower < center < upper:
            def distribution(random):
                if random.randint(0, 1):
                    return random.randint(center, upper)
                else:
                    return random.randint(lower, center)
        else:
            def distribution(random):
                return random.randint(lower, upper)

    gap = upper - lower
    bits = bit_length(gap)
    nbytes = bits // 8 + int(bits % 8 != 0)
    mask = saturate(gap)

    def byte_distribution(random, n):
        assert n == nbytes
        v = distribution(random)
        if v >= center:
            probe = v - center
        else:
            probe = upper - v
        return int_to_bytes(probe, n)

    probe = gap + 1

    while probe > gap:
        probe = int_from_bytes(
            data.draw_bytes(nbytes, byte_distribution)
        ) & mask

    if center == upper:
        result = upper - probe
    elif center == lower:
        result = lower + probe
    else:
        if center + probe <= upper:
            result = center + probe
        else:
            result = upper - probe
    assert lower <= result <= upper
    return int(result)
コード例 #14
0
def get_random_for_wrapped_test(test, wrapped_test):
    settings = wrapped_test._hypothesis_internal_use_settings
    wrapped_test._hypothesis_internal_use_generated_seed = None

    if wrapped_test._hypothesis_internal_use_seed is not None:
        return Random(wrapped_test._hypothesis_internal_use_seed)
    elif settings.derandomize:
        return Random(int_from_bytes(function_digest(test)))
    elif global_force_seed is not None:
        return Random(global_force_seed)
    else:
        seed = rnd_module.getrandbits(128)
        wrapped_test._hypothesis_internal_use_generated_seed = seed
        return Random(seed)
コード例 #15
0
ファイル: core.py プロジェクト: yssource/hypothesis
def get_random_for_wrapped_test(test, wrapped_test):
    settings = wrapped_test._hypothesis_internal_use_settings
    wrapped_test._hypothesis_internal_use_generated_seed = None

    if wrapped_test._hypothesis_internal_use_seed is not None:
        return Random(wrapped_test._hypothesis_internal_use_seed)
    elif settings.derandomize:
        return Random(int_from_bytes(function_digest(test)))
    elif global_force_seed is not None:
        return Random(global_force_seed)
    else:
        seed = rnd_module.getrandbits(128)
        wrapped_test._hypothesis_internal_use_generated_seed = seed
        return Random(seed)
コード例 #16
0
def test_regression_1():
    # This is a really hard to reproduce bug that previously triggered a very
    # specific exception inside one of the shrink passes. It's unclear how
    # useful this regression test really is, but nothing else caught the
    # problem.
    @run_to_buffer
    def x(data):
        data.write(hbytes(b'\x01\x02'))
        data.write(hbytes(b'\x01\x00'))
        v = data.draw_bits(41)
        if v >= 512 or v == 254:
            data.mark_interesting()
    assert list(x)[:-2] == [1, 2, 1, 0, 0, 0, 0, 0]

    assert int_from_bytes(x[-2:]) in (254, 512)
コード例 #17
0
ファイル: engine.py プロジェクト: yssource/hypothesis
        def go(ex):
            if ex.length == 0:
                return
            if len(ex.children) == 0:
                stack[-1].append(int_from_bytes(data.buffer[ex.start:ex.end]))
            else:
                node = []
                stack.append(node)

                for v in ex.children:
                    go(v)
                stack.pop()
                if len(node) == 1:
                    stack[-1].extend(node)
                else:
                    stack[-1].append(node)
コード例 #18
0
def test_regression_1():
    # This is a really hard to reproduce bug that previously triggered a very
    # specific exception inside one of the shrink passes. It's unclear how
    # useful this regression test really is, but nothing else caught the
    # problem.
    @run_to_buffer
    def x(data):
        data.write(b"\x01\x02")
        data.write(b"\x01\x00")
        v = data.draw_bits(41)
        if v >= 512 or v == 254:
            data.mark_interesting()

    assert list(x)[:-2] == [1, 2, 1, 0, 0, 0, 0, 0]

    assert int_from_bytes(x[-2:]) in (254, 512)
コード例 #19
0
        def go(ex):
            if ex.length == 0:
                return
            if len(ex.children) == 0:
                stack[-1].append(int_from_bytes(data.buffer[ex.start : ex.end]))
            else:
                node = []
                stack.append(node)

                for v in ex.children:
                    go(v)
                stack.pop()
                if len(node) == 1:
                    stack[-1].extend(node)
                else:
                    stack[-1].append(node)
コード例 #20
0
ファイル: data.py プロジェクト: benanhalt/hypothesis
    def draw_bits(self, n, *, forced=None):
        """Return an ``n``-bit integer from the underlying source of
        bytes. If ``forced`` is set to an integer will instead
        ignore the underlying source and simulate a draw as if it had
        returned that integer."""
        self.__assert_not_frozen("draw_bits")
        if n == 0:
            return 0
        assert n > 0
        n_bytes = bits_to_bytes(n)
        self.__check_capacity(n_bytes)

        if forced is not None:
            buf = int_to_bytes(forced, n_bytes)
        elif self.__bytes_drawn < len(self.__prefix):
            index = self.__bytes_drawn
            buf = self.__prefix[index:index + n_bytes]
            if len(buf) < n_bytes:
                buf += uniform(self.__random, n_bytes - len(buf))
        else:
            buf = uniform(self.__random, n_bytes)
        buf = bytearray(buf)
        self.__bytes_drawn += n_bytes

        assert len(buf) == n_bytes

        # If we have a number of bits that is not a multiple of 8
        # we have to mask off the high bits.
        buf[0] &= BYTE_MASKS[n % 8]
        buf = bytes(buf)
        result = int_from_bytes(buf)

        self.observer.draw_bits(n, forced is not None, result)
        self.__example_record.draw_bits(n, forced)

        initial = self.index

        self.buffer.extend(buf)
        self.index = len(self.buffer)

        if forced is not None:
            self.forced_indices.update(range(initial, self.index))

        self.blocks.add_endpoint(self.index)

        assert bit_length(result) <= n
        return result
コード例 #21
0
ファイル: utils.py プロジェクト: E-Tahta/python-hypothesis
def geometric(data, p):
    denom = math.log1p(-p)
    n_bytes = 8

    def distribution(random, n):
        assert n == n_bytes
        for _ in range(100):
            try:
                return int_to_bytes(int(
                    math.log1p(-random.random()) / denom), n)
            # This is basically impossible to hit but is required for
            # correctness
            except OverflowError:  # pragma: no cover
                pass
        # We got a one in a million chance 100 times in a row. Something is up.
        assert False  # pragma: no cover
    return int_from_bytes(data.draw_bytes(n_bytes, distribution))
コード例 #22
0
ファイル: utils.py プロジェクト: AWhetter/hypothesis-python
def geometric(data, p):
    denom = math.log1p(-p)
    n_bytes = 8

    def distribution(random, n):
        assert n == n_bytes
        for _ in range(100):
            try:
                return int_to_bytes(int(
                    math.log1p(-random.random()) / denom), n)
            # This is basically impossible to hit but is required for
            # correctness
            except OverflowError:  # pragma: no cover
                pass
        # We got a one in a million chance 100 times in a row. Something is up.
        assert False  # pragma: no cover
    return int_from_bytes(data.draw_bytes(n_bytes, distribution))
コード例 #23
0
ファイル: lexical.py プロジェクト: Wilfred/hypothesis-python
    def float_hack(self):
        """Our encoding of floating point numbers does the right thing when you
        lexically shrink it, but there are some highly non-obvious lexical
        shrinks corresponding to natural floating point operations.

        We can't actually tell when the floating point encoding is being used
        (that would break the assumptions that Hypothesis doesn't inspect
        the generated values), but we can cheat: We just guess when it might be
        being used and perform shrinks that are valid regardless of our guess
        is correct.

        So that's what this method does. It's a cheat to give us good shrinking
        of floating at low cost in runtime and only moderate cost in elegance.
        """
        # If the block is of the wrong size then we're certainly not using the
        # float encoding.
        if self.size != 8:
            return

        # If the high bit is zero then we're in the integer representation of
        # floats so we don't need these hacks because it will shrink normally.
        if self.current[0] >> 7 == 0:
            return

        i = self.current_int
        f = lex_to_float(i)

        # This floating point number can be represented in our simple format.
        # So we try converting it to that (which will give the same float, but
        # a different encoding of it). If that doesn't work then the float
        # value of this doesn't unambiguously give the desired predicate, so
        # this approach isn't useful. If it *does* work, then we're now in a
        # situation where we don't need it, so either way we return here.
        if is_simple(f):
            self.incorporate_float(f)
            return

        self.delegate(
            Float,
            convert_to=lambda b: lex_to_float(int_from_bytes(b)),
            convert_from=lambda f: int_to_bytes(float_to_lex(f), self.size),
        )
コード例 #24
0
    def float_hack(self):
        """Our encoding of floating point numbers does the right thing when you
        lexically shrink it, but there are some highly non-obvious lexical
        shrinks corresponding to natural floating point operations.

        We can't actually tell when the floating point encoding is being used
        (that would break the assumptions that Hypothesis doesn't inspect
        the generated values), but we can cheat: We just guess when it might be
        being used and perform shrinks that are valid regardless of our guess
        is correct.

        So that's what this method does. It's a cheat to give us good shrinking
        of floating at low cost in runtime and only moderate cost in elegance.
        """
        # If the block is of the wrong size then we're certainly not using the
        # float encoding.
        if self.size != 8:
            return

        # If the high bit is zero then we're in the integer representation of
        # floats so we don't need these hacks because it will shrink normally.
        if self.current[0] >> 7 == 0:
            return

        i = self.current_int
        f = lex_to_float(i)

        # This floating point number can be represented in our simple format.
        # So we try converting it to that (which will give the same float, but
        # a different encoding of it). If that doesn't work then the float
        # value of this doesn't unambiguously give the desired predicate, so
        # this approach isn't useful. If it *does* work, then we're now in a
        # situation where we don't need it, so either way we return here.
        if is_simple(f):
            self.incorporate_float(f)
            return

        self.delegate(
            Float,
            convert_to=lambda b: lex_to_float(int_from_bytes(b)),
            convert_from=lambda f: int_to_bytes(float_to_lex(f), self.size),
        )
コード例 #25
0
ファイル: data.py プロジェクト: jks7743/hypothesis
    def draw_bits(self, n, forced=None):
        """Return an ``n``-bit integer from the underlying source of
        bytes. If ``forced`` is set to an integer will instead
        ignore the underlying source and simulate a draw as if it had
        returned that integer."""
        self.__assert_not_frozen("draw_bits")
        if n == 0:
            return 0
        assert n > 0
        n_bytes = bits_to_bytes(n)
        self.__check_capacity(n_bytes)

        if forced is not None:
            buf = bytearray(int_to_bytes(forced, n_bytes))
        else:
            buf = bytearray(self._draw_bytes(self, n_bytes))
        assert len(buf) == n_bytes

        # If we have a number of bits that is not a multiple of 8
        # we have to mask off the high bits.
        buf[0] &= BYTE_MASKS[n % 8]
        buf = hbytes(buf)
        result = int_from_bytes(buf)

        self.observer.draw_bits(n, forced is not None, result)

        self.start_example(DRAW_BYTES_LABEL)
        initial = self.index

        self.buffer.extend(buf)
        self.index = len(self.buffer)

        if forced is not None:
            self.forced_indices.update(hrange(initial, self.index))

        self.blocks.add_endpoint(self.index)

        self.stop_example()

        assert bit_length(result) <= n
        return result
コード例 #26
0
ファイル: utils.py プロジェクト: Teraisa/flask-advanced
def integer_range(data, lower, upper, center=None):
    assert lower <= upper
    if lower == upper:
        return int(lower)

    if center is None:
        center = lower
    center = min(max(center, lower), upper)

    bits = bit_length(max(upper - center, center - lower))

    nbytes = bits // 8 + int(bits % 8 != 0)

    if center == upper:
        above = False
    elif center == lower:
        above = True
    else:
        above = boolean(data)

    if above:
        gap = upper - center
    else:
        gap = center - lower

    assert gap > 0

    mask = saturate(gap)
    probe = gap + 1

    while probe > gap:
        probe = int_from_bytes(data.draw_bytes(nbytes)) & mask

    if above:
        result = center + probe
    else:
        result = center - probe

    assert lower <= result <= upper
    return int(result)
コード例 #27
0
    def draw_bits(self, n, forced=None):
        """Return an ``n``-bit integer from the underlying source of
        bytes. If ``forced`` is set to an integer will instead
        ignore the underlying source and simulate a draw as if it had
        returned that integer."""
        self.__assert_not_frozen("draw_bits")
        if n == 0:
            return 0
        assert n > 0
        n_bytes = bits_to_bytes(n)
        self.__check_capacity(n_bytes)

        if forced is not None:
            buf = bytearray(int_to_bytes(forced, n_bytes))
        else:
            buf = bytearray(self._draw_bytes(self, n_bytes))
        assert len(buf) == n_bytes

        # If we have a number of bits that is not a multiple of 8
        # we have to mask off the high bits.
        buf[0] &= BYTE_MASKS[n % 8]
        buf = hbytes(buf)
        result = int_from_bytes(buf)

        self.observer.draw_bits(n, forced is not None, result)
        self.__example_record.draw_bits(n, forced)

        initial = self.index

        self.buffer.extend(buf)
        self.index = len(self.buffer)

        if forced is not None:
            self.forced_indices.update(hrange(initial, self.index))

        self.blocks.add_endpoint(self.index)

        assert bit_length(result) <= n
        return result
コード例 #28
0
 def f(data):
     i = int_from_bytes(data.draw_bytes(2))
     data.draw_bytes(i)
     data.mark_interesting()
コード例 #29
0
def calc_label_from_name(name: str) -> int:
    hashed = hashlib.sha384(name.encode()).digest()
    return int_from_bytes(hashed[:8])
コード例 #30
0
ファイル: lexical.py プロジェクト: Wilfred/hypothesis-python
 def current_int(self):
     return int_from_bytes(self.current)
コード例 #31
0
def test_to_int_in_big_endian_order(x, y):
    x, y = sorted((x, y))
    assert 0 <= int_from_bytes(x) <= int_from_bytes(y)
コード例 #32
0
def test_convert_back(bs):
    bs = bytearray(bs)
    assert int_to_bytes(int_from_bytes(bs), len(bs)) == bs
コード例 #33
0
ファイル: utils.py プロジェクト: Wilfred/hypothesis-python
def getrandbits(data, n):
    n_bytes = n // 8
    if n % 8 != 0:
        n_bytes += 1
    return int_from_bytes(data.draw_bytes(n_bytes)) & ((1 << n) - 1)
コード例 #34
0
    def float_hack(self):
        """Our encoding of floating point numbers does the right thing when you
        lexically shrink it, but there are some highly non-obvious lexical
        shrinks corresponding to natural floating point operations.

        We can't actually tell when the floating point encoding is being used
        (that would break the assumptions that Hypothesis doesn't inspect
        the generated values), but we can cheat: We just guess when it might be
        being used and perform shrinks that are valid regardless of our guess
        is correct.

        So that's what this method does. It's a cheat to give us good shrinking
        of floating at low cost in runtime and only moderate cost in elegance.

        """

        # If the block is of the wrong size then we're certainly not using the
        # float encoding.
        if self.size != 8:
            return

        # If the high bit is zero then we're in the integer representation of
        # floats so we don't need these hacks because it will shrink normally.
        if self.current[0] >> 7 == 0:
            return

        i = int_from_bytes(self.current)
        f = lex_to_float(i)

        # This floating point number can be represented in our simple format.
        # So we try converting it to that (which will give the same float, but
        # a different encoding of it). If that doesn't work then the float
        # value of this doesn't unambiguously give the desired predicate, so
        # this approach isn't useful. If it *does* work, then we're now in a
        # situation where we don't need it, so either way we return here.
        if is_simple(f):
            self.incorporate_float(f)
            return

        # We check for a bunch of standard "large" floats. If we're currently
        # worse than them and the shrink downwards doesn't help, abort early
        # because there's not much useful we can do here.
        for g in [
            float('nan'), float('inf'), sys.float_info.max,
        ]:
            j = float_to_lex(g)
            if j < i:
                if self.incorporate_int(j):
                    f = g
                    i = j

        if math.isinf(f) or math.isnan(f):
            return

        # Finally we get to the important bit: Each of these is a small change
        # to the floating point number that corresponds to a large change in
        # the lexical representation. Trying these ensures that our floating
        # point shrink can always move past these obstacles. In particular it
        # ensures we can always move to integer boundaries and shrink past a
        # change that would require shifting the exponent while not changing
        # the float value much.
        for g in [
            floor(f), ceil(f),
        ]:
            if self.incorporate_float(g):
                return

        if f > 2:
            self.incorporate_float(f - 1)
コード例 #35
0
ファイル: strategies.py プロジェクト: EYcab/hypothesis-python
def calc_label(cls):
    name = str_to_bytes(qualname(cls))
    hashed = hashlib.md5(name).digest()
    return int_from_bytes(hashed[:8])
コード例 #36
0
ファイル: utils.py プロジェクト: Wilfred/hypothesis-python
def calc_label_from_name(name):
    hashed = hashlib.md5(str_to_bytes(name)).digest()
    return int_from_bytes(hashed[:8])
コード例 #37
0
def n_byte_unsigned(data, n):
    return int_from_bytes(data.draw_bytes(n))
コード例 #38
0
ファイル: utils.py プロジェクト: AWhetter/hypothesis-python
def n_byte_unsigned(data, n):
    return int_from_bytes(data.draw_bytes(n))
コード例 #39
0
    def attempt_to_improve(self, example_index, upwards):
        """Part of our hill climbing implementation. Attempts to improve a
        given score by regenerating an example."""

        data = self.current_data
        self.current_score

        ex = data.examples[example_index]
        assert ex.length > 0
        prefix = data.buffer[:ex.start]
        suffix = data.buffer[ex.end:]

        existing = data.buffer[ex.start:ex.end]

        existing_as_int = int_from_bytes(existing)
        max_int_value = (256**len(existing)) - 1

        if existing_as_int == max_int_value and upwards:
            return False

        if existing_as_int == 0 and not upwards:
            return False

        # We make a mix of small and large jumps. Neither are guaranteeed to
        # work, but each will work in circumstances the other might not. Small
        # jumps are especially important for the last steps towards a local
        # maximum - often large jumps will break some important property of the
        # test case while small, more careful, jumps will not. On the flip side
        # we sometimes end up in circumstances where small jumps don't work
        # but a random draw will produce a good result with reasonable
        # probability, and we don't want to be too timid in our optimisation as
        # it will take too long to complete.
        if self.random.randint(0, 1):
            if upwards:
                replacement_as_int = self.random.randint(
                    existing_as_int + 1, max_int_value)
            else:
                replacement_as_int = self.random.randint(
                    0, existing_as_int - 1)
        elif upwards:
            replacement_as_int = existing_as_int + 1
        else:
            replacement_as_int = existing_as_int - 1

        replacement = int_to_bytes(replacement_as_int, len(existing))

        attempt = self.engine.cached_test_function(
            prefix + replacement + suffix,
            extend=BUFFER_SIZE,
        )

        if self.consider_new_test_data(attempt):
            return True

        if attempt.status < Status.VALID:
            return False

        ex_attempt = attempt.examples[example_index]

        replacement = attempt.buffer[ex_attempt.start:ex_attempt.end]

        return self.consider_new_buffer(prefix + replacement + suffix)
コード例 #40
0
 def parse_buf(b):
     return flt.lex_to_float(int_from_bytes(b))
コード例 #41
0
 def current_int(self):
     return int_from_bytes(self.current)
コード例 #42
0
 def do_draw(self, data):
     b = int_from_bytes(data.draw_bytes(2))
     assume(b == 3)
     print('ohai')
コード例 #43
0
ファイル: utils.py プロジェクト: joydu902/UofT-Coursework
def getrandbits(data, n):
    n_bytes = n // 8
    if n % 8 != 0:
        n_bytes += 1
    return int_from_bytes(data.draw_bytes(n_bytes)) & ((1 << n) - 1)
コード例 #44
0
def calc_label_from_name(name):
    hashed = hashlib.sha384(str_to_bytes(name)).digest()
    return int_from_bytes(hashed[:8])
コード例 #45
0
    def hill_climb(self):
        """The main hill climbing loop where we actually do the work: Take
        data, and attempt to improve its score for target. select_example takes
        a data object and returns an index to an example where we should focus
        our efforts."""

        blocks_examined = set()

        prev = None
        i = len(self.current_data.blocks) - 1
        while i >= 0 and self.improvements <= self.max_improvements:
            if prev is not self.current_data:
                i = len(self.current_data.blocks) - 1
                prev = self.current_data

            if i in blocks_examined:
                i -= 1
                continue

            blocks_examined.add(i)
            data = self.current_data
            block = data.blocks[i]
            prefix = data.buffer[:block.start]

            existing = data.buffer[block.start:block.end]
            existing_as_int = int_from_bytes(existing)
            max_int_value = (256**len(existing)) - 1

            if existing_as_int == max_int_value:
                continue

            def attempt_replace(v):
                """Try replacing the current block in the current best test case
                 with an integer of value i. Note that we use the *current*
                best and not the one we started with. This helps ensure that
                if we luck into a good draw when making random choices we get
                to keep the good bits."""
                if v < 0 or v > max_int_value:
                    return False
                v_as_bytes = int_to_bytes(v, len(existing))

                # We make a couple attempts at replacement. This only matters
                # if we end up growing the buffer - otherwise we exit the loop
                # early - but in the event that there *is* some randomized
                # component we want to give it a couple of tries to succeed.
                for _ in range(3):
                    attempt = self.engine.cached_test_function(
                        prefix + v_as_bytes +
                        self.current_data.buffer[block.end:] +
                        bytes(BUFFER_SIZE), )

                    if self.consider_new_test_data(attempt):
                        return True

                    if attempt.status < Status.INVALID or len(
                            attempt.buffer) == len(self.current_data.buffer):
                        return False

                    for i, ex in enumerate(self.current_data.examples):
                        if ex.start >= block.end:
                            break
                        if ex.end <= block.start:
                            continue
                        ex_attempt = attempt.examples[i]
                        if ex.length == ex_attempt.length:
                            continue
                        replacement = attempt.buffer[ex_attempt.
                                                     start:ex_attempt.end]
                        if self.consider_new_test_data(
                                self.engine.cached_test_function(
                                    prefix + replacement +
                                    self.current_data.buffer[ex.end:])):
                            return True
                return False

            # We unconditionally scan both upwards and downwards. The reason
            # for this is that we allow "lateral" moves that don't increase the
            # score but instead leave it constant. All else being equal we'd
            # like to leave the test case closer to shrunk, so afterwards we
            # try lowering the value towards zero even if we've just raised it.

            if not attempt_replace(max_int_value):
                find_integer(lambda k: attempt_replace(k + existing_as_int))

            existing = self.current_data.buffer[block.start:block.end]
            existing_as_int = int_from_bytes(existing)
            if not attempt_replace(0):
                find_integer(lambda k: attempt_replace(existing_as_int - k))
コード例 #46
0
 def do_draw(self, data):
     b = int_from_bytes(data.draw_bytes(2))
     assume(b == 3)
     print('ohai')
コード例 #47
0
def test_to_int_in_big_endian_order(x, y):
    x, y = sorted((x, y))
    assert 0 <= int_from_bytes(x) <= int_from_bytes(y)
コード例 #48
0
def test_convert_back(bs):
    bs = bytearray(bs)
    assert int_to_bytes(int_from_bytes(bs), len(bs)) == bs
コード例 #49
0
 def parse_buf(b):
     return flt.lex_to_float(int_from_bytes(b))