jpountz commented on code in PR #13989:
URL: https://github.com/apache/lucene/pull/13989#discussion_r1854638074


##########
lucene/core/src/test/org/apache/lucene/store/TestBufferedChecksum.java:
##########
@@ -63,4 +67,127 @@ public void testRandom() {
     }
     assertEquals(c1.getValue(), c2.getValue());
   }
+
+  public void testDifferentInputTypes() {
+    Checksum crc = new CRC32();
+    BufferedChecksum buffered = new BufferedChecksum(new CRC32());
+    int iterations = atLeast(1000);
+    for (int i = 0; i < iterations; i++) {
+      byte[] input = new byte[4096];
+      random().nextBytes(input);
+      crc.update(input);
+      final long checksum = crc.getValue();
+      crc.reset();
+      updateByShorts(checksum, buffered, input);
+      updateByInts(checksum, buffered, input);
+      updateByLongs(checksum, buffered, input);
+      updateByChunkOfBytes(checksum, buffered, input);
+      updateByChunkOfLongs(checksum, buffered, input);
+    }
+  }
+
+  private void updateByChunkOfBytes(long expected, BufferedChecksum checksum, 
byte[] input) {
+    for (int i = 0; i < input.length; i++) {
+      checksum.update(input[i]);
+    }
+    checkChecksumValueAndReset(expected, checksum);
+
+    checksum.update(input);
+    checkChecksumValueAndReset(expected, checksum);
+
+    int iterations = atLeast(10);
+    for (int ite = 0; ite < iterations; ite++) {
+      int len0 = random().nextInt(input.length / 2);
+      checksum.update(input, 0, len0);
+      checksum.update(input, len0, input.length - len0);
+      checkChecksumValueAndReset(expected, checksum);
+
+      checksum.update(input, 0, len0);
+      int len1 = random().nextInt(input.length / 4);
+      for (int i = 0; i < len1; i++) {
+        checksum.update(input[len0 + i]);
+      }
+      checksum.update(input, len0 + len1, input.length - len1 - len0);
+      checkChecksumValueAndReset(expected, checksum);
+    }
+  }
+
+  private void updateByShorts(long expected, BufferedChecksum checksum, byte[] 
input) {
+    int ix = shiftArray(checksum, input);
+    while (ix < input.length - Short.BYTES) {
+      checksum.updateShort((short) BitUtil.VH_LE_SHORT.get(input, ix));
+      ix += Short.BYTES;
+    }
+    checksum.update(input, ix, input.length - ix);
+    checkChecksumValueAndReset(expected, checksum);
+  }
+
+  private void updateByInts(long expected, BufferedChecksum checksum, byte[] 
input) {
+    int ix = shiftArray(checksum, input);
+    while (ix < input.length - Integer.BYTES) {
+      checksum.updateInt((int) BitUtil.VH_LE_INT.get(input, ix));
+      ix += Integer.BYTES;
+    }
+    checksum.update(input, ix, input.length - ix);
+    checkChecksumValueAndReset(expected, checksum);
+  }
+
+  private void updateByLongs(long expected, BufferedChecksum checksum, byte[] 
input) {
+    int ix = shiftArray(checksum, input);
+    while (ix < input.length - Long.BYTES) {
+      checksum.updateLong((long) BitUtil.VH_LE_LONG.get(input, ix));
+      ix += Long.BYTES;
+    }
+    checksum.update(input, ix, input.length - ix);
+    checkChecksumValueAndReset(expected, checksum);
+  }
+
+  private static int shiftArray(BufferedChecksum checksum, byte[] input) {
+    int ix = random().nextInt(input.length / 4);
+    checksum.update(input, 0, ix);
+    return ix;
+  }
+
+  private void updateByChunkOfLongs(long expected, BufferedChecksum checksum, 
byte[] input) {
+    LongBuffer b = 
ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN).asLongBuffer();
+    long[] longInput = new long[input.length / Long.BYTES];
+    b.get(longInput);
+    for (int i = 0; i < longInput.length; i++) {
+      checksum.updateLong(longInput[i]);
+    }
+    checkChecksumValueAndReset(expected, checksum);
+
+    checksum.updateLongs(longInput, 0, longInput.length);
+    checkChecksumValueAndReset(expected, checksum);
+
+    int iterations = atLeast(10);
+    for (int ite = 0; ite < iterations; ite++) {
+      int len0 = random().nextInt(longInput.length / 2);
+      checksum.updateLongs(longInput, 0, len0);
+      checksum.updateLongs(longInput, len0, longInput.length - len0);
+      checkChecksumValueAndReset(expected, checksum);
+
+      checksum.updateLongs(longInput, 0, len0);
+      int len1 = random().nextInt(longInput.length / 4);
+      for (int i = 0; i < len1; i++) {
+        checksum.updateLong(longInput[len0 + i]);
+      }
+      checksum.updateLongs(longInput, len0 + len1, longInput.length - len1 - 
len0);
+      checkChecksumValueAndReset(expected, checksum);
+
+      checksum.updateLongs(longInput, 0, len0);
+      checksum.update(input, len0 * Long.BYTES, input.length - len0 * 
Long.BYTES);
+      checkChecksumValueAndReset(expected, checksum);
+
+      len0 &= ~(Long.BYTES - 1); // truncates to multiple of Long.BYTES
+      checksum.update(input, 0, len0 * Long.BYTES);

Review Comment:
   Can we test with a shift for long[] too?



##########
lucene/core/src/test/org/apache/lucene/store/TestBufferedChecksum.java:
##########
@@ -63,4 +67,127 @@ public void testRandom() {
     }
     assertEquals(c1.getValue(), c2.getValue());
   }
+
+  public void testDifferentInputTypes() {
+    Checksum crc = new CRC32();
+    BufferedChecksum buffered = new BufferedChecksum(new CRC32());
+    int iterations = atLeast(1000);
+    for (int i = 0; i < iterations; i++) {
+      byte[] input = new byte[4096];
+      random().nextBytes(input);
+      crc.update(input);
+      final long checksum = crc.getValue();
+      crc.reset();
+      updateByShorts(checksum, buffered, input);
+      updateByInts(checksum, buffered, input);
+      updateByLongs(checksum, buffered, input);
+      updateByChunkOfBytes(checksum, buffered, input);
+      updateByChunkOfLongs(checksum, buffered, input);
+    }
+  }
+
+  private void updateByChunkOfBytes(long expected, BufferedChecksum checksum, 
byte[] input) {
+    for (int i = 0; i < input.length; i++) {
+      checksum.update(input[i]);
+    }
+    checkChecksumValueAndReset(expected, checksum);
+
+    checksum.update(input);
+    checkChecksumValueAndReset(expected, checksum);
+
+    int iterations = atLeast(10);
+    for (int ite = 0; ite < iterations; ite++) {
+      int len0 = random().nextInt(input.length / 2);
+      checksum.update(input, 0, len0);
+      checksum.update(input, len0, input.length - len0);
+      checkChecksumValueAndReset(expected, checksum);
+
+      checksum.update(input, 0, len0);
+      int len1 = random().nextInt(input.length / 4);
+      for (int i = 0; i < len1; i++) {
+        checksum.update(input[len0 + i]);
+      }
+      checksum.update(input, len0 + len1, input.length - len1 - len0);
+      checkChecksumValueAndReset(expected, checksum);
+    }
+  }
+
+  private void updateByShorts(long expected, BufferedChecksum checksum, byte[] 
input) {
+    int ix = shiftArray(checksum, input);
+    while (ix < input.length - Short.BYTES) {

Review Comment:
   I believe you meant `<=`?
   
   ```suggestion
       while (ix <= input.length - Short.BYTES) {
   ```



-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to