1/*
2 * Copyright (C) 2016 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 junit.framework.TestCase;
20
21import java.util.Arrays;
22
23public class StringBufferTest extends TestCase {
24
25    public void testChars() {
26        StringBuffer s = new StringBuffer("Hello\n\tworld");
27        int[] expected = new int[s.length()];
28        for (int i = 0; i < s.length(); ++i) {
29            expected[i] = (int) s.charAt(i);
30        }
31        assertTrue(Arrays.equals(expected, s.chars().toArray()));
32
33        // Surrogate code point
34        char high = '\uD83D', low = '\uDE02';
35        StringBuffer surrogateCP = new StringBuffer().append(new char[]{high, low, low});
36        assertTrue(Arrays.equals(new int[]{high, low, low}, surrogateCP.chars().toArray()));
37    }
38
39    public void testCodePoints() {
40        StringBuffer s = new StringBuffer("Hello\n\tworld");
41        int[] expected = new int[s.length()];
42        for (int i = 0; i < s.length(); ++i) {
43            expected[i] = (int) s.charAt(i);
44        }
45        assertTrue(Arrays.equals(expected, s.codePoints().toArray()));
46
47        // Surrogate code point
48        char high = '\uD83D', low = '\uDE02';
49        StringBuffer surrogateCP = new StringBuffer().append(new char[]{high, low, low, '0'});
50        assertEquals(Character.toCodePoint(high, low), surrogateCP.codePoints().toArray()[0]);
51        assertEquals((int) low, surrogateCP.codePoints().toArray()[1]); // Unmatched surrogate.
52        assertEquals((int) '0', surrogateCP.codePoints().toArray()[2]);
53    }
54}
55