Skip to content

Commit f2c6ea3

Browse files
martaiborraFrancescAlted
authored andcommitted
Add tests
1 parent ae36c14 commit f2c6ea3

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/test_ucodecs.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,72 @@ def decoder1(input, output, meta, schunk):
8383
assert np.array_equal(data, out)
8484

8585
blosc2.remove_urlpath(urlpath)
86+
87+
88+
@pytest.mark.parametrize(
89+
"cparams, dparams",
90+
[
91+
({"codec": 163, "nthreads": 1}, {"nthreads": 4}),
92+
({"codec": 163, "nthreads": 4}, {"nthreads": 1}),
93+
],
94+
)
95+
def test_pyucodecs_error(cparams, dparams):
96+
chunk_len = 20 * 1000
97+
dtype = np.dtype(np.int32)
98+
99+
def encoder1(input, output, meta, schunk):
100+
nd_input = input.view(dtype)
101+
if np.max(nd_input) == np.min(nd_input):
102+
output[0 : schunk.typesize] = input[0 : schunk.typesize]
103+
n = nd_input.size.to_bytes(4, sys.byteorder)
104+
output[schunk.typesize : schunk.typesize + 4] = [n[i] for i in range(4)]
105+
return schunk.typesize + 4
106+
else:
107+
# memcpy
108+
return 0
109+
110+
def decoder1(input, output, meta, schunk):
111+
nd_input = input.view(np.int32)
112+
nd_output = output.view(dtype)
113+
nd_output[0 : nd_input[1]] = [nd_input[0]] * nd_input[1]
114+
return nd_input[1] * schunk.typesize
115+
116+
if cparams["codec"] not in blosc2.ucodecs_registry:
117+
blosc2.register_codec("codec3", cparams["codec"], encoder1, decoder1)
118+
119+
nchunks = 2
120+
fill_value = 341
121+
data = np.full(chunk_len * nchunks, fill_value, dtype=dtype)
122+
123+
with pytest.raises(ValueError):
124+
_ = blosc2.SChunk(
125+
chunksize=chunk_len * dtype.itemsize,
126+
data=data,
127+
cparams=cparams,
128+
dparams=dparams,
129+
)
130+
131+
132+
@pytest.mark.parametrize(
133+
"cparams, dparams",
134+
[
135+
({"codec": 254, "nthreads": 1}, {"nthreads": 4}),
136+
({"codec": 254, "nthreads": 4}, {"nthreads": 1}),
137+
],
138+
)
139+
def test_dynamic_ucodecs_error(cparams, dparams):
140+
blosc2.register_codec("codec4", cparams["codec"], None, None)
141+
142+
chunk_len = 100
143+
dtype = np.dtype(np.int32)
144+
nchunks = 1
145+
fill_value = 341
146+
data = np.arange(chunk_len * nchunks, dtype=dtype)
147+
148+
with pytest.raises(RuntimeError):
149+
schunk = blosc2.SChunk(
150+
chunksize=chunk_len * dtype.itemsize,
151+
data=data,
152+
cparams=cparams,
153+
dparams=dparams,
154+
)

tests/test_ufilters.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,64 @@ def backward2(input, output, meta, schunk):
9090
assert np.array_equal(data, out)
9191

9292
blosc2.remove_urlpath(urlpath)
93+
94+
@pytest.mark.parametrize(
95+
"cparams, dparams",
96+
[
97+
({"nthreads": 4, "filters": [255, blosc2.Filter.SHUFFLE], "filters_meta": [0, 0]}, {"nthreads": 1}),
98+
({"nthreads": 1, "filters": [255], "filters_meta": [4]}, {"nthreads": 4})
99+
],
100+
)
101+
def test_pyufilters_error(cparams, dparams):
102+
dtype = np.dtype(np.int32)
103+
def forward(input, output, meta, schunk):
104+
nd_input = input.view(dtype)
105+
nd_output = output.view(dtype)
106+
107+
nd_output[:] = nd_input + 1
108+
109+
def backward(input, output, meta, schunk):
110+
nd_input = input.view(dtype)
111+
nd_output = output.view(dtype)
112+
113+
nd_output[:] = nd_input - 1
114+
if 255 not in blosc2.ufilters_registry:
115+
blosc2.register_filter(255, forward, backward)
116+
117+
nchunks = 1
118+
chunk_len = 100
119+
fill_value = 341
120+
data = np.full(chunk_len * nchunks, fill_value, dtype=dtype)
121+
122+
with pytest.raises(ValueError):
123+
_ = blosc2.SChunk(
124+
chunksize=chunk_len * dtype.itemsize,
125+
data=data,
126+
cparams=cparams,
127+
dparams=dparams,
128+
)
129+
130+
131+
@pytest.mark.parametrize(
132+
"cparams, dparams",
133+
[
134+
({"nthreads": 4, "filters": [163, blosc2.Filter.SHUFFLE], "filters_meta": [0, 0]}, {"nthreads": 1}),
135+
({"nthreads": 1, "filters": [163], "filters_meta": [4]}, {"nthreads": 4})
136+
],
137+
)
138+
def test_dynamic_ufilters_error(cparams, dparams):
139+
dtype = np.dtype(np.int32)
140+
blosc2.register_filter(163, None, None, "ufilter_test")
141+
142+
nchunks = 1
143+
chunk_len = 100
144+
fill_value = 341
145+
data = np.full(chunk_len * nchunks, fill_value, dtype=dtype)
146+
147+
with pytest.raises(RuntimeError):
148+
_ = blosc2.SChunk(
149+
chunksize=chunk_len * dtype.itemsize,
150+
data=data,
151+
cparams=cparams,
152+
dparams=dparams,
153+
)

0 commit comments

Comments
 (0)