Path: blob/master/test/jdk/java/lang/HashCode.java
41145 views
/*1* Copyright 2009 Google, Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4245470 708891326* @summary Test the primitive wrappers hashCode()27* @key randomness28*/2930import java.util.Objects;31import java.util.Random;3233public class HashCode {3435final Random rnd = new Random();3637void testOrdinals(String args[]) throws Exception {38long[] longs = {39Long.MIN_VALUE,40Integer.MIN_VALUE,41Short.MIN_VALUE,42Character.MIN_VALUE,43Byte.MIN_VALUE,44-1, 0, 1,45Byte.MAX_VALUE,46Character.MAX_VALUE,47Short.MAX_VALUE,48Integer.MAX_VALUE,49Long.MAX_VALUE,50rnd.nextInt(),51};5253for (long x : longs) {54check( new Long(x).hashCode() == (int)((long)x ^ (long)x>>>32));55check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32));56check( (new Long(x)).hashCode() == Long.hashCode(x));57check( new Integer((int)x).hashCode() == (int) x);58check(Integer.valueOf((int)x).hashCode() == (int) x);59check( (new Integer((int)x)).hashCode() == Integer.hashCode((int)x));60check( new Short((short)x).hashCode() == (short) x);61check(Short.valueOf((short)x).hashCode() == (short) x);62check( (new Short((short)x)).hashCode() == Short.hashCode((short)x));63check( new Character((char) x).hashCode() == (char) x);64check(Character.valueOf((char) x).hashCode() == (char) x);65check( (new Character((char)x)).hashCode() == Character.hashCode((char)x));66check( new Byte((byte) x).hashCode() == (byte) x);67check(Byte.valueOf((byte) x).hashCode() == (byte) x);68check( (new Byte((byte)x)).hashCode() == Byte.hashCode((byte)x));69}70}7172void testBoolean() {73check( Boolean.FALSE.hashCode() == 1237);74check( Boolean.TRUE.hashCode() == 1231);75check( Boolean.valueOf(false).hashCode() == 1237);76check( Boolean.valueOf(true).hashCode() == 1231);77check( (new Boolean(false)).hashCode() == 1237);78check( (new Boolean(true)).hashCode() == 1231);79check( Boolean.hashCode(false) == 1237);80check( Boolean.hashCode(true) == 1231);81}8283void testFloat() {84float[] floats = {85Float.NaN,86Float.NEGATIVE_INFINITY,87-1f,880f,891f,90Float.POSITIVE_INFINITY91};9293for(float f : floats) {94check( Float.hashCode(f) == Float.floatToIntBits(f));95check( Float.valueOf(f).hashCode() == Float.floatToIntBits(f));96check( (new Float(f)).hashCode() == Float.floatToIntBits(f));97}98}99100void testDouble() {101double[] doubles = {102Double.NaN,103Double.NEGATIVE_INFINITY,104-1f,1050f,1061f,107Double.POSITIVE_INFINITY108};109110for(double d : doubles) {111long bits = Double.doubleToLongBits(d);112int bitsHash = (int)(bits^(bits>>>32));113check( Double.hashCode(d) == bitsHash);114check( Double.valueOf(d).hashCode() == bitsHash);115check( (new Double(d)).hashCode() == bitsHash);116}117}118119//--------------------- Infrastructure ---------------------------120volatile int passed = 0, failed = 0;121void pass() {passed++;}122void fail() {failed++; Thread.dumpStack();}123void fail(String msg) {System.err.println(msg); fail();}124void unexpected(Throwable t) {failed++; t.printStackTrace();}125void check(boolean cond) {if (cond) pass(); else fail();}126void equal(Object x, Object y) {127if (Objects.equals(x,y)) pass();128else fail(x + " not equal to " + y);}129public static void main(String[] args) throws Throwable {130new HashCode().instanceMain(args);}131public void instanceMain(String[] args) throws Throwable {132try { testOrdinals(args);133testBoolean();134testFloat();135testDouble();136} catch (Throwable t) {unexpected(t);}137System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);138if (failed > 0) throw new AssertionError("Some tests failed");}139}140141142