jerqi commented on code in PR #2720:
URL: https://github.com/apache/uniffle/pull/2720#discussion_r2735012207


##########
common/src/test/java/org/apache/uniffle/common/util/ChecksumUtilsTest.java:
##########
@@ -112,4 +115,58 @@ public void crc32ByteBufferTest() throws Exception {
     directOffsetBuffer.put(data);
     assertEquals(expectCrc, ChecksumUtils.getCrc32(directOffsetBuffer, offset, 
length));
   }
+
+  @Test
+  public void crc32ByteBufEmptyReadableBytesShouldReturnZero() {
+    ByteBuf byteBuf = Unpooled.buffer(16);
+    assertEquals(0, byteBuf.readableBytes());
+    assertEquals(0L, ChecksumUtils.getCrc32(byteBuf));
+  }
+
+  @Test
+  public void crc32ByteBufShouldRespectReaderIndexAndNotChangeIt() {
+    byte[] data = new byte[1024];
+    new Random().nextBytes(data);
+    ByteBuf byteBuf = Unpooled.wrappedBuffer(data);
+
+    int readerIndex = 17;
+    byteBuf.readerIndex(readerIndex);
+
+    CRC32 crc32 = new CRC32();
+    crc32.update(data, readerIndex, data.length - readerIndex);
+    long expected = crc32.getValue();
+
+    assertEquals(expected, ChecksumUtils.getCrc32(byteBuf));
+    assertEquals(readerIndex, byteBuf.readerIndex());
+  }
+
+  @Test
+  public void crc32CompositeByteBufShouldIterateOverNioBuffers() {
+    byte[] part1 = new byte[128];
+    byte[] part2 = new byte[256];
+    Random random = new Random();
+    random.nextBytes(part1);
+    random.nextBytes(part2);
+
+    CompositeByteBuf composite = Unpooled.compositeBuffer();
+    composite.addComponent(true, Unpooled.wrappedBuffer(part1));
+    composite.addComponent(true, Unpooled.wrappedBuffer(part2));
+
+    // Ensure this test hits the composite path (nioBufferCount > 1).
+    // Note: CompositeByteBuf.nioBufferCount() depends on 
readerIndex/readableBytes, so check it
+    // here.
+    assertEquals(true, composite.nioBufferCount() > 1);

Review Comment:
   cc @zuston 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to