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
17package com.google.common.hash;
18
19import static com.google.common.hash.Hashing.murmur3_128;
20
21import com.google.common.base.Charsets;
22import com.google.common.hash.HashTestUtils.HashFn;
23
24import junit.framework.TestCase;
25
26import java.nio.ByteBuffer;
27import java.nio.ByteOrder;
28
29/**
30 * Tests for {@link Murmur3_128HashFunction}.
31 */
32public class Murmur3Hash128Test extends TestCase {
33  public void testKnownValues() {
34    assertHash(0, 0x629942693e10f867L, 0x92db0b82baeb5347L, "hell");
35    assertHash(1, 0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello");
36    assertHash(2, 0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello ");
37    assertHash(3, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L, "hello w");
38    assertHash(4, 0x79f6305a386c572cL, 0x46305aed3483b94eL, "hello wo");
39    assertHash(5, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL, "hello wor");
40    assertHash(0, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L,
41        "The quick brown fox jumps over the lazy dog");
42    assertHash(0, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL,
43        "The quick brown fox jumps over the lazy cog");
44
45    // Known output from Python smhasher
46    HashCode foxHash =
47        murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8);
48    assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.toString());
49  }
50
51  private static void assertHash(int seed, long expected1, long expected2, String stringInput) {
52    HashCode expected = toHashCode(expected1, expected2);
53    byte[] input = HashTestUtils.ascii(stringInput);
54    assertEquals(expected, murmur3_128(seed).hashBytes(input));
55    assertEquals(expected, murmur3_128(seed).newHasher().putBytes(input).hash());
56  }
57
58  /**
59   * Returns a {@link HashCode} for a sequence of longs, in big-endian order.
60   */
61  private static HashCode toHashCode(long... longs) {
62    ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN);
63    for (long x : longs) {
64      bb.putLong(x);
65    }
66    return HashCode.fromBytes(bb.array());
67  }
68
69  public void testParanoid() {
70    HashFn hf = new HashFn() {
71      @Override public byte[] hash(byte[] input, int seed) {
72        Hasher hasher = murmur3_128(seed).newHasher();
73        Funnels.byteArrayFunnel().funnel(input, hasher);
74        return hasher.hash().asBytes();
75      }
76    };
77    // Murmur3F, MurmurHash3 for x64, 128-bit (MurmurHash3_x64_128)
78    // From http://code.google.com/p/smhasher/source/browse/trunk/main.cpp
79    HashTestUtils.verifyHashFunction(hf, 128, 0x6384BA69);
80  }
81
82  public void testInvariants() {
83    HashTestUtils.assertInvariants(murmur3_128());
84  }
85}
86