CharacterTest.java revision 63744c884dd4b4f4307f2b021fb894af164972af
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.lang;
18
19import java.lang.reflect.Method;
20
21public class CharacterTest extends junit.framework.TestCase {
22  public void test_valueOfC() {
23    // The JLS requires caching for chars between "\u0000 to \u007f":
24    // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7
25    // Harmony caches 0-512 and tests for this behavior, so we suppress that test and use this.
26    for (char c = '\u0000'; c <= '\u007f'; ++c) {
27      Character e = new Character(c);
28      Character a = Character.valueOf(c);
29      assertEquals(e, a);
30      assertSame(Character.valueOf(c), Character.valueOf(c));
31    }
32    for (int c = '\u0080'; c <= Character.MAX_VALUE; ++c) {
33      assertEquals(new Character((char) c), Character.valueOf((char) c));
34    }
35  }
36
37  public void test_isBmpCodePoint() throws Exception {
38    assertTrue(Character.isBmpCodePoint(0x0000));
39    assertTrue(Character.isBmpCodePoint(0x0666));
40    assertTrue(Character.isBmpCodePoint(0xffff));
41    assertFalse(Character.isBmpCodePoint(0x10000));
42    assertFalse(Character.isBmpCodePoint(-1));
43    assertFalse(Character.isBmpCodePoint(Integer.MAX_VALUE));
44    assertFalse(Character.isBmpCodePoint(Integer.MIN_VALUE));
45  }
46
47  public void test_isSurrogate() throws Exception {
48    assertFalse(Character.isSurrogate('\u0000'));
49    assertFalse(Character.isSurrogate('\u0666'));
50    assertFalse(Character.isSurrogate((char) (Character.MIN_SURROGATE - 1)));
51    for (char ch = Character.MIN_SURROGATE; ch <= Character.MAX_SURROGATE; ++ch) {
52      assertTrue(Character.isSurrogate(ch));
53    }
54    assertFalse(Character.isSurrogate((char) (Character.MAX_SURROGATE + 1)));
55  }
56
57  public void test_highSurrogate() throws Exception {
58    // The behavior for non-supplementary code points (like these two) is undefined.
59    // These are the obvious results if you don't do anything special.
60    assertEquals(0xd7c0, Character.highSurrogate(0x0000));
61    assertEquals(0xd7c1, Character.highSurrogate(0x0666));
62    // These two tests must pass, though.
63    assertEquals(0xd800, Character.highSurrogate(0x010000));
64    assertEquals(0xdbff, Character.highSurrogate(0x10ffff));
65  }
66
67  public void test_lowSurrogate() throws Exception {
68    // The behavior for non-supplementary code points (like these two) is undefined.
69    // These are the obvious results if you don't do anything special.
70    assertEquals(0xdc00, Character.lowSurrogate(0x0000));
71    assertEquals(0xde66, Character.lowSurrogate(0x0666));
72    // These two tests must pass, though.
73    assertEquals(0xdc00, Character.lowSurrogate(0x010000));
74    assertEquals(0xdfff, Character.lowSurrogate(0x10ffff));
75  }
76
77  public void test_getName() throws Exception {
78    // Character.getName requires the corresponding ICU data.
79    // Changed from "NULL" and "BELL" by Unicode 49.2
80    assertEquals("<control-0000>", Character.getName(0x0000));
81    assertEquals("<control-0007>", Character.getName(0x0007));
82    assertEquals("LATIN SMALL LETTER L", Character.getName('l'));
83    // This changed name from Unicode 1.0. Used to be "OPENING...".
84    assertEquals("LEFT CURLY BRACKET", Character.getName('{'));
85    assertEquals("ARABIC-INDIC DIGIT SIX", Character.getName(0x0666));
86    assertEquals("LINEAR B SYLLABLE B008 A", Character.getName(0x010000));
87
88    // Some private use code points.
89    assertEquals("PRIVATE USE AREA E000", Character.getName(0xe000));
90    assertEquals("SUPPLEMENTARY PRIVATE USE AREA A F0000", Character.getName(0xf0000));
91
92    // An unassigned code point.
93    assertNull(Character.getName(0x10ffff));
94
95    try {
96      Character.getName(-1);
97      fail();
98    } catch (IllegalArgumentException expected) {
99    }
100    try {
101      Character.getName(Integer.MAX_VALUE);
102      fail();
103    } catch (IllegalArgumentException expected) {
104    }
105    try {
106      Character.getName(Integer.MIN_VALUE);
107      fail();
108    } catch (IllegalArgumentException expected) {
109    }
110  }
111
112  public void test_compare() throws Exception {
113    assertEquals(0, Character.compare('a', 'a'));
114    assertTrue(Character.compare('a', 'b') < 0);
115    assertTrue(Character.compare('b', 'a') > 0);
116  }
117
118  public void test_UnicodeBlock_all() throws Exception {
119    for (int i = 0; i <= 0x100000; ++i) {
120      Character.UnicodeBlock.of(i);
121    }
122  }
123
124  public void test_UnicodeBlock_of() throws Exception {
125    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.of(1));
126    assertEquals(Character.UnicodeBlock.HANGUL_JAMO, Character.UnicodeBlock.of(0x1100));
127    assertEquals(Character.UnicodeBlock.CYPRIOT_SYLLABARY, Character.UnicodeBlock.of(0x10800));
128    assertEquals(Character.UnicodeBlock.VARIATION_SELECTORS_SUPPLEMENT, Character.UnicodeBlock.of(0xe0100));
129    // Unicode 4.1.
130    assertEquals(Character.UnicodeBlock.ANCIENT_GREEK_MUSICAL_NOTATION, Character.UnicodeBlock.of(0x1d200));
131    // Unicode 5.0.
132    assertEquals(Character.UnicodeBlock.NKO, Character.UnicodeBlock.of(0x07c0));
133    // Unicode 5.1.
134    assertEquals(Character.UnicodeBlock.SUNDANESE, Character.UnicodeBlock.of(0x1b80));
135    // Unicode 5.2.
136    assertEquals(Character.UnicodeBlock.SAMARITAN, Character.UnicodeBlock.of(0x0800));
137    // Unicode 6.0.
138    assertEquals(Character.UnicodeBlock.MANDAIC, Character.UnicodeBlock.of(0x0840));
139  }
140
141  public void test_UnicodeBlock_forName() throws Exception {
142    // No negative tests here because icu4c is more lenient than the RI;
143    // we'd allow "basic-latin", and "hangul jamo extended b", for example.
144    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic latin"));
145    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BaSiC LaTiN"));
146    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BasicLatin"));
147    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BASIC_LATIN"));
148    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic_LATIN"));
149
150    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL_JAMO_EXTENDED_B"));
151    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL JAMO EXTENDED-B"));
152
153    // Failure cases.
154    try {
155      Character.UnicodeBlock.forName(null);
156      fail();
157    } catch (NullPointerException expected) {
158    }
159    try {
160      Character.UnicodeBlock.forName("this unicode block does not exist");
161      fail();
162    } catch (IllegalArgumentException expected) {
163    }
164
165    // Renamed blocks.
166    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek"));
167    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek And Coptic"));
168    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Marks For Symbols"));
169    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Diacritical Marks For Symbols"));
170    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("COMBINING_MARKS_FOR_SYMBOLS"));
171    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Marks for Symbols"));
172    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("CombiningMarksforSymbols"));
173    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplementary"));
174    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplement"));
175  }
176
177  public void test_isAlphabetic() throws Exception {
178    assertTrue(Character.isAlphabetic('A'));
179    assertTrue(Character.isAlphabetic('a'));
180    assertFalse(Character.isAlphabetic('1'));
181    assertTrue(Character.isAlphabetic(0x113c)); // Hangul j
182  }
183
184  public void test_isIdeographic() throws Exception {
185    assertFalse(Character.isIdeographic('A'));
186    assertFalse(Character.isIdeographic('a'));
187    assertFalse(Character.isIdeographic('1'));
188    assertFalse(Character.isIdeographic(0x113c)); // Hangul j
189
190    assertTrue(Character.isIdeographic(0x4db5));
191    assertTrue(Character.isIdeographic(0x2f999));
192    assertFalse(Character.isIdeographic(0x2f99)); // Kangxi radical shell
193  }
194
195  // http://b/9690863
196  public void test_isDigit_against_icu4c() throws Exception {
197    Method m = Character.class.getDeclaredMethod("isDigit" + "Impl", int.class);
198    m.setAccessible(true);
199    for (int i = 0; i <= 0xffff; ++i) {
200      assertEquals(m.invoke(null, i), Character.isDigit(i));
201    }
202  }
203
204  // http://b/9690863
205  public void test_isIdentifierIgnorable_against_icu4c() throws Exception {
206    Method m = Character.class.getDeclaredMethod("isIdentifierIgnorable" + "Impl", int.class);
207    m.setAccessible(true);
208    for (int i = 0; i <= 0xffff; ++i) {
209      assertEquals(m.invoke(null, i), Character.isIdentifierIgnorable(i));
210    }
211  }
212
213  // http://b/9690863
214  public void test_isLetter_against_icu4c() throws Exception {
215    Method m = Character.class.getDeclaredMethod("isLetter" + "Impl", int.class);
216    m.setAccessible(true);
217    for (int i = 0; i <= 0xffff; ++i) {
218      assertEquals(m.invoke(null, i), Character.isLetter(i));
219    }
220  }
221
222  // http://b/9690863
223  public void test_isLetterOrDigit_against_icu4c() throws Exception {
224    Method m = Character.class.getDeclaredMethod("isLetterOrDigit" + "Impl", int.class);
225    m.setAccessible(true);
226    for (int i = 0; i <= 0xffff; ++i) {
227      assertEquals(m.invoke(null, i), Character.isLetterOrDigit(i));
228    }
229  }
230
231  // http://b/9690863
232  public void test_isLowerCase_against_icu4c() throws Exception {
233    Method m = Character.class.getDeclaredMethod("isLowerCase" + "Impl", int.class);
234    m.setAccessible(true);
235    for (int i = 0; i <= 0xffff; ++i) {
236      assertEquals(m.invoke(null, i), Character.isLowerCase(i));
237    }
238  }
239
240  // http://b/9690863
241  public void test_isSpaceChar_against_icu4c() throws Exception {
242    Method m = Character.class.getDeclaredMethod("isSpaceChar" + "Impl", int.class);
243    m.setAccessible(true);
244    for (int i = 0; i <= 0xffff; ++i) {
245      // ICU and the RI disagree about character 0x180e. Remove this special case if this changes
246      // or Android decides to follow ICU exactly.
247      if (i == 0x180e) {
248        assertTrue(Character.isSpaceChar(i));
249        assertFalse((Boolean) m.invoke(null, i));
250      } else {
251        assertEquals("Failed for character " + i, m.invoke(null, i), Character.isSpaceChar(i));
252      }
253    }
254  }
255
256  // http://b/9690863
257  public void test_isUpperCase_against_icu4c() throws Exception {
258    Method m = Character.class.getDeclaredMethod("isUpperCase" + "Impl", int.class);
259    m.setAccessible(true);
260    for (int i = 0; i <= 0xffff; ++i) {
261      assertEquals(m.invoke(null, i), Character.isUpperCase(i));
262    }
263  }
264
265  // http://b/9690863
266  public void test_isWhitespace_against_icu4c() throws Exception {
267    Method m = Character.class.getDeclaredMethod("isWhitespace" + "Impl", int.class);
268    m.setAccessible(true);
269    for (int i = 0; i <= 0xffff; ++i) {
270      // ICU and the RI disagree about character 0x180e. Remove this special case if this changes
271      // or Android decides to follow ICU exactly.
272      if (i == 0x180e) {
273          assertTrue(Character.isWhitespace(i));
274          assertFalse((Boolean) m.invoke(null, i));
275      } else {
276        assertEquals("Failed for character " + i, m.invoke(null, i), Character.isWhitespace(i));
277      }
278    }
279  }
280}
281