Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for virtual_offsets function #265

Merged
merged 1 commit into from
Jul 18, 2023
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
2 changes: 2 additions & 0 deletions cubed/storage/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __getitem__(self, key):


def _key_to_index_tuple(selection):
if isinstance(selection, slice):
selection = (selection,)
assert all(isinstance(s, (slice, Integral)) for s in selection)
sel = []
for s in selection:
Expand Down
31 changes: 31 additions & 0 deletions cubed/tests/storage/test_virtual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from itertools import product
from math import prod

import numpy as np
import pytest

from cubed.storage.virtual import virtual_offsets


@pytest.mark.parametrize("shape", [(), (3,), (3, 2)])
def test_virtual_offsets(shape):
v_offsets = virtual_offsets(shape)
offsets = np.arange(prod(shape)).reshape(shape, order="C")
for t in product(*(range(n) for n in shape)):
assert v_offsets[t] == offsets[t]

# test some length 1 slices
if len(shape) == 1:
assert v_offsets[1:2] == offsets[1:2]
elif len(shape) == 2:
assert v_offsets[1:2, 0:1] == offsets[1:2, 0:1]


def test_virtual_offsets_fails():
with pytest.raises(NotImplementedError):
v_offsets = virtual_offsets((3,))
v_offsets[0:2]

with pytest.raises(NotImplementedError):
v_offsets = virtual_offsets((3, 2))
v_offsets[0:2, 1]