Path: blob/master/test/jdk/sun/nio/cs/FindASCIIRangeCodingBugs.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 637829525* @summary Roundtrip Encoding/Decoding of ASCII chars from 0x00-0x7f26*/2728import java.util.*;29import java.nio.*;30import java.nio.charset.*;3132public class FindASCIIRangeCodingBugs {33private static int failures = 0;34private static byte[] asciiBytes = new byte[0x80];35private static char[] asciiChars = new char[0x80];36private static String asciiString;3738private static void check(String csn) throws Exception {39System.out.println(csn);40if (! Arrays.equals(asciiString.getBytes(csn), asciiBytes)) {41System.out.printf("%s -> bytes%n", csn);42failures++;43}44if (! new String(asciiBytes, csn).equals(asciiString)) {45System.out.printf("%s -> chars%n", csn);46failures++;47}48}4950public static void main(String[] args) throws Exception {51for (int i = 0; i < 0x80; i++) {52asciiBytes[i] = (byte) i;53asciiChars[i] = (char) i;54}55asciiString = new String(asciiChars);56Charset ascii = Charset.forName("ASCII");57for (Map.Entry<String,Charset> e58: Charset.availableCharsets().entrySet()) {59String csn = e.getKey();60Charset cs = e.getValue();61if (!cs.contains(ascii) ||62csn.matches(".*2022.*") || //iso2022 family63csn.matches("x-windows-5022[0|1]") || //windows 2022jp64csn.matches(".*UTF-[16|32].*")) //multi-bytes65continue;66if (! cs.canEncode()) continue;67try {68check(csn);69} catch (Throwable t) {70t.printStackTrace();71failures++;72}73}74if (failures > 0)75throw new Exception(failures + "tests failed");76}77}787980