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_32; 20 21 import com.google.common.hash.HashTestUtils.HashFn; 22 23 import junit.framework.TestCase; 24 25 /** 26 * Tests for {@link Murmur3_32HashFunction}. 27 */ 28 public class Murmur3Hash32Test extends TestCase { 29 public void testKnownIntegerInputs() { 30 assertHash(593689054, murmur3_32().hashInt(0)); 31 assertHash(-189366624, murmur3_32().hashInt(-42)); 32 assertHash(-1134849565, murmur3_32().hashInt(42)); 33 assertHash(-1718298732, murmur3_32().hashInt(Integer.MIN_VALUE)); 34 assertHash(-1653689534, murmur3_32().hashInt(Integer.MAX_VALUE)); 35 } 36 37 public void testKnownLongInputs() { 38 assertHash(1669671676, murmur3_32().hashLong(0L)); 39 assertHash(-846261623, murmur3_32().hashLong(-42L)); 40 assertHash(1871679806, murmur3_32().hashLong(42L)); 41 assertHash(1366273829, murmur3_32().hashLong(Long.MIN_VALUE)); 42 assertHash(-2106506049, murmur3_32().hashLong(Long.MAX_VALUE)); 43 } 44 45 public void testKnownStringInputs() { 46 assertHash(0, murmur3_32().hashUnencodedChars("")); 47 assertHash(679745764, murmur3_32().hashUnencodedChars("k")); 48 assertHash(1510782915, murmur3_32().hashUnencodedChars("hell")); 49 assertHash(-675079799, murmur3_32().hashUnencodedChars("hello")); 50 assertHash(1935035788, murmur3_32().hashUnencodedChars("http://www.google.com/")); 51 assertHash(-528633700, 52 murmur3_32().hashUnencodedChars("The quick brown fox jumps over the lazy dog")); 53 } 54 55 private static void assertHash(int expected, HashCode actual) { 56 assertEquals(HashCode.fromInt(expected), actual); 57 } 58 59 public void testParanoid() { 60 HashFn hf = new HashFn() { 61 @Override public byte[] hash(byte[] input, int seed) { 62 Hasher hasher = murmur3_32(seed).newHasher(); 63 Funnels.byteArrayFunnel().funnel(input, hasher); 64 return hasher.hash().asBytes(); 65 } 66 }; 67 // Murmur3A, MurmurHash3 for x86, 32-bit (MurmurHash3_x86_32) 68 // http://code.google.com/p/smhasher/source/browse/trunk/main.cpp 69 HashTestUtils.verifyHashFunction(hf, 32, 0xB0F57EE3); 70 } 71 72 public void testInvariants() { 73 HashTestUtils.assertInvariants(murmur3_32()); 74 } 75 } 76