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