12859
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test @summary Ensure that Murmur3 hash performs according to specification.
|
|
26 |
* @compile -XDignore.symbol.file Hashing.java
|
|
27 |
*/
|
|
28 |
public class Hashing {
|
|
29 |
|
|
30 |
static final byte ONE_BYTE[] = {
|
|
31 |
(byte) 0x80};
|
|
32 |
static final byte TWO_BYTE[] = {
|
|
33 |
(byte) 0x80, (byte) 0x81};
|
|
34 |
static final char ONE_CHAR[] = {
|
|
35 |
(char) 0x8180};
|
|
36 |
static final byte THREE_BYTE[] = {
|
|
37 |
(byte) 0x80, (byte) 0x81, (byte) 0x82};
|
|
38 |
static final byte FOUR_BYTE[] = {
|
|
39 |
(byte) 0x80, (byte) 0x81, (byte) 0x82, (byte) 0x83};
|
|
40 |
static final char TWO_CHAR[] = {
|
|
41 |
(char) 0x8180, (char) 0x8382};
|
|
42 |
static final int ONE_INT[] = {
|
|
43 |
0x83828180};
|
|
44 |
static final byte SIX_BYTE[] = {
|
|
45 |
(byte) 0x80, (byte) 0x81, (byte) 0x82,
|
|
46 |
(byte) 0x83, (byte) 0x84, (byte) 0x85};
|
|
47 |
static final char THREE_CHAR[] = {
|
|
48 |
(char) 0x8180, (char) 0x8382, (char) 0x8584};
|
|
49 |
static final byte EIGHT_BYTE[] = {
|
|
50 |
(byte) 0x80, (byte) 0x81, (byte) 0x82,
|
|
51 |
(byte) 0x83, (byte) 0x84, (byte) 0x85,
|
|
52 |
(byte) 0x86, (byte) 0x87};
|
|
53 |
static final char FOUR_CHAR[] = {
|
|
54 |
(char) 0x8180, (char) 0x8382,
|
|
55 |
(char) 0x8584, (char) 0x8786};
|
|
56 |
static final int TWO_INT[] = {
|
|
57 |
0x83828180, 0x87868584};
|
|
58 |
// per http://code.google.com/p/smhasher/source/browse/trunk/main.cpp, line:72
|
|
59 |
static final int MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
|
|
60 |
|
|
61 |
public static void testMurmur3_32_ByteArray() {
|
|
62 |
System.out.println("testMurmur3_32_ByteArray");
|
|
63 |
|
|
64 |
byte[] vector = new byte[256];
|
|
65 |
byte[] hashes = new byte[4 * 256];
|
|
66 |
|
|
67 |
for (int i = 0; i < 256; i++) {
|
|
68 |
vector[i] = (byte) i;
|
|
69 |
}
|
|
70 |
|
|
71 |
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
|
|
72 |
for (int i = 0; i < 256; i++) {
|
|
73 |
int hash = sun.misc.Hashing.murmur3_32(256 - i, vector, 0, i);
|
|
74 |
|
|
75 |
hashes[i * 4] = (byte) hash;
|
|
76 |
hashes[i * 4 + 1] = (byte) (hash >>> 8);
|
|
77 |
hashes[i * 4 + 2] = (byte) (hash >>> 16);
|
|
78 |
hashes[i * 4 + 3] = (byte) (hash >>> 24);
|
|
79 |
}
|
|
80 |
|
|
81 |
// hash to get final result.
|
|
82 |
int final_hash = sun.misc.Hashing.murmur3_32(0, hashes);
|
|
83 |
|
|
84 |
if (MURMUR3_32_X86_CHECK_VALUE != final_hash) {
|
|
85 |
throw new RuntimeException(
|
|
86 |
String.format("Calculated hash result not as expected. Expected %08X got %08X",
|
|
87 |
MURMUR3_32_X86_CHECK_VALUE,
|
|
88 |
final_hash));
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
public static void testEquivalentHashes() {
|
|
93 |
int bytes, chars, ints;
|
|
94 |
|
|
95 |
System.out.println("testEquivalentHashes");
|
|
96 |
|
|
97 |
bytes = sun.misc.Hashing.murmur3_32(TWO_BYTE);
|
|
98 |
chars = sun.misc.Hashing.murmur3_32(ONE_CHAR);
|
|
99 |
if (bytes != chars) {
|
|
100 |
throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x", bytes, chars));
|
|
101 |
}
|
|
102 |
|
|
103 |
bytes = sun.misc.Hashing.murmur3_32(FOUR_BYTE);
|
|
104 |
chars = sun.misc.Hashing.murmur3_32(TWO_CHAR);
|
|
105 |
ints = sun.misc.Hashing.murmur3_32(ONE_INT);
|
|
106 |
if ((bytes != chars) || (bytes != ints)) {
|
|
107 |
throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x != i:%08x", bytes, chars, ints));
|
|
108 |
}
|
|
109 |
bytes = sun.misc.Hashing.murmur3_32(SIX_BYTE);
|
|
110 |
chars = sun.misc.Hashing.murmur3_32(THREE_CHAR);
|
|
111 |
if (bytes != chars) {
|
|
112 |
throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x", bytes, chars));
|
|
113 |
}
|
|
114 |
|
|
115 |
bytes = sun.misc.Hashing.murmur3_32(EIGHT_BYTE);
|
|
116 |
chars = sun.misc.Hashing.murmur3_32(FOUR_CHAR);
|
|
117 |
ints = sun.misc.Hashing.murmur3_32(TWO_INT);
|
|
118 |
if ((bytes != chars) || (bytes != ints)) {
|
|
119 |
throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x != i:%08x", bytes, chars, ints));
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
public static void main(String[] args) {
|
|
124 |
testMurmur3_32_ByteArray();
|
|
125 |
testEquivalentHashes();
|
|
126 |
}
|
|
127 |
}
|