Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,24 @@ def numpy_arrays(
@st.composite # type: ignore[misc]
def np_array_and_chunks(
draw: st.DrawFn, *, arrays: st.SearchStrategy[np.ndarray] = numpy_arrays
) -> tuple[np.ndarray, tuple[int]]: # type: ignore[type-arg]
) -> tuple[np.ndarray, tuple[int, ...]]: # type: ignore[type-arg]
"""A hypothesis strategy to generate small sized random arrays.

Returns: a tuple of the array and a suitable random chunking for it.
"""
array = draw(arrays)
# We want this strategy to shrink towards arrays with smaller number of chunks
# 1. st.integers() shrinks towards smaller values. So we use that to generate number of chunks
numchunks = draw(st.tuples(*[st.integers(min_value=1, max_value=size) for size in array.shape]))
numchunks = draw(
st.tuples(
*[st.integers(min_value=0 if size == 0 else 1, max_value=size) for size in array.shape]
)
)
# 2. and now generate the chunks tuple
chunks = tuple(size // nchunks for size, nchunks in zip(array.shape, numchunks, strict=True))
chunks = tuple(
size // nchunks if nchunks > 0 else 0
for size, nchunks in zip(array.shape, numchunks, strict=True)
)
return (array, chunks)


Expand Down
6 changes: 4 additions & 2 deletions tests/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import hypothesis.extra.numpy as npst # noqa: E402
import hypothesis.strategies as st # noqa: E402
from hypothesis import given # noqa: E402
from hypothesis import assume, given # noqa: E402

from zarr.testing.strategies import arrays, basic_indices, numpy_arrays, zarr_formats # noqa: E402

Expand Down Expand Up @@ -35,11 +35,13 @@ def test_basic_indexing(data: st.DataObject) -> None:
@given(data=st.data())
def test_vindex(data: st.DataObject) -> None:
zarray = data.draw(arrays())
# integer_array_indices can't handle 0-size dimensions.
assume(all(s > 0 for s in zarray.shape))
nparray = zarray[:]

indexer = data.draw(
npst.integer_array_indices(
shape=nparray.shape, result_shape=npst.array_shapes(max_dims=None)
shape=nparray.shape, result_shape=npst.array_shapes(min_side=1, max_dims=None)
)
)
actual = zarray.vindex[indexer]
Expand Down