Repository navigation
Expand file tree
/
Copy pathStoreTest.java
More file actions
235 lines (204 loc) · 9.31 KB
/
Copy pathStoreTest.java
File metadata and controls
235 lines (204 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package dev.zarr.zarrjava.store;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.ZarrTest;
import dev.zarr.zarrjava.core.Array;
import dev.zarr.zarrjava.core.Attributes;
import dev.zarr.zarrjava.core.Group;
import dev.zarr.zarrjava.core.Node;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import ucar.ma2.DataType;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class StoreTest extends ZarrTest {
/**
* Returns a StoreHandle with some test data written to it (optimally not written by the same Store implementation).
*/
abstract StoreHandle storeHandleWithData();
abstract StoreHandle storeHandleWithoutData();
/**
* Returns a Store with some test arrays written to it used to test list() and exist() (can be written by the same Store implementation).
*/
abstract Store storeWithArrays() throws ZarrException, IOException;
@Test
public void testInputStream() throws IOException {
StoreHandle storeHandle = storeHandleWithData();
InputStream is = storeHandle.getInputStream(10, 20);
byte[] buffer = new byte[10];
int bytesRead = is.read(buffer);
Assertions.assertEquals(10, bytesRead);
byte[] expectedBuffer = new byte[10];
storeHandle.read(10, 20).get(expectedBuffer);
Assertions.assertArrayEquals(expectedBuffer, buffer);
}
@Test
public void testInputStreamOpenEnded() throws IOException {
StoreHandle storeHandle = storeHandleWithData();
ByteBuffer expected = storeHandle.read();
try (InputStream is = storeHandle.getInputStream()) {
Assertions.assertNotNull(is, "Open-ended getInputStream() returned null");
byte[] actual = readFully(is);
byte[] expectedBytes = new byte[expected.remaining()];
expected.get(expectedBytes);
Assertions.assertArrayEquals(expectedBytes, actual);
}
}
private static byte[] readFully(InputStream is) throws IOException {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return baos.toByteArray();
}
@Test
public void testExists() throws ZarrException, IOException {
Assertions.assertTrue(storeHandleWithData().exists());
Assertions.assertFalse(storeHandleWithoutData().exists());
Assertions.assertFalse(storeWithArrays().resolve("").exists());
}
@Test
public void testListedItemsExist() throws IOException, ZarrException {
Store store = storeWithArrays();
if (!(store instanceof Store.ListableStore)) {
Assertions.fail("Store is not listable");
}
Set<String[]> nodes = ((Store.ListableStore) store).list().limit(10).collect(Collectors.toSet());
Assertions.assertFalse(nodes.isEmpty()); // to ensure the sensitivity of this test
nodes.forEach(keys -> {
StoreHandle handle = store.resolve(keys);
Assertions.assertTrue(handle.exists(), "Listed key does not exist: " + String.join("/", keys));
});
}
@Test
public abstract void testListChildren() throws ZarrException, IOException;
@Test
public void testGetSize() {
StoreHandle storeHandle = storeHandleWithData();
long size = storeHandle.getSize();
long actual_size = storeHandle.read().remaining();
Assertions.assertEquals(actual_size, size);
}
@Test
public void testGetWithStartEnd() {
StoreHandle storeHandle = storeHandleWithData();
long size = storeHandle.getSize();
if (size < 20) {
Assertions.fail("Store size is too small to test get with start and end");
}
ByteBuffer buffer = storeHandle.read(5, 15);
Assertions.assertEquals(10, buffer.remaining());
ByteBuffer fullBuffer = storeHandle.read();
byte[] expectedBytes = new byte[10];
fullBuffer.position(5);
fullBuffer.get(expectedBytes, 0, 10);
byte[] actualBytes = new byte[10];
buffer.get(actualBytes, 0, 10);
Assertions.assertArrayEquals(expectedBytes, actualBytes);
buffer = storeHandle.read(size - 10);
Assertions.assertEquals(10, buffer.remaining());
fullBuffer.position((int) (size - 10));
fullBuffer.get(expectedBytes, 0, 10);
buffer.get(actualBytes, 0, 10);
Assertions.assertArrayEquals(expectedBytes, actualBytes);
}
@Test
public void testGetSuffix() {
// a negative start denotes a suffix read (the last -start bytes), as used for sharding indexes
StoreHandle storeHandle = storeHandleWithData();
long size = storeHandle.getSize();
if (size < 20) {
Assertions.fail("Store size is too small to test suffix reads");
}
ByteBuffer buffer = storeHandle.read(-10);
Assertions.assertEquals(10, buffer.remaining());
ByteBuffer fullBuffer = storeHandle.read();
byte[] expectedBytes = new byte[10];
fullBuffer.position((int) (size - 10));
fullBuffer.get(expectedBytes, 0, 10);
byte[] actualBytes = new byte[10];
buffer.get(actualBytes, 0, 10);
Assertions.assertArrayEquals(expectedBytes, actualBytes);
}
@Test
public abstract void testList() throws ZarrException, IOException;
byte[] testData() {
byte[] testData = new byte[1024 * 1024];
for (int i = 0; i < testData.length; i++) {
testData[i] = (byte) (i % 256);
}
return testData;
}
int[] testDataInt() {
int[] testData = new int[1024 * 1024];
for (int i = 0; i < testData.length; i++) {
testData[i] = i;
}
return testData;
}
Group writeTestGroupV3(StoreHandle storeHandle, boolean useParallel) throws ZarrException, IOException {
dev.zarr.zarrjava.v3.Group group = dev.zarr.zarrjava.v3.Group.create(storeHandle);
dev.zarr.zarrjava.v3.Array array = group.createArray("array", b -> b
.withShape(1024, 1024)
.withDataType(dev.zarr.zarrjava.v3.DataType.UINT8)
.withChunkShape(512, 512)
);
array.write(ucar.ma2.Array.factory(DataType.BYTE, new int[]{1024, 1024}, testData()), useParallel);
dev.zarr.zarrjava.v3.Group subgroup = group.createGroup("subgroup");
dev.zarr.zarrjava.v3.Array subgrouparray = subgroup.createArray("array", b -> b
.withShape(1024, 1024)
.withDataType(dev.zarr.zarrjava.v3.DataType.UINT8)
.withChunkShape(512, 512)
);
subgrouparray.write(ucar.ma2.Array.factory(DataType.BYTE, new int[]{1024, 1024}, testData()), useParallel);
group.setAttributes(new Attributes(b -> b.set("some", "value")));
return group;
}
void assertIsTestGroupV3(Group group, boolean useParallel) throws ZarrException, IOException {
Stream<Node> nodes = group.list();
List<Node> nodeList = nodes.collect(Collectors.toList());
Assertions.assertEquals(3, nodeList.size());
Array array = (Array) group.get("array");
Assertions.assertNotNull(array);
ucar.ma2.Array result = array.read(useParallel);
Assertions.assertArrayEquals(testData(), (byte[]) result.get1DJavaArray(DataType.BYTE));
Group subgroup = (Group) group.get("subgroup");
Array subgrouparray = (Array) subgroup.get("array");
result = subgrouparray.read(useParallel);
Assertions.assertArrayEquals(testData(), (byte[]) result.get1DJavaArray(ucar.ma2.DataType.BYTE));
Attributes attrs = group.metadata().attributes();
Assertions.assertNotNull(attrs);
Assertions.assertEquals("value", attrs.getString("some"));
}
dev.zarr.zarrjava.v2.Group writeTestGroupV2(StoreHandle storeHandle, boolean useParallel) throws ZarrException, IOException {
dev.zarr.zarrjava.v2.Group group = dev.zarr.zarrjava.v2.Group.create(storeHandle);
dev.zarr.zarrjava.v2.Array array = group.createArray("array", b -> b
.withShape(1024, 1024)
.withDataType(dev.zarr.zarrjava.v2.DataType.UINT8)
.withChunks(512, 512)
);
array.write(ucar.ma2.Array.factory(DataType.BYTE, new int[]{1024, 1024}, testData()), useParallel);
group.createGroup("subgroup");
group.setAttributes(new Attributes().set("some", "value"));
return group;
}
void assertIsTestGroupV2(Group group, boolean useParallel) throws ZarrException, IOException {
Stream<Node> nodes = group.list();
Assertions.assertEquals(2, nodes.count());
Array array = (Array) group.get("array");
Assertions.assertNotNull(array);
ucar.ma2.Array result = array.read(useParallel);
Assertions.assertArrayEquals(testData(), (byte[]) result.get1DJavaArray(DataType.BYTE));
Attributes attrs = group.metadata().attributes();
Assertions.assertNotNull(attrs);
Assertions.assertEquals("value", attrs.getString("some"));
}
}