| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from hypothesis import HealthCheck, given, settings, strategies as st |
| 14 |
|
| 15 |
from tests.common.debug import assert_all_examples, find_any, minimal |
| 16 |
|
| 17 |
use_several_sizes = pytest.mark.parametrize("size", [1, 2, 5, 10, 100, 1000]) |
| 18 |
|
| 19 |
|
| 20 |
@use_several_sizes |
| 21 |
def test_stop_stays_within_bounds(size): |
| 22 |
assert_all_examples( |
| 23 |
st.slices(size), |
| 24 |
lambda x: x.stop is None or (x.stop >= -size and x.stop <= size), |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
@use_several_sizes |
| 29 |
def test_start_stay_within_bounds(size): |
| 30 |
assert_all_examples( |
| 31 |
st.slices(size).filter(lambda x: x.start is not None), |
| 32 |
lambda x: range(size)[x.start] or True, # no IndexError raised |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
@use_several_sizes |
| 37 |
def test_step_stays_within_bounds(size): |
| 38 |
# indices -> (start, stop, step) |
| 39 |
# Stop is exclusive so we use -1 as the floor. |
| 40 |
# This uses the indices that slice produces to make this test more readable |
| 41 |
# due to how splice processes None being a little complex |
| 42 |
assert_all_examples( |
| 43 |
st.slices(size), |
| 44 |
lambda x: ( |
| 45 |
x.indices(size)[0] + x.indices(size)[2] <= size |
| 46 |
and x.indices(size)[0] + x.indices(size)[2] >= -size |
| 47 |
) |
| 48 |
or x.start % size == x.stop % size, |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
@use_several_sizes |
| 53 |
def test_step_will_not_be_zero(size): |
| 54 |
assert_all_examples(st.slices(size), lambda x: x.step != 0) |
| 55 |
|
| 56 |
|
| 57 |
@use_several_sizes |
| 58 |
def test_slices_will_shrink(size): |
| 59 |
sliced = minimal(st.slices(size)) |
| 60 |
assert sliced.start == 0 or sliced.start is None |
| 61 |
assert sliced.stop == 0 or sliced.stop is None |
| 62 |
assert sliced.step is None |
| 63 |
|
| 64 |
|
| 65 |
@given(st.integers(1, 1000)) |
| 66 |
@settings(deadline=None, suppress_health_check=list(HealthCheck)) |
| 67 |
def test_step_will_be_negative(size): |
| 68 |
find_any(st.slices(size), lambda x: (x.step or 1) < 0) |
| 69 |
|
| 70 |
|
| 71 |
@given(st.integers(1, 1000)) |
| 72 |
@settings(deadline=None) |
| 73 |
def test_step_will_be_positive(size): |
| 74 |
find_any(st.slices(size), lambda x: (x.step or 1) > 0) |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.parametrize("size", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 78 |
def test_stop_will_equal_size(size): |
| 79 |
find_any(st.slices(size), lambda x: x.stop == size, settings(max_examples=10**6)) |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize("size", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 83 |
def test_start_will_equal_size(size): |
| 84 |
find_any( |
| 85 |
st.slices(size), lambda x: x.start == size - 1, settings(max_examples=10**6) |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
@given(st.integers(1, 1000)) |
| 90 |
@settings(deadline=None, max_examples=20) |
| 91 |
def test_start_will_equal_0(size): |
| 92 |
find_any(st.slices(size), lambda x: x.start == 0) |
| 93 |
|
| 94 |
|
| 95 |
@given(st.integers(1, 1000)) |
| 96 |
@settings(deadline=None) |
| 97 |
def test_start_will_equal_stop(size): |
| 98 |
find_any(st.slices(size), lambda x: x.start == x.stop) |
| 99 |
|
| 100 |
|
| 101 |
def test_size_is_equal_0(): |
| 102 |
assert_all_examples( |
| 103 |
st.slices(0), lambda x: x.step != 0 and x.start is None and x.stop is None |
| 104 |
) |