Skip to content

Commit 91778d8

Browse files
committed
add tests
1 parent 25d8169 commit 91778d8

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/main/java/dev/zarr/zarrjava/utils/IndexingUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static ChunkProjection computeProjection(
8787

8888
if (selOffset[dimIdx] + selShape[dimIdx] > dimLimit) {
8989
// selection ends after current chunk
90-
shape[dimIdx] = (int) (chunkShape[dimIdx] - chunkOffset[dimIdx]);
90+
shape[dimIdx] = chunkShape[dimIdx] - chunkOffset[dimIdx];
9191
} else {
9292
// selection ends within current chunk
9393
shape[dimIdx] = (int) (selOffset[dimIdx] + selShape[dimIdx] - dimOffset

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.zarr.zarrjava.core.Attributes;
77
import dev.zarr.zarrjava.store.FilesystemStore;
88
import dev.zarr.zarrjava.store.HttpStore;
9+
import dev.zarr.zarrjava.store.MemoryStore;
910
import dev.zarr.zarrjava.store.StoreHandle;
1011
import dev.zarr.zarrjava.utils.MultiArrayUtils;
1112
import dev.zarr.zarrjava.v3.*;
@@ -26,7 +27,6 @@
2627

2728
import java.io.BufferedReader;
2829
import java.io.IOException;
29-
import java.nio.ByteBuffer;
3030
import java.nio.file.Files;
3131
import java.nio.file.NoSuchFileException;
3232
import java.nio.file.Path;
@@ -107,6 +107,17 @@ static Stream<Function<ArrayMetadataBuilder, ArrayMetadataBuilder>> chunkKeyEnco
107107
return Stream.concat(builders, codecBuilders().map(codecFunc -> b -> b.withCodecs(codecFunc)));
108108
}
109109

110+
static Stream<Arguments> unalignedArrayAccessProvider() {
111+
Stream.Builder<Arguments> builder = Stream.builder();
112+
builder.add(Arguments.of(52, 17, 32));
113+
builder.add(Arguments.of(71, 11, 12));
114+
builder.add(Arguments.of(52, 17, 17));
115+
builder.add(Arguments.of(50, 3, 7));
116+
builder.add(Arguments.of(50, 3, 22));
117+
builder.add(Arguments.of(13, 31, 21));
118+
return builder.build();
119+
}
120+
110121
@ParameterizedTest
111122
@MethodSource("invalidCodecBuilder")
112123
public void testCheckInvalidCodecConfiguration(Function<CodecBuilder, CodecBuilder> codecBuilder) {
@@ -737,40 +748,29 @@ public void testGroupAttributes() throws IOException, ZarrException {
737748
Assertions.assertEquals("group_value", group.metadata().attributes().getString("group_attr"));
738749
}
739750

740-
@Test
741-
public void testTemp() throws ZarrException, IOException {
742-
String filePath = TESTOUTPUT + "/testTempV3.txt";
743-
int imageWidth = 52;
744-
int imageHeight = 1;
745-
int chunkWidth = 17;
746-
Array input = Array.create(
747-
new FilesystemStore(filePath).resolve("0"),
751+
@ParameterizedTest
752+
@MethodSource("unalignedArrayAccessProvider")
753+
public void testUnalignedArrayAccess(int arrayShape, int chunkShape, int accessShape) throws ZarrException, IOException {
754+
Array array = Array.create(
755+
new MemoryStore().resolve(),
748756
Array.metadataBuilder()
749-
.withShape(imageHeight, imageWidth)
750-
.withDataType(DataType.UINT8)
751-
.withChunkShape(imageHeight, chunkWidth)
757+
.withShape(arrayShape)
758+
.withDataType(DataType.UINT32)
759+
.withChunkShape(chunkShape)
752760
.withFillValue(0)
753761
.build()
754762
);
755763

756-
byte[] buf = new byte[imageWidth];
757-
for (int i = 0; i < buf.length; i++) {
758-
buf[i] = (byte) i;
759-
}
760-
ByteBuffer bytes = ByteBuffer.wrap(buf);
761-
ucar.ma2.Array data = ucar.ma2.Array.factory(ucar.ma2.DataType.BYTE, new int[]{imageHeight, imageWidth}, bytes);
762-
input.write(new long[]{0, 0}, data);
763-
764-
long[] readPosition = new long[]{0, 0};
765-
int[] readSize = new int[]{1, 32};
766-
for (int tile = 0; tile < buf.length; tile += readSize[1]) {
767-
readPosition[1] = tile;
768-
readSize[1] = (int) Math.min(readSize[1], buf.length - readPosition[1]);
769-
ucar.ma2.Array readTile = input.read(readPosition, readSize);
770-
for (int i = 0; i < readSize[1]; i++) {
771-
byte pixel = readTile.getByte(i);
772-
Assertions.assertEquals(buf[tile + i], pixel);
773-
}
764+
int[] testData = new int[arrayShape];
765+
Arrays.setAll(testData, p -> (byte) p);
766+
ucar.ma2.Array data = ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{arrayShape}, testData);
767+
array.write(data);
768+
769+
for (int i = 0; i < arrayShape; i += accessShape) {
770+
accessShape = Math.min(accessShape, arrayShape - i);
771+
ucar.ma2.Array result = array.read(new long[]{i}, new int[]{accessShape});
772+
int[] expectedData = Arrays.copyOfRange(testData, i, i + accessShape);
773+
Assertions.assertArrayEquals(expectedData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.UINT));
774774
}
775775
}
776776
}

0 commit comments

Comments
 (0)