-
Notifications
You must be signed in to change notification settings - Fork 591
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
Add small integer representation #4204
Draft
DRMacIver
wants to merge
1
commit into
master
Choose a base branch
from
DRMacIver/smol-integers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
RELEASE_TYPE: patch | ||
|
||
This release improves the internal representation of integers. This should have relatively | ||
little user visible difference, but will improve performance of both generation and shrinking | ||
in some cases, and also will improve shrink quality in a few others. In particular code like | ||
``st.one_of(st.integers(), st.text())`` should now reliably prefer ``0`` over ``""``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 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 random import Random | ||
|
||
from hypothesis import assume, example, given, strategies as st | ||
from hypothesis.errors import StopTest | ||
from hypothesis.internal.conjecture.data import ConjectureData, Status | ||
from hypothesis.internal.conjecture.engine import ConjectureRunner | ||
|
||
|
||
@st.composite | ||
def integer_buffer(draw): | ||
for _ in range(100): | ||
buf = draw(st.binary(min_size=8)) | ||
try: | ||
data = ConjectureData.for_buffer(buf) | ||
data.draw_integer() | ||
return bytes(data.buffer) | ||
except StopTest: | ||
continue | ||
assume(False) | ||
|
||
|
||
@example( | ||
n=-46, | ||
buffer=b"f\x00\x01\x01\x01", | ||
) | ||
@given(st.integers(), integer_buffer()) | ||
def test_will_always_shrink_an_integer_to_a_boundary(n, buffer): | ||
if n > 0: | ||
|
||
def test_function(data): | ||
if data.draw_integer() >= n: | ||
data.mark_interesting() | ||
|
||
elif n < 0: | ||
|
||
def test_function(data): | ||
if data.draw_integer() <= n: | ||
data.mark_interesting() | ||
|
||
else: | ||
|
||
def test_function(data): | ||
data.draw_integer() | ||
data.mark_interesting() | ||
|
||
runner = ConjectureRunner(test_function, random=Random(0)) | ||
assume(runner.cached_test_function(buffer).status == Status.INTERESTING) | ||
|
||
runner.shrink_interesting_examples() | ||
|
||
(shrunk,) = runner.interesting_examples.values() | ||
|
||
result = ConjectureData.for_buffer(shrunk.buffer).draw_integer() | ||
assert result == n |
27 changes: 27 additions & 0 deletions
27
hypothesis-python/tests/nocover/test_minimal_representations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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/. | ||
|
||
import hypothesis.strategies as st | ||
from hypothesis.internal.conjecture.data import ConjectureData | ||
from hypothesis.internal.conjecture.engine import BUFFER_SIZE | ||
from hypothesis.strategies import SearchStrategy | ||
|
||
|
||
def minimal_buffer_for(strategy: SearchStrategy) -> bytes: | ||
data = ConjectureData.for_buffer(bytes(BUFFER_SIZE)) | ||
# TODO: Not all strategies will actually produce a valid result | ||
# for all zero bytes. When we have one we want to test this | ||
# will require updating to use the shrinker. | ||
data.draw(strategy) | ||
return bytes(data.buffer) | ||
|
||
|
||
def test_integers_have_a_one_byte_representation(): | ||
assert len(minimal_buffer_for(st.integers())) == 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shrink_target.choices[0]
is a nice concise alternative here! I envision.buffer[i]
->.choices[i]
being the default migration path for bytestring tests, though of course with tweaked indices.(or better yet
shrinker.choices
, using the implicit forwarding to.shrink_target
).