Path: blob/master/test/jdk/java/io/charStreams/StringConvert.java
41149 views
/*1* Copyright (c) 1997, 2000, 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@summary General tests of String constructors and methods that convert25between character encodings. This should really be in26java/lang/String, but it shares code with the I/O charStream27tests.28*/2930import java.io.*;313233public class StringConvert {3435static String enc = "UTF8";36static int limit = 500;37static int max = 0xffff;3839static void fail(String s) {40throw new RuntimeException(s);41}4243public static void main(String[] args) throws Exception {44PrintStream log = System.err;45IntGenerator ig = new IntGenerator();46CharGenerator cg;47StringGenerator sg;48String s;49int i = 0;5051/* String(byte[] bytes, String enc)52getBytes(String enc)53*/54log.println("-- String(byte[], String), getBytes(String)");55i = 0;56cg = new CharGenerator(ig, 0, max);57sg = new StringGenerator(ig, cg, limit);58while ((s = sg.next()) != null) {59byte[] b = s.getBytes(enc);60String t = new String(b, enc);61if (!s.equals(t)) {62int n = Math.min(s.length(), t.length());63for (int j = 0; j < n; j++) {64if (s.charAt(j) != t.charAt(j)) {65log.println("Mismatch: " + j + " "66+ Integer.toHexString(s.charAt(j))67+ " != "68+ Integer.toHexString(t.charAt(j)));69}70}71fail("Conversion failure");72}73log.println("[" + i + "] " + s.length());74i++;75}7677/* String(byte[] bytes)78getBytes()79*/80log.println("-- String(byte[]), getBytes()");81i = 0;82cg = new CharGenerator(ig, 0x20, 0x7e);83sg = new StringGenerator(ig, cg, limit);84while ((s = sg.next()) != null) {85log.println("[" + i + "] \"" + s + "\"");86byte[] b = s.getBytes();87String t = new String(b);88if (! s.equals(t))89fail("Conversion failure");90i++;91}9293/* String(byte[] bytes, int offset, int length)94getBytes()95*/96log.println("-- String(byte[], int, int), getBytes()");97i = 0;98cg = new CharGenerator(ig, 0x20, 0x7e);99sg = new StringGenerator(ig, cg, limit);100while ((s = sg.next()) != null) {101log.println("[" + i + "] \"" + s + "\"");102byte[] b = s.getBytes();103int o = ig.next(s.length() - 1);104int n = ig.next(s.length() - o);105String t = new String(b, o, n);106if (! s.substring(o, o + n).equals(t))107fail("Conversion failure");108i++;109}110111/* String(byte[] bytes, int offset, int length, String enc)112getBytes(String enc)113*/114log.println("-- String(byte[], int, int, String), getBytes(String)");115i = 0;116cg = new CharGenerator(ig);117sg = new StringGenerator(ig, cg, limit);118while ((s = sg.next()) != null) {119log.println("[" + i + "] " + s.length());120byte[] b = s.getBytes(enc);121int o = ig.next(100);122byte[] b2 = new byte[b.length + o];123System.arraycopy(b, 0, b2, o, b.length);124String t = new String(b2, o, b.length, enc);125if (! s.equals(t))126fail("Conversion failure");127i++;128}129130/* Substrings */131log.println("-- Substrings");132i = 0;133cg = new CharGenerator(ig, 0x20, 0x7e);134sg = new StringGenerator(ig, cg, limit);135while ((s = sg.next()) != null) {136log.println("[" + i + "] \"" + s + "\"");137int o = ig.next(s.length() - 1);138int n = ig.next(s.length() - o);139String s2 = s.substring(o, o + n);140byte[] b = s2.getBytes();141String t = new String(b);142if (! s2.equals(t))143fail("Conversion failure");144i++;145}146147}148149}150151152