Path: blob/master/test/jdk/java/lang/String/EqualsIgnoreCase.java
41149 views
/*1* Copyright (c) 2015, 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/* @test24* @bug 813882425* @summary Test expected equalsIgnoreCase behavior for some known asymmetric case mappings26*/2728public class EqualsIgnoreCase {29private static final String SMALL_I = "i";30private static final String CAPITAL_I = "I";31// Characters that do not map symmetrically between upper/lower case32private static final String SMALL_DOTLESS_I = "\u0131";33private static final String CAPITAL_I_WITH_DOT = "\u0130";34private static final String LOWER_GREEK_THETA = "\u03D1";35private static final String CAPITAL_GREEK_THETA = "\u03F4";3637public static void main(String[] args) {38compareFuncs(SMALL_I, CAPITAL_I, true, true);39compareFuncs(CAPITAL_I_WITH_DOT, SMALL_DOTLESS_I, true, false);40compareFuncs(LOWER_GREEK_THETA, CAPITAL_GREEK_THETA, true, false);41}4243/**44* Compare the actual results of equalsIgnoreCase():45* toUpperCase(toLowerCase(eachChar))46* to the behavior described in the equalsIgnoreCase() spec prior to 8138824:47* toUpperCase(eachChar)48* toLowerCase(eachChar)49*50* @param s1 A string51* @param s2 Another string52* @param expectEquals Expected result of equalsIgnoreCase()53* @param expectTuTl Expected result of toUpperToLowerOriginals()54*/55private static void compareFuncs(String s1, String s2, boolean expectEquals, boolean expectTuTl) {56System.out.println(s1 + ", " + s2);57boolean equalsResult = s1.equalsIgnoreCase(s2);58System.out.println("equalsIgnoreCase:" + equalsResult);5960boolean tuTlResult = toUpperToLowerOriginals(s1, s2);61System.out.println("tUtLO:" + tuTlResult);62boolean failed = false;6364if (equalsResult != expectEquals) {65System.out.println("Expected " + expectEquals + " from equalsIgnoreCase() but got " + equalsResult);66failed = true;67}68if (tuTlResult != expectTuTl) {69System.out.println("Expected " + expectTuTl + " from toUpperToLowerOriginals() but got " + tuTlResult);70failed = true;71}72if (failed) { throw new RuntimeException("Test Failed"); }73}7475/**76* Apply toUpperCase() and toLowerCase() to corresponding chars of both77* Strings. Returns true if each pair of corresponding chars are either:78* 1. == after both are converted to upper case79* or80* 2. == after both are converted to lower case81* and the String lengths are equal.82*/83private static boolean toUpperToLowerOriginals(String str1, String str2) {84if (str1.length() != str2.length()) { return false; }85for (int i = 0; i < str1.length(); i++) {86char c1 = str1.charAt(i);87char c2 = str2.charAt(i);8889char uc1 = Character.toUpperCase(c1);90char uc2 = Character.toUpperCase(c2);91boolean upperMatch = uc1 == uc2;9293char lc1 = Character.toLowerCase(c1);94char lc2 = Character.toLowerCase(c2);95boolean lowerMatch = lc1 == lc2;9697if (!(upperMatch || lowerMatch)) {98return false;99}100}101return true;102}103}104105106