jdk/test/java/lang/HashCode.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 14503 0729d9e57ed5
child 30046 cf2c86e1819e
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     1
/*
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     2
 * Copyright 2009 Google, Inc.  All Rights Reserved.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     4
 *
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     8
 *
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    13
 * accompanied this code).
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    14
 *
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3942
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3942
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3942
diff changeset
    21
 * questions.
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    22
 */
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    23
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    24
/*
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    25
 * @test
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    26
 * @bug 4245470 7088913
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    27
 * @summary Test the primitive wrappers hashCode()
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    28
 */
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    29
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    30
import java.util.Objects;
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    31
import java.util.Random;
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    32
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    33
public class HashCode {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    34
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    35
    final Random rnd = new Random();
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    36
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    37
    void testOrdinals(String args[]) throws Exception {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    38
        long[] longs = {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    39
            Long.MIN_VALUE,
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    40
            Integer.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    41
            Short.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    42
            Character.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    43
            Byte.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    44
            -1, 0, 1,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    45
            Byte.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    46
            Character.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    47
            Short.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    48
            Integer.MAX_VALUE,
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    49
            Long.MAX_VALUE,
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    50
            rnd.nextInt(),
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    51
        };
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    52
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    53
        for (long x : longs) {
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    54
            check(    new Long(x).hashCode() == (int)((long)x ^ (long)x>>>32));
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    55
            check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32));
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    56
            check(  (new Long(x)).hashCode() == Long.hashCode(x));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    57
            check(    new Integer((int)x).hashCode() == (int) x);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    58
            check(Integer.valueOf((int)x).hashCode() == (int) x);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    59
            check(  (new Integer((int)x)).hashCode() == Integer.hashCode((int)x));
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    60
            check(    new Short((short)x).hashCode() == (short) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    61
            check(Short.valueOf((short)x).hashCode() == (short) x);
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    62
            check(         (new Short((short)x)).hashCode() == Short.hashCode((short)x));
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    63
            check(    new Character((char) x).hashCode() == (char) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    64
            check(Character.valueOf((char) x).hashCode() == (char) x);
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    65
            check(         (new Character((char)x)).hashCode() == Character.hashCode((char)x));
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    66
            check(    new Byte((byte) x).hashCode() == (byte) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    67
            check(Byte.valueOf((byte) x).hashCode() == (byte) x);
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    68
            check(         (new Byte((byte)x)).hashCode() == Byte.hashCode((byte)x));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    69
        }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    70
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    71
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    72
    void testBoolean() {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    73
        check( Boolean.FALSE.hashCode() == 1237);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    74
        check( Boolean.TRUE.hashCode() == 1231);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    75
        check( Boolean.valueOf(false).hashCode() == 1237);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    76
        check( Boolean.valueOf(true).hashCode() == 1231);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    77
        check( (new Boolean(false)).hashCode() == 1237);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    78
        check( (new Boolean(true)).hashCode() == 1231);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    79
        check( Boolean.hashCode(false) == 1237);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    80
        check( Boolean.hashCode(true) == 1231);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    81
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    82
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    83
    void testFloat() {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    84
        float[] floats = {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    85
            Float.NaN,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    86
            Float.NEGATIVE_INFINITY,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    87
               -1f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    88
               0f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    89
               1f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    90
               Float.POSITIVE_INFINITY
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    91
        };
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    92
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    93
        for(float f : floats) {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    94
            check( Float.hashCode(f) == Float.floatToIntBits(f));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    95
            check( Float.valueOf(f).hashCode() == Float.floatToIntBits(f));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    96
            check( (new Float(f)).hashCode() == Float.floatToIntBits(f));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    97
        }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    98
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
    99
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   100
    void testDouble() {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   101
        double[] doubles = {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   102
            Double.NaN,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   103
            Double.NEGATIVE_INFINITY,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   104
               -1f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   105
               0f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   106
               1f,
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   107
               Double.POSITIVE_INFINITY
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   108
        };
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   109
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   110
        for(double d : doubles) {
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   111
            long bits = Double.doubleToLongBits(d);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   112
            int bitsHash = (int)(bits^(bits>>>32));
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   113
            check( Double.hashCode(d) == bitsHash);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   114
            check( Double.valueOf(d).hashCode() == bitsHash);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   115
            check( (new Double(d)).hashCode() == bitsHash);
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   116
        }
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   117
    }
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   118
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   119
    //--------------------- Infrastructure ---------------------------
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   120
    volatile int passed = 0, failed = 0;
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   121
    void pass() {passed++;}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   122
    void fail() {failed++; Thread.dumpStack();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   123
    void fail(String msg) {System.err.println(msg); fail();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   124
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   125
    void check(boolean cond) {if (cond) pass(); else fail();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   126
    void equal(Object x, Object y) {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   127
        if (Objects.equals(x,y)) pass();
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   128
        else fail(x + " not equal to " + y);}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   129
    public static void main(String[] args) throws Throwable {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   130
        new HashCode().instanceMain(args);}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   131
    public void instanceMain(String[] args) throws Throwable {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   132
        try { testOrdinals(args);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   133
              testBoolean();
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   134
                testFloat();
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   135
                testDouble();
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 5506
diff changeset
   136
        } catch (Throwable t) {unexpected(t);}
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   137
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   138
        if (failed > 0) throw new AssertionError("Some tests failed");}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
   139
}