jdk/test/java/lang/HashCode.java
author lana
Thu, 30 Jun 2011 14:19:25 -0700
changeset 10188 cdd8666ce536
parent 5506 202f599c92aa
child 14503 0729d9e57ed5
permissions -rw-r--r--
Merge
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
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    26
 * @bug 4245470
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
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    30
import java.util.Random;
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    31
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    32
public class HashCode {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    33
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    34
    final Random rnd = new Random();
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    35
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    36
    void test(String args[]) throws Exception {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    37
        int[] ints = {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    38
            Integer.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    39
            Short.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    40
            Character.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    41
            Byte.MIN_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    42
            -1, 0, 1,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    43
            Byte.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    44
            Character.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    45
            Short.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    46
            Integer.MAX_VALUE,
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    47
            rnd.nextInt(),
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    48
        };
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    49
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    50
        for (int x : ints) {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    51
            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
    52
            check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32));
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    53
            check(    new Integer(x).hashCode() == x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    54
            check(Integer.valueOf(x).hashCode() == x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    55
            check(    new Short((short)x).hashCode() == (short) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    56
            check(Short.valueOf((short)x).hashCode() == (short) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    57
            check(    new Character((char) x).hashCode() == (char) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    58
            check(Character.valueOf((char) x).hashCode() == (char) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    59
            check(    new Byte((byte) x).hashCode() == (byte) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    60
            check(Byte.valueOf((byte) x).hashCode() == (byte) x);
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    61
        }
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    62
    }
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    63
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    64
    //--------------------- Infrastructure ---------------------------
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    65
    volatile int passed = 0, failed = 0;
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    66
    void pass() {passed++;}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    67
    void fail() {failed++; Thread.dumpStack();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    68
    void fail(String msg) {System.err.println(msg); fail();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    69
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    70
    void check(boolean cond) {if (cond) pass(); else fail();}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    71
    void equal(Object x, Object y) {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    72
        if (x == null ? y == null : x.equals(y)) pass();
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    73
        else fail(x + " not equal to " + y);}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    74
    public static void main(String[] args) throws Throwable {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    75
        new HashCode().instanceMain(args);}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    76
    public void instanceMain(String[] args) throws Throwable {
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    77
        try {test(args);} catch (Throwable t) {unexpected(t);}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    78
        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
    79
        if (failed > 0) throw new AssertionError("Some tests failed");}
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
diff changeset
    80
}