test/jdk/java/lang/String/ToUpperCase.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 33663 jdk/test/java/lang/String/ToUpperCase.java@2cd62a4bd471
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
    @test
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
    26
    @bug 4219630 4304573 4533872 4900935 8042589 8054307
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    @summary toUpperCase should upper-case German sharp s correctly even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
             it's the only character in the string. should also uppercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
             all of the 1:M char mappings correctly.  Also it should handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
             Locale specific (lt, tr, and az) uppercasings and supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
             characters correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ToUpperCase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Locale turkish = new Locale("tr", "TR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Locale lt = new Locale("lt"); // Lithanian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        Locale az = new Locale("az"); // Azeri
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        test("\u00DF", turkish, "SS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        test("a\u00DF", turkish, "ASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        test("i", turkish, "\u0130");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        test("i", az, "\u0130");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        test("\u0131", turkish, "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        test("\u00DF", Locale.GERMANY, "SS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        test("a\u00DF", Locale.GERMANY, "ASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        test("i", Locale.GERMANY, "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // test some of the 1:M uppercase mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test("abc\u00DF", Locale.US, "ABC\u0053\u0053");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test("\u0149abc", Locale.US, "\u02BC\u004EABC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        test("\u0149abc", turkish, "\u02BC\u004EABC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        test("\u1F52", Locale.US, "\u03A5\u0313\u0300");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        test("\u0149\u1F52", Locale.US, "\u02BC\u004E\u03A5\u0313\u0300");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        test("\u1F54ZZZ", Locale.US, "\u03A5\u0313\u0301ZZZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        test("\u1F54ZZZ", turkish, "\u03A5\u0313\u0301ZZZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        test("a\u00DF\u1F56", Locale.US, "ASS\u03A5\u0313\u0342");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        test("\u1FAD", turkish, "\u1F6D\u0399");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        test("i\u1FC7", turkish, "\u0130\u0397\u0342\u0399");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        test("i\u1FC7", az, "\u0130\u0397\u0342\u0399");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        test("i\u1FC7", Locale.US, "I\u0397\u0342\u0399");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        test("\uFB04", Locale.US, "\u0046\u0046\u004C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        test("\uFB17AbCdEfi", turkish, "\u0544\u053DABCDEF\u0130");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        test("\uFB17AbCdEfi", az, "\u0544\u053DABCDEF\u0130");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Remove DOT ABOVE after "i" in Lithuanian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        test("i\u0307", lt, "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        test("\u0307", lt, "\u0307");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        test("\u0307i", lt, "\u0307I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        test("j\u0307", lt, "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        test("abci\u0307def", lt, "ABCIDEF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        test("a\u0307", lt, "A\u0307");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        test("abc\u0307def", lt, "ABC\u0307DEF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        test("i\u0307", Locale.US, "I\u0307");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        test("i\u0307", turkish, "\u0130\u0307");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Supplementary character tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // U+10400 ("\uD801\uDC00"): DESERET CAPITAL LETTER LONG I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // U+10401 ("\uD801\uDC01"): DESERET CAPITAL LETTER LONG E
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // U+10402 ("\uD801\uDC02"): DESERET CAPITAL LETTER LONG A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // U+10428 ("\uD801\uDC28"): DESERET SMALL LETTER LONG I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // U+10429 ("\uD801\uDC29"): DESERET SMALL LETTER LONG E
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // U+1042A ("\uD801\uDC2A"): DESERET SMALL LETTER LONG A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // valid code point tests:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        test("\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "\uD801\uDC00\uD801\uDC01\uD801\uDC02");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        test("\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // invalid code point tests:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        test("\uD800\uD800\uD801a\uDC00\uDC00\uDC00b", Locale.US, "\uD800\uD800\uD801A\uDC00\uDC00\uDC00B");
25652
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    94
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    95
        // lower/uppercase + surrogates
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    96
        test("a\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    97
        test("A\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    98
        test("a\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "A\uD801\uDC00\uD801\uDC01\uD801\uDC02");
3fa5768212c4 8042589: String.toLowerCase do not work for some concatenated strings
sherman
parents: 5506
diff changeset
    99
        test("A\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "A\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   100
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   101
        // test latin1 only case
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   102
        StringBuilder src = new StringBuilder(0x100);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   103
        StringBuilder exp = new StringBuilder(0x100);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   104
        for (int cp = 0; cp < 0x100; cp++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   105
            int upperCase = Character.toUpperCase(cp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   106
            if (upperCase == -1) {    //Character.ERROR
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   107
                continue;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   108
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   109
            src.appendCodePoint(cp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   110
            if (cp == '\u00df') {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   111
                exp.append("SS");     // need Character.toUpperCaseEx()
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   112
            } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   113
                exp.appendCodePoint(upperCase);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   114
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   115
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   116
        test(src.toString(), Locale.US, exp.toString());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   117
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   118
        // test non-latin1 -> latin1
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   119
        src = new StringBuilder(0x100).append("ABC");
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   120
        exp = new StringBuilder(0x100).append("ABC");
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   121
        for (int cp = 0x100; cp < 0x10000; cp++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   122
            int upperCase  = Character.toUpperCase(cp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   123
            if (upperCase < 0x100) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   124
                src.appendCodePoint(cp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   125
                exp.appendCodePoint(upperCase);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   126
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   127
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   128
        test(src.toString(), Locale.US, exp.toString());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static void test(String in, Locale locale, String expected) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   133
        test0(in, locale,expected);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   134
        // trigger different code paths
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   135
        for (String[] ss :  new String[][] {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   136
                                new String[] {"abc",      "ABC"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   137
                                new String[] {"AbC",      "ABC"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   138
                                new String[] {"ABC",      "ABC"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   139
                                new String[] {"AB\u4e00", "AB\u4e00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   140
                                new String[] {"ab\u4e00", "AB\u4e00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   141
                                new String[] {"aB\u4e00", "AB\u4e00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   142
                                new String[] {"AB\uD800\uDC00", "AB\uD800\uDC00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   143
                                new String[] {"Ab\uD800\uDC00", "AB\uD800\uDC00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   144
                                new String[] {"ab\uD800\uDC00", "AB\uD800\uDC00"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   145
                                new String[] {"AB\uD801\uDC44", "AB\uD801\uDC1C"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   146
                                new String[] {"Ab\uD801\uDC44", "AB\uD801\uDC1C"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   147
                                new String[] {"ab\uD801\uDC44", "AB\uD801\uDC1C"},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   148
                            }) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   149
            test0(ss[0] + " " + in, locale, ss[1] + " " + expected);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   150
            test0(in + " " + ss[0], locale, expected + " " + ss[1]);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   151
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   152
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   153
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   154
    static void test0(String in, Locale locale, String expected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String result = in.toUpperCase(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!result.equals(expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.err.println("input: " + in + ", locale: " + locale +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    ", expected: " + expected + ", actual: " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25652
diff changeset
   161
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}