@@ -790,6 +790,83 @@ public void testResizeArrayShrink() throws IOException, ZarrException {
790790 Assertions .assertArrayEquals (expectedData , (int []) data .get1DJavaArray (ma2DataType ));
791791 }
792792
793+ @ Test
794+ public void testResizeArrayShrinkWithChunkCleanup () throws IOException , ZarrException {
795+ int [] testData = new int [10 * 10 ];
796+ Arrays .setAll (testData , p -> p );
797+
798+ StoreHandle storeHandle = new FilesystemStore (TESTOUTPUT ).resolve ("testResizeArrayShrinkWithChunkCleanupV3" );
799+ ArrayMetadata arrayMetadata = Array .metadataBuilder ()
800+ .withShape (10 , 10 )
801+ .withDataType (DataType .UINT32 )
802+ .withChunkShape (5 , 5 )
803+ .withFillValue (99 )
804+ .build ();
805+ ucar .ma2 .DataType ma2DataType = arrayMetadata .dataType .getMA2DataType ();
806+ Array array = Array .create (storeHandle , arrayMetadata );
807+ array .write (new long []{0 , 0 }, ucar .ma2 .Array .factory (ma2DataType , new int []{10 , 10 }, testData ));
808+
809+ // Verify all 4 chunks exist before resize (v3 default encoding has "c" prefix)
810+ Assertions .assertTrue (storeHandle .resolve ("c" , "0" , "0" ).exists ());
811+ Assertions .assertTrue (storeHandle .resolve ("c" , "0" , "1" ).exists ());
812+ Assertions .assertTrue (storeHandle .resolve ("c" , "1" , "0" ).exists ());
813+ Assertions .assertTrue (storeHandle .resolve ("c" , "1" , "1" ).exists ());
814+
815+ // Resize with chunk cleanup (resizeMetadataOnly=false)
816+ array = array .resize (new long []{5 , 5 }, false );
817+ Assertions .assertArrayEquals (new int []{5 , 5 }, array .read ().getShape ());
818+
819+ // Verify only chunk (0,0) still exists
820+ Assertions .assertTrue (storeHandle .resolve ("c" , "0" , "0" ).exists ());
821+ Assertions .assertFalse (storeHandle .resolve ("c" , "0" , "1" ).exists ());
822+ Assertions .assertFalse (storeHandle .resolve ("c" , "1" , "0" ).exists ());
823+ Assertions .assertFalse (storeHandle .resolve ("c" , "1" , "1" ).exists ());
824+
825+ ucar .ma2 .Array data = array .read ();
826+ int [] expectedData = new int [5 * 5 ];
827+ for (int i = 0 ; i < 5 ; i ++) {
828+ for (int j = 0 ; j < 5 ; j ++) {
829+ expectedData [i * 5 + j ] = testData [i * 10 + j ];
830+ }
831+ }
832+ Assertions .assertArrayEquals (expectedData , (int []) data .get1DJavaArray (ma2DataType ));
833+ }
834+
835+ @ Test
836+ public void testResizeArrayShrinkWithBoundaryTrimming () throws IOException , ZarrException {
837+ int [] testData = new int [10 * 10 ];
838+ Arrays .setAll (testData , p -> p );
839+
840+ StoreHandle storeHandle = new FilesystemStore (TESTOUTPUT ).resolve ("testResizeArrayShrinkWithBoundaryTrimmingV3" );
841+ ArrayMetadata arrayMetadata = Array .metadataBuilder ()
842+ .withShape (10 , 10 )
843+ .withDataType (DataType .UINT32 )
844+ .withChunkShape (5 , 5 )
845+ .withFillValue (99 )
846+ .build ();
847+ ucar .ma2 .DataType ma2DataType = arrayMetadata .dataType .getMA2DataType ();
848+ Array array = Array .create (storeHandle , arrayMetadata );
849+ array .write (new long []{0 , 0 }, ucar .ma2 .Array .factory (ma2DataType , new int []{10 , 10 }, testData ));
850+
851+ // Resize to 7x7 (crosses chunk boundary, should trim boundary chunks)
852+ array = array .resize (new long []{7 , 7 }, false );
853+ Assertions .assertArrayEquals (new int []{7 , 7 }, array .read ().getShape ());
854+
855+ // Verify chunks (0,0), (0,1), (1,0), (1,1) still exist (boundary trimmed, not deleted)
856+ Assertions .assertTrue (storeHandle .resolve ("c" , "0" , "0" ).exists ());
857+ Assertions .assertTrue (storeHandle .resolve ("c" , "0" , "1" ).exists ());
858+ Assertions .assertTrue (storeHandle .resolve ("c" , "1" , "0" ).exists ());
859+ Assertions .assertTrue (storeHandle .resolve ("c" , "1" , "1" ).exists ());
860+
861+ // Now resize to expand again and check that trimmed area has fill value
862+ array = array .resize (new long []{10 , 10 }, true );
863+ ucar .ma2 .Array data = array .read (new long []{7 , 0 }, new int []{3 , 10 });
864+ // All values in rows 7-9 should be fill value (99)
865+ int [] expectedFillData = new int [3 * 10 ];
866+ Arrays .fill (expectedFillData , 99 );
867+ Assertions .assertArrayEquals (expectedFillData , (int []) data .get1DJavaArray (ma2DataType ));
868+ }
869+
793870 @ Test
794871 public void testGroupAttributes () throws IOException , ZarrException {
795872 StoreHandle storeHandle = new FilesystemStore (TESTOUTPUT ).resolve ("testGroupAttributesV3" );
0 commit comments