Skip to content

Commit 486d8a8

Browse files
committed
MurmurHash3: Added random block sizes to incremental hash test.
1 parent 64f1b4d commit 486d8a8

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/test/java/org/apache/commons/codec/digest/MurmurHash3Test.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,10 @@ public void testIncrementalHash32() {
747747
assertIncrementalHash32(bytes, seed, 4, 3);
748748
// Complete blocks
749749
assertIncrementalHash32(bytes, seed, 4, 16, 64);
750+
// Some random blocks
751+
for (int i = 0; i < 10; i++) {
752+
assertIncrementalHash32x86(bytes, seed, createRandomBlocks(bytes.length));
753+
}
750754
}
751755
}
752756

@@ -801,6 +805,10 @@ public void testIncrementalHash32x86() {
801805
assertIncrementalHash32x86(bytes, seed, 4, 3);
802806
// Complete blocks
803807
assertIncrementalHash32x86(bytes, seed, 4, 16, 64);
808+
// Some random blocks
809+
for (int i = 0; i < 10; i++) {
810+
assertIncrementalHash32x86(bytes, seed, createRandomBlocks(bytes.length));
811+
}
804812
}
805813
}
806814

@@ -829,4 +837,23 @@ private static void assertIncrementalHash32x86(byte[] bytes, int seed, int... bl
829837
Assert.assertEquals("Hashes differ after no additional data", h1, inc.end());
830838
}
831839
}
840+
841+
/**
842+
* Creates the random blocks of data to process up to max length.
843+
*
844+
* @param maxLength the max length
845+
* @return the blocks
846+
*/
847+
private static int[] createRandomBlocks(int maxLength) {
848+
int[] blocks = new int[20];
849+
int count = 0;
850+
int length = 0;
851+
while (count < blocks.length && length < maxLength) {
852+
// range of 1 to 8 for up to two 4 byte blocks
853+
final int size = ThreadLocalRandom.current().nextInt(1, 9);
854+
blocks[count++] = size;
855+
length += size;
856+
}
857+
return Arrays.copyOf(blocks, count);
858+
}
832859
}

0 commit comments

Comments
 (0)