| 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.errors import InvalidArgument |
| 14 |
from hypothesis.extra.array_api import NominalVersion, make_strategies_namespace |
| 15 |
|
| 16 |
from tests.array_api.common import MIN_VER_FOR_COMPLEX |
| 17 |
from tests.common.debug import check_can_generate_examples |
| 18 |
|
| 19 |
|
| 20 |
def e(name, *, _min_version: NominalVersion | None = None, **kwargs): |
| 21 |
kw = ", ".join(f"{k}={v!r}" for k, v in kwargs.items()) |
| 22 |
id_ = f"{name}({kw})" |
| 23 |
if _min_version is None: |
| 24 |
marks = () |
| 25 |
else: |
| 26 |
marks = pytest.mark.xp_min_version(_min_version) |
| 27 |
return pytest.param(name, kwargs, id=id_, marks=marks) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize( |
| 31 |
("strat_name", "kwargs"), |
| 32 |
[ |
| 33 |
e("arrays", dtype=1, shape=5), |
| 34 |
e("arrays", dtype=None, shape=5), |
| 35 |
e("arrays", dtype="int8", shape=(0.5,)), |
| 36 |
e("arrays", dtype="int8", shape=1, fill=3), |
| 37 |
e("arrays", dtype="int8", shape=1, elements="not a strategy"), |
| 38 |
e("arrays", dtype="int8", shape="not a shape or strategy"), |
| 39 |
e("array_shapes", min_side=2, max_side=1), |
| 40 |
e("array_shapes", min_dims=3, max_dims=2), |
| 41 |
e("array_shapes", min_dims=-1), |
| 42 |
e("array_shapes", min_side=-1), |
| 43 |
e("array_shapes", min_side="not an int"), |
| 44 |
e("array_shapes", max_side="not an int"), |
| 45 |
e("array_shapes", min_dims="not an int"), |
| 46 |
e("array_shapes", max_dims="not an int"), |
| 47 |
e("from_dtype", dtype=1), |
| 48 |
e("from_dtype", dtype=None), |
| 49 |
e("from_dtype", dtype="int8", min_value="not an int"), |
| 50 |
e("from_dtype", dtype="int8", max_value="not an int"), |
| 51 |
e("from_dtype", dtype="float32", min_value="not a float"), |
| 52 |
e("from_dtype", dtype="float32", max_value="not a float"), |
| 53 |
e("from_dtype", dtype="int8", min_value=10, max_value=5), |
| 54 |
e("from_dtype", dtype="float32", min_value=10, max_value=5), |
| 55 |
e("from_dtype", dtype="int8", min_value=-999), |
| 56 |
e("from_dtype", dtype="int8", max_value=-999), |
| 57 |
e("from_dtype", dtype="int8", min_value=999), |
| 58 |
e("from_dtype", dtype="int8", max_value=999), |
| 59 |
e("from_dtype", dtype="uint8", min_value=-999), |
| 60 |
e("from_dtype", dtype="uint8", max_value=-999), |
| 61 |
e("from_dtype", dtype="uint8", min_value=999), |
| 62 |
e("from_dtype", dtype="uint8", max_value=999), |
| 63 |
e("from_dtype", dtype="float32", min_value=-4e38), |
| 64 |
e("from_dtype", dtype="float32", max_value=-4e38), |
| 65 |
e("from_dtype", dtype="float32", min_value=4e38), |
| 66 |
e("from_dtype", dtype="float32", max_value=4e38), |
| 67 |
e("integer_dtypes", sizes=()), |
| 68 |
e("integer_dtypes", sizes=(3,)), |
| 69 |
e("unsigned_integer_dtypes", sizes=()), |
| 70 |
e("unsigned_integer_dtypes", sizes=(3,)), |
| 71 |
e("floating_dtypes", sizes=()), |
| 72 |
e("floating_dtypes", sizes=(3,)), |
| 73 |
e("complex_dtypes", _min_version=MIN_VER_FOR_COMPLEX, sizes=()), |
| 74 |
e("complex_dtypes", _min_version=MIN_VER_FOR_COMPLEX, sizes=(3,)), |
| 75 |
e("valid_tuple_axes", ndim=-1), |
| 76 |
e("valid_tuple_axes", ndim=2, min_size=-1), |
| 77 |
e("valid_tuple_axes", ndim=2, min_size=3, max_size=10), |
| 78 |
e("valid_tuple_axes", ndim=2, min_size=2, max_size=1), |
| 79 |
e("valid_tuple_axes", ndim=2.0, min_size=2, max_size=1), |
| 80 |
e("valid_tuple_axes", ndim=2, min_size=1.0, max_size=2), |
| 81 |
e("valid_tuple_axes", ndim=2, min_size=1, max_size=2.0), |
| 82 |
e("valid_tuple_axes", ndim=2, min_size=1, max_size=3), |
| 83 |
e("broadcastable_shapes", shape="a"), |
| 84 |
e("broadcastable_shapes", shape=(2, 2), min_side="a"), |
| 85 |
e("broadcastable_shapes", shape=(2, 2), min_dims="a"), |
| 86 |
e("broadcastable_shapes", shape=(2, 2), max_side="a"), |
| 87 |
e("broadcastable_shapes", shape=(2, 2), max_dims="a"), |
| 88 |
e("broadcastable_shapes", shape=(2, 2), min_side=-1), |
| 89 |
e("broadcastable_shapes", shape=(2, 2), min_dims=-1), |
| 90 |
e("broadcastable_shapes", shape=(2, 2), min_side=1, max_side=0), |
| 91 |
e("broadcastable_shapes", shape=(2, 2), min_dims=1, max_dims=0), |
| 92 |
e( |
| 93 |
"broadcastable_shapes", # max_side too small |
| 94 |
shape=(5, 1), |
| 95 |
min_dims=2, |
| 96 |
max_dims=4, |
| 97 |
min_side=2, |
| 98 |
max_side=3, |
| 99 |
), |
| 100 |
e( |
| 101 |
"broadcastable_shapes", # min_side too large |
| 102 |
shape=(0, 1), |
| 103 |
min_dims=2, |
| 104 |
max_dims=4, |
| 105 |
min_side=2, |
| 106 |
max_side=3, |
| 107 |
), |
| 108 |
e( |
| 109 |
"broadcastable_shapes", # default max_dims unsatisfiable |
| 110 |
shape=(5, 3, 2, 1), |
| 111 |
min_dims=3, |
| 112 |
max_dims=None, |
| 113 |
min_side=2, |
| 114 |
max_side=3, |
| 115 |
), |
| 116 |
e( |
| 117 |
"broadcastable_shapes", # default max_dims unsatisfiable |
| 118 |
shape=(0, 3, 2, 1), |
| 119 |
min_dims=3, |
| 120 |
max_dims=None, |
| 121 |
min_side=2, |
| 122 |
max_side=3, |
| 123 |
), |
| 124 |
e("mutually_broadcastable_shapes", num_shapes=0), |
| 125 |
e("mutually_broadcastable_shapes", num_shapes="a"), |
| 126 |
e("mutually_broadcastable_shapes", num_shapes=2, base_shape="a"), |
| 127 |
e( |
| 128 |
"mutually_broadcastable_shapes", # min_side is invalid type |
| 129 |
num_shapes=2, |
| 130 |
min_side="a", |
| 131 |
), |
| 132 |
e( |
| 133 |
"mutually_broadcastable_shapes", # min_dims is invalid type |
| 134 |
num_shapes=2, |
| 135 |
min_dims="a", |
| 136 |
), |
| 137 |
e( |
| 138 |
"mutually_broadcastable_shapes", # max_side is invalid type |
| 139 |
num_shapes=2, |
| 140 |
max_side="a", |
| 141 |
), |
| 142 |
e( |
| 143 |
"mutually_broadcastable_shapes", # max_side is invalid type |
| 144 |
num_shapes=2, |
| 145 |
max_dims="a", |
| 146 |
), |
| 147 |
e( |
| 148 |
"mutually_broadcastable_shapes", # min_side is out of domain |
| 149 |
num_shapes=2, |
| 150 |
min_side=-1, |
| 151 |
), |
| 152 |
e( |
| 153 |
"mutually_broadcastable_shapes", # min_dims is out of domain |
| 154 |
num_shapes=2, |
| 155 |
min_dims=-1, |
| 156 |
), |
| 157 |
e( |
| 158 |
"mutually_broadcastable_shapes", # max_side < min_side |
| 159 |
num_shapes=2, |
| 160 |
min_side=1, |
| 161 |
max_side=0, |
| 162 |
), |
| 163 |
e( |
| 164 |
"mutually_broadcastable_shapes", # max_dims < min_dims |
| 165 |
num_shapes=2, |
| 166 |
min_dims=1, |
| 167 |
max_dims=0, |
| 168 |
), |
| 169 |
e( |
| 170 |
"mutually_broadcastable_shapes", # max_side too small |
| 171 |
num_shapes=2, |
| 172 |
base_shape=(5, 1), |
| 173 |
min_dims=2, |
| 174 |
max_dims=4, |
| 175 |
min_side=2, |
| 176 |
max_side=3, |
| 177 |
), |
| 178 |
e( |
| 179 |
"mutually_broadcastable_shapes", # min_side too large |
| 180 |
num_shapes=2, |
| 181 |
base_shape=(0, 1), |
| 182 |
min_dims=2, |
| 183 |
max_dims=4, |
| 184 |
min_side=2, |
| 185 |
max_side=3, |
| 186 |
), |
| 187 |
e( |
| 188 |
"mutually_broadcastable_shapes", # user-specified max_dims unsatisfiable |
| 189 |
num_shapes=1, |
| 190 |
base_shape=(5, 3, 2, 1), |
| 191 |
min_dims=3, |
| 192 |
max_dims=4, |
| 193 |
min_side=2, |
| 194 |
max_side=3, |
| 195 |
), |
| 196 |
e( |
| 197 |
"mutually_broadcastable_shapes", # user-specified max_dims unsatisfiable |
| 198 |
num_shapes=2, |
| 199 |
base_shape=(0, 3, 2, 1), |
| 200 |
min_dims=3, |
| 201 |
max_dims=4, |
| 202 |
min_side=2, |
| 203 |
max_side=3, |
| 204 |
), |
| 205 |
e("indices", shape=0), |
| 206 |
e("indices", shape=("1", "2")), |
| 207 |
e("indices", shape=(0, -1)), |
| 208 |
e("indices", shape=(0, 0), allow_newaxis=None), |
| 209 |
e("indices", shape=(0, 0), allow_ellipsis=None), |
| 210 |
e("indices", shape=(0, 0), min_dims=-1), |
| 211 |
e("indices", shape=(0, 0), min_dims=1.0), |
| 212 |
e("indices", shape=(0, 0), max_dims=-1), |
| 213 |
e("indices", shape=(0, 0), max_dims=1.0), |
| 214 |
e("indices", shape=(0, 0), min_dims=2, max_dims=1), |
| 215 |
e("indices", shape=(3, 3, 3), min_dims=4), |
| 216 |
e("indices", shape=(3, 3, 3), max_dims=5), |
| 217 |
e("indices", shape=5, min_dims=0), |
| 218 |
e("indices", shape=(5,), min_dims=2), |
| 219 |
e("indices", shape=(5,), max_dims=2), |
| 220 |
], |
| 221 |
) |
| 222 |
def test_raise_invalid_argument(xp, xps, strat_name, kwargs): |
| 223 |
"""Strategies raise helpful error with invalid arguments.""" |
| 224 |
strat_func = getattr(xps, strat_name) |
| 225 |
strat = strat_func(**kwargs) |
| 226 |
with pytest.raises(InvalidArgument): |
| 227 |
check_can_generate_examples(strat) |
| 228 |
|
| 229 |
|
| 230 |
@pytest.mark.parametrize("api_version", [..., "latest", "1970.01", 42]) |
| 231 |
def test_make_strategies_namespace_raise_invalid_argument(xp, api_version): |
| 232 |
"""Function raises helpful error with invalid arguments.""" |
| 233 |
with pytest.raises(InvalidArgument): |
| 234 |
make_strategies_namespace(xp, api_version=api_version) |