Path: blob/master/test/jdk/java/util/StringTokenizer/Supplementary.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*I25* @test26* @bug 490072727* @summary Unit test for supplementary characters (JSR-204)28*/2930import java.util.StringTokenizer;3132public class Supplementary {33public static void main(String[] args) {34String text =35"ab\uD800\uDC00\uD800\uDC01cd\uD800\uDC00\uD800xy \uD801\uDC00z\t123\uDCFF456";36String delims = " \t\r\n\f.\uD800\uDC00,:;";37String[] expected =38{"ab", "\uD800\uDC01cd", "\uD800xy", "\uD801\uDC00z", "123\uDCFF456" };39testTokenizer(text, delims, expected);4041delims = " \t\r\n\f.,:;\uDCFF";42expected = new String[] { "ab\uD800\uDC00\uD800\uDC01cd\uD800\uDC00\uD800xy",43"\uD801\uDC00z",44"123",45"456" };46testTokenizer(text, delims, expected);4748delims = "\uD800";49expected = new String[] { "ab\uD800\uDC00\uD800\uDC01cd\uD800\uDC00",50"xy \uD801\uDC00z\t123\uDCFF456" };51testTokenizer(text, delims, expected);52}5354static void testTokenizer(String text, String delims, String[] expected) {55StringTokenizer tokenizer = new StringTokenizer(text, delims);56int n = tokenizer.countTokens();57if (n != expected.length) {58throw new RuntimeException("countToken(): wrong value " + n59+ ", expected " + expected.length);60}61int i = 0;62while (tokenizer.hasMoreTokens()) {63String token = tokenizer.nextToken();64if (!token.equals(expected[i++])) {65throw new RuntimeException("nextToken(): wrong token. got \""66+ token + "\", expected \"" + expected[i-1]);67}68}69if (i != expected.length) {70throw new RuntimeException("unexpected the number of tokens: " + i71+ ", expected " + expected.length);72}73}74}757677