1 /* 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4245470 7088913 27 * @summary Test the primitive wrappers hashCode() 28 * @key randomness 29 */ 30 31 import java.util.Objects; 32 import java.util.Random; 33 34 public class HashCode { 35 36 final Random rnd = new Random(); 37 38 void testOrdinals(String args[]) throws Exception { 39 long[] longs = { 40 Long.MIN_VALUE, 41 Integer.MIN_VALUE, 42 Short.MIN_VALUE, 43 Character.MIN_VALUE, 44 Byte.MIN_VALUE, 45 -1, 0, 1, 46 Byte.MAX_VALUE, 47 Character.MAX_VALUE, 48 Short.MAX_VALUE, 49 Integer.MAX_VALUE, 50 Long.MAX_VALUE, 51 rnd.nextInt(), 52 }; 53 54 for (long x : longs) { 55 check( new Long(x).hashCode() == (int)((long)x ^ (long)x>>>32)); 56 check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32)); 57 check( (new Long(x)).hashCode() == Long.hashCode(x)); 58 check( new Integer((int)x).hashCode() == (int) x); 59 check(Integer.valueOf((int)x).hashCode() == (int) x); 60 check( (new Integer((int)x)).hashCode() == Integer.hashCode((int)x)); 61 check( new Short((short)x).hashCode() == (short) x); 62 check(Short.valueOf((short)x).hashCode() == (short) x); 63 check( (new Short((short)x)).hashCode() == Short.hashCode((short)x)); 64 check( new Character((char) x).hashCode() == (char) x); 65 check(Character.valueOf((char) x).hashCode() == (char) x); 66 check( (new Character((char)x)).hashCode() == Character.hashCode((char)x)); 67 check( new Byte((byte) x).hashCode() == (byte) x); 68 check(Byte.valueOf((byte) x).hashCode() == (byte) x); 69 check( (new Byte((byte)x)).hashCode() == Byte.hashCode((byte)x)); 70 } 71 } 72 73 void testBoolean() { 74 check( Boolean.FALSE.hashCode() == 1237); 75 check( Boolean.TRUE.hashCode() == 1231); 76 check( Boolean.valueOf(false).hashCode() == 1237); 77 check( Boolean.valueOf(true).hashCode() == 1231); 78 check( (new Boolean(false)).hashCode() == 1237); 79 check( (new Boolean(true)).hashCode() == 1231); 80 check( Boolean.hashCode(false) == 1237); 81 check( Boolean.hashCode(true) == 1231); 82 } 83 84 void testFloat() { 85 float[] floats = { 86 Float.NaN, 87 Float.NEGATIVE_INFINITY, 88 -1f, 89 0f, 90 1f, 91 Float.POSITIVE_INFINITY 92 }; 93 94 for(float f : floats) { 95 check( Float.hashCode(f) == Float.floatToIntBits(f)); 96 check( Float.valueOf(f).hashCode() == Float.floatToIntBits(f)); 97 check( (new Float(f)).hashCode() == Float.floatToIntBits(f)); 98 } 99 } 100 101 void testDouble() { 102 double[] doubles = { 103 Double.NaN, 104 Double.NEGATIVE_INFINITY, 105 -1f, 106 0f, 107 1f, 108 Double.POSITIVE_INFINITY 109 }; 110 111 for(double d : doubles) { 112 long bits = Double.doubleToLongBits(d); 113 int bitsHash = (int)(bits^(bits>>>32)); 114 check( Double.hashCode(d) == bitsHash); 115 check( Double.valueOf(d).hashCode() == bitsHash); 116 check( (new Double(d)).hashCode() == bitsHash); 117 } 118 } 119 120 //--------------------- Infrastructure --------------------------- 121 volatile int passed = 0, failed = 0; 122 void pass() {passed++;} 123 void fail() {failed++; Thread.dumpStack();} 124 void fail(String msg) {System.err.println(msg); fail();} 125 void unexpected(Throwable t) {failed++; t.printStackTrace();} 126 void check(boolean cond) {if (cond) pass(); else fail();} 127 void equal(Object x, Object y) { 128 if (Objects.equals(x,y)) pass(); 129 else fail(x + " not equal to " + y);} 130 public static void main(String[] args) throws Throwable { 131 new HashCode().instanceMain(args);} 132 public void instanceMain(String[] args) throws Throwable { 133 try { testOrdinals(args); 134 testBoolean(); 135 testFloat(); 136 testDouble(); 137 } catch (Throwable t) {unexpected(t);} 138 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 139 if (failed > 0) throw new AssertionError("Some tests failed");} 140 }