| 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 math |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import given, settings, strategies as st |
| 16 |
from hypothesis.errors import InvalidArgument |
| 17 |
from hypothesis.extra.array_api import COMPLEX_NAMES, REAL_NAMES |
| 18 |
from hypothesis.internal.floats import width_smallest_normals |
| 19 |
|
| 20 |
from tests.array_api.common import ( |
| 21 |
MIN_VER_FOR_COMPLEX, |
| 22 |
dtype_name_params, |
| 23 |
flushes_to_zero, |
| 24 |
) |
| 25 |
from tests.common.debug import ( |
| 26 |
assert_all_examples, |
| 27 |
check_can_generate_examples, |
| 28 |
find_any, |
| 29 |
minimal, |
| 30 |
) |
| 31 |
from tests.common.utils import flaky |
| 32 |
|
| 33 |
|
| 34 |
def skip_on_missing_unique_values(xp): |
| 35 |
if not hasattr(xp, "unique_values"): |
| 36 |
pytest.mark.skip("xp.unique_values() is not required to exist") |
| 37 |
|
| 38 |
|
| 39 |
def xfail_on_indistinct_nans(xp): |
| 40 |
""" |
| 41 |
xp.unique_value() should return distinct NaNs - if not, tests that (rightly) |
| 42 |
assume such behaviour will likely fail. For example, NumPy 1.22 treats NaNs |
| 43 |
as indistinct, so tests that use this function will be marked as xfail. |
| 44 |
See https://mail.python.org/pipermail/numpy-discussion/2021-August/081995.html |
| 45 |
""" |
| 46 |
skip_on_missing_unique_values(xp) |
| 47 |
two_nans = xp.asarray([float("nan"), float("nan")]) |
| 48 |
if xp.unique_values(two_nans).size != 2: |
| 49 |
pytest.xfail("NaNs not distinct") |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize("dtype_name", dtype_name_params) |
| 53 |
def test_draw_arrays_from_dtype(xp, xps, dtype_name): |
| 54 |
"""Draw arrays from dtypes.""" |
| 55 |
dtype = getattr(xp, dtype_name) |
| 56 |
assert_all_examples(xps.arrays(dtype, ()), lambda x: x.dtype == dtype) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize("dtype_name", dtype_name_params) |
| 60 |
def test_draw_arrays_from_scalar_names(xp, xps, dtype_name): |
| 61 |
"""Draw arrays from dtype names.""" |
| 62 |
dtype = getattr(xp, dtype_name) |
| 63 |
assert_all_examples(xps.arrays(dtype_name, ()), lambda x: x.dtype == dtype) |
| 64 |
|
| 65 |
|
| 66 |
@given(data=st.data()) |
| 67 |
def test_draw_arrays_from_shapes(xp, xps, data): |
| 68 |
"""Draw arrays from shapes.""" |
| 69 |
shape = data.draw(xps.array_shapes()) |
| 70 |
x = data.draw(xps.arrays(xp.int8, shape)) |
| 71 |
assert x.ndim == len(shape) |
| 72 |
assert x.shape == shape |
| 73 |
|
| 74 |
|
| 75 |
@given(data=st.data()) |
| 76 |
def test_draw_arrays_from_int_shapes(xp, xps, data): |
| 77 |
"""Draw arrays from integers as shapes.""" |
| 78 |
size = data.draw(st.integers(0, 10)) |
| 79 |
x = data.draw(xps.arrays(xp.int8, size)) |
| 80 |
assert x.shape == (size,) |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.parametrize( |
| 84 |
"strat_name", |
| 85 |
[ |
| 86 |
"scalar_dtypes", |
| 87 |
"boolean_dtypes", |
| 88 |
"integer_dtypes", |
| 89 |
"unsigned_integer_dtypes", |
| 90 |
"floating_dtypes", |
| 91 |
"real_dtypes", |
| 92 |
pytest.param( |
| 93 |
"complex_dtypes", marks=pytest.mark.xp_min_version(MIN_VER_FOR_COMPLEX) |
| 94 |
), |
| 95 |
], |
| 96 |
) |
| 97 |
def test_draw_arrays_from_dtype_strategies(xp, xps, strat_name): |
| 98 |
"""Draw arrays from dtype strategies.""" |
| 99 |
strat_func = getattr(xps, strat_name) |
| 100 |
strat = strat_func() |
| 101 |
find_any(xps.arrays(strat, ())) |
| 102 |
|
| 103 |
|
| 104 |
@settings(deadline=None) |
| 105 |
@given(data=st.data()) |
| 106 |
def test_draw_arrays_from_dtype_name_strategies(xp, xps, data): |
| 107 |
"""Draw arrays from dtype name strategies.""" |
| 108 |
all_names = ("bool", *REAL_NAMES) |
| 109 |
if xps.api_version > "2021.12": |
| 110 |
all_names += COMPLEX_NAMES |
| 111 |
sample_names = data.draw( |
| 112 |
st.lists(st.sampled_from(all_names), min_size=1, unique=True) |
| 113 |
) |
| 114 |
find_any(xps.arrays(st.sampled_from(sample_names), ())) |
| 115 |
|
| 116 |
|
| 117 |
def test_generate_arrays_from_shapes_strategy(xp, xps): |
| 118 |
"""Generate arrays from shapes strategy.""" |
| 119 |
find_any(xps.arrays(xp.int8, xps.array_shapes())) |
| 120 |
|
| 121 |
|
| 122 |
def test_generate_arrays_from_integers_strategy_as_shape(xp, xps): |
| 123 |
"""Generate arrays from integers strategy as shapes strategy.""" |
| 124 |
find_any(xps.arrays(xp.int8, st.integers(0, 100))) |
| 125 |
|
| 126 |
|
| 127 |
def test_generate_arrays_from_zero_dimensions(xp, xps): |
| 128 |
"""Generate arrays from empty shape.""" |
| 129 |
assert_all_examples(xps.arrays(xp.int8, ()), lambda x: x.shape == ()) |
| 130 |
|
| 131 |
|
| 132 |
@given(data=st.data()) |
| 133 |
def test_generate_arrays_from_zero_sided_shapes(xp, xps, data): |
| 134 |
"""Generate arrays from shapes with at least one 0-sized dimension.""" |
| 135 |
shape = data.draw(xps.array_shapes(min_side=0).filter(lambda s: 0 in s)) |
| 136 |
arr = data.draw(xps.arrays(xp.int8, shape)) |
| 137 |
assert arr.shape == shape |
| 138 |
|
| 139 |
|
| 140 |
def test_generate_arrays_from_unsigned_ints(xp, xps): |
| 141 |
"""Generate arrays from unsigned integer dtype.""" |
| 142 |
assert_all_examples(xps.arrays(xp.uint32, (5, 5)), lambda x: xp.all(x >= 0)) |
| 143 |
# Ensure we're not just picking non-negative signed integers |
| 144 |
signed_max = xp.iinfo(xp.int32).max |
| 145 |
find_any(xps.arrays(xp.uint32, (5, 5)), lambda x: xp.any(x > signed_max)) |
| 146 |
|
| 147 |
|
| 148 |
def test_generate_arrays_from_0d_arrays(xp, xps): |
| 149 |
"""Generate arrays from 0d array elements.""" |
| 150 |
assert_all_examples( |
| 151 |
xps.arrays( |
| 152 |
dtype=xp.uint8, |
| 153 |
shape=(5, 5), |
| 154 |
elements=xps.from_dtype(xp.uint8).map( |
| 155 |
lambda e: xp.asarray(e, dtype=xp.uint8) |
| 156 |
), |
| 157 |
), |
| 158 |
lambda x: x.shape == (5, 5), |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def test_minimize_arrays_with_default_dtype_shape_strategies(xp, xps): |
| 163 |
"""Strategy with default scalar_dtypes and array_shapes strategies minimize |
| 164 |
to a boolean 1-dimensional array of size 1.""" |
| 165 |
smallest = minimal(xps.arrays(xps.scalar_dtypes(), xps.array_shapes())) |
| 166 |
assert smallest.shape == (1,) |
| 167 |
assert smallest.dtype == xp.bool |
| 168 |
assert not xp.any(smallest) |
| 169 |
|
| 170 |
|
| 171 |
def test_minimize_arrays_with_0d_shape_strategy(xp, xps): |
| 172 |
"""Strategy with shape strategy that can generate empty tuples minimizes to |
| 173 |
0d arrays.""" |
| 174 |
smallest = minimal(xps.arrays(xp.int8, xps.array_shapes(min_dims=0))) |
| 175 |
assert smallest.shape == () |
| 176 |
|
| 177 |
|
| 178 |
@pytest.mark.parametrize("dtype", dtype_name_params[1:]) |
| 179 |
def test_minimizes_numeric_arrays(xp, xps, dtype): |
| 180 |
"""Strategies with numeric dtypes minimize to zero-filled arrays.""" |
| 181 |
smallest = minimal(xps.arrays(dtype, (2, 2))) |
| 182 |
assert xp.all(smallest == 0) |
| 183 |
|
| 184 |
|
| 185 |
def test_minimize_large_uint_arrays(xp, xps): |
| 186 |
"""Strategy with uint dtype and largely sized shape minimizes to a good |
| 187 |
example.""" |
| 188 |
if not hasattr(xp, "nonzero"): |
| 189 |
pytest.skip("optional API") |
| 190 |
smallest = minimal(xps.arrays(xp.uint8, 100), lambda x: xp.any(x) and not xp.all(x)) |
| 191 |
assert xp.all(xp.logical_or(smallest == 0, smallest == 1)) |
| 192 |
idx = xp.nonzero(smallest)[0] |
| 193 |
assert idx.size in (1, smallest.size - 1) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.filterwarnings("ignore::RuntimeWarning") |
| 197 |
@flaky(max_runs=50, min_passes=1) |
| 198 |
def test_minimize_float_arrays(xp, xps): |
| 199 |
"""Strategy with float dtype minimizes to a good example. |
| 200 |
|
| 201 |
We filter runtime warnings and expect flaky array generation for |
| 202 |
specifically NumPy - this behaviour may not be required when testing |
| 203 |
with other array libraries. |
| 204 |
""" |
| 205 |
smallest = minimal(xps.arrays(xp.float32, 50), lambda x: xp.sum(x) >= 1.0) |
| 206 |
# TODO_IR the shrinker gets stuck when the first failure is math.inf, because |
| 207 |
# downcasting inf to a float32 overflows, triggering rejection sampling which |
| 208 |
# is then immediately not a shrink (specifically it overruns the attempt data). |
| 209 |
# |
| 210 |
# this should be resolved by adding float widths to the ir. |
| 211 |
assert xp.sum(smallest) in (1, 50) or all(math.isinf(v) for v in smallest) |
| 212 |
|
| 213 |
|
| 214 |
def test_minimizes_to_fill(xp, xps): |
| 215 |
"""Strategy with single fill value minimizes to arrays only containing said |
| 216 |
fill value.""" |
| 217 |
smallest = minimal(xps.arrays(xp.float32, 10, fill=st.just(3.0))) |
| 218 |
assert xp.all(smallest == 3.0) |
| 219 |
|
| 220 |
|
| 221 |
def test_generate_unique_arrays(xp, xps): |
| 222 |
"""Generates unique arrays.""" |
| 223 |
skip_on_missing_unique_values(xp) |
| 224 |
assert_all_examples( |
| 225 |
xps.arrays(xp.int8, st.integers(0, 20), unique=True), |
| 226 |
lambda x: xp.unique_values(x).size == x.size, |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
def test_cannot_draw_unique_arrays_with_too_small_elements(xp, xps): |
| 231 |
"""Unique strategy with elements strategy range smaller than its size raises |
| 232 |
helpful error.""" |
| 233 |
with pytest.raises(InvalidArgument): |
| 234 |
check_can_generate_examples( |
| 235 |
xps.arrays(xp.int8, 10, elements=st.integers(0, 5), unique=True) |
| 236 |
) |
| 237 |
|
| 238 |
|
| 239 |
def test_cannot_fill_arrays_with_non_castable_value(xp, xps): |
| 240 |
"""Strategy with fill not castable to dtype raises helpful error.""" |
| 241 |
with pytest.raises(InvalidArgument): |
| 242 |
check_can_generate_examples( |
| 243 |
xps.arrays(xp.int8, 10, fill=st.just("not a castable value")) |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
def test_generate_unique_arrays_with_high_collision_elements(xp, xps): |
| 248 |
"""Generates unique arrays with just elements of 0.0 and NaN fill.""" |
| 249 |
|
| 250 |
@given( |
| 251 |
xps.arrays( |
| 252 |
dtype=xp.float32, |
| 253 |
shape=st.integers(0, 20), |
| 254 |
elements=st.just(0.0), |
| 255 |
fill=st.just(xp.nan), |
| 256 |
unique=True, |
| 257 |
) |
| 258 |
) |
| 259 |
def test(x): |
| 260 |
zero_mask = x == 0.0 |
| 261 |
assert xp.sum(xp.astype(zero_mask, xp.uint8)) <= 1 |
| 262 |
|
| 263 |
test() |
| 264 |
|
| 265 |
|
| 266 |
def test_generate_unique_arrays_using_all_elements(xp, xps): |
| 267 |
"""Unique strategy with elements strategy range equal to its size will only |
| 268 |
generate arrays with one of each possible element.""" |
| 269 |
skip_on_missing_unique_values(xp) |
| 270 |
assert_all_examples( |
| 271 |
xps.arrays(xp.int8, (4,), elements=st.integers(0, 3), unique=True), |
| 272 |
lambda x: xp.unique_values(x).size == x.size, |
| 273 |
) |
| 274 |
|
| 275 |
|
| 276 |
def test_may_fill_unique_arrays_with_nan(xp, xps): |
| 277 |
"""Unique strategy with NaN fill can generate arrays holding NaNs.""" |
| 278 |
find_any( |
| 279 |
xps.arrays( |
| 280 |
dtype=xp.float32, |
| 281 |
shape=10, |
| 282 |
elements={"allow_nan": False}, |
| 283 |
unique=True, |
| 284 |
fill=st.just(xp.nan), |
| 285 |
), |
| 286 |
lambda x: xp.any(xp.isnan(x)), |
| 287 |
) |
| 288 |
|
| 289 |
|
| 290 |
def test_may_not_fill_unique_array_with_non_nan(xp, xps): |
| 291 |
"""Unique strategy with just fill elements of 0.0 raises helpful error.""" |
| 292 |
strat = xps.arrays( |
| 293 |
dtype=xp.float32, |
| 294 |
shape=10, |
| 295 |
elements={"allow_nan": False}, |
| 296 |
unique=True, |
| 297 |
fill=st.just(0.0), |
| 298 |
) |
| 299 |
with pytest.raises(InvalidArgument): |
| 300 |
check_can_generate_examples(strat) |
| 301 |
|
| 302 |
|
| 303 |
def test_floating_point_array(): |
| 304 |
import warnings |
| 305 |
|
| 306 |
from hypothesis.extra.array_api import make_strategies_namespace |
| 307 |
|
| 308 |
try: |
| 309 |
with warnings.catch_warnings(): |
| 310 |
warnings.simplefilter("ignore") |
| 311 |
import numpy.array_api as nxp |
| 312 |
except ModuleNotFoundError: |
| 313 |
import numpy as nxp |
| 314 |
xps = make_strategies_namespace(nxp) |
| 315 |
dtypes = xps.floating_dtypes() | xps.complex_dtypes() |
| 316 |
|
| 317 |
strat = xps.arrays(dtype=dtypes, shape=10) |
| 318 |
|
| 319 |
check_can_generate_examples(strat) |
| 320 |
|
| 321 |
|
| 322 |
@pytest.mark.parametrize( |
| 323 |
"kwargs", |
| 324 |
[ |
| 325 |
{"elements": st.just(300)}, |
| 326 |
{"elements": st.nothing(), "fill": st.just(300)}, |
| 327 |
], |
| 328 |
) |
| 329 |
def test_may_not_use_overflowing_integers(xp, xps, kwargs): |
| 330 |
"""Strategy with elements strategy range outside the dtype's bounds raises |
| 331 |
helpful error.""" |
| 332 |
with pytest.raises(InvalidArgument): |
| 333 |
check_can_generate_examples(xps.arrays(dtype=xp.int8, shape=1, **kwargs)) |
| 334 |
|
| 335 |
|
| 336 |
@pytest.mark.parametrize("fill", [False, True]) |
| 337 |
@pytest.mark.parametrize( |
| 338 |
"dtype, strat", |
| 339 |
[ |
| 340 |
("float32", st.floats(min_value=10**40, allow_infinity=False)), |
| 341 |
("float64", st.floats(min_value=10**40, allow_infinity=False)), |
| 342 |
pytest.param( |
| 343 |
"complex64", |
| 344 |
st.complex_numbers(min_magnitude=10**300, allow_infinity=False), |
| 345 |
marks=pytest.mark.xp_min_version(MIN_VER_FOR_COMPLEX), |
| 346 |
), |
| 347 |
], |
| 348 |
) |
| 349 |
def test_may_not_use_unrepresentable_elements(xp, xps, fill, dtype, strat): |
| 350 |
"""Strategy with elements not representable by the dtype raises helpful error.""" |
| 351 |
if fill: |
| 352 |
kw = {"elements": st.nothing(), "fill": strat} |
| 353 |
else: |
| 354 |
kw = {"elements": strat} |
| 355 |
with pytest.raises(InvalidArgument): |
| 356 |
check_can_generate_examples(xps.arrays(dtype=dtype, shape=1, **kw)) |
| 357 |
|
| 358 |
|
| 359 |
def test_floats_can_be_constrained(xp, xps): |
| 360 |
"""Strategy with float dtype and specified elements strategy range |
| 361 |
(inclusive) generates arrays with elements inside said range.""" |
| 362 |
assert_all_examples( |
| 363 |
xps.arrays( |
| 364 |
dtype=xp.float32, shape=10, elements={"min_value": 0, "max_value": 1} |
| 365 |
), |
| 366 |
lambda x: xp.all(x >= 0) and xp.all(x <= 1), |
| 367 |
) |
| 368 |
|
| 369 |
|
| 370 |
def test_floats_can_be_constrained_excluding_endpoints(xp, xps): |
| 371 |
"""Strategy with float dtype and specified elements strategy range |
| 372 |
(exclusive) generates arrays with elements inside said range.""" |
| 373 |
assert_all_examples( |
| 374 |
xps.arrays( |
| 375 |
dtype=xp.float32, |
| 376 |
shape=10, |
| 377 |
elements={ |
| 378 |
"min_value": 0, |
| 379 |
"max_value": 1, |
| 380 |
"exclude_min": True, |
| 381 |
"exclude_max": True, |
| 382 |
}, |
| 383 |
), |
| 384 |
lambda x: xp.all(x > 0) and xp.all(x < 1), |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
def test_is_still_unique_with_nan_fill(xp, xps): |
| 389 |
"""Unique strategy with NaN fill generates unique arrays.""" |
| 390 |
skip_on_missing_unique_values(xp) |
| 391 |
xfail_on_indistinct_nans(xp) |
| 392 |
assert_all_examples( |
| 393 |
xps.arrays( |
| 394 |
dtype=xp.float32, |
| 395 |
elements={"allow_nan": False}, |
| 396 |
shape=10, |
| 397 |
unique=True, |
| 398 |
fill=st.just(xp.nan), |
| 399 |
), |
| 400 |
lambda x: xp.unique_values(x).size == x.size, |
| 401 |
) |
| 402 |
|
| 403 |
|
| 404 |
def test_unique_array_with_fill_can_use_all_elements(xp, xps): |
| 405 |
"""Unique strategy with elements range equivalent to its size and NaN fill |
| 406 |
can generate arrays with all possible values.""" |
| 407 |
skip_on_missing_unique_values(xp) |
| 408 |
xfail_on_indistinct_nans(xp) |
| 409 |
find_any( |
| 410 |
xps.arrays( |
| 411 |
dtype=xp.float32, |
| 412 |
shape=10, |
| 413 |
unique=True, |
| 414 |
elements=st.integers(1, 9), |
| 415 |
fill=st.just(xp.nan), |
| 416 |
), |
| 417 |
lambda x: xp.unique_values(x).size == x.size, |
| 418 |
) |
| 419 |
|
| 420 |
|
| 421 |
def test_generate_unique_arrays_without_fill(xp, xps): |
| 422 |
"""Generate arrays from unique strategy with no fill. |
| 423 |
|
| 424 |
Covers the collision-related branches for fully dense unique arrays. |
| 425 |
Choosing 25 of 256 possible values means we're almost certain to see |
| 426 |
collisions thanks to the birthday paradox, but finding unique values should |
| 427 |
still be easy. |
| 428 |
""" |
| 429 |
skip_on_missing_unique_values(xp) |
| 430 |
assert_all_examples( |
| 431 |
xps.arrays(dtype=xp.uint8, shape=25, unique=True, fill=st.nothing()), |
| 432 |
lambda x: xp.unique_values(x).size == x.size, |
| 433 |
) |
| 434 |
|
| 435 |
|
| 436 |
def test_efficiently_generate_unique_arrays_using_all_elements(xp, xps): |
| 437 |
"""Unique strategy with elements strategy range equivalent to its size |
| 438 |
generates arrays with all possible values. Generation is not too slow. |
| 439 |
|
| 440 |
Avoids the birthday paradox with UniqueSampledListStrategy. |
| 441 |
""" |
| 442 |
skip_on_missing_unique_values(xp) |
| 443 |
assert_all_examples( |
| 444 |
xps.arrays(dtype=xp.int8, shape=255, unique=True), |
| 445 |
lambda x: xp.unique_values(x).size == x.size, |
| 446 |
) |
| 447 |
|
| 448 |
|
| 449 |
@given(st.data(), st.integers(-100, 100), st.integers(1, 100)) |
| 450 |
def test_array_element_rewriting(xp, xps, data, start, size): |
| 451 |
"""Unique strategy generates arrays with expected elements.""" |
| 452 |
x = data.draw( |
| 453 |
xps.arrays( |
| 454 |
dtype=xp.int64, |
| 455 |
shape=size, |
| 456 |
elements=st.integers(start, start + size - 1), |
| 457 |
unique=True, |
| 458 |
) |
| 459 |
) |
| 460 |
x_set_expect = xp.arange(start, start + size, dtype=xp.int64) |
| 461 |
x_set = xp.sort(xp.unique_values(x)) |
| 462 |
assert xp.all(x_set == x_set_expect) |
| 463 |
|
| 464 |
|
| 465 |
def test_generate_0d_arrays_with_no_fill(xp, xps): |
| 466 |
"""Generate arrays with zero-dimensions and no fill.""" |
| 467 |
assert_all_examples( |
| 468 |
xps.arrays(xp.bool, (), fill=st.nothing()), |
| 469 |
lambda x: x.dtype == xp.bool and x.shape == (), |
| 470 |
) |
| 471 |
|
| 472 |
|
| 473 |
@pytest.mark.parametrize("dtype", ["float32", "float64"]) |
| 474 |
@pytest.mark.parametrize("low", [-2.0, -1.0, 0.0, 1.0]) |
| 475 |
@given(st.data()) |
| 476 |
def test_excluded_min_in_float_arrays(xp, xps, dtype, low, data): |
| 477 |
"""Strategy with elements strategy excluding min does not generate arrays |
| 478 |
with elements less or equal to said min.""" |
| 479 |
strat = xps.arrays( |
| 480 |
dtype=dtype, |
| 481 |
shape=(), |
| 482 |
elements={ |
| 483 |
"min_value": low, |
| 484 |
"max_value": low + 1, |
| 485 |
"exclude_min": True, |
| 486 |
}, |
| 487 |
) |
| 488 |
x = data.draw(strat, label="array") |
| 489 |
assert xp.all(x > low) |
| 490 |
|
| 491 |
|
| 492 |
@st.composite |
| 493 |
def distinct_int64_integers(draw): |
| 494 |
used = draw(st.shared(st.builds(set), key="distinct_int64_integers.used")) |
| 495 |
i = draw(st.integers(-(2**63), 2**63 - 1).filter(lambda x: x not in used)) |
| 496 |
used.add(i) |
| 497 |
return i |
| 498 |
|
| 499 |
|
| 500 |
def test_does_not_reuse_distinct_integers(xp, xps): |
| 501 |
"""Strategy with distinct integer elements strategy generates arrays with |
| 502 |
distinct values.""" |
| 503 |
skip_on_missing_unique_values(xp) |
| 504 |
assert_all_examples( |
| 505 |
xps.arrays(xp.int64, 10, elements=distinct_int64_integers()), |
| 506 |
lambda x: xp.unique_values(x).size == x.size, |
| 507 |
) |
| 508 |
|
| 509 |
|
| 510 |
def test_may_reuse_distinct_integers_if_asked(xp, xps): |
| 511 |
"""Strategy with shared elements and fill strategies of distinct integers |
| 512 |
may generate arrays with non-distinct values.""" |
| 513 |
skip_on_missing_unique_values(xp) |
| 514 |
find_any( |
| 515 |
xps.arrays( |
| 516 |
xp.int64, |
| 517 |
10, |
| 518 |
elements=distinct_int64_integers(), |
| 519 |
fill=distinct_int64_integers(), |
| 520 |
), |
| 521 |
lambda x: xp.unique_values(x).size < x.size, |
| 522 |
) |
| 523 |
|
| 524 |
|
| 525 |
def test_subnormal_elements_validation(xp, xps): |
| 526 |
"""Strategy with subnormal elements strategy is correctly validated. |
| 527 |
|
| 528 |
For FTZ builds of array modules, a helpful error should raise. Conversely, |
| 529 |
for builds of array modules which support subnormals, the strategy should |
| 530 |
generate arrays without raising. |
| 531 |
""" |
| 532 |
elements = { |
| 533 |
"min_value": 0.0, |
| 534 |
"max_value": width_smallest_normals[32], |
| 535 |
"exclude_min": True, |
| 536 |
"exclude_max": True, |
| 537 |
"allow_subnormal": True, |
| 538 |
} |
| 539 |
strat = xps.arrays(xp.float32, 10, elements=elements) |
| 540 |
if flushes_to_zero(xp, width=32): |
| 541 |
with pytest.raises(InvalidArgument, match="Generated subnormal float"): |
| 542 |
check_can_generate_examples(strat) |
| 543 |
else: |
| 544 |
check_can_generate_examples(strat) |