Path: blob/master/test/jdk/java/nio/charset/CharsetEncoder/CanEncode.java
41153 views
/*1* Copyright (c) 2010, 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/* @test24* @bug 482121325* @summary Unit test for CharsetEncoder.canEncode methods26*/2728import java.io.*;29import java.nio.*;30import java.nio.charset.*;313233public class CanEncode {3435private static int errors = 0;36private static PrintStream out = System.err;3738private static void wrong(CharsetEncoder ce, boolean can, String what) {39out.println(ce.charset().name()40+ ": Wrong answer for " + what41+ ": " + !can);42errors++;43}4445private static void ck(CharsetEncoder ce, char c, boolean can)46throws Exception47{48if (ce.canEncode(c) != can)49wrong(ce, can,50("'" + c + "' (0x"51+ Integer.toHexString(c & 0xffff) + ")"));52}5354private static void ck(CharsetEncoder ce, String s, boolean can)55throws Exception56{57if (ce.canEncode(CharBuffer.wrap(s.toCharArray())) != can)58wrong(ce, can, "array \"" + s + "\"");59if (ce.canEncode(CharBuffer.wrap(s)) != can)60wrong(ce, can, "buffer \"" + s + "\"");61}6263private static void test(String csn) throws Exception {6465Charset cs = Charset.forName(csn);66CharsetEncoder ce = cs.newEncoder();6768if (cs.name().equals("US-ASCII")) {69ck(ce, 'x', true);70ck(ce, '\u00B6', false);71ck(ce, "x", true);72ck(ce, "\u00B6", false);73ck(ce, "xyzzy", true);74ck(ce, "xy\u00B6", false);75}7677// Unpaired surrogates should never be encodable78ck(ce, '\ud800', false);79ck(ce, '\ud801', false);80ck(ce, '\udffe', false);81ck(ce, '\udfff', false);82ck(ce, "\ud800", false);83ck(ce, "\ud801", false);84ck(ce, "\udffe", false);85ck(ce, "\udfff", false);8687}8889public static void main(String[] args) throws Exception {90test("US-ASCII");91test("UTF-8");92}9394}959697