5047N/A/*
5047N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5047N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5047N/A *
5047N/A * This code is free software; you can redistribute it and/or modify it
5047N/A * under the terms of the GNU General Public License version 2 only, as
5047N/A * published by the Free Software Foundation.
5047N/A *
5047N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5047N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5047N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5047N/A * version 2 for more details (a copy is included in the LICENSE file that
5047N/A * accompanied this code).
5047N/A *
5047N/A * You should have received a copy of the GNU General Public License version
5047N/A * 2 along with this work; if not, write to the Free Software Foundation,
5047N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5047N/A *
5047N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5047N/A * or visit www.oracle.com if you need additional information or have any
5047N/A * questions.
5047N/A */
5047N/A
5047N/A/*
5047N/A * @test @summary Ensure that Murmur3 hash performs according to specification.
5047N/A * @compile -XDignore.symbol.file Hashing.java
5047N/A */
5047N/Apublic class Hashing {
5047N/A
5047N/A static final byte ONE_BYTE[] = {
5047N/A (byte) 0x80};
5047N/A static final byte TWO_BYTE[] = {
5047N/A (byte) 0x80, (byte) 0x81};
5047N/A static final char ONE_CHAR[] = {
5047N/A (char) 0x8180};
5047N/A static final byte THREE_BYTE[] = {
5047N/A (byte) 0x80, (byte) 0x81, (byte) 0x82};
5047N/A static final byte FOUR_BYTE[] = {
5047N/A (byte) 0x80, (byte) 0x81, (byte) 0x82, (byte) 0x83};
5047N/A static final char TWO_CHAR[] = {
5047N/A (char) 0x8180, (char) 0x8382};
5047N/A static final int ONE_INT[] = {
5047N/A 0x83828180};
5047N/A static final byte SIX_BYTE[] = {
5047N/A (byte) 0x80, (byte) 0x81, (byte) 0x82,
5047N/A (byte) 0x83, (byte) 0x84, (byte) 0x85};
5047N/A static final char THREE_CHAR[] = {
5047N/A (char) 0x8180, (char) 0x8382, (char) 0x8584};
5047N/A static final byte EIGHT_BYTE[] = {
5047N/A (byte) 0x80, (byte) 0x81, (byte) 0x82,
5047N/A (byte) 0x83, (byte) 0x84, (byte) 0x85,
5047N/A (byte) 0x86, (byte) 0x87};
5047N/A static final char FOUR_CHAR[] = {
5047N/A (char) 0x8180, (char) 0x8382,
5047N/A (char) 0x8584, (char) 0x8786};
5047N/A static final int TWO_INT[] = {
5047N/A 0x83828180, 0x87868584};
5047N/A // per http://code.google.com/p/smhasher/source/browse/trunk/main.cpp, line:72
5047N/A static final int MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
5047N/A
5047N/A public static void testMurmur3_32_ByteArray() {
5047N/A System.out.println("testMurmur3_32_ByteArray");
5047N/A
5047N/A byte[] vector = new byte[256];
5047N/A byte[] hashes = new byte[4 * 256];
5047N/A
5047N/A for (int i = 0; i < 256; i++) {
5047N/A vector[i] = (byte) i;
5047N/A }
5047N/A
5047N/A // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
5047N/A for (int i = 0; i < 256; i++) {
5047N/A int hash = sun.misc.Hashing.murmur3_32(256 - i, vector, 0, i);
5047N/A
5047N/A hashes[i * 4] = (byte) hash;
5047N/A hashes[i * 4 + 1] = (byte) (hash >>> 8);
5047N/A hashes[i * 4 + 2] = (byte) (hash >>> 16);
5047N/A hashes[i * 4 + 3] = (byte) (hash >>> 24);
5047N/A }
5047N/A
5047N/A // hash to get final result.
5047N/A int final_hash = sun.misc.Hashing.murmur3_32(0, hashes);
5047N/A
5047N/A if (MURMUR3_32_X86_CHECK_VALUE != final_hash) {
5047N/A throw new RuntimeException(
5047N/A String.format("Calculated hash result not as expected. Expected %08X got %08X",
5047N/A MURMUR3_32_X86_CHECK_VALUE,
5047N/A final_hash));
5047N/A }
5047N/A }
5047N/A
5047N/A public static void testEquivalentHashes() {
5047N/A int bytes, chars, ints;
5047N/A
5047N/A System.out.println("testEquivalentHashes");
5047N/A
5047N/A bytes = sun.misc.Hashing.murmur3_32(TWO_BYTE);
5047N/A chars = sun.misc.Hashing.murmur3_32(ONE_CHAR);
5047N/A if (bytes != chars) {
5047N/A throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x", bytes, chars));
5047N/A }
5047N/A
5047N/A bytes = sun.misc.Hashing.murmur3_32(FOUR_BYTE);
5047N/A chars = sun.misc.Hashing.murmur3_32(TWO_CHAR);
5047N/A ints = sun.misc.Hashing.murmur3_32(ONE_INT);
5047N/A if ((bytes != chars) || (bytes != ints)) {
5047N/A throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x != i:%08x", bytes, chars, ints));
5047N/A }
5047N/A bytes = sun.misc.Hashing.murmur3_32(SIX_BYTE);
5047N/A chars = sun.misc.Hashing.murmur3_32(THREE_CHAR);
5047N/A if (bytes != chars) {
5047N/A throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x", bytes, chars));
5047N/A }
5047N/A
5047N/A bytes = sun.misc.Hashing.murmur3_32(EIGHT_BYTE);
5047N/A chars = sun.misc.Hashing.murmur3_32(FOUR_CHAR);
5047N/A ints = sun.misc.Hashing.murmur3_32(TWO_INT);
5047N/A if ((bytes != chars) || (bytes != ints)) {
5047N/A throw new RuntimeException(String.format("Hashes did not match. b:%08x != c:%08x != i:%08x", bytes, chars, ints));
5047N/A }
5047N/A }
5047N/A
5047N/A public static void main(String[] args) {
5047N/A testMurmur3_32_ByteArray();
5047N/A testEquivalentHashes();
5047N/A }
5047N/A}