Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/text/IntHashtable/Bug4170614Test.java
38839 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. 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(this test doesn't have an at-test tag because it's run by a shell25script instead of directly by the test harness)26*/2728/*29*30*31* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved32* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved33*34* Portions copyright (c) 2007 Sun Microsystems, Inc.35* All Rights Reserved.36*37* The original version of this source code and documentation38* is copyrighted and owned by Taligent, Inc., a wholly-owned39* subsidiary of IBM. These materials are provided under terms40* of a License Agreement between Taligent and Sun. This technology41* is protected by multiple US and International patents.42*43* This notice and attribution to Taligent may not be removed.44* Taligent is a registered trademark of Taligent, Inc.45*46* Permission to use, copy, modify, and distribute this software47* and its documentation for NON-COMMERCIAL purposes and without48* fee is hereby granted provided that this copyright notice49* appears in all copies. Please refer to the file "copyright.html"50* for further important copyright and licensing information.51*52* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF53* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED54* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A55* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR56* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR57* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.58*59*/60package java.text;61import sun.text.IntHashtable;626364/**65* This class tests some internal hashCode() functions.66* Bug #4170614 complained that we had two iternal classes that67* break the invariant that if a.equals(b) than a.hashCode() ==68* b.hashCode(). This is because these classes overrode equals()69* but not hashCode(). These are both purely internal classes, and70* the library itself doesn't actually call hashCode(), so this isn't71* actually causing anyone problems yet. But if these classes are72* ever exposed in the API, their hashCode() methods need to work right.73* PatternEntry will never be exposed in the API, but IntHashtable74* might be. This is a shell test to allow us to access classes that75* are declared package private.76* @author Richard Gillam77*/78public class Bug4170614Test {79public static void main(String[] args) throws Exception {80testIntHashtable();81testPatternEntry();82}838485public static void testIntHashtable() throws Exception {86IntHashtable fred = new IntHashtable();87fred.put(1, 10);88fred.put(2, 20);89fred.put(3, 30);9091IntHashtable barney = new IntHashtable();92barney.put(1, 10);93barney.put(3, 30);94barney.put(2, 20);9596IntHashtable homer = new IntHashtable();97homer.put(3, 30);98homer.put(1, 10);99homer.put(7, 900);100101if (fred.equals(barney)) {102System.out.println("fred.equals(barney)");103}104else {105System.out.println("!fred.equals(barney)");106}107System.out.println("fred.hashCode() == " + fred.hashCode());108System.out.println("barney.hashCode() == " + barney.hashCode());109110if (!fred.equals(barney)) {111throw new Exception("equals() failed on two hashtables that are equal");112}113114if (fred.hashCode() != barney.hashCode()) {115throw new Exception("hashCode() failed on two hashtables that are equal");116}117118System.out.println();119if (fred.equals(homer)) {120System.out.println("fred.equals(homer)");121}122else {123System.out.println("!fred.equals(homer)");124}125System.out.println("fred.hashCode() == " + fred.hashCode());126System.out.println("homer.hashCode() == " + homer.hashCode());127128if (fred.equals(homer)) {129throw new Exception("equals() failed on two hashtables that are not equal");130}131132if (fred.hashCode() == homer.hashCode()) {133throw new Exception("hashCode() failed on two hashtables that are not equal");134}135136System.out.println();137System.out.println("testIntHashtable() passed.\n");138}139140public static void testPatternEntry() throws Exception {141PatternEntry fred = new PatternEntry(1,142new StringBuffer("hello"),143new StringBuffer("up"));144PatternEntry barney = new PatternEntry(1,145new StringBuffer("hello"),146new StringBuffer("down"));147// (equals() only considers the "chars" field, so fred and barney are equal)148PatternEntry homer = new PatternEntry(1,149new StringBuffer("goodbye"),150new StringBuffer("up"));151152if (fred.equals(barney)) {153System.out.println("fred.equals(barney)");154}155else {156System.out.println("!fred.equals(barney)");157}158System.out.println("fred.hashCode() == " + fred.hashCode());159System.out.println("barney.hashCode() == " + barney.hashCode());160161if (!fred.equals(barney)) {162throw new Exception("equals() failed on two hashtables that are equal");163}164165if (fred.hashCode() != barney.hashCode()) {166throw new Exception("hashCode() failed on two hashtables that are equal");167}168169System.out.println();170if (fred.equals(homer)) {171System.out.println("fred.equals(homer)");172}173else {174System.out.println("!fred.equals(homer)");175}176System.out.println("fred.hashCode() == " + fred.hashCode());177System.out.println("homer.hashCode() == " + homer.hashCode());178179if (fred.equals(homer)) {180throw new Exception("equals() failed on two hashtables that are not equal");181}182183if (fred.hashCode() == homer.hashCode()) {184throw new Exception("hashCode() failed on two hashtables that are not equal");185}186187System.out.println();188System.out.println("testPatternEntry() passed.\n");189}190}191192193