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