-
Notifications
You must be signed in to change notification settings - Fork 575
/
lexical.py
53 lines (41 loc) · 1.57 KB
/
lexical.py
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
# 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 hypothesis.internal.compat import int_from_bytes, int_to_bytes
from hypothesis.internal.conjecture.shrinking.common import Shrinker
from hypothesis.internal.conjecture.shrinking.integer import Integer
from hypothesis.internal.conjecture.shrinking.ordering import Ordering
"""
This module implements a lexicographic minimizer for blocks of bytes.
"""
class Lexical(Shrinker):
def make_immutable(self, value):
return bytes(value)
@property
def size(self):
return len(self.current)
def check_invariants(self, value):
assert len(value) == self.size
def left_is_better(self, left, right):
return left < right
def incorporate_int(self, i):
return self.incorporate(int_to_bytes(i, self.size))
@property
def current_int(self):
return int_from_bytes(self.current)
def minimize_as_integer(self):
Integer.shrink(
self.current_int,
lambda c: c == self.current_int or self.incorporate_int(c),
)
def partial_sort(self):
Ordering.shrink(self.current, self.consider)
def run_step(self):
self.minimize_as_integer()
self.partial_sort()