Path: blob/master/test/jdk/java/lang/String/ToUpperCase.java
41149 views
/*1* Copyright (c) 2000, 2003, 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@test25@bug 4219630 4304573 4533872 4900935 8042589 805430726@summary toUpperCase should upper-case German sharp s correctly even if27it's the only character in the string. should also uppercase28all of the 1:M char mappings correctly. Also it should handle29Locale specific (lt, tr, and az) uppercasings and supplementary30characters correctly.31*/3233import java.util.Locale;3435public class ToUpperCase {3637public static void main(String[] args) {38Locale turkish = new Locale("tr", "TR");39Locale lt = new Locale("lt"); // Lithanian40Locale az = new Locale("az"); // Azeri4142test("\u00DF", turkish, "SS");43test("a\u00DF", turkish, "ASS");44test("i", turkish, "\u0130");45test("i", az, "\u0130");46test("\u0131", turkish, "I");47test("\u00DF", Locale.GERMANY, "SS");48test("a\u00DF", Locale.GERMANY, "ASS");49test("i", Locale.GERMANY, "I");5051// test some of the 1:M uppercase mappings52test("abc\u00DF", Locale.US, "ABC\u0053\u0053");53test("\u0149abc", Locale.US, "\u02BC\u004EABC");54test("\u0149abc", turkish, "\u02BC\u004EABC");55test("\u1F52", Locale.US, "\u03A5\u0313\u0300");56test("\u0149\u1F52", Locale.US, "\u02BC\u004E\u03A5\u0313\u0300");57test("\u1F54ZZZ", Locale.US, "\u03A5\u0313\u0301ZZZ");58test("\u1F54ZZZ", turkish, "\u03A5\u0313\u0301ZZZ");59test("a\u00DF\u1F56", Locale.US, "ASS\u03A5\u0313\u0342");60test("\u1FAD", turkish, "\u1F6D\u0399");61test("i\u1FC7", turkish, "\u0130\u0397\u0342\u0399");62test("i\u1FC7", az, "\u0130\u0397\u0342\u0399");63test("i\u1FC7", Locale.US, "I\u0397\u0342\u0399");64test("\uFB04", Locale.US, "\u0046\u0046\u004C");65test("\uFB17AbCdEfi", turkish, "\u0544\u053DABCDEF\u0130");66test("\uFB17AbCdEfi", az, "\u0544\u053DABCDEF\u0130");6768// Remove DOT ABOVE after "i" in Lithuanian69test("i\u0307", lt, "I");70test("\u0307", lt, "\u0307");71test("\u0307i", lt, "\u0307I");72test("j\u0307", lt, "J");73test("abci\u0307def", lt, "ABCIDEF");74test("a\u0307", lt, "A\u0307");75test("abc\u0307def", lt, "ABC\u0307DEF");76test("i\u0307", Locale.US, "I\u0307");77test("i\u0307", turkish, "\u0130\u0307");7879// Supplementary character tests80//81// U+10400 ("\uD801\uDC00"): DESERET CAPITAL LETTER LONG I82// U+10401 ("\uD801\uDC01"): DESERET CAPITAL LETTER LONG E83// U+10402 ("\uD801\uDC02"): DESERET CAPITAL LETTER LONG A84// U+10428 ("\uD801\uDC28"): DESERET SMALL LETTER LONG I85// U+10429 ("\uD801\uDC29"): DESERET SMALL LETTER LONG E86// U+1042A ("\uD801\uDC2A"): DESERET SMALL LETTER LONG A87//88// valid code point tests:89test("\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "\uD801\uDC00\uD801\uDC01\uD801\uDC02");90test("\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");91// invalid code point tests:92test("\uD800\uD800\uD801a\uDC00\uDC00\uDC00b", Locale.US, "\uD800\uD800\uD801A\uDC00\uDC00\uDC00B");9394// lower/uppercase + surrogates95test("a\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");96test("A\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");97test("a\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "A\uD801\uDC00\uD801\uDC01\uD801\uDC02");98test("A\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "A\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");99100// test latin1 only case101StringBuilder src = new StringBuilder(0x100);102StringBuilder exp = new StringBuilder(0x100);103for (int cp = 0; cp < 0x100; cp++) {104int upperCase = Character.toUpperCase(cp);105if (upperCase == -1) { //Character.ERROR106continue;107}108src.appendCodePoint(cp);109if (cp == '\u00df') {110exp.append("SS"); // need Character.toUpperCaseEx()111} else {112exp.appendCodePoint(upperCase);113}114}115test(src.toString(), Locale.US, exp.toString());116117// test non-latin1 -> latin1118src = new StringBuilder(0x100).append("ABC");119exp = new StringBuilder(0x100).append("ABC");120for (int cp = 0x100; cp < 0x10000; cp++) {121int upperCase = Character.toUpperCase(cp);122if (upperCase < 0x100) {123src.appendCodePoint(cp);124exp.appendCodePoint(upperCase);125}126}127test(src.toString(), Locale.US, exp.toString());128129}130131static void test(String in, Locale locale, String expected) {132test0(in, locale,expected);133// trigger different code paths134for (String[] ss : new String[][] {135new String[] {"abc", "ABC"},136new String[] {"AbC", "ABC"},137new String[] {"ABC", "ABC"},138new String[] {"AB\u4e00", "AB\u4e00"},139new String[] {"ab\u4e00", "AB\u4e00"},140new String[] {"aB\u4e00", "AB\u4e00"},141new String[] {"AB\uD800\uDC00", "AB\uD800\uDC00"},142new String[] {"Ab\uD800\uDC00", "AB\uD800\uDC00"},143new String[] {"ab\uD800\uDC00", "AB\uD800\uDC00"},144new String[] {"AB\uD801\uDC44", "AB\uD801\uDC1C"},145new String[] {"Ab\uD801\uDC44", "AB\uD801\uDC1C"},146new String[] {"ab\uD801\uDC44", "AB\uD801\uDC1C"},147}) {148test0(ss[0] + " " + in, locale, ss[1] + " " + expected);149test0(in + " " + ss[0], locale, expected + " " + ss[1]);150}151}152153static void test0(String in, Locale locale, String expected) {154String result = in.toUpperCase(locale);155if (!result.equals(expected)) {156System.err.println("input: " + in + ", locale: " + locale +157", expected: " + expected + ", actual: " + result);158throw new RuntimeException();159}160}161}162163164