| 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 numpy |
| 12 |
import pytest |
| 13 |
|
| 14 |
from hypothesis import strategies as st |
| 15 |
from hypothesis.errors import InvalidArgument |
| 16 |
from hypothesis.extra import numpy as nps |
| 17 |
|
| 18 |
from tests.common.debug import check_can_generate_examples |
| 19 |
from tests.common.utils import checks_deprecated_behaviour |
| 20 |
|
| 21 |
|
| 22 |
def e(a, **kwargs): |
| 23 |
kw = ", ".join(f"{k}={v!r}" for k, v in kwargs.items()) |
| 24 |
return pytest.param(a, kwargs, id=f"{a.__name__}({kw})") |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
("function", "kwargs"), |
| 29 |
[ |
| 30 |
e(nps.array_dtypes, min_size=2, max_size=1), |
| 31 |
e(nps.array_dtypes, min_size=-1), |
| 32 |
e(nps.array_shapes, min_side=2, max_side=1), |
| 33 |
e(nps.array_shapes, min_dims=3, max_dims=2), |
| 34 |
e(nps.array_shapes, min_dims=-1), |
| 35 |
e(nps.array_shapes, min_side=-1), |
| 36 |
e(nps.array_shapes, min_side="not an int"), |
| 37 |
e(nps.array_shapes, max_side="not an int"), |
| 38 |
e(nps.array_shapes, min_dims="not an int"), |
| 39 |
e(nps.array_shapes, max_dims="not an int"), |
| 40 |
e(nps.array_shapes, min_dims=33), |
| 41 |
e(nps.array_shapes, max_dims=33), |
| 42 |
e(nps.arrays, dtype=float, shape=(0.5,)), |
| 43 |
e(nps.arrays, dtype=numpy.void, shape=1), |
| 44 |
e(nps.arrays, dtype=float, shape=1, fill=3), |
| 45 |
e(nps.arrays, dtype="U", shape=1, elements=st.just("abc\0\0")), |
| 46 |
e(nps.arrays, dtype=int, shape=1, elements="not a strategy"), |
| 47 |
e(nps.byte_string_dtypes, min_len=-1), |
| 48 |
e(nps.byte_string_dtypes, min_len=2, max_len=1), |
| 49 |
e(nps.byte_string_dtypes, min_len=0, max_len=0), |
| 50 |
e(nps.datetime64_dtypes, max_period=11), |
| 51 |
e(nps.datetime64_dtypes, min_period=11), |
| 52 |
e(nps.datetime64_dtypes, min_period="Y", max_period="M"), |
| 53 |
e(nps.timedelta64_dtypes, max_period=11), |
| 54 |
e(nps.timedelta64_dtypes, min_period=11), |
| 55 |
e(nps.timedelta64_dtypes, min_period="Y", max_period="M"), |
| 56 |
e(nps.unicode_string_dtypes, min_len=-1), |
| 57 |
e(nps.unicode_string_dtypes, min_len=2, max_len=1), |
| 58 |
e(nps.unicode_string_dtypes, min_len=0, max_len=0), |
| 59 |
e(nps.unsigned_integer_dtypes, endianness=3), |
| 60 |
e(nps.unsigned_integer_dtypes, sizes=()), |
| 61 |
e(nps.unsigned_integer_dtypes, sizes=(3,)), |
| 62 |
e(nps.from_dtype, dtype="float64"), |
| 63 |
e(nps.from_dtype, dtype=float), |
| 64 |
e(nps.from_dtype, dtype=numpy.int8), |
| 65 |
e(nps.from_dtype, dtype=1), |
| 66 |
e(nps.from_dtype, dtype=numpy.dtype("uint8"), min_value=-999), |
| 67 |
e(nps.from_dtype, dtype=numpy.dtype("uint8"), max_value=999), |
| 68 |
e(nps.from_dtype, dtype=numpy.dtype("int8"), min_value=-999), |
| 69 |
e(nps.from_dtype, dtype=numpy.dtype("int8"), max_value=999), |
| 70 |
e(nps.from_dtype, dtype=numpy.dtype("S4"), max_size=5), |
| 71 |
e(nps.from_dtype, dtype=numpy.dtype("U4"), max_size=5), |
| 72 |
e(nps.valid_tuple_axes, ndim=-1), |
| 73 |
e(nps.valid_tuple_axes, ndim=2, min_size=-1), |
| 74 |
e(nps.valid_tuple_axes, ndim=2, min_size=3, max_size=10), |
| 75 |
e(nps.valid_tuple_axes, ndim=2, min_size=2, max_size=1), |
| 76 |
e(nps.valid_tuple_axes, ndim=2.0, min_size=2, max_size=1), |
| 77 |
e(nps.valid_tuple_axes, ndim=2, min_size=1.0, max_size=2), |
| 78 |
e(nps.valid_tuple_axes, ndim=2, min_size=1, max_size=2.0), |
| 79 |
e(nps.valid_tuple_axes, ndim=2, min_size=1, max_size=3), |
| 80 |
e(nps.broadcastable_shapes, shape="a"), |
| 81 |
e(nps.broadcastable_shapes, shape=(2, 2), min_side="a"), |
| 82 |
e(nps.broadcastable_shapes, shape=(2, 2), min_dims="a"), |
| 83 |
e(nps.broadcastable_shapes, shape=(2, 2), max_side="a"), |
| 84 |
e(nps.broadcastable_shapes, shape=(2, 2), max_dims="a"), |
| 85 |
e(nps.broadcastable_shapes, shape=(2, 2), min_side=-1), |
| 86 |
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=-1), |
| 87 |
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=33, max_dims=None), |
| 88 |
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=1, max_dims=33), |
| 89 |
e(nps.broadcastable_shapes, shape=(2, 2), min_side=1, max_side=0), |
| 90 |
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=1, max_dims=0), |
| 91 |
e( |
| 92 |
nps.broadcastable_shapes, # max_side too small |
| 93 |
shape=(5, 1), |
| 94 |
min_dims=2, |
| 95 |
max_dims=4, |
| 96 |
min_side=2, |
| 97 |
max_side=3, |
| 98 |
), |
| 99 |
e( |
| 100 |
nps.broadcastable_shapes, # min_side too large |
| 101 |
shape=(0, 1), |
| 102 |
min_dims=2, |
| 103 |
max_dims=4, |
| 104 |
min_side=2, |
| 105 |
max_side=3, |
| 106 |
), |
| 107 |
e( |
| 108 |
nps.broadcastable_shapes, # default max_dims unsatisfiable |
| 109 |
shape=(5, 3, 2, 1), |
| 110 |
min_dims=3, |
| 111 |
max_dims=None, |
| 112 |
min_side=2, |
| 113 |
max_side=3, |
| 114 |
), |
| 115 |
e( |
| 116 |
nps.broadcastable_shapes, # default max_dims unsatisfiable |
| 117 |
shape=(0, 3, 2, 1), |
| 118 |
min_dims=3, |
| 119 |
max_dims=None, |
| 120 |
min_side=2, |
| 121 |
max_side=3, |
| 122 |
), |
| 123 |
e(nps.mutually_broadcastable_shapes), |
| 124 |
e(nps.mutually_broadcastable_shapes, num_shapes=0), |
| 125 |
e(nps.mutually_broadcastable_shapes, num_shapes="a"), |
| 126 |
e(nps.mutually_broadcastable_shapes, num_shapes=2, base_shape="a"), |
| 127 |
e( |
| 128 |
nps.mutually_broadcastable_shapes, # min_side is invalid type |
| 129 |
num_shapes=2, |
| 130 |
min_side="a", |
| 131 |
), |
| 132 |
e( |
| 133 |
nps.mutually_broadcastable_shapes, # min_dims is invalid type |
| 134 |
num_shapes=2, |
| 135 |
min_dims="a", |
| 136 |
), |
| 137 |
e( |
| 138 |
nps.mutually_broadcastable_shapes, # max_side is invalid type |
| 139 |
num_shapes=2, |
| 140 |
max_side="a", |
| 141 |
), |
| 142 |
e( |
| 143 |
nps.mutually_broadcastable_shapes, # max_side is invalid type |
| 144 |
num_shapes=2, |
| 145 |
max_dims="a", |
| 146 |
), |
| 147 |
e( |
| 148 |
nps.mutually_broadcastable_shapes, # min_side is out of domain |
| 149 |
num_shapes=2, |
| 150 |
min_side=-1, |
| 151 |
), |
| 152 |
e( |
| 153 |
nps.mutually_broadcastable_shapes, # min_dims is out of domain |
| 154 |
num_shapes=2, |
| 155 |
min_dims=-1, |
| 156 |
), |
| 157 |
e( |
| 158 |
nps.mutually_broadcastable_shapes, # min_dims is out of domain |
| 159 |
num_shapes=2, |
| 160 |
min_dims=33, |
| 161 |
), |
| 162 |
e( |
| 163 |
nps.mutually_broadcastable_shapes, # max_dims is out of domain |
| 164 |
num_shapes=2, |
| 165 |
max_dims=33, |
| 166 |
), |
| 167 |
e( |
| 168 |
nps.mutually_broadcastable_shapes, # max_side < min_side |
| 169 |
num_shapes=2, |
| 170 |
min_side=1, |
| 171 |
max_side=0, |
| 172 |
), |
| 173 |
e( |
| 174 |
nps.mutually_broadcastable_shapes, # max_dims < min_dims |
| 175 |
num_shapes=2, |
| 176 |
min_dims=1, |
| 177 |
max_dims=0, |
| 178 |
), |
| 179 |
e( |
| 180 |
nps.mutually_broadcastable_shapes, # max_side too small |
| 181 |
num_shapes=2, |
| 182 |
base_shape=(5, 1), |
| 183 |
min_dims=2, |
| 184 |
max_dims=4, |
| 185 |
min_side=2, |
| 186 |
max_side=3, |
| 187 |
), |
| 188 |
e( |
| 189 |
nps.mutually_broadcastable_shapes, # min_side too large |
| 190 |
num_shapes=2, |
| 191 |
base_shape=(0, 1), |
| 192 |
min_dims=2, |
| 193 |
max_dims=4, |
| 194 |
min_side=2, |
| 195 |
max_side=3, |
| 196 |
), |
| 197 |
e( |
| 198 |
nps.mutually_broadcastable_shapes, # user-specified max_dims unsatisfiable |
| 199 |
num_shapes=1, |
| 200 |
base_shape=(5, 3, 2, 1), |
| 201 |
min_dims=3, |
| 202 |
max_dims=4, |
| 203 |
min_side=2, |
| 204 |
max_side=3, |
| 205 |
), |
| 206 |
e( |
| 207 |
nps.mutually_broadcastable_shapes, # user-specified max_dims unsatisfiable |
| 208 |
num_shapes=2, |
| 209 |
base_shape=(0, 3, 2, 1), |
| 210 |
min_dims=3, |
| 211 |
max_dims=4, |
| 212 |
min_side=2, |
| 213 |
max_side=3, |
| 214 |
), |
| 215 |
e( |
| 216 |
nps.mutually_broadcastable_shapes, # valid to pass num_shapes xor gufunc |
| 217 |
num_shapes=2, |
| 218 |
signature="()->()", |
| 219 |
), |
| 220 |
e( |
| 221 |
nps.mutually_broadcastable_shapes, # element-wise ufunc has signature=None |
| 222 |
signature=numpy.add.signature, |
| 223 |
), |
| 224 |
e( |
| 225 |
nps.mutually_broadcastable_shapes, # multiple outputs not yet supported |
| 226 |
signature="()->(),()", |
| 227 |
), |
| 228 |
e( |
| 229 |
nps.mutually_broadcastable_shapes, # output has dimension not in inputs |
| 230 |
signature="()->(i)", |
| 231 |
), |
| 232 |
e( |
| 233 |
nps.mutually_broadcastable_shapes, # frozen-optional is ambiguous & banned |
| 234 |
signature="(2?)->()", |
| 235 |
), |
| 236 |
e( |
| 237 |
nps.mutually_broadcastable_shapes, # signature must be in string format |
| 238 |
signature=([(), ()], [()]), |
| 239 |
), |
| 240 |
e( |
| 241 |
nps.mutually_broadcastable_shapes, # string must match signature regex |
| 242 |
signature="this string isn't a valid signature", |
| 243 |
), |
| 244 |
e( |
| 245 |
nps.mutually_broadcastable_shapes, # shape with too many dimensions |
| 246 |
signature="(" + ",".join(f"d{n}" for n in range(33)) + ")->()", |
| 247 |
), |
| 248 |
e( |
| 249 |
nps.mutually_broadcastable_shapes, # max_dims too large given ufunc |
| 250 |
signature=numpy.matmul.signature, |
| 251 |
max_dims=32, |
| 252 |
), |
| 253 |
e( |
| 254 |
nps.mutually_broadcastable_shapes, # least valid max_dims is < min_dims |
| 255 |
signature=numpy.matmul.signature, |
| 256 |
min_dims=32, |
| 257 |
), |
| 258 |
e(nps.basic_indices, shape=0), |
| 259 |
e(nps.basic_indices, shape=("1", "2")), |
| 260 |
e(nps.basic_indices, shape=(0, -1)), |
| 261 |
e(nps.basic_indices, shape=(0, 0), allow_newaxis=None), |
| 262 |
e(nps.basic_indices, shape=(0, 0), allow_ellipsis=None), |
| 263 |
e(nps.basic_indices, shape=(0, 0), min_dims=-1), |
| 264 |
e(nps.basic_indices, shape=(0, 0), min_dims=1.0), |
| 265 |
e(nps.basic_indices, shape=(0, 0), max_dims=-1), |
| 266 |
e(nps.basic_indices, shape=(0, 0), max_dims=1.0), |
| 267 |
e(nps.basic_indices, shape=(0, 0), min_dims=2, max_dims=1), |
| 268 |
e(nps.basic_indices, shape=(3, 3, 3), max_dims="not an int"), |
| 269 |
e(nps.integer_array_indices, shape=()), |
| 270 |
e(nps.integer_array_indices, shape=(2, 0)), |
| 271 |
e(nps.integer_array_indices, shape="a"), |
| 272 |
e(nps.integer_array_indices, shape=(2,), result_shape=(2, 2)), |
| 273 |
e(nps.integer_array_indices, shape=(2,), dtype=float), |
| 274 |
], |
| 275 |
) |
| 276 |
def test_raise_invalid_argument(function, kwargs): |
| 277 |
with pytest.raises(InvalidArgument): |
| 278 |
check_can_generate_examples(function(**kwargs)) |
| 279 |
|
| 280 |
|
| 281 |
@pytest.mark.parametrize( |
| 282 |
("function", "kwargs"), |
| 283 |
[ |
| 284 |
e(nps.basic_indices, shape=(0, 0), min_dims=50), |
| 285 |
e(nps.basic_indices, shape=(0, 0), max_dims=50), |
| 286 |
], |
| 287 |
) |
| 288 |
@checks_deprecated_behaviour |
| 289 |
def test_raise_invalid_argument_deprecated(function, kwargs): |
| 290 |
with pytest.raises(InvalidArgument): |
| 291 |
check_can_generate_examples(function(**kwargs)) |