test/java/lang/String/ToLowerCase.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25     @test
  26     @bug 4217441 4533872 4900935 8020037 8032012 8041791 8042589
  27     @summary toLowerCase should lower-case Greek Sigma correctly depending
  28              on the context (final/non-final).  Also it should handle
  29              Locale specific (lt, tr, and az) lowercasings and supplementary
  30              characters correctly.
  31 */
  32 
  33 import java.util.Locale;
  34 
  35 public class ToLowerCase {
  36 
  37     public static void main(String[] args) {
  38         Locale turkish = new Locale("tr", "TR");
  39         Locale lt = new Locale("lt"); // Lithanian
  40         Locale az = new Locale("az"); // Azeri
  41 
  42         // Greek Sigma final/non-final tests
  43         test("\u03A3", Locale.US, "\u03C3");
  44         test("LAST\u03A3", Locale.US, "last\u03C2");
  45         test("MID\u03A3DLE", Locale.US, "mid\u03C3dle");
  46         test("WORD1 \u03A3 WORD3", Locale.US, "word1 \u03C3 word3");


 117         StringBuilder exp = new StringBuilder(0x20000);
 118         for (int cp = 0; cp < 0x20000; cp++) {
 119             if (cp >= Character.MIN_HIGH_SURROGATE && cp <= Character.MAX_HIGH_SURROGATE) {
 120                 continue;
 121             }
 122             if (cp == 0x0130) {
 123                 // Although UnicodeData.txt has the lower case char as \u0069, it should be
 124                 // handled with the rules in SpecialCasing.txt, i.e., \u0069\u0307 in
 125                 // non Turkic locales.
 126                 continue;
 127             }
 128             int lowerCase = Character.toLowerCase(cp);
 129             if (lowerCase == -1) {    //Character.ERROR
 130                 continue;
 131             }
 132             src.appendCodePoint(cp);
 133             exp.appendCodePoint(lowerCase);
 134         }
 135         test(src.toString(), Locale.US, exp.toString());
 136 
























 137     }
 138 
 139     static void test(String in, Locale locale, String expected) {






















 140         String result = in.toLowerCase(locale);
 141         if (!result.equals(expected)) {
 142             System.err.println("input: " + in + ", locale: " + locale +
 143                     ", expected: " + expected + ", actual: " + result);
 144             throw new RuntimeException();
 145         }
 146    }
 147 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25     @test
  26     @bug 4217441 4533872 4900935 8020037 8032012 8041791 8042589 8054307
  27     @summary toLowerCase should lower-case Greek Sigma correctly depending
  28              on the context (final/non-final).  Also it should handle
  29              Locale specific (lt, tr, and az) lowercasings and supplementary
  30              characters correctly.
  31 */
  32 
  33 import java.util.Locale;
  34 
  35 public class ToLowerCase {
  36 
  37     public static void main(String[] args) {
  38         Locale turkish = new Locale("tr", "TR");
  39         Locale lt = new Locale("lt"); // Lithanian
  40         Locale az = new Locale("az"); // Azeri
  41 
  42         // Greek Sigma final/non-final tests
  43         test("\u03A3", Locale.US, "\u03C3");
  44         test("LAST\u03A3", Locale.US, "last\u03C2");
  45         test("MID\u03A3DLE", Locale.US, "mid\u03C3dle");
  46         test("WORD1 \u03A3 WORD3", Locale.US, "word1 \u03C3 word3");


 117         StringBuilder exp = new StringBuilder(0x20000);
 118         for (int cp = 0; cp < 0x20000; cp++) {
 119             if (cp >= Character.MIN_HIGH_SURROGATE && cp <= Character.MAX_HIGH_SURROGATE) {
 120                 continue;
 121             }
 122             if (cp == 0x0130) {
 123                 // Although UnicodeData.txt has the lower case char as \u0069, it should be
 124                 // handled with the rules in SpecialCasing.txt, i.e., \u0069\u0307 in
 125                 // non Turkic locales.
 126                 continue;
 127             }
 128             int lowerCase = Character.toLowerCase(cp);
 129             if (lowerCase == -1) {    //Character.ERROR
 130                 continue;
 131             }
 132             src.appendCodePoint(cp);
 133             exp.appendCodePoint(lowerCase);
 134         }
 135         test(src.toString(), Locale.US, exp.toString());
 136 
 137         // test latin1
 138         src = new StringBuilder(0x100);
 139         exp = new StringBuilder(0x100);
 140         for (int cp = 0; cp < 0x100; cp++) {
 141             int lowerCase = Character.toLowerCase(cp);
 142             if (lowerCase == -1) {    //Character.ERROR
 143                 continue;
 144             }
 145             src.appendCodePoint(cp);
 146             exp.appendCodePoint(lowerCase);
 147         }
 148         test(src.toString(), Locale.US, exp.toString());
 149 
 150         // test non-latin1 -> latin1
 151         src = new StringBuilder(0x100).append("abc");
 152         exp = new StringBuilder(0x100).append("abc");
 153         for (int cp = 0x100; cp < 0x10000; cp++) {
 154             int lowerCase  = Character.toLowerCase(cp);
 155             if (lowerCase < 0x100 && cp != '\u0130') {
 156                 src.appendCodePoint(cp);
 157                 exp.appendCodePoint(lowerCase);
 158             }
 159         }
 160         test(src.toString(), Locale.US, exp.toString());
 161     }
 162 
 163     static void test(String in, Locale locale, String expected) {
 164         test0(in, locale,expected);
 165         for (String[] ss :  new String[][] {
 166                                 new String[] {"abc",      "abc"},
 167                                 new String[] {"aBc",      "abc"},
 168                                 new String[] {"ABC",      "abc"},
 169                                 new String[] {"ab\u4e00", "ab\u4e00"},
 170                                 new String[] {"aB\u4e00", "ab\u4e00"},
 171                                 new String[] {"AB\u4e00", "ab\u4e00"},
 172                                 new String[] {"ab\uD800\uDC00", "ab\uD800\uDC00"},
 173                                 new String[] {"aB\uD800\uDC00", "ab\uD800\uDC00"},
 174                                 new String[] {"AB\uD800\uDC00", "ab\uD800\uDC00"},
 175                                 new String[] {"ab\uD801\uDC1C", "ab\uD801\uDC44"},
 176                                 new String[] {"aB\uD801\uDC1C", "ab\uD801\uDC44"},
 177                                 new String[] {"AB\uD801\uDC1C", "ab\uD801\uDC44"},
 178 
 179                             }) {
 180             test0(ss[0] + " " + in, locale, ss[1] + " " + expected);
 181             test0(in + " " + ss[0], locale, expected + " " + ss[1]);
 182         }
 183     }
 184 
 185     static void test0(String in, Locale locale, String expected) {
 186         String result = in.toLowerCase(locale);
 187         if (!result.equals(expected)) {
 188             System.err.println("input: " + in + ", locale: " + locale +
 189                     ", expected: " + expected + ", actual: " + result);
 190             throw new RuntimeException();
 191         }
 192     }
 193 }