test/jdk/java/lang/String/EqualsIgnoreCase.java
author jlaskey
Fri, 18 May 2018 08:43:49 -0300
changeset 50175 589ed2770141
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200436: String::isBlank Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     1
/*
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     4
 *
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     8
 *
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    13
 * accompanied this code).
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    14
 *
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    18
 *
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    21
 * questions.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    22
 */
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    23
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    24
/* @test
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    25
 * @bug 8138824
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    26
 * @summary Test expected equalsIgnoreCase behavior for some known asymmetric case mappings
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    27
 */
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    28
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    29
public class EqualsIgnoreCase {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    30
    private static final String SMALL_I = "i";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    31
    private static final String CAPITAL_I = "I";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    32
    // Characters that do not map symmetrically between upper/lower case
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    33
    private static final String SMALL_DOTLESS_I = "\u0131";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    34
    private static final String CAPITAL_I_WITH_DOT = "\u0130";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    35
    private static final String LOWER_GREEK_THETA = "\u03D1";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    36
    private static final String CAPITAL_GREEK_THETA = "\u03F4";
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    37
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    38
    public static void main(String[] args) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    39
        compareFuncs(SMALL_I, CAPITAL_I, true, true);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    40
        compareFuncs(CAPITAL_I_WITH_DOT, SMALL_DOTLESS_I, true, false);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    41
        compareFuncs(LOWER_GREEK_THETA, CAPITAL_GREEK_THETA, true, false);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    42
    }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    43
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    44
    /**
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    45
     * Compare the actual results of equalsIgnoreCase():
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    46
     *   toUpperCase(toLowerCase(eachChar))
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    47
     * to the behavior described in the equalsIgnoreCase() spec prior to 8138824:
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    48
     *   toUpperCase(eachChar)
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    49
     *   toLowerCase(eachChar)
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    50
     *
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    51
     * @param s1 A string
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    52
     * @param s2 Another string
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    53
     * @param expectEquals Expected result of equalsIgnoreCase()
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    54
     * @param expectTuTl Expected result of toUpperToLowerOriginals()
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    55
     */
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    56
    private static void compareFuncs(String s1, String s2, boolean expectEquals, boolean expectTuTl) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    57
        System.out.println(s1 + ", " + s2);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    58
        boolean equalsResult = s1.equalsIgnoreCase(s2);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    59
        System.out.println("equalsIgnoreCase:" + equalsResult);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    60
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    61
        boolean tuTlResult = toUpperToLowerOriginals(s1, s2);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    62
        System.out.println("tUtLO:" + tuTlResult);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    63
        boolean failed = false;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    64
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    65
        if (equalsResult != expectEquals) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    66
            System.out.println("Expected " + expectEquals + " from equalsIgnoreCase() but got " + equalsResult);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    67
            failed = true;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    68
        }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    69
        if (tuTlResult != expectTuTl) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    70
            System.out.println("Expected " + expectTuTl + " from toUpperToLowerOriginals() but got " + tuTlResult);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    71
            failed = true;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    72
        }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    73
        if (failed) { throw new RuntimeException("Test Failed"); }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    74
    }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    75
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    76
    /**
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    77
     * Apply toUpperCase() and toLowerCase() to corresponding chars of both
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    78
     * Strings.  Returns true if each pair of corresponding chars are either:
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    79
     *   1. == after both are converted to upper case
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    80
     * or
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    81
     *   2. == after both are converted to lower case
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    82
     * and the String lengths are equal.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    83
     */
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    84
    private static boolean toUpperToLowerOriginals(String str1, String str2) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    85
        if (str1.length() != str2.length()) { return false; }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    86
        for (int i = 0; i < str1.length(); i++) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    87
            char c1 = str1.charAt(i);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    88
            char c2 = str2.charAt(i);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    89
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    90
            char uc1 = Character.toUpperCase(c1);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    91
            char uc2 = Character.toUpperCase(c2);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    92
            boolean upperMatch = uc1 == uc2;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    93
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    94
            char lc1 = Character.toLowerCase(c1);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    95
            char lc2 = Character.toLowerCase(c2);
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    96
            boolean lowerMatch = lc1 == lc2;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    97
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    98
            if (!(upperMatch || lowerMatch)) {
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
    99
                return false;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
   100
            }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
   101
        }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
   102
        return true;
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
   103
    }
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
diff changeset
   104
}