• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static com.google.common.hash.Hashing.murmur3_32_fixed;
21 import static java.nio.charset.StandardCharsets.UTF_16;
22 import static java.nio.charset.StandardCharsets.UTF_16LE;
23 import static java.nio.charset.StandardCharsets.UTF_8;
24 
25 import com.google.common.hash.HashTestUtils.HashFn;
26 import java.nio.charset.Charset;
27 import java.util.Random;
28 import junit.framework.TestCase;
29 
30 /** Tests for {@link Murmur3_32HashFunction}. */
31 public class Murmur3Hash32Test extends TestCase {
testKnownIntegerInputs()32   public void testKnownIntegerInputs() {
33     assertHash(593689054, murmur3_32().hashInt(0));
34     assertHash(-189366624, murmur3_32().hashInt(-42));
35     assertHash(-1134849565, murmur3_32().hashInt(42));
36     assertHash(-1718298732, murmur3_32().hashInt(Integer.MIN_VALUE));
37     assertHash(-1653689534, murmur3_32().hashInt(Integer.MAX_VALUE));
38   }
39 
testKnownLongInputs()40   public void testKnownLongInputs() {
41     assertHash(1669671676, murmur3_32().hashLong(0L));
42     assertHash(-846261623, murmur3_32().hashLong(-42L));
43     assertHash(1871679806, murmur3_32().hashLong(42L));
44     assertHash(1366273829, murmur3_32().hashLong(Long.MIN_VALUE));
45     assertHash(-2106506049, murmur3_32().hashLong(Long.MAX_VALUE));
46   }
47 
testKnownStringInputs()48   public void testKnownStringInputs() {
49     assertHash(0, murmur3_32().hashUnencodedChars(""));
50     assertHash(679745764, murmur3_32().hashUnencodedChars("k"));
51     assertHash(1510782915, murmur3_32().hashUnencodedChars("hell"));
52     assertHash(-675079799, murmur3_32().hashUnencodedChars("hello"));
53     assertHash(1935035788, murmur3_32().hashUnencodedChars("http://www.google.com/"));
54     assertHash(
55         -528633700, murmur3_32().hashUnencodedChars("The quick brown fox jumps over the lazy dog"));
56   }
57 
58   @SuppressWarnings("deprecation")
testKnownEncodedStringInputs()59   public void testKnownEncodedStringInputs() {
60     assertStringHash(0, "", UTF_8);
61     assertStringHash(0xcfbda5d1, "k", UTF_8);
62     assertStringHash(0xa167dbf3, "hell", UTF_8);
63     assertStringHash(0x248bfa47, "hello", UTF_8);
64     assertStringHash(0x3d41b97c, "http://www.google.com/", UTF_8);
65     assertStringHash(0x2e4ff723, "The quick brown fox jumps over the lazy dog", UTF_8);
66     assertStringHash(0xb5a4be05, "ABCDefGHI\u0799", UTF_8);
67     assertStringHash(0xfc5ba834, "毎月1日,毎週月曜日", UTF_8);
68     assertStringHash(0x8a5c3699, "surrogate pair: \uD83D\uDCB0", UTF_8);
69 
70     assertStringHash(0, "", UTF_16LE);
71     assertStringHash(0x288418e4, "k", UTF_16LE);
72     assertStringHash(0x5a0cb7c3, "hell", UTF_16LE);
73     assertStringHash(0xd7c31989, "hello", UTF_16LE);
74     assertStringHash(0x73564d8c, "http://www.google.com/", UTF_16LE);
75     assertStringHash(0xe07db09c, "The quick brown fox jumps over the lazy dog", UTF_16LE);
76     assertStringHash(0xfefa3e76, "ABCDefGHI\u0799", UTF_16LE);
77     assertStringHash(0x6a7be132, "毎月1日,毎週月曜日", UTF_16LE);
78     assertStringHash(0x5a2d41c7, "surrogate pair: \uD83D\uDCB0", UTF_16LE);
79   }
80 
81   @SuppressWarnings("deprecation")
assertStringHash(int expected, String string, Charset charset)82   private void assertStringHash(int expected, String string, Charset charset) {
83     if (allBmp(string)) {
84       assertHash(expected, murmur3_32().hashString(string, charset));
85     }
86     assertHash(expected, murmur3_32_fixed().hashString(string, charset));
87     assertHash(expected, murmur3_32().newHasher().putString(string, charset).hash());
88     assertHash(expected, murmur3_32_fixed().newHasher().putString(string, charset).hash());
89     assertHash(expected, murmur3_32().hashBytes(string.getBytes(charset)));
90     assertHash(expected, murmur3_32_fixed().hashBytes(string.getBytes(charset)));
91     assertHash(expected, murmur3_32().newHasher().putBytes(string.getBytes(charset)).hash());
92     assertHash(expected, murmur3_32_fixed().newHasher().putBytes(string.getBytes(charset)).hash());
93   }
94 
allBmp(String string)95   private boolean allBmp(String string) {
96     // Ordinarily we'd use something like i += Character.charCount(string.codePointAt(i)) here. But
97     // we can get away with i++ because the whole point of this method is to return false if we find
98     // a code point that doesn't fit in a char.
99     for (int i = 0; i < string.length(); i++) {
100       if (string.codePointAt(i) > 0xffff) {
101         return false;
102       }
103     }
104     return true;
105   }
106 
107   @SuppressWarnings("deprecation")
testSimpleStringUtf8()108   public void testSimpleStringUtf8() {
109     assertEquals(
110         murmur3_32().hashBytes("ABCDefGHI\u0799".getBytes(UTF_8)),
111         murmur3_32().hashString("ABCDefGHI\u0799", UTF_8));
112   }
113 
114   @SuppressWarnings("deprecation")
testEncodedStringInputs()115   public void testEncodedStringInputs() {
116     Random rng = new Random(0);
117     for (int z = 0; z < 100; z++) {
118       String str;
119       int[] codePoints = new int[rng.nextInt(8)];
120       for (int i = 0; i < codePoints.length; i++) {
121         do {
122           codePoints[i] = rng.nextInt(0x800);
123         } while (!Character.isValidCodePoint(codePoints[i])
124             || (codePoints[i] >= Character.MIN_SURROGATE
125                 && codePoints[i] <= Character.MAX_SURROGATE));
126       }
127       StringBuilder builder = new StringBuilder();
128       for (int i = 0; i < codePoints.length; i++) {
129         builder.appendCodePoint(codePoints[i]);
130       }
131       str = builder.toString();
132       HashCode hashUtf8 = murmur3_32().hashBytes(str.getBytes(UTF_8));
133       assertEquals(hashUtf8, murmur3_32().newHasher().putBytes(str.getBytes(UTF_8)).hash());
134       assertEquals(hashUtf8, murmur3_32().hashString(str, UTF_8));
135       assertEquals(hashUtf8, murmur3_32().newHasher().putString(str, UTF_8).hash());
136       HashCode hashUtf16 = murmur3_32().hashBytes(str.getBytes(UTF_16));
137       assertEquals(hashUtf16, murmur3_32().newHasher().putBytes(str.getBytes(UTF_16)).hash());
138       assertEquals(hashUtf16, murmur3_32().hashString(str, UTF_16));
139       assertEquals(hashUtf16, murmur3_32().newHasher().putString(str, UTF_16).hash());
140     }
141   }
142 
assertHash(int expected, HashCode actual)143   private static void assertHash(int expected, HashCode actual) {
144     assertEquals(HashCode.fromInt(expected), actual);
145   }
146 
testParanoidHashBytes()147   public void testParanoidHashBytes() {
148     HashFn hf =
149         new HashFn() {
150           @Override
151           public byte[] hash(byte[] input, int seed) {
152             return murmur3_32(seed).hashBytes(input).asBytes();
153           }
154         };
155     // Murmur3A, MurmurHash3 for x86, 32-bit (MurmurHash3_x86_32)
156     // https://github.com/aappleby/smhasher/blob/master/src/main.cpp
157     HashTestUtils.verifyHashFunction(hf, 32, 0xB0F57EE3);
158   }
159 
testParanoid()160   public void testParanoid() {
161     HashFn hf =
162         new HashFn() {
163           @Override
164           public byte[] hash(byte[] input, int seed) {
165             Hasher hasher = murmur3_32(seed).newHasher();
166             Funnels.byteArrayFunnel().funnel(input, hasher);
167             return hasher.hash().asBytes();
168           }
169         };
170     // Murmur3A, MurmurHash3 for x86, 32-bit (MurmurHash3_x86_32)
171     // https://github.com/aappleby/smhasher/blob/master/src/main.cpp
172     HashTestUtils.verifyHashFunction(hf, 32, 0xB0F57EE3);
173   }
174 
testInvariants()175   public void testInvariants() {
176     HashTestUtils.assertInvariants(murmur3_32());
177   }
178 
179   @SuppressWarnings("deprecation")
testInvalidUnicodeHashString()180   public void testInvalidUnicodeHashString() {
181     String str =
182         new String(
183             new char[] {'a', Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE, 'z'});
184     assertEquals(murmur3_32().hashBytes(str.getBytes(UTF_8)), murmur3_32().hashString(str, UTF_8));
185     assertEquals(
186         murmur3_32_fixed().hashBytes(str.getBytes(UTF_8)), murmur3_32().hashString(str, UTF_8));
187   }
188 
189   @SuppressWarnings("deprecation")
testInvalidUnicodeHasherPutString()190   public void testInvalidUnicodeHasherPutString() {
191     String str =
192         new String(
193             new char[] {'a', Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE, 'z'});
194     assertEquals(
195         murmur3_32().hashBytes(str.getBytes(UTF_8)),
196         murmur3_32().newHasher().putString(str, UTF_8).hash());
197     assertEquals(
198         murmur3_32_fixed().hashBytes(str.getBytes(UTF_8)),
199         murmur3_32_fixed().newHasher().putString(str, UTF_8).hash());
200   }
201 }
202