Path: blob/master/test/jdk/java/lang/String/ToLowerCase.java
41152 views
/*1* Copyright (c) 2003, 2014, 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 4217441 4533872 4900935 8020037 8032012 8041791 8042589 805430726@summary toLowerCase should lower-case Greek Sigma correctly depending27on the context (final/non-final). Also it should handle28Locale specific (lt, tr, and az) lowercasings and supplementary29characters correctly.30*/3132import java.util.Locale;3334public class ToLowerCase {3536public static void main(String[] args) {37Locale turkish = new Locale("tr", "TR");38Locale lt = new Locale("lt"); // Lithanian39Locale az = new Locale("az"); // Azeri4041// Greek Sigma final/non-final tests42test("\u03A3", Locale.US, "\u03C3");43test("LAST\u03A3", Locale.US, "last\u03C2");44test("MID\u03A3DLE", Locale.US, "mid\u03C3dle");45test("WORD1 \u03A3 WORD3", Locale.US, "word1 \u03C3 word3");46test("WORD1 LAST\u03A3 WORD3", Locale.US, "word1 last\u03C2 word3");47test("WORD1 MID\u03A3DLE WORD3", Locale.US, "word1 mid\u03C3dle word3");48test("\u0399\u0395\u03a3\u03a5\u03a3 \u03a7\u03a1\u0399\u03a3\u03a4\u039f\u03a3", Locale.US,49"\u03b9\u03b5\u03c3\u03c5\u03c2 \u03c7\u03c1\u03b9\u03c3\u03c4\u03bf\u03c2"); // "IESUS XRISTOS"5051// Explicit dot above for I's and J's whenever there are more accents above (Lithanian)52test("I", lt, "i");53test("I\u0300", lt, "i\u0307\u0300"); // "I" followed by COMBINING GRAVE ACCENT (cc==230)54test("I\u0316", lt, "i\u0316"); // "I" followed by COMBINING GRAVE ACCENT BELOW (cc!=230)55test("J", lt, "j");56test("J\u0300", lt, "j\u0307\u0300"); // "J" followed by COMBINING GRAVE ACCENT (cc==230)57test("J\u0316", lt, "j\u0316"); // "J" followed by COMBINING GRAVE ACCENT BELOW (cc!=230)58test("\u012E", lt, "\u012F");59test("\u012E\u0300", lt, "\u012F\u0307\u0300"); // "I (w/ OGONEK)" followed by COMBINING GRAVE ACCENT (cc==230)60test("\u012E\u0316", lt, "\u012F\u0316"); // "I (w/ OGONEK)" followed by COMBINING GRAVE ACCENT BELOW (cc!=230)61test("\u00CC", lt, "i\u0307\u0300");62test("\u00CD", lt, "i\u0307\u0301");63test("\u0128", lt, "i\u0307\u0303");64test("I\u0300", Locale.US, "i\u0300"); // "I" followed by COMBINING GRAVE ACCENT (cc==230)65test("J\u0300", Locale.US, "j\u0300"); // "J" followed by COMBINING GRAVE ACCENT (cc==230)66test("\u012E\u0300", Locale.US, "\u012F\u0300"); // "I (w/ OGONEK)" followed by COMBINING GRAVE ACCENT (cc==230)67test("\u00CC", Locale.US, "\u00EC");68test("\u00CD", Locale.US, "\u00ED");69test("\u0128", Locale.US, "\u0129");7071// I-dot tests72test("\u0130", turkish, "i");73test("\u0130", az, "i");74test("\u0130", lt, "\u0069\u0307");75test("\u0130", Locale.US, "\u0069\u0307");76test("\u0130", Locale.JAPAN, "\u0069\u0307");77test("\u0130", Locale.ROOT, "\u0069\u0307");7879// Remove dot_above in the sequence I + dot_above (Turkish and Azeri)80test("I\u0307", turkish, "i");81test("I\u0307", az, "i");82test("J\u0307", turkish, "j\u0307");83test("J\u0307", az, "j\u0307");8485// Unless an I is before a dot_above, it turns into a dotless i (Turkish and Azeri)86test("I", turkish, "\u0131");87test("I", az, "\u0131");88test("I", Locale.US, "i");89test("IABC", turkish, "\u0131abc");90test("IABC", az, "\u0131abc");91test("IABC", Locale.US, "iabc");9293// Supplementary character tests94//95// U+10400 ("\uD801\uDC00"): DESERET CAPITAL LETTER LONG I96// U+10401 ("\uD801\uDC01"): DESERET CAPITAL LETTER LONG E97// U+10402 ("\uD801\uDC02"): DESERET CAPITAL LETTER LONG A98// U+10428 ("\uD801\uDC28"): DESERET SMALL LETTER LONG I99// U+10429 ("\uD801\uDC29"): DESERET SMALL LETTER LONG E100// U+1042A ("\uD801\uDC2A"): DESERET SMALL LETTER LONG A101//102// valid code point tests:103test("\uD801\uDC00\uD801\uDC01\uD801\uDC02", Locale.US, "\uD801\uDC28\uD801\uDC29\uD801\uDC2A");104test("\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C", Locale.US, "\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac");105// invalid code point tests:106test("\uD800\uD800\uD801A\uDC00\uDC00\uDC00B", Locale.US, "\uD800\uD800\uD801a\uDC00\uDC00\uDC00b");107108// lower/uppercase + surrogates109test("a\uD801\uDC1c", Locale.ROOT, "a\uD801\uDC44");110test("A\uD801\uDC1c", Locale.ROOT, "a\uD801\uDC44");111test("a\uD801\uDC00\uD801\uDC01\uD801\uDC02", Locale.US, "a\uD801\uDC28\uD801\uDC29\uD801\uDC2A");112test("A\uD801\uDC00\uD801\uDC01\uD801\uDC02", Locale.US, "a\uD801\uDC28\uD801\uDC29\uD801\uDC2A");113114// test bmp + supp1115StringBuilder src = new StringBuilder(0x20000);116StringBuilder exp = new StringBuilder(0x20000);117for (int cp = 0; cp < 0x20000; cp++) {118if (cp >= Character.MIN_HIGH_SURROGATE && cp <= Character.MAX_HIGH_SURROGATE) {119continue;120}121if (cp == 0x0130) {122// Although UnicodeData.txt has the lower case char as \u0069, it should be123// handled with the rules in SpecialCasing.txt, i.e., \u0069\u0307 in124// non Turkic locales.125continue;126}127int lowerCase = Character.toLowerCase(cp);128if (lowerCase == -1) { //Character.ERROR129continue;130}131src.appendCodePoint(cp);132exp.appendCodePoint(lowerCase);133}134test(src.toString(), Locale.US, exp.toString());135136// test latin1137src = new StringBuilder(0x100);138exp = new StringBuilder(0x100);139for (int cp = 0; cp < 0x100; cp++) {140int lowerCase = Character.toLowerCase(cp);141if (lowerCase == -1) { //Character.ERROR142continue;143}144src.appendCodePoint(cp);145exp.appendCodePoint(lowerCase);146}147test(src.toString(), Locale.US, exp.toString());148149// test non-latin1 -> latin1150src = new StringBuilder(0x100).append("abc");151exp = new StringBuilder(0x100).append("abc");152for (int cp = 0x100; cp < 0x10000; cp++) {153int lowerCase = Character.toLowerCase(cp);154if (lowerCase < 0x100 && cp != '\u0130') {155src.appendCodePoint(cp);156exp.appendCodePoint(lowerCase);157}158}159test(src.toString(), Locale.US, exp.toString());160}161162static void test(String in, Locale locale, String expected) {163test0(in, locale,expected);164for (String[] ss : new String[][] {165new String[] {"abc", "abc"},166new String[] {"aBc", "abc"},167new String[] {"ABC", "abc"},168new String[] {"ab\u4e00", "ab\u4e00"},169new String[] {"aB\u4e00", "ab\u4e00"},170new String[] {"AB\u4e00", "ab\u4e00"},171new String[] {"ab\uD800\uDC00", "ab\uD800\uDC00"},172new String[] {"aB\uD800\uDC00", "ab\uD800\uDC00"},173new String[] {"AB\uD800\uDC00", "ab\uD800\uDC00"},174new String[] {"ab\uD801\uDC1C", "ab\uD801\uDC44"},175new String[] {"aB\uD801\uDC1C", "ab\uD801\uDC44"},176new String[] {"AB\uD801\uDC1C", "ab\uD801\uDC44"},177178}) {179test0(ss[0] + " " + in, locale, ss[1] + " " + expected);180test0(in + " " + ss[0], locale, expected + " " + ss[1]);181}182}183184static void test0(String in, Locale locale, String expected) {185String result = in.toLowerCase(locale);186if (!result.equals(expected)) {187System.err.println("input: " + in + ", locale: " + locale +188", expected: " + expected + ", actual: " + result);189throw new RuntimeException();190}191}192}193194195