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