@Override public byte[] hash(byte[] input, int seed) { Hasher hasher = murmur3_32(seed).newHasher(); Funnels.byteArrayFunnel().funnel(input, hasher); return hasher.hash().asBytes(); } };
@AndroidIncompatible // slow TODO(cpovirk): Maybe just reduce iterations under Android. public void testGoodFastHash() { for (int i = 1; i < 200; i += 17) { HashFunction hasher = Hashing.goodFastHash(i); assertTrue(hasher.bits() >= i); HashTestUtils.assertInvariants(hasher); } }
@Override public byte[] hash(byte[] input, int seed) { Hasher hasher = murmur3_128(seed).newHasher(); Funnels.byteArrayFunnel().funnel(input, hasher); return hasher.hash().asBytes(); } };
public void testHashTwice() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals( "9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.hash(); fail(); } catch (IllegalStateException expected) { } }
private static void assertSip(String input, long expected) { assertEquals(expected, SIP_WITH_KEY.hashString(input, UTF_8).asLong()); assertEquals(expected, SIP_WITH_KEY.newHasher().putString(input, UTF_8).hash().asLong()); assertEquals(expected, SIP_WITHOUT_KEY.hashString(input, UTF_8).asLong()); assertEquals(expected, SIP_WITHOUT_KEY.newHasher().putString(input, UTF_8).hash().asLong()); }
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()); }
private static void assertSip(byte[] input, long expected) { assertEquals(expected, SIP_WITH_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITH_KEY.newHasher().putBytes(input).hash().asLong()); assertEquals(expected, SIP_WITHOUT_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITHOUT_KEY.newHasher().putBytes(input).hash().asLong()); } }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
public void testHashFloatIsStable() { // Just a spot check. Better than nothing. Hasher hasher = HASH_FN.newHasher(); hasher.putFloat(0x01000101f).putFloat(0f); assertEquals(0x49f9d18ee8ae1b28L, hasher.hash().asLong()); hasher = HASH_FN.newHasher(); hasher.putDouble(0x0000000001000101d); assertEquals(0x388ee898bad75cbfL, hasher.hash().asLong()); }
public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectly() throws Exception { byte[] buf = new byte[] {'y', 'a', 'm', 's'}; HashCode expectedHash = Hashing.md5().hashBytes(buf); HashingOutputStream out = new HashingOutputStream(Hashing.md5(), buffer); out.write(buf); assertEquals(expectedHash, out.hash()); }
/** * Verifies that the crc of an array of byte data matches the expected value. * * @param expectedCrc the expected crc value. * @param data the data to run the checksum on. */ private static void assertCrc(int expectedCrc, byte[] data) { int actualCrc = Hashing.crc32c().hashBytes(data).asInt(); assertEquals(expectedCrc, actualCrc); }
public void test15ByteStringFromSipHashPaper() { byte[] message = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; long k0 = 0x0706050403020100L; long k1 = 0x0f0e0d0c0b0a0908L; assertEquals(0xa129ca6149be45e5L, Hashing.sipHash24(k0, k1).hashBytes(message).asLong()); }
public void testKnownStringInputs() { assertHash(0, murmur3_32().hashUnencodedChars("")); assertHash(679745764, murmur3_32().hashUnencodedChars("k")); assertHash(1510782915, murmur3_32().hashUnencodedChars("hell")); assertHash(-675079799, murmur3_32().hashUnencodedChars("hello")); assertHash(1935035788, murmur3_32().hashUnencodedChars("http://www.google.com/")); assertHash( -528633700, murmur3_32().hashUnencodedChars("The quick brown fox jumps over the lazy dog")); }
public void testPutAllWithSelf() { BloomFilter<Integer> bf1 = BloomFilter.create(Funnels.integerFunnel(), 1); try { assertFalse(bf1.isCompatible(bf1)); bf1.putAll(bf1); fail(); } catch (IllegalArgumentException expected) { } }
public void testFailureWhenMoreThan255HashFunctionsAreNeeded() { try { int n = 1000; double p = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000001; BloomFilter.create(Funnels.unencodedCharsFunnel(), n, p); fail(); } catch (IllegalArgumentException expected) { } }
public void testPutAfterHash() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals( "9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.putInt(42); fail(); } catch (IllegalStateException expected) { } }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }