2115N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2115N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2115N/A *
2115N/A * This code is free software; you can redistribute it and/or modify it
2115N/A * under the terms of the GNU General Public License version 2 only, as
2115N/A * published by the Free Software Foundation.
2115N/A *
2115N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2115N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2115N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2115N/A * version 2 for more details (a copy is included in the LICENSE file that
2115N/A * accompanied this code).
2115N/A *
2115N/A * You should have received a copy of the GNU General Public License version
2115N/A * 2 along with this work; if not, write to the Free Software Foundation,
2115N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2115N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2115N/A */
2115N/A
2115N/A/* @test
2115N/A * @bug 4821213
2115N/A * @summary Unit test for CharsetEncoder.canEncode methods
2115N/A */
2115N/A
2115N/Aimport java.io.*;
2115N/Aimport java.nio.*;
2115N/Aimport java.nio.charset.*;
2115N/A
2115N/A
2115N/Apublic class CanEncode {
2115N/A
2115N/A private static int errors = 0;
2115N/A private static PrintStream out = System.err;
2115N/A
2115N/A private static void wrong(CharsetEncoder ce, boolean can, String what) {
2115N/A out.println(ce.charset().name()
2115N/A + ": Wrong answer for " + what
2115N/A + ": " + !can);
2115N/A errors++;
2115N/A }
2115N/A
2115N/A private static void ck(CharsetEncoder ce, char c, boolean can)
2115N/A throws Exception
2115N/A {
2115N/A if (ce.canEncode(c) != can)
2115N/A wrong(ce, can,
2115N/A ("'" + c + "' (0x"
2115N/A + Integer.toHexString(c & 0xffff) + ")"));
2115N/A }
2115N/A
2115N/A private static void ck(CharsetEncoder ce, String s, boolean can)
2115N/A throws Exception
2115N/A {
2115N/A if (ce.canEncode(CharBuffer.wrap(s.toCharArray())) != can)
2115N/A wrong(ce, can, "array \"" + s + "\"");
2115N/A if (ce.canEncode(CharBuffer.wrap(s)) != can)
2115N/A wrong(ce, can, "buffer \"" + s + "\"");
2115N/A }
2115N/A
2115N/A private static void test(String csn) throws Exception {
2115N/A
2115N/A Charset cs = Charset.forName(csn);
2115N/A CharsetEncoder ce = cs.newEncoder();
2115N/A
2115N/A if (cs.name().equals("US-ASCII")) {
2115N/A ck(ce, 'x', true);
2115N/A ck(ce, '\u00B6', false);
2115N/A ck(ce, "x", true);
2115N/A ck(ce, "\u00B6", false);
2115N/A ck(ce, "xyzzy", true);
2115N/A ck(ce, "xy\u00B6", false);
2115N/A }
2115N/A
2115N/A // Unpaired surrogates should never be encodable
2115N/A ck(ce, '\ud800', false);
2115N/A ck(ce, '\ud801', false);
2115N/A ck(ce, '\udffe', false);
2115N/A ck(ce, '\udfff', false);
2115N/A ck(ce, "\ud800", false);
2115N/A ck(ce, "\ud801", false);
2115N/A ck(ce, "\udffe", false);
2115N/A ck(ce, "\udfff", false);
2115N/A
2115N/A }
2115N/A
2115N/A public static void main(String[] args) throws Exception {
2115N/A test("US-ASCII");
2115N/A test("UTF-8");
2115N/A }
2115N/A
2115N/A}