blob: dd51ce641220cbc15d33e8380188e56bf01410a5 [file] [log] [blame]
mduigou7824dda2013-05-01 08:35:09 -07001/*
katleman6de23602013-05-09 15:04:56 -07002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
mduigou7824dda2013-05-01 08:35:09 -07003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
katleman6de23602013-05-09 15:04:56 -07007 * published by the Free Software Foundation.
mduigou7824dda2013-05-01 08:35:09 -07008 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.Arrays;
25import java.util.List;
26import java.util.NoSuchElementException;
27import java.util.PrimitiveIterator;
28import java.util.stream.Collectors;
29
30import org.testng.annotations.Test;
31
32import static org.testng.Assert.*;
33
34/*
35 * @test
36 * @summary Unit test for CharSequence default methods
37 * @bug 8012665
38 * @run testng DefaultTest
39 */
40
41@Test(groups = "lib")
42public class DefaultTest {
43
44 @Test(expectedExceptions = NoSuchElementException.class)
45 public void testEmptyChars() {
46 PrimitiveIterator.OfInt s = "".chars().iterator();
47 assertFalse(s.hasNext());
48 int ch = s.nextInt();
49 }
50
51 public void testSimpleChars() {
52 List<Integer> list = "abc".chars().boxed().collect(Collectors.toList());
53 assertEquals(list, Arrays.asList((int) 'a', (int) 'b', (int) 'c'));
54 }
55
56 @Test(expectedExceptions = NoSuchElementException.class)
57 public void testEmptyCodePoints() {
58 PrimitiveIterator.OfInt s = "".codePoints().iterator();
59 assertFalse(s.hasNext());
60 int cp = s.nextInt();
61 }
62
63 public void testSimpleCodePoints() {
64 List<Integer> list = "abc".codePoints().boxed().collect(Collectors.toList());
65 assertEquals(list, Arrays.asList((int)'a', (int)'b', (int)'c'));
66 }
67
68 public void testUndefCodePoints() {
69 List<Integer> list = "X\ufffeY".codePoints().boxed().collect(Collectors.toList());
70 assertEquals(list, Arrays.asList((int)'X', 0xFFFE, (int)'Y'));
71 }
72
73 public void testSurrogatePairing() {
74 // U+1D11E = MUSICAL SYMBOL G CLEF
75 // equivalent to surrogate pair U+D834 U+DD1E
76 List<Integer> list;
77 final int GCLEF = 0x1d11e;
78
79 list = "\ud834\udd1e".codePoints().boxed().collect(Collectors.toList());
80 assertEquals(list, Arrays.asList(GCLEF));
81 list = "A\ud834\udd1e".codePoints().boxed().collect(Collectors.toList());
82 assertEquals(list, Arrays.asList((int)'A', GCLEF));
83 list = "\ud834\udd1eB".codePoints().boxed().collect(Collectors.toList());
84 assertEquals(list, Arrays.asList(GCLEF, (int)'B'));
85 list = "X\ud834\udd1eY".codePoints().boxed().collect(Collectors.toList());
86 assertEquals(list, Arrays.asList((int)'X', GCLEF, (int)'Y'));
87 }
88
89 public void testUndefUnpaired() {
90 List<Integer> list = "W\udd1eX\ud834Y\ufffeZ".codePoints().boxed().collect(Collectors.toList());
91 assertEquals(list, Arrays.asList(
92 (int)'W', 0xdd1e, (int)'X', 0xd834, (int)'Y', 0xfffe, (int)'Z'));
93 }
94}