Skip to content

Commit

Permalink
Test the new methods in BufferedChecksumInput
Browse files Browse the repository at this point in the history
  • Loading branch information
jfboeuf committed Nov 12, 2024
1 parent d696059 commit dd096a8
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions lucene/core/src/test/org/apache/lucene/store/TestBufferedChecksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
*/
package org.apache.lucene.store;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.LongBuffer;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.BitUtil;

public class TestBufferedChecksum extends LuceneTestCase {

Expand Down Expand Up @@ -63,4 +67,104 @@ 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);
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) {
for (int i = 0; i < input.length / Short.BYTES; i++) {
checksum.updateShort((short) BitUtil.VH_LE_SHORT.get(input, i * Short.BYTES));
}
checkChecksumValueAndReset(expected, checksum);
}

private void updateByInts(long expected, BufferedChecksum checksum, byte[] input) {
for (int i = 0; i < input.length / Integer.BYTES; i++) {
checksum.updateInt((int) BitUtil.VH_LE_INT.get(input, i * Integer.BYTES));
}
checkChecksumValueAndReset(expected, checksum);
}

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);
checksum.updateLongs(longInput, len0, longInput.length - len0);
checkChecksumValueAndReset(expected, checksum);
}
}

private void checkChecksumValueAndReset(long expected, Checksum checksum) {
assertEquals(expected, checksum.getValue());
checksum.reset();
}
}

0 comments on commit dd096a8

Please sign in to comment.