-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
165 additions
and
2 deletions.
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,63 @@ | ||
# 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.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 |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
# 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.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 |