CharacterTest.java revision 35edbd5fca5f4180fe7a57101a38ea01a2d9cc62
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
19public class CharacterTest extends junit.framework.TestCase {
20  public void test_valueOfC() {
21    // The JLS requires caching for chars between "\u0000 to \u007f":
22    // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7
23    // Harmony caches 0-512 and tests for this behavior, so we suppress that test and use this.
24    for (char c = '\u0000'; c <= '\u007f'; ++c) {
25      Character e = new Character(c);
26      Character a = Character.valueOf(c);
27      assertEquals(e, a);
28      assertSame(Character.valueOf(c), Character.valueOf(c));
29    }
30    for (int c = '\u0080'; c <= Character.MAX_VALUE; ++c) {
31      assertEquals(new Character((char) c), Character.valueOf((char) c));
32    }
33  }
34
35  public void test_isBmpCodePoint() throws Exception {
36    assertTrue(Character.isBmpCodePoint(0x0000));
37    assertTrue(Character.isBmpCodePoint(0x0666));
38    assertTrue(Character.isBmpCodePoint(0xffff));
39    assertFalse(Character.isBmpCodePoint(0x10000));
40    assertFalse(Character.isBmpCodePoint(-1));
41    assertFalse(Character.isBmpCodePoint(Integer.MAX_VALUE));
42    assertFalse(Character.isBmpCodePoint(Integer.MIN_VALUE));
43  }
44
45  public void test_isSurrogate() throws Exception {
46    assertFalse(Character.isSurrogate('\u0000'));
47    assertFalse(Character.isSurrogate('\u0666'));
48    assertFalse(Character.isSurrogate((char) (Character.MIN_SURROGATE - 1)));
49    for (char ch = Character.MIN_SURROGATE; ch <= Character.MAX_SURROGATE; ++ch) {
50      assertTrue(Character.isSurrogate(ch));
51    }
52    assertFalse(Character.isSurrogate((char) (Character.MAX_SURROGATE + 1)));
53  }
54
55  public void test_highSurrogate() throws Exception {
56    // The behavior for non-supplementary code points (like these two) is undefined.
57    // These are the obvious results if you don't do anything special.
58    assertEquals(0xd7c0, Character.highSurrogate(0x0000));
59    assertEquals(0xd7c1, Character.highSurrogate(0x0666));
60    // These two tests must pass, though.
61    assertEquals(0xd800, Character.highSurrogate(0x010000));
62    assertEquals(0xdbff, Character.highSurrogate(0x10ffff));
63  }
64
65  public void test_lowSurrogate() throws Exception {
66    // The behavior for non-supplementary code points (like these two) is undefined.
67    // These are the obvious results if you don't do anything special.
68    assertEquals(0xdc00, Character.lowSurrogate(0x0000));
69    assertEquals(0xde66, Character.lowSurrogate(0x0666));
70    // These two tests must pass, though.
71    assertEquals(0xdc00, Character.lowSurrogate(0x010000));
72    assertEquals(0xdfff, Character.lowSurrogate(0x10ffff));
73  }
74
75  public void test_getName() throws Exception {
76    // Character.getName requires the corresponding ICU data.
77    // Changed from "NULL" and "BELL" by Unicode 49.2
78    assertEquals("<control-0000>", Character.getName(0x0000));
79    assertEquals("<control-0007>", Character.getName(0x0007));
80    assertEquals("LATIN SMALL LETTER L", Character.getName('l'));
81    // This changed name from Unicode 1.0. Used to be "OPENING...".
82    assertEquals("LEFT CURLY BRACKET", Character.getName('{'));
83    assertEquals("ARABIC-INDIC DIGIT SIX", Character.getName(0x0666));
84    assertEquals("LINEAR B SYLLABLE B008 A", Character.getName(0x010000));
85
86    // Some private use code points.
87    assertEquals("PRIVATE USE AREA E000", Character.getName(0xe000));
88    assertEquals("SUPPLEMENTARY PRIVATE USE AREA A F0000", Character.getName(0xf0000));
89
90    // An unassigned code point.
91    assertNull(Character.getName(0x10ffff));
92
93    try {
94      Character.getName(-1);
95      fail();
96    } catch (IllegalArgumentException expected) {
97    }
98    try {
99      Character.getName(Integer.MAX_VALUE);
100      fail();
101    } catch (IllegalArgumentException expected) {
102    }
103    try {
104      Character.getName(Integer.MIN_VALUE);
105      fail();
106    } catch (IllegalArgumentException expected) {
107    }
108  }
109
110  public void test_compare() throws Exception {
111    assertEquals(0, Character.compare('a', 'a'));
112    assertTrue(Character.compare('a', 'b') < 0);
113    assertTrue(Character.compare('b', 'a') > 0);
114  }
115
116  public void test_UnicodeBlock_all() throws Exception {
117    for (int i = 0; i <= 0x100000; ++i) {
118      Character.UnicodeBlock.of(i);
119    }
120  }
121
122  public void test_UnicodeBlock_of() throws Exception {
123    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.of(1));
124    assertEquals(Character.UnicodeBlock.HANGUL_JAMO, Character.UnicodeBlock.of(0x1100));
125    assertEquals(Character.UnicodeBlock.CYPRIOT_SYLLABARY, Character.UnicodeBlock.of(0x10800));
126    assertEquals(Character.UnicodeBlock.VARIATION_SELECTORS_SUPPLEMENT, Character.UnicodeBlock.of(0xe0100));
127    // Unicode 4.1.
128    assertEquals(Character.UnicodeBlock.ANCIENT_GREEK_MUSICAL_NOTATION, Character.UnicodeBlock.of(0x1d200));
129    // Unicode 5.0.
130    assertEquals(Character.UnicodeBlock.NKO, Character.UnicodeBlock.of(0x07c0));
131    // Unicode 5.1.
132    assertEquals(Character.UnicodeBlock.SUNDANESE, Character.UnicodeBlock.of(0x1b80));
133    // Unicode 5.2.
134    assertEquals(Character.UnicodeBlock.SAMARITAN, Character.UnicodeBlock.of(0x0800));
135    // Unicode 6.0.
136    assertEquals(Character.UnicodeBlock.MANDAIC, Character.UnicodeBlock.of(0x0840));
137  }
138
139  public void test_UnicodeBlock_forName() throws Exception {
140    // No negative tests here because icu4c is more lenient than the RI;
141    // we'd allow "basic-latin", and "hangul jamo extended b", for example.
142    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic latin"));
143    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BaSiC LaTiN"));
144    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BasicLatin"));
145    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BASIC_LATIN"));
146    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic_LATIN"));
147
148    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL_JAMO_EXTENDED_B"));
149    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL JAMO EXTENDED-B"));
150
151    // Failure cases.
152    try {
153      Character.UnicodeBlock.forName(null);
154      fail();
155    } catch (NullPointerException expected) {
156    }
157    try {
158      Character.UnicodeBlock.forName("this unicode block does not exist");
159      fail();
160    } catch (IllegalArgumentException expected) {
161    }
162
163    // Renamed blocks.
164    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek"));
165    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek And Coptic"));
166    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Marks For Symbols"));
167    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Diacritical Marks For Symbols"));
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 Marks for Symbols"));
170    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("CombiningMarksforSymbols"));
171    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplementary"));
172    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplement"));
173  }
174}
175