| 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 copy |
| 12 |
import inspect |
| 13 |
import sys |
| 14 |
import warnings |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from hypothesis import example, given, strategies as st |
| 19 |
from hypothesis.errors import HypothesisWarning |
| 20 |
from hypothesis.internal.conjecture import junkdrawer |
| 21 |
from hypothesis.internal.conjecture.junkdrawer import ( |
| 22 |
IntList, |
| 23 |
LazySequenceCopy, |
| 24 |
binary_search, |
| 25 |
endswith, |
| 26 |
ensure_free_stackframes, |
| 27 |
replace_all, |
| 28 |
stack_depth_of_caller, |
| 29 |
startswith, |
| 30 |
) |
| 31 |
from hypothesis.internal.floats import clamp, float_to_int, sign_aware_lte |
| 32 |
|
| 33 |
from tests.common.utils import restore_recursion_limit |
| 34 |
|
| 35 |
|
| 36 |
def test_out_of_range(): |
| 37 |
x = LazySequenceCopy([1, 2, 3]) |
| 38 |
|
| 39 |
with pytest.raises(IndexError): |
| 40 |
x[3] |
| 41 |
|
| 42 |
with pytest.raises(IndexError): |
| 43 |
x[-4] |
| 44 |
|
| 45 |
|
| 46 |
def test_pass_through(): |
| 47 |
x = LazySequenceCopy([1, 2, 3]) |
| 48 |
assert x[0] == 1 |
| 49 |
assert x[1] == 2 |
| 50 |
assert x[2] == 3 |
| 51 |
|
| 52 |
|
| 53 |
def test_can_assign_without_changing_underlying(): |
| 54 |
underlying = [1, 2, 3] |
| 55 |
x = LazySequenceCopy(underlying) |
| 56 |
x[1] = 10 |
| 57 |
assert x[1] == 10 |
| 58 |
assert underlying[1] == 2 |
| 59 |
|
| 60 |
|
| 61 |
def test_pop(): |
| 62 |
x = LazySequenceCopy([2, 3]) |
| 63 |
assert x.pop() == 3 |
| 64 |
assert x.pop() == 2 |
| 65 |
|
| 66 |
with pytest.raises(IndexError): |
| 67 |
x.pop() |
| 68 |
|
| 69 |
|
| 70 |
@st.composite |
| 71 |
def clamp_inputs(draw): |
| 72 |
lower = draw(st.floats(allow_nan=False)) |
| 73 |
value = draw(st.floats(allow_nan=False)) |
| 74 |
upper = draw(st.floats(min_value=lower, allow_nan=False)) |
| 75 |
return (lower, value, upper) |
| 76 |
|
| 77 |
|
| 78 |
@example((1, 5, 10)) |
| 79 |
@example((1, 10, 5)) |
| 80 |
@example((5, 10, 5)) |
| 81 |
@example((5, 1, 10)) |
| 82 |
@example((-5, 0.0, -0.0)) |
| 83 |
@example((0.0, -0.0, 5)) |
| 84 |
@example((-0.0, 0.0, 0.0)) |
| 85 |
@example((-0.0, -0.0, 0.0)) |
| 86 |
@given(clamp_inputs()) |
| 87 |
def test_clamp(input): |
| 88 |
lower, value, upper = input |
| 89 |
clamped = clamp(lower, value, upper) |
| 90 |
|
| 91 |
assert sign_aware_lte(lower, clamped) |
| 92 |
assert sign_aware_lte(clamped, upper) |
| 93 |
if sign_aware_lte(lower, value) and sign_aware_lte(value, upper): |
| 94 |
assert float_to_int(value) == float_to_int(clamped) |
| 95 |
if lower > value: |
| 96 |
assert float_to_int(clamped) == float_to_int(lower) |
| 97 |
if value > upper: |
| 98 |
assert float_to_int(clamped) == float_to_int(upper) |
| 99 |
|
| 100 |
|
| 101 |
# this would be more robust as a stateful test, where each rule is a list operation |
| 102 |
# on (1) the canonical python list and (2) its LazySequenceCopy. We would assert |
| 103 |
# that the return values and lists match after each rule, and the original list |
| 104 |
# is unmodified. |
| 105 |
@pytest.mark.parametrize("should_mask", [True, False]) |
| 106 |
@given(lst=st.lists(st.integers(), min_size=1), data=st.data()) |
| 107 |
def test_pop_sequence_copy(lst, data, should_mask): |
| 108 |
original = copy.copy(lst) |
| 109 |
pop_i = data.draw(st.integers(0, len(lst) - 1)) |
| 110 |
if should_mask: |
| 111 |
mask_i = data.draw(st.integers(0, len(lst) - 1)) |
| 112 |
mask_value = data.draw(st.integers()) |
| 113 |
|
| 114 |
def pop(l): |
| 115 |
if should_mask: |
| 116 |
l[mask_i] = mask_value |
| 117 |
return l.pop(pop_i) |
| 118 |
|
| 119 |
expected = copy.copy(lst) |
| 120 |
l = LazySequenceCopy(lst) |
| 121 |
|
| 122 |
assert pop(expected) == pop(l) |
| 123 |
assert list(l) == expected |
| 124 |
# modifications to the LazySequenceCopy should not modify the original list |
| 125 |
assert original == lst |
| 126 |
|
| 127 |
|
| 128 |
def test_assignment(): |
| 129 |
y = [1, 2, 3] |
| 130 |
x = LazySequenceCopy(y) |
| 131 |
x[-1] = 5 |
| 132 |
assert list(x) == [1, 2, 5] |
| 133 |
x[-1] = 7 |
| 134 |
assert list(x) == [1, 2, 7] |
| 135 |
|
| 136 |
|
| 137 |
def test_replacement(): |
| 138 |
result = replace_all([1, 1, 1, 1], [(1, 3, [3, 4])]) |
| 139 |
assert result == [1, 3, 4, 1] |
| 140 |
|
| 141 |
|
| 142 |
def test_int_list_cannot_contain_negative(): |
| 143 |
with pytest.raises(ValueError): |
| 144 |
IntList([-1]) |
| 145 |
|
| 146 |
|
| 147 |
def test_int_list_can_contain_arbitrary_size(): |
| 148 |
n = 2**65 |
| 149 |
assert list(IntList([n])) == [n] |
| 150 |
|
| 151 |
|
| 152 |
def test_int_list_of_length(): |
| 153 |
assert list(IntList.of_length(10)) == [0] * 10 |
| 154 |
|
| 155 |
|
| 156 |
def test_int_list_equality(): |
| 157 |
ls = [1, 2, 3] |
| 158 |
x = IntList(ls) |
| 159 |
y = IntList(ls) |
| 160 |
|
| 161 |
assert ls != x |
| 162 |
assert x != ls |
| 163 |
assert not (x == ls) # noqa |
| 164 |
assert x == x |
| 165 |
assert x == y |
| 166 |
|
| 167 |
|
| 168 |
def test_int_list_extend(): |
| 169 |
x = IntList.of_length(3) |
| 170 |
n = 2**64 - 1 |
| 171 |
x.extend([n]) |
| 172 |
assert list(x) == [0, 0, 0, n] |
| 173 |
|
| 174 |
|
| 175 |
def test_int_list_slice(): |
| 176 |
x = IntList([1, 2]) |
| 177 |
assert list(x[:1]) == [1] |
| 178 |
assert list(x[0:2]) == [1, 2] |
| 179 |
assert list(x[1:]) == [2] |
| 180 |
|
| 181 |
|
| 182 |
def test_int_list_del(): |
| 183 |
x = IntList([1, 2]) |
| 184 |
del x[0] |
| 185 |
assert x == IntList([2]) |
| 186 |
|
| 187 |
|
| 188 |
def test_int_list_insert(): |
| 189 |
x = IntList([1, 3]) |
| 190 |
x.insert(1, 2) |
| 191 |
assert x == IntList([1, 2, 3]) |
| 192 |
|
| 193 |
|
| 194 |
@pytest.mark.parametrize("n", [0, 1, 30, 70]) |
| 195 |
def test_binary_search(n): |
| 196 |
i = binary_search(0, 100, lambda i: i <= n) |
| 197 |
assert i == n |
| 198 |
|
| 199 |
|
| 200 |
def recur(i): |
| 201 |
assert len(inspect.stack(0)) == stack_depth_of_caller() |
| 202 |
if i >= 1: |
| 203 |
recur(i - 1) |
| 204 |
|
| 205 |
|
| 206 |
def test_stack_size_detection(): |
| 207 |
recur(100) |
| 208 |
|
| 209 |
|
| 210 |
@given(st.binary(), st.binary()) |
| 211 |
def test_startswith(b1, b2): |
| 212 |
assert b1.startswith(b2) == startswith(b1, b2) |
| 213 |
|
| 214 |
|
| 215 |
@given(st.binary(), st.binary()) |
| 216 |
def test_endswith(b1, b2): |
| 217 |
assert b1.endswith(b2) == endswith(b1, b2) |
| 218 |
|
| 219 |
|
| 220 |
def test_stackframes_warns_when_recursion_limit_is_changed(): |
| 221 |
match = ( |
| 222 |
"The recursion limit will not be reset, since it was changed during " |
| 223 |
"test execution." |
| 224 |
) |
| 225 |
with ( |
| 226 |
restore_recursion_limit(), |
| 227 |
pytest.warns(HypothesisWarning, match=match) as warnings, |
| 228 |
ensure_free_stackframes(), |
| 229 |
): |
| 230 |
sys.setrecursionlimit(100) |
| 231 |
|
| 232 |
# we only got the warning once |
| 233 |
assert len(warnings) == 1 |
| 234 |
|
| 235 |
|
| 236 |
def test_stackframes_cleans_up_on_werror(): |
| 237 |
limiter = junkdrawer._stackframe_limiter |
| 238 |
with restore_recursion_limit(), warnings.catch_warnings(): |
| 239 |
warnings.simplefilter("error") |
| 240 |
assert limiter._active_contexts == 0 |
| 241 |
|
| 242 |
# the code for this cleanup case only triggers when the warning is raised |
| 243 |
# on __enter__. set that up by entering one context, changing the limit, |
| 244 |
# then entering another. |
| 245 |
with pytest.raises(HypothesisWarning), ensure_free_stackframes(): |
| 246 |
assert limiter._active_contexts == 1 |
| 247 |
sys.setrecursionlimit(101) |
| 248 |
|
| 249 |
with ensure_free_stackframes(): |
| 250 |
assert limiter._active_contexts == 2 |
| 251 |
sys.setrecursionlimit(102) |
| 252 |
|
| 253 |
assert limiter._active_contexts == 1 |
| 254 |
|
| 255 |
assert limiter._active_contexts == 0 |