CharacterTest.java revision 4f5a696869e0348cf21dad9050d59b20849aff96
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        assertEquals("NULL", Character.getName(0x0000));
78        assertEquals("BELL", Character.getName(0x0007));
79        assertEquals("LATIN SMALL LETTER L", Character.getName('l'));
80        // This changed name from Unicode 1.0. Used to be "OPENING...".
81        assertEquals("LEFT CURLY BRACKET", Character.getName('{'));
82        assertEquals("ARABIC-INDIC DIGIT SIX", Character.getName(0x0666));
83        assertEquals("LINEAR B SYLLABLE B008 A", Character.getName(0x010000));
84
85        // Some private use code points.
86        assertEquals("PRIVATE USE AREA E000", Character.getName(0xe000));
87        assertEquals("SUPPLEMENTARY PRIVATE USE AREA A F0000", Character.getName(0xf0000));
88
89        // An unassigned code point.
90        assertNull(Character.getName(0x10ffff));
91
92        try {
93            Character.getName(-1);
94            fail();
95        } catch (IllegalArgumentException expected) {
96        }
97        try {
98            Character.getName(Integer.MAX_VALUE);
99            fail();
100        } catch (IllegalArgumentException expected) {
101        }
102        try {
103            Character.getName(Integer.MIN_VALUE);
104            fail();
105        } catch (IllegalArgumentException expected) {
106        }
107    }
108
109    public void test_compare() throws Exception {
110        assertEquals(0, Character.compare('a', 'a'));
111        assertTrue(Character.compare('a', 'b') < 0);
112        assertTrue(Character.compare('b', 'a') > 0);
113    }
114}
115