Skip to content

Commit

Permalink
Merge pull request #4 from dcherian/zero-chunks
Browse files Browse the repository at this point in the history
have property tests support zero-sized chunks
  • Loading branch information
brokkoli71 authored Oct 23, 2024
2 parents 07edb89 + a1eea78 commit 3749dc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 3749dc7

Please sign in to comment.