diff options
Diffstat (limited to 'guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java')
-rw-r--r-- | guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java | 71 |
1 files changed, 28 insertions, 43 deletions
diff --git a/guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java b/guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java index ab319fb..4a4eea7 100644 --- a/guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java +++ b/guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java @@ -16,55 +16,44 @@ package com.google.common.hash; +import static com.google.common.hash.HashTestUtils.ascii; +import static com.google.common.hash.HashTestUtils.toBytes; import static com.google.common.hash.Hashing.murmur3_128; +import static java.nio.ByteOrder.LITTLE_ENDIAN; -import com.google.common.base.Charsets; import com.google.common.hash.Funnels; import com.google.common.hash.HashTestUtils.HashFn; import junit.framework.TestCase; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; +import java.util.Arrays; /** - * Tests for {@link Murmur3_128HashFunction}. + * Tests for Murmur3Hash128. */ public class Murmur3Hash128Test extends TestCase { - public void testKnownValues() { - assertHash(0, 0x629942693e10f867L, 0x92db0b82baeb5347L, "hell"); - assertHash(1, 0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello"); - assertHash(2, 0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello "); - assertHash(3, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L, "hello w"); - assertHash(4, 0x79f6305a386c572cL, 0x46305aed3483b94eL, "hello wo"); - assertHash(5, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL, "hello wor"); - assertHash(0, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L, - "The quick brown fox jumps over the lazy dog"); - assertHash(0, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL, - "The quick brown fox jumps over the lazy cog"); - - // Known output from Python smhasher - HashCode foxHash = - murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8); - assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.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 testCompatibilityWithCPlusPlus() { + assertHash(0, toBytes(LITTLE_ENDIAN, 0x629942693e10f867L, 0x92db0b82baeb5347L), + ascii("hell")); + assertHash(1, toBytes(LITTLE_ENDIAN, 0xa78ddff5adae8d10L, 0x128900ef20900135L), + ascii("hello")); + assertHash(2, toBytes(LITTLE_ENDIAN, 0x8a486b23f422e826L, 0xf962a2c58947765fL), + ascii("hello ")); + assertHash(3, toBytes(LITTLE_ENDIAN, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L), + ascii("hello w")); + assertHash(4, toBytes(LITTLE_ENDIAN, 0x79f6305a386c572cL, 0x46305aed3483b94eL), + ascii("hello wo")); + assertHash(5, toBytes(LITTLE_ENDIAN, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL), + ascii("hello wor")); + assertHash(0, toBytes(LITTLE_ENDIAN, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L), + ascii("The quick brown fox jumps over the lazy dog")); + assertHash(0, toBytes(LITTLE_ENDIAN, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL), + ascii("The quick brown fox jumps over the lazy cog")); } - - /** - * Returns a {@link HashCode} for a sequence of longs, in big-endian order. - */ - private static HashCode toHashCode(long... longs) { - ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN); - for (long x : longs) { - bb.putLong(x); - } - return HashCodes.fromBytes(bb.array()); + + private static void assertHash(int seed, byte[] expectedHash, byte[] input) { + byte[] hash = murmur3_128(seed).newHasher().putBytes(input).hash().asBytes(); + assertTrue(Arrays.equals(expectedHash, hash)); } public void testParanoid() { @@ -75,12 +64,8 @@ public class Murmur3Hash128Test extends TestCase { return hasher.hash().asBytes(); } }; - // Murmur3F, MurmurHash3 for x64, 128-bit (MurmurHash3_x64_128) - // From http://code.google.com/p/smhasher/source/browse/trunk/main.cpp + // the magic number comes from: + // http://code.google.com/p/smhasher/source/browse/trunk/main.cpp, #74 HashTestUtils.verifyHashFunction(hf, 128, 0x6384BA69); } - - public void testInvariants() { - HashTestUtils.assertInvariants(murmur3_128()); - } } |