Path: blob/master/test/jdk/sun/nio/cs/FindCanEncodeBugs.java
41149 views
/*1* Copyright (c) 2008, 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 5066863 5066867 5066874 5066879 5066884 5066887 5065777 673065225@summary canEncode() false iff encode() throws CharacterCodingException26@run main/timeout=1200 FindCanEncodeBugs27@author Martin Buchholz28*/2930import java.util.*;31import java.nio.charset.*;32import java.nio.*;3334public class FindCanEncodeBugs {35static boolean encodable1(CharsetEncoder enc, char c) {36enc.reset();37return enc.canEncode(c);38}3940static boolean encodable2(CharsetEncoder enc, char c) {41enc.reset();42try { enc.encode(CharBuffer.wrap(new char[]{c})); return true; }43catch (CharacterCodingException e) { return false; }44}4546public static void main(String[] args) throws Exception {47int failures = 0;4849for (Map.Entry<String,Charset> e50: Charset.availableCharsets().entrySet()) {51String csn = e.getKey();52Charset cs = e.getValue();5354if (! cs.canEncode() || csn.matches("x-COMPOUND_TEXT"))55continue;5657//System.out.println(csn);5859CharsetEncoder enc = cs.newEncoder();6061for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {62boolean encodable1 = encodable1(enc, (char)i);63boolean encodable2 = encodable2(enc, (char)i);64if (encodable1 != encodable2) {65int start = i;66int end = i;67for (int j = i;68j <= '\uffff' &&69encodable1(enc, (char)j) == encodable1 &&70encodable2(enc, (char)j) == encodable2;71j++)72end = j;73System.out.printf("charset=%-18s canEncode=%-5b ",74csn, encodable1);75if (start == end)76System.out.printf("\'\\u%04x\'%n", start);77else78System.out.printf("\'\\u%04x\' - \'\\u%04x\'%n",79start, end);80i = end;81failures++;82}83}84}8586if (failures > 0)87throw new Exception(failures + " failures");88}89}909192