summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/lang/CharacterTest.java
blob: 8c6f06fbd17ba07723652e4e89f9bf02f1f3ef6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.lang;

import java.lang.reflect.Method;

public class CharacterTest extends junit.framework.TestCase {
  public void test_valueOfC() {
    // The JLS requires caching for chars between "\u0000 to \u007f":
    // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7
    // Harmony caches 0-512 and tests for this behavior, so we suppress that test and use this.
    for (char c = '\u0000'; c <= '\u007f'; ++c) {
      Character e = new Character(c);
      Character a = Character.valueOf(c);
      assertEquals(e, a);
      assertSame(Character.valueOf(c), Character.valueOf(c));
    }
    for (int c = '\u0080'; c <= Character.MAX_VALUE; ++c) {
      assertEquals(new Character((char) c), Character.valueOf((char) c));
    }
  }

  public void test_isBmpCodePoint() throws Exception {
    assertTrue(Character.isBmpCodePoint(0x0000));
    assertTrue(Character.isBmpCodePoint(0x0666));
    assertTrue(Character.isBmpCodePoint(0xffff));
    assertFalse(Character.isBmpCodePoint(0x10000));
    assertFalse(Character.isBmpCodePoint(-1));
    assertFalse(Character.isBmpCodePoint(Integer.MAX_VALUE));
    assertFalse(Character.isBmpCodePoint(Integer.MIN_VALUE));
  }

  public void test_isSurrogate() throws Exception {
    assertFalse(Character.isSurrogate('\u0000'));
    assertFalse(Character.isSurrogate('\u0666'));
    assertFalse(Character.isSurrogate((char) (Character.MIN_SURROGATE - 1)));
    for (char ch = Character.MIN_SURROGATE; ch <= Character.MAX_SURROGATE; ++ch) {
      assertTrue(Character.isSurrogate(ch));
    }
    assertFalse(Character.isSurrogate((char) (Character.MAX_SURROGATE + 1)));
  }

  public void test_highSurrogate() throws Exception {
    // The behavior for non-supplementary code points (like these two) is undefined.
    // These are the obvious results if you don't do anything special.
    assertEquals(0xd7c0, Character.highSurrogate(0x0000));
    assertEquals(0xd7c1, Character.highSurrogate(0x0666));
    // These two tests must pass, though.
    assertEquals(0xd800, Character.highSurrogate(0x010000));
    assertEquals(0xdbff, Character.highSurrogate(0x10ffff));
  }

  public void test_lowSurrogate() throws Exception {
    // The behavior for non-supplementary code points (like these two) is undefined.
    // These are the obvious results if you don't do anything special.
    assertEquals(0xdc00, Character.lowSurrogate(0x0000));
    assertEquals(0xde66, Character.lowSurrogate(0x0666));
    // These two tests must pass, though.
    assertEquals(0xdc00, Character.lowSurrogate(0x010000));
    assertEquals(0xdfff, Character.lowSurrogate(0x10ffff));
  }

  public void test_getName() throws Exception {
    // Character.getName requires the corresponding ICU data.
    // Changed from "NULL" and "BELL" by Unicode 49.2
    assertEquals("<control-0000>", Character.getName(0x0000));
    assertEquals("<control-0007>", Character.getName(0x0007));
    assertEquals("LATIN SMALL LETTER L", Character.getName('l'));
    // This changed name from Unicode 1.0. Used to be "OPENING...".
    assertEquals("LEFT CURLY BRACKET", Character.getName('{'));
    assertEquals("ARABIC-INDIC DIGIT SIX", Character.getName(0x0666));
    assertEquals("LINEAR B SYLLABLE B008 A", Character.getName(0x010000));

    // Some private use code points.
    assertEquals("PRIVATE USE AREA E000", Character.getName(0xe000));
    assertEquals("SUPPLEMENTARY PRIVATE USE AREA A F0000", Character.getName(0xf0000));

    // An unassigned code point.
    assertNull(Character.getName(0x10ffff));

    try {
      Character.getName(-1);
      fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
      Character.getName(Integer.MAX_VALUE);
      fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
      Character.getName(Integer.MIN_VALUE);
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }

  public void test_compare() throws Exception {
    assertEquals(0, Character.compare('a', 'a'));
    assertTrue(Character.compare('a', 'b') < 0);
    assertTrue(Character.compare('b', 'a') > 0);
  }

  public void test_UnicodeBlock_all() throws Exception {
    for (int i = 0; i <= 0x100000; ++i) {
      Character.UnicodeBlock.of(i);
    }
  }

  public void test_UnicodeBlock_of() throws Exception {
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.of(1));
    assertEquals(Character.UnicodeBlock.HANGUL_JAMO, Character.UnicodeBlock.of(0x1100));
    assertEquals(Character.UnicodeBlock.CYPRIOT_SYLLABARY, Character.UnicodeBlock.of(0x10800));
    assertEquals(Character.UnicodeBlock.VARIATION_SELECTORS_SUPPLEMENT, Character.UnicodeBlock.of(0xe0100));
    // Unicode 4.1.
    assertEquals(Character.UnicodeBlock.ANCIENT_GREEK_MUSICAL_NOTATION, Character.UnicodeBlock.of(0x1d200));
    // Unicode 5.0.
    assertEquals(Character.UnicodeBlock.NKO, Character.UnicodeBlock.of(0x07c0));
    // Unicode 5.1.
    assertEquals(Character.UnicodeBlock.SUNDANESE, Character.UnicodeBlock.of(0x1b80));
    // Unicode 5.2.
    assertEquals(Character.UnicodeBlock.SAMARITAN, Character.UnicodeBlock.of(0x0800));
    // Unicode 6.0.
    assertEquals(Character.UnicodeBlock.MANDAIC, Character.UnicodeBlock.of(0x0840));
  }

  public void test_UnicodeBlock_forName() throws Exception {
    // No negative tests here because icu4c is more lenient than the RI;
    // we'd allow "basic-latin", and "hangul jamo extended b", for example.
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic latin"));
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BaSiC LaTiN"));
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BasicLatin"));
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("BASIC_LATIN"));
    assertEquals(Character.UnicodeBlock.BASIC_LATIN, Character.UnicodeBlock.forName("basic_LATIN"));

    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL_JAMO_EXTENDED_B"));
    assertEquals(Character.UnicodeBlock.HANGUL_JAMO_EXTENDED_B, Character.UnicodeBlock.forName("HANGUL JAMO EXTENDED-B"));

    // Failure cases.
    try {
      Character.UnicodeBlock.forName(null);
      fail();
    } catch (NullPointerException expected) {
    }
    try {
      Character.UnicodeBlock.forName("this unicode block does not exist");
      fail();
    } catch (IllegalArgumentException expected) {
    }

    // Renamed blocks.
    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek"));
    assertEquals(Character.UnicodeBlock.GREEK, Character.UnicodeBlock.forName("Greek And Coptic"));
    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Marks For Symbols"));
    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Diacritical Marks For Symbols"));
    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("COMBINING_MARKS_FOR_SYMBOLS"));
    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("Combining Marks for Symbols"));
    assertEquals(Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, Character.UnicodeBlock.forName("CombiningMarksforSymbols"));
    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplementary"));
    assertEquals(Character.UnicodeBlock.CYRILLIC_SUPPLEMENTARY, Character.UnicodeBlock.forName("Cyrillic Supplement"));
  }

  public void test_isAlphabetic() throws Exception {
    assertTrue(Character.isAlphabetic('A'));
    assertTrue(Character.isAlphabetic('a'));
    assertFalse(Character.isAlphabetic('1'));
    assertTrue(Character.isAlphabetic(0x113c)); // Hangul j
  }

  public void test_isIdeographic() throws Exception {
    assertFalse(Character.isIdeographic('A'));
    assertFalse(Character.isIdeographic('a'));
    assertFalse(Character.isIdeographic('1'));
    assertFalse(Character.isIdeographic(0x113c)); // Hangul j

    assertTrue(Character.isIdeographic(0x4db5));
    assertTrue(Character.isIdeographic(0x2f999));
    assertFalse(Character.isIdeographic(0x2f99)); // Kangxi radical shell
  }

  // http://b/9690863
  public void test_isDigit_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isDigit" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isDigit(i));
    }
  }

  // http://b/9690863
  public void test_isIdentifierIgnorable_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isIdentifierIgnorable" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isIdentifierIgnorable(i));
    }
  }

  // http://b/9690863
  public void test_isLetter_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isLetter" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isLetter(i));
    }
  }

  // http://b/9690863
  public void test_isLetterOrDigit_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isLetterOrDigit" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isLetterOrDigit(i));
    }
  }

  // http://b/9690863
  public void test_isLowerCase_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isLowerCase" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isLowerCase(i));
    }
  }

  // http://b/9690863
  public void test_isSpaceChar_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isSpaceChar" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      // ICU and the RI disagree about character 0x180e. Remove this special case if this changes
      // or Android decides to follow ICU exactly.
      if (i == 0x180e) {
        assertTrue(Character.isSpaceChar(i));
        assertFalse((Boolean) m.invoke(null, i));
      } else {
        assertEquals("Failed for character " + i, m.invoke(null, i), Character.isSpaceChar(i));
      }
    }
  }

  // http://b/9690863
  public void test_isUpperCase_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isUpperCase" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      assertEquals(m.invoke(null, i), Character.isUpperCase(i));
    }
  }

  // http://b/9690863
  public void test_isWhitespace_against_icu4c() throws Exception {
    Method m = Character.class.getDeclaredMethod("isWhitespace" + "Impl", int.class);
    m.setAccessible(true);
    for (int i = 0; i <= 0xffff; ++i) {
      // ICU and the RI disagree about character 0x180e. Remove this special case if this changes
      // or Android decides to follow ICU exactly.
      if (i == 0x180e) {
          assertTrue(Character.isWhitespace(i));
          assertFalse((Boolean) m.invoke(null, i));
      } else {
        assertEquals("Failed for character " + i, m.invoke(null, i), Character.isWhitespace(i));
      }
    }
  }

  // http://b/15492712
  public void test_getDirectionality() throws Exception {
    // We shouldn't throw an exception for any code point.
    for (int c = '\u0000'; c <= Character.MAX_VALUE; ++c) {
      Character.getDirectionality(c);
    }
    assertEquals(Character.DIRECTIONALITY_UNDEFINED, Character.getDirectionality(0x2066));
    assertEquals(Character.DIRECTIONALITY_UNDEFINED, Character.getDirectionality(0x2067));
    assertEquals(Character.DIRECTIONALITY_UNDEFINED, Character.getDirectionality(0x2068));
    assertEquals(Character.DIRECTIONALITY_UNDEFINED, Character.getDirectionality(0x2069));
  }
}