Skip to content

Commit bd12c7d

Browse files
committed
migrate reorder_examples to the ir
1 parent 2e688b1 commit bd12c7d

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/data.py

+34
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ def end(self) -> int:
289289
"""
290290
return self.owner.ends[self.index]
291291

292+
@property
293+
def ir_start(self) -> int:
294+
return self.owner.ir_starts[self.index]
295+
296+
@property
297+
def ir_end(self) -> int:
298+
return self.owner.ir_ends[self.index]
299+
292300
@property
293301
def depth(self):
294302
"""Depth of this example in the example tree. The top-level example has a
@@ -529,6 +537,32 @@ def starts(self) -> IntList:
529537
def ends(self) -> IntList:
530538
return self.starts_and_ends[1]
531539

540+
class _ir_starts_and_ends(ExampleProperty):
541+
def begin(self):
542+
self.starts = IntList.of_length(len(self.examples))
543+
self.ends = IntList.of_length(len(self.examples))
544+
545+
def start_example(self, i: int, label_index: int) -> None:
546+
self.starts[i] = self.ir_node_count
547+
548+
def stop_example(self, i: int, *, discarded: bool) -> None:
549+
self.ends[i] = self.ir_node_count
550+
551+
def finish(self) -> Tuple[IntList, IntList]:
552+
return (self.starts, self.ends)
553+
554+
ir_starts_and_ends: "Tuple[IntList, IntList]" = calculated_example_property(
555+
_ir_starts_and_ends
556+
)
557+
558+
@property
559+
def ir_starts(self) -> IntList:
560+
return self.ir_starts_and_ends[0]
561+
562+
@property
563+
def ir_ends(self) -> IntList:
564+
return self.ir_starts_and_ends[1]
565+
532566
class _discarded(ExampleProperty):
533567
def begin(self) -> None:
534568
self.result: "Set[int]" = set() # type: ignore # IntList in parent class

hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

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

38+
T = TypeVar("T")
39+
3840

3941
def array_or_list(
4042
code: str, contents: Iterable[int]
@@ -45,25 +47,25 @@ def array_or_list(
4547

4648

4749
def replace_all(
48-
buffer: Sequence[int],
49-
replacements: Iterable[Tuple[int, int, Sequence[int]]],
50-
) -> bytes:
51-
"""Substitute multiple replacement values into a buffer.
50+
ls: Sequence[T],
51+
replacements: Iterable[Tuple[int, int, Sequence[T]]],
52+
) -> List[T]:
53+
"""Substitute multiple replacement values into a list.
5254
5355
Replacements is a list of (start, end, value) triples.
5456
"""
5557

56-
result = bytearray()
58+
result: List[T] = []
5759
prev = 0
5860
offset = 0
5961
for u, v, r in replacements:
60-
result.extend(buffer[prev:u])
62+
result.extend(ls[prev:u])
6163
result.extend(r)
6264
prev = v
6365
offset += len(r) - (v - u)
64-
result.extend(buffer[prev:])
65-
assert len(result) == len(buffer) + offset
66-
return bytes(result)
66+
result.extend(ls[prev:])
67+
assert len(result) == len(ls) + offset
68+
return result
6769

6870

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

192194

193-
T = TypeVar("T")
194-
195-
196195
class LazySequenceCopy:
197196
"""A "copy" of a sequence that works by inserting a mask in front
198197
of the underlying sequence, so that you can mutate it without changing

hypothesis-python/src/hypothesis/internal/conjecture/shrinker.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -1412,20 +1412,31 @@ def test_not_equal(x, y):
14121412
ex = chooser.choose(self.examples)
14131413
label = chooser.choose(ex.children).label
14141414

1415-
group = [c for c in ex.children if c.label == label]
1416-
if len(group) <= 1:
1415+
examples = [c for c in ex.children if c.label == label]
1416+
if len(examples) <= 1:
14171417
return
1418-
14191418
st = self.shrink_target
1420-
pieces = [st.buffer[ex.start : ex.end] for ex in group]
1421-
endpoints = [(ex.start, ex.end) for ex in group]
1419+
endpoints = [(ex.ir_start, ex.ir_end) for ex in examples]
14221420

14231421
Ordering.shrink(
1424-
pieces,
1425-
lambda ls: self.consider_new_buffer(
1426-
replace_all(st.buffer, [(u, v, r) for (u, v), r in zip(endpoints, ls)])
1422+
range(len(examples)),
1423+
lambda indices: self.consider_new_tree(
1424+
replace_all(
1425+
st.examples.ir_nodes,
1426+
[
1427+
(
1428+
u,
1429+
v,
1430+
st.examples.ir_nodes[
1431+
examples[i].ir_start : examples[i].ir_end
1432+
],
1433+
)
1434+
for (u, v), i in zip(endpoints, indices)
1435+
],
1436+
)
14271437
),
14281438
random=self.random,
1439+
key=lambda i: st.buffer[examples[i].start : examples[i].end],
14291440
)
14301441

14311442
def run_block_program(self, i, description, original, repeats=1):

hypothesis-python/tests/conjecture/test_junkdrawer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def test_assignment():
107107

108108

109109
def test_replacement():
110-
result = replace_all(bytes([1, 1, 1, 1]), [(1, 3, bytes([3, 4]))])
111-
assert result == bytes([1, 3, 4, 1])
110+
result = replace_all([1, 1, 1, 1], [(1, 3, [3, 4])])
111+
assert result == [1, 3, 4, 1]
112112

113113

114114
def test_int_list_cannot_contain_negative():

0 commit comments

Comments
 (0)