5656import java .nio .file .NoSuchFileException ;
5757import java .util .*;
5858import java .util .function .Function ;
59+ import java .util .stream .IntStream ;
5960
61+ import static org .junit .Assert .assertArrayEquals ;
6062import static org .junit .Assert .assertEquals ;
6163import static org .junit .Assert .assertTrue ;
6264
@@ -128,6 +130,11 @@ public void removeTempWriters() {
128130
129131 private DatasetAttributes getTestAttributes (long [] dimensions , int [] shardSize , int [] blockSize ) {
130132
133+ return getTestAttributes (DataType .UINT8 , dimensions , shardSize , blockSize );
134+ }
135+
136+ private DatasetAttributes getTestAttributes (DataType dataType , long [] dimensions , int [] shardSize , int [] blockSize ) {
137+
131138 DefaultShardCodecInfo blockCodec = new DefaultShardCodecInfo (
132139 blockSize ,
133140 new N5BlockCodecInfo (),
@@ -139,7 +146,7 @@ private DatasetAttributes getTestAttributes(long[] dimensions, int[] shardSize,
139146 return new DatasetAttributes (
140147 dimensions ,
141148 shardSize ,
142- DataType . UINT8 ,
149+ dataType ,
143150 blockCodec );
144151 }
145152
@@ -412,6 +419,51 @@ public void writeReadBlockTest() {
412419 }
413420 }
414421
422+ @ Test
423+ public void writeReadShardTest () {
424+
425+ try ( final N5Writer n5 = tempN5Factory .createTempN5Writer () ) {
426+
427+ final int [] shardSize = new int [] {4 ,4 };
428+ final int shardN = 16 ;
429+
430+ final int [] blockSize = new int [] {2 ,2 };
431+ final int blockN = 4 ;
432+
433+ final String dataset = "writeReadShard" ;
434+ DatasetAttributes attrs = getTestAttributes (DataType .INT32 , new long []{8 , 8 }, shardSize , blockSize );
435+
436+ final int [] shardData = range (shardN );
437+ IntArrayDataBlock shard = new IntArrayDataBlock (shardSize , new long []{0 , 0 }, shardData );
438+
439+ n5 .writeShard (dataset , attrs , shard );
440+ DataBlock <int []> readShard = n5 .readShard (dataset , attrs , 0 , 0 );
441+ assertArrayEquals (shardData , readShard .getData ());
442+
443+
444+ /**
445+ * The 4x4 shard at (0,0)
446+ * and the 2x2 blocks it contains
447+ *
448+ *
449+ * 0 1 | 2 3
450+ * 4 5 | 6 7
451+ * ----------------
452+ * 8 9 | 10 11
453+ * 12 13 | 14 15
454+ */
455+
456+ assertArrayEquals (new int []{0 , 1 , 4 , 5 }, (int [])n5 .readBlock (dataset , attrs , 0 , 0 ).getData ());
457+ assertArrayEquals (new int []{2 , 3 , 6 , 7 }, (int [])n5 .readBlock (dataset , attrs , 1 , 0 ).getData ());
458+ assertArrayEquals (new int []{8 , 9 , 12 , 13 }, (int [])n5 .readBlock (dataset , attrs , 0 , 1 ).getData ());
459+ assertArrayEquals (new int []{10 , 11 , 14 , 15 }, (int [])n5 .readBlock (dataset , attrs , 1 , 1 ).getData ());
460+ }
461+ }
462+
463+ private int [] range (int N ) {
464+ return IntStream .range (0 , N ).toArray ();
465+ }
466+
415467 /**
416468 * Checks how many read calls to the backend are performed for a particular readBlocks
417469 * call. At this time (Nov 4 2025), one read for the index, and one read per block are performed.
@@ -484,7 +536,7 @@ public void shardExistsTest() {
484536
485537 final String dataset = "shardExists" ;
486538 writer .remove (dataset );
487- writer .createDataset (dataset , datasetAttributes );
539+ DatasetAttributes attrs = writer .createDataset (dataset , datasetAttributes );
488540
489541 final int [] blockSize = datasetAttributes .getBlockSize ();
490542 final int numElements = blockSize [0 ] * blockSize [1 ];
@@ -497,7 +549,7 @@ public void shardExistsTest() {
497549 /* write blocks to shards (0,0), (1,0), and (2,2) */
498550 writer .writeBlocks (
499551 dataset ,
500- datasetAttributes ,
552+ attrs ,
501553 new ByteArrayDataBlock (blockSize , new long []{0 , 0 }, data ), /* shard (0, 0) */
502554 new ByteArrayDataBlock (blockSize , new long []{4 , 0 }, data ), /* shard (1, 0) */
503555 new ByteArrayDataBlock (blockSize , new long []{11 , 11 }, data ) /* shard (2, 2) */
@@ -507,7 +559,7 @@ public void shardExistsTest() {
507559
508560 Function <long [], Boolean > assertShardExistsTracking = (gridPosition ) -> {
509561 trackingWriter .resetAllTracking ();
510- final Boolean exists = writer .shardExists (dataset , datasetAttributes , gridPosition );
562+ final Boolean exists = writer .shardExists (dataset , attrs , gridPosition );
511563 assertEquals ("isFileCheck incremented" , 1 , trackingWriter .getNumIsFileCalls ());
512564 assertEquals ("No Bytes Read" , 0 , trackingWriter .getTotalBytesRead ());
513565 return exists ;
0 commit comments