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