Skip to content

Commit ecd1dea

Browse files
committed
add testCodecsWriteRead
1 parent 18693d7 commit ecd1dea

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

src/test/java/dev/zarr/zarrjava/ZarrTest.java

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
import java.nio.file.Paths;
2424
import java.util.Arrays;
2525
import java.util.Comparator;
26-
import java.util.HashMap;
26+
import java.util.Map;
2727
import java.util.stream.Stream;
2828

29-
import org.junit.Ignore;
3029
import org.junit.jupiter.api.*;
3130
import org.junit.jupiter.params.ParameterizedTest;
3231
import org.junit.jupiter.params.provider.ValueSource;
@@ -174,7 +173,6 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
174173
Arrays.setAll(data, p -> p);
175174
array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16}, data));
176175

177-
178176
String command = "zarrita/bin/python";
179177

180178
ProcessBuilder pb = new ProcessBuilder(command, ZARRITA_READ_PATH.toString(), codec, TESTOUTPUT.toString());
@@ -197,6 +195,60 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
197195
}
198196

199197

198+
@ParameterizedTest
199+
@ValueSource(strings = {"blosc", "gzip", "zstd", "bytes", "transpose", "sharding"})
200+
public void testCodecsWriteRead(String codec) throws IOException, ZarrException, InterruptedException {
201+
int[] testData = new int[16 * 16 * 16];
202+
Arrays.setAll(testData, p -> p);
203+
204+
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("testWriteAndRead3d", codec);
205+
ArrayMetadataBuilder builder = Array.metadataBuilder()
206+
.withShape(16, 16, 16)
207+
.withDataType(DataType.UINT32)
208+
.withChunkShape(2, 4, 8)
209+
.withFillValue(0)
210+
.withAttributes(Map.of("test_key", "test_value"));
211+
212+
switch (codec){
213+
case "blosc":
214+
builder = builder.withCodecs(c -> c.withBlosc());
215+
break;
216+
case "gzip":
217+
builder = builder.withCodecs(c -> c.withGzip());
218+
break;
219+
case "zstd":
220+
builder = builder.withCodecs(c -> c.withZstd(0));
221+
break;
222+
case "bytes":
223+
builder = builder.withCodecs(c -> c.withBytes("LITTLE"));
224+
break;
225+
case "transpose":
226+
builder = builder.withCodecs(c -> c.withTranspose("F"));
227+
break;
228+
case "sharding":
229+
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2, 4}, c1 -> c1.withBytes("LITTLE")));
230+
break;
231+
case "crc32c":
232+
//missing
233+
break;
234+
default:
235+
throw new IllegalArgumentException("Invalid Codec: "+codec);
236+
}
237+
238+
Array writeArray = Array.create(storeHandle,builder.build());
239+
writeArray.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16, 16}, testData));
240+
241+
Array readArray = Array.open(storeHandle);
242+
ucar.ma2.Array result = readArray.read();
243+
244+
Assertions.assertArrayEquals(new int[]{16, 16, 16}, result.getShape());
245+
Assertions.assertEquals(DataType.UINT32, readArray.metadata.dataType);
246+
Assertions.assertArrayEquals(new int[]{2, 4, 8}, readArray.metadata.chunkShape());
247+
Assertions.assertEquals("test_value", readArray.metadata.attributes.get("test_key"));
248+
249+
Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.INT));
250+
}
251+
200252
@Test
201253
public void testFileSystemStores() throws IOException, ZarrException {
202254
FilesystemStore fsStore = new FilesystemStore(TESTDATA);
@@ -269,7 +321,7 @@ public void testV3Access() throws IOException, ZarrException {
269321
writeArray.access().withOffset(0, 3073, 3073, 513).write(outArray);
270322
}
271323

272-
@Ignore
324+
@Disabled("uses excessive memory")
273325
@Test
274326
public void testV3ShardingReadWrite() throws IOException, ZarrException {
275327
Array readArray = Array.open(
@@ -287,16 +339,17 @@ public void testV3ShardingReadWrite() throws IOException, ZarrException {
287339

288340
@Test
289341
public void testV3Codecs() throws IOException, ZarrException {
342+
int[] readShape = new int[]{1, 1, 1024, 1024};
290343
Array readArray = Array.open(
291344
new FilesystemStore(TESTDATA).resolve("l4_sample", "color", "8-8-2"));
292-
ucar.ma2.Array readArrayContent = readArray.read();
345+
ucar.ma2.Array readArrayContent = readArray.read(new long[4], readShape);
293346
{
294347
Array gzipArray = Array.create(
295348
new FilesystemStore(TESTOUTPUT).resolve("l4_sample_gzip", "color", "8-8-2"),
296349
Array.metadataBuilder(readArray.metadata).withCodecs(c -> c.withGzip(5)).build()
297350
);
298351
gzipArray.write(readArrayContent);
299-
ucar.ma2.Array outGzipArray = gzipArray.read();
352+
ucar.ma2.Array outGzipArray = gzipArray.read(new long[4], readShape);
300353
assert MultiArrayUtils.allValuesEqual(outGzipArray, readArrayContent);
301354
}
302355
{
@@ -305,7 +358,7 @@ public void testV3Codecs() throws IOException, ZarrException {
305358
Array.metadataBuilder(readArray.metadata).withCodecs(c -> c.withBlosc("zstd", 5)).build()
306359
);
307360
bloscArray.write(readArrayContent);
308-
ucar.ma2.Array outBloscArray = bloscArray.read();
361+
ucar.ma2.Array outBloscArray = bloscArray.read(new long[4], readShape);
309362
assert MultiArrayUtils.allValuesEqual(outBloscArray, readArrayContent);
310363
}
311364
{
@@ -314,7 +367,7 @@ public void testV3Codecs() throws IOException, ZarrException {
314367
Array.metadataBuilder(readArray.metadata).withCodecs(c -> c.withZstd(10)).build()
315368
);
316369
zstdArray.write(readArrayContent);
317-
ucar.ma2.Array outZstdArray = zstdArray.read();
370+
ucar.ma2.Array outZstdArray = zstdArray.read(new long[4], readShape);
318371
assert MultiArrayUtils.allValuesEqual(outZstdArray, readArrayContent);
319372
}
320373
}
@@ -345,9 +398,7 @@ public void testV3Group() throws IOException, ZarrException {
345398
FilesystemStore fsStore = new FilesystemStore(TESTOUTPUT);
346399

347400
Group group = Group.create(fsStore.resolve("testgroup"));
348-
Group group2 = group.createGroup("test2", new HashMap<String, Object>() {{
349-
put("hello", "world");
350-
}});
401+
Group group2 = group.createGroup("test2", Map.of("hello", "world"));
351402
Array array = group2.createArray("array", b ->
352403
b.withShape(10, 10)
353404
.withDataType(DataType.UINT8)

0 commit comments

Comments
 (0)