mduigou | 7824dda | 2013-05-01 08:35:09 -0700 | [diff] [blame] | 1 | /* |
katleman | 6de2360 | 2013-05-09 15:04:56 -0700 | [diff] [blame] | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
mduigou | 7824dda | 2013-05-01 08:35:09 -0700 | [diff] [blame] | 3 | * 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 |
katleman | 6de2360 | 2013-05-09 15:04:56 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
mduigou | 7824dda | 2013-05-01 08:35:09 -0700 | [diff] [blame] | 8 | * |
| 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 | |
| 24 | import java.util.Arrays; |
| 25 | import java.util.List; |
| 26 | import java.util.NoSuchElementException; |
| 27 | import java.util.PrimitiveIterator; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | import org.testng.annotations.Test; |
| 31 | |
| 32 | import 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") |
| 42 | public 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 | } |