Skip to content

Commit

Permalink
migrate reorder_examples to the ir
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Mar 18, 2024
1 parent 2e688b1 commit bd12c7d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
34 changes: 34 additions & 0 deletions hypothesis-python/src/hypothesis/internal/conjecture/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ def end(self) -> int:
"""
return self.owner.ends[self.index]

@property
def ir_start(self) -> int:
return self.owner.ir_starts[self.index]

@property
def ir_end(self) -> int:
return self.owner.ir_ends[self.index]

@property
def depth(self):
"""Depth of this example in the example tree. The top-level example has a
Expand Down Expand Up @@ -529,6 +537,32 @@ def starts(self) -> IntList:
def ends(self) -> IntList:
return self.starts_and_ends[1]

class _ir_starts_and_ends(ExampleProperty):
def begin(self):
self.starts = IntList.of_length(len(self.examples))
self.ends = IntList.of_length(len(self.examples))

def start_example(self, i: int, label_index: int) -> None:
self.starts[i] = self.ir_node_count

def stop_example(self, i: int, *, discarded: bool) -> None:
self.ends[i] = self.ir_node_count

def finish(self) -> Tuple[IntList, IntList]:
return (self.starts, self.ends)

ir_starts_and_ends: "Tuple[IntList, IntList]" = calculated_example_property(
_ir_starts_and_ends
)

@property
def ir_starts(self) -> IntList:
return self.ir_starts_and_ends[0]

@property
def ir_ends(self) -> IntList:
return self.ir_starts_and_ends[1]

class _discarded(ExampleProperty):
def begin(self) -> None:
self.result: "Set[int]" = set() # type: ignore # IntList in parent class
Expand Down
23 changes: 11 additions & 12 deletions hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

ARRAY_CODES = ["B", "H", "I", "L", "Q", "O"]

T = TypeVar("T")


def array_or_list(
code: str, contents: Iterable[int]
Expand All @@ -45,25 +47,25 @@ def array_or_list(


def replace_all(
buffer: Sequence[int],
replacements: Iterable[Tuple[int, int, Sequence[int]]],
) -> bytes:
"""Substitute multiple replacement values into a buffer.
ls: Sequence[T],
replacements: Iterable[Tuple[int, int, Sequence[T]]],
) -> List[T]:
"""Substitute multiple replacement values into a list.
Replacements is a list of (start, end, value) triples.
"""

result = bytearray()
result: List[T] = []
prev = 0
offset = 0
for u, v, r in replacements:
result.extend(buffer[prev:u])
result.extend(ls[prev:u])
result.extend(r)
prev = v
offset += len(r) - (v - u)
result.extend(buffer[prev:])
assert len(result) == len(buffer) + offset
return bytes(result)
result.extend(ls[prev:])
assert len(result) == len(ls) + offset
return result


NEXT_ARRAY_CODE = dict(zip(ARRAY_CODES, ARRAY_CODES[1:]))
Expand Down Expand Up @@ -190,9 +192,6 @@ def uniform(random: Random, n: int) -> bytes:
return random.getrandbits(n * 8).to_bytes(n, "big")


T = TypeVar("T")


class LazySequenceCopy:
"""A "copy" of a sequence that works by inserting a mask in front
of the underlying sequence, so that you can mutate it without changing
Expand Down
27 changes: 19 additions & 8 deletions hypothesis-python/src/hypothesis/internal/conjecture/shrinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,20 +1412,31 @@ def test_not_equal(x, y):
ex = chooser.choose(self.examples)
label = chooser.choose(ex.children).label

group = [c for c in ex.children if c.label == label]
if len(group) <= 1:
examples = [c for c in ex.children if c.label == label]
if len(examples) <= 1:
return

st = self.shrink_target
pieces = [st.buffer[ex.start : ex.end] for ex in group]
endpoints = [(ex.start, ex.end) for ex in group]
endpoints = [(ex.ir_start, ex.ir_end) for ex in examples]

Ordering.shrink(
pieces,
lambda ls: self.consider_new_buffer(
replace_all(st.buffer, [(u, v, r) for (u, v), r in zip(endpoints, ls)])
range(len(examples)),
lambda indices: self.consider_new_tree(
replace_all(
st.examples.ir_nodes,
[
(
u,
v,
st.examples.ir_nodes[
examples[i].ir_start : examples[i].ir_end
],
)
for (u, v), i in zip(endpoints, indices)
],
)
),
random=self.random,
key=lambda i: st.buffer[examples[i].start : examples[i].end],
)

def run_block_program(self, i, description, original, repeats=1):
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/tests/conjecture/test_junkdrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_assignment():


def test_replacement():
result = replace_all(bytes([1, 1, 1, 1]), [(1, 3, bytes([3, 4]))])
assert result == bytes([1, 3, 4, 1])
result = replace_all([1, 1, 1, 1], [(1, 3, [3, 4])])
assert result == [1, 3, 4, 1]


def test_int_list_cannot_contain_negative():
Expand Down

0 comments on commit bd12c7d

Please sign in to comment.