Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain the last block's length without having to create it #1873

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This change makes a tiny improvement to the core engine's bookkeeping.
There is no user-visible change.
12 changes: 11 additions & 1 deletion hypothesis-python/src/hypothesis/internal/conjecture/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def transfer_ownership(self, new_owner):

def start(self, i):
"""Equivalent to self[i].start."""
i = self._check_index(i)

if i == 0:
return 0
else:
Expand All @@ -222,6 +224,10 @@ def all_bounds(self):
yield (prev, e)
prev = e

@property
def last_block_length(self):
return self.end(-1) - self.start(-1)

def __len__(self):
return len(self.endpoints)

Expand All @@ -231,12 +237,16 @@ def __known_block(self, i):
except (KeyError, IndexError):
return None

def __getitem__(self, i):
def _check_index(self, i):
n = len(self)
if i < -n or i >= n:
raise IndexError("Index %d out of range [-%d, %d)" % (i, n, n))
if i < 0:
i += n
return i

def __getitem__(self, i):
i = self._check_index(i)
assert i >= 0
result = self.__known_block(i)
if result is not None:
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def do_draw(self, data):
# Easy, right?
n = len(self.rules)
i = cu.integer_range(data, 0, n - 1)
block_length = data.blocks[-1].length
block_length = data.blocks.last_block_length
rule = self.rules[i]
if not self.is_valid(rule):
valid_rules = [j for j, r in enumerate(self.rules) if self.is_valid(r)]
Expand Down
32 changes: 32 additions & 0 deletions hypothesis-python/tests/cover/test_conjecture_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,35 @@ def conclude_test(self, status, reason):
x.freeze()

assert observer.conclusion == (Status.VALID, None)


def test_handles_start_indices_like_a_list():
n = 5
d = ConjectureData.for_buffer([1] * n)
for _ in hrange(n):
d.draw_bits(1)

for i in hrange(-2 * n, 2 * n + 1):
try:
start = d.blocks.start(i)
except IndexError:
# Directly retrieving the start position failed, so check that
# indexing also fails.
with pytest.raises(IndexError):
d.blocks[i]
continue

# Directly retrieving the start position succeeded, so check that
# indexing also succeeds, and gives the same position.
assert start == d.blocks[i].start


def test_last_block_length():
d = ConjectureData.for_buffer([0] * 15)

with pytest.raises(IndexError):
d.blocks.last_block_length

for n in hrange(1, 5 + 1):
d.draw_bits(n * 8)
assert d.blocks.last_block_length == n