public void testHashing() { for (String stringToTest : INPUTS) { for (String algorithmToTest : ALGORITHMS.keySet()) { assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest); } } }
private static void assertShortcutsAreEquivalent(HashFunction hashFunction, int trials) { Random random = new Random(42085L); for (int i = 0; i < trials; i++) { assertHashBytesEquivalence(hashFunction, random); assertHashByteBufferEquivalence(hashFunction, random); assertHashIntEquivalence(hashFunction, random); assertHashLongEquivalence(hashFunction, random); assertHashStringEquivalence(hashFunction, random); assertHashStringWithSurrogatesEquivalence(hashFunction, random); } }
static void assertHashByteBufferInvariants(HashFunction hashFunction) { assertHashByteBufferMatchesBytes(hashFunction); assertHashByteBufferExhaustsBuffer(hashFunction); assertHashByteBufferPreservesByteOrder(hashFunction); assertHasherByteBufferPreservesByteOrder(hashFunction); }
/** * Checks that a Hasher returns the same HashCode when given the same input, and also that the * collision rate looks sane. */ static void assertInvariants(HashFunction hashFunction) { int objects = 100; Set<HashCode> hashcodes = Sets.newHashSetWithExpectedSize(objects); Random random = new Random(314159); for (int i = 0; i < objects; i++) { int value = random.nextInt(); HashCode hashcode1 = hashFunction.hashInt(value); HashCode hashcode2 = hashFunction.hashInt(value); Assert.assertEquals(hashcode1, hashcode2); // idempotent Assert.assertEquals(hashFunction.bits(), hashcode1.bits()); Assert.assertEquals(hashFunction.bits(), hashcode1.asBytes().length * 8); hashcodes.add(hashcode1); } Assert.assertTrue(hashcodes.size() > objects * 0.95); // quite relaxed test assertHashBytesThrowsCorrectExceptions(hashFunction); assertIndependentHashers(hashFunction); assertShortcutsAreEquivalent(hashFunction, 512); }
/** * Checks that a Hasher returns the same HashCode when given the same input, and also * that the collision rate looks sane. */ static void assertInvariants(HashFunction hashFunction) { int objects = 100; Set<HashCode> hashcodes = Sets.newHashSetWithExpectedSize(objects); Random random = new Random(314159); for (int i = 0; i < objects; i++) { int value = random.nextInt(); HashCode hashcode1 = hashFunction.hashInt(value); HashCode hashcode2 = hashFunction.hashInt(value); Assert.assertEquals(hashcode1, hashcode2); // idempotent Assert.assertEquals(hashFunction.bits(), hashcode1.bits()); Assert.assertEquals(hashFunction.bits(), hashcode1.asBytes().length * 8); hashcodes.add(hashcode1); } Assert.assertTrue(hashcodes.size() > objects * 0.95); // quite relaxed test assertHashBytesThrowsCorrectExceptions(hashFunction); assertIndependentHashers(hashFunction); assertShortcutsAreEquivalent(hashFunction, 512); }
private static void assertShortcutsAreEquivalent(HashFunction hashFunction, int trials) { Random random = new Random(42085L); for (int i = 0; i < trials; i++) { assertHashBytesEquivalence(hashFunction, random); assertHashByteBufferEquivalence(hashFunction, random); assertHashIntEquivalence(hashFunction, random); assertHashLongEquivalence(hashFunction, random); assertHashStringEquivalence(hashFunction, random); assertHashStringWithSurrogatesEquivalence(hashFunction, random); } }
static void assertHashByteBufferInvariants(HashFunction hashFunction) { assertHashByteBufferMatchesBytes(hashFunction); assertHashByteBufferExhaustsBuffer(hashFunction); assertHashByteBufferPreservesByteOrder(hashFunction); assertHasherByteBufferPreservesByteOrder(hashFunction); }
private static void assertChecksum(ImmutableSupplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); Checksum checksum = supplier.get(); checksum.update(bytes, 0, bytes.length); long value = checksum.getValue(); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(toString, func.toString()); assertEquals(value, func.hashBytes(bytes).padToLong()); }
private static void assertHash32( int expected, ImmutableSupplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(expected, func.hashBytes(bytes).asInt()); assertEquals(toString, func.toString()); } }
private static void assertHash(int seed, long expected1, long expected2, String stringInput) { HashCode expected = toHashCode(expected1, expected2); byte[] input = HashTestUtils.ascii(stringInput); assertEquals(expected, murmur3_128(seed).hashBytes(input)); assertEquals(expected, murmur3_128(seed).newHasher().putBytes(input).hash()); }
public void testHashing() throws Exception { for (String stringToTest : INPUTS) { for (Table.Cell<String, SecretKey, HashFunction> cell : ALGORITHMS.cellSet()) { String algorithm = cell.getRowKey(); SecretKey key = cell.getColumnKey(); HashFunction hashFunc = cell.getValue(); assertMacHashing(HashTestUtils.ascii(stringToTest), algorithm, key, hashFunc); } } }
public void testHashing() { for (String stringToTest : INPUTS) { for (String algorithmToTest : ALGORITHMS.keySet()) { assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest); } } }
private static void assertChecksum(Supplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); Checksum checksum = supplier.get(); checksum.update(bytes, 0, bytes.length); long value = checksum.getValue(); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(toString, func.toString()); assertEquals(value, func.hashBytes(bytes).padToLong()); }
private static void assertHash32(int expected, Supplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(expected, func.hashBytes(bytes).asInt()); assertEquals(toString, func.toString()); } }
private static void assertHash(int seed, long expected1, long expected2, String stringInput) { HashCode expected = toHashCode(expected1, expected2); byte[] input = HashTestUtils.ascii(stringInput); assertEquals(expected, murmur3_128(seed).hashBytes(input)); assertEquals(expected, murmur3_128(seed).newHasher().putBytes(input).hash()); }
public void testHashing() throws Exception { for (String stringToTest : INPUTS) { for (Table.Cell<String, SecretKey, HashFunction> cell : ALGORITHMS.cellSet()) { String algorithm = cell.getRowKey(); SecretKey key = cell.getColumnKey(); HashFunction hashFunc = cell.getValue(); assertMacHashing(HashTestUtils.ascii(stringToTest), algorithm, key, hashFunc); } } }