1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.hash; 18 19 import static com.google.common.hash.Hashing.murmur3_128; 20 21 import com.google.common.base.Charsets; 22 import com.google.common.hash.HashTestUtils.HashFn; 23 import java.nio.ByteBuffer; 24 import java.nio.ByteOrder; 25 import junit.framework.TestCase; 26 27 /** Tests for {@link Murmur3_128HashFunction}. */ 28 public class Murmur3Hash128Test extends TestCase { testKnownValues()29 public void testKnownValues() { 30 assertHash(0, 0x629942693e10f867L, 0x92db0b82baeb5347L, "hell"); 31 assertHash(1, 0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello"); 32 assertHash(2, 0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello "); 33 assertHash(3, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L, "hello w"); 34 assertHash(4, 0x79f6305a386c572cL, 0x46305aed3483b94eL, "hello wo"); 35 assertHash(5, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL, "hello wor"); 36 assertHash( 37 0, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L, "The quick brown fox jumps over the lazy dog"); 38 assertHash( 39 0, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL, "The quick brown fox jumps over the lazy cog"); 40 41 // Known output from Python smhasher 42 HashCode foxHash = 43 murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8); 44 assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.toString()); 45 } 46 assertHash(int seed, long expected1, long expected2, String stringInput)47 private static void assertHash(int seed, long expected1, long expected2, String stringInput) { 48 HashCode expected = toHashCode(expected1, expected2); 49 byte[] input = HashTestUtils.ascii(stringInput); 50 assertEquals(expected, murmur3_128(seed).hashBytes(input)); 51 assertEquals(expected, murmur3_128(seed).newHasher().putBytes(input).hash()); 52 } 53 54 /** Returns a {@link HashCode} for a sequence of longs, in big-endian order. */ toHashCode(long... longs)55 private static HashCode toHashCode(long... longs) { 56 ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN); 57 for (long x : longs) { 58 bb.putLong(x); 59 } 60 return HashCode.fromBytes(bb.array()); 61 } 62 testParanoid()63 public void testParanoid() { 64 HashFn hf = 65 new HashFn() { 66 @Override 67 public byte[] hash(byte[] input, int seed) { 68 Hasher hasher = murmur3_128(seed).newHasher(); 69 Funnels.byteArrayFunnel().funnel(input, hasher); 70 return hasher.hash().asBytes(); 71 } 72 }; 73 // Murmur3F, MurmurHash3 for x64, 128-bit (MurmurHash3_x64_128) 74 // From http://code.google.com/p/smhasher/source/browse/trunk/main.cpp 75 HashTestUtils.verifyHashFunction(hf, 128, 0x6384BA69); 76 } 77 testInvariants()78 public void testInvariants() { 79 HashTestUtils.assertInvariants(murmur3_128()); 80 } 81 } 82