blob: 6d62facea7b66531b35b9f0dbe9ea07fddbba081 [file] [log] [blame]
martin9ba280b2009-09-22 18:30:58 -07001/*
2 * Copyright 2009 Google, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4245470
27 * @summary Test the primitive wrappers hashCode()
28 */
29
30import java.util.Random;
31
32public class HashCode {
33
34 final Random rnd = new Random();
35
36 void test(String args[]) throws Exception {
37 int[] ints = {
38 Integer.MIN_VALUE,
39 Short.MIN_VALUE,
40 Character.MIN_VALUE,
41 Byte.MIN_VALUE,
42 -1, 0, 1,
43 Byte.MAX_VALUE,
44 Character.MAX_VALUE,
45 Short.MAX_VALUE,
46 Integer.MAX_VALUE,
47 rnd.nextInt(),
48 };
49
50 for (int x : ints) {
51 check( new Long(x).hashCode() == (int)((long)x ^ (long)x>>>32));
52 check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32));
53 check( new Integer(x).hashCode() == x);
54 check(Integer.valueOf(x).hashCode() == x);
55 check( new Short((short)x).hashCode() == (short) x);
56 check(Short.valueOf((short)x).hashCode() == (short) x);
57 check( new Character((char) x).hashCode() == (char) x);
58 check(Character.valueOf((char) x).hashCode() == (char) x);
59 check( new Byte((byte) x).hashCode() == (byte) x);
60 check(Byte.valueOf((byte) x).hashCode() == (byte) x);
61 }
62 }
63
64 //--------------------- Infrastructure ---------------------------
65 volatile int passed = 0, failed = 0;
66 void pass() {passed++;}
67 void fail() {failed++; Thread.dumpStack();}
68 void fail(String msg) {System.err.println(msg); fail();}
69 void unexpected(Throwable t) {failed++; t.printStackTrace();}
70 void check(boolean cond) {if (cond) pass(); else fail();}
71 void equal(Object x, Object y) {
72 if (x == null ? y == null : x.equals(y)) pass();
73 else fail(x + " not equal to " + y);}
74 public static void main(String[] args) throws Throwable {
75 new HashCode().instanceMain(args);}
76 public void instanceMain(String[] args) throws Throwable {
77 try {test(args);} catch (Throwable t) {unexpected(t);}
78 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
79 if (failed > 0) throw new AssertionError("Some tests failed");}
80}