1/*
2 * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @summary General tests of String constructors and methods that convert
26            between character encodings.  This should really be in
27            java/lang/String, but it shares code with the I/O charStream
28            tests.
29 */
30
31import java.io.*;
32
33
34public class StringConvert {
35
36    static String enc = "UTF8";
37    static int limit = 500;
38    static int max = 0xffff;
39
40    static void fail(String s) {
41        throw new RuntimeException(s);
42    }
43
44    public static void main(String[] args) throws Exception {
45        PrintStream log = System.err;
46        IntGenerator ig = new IntGenerator();
47        CharGenerator cg;
48        StringGenerator sg;
49        String s;
50        int i = 0;
51
52        /* String(byte[] bytes, String enc)
53           getBytes(String enc)
54         */
55        log.println("-- String(byte[], String), getBytes(String)");
56        i = 0;
57        cg = new CharGenerator(ig, 0, max);
58        sg = new StringGenerator(ig, cg, limit);
59        while ((s = sg.next()) != null) {
60            byte[] b = s.getBytes(enc);
61            String t = new String(b, enc);
62            if (!s.equals(t)) {
63                int n = Math.min(s.length(), t.length());
64                for (int j = 0; j < n; j++) {
65                    if (s.charAt(j) != t.charAt(j)) {
66                        log.println("Mismatch: " + j + " "
67                                    + Integer.toHexString(s.charAt(j))
68                                    + " != "
69                                    + Integer.toHexString(t.charAt(j)));
70                    }
71                }
72                fail("Conversion failure");
73            }
74            log.println("[" + i + "] " + s.length());
75            i++;
76        }
77
78        /* String(byte[] bytes)
79           getBytes()
80         */
81        log.println("-- String(byte[]), getBytes()");
82        i = 0;
83        cg = new CharGenerator(ig, 0x20, 0x7e);
84        sg = new StringGenerator(ig, cg, limit);
85        while ((s = sg.next()) != null) {
86            log.println("[" + i + "] \"" + s + "\"");
87            byte[] b = s.getBytes();
88            String t = new String(b);
89            if (! s.equals(t))
90                fail("Conversion failure");
91            i++;
92        }
93
94        /* String(byte[] bytes, int offset, int length)
95           getBytes()
96         */
97        log.println("-- String(byte[], int, int), getBytes()");
98        i = 0;
99        cg = new CharGenerator(ig, 0x20, 0x7e);
100        sg = new StringGenerator(ig, cg, limit);
101        while ((s = sg.next()) != null) {
102            log.println("[" + i + "] \"" + s + "\"");
103            byte[] b = s.getBytes();
104            int o = ig.next(s.length() - 1);
105            int n = ig.next(s.length() - o);
106            String t = new String(b, o, n);
107            if (! s.substring(o, o + n).equals(t))
108                fail("Conversion failure");
109            i++;
110        }
111
112        /* String(byte[] bytes, int offset, int length, String enc)
113           getBytes(String enc)
114         */
115        log.println("-- String(byte[], int, int, String), getBytes(String)");
116        i = 0;
117        cg = new CharGenerator(ig);
118        sg = new StringGenerator(ig, cg, limit);
119        while ((s = sg.next()) != null) {
120            log.println("[" + i + "] " + s.length());
121            byte[] b = s.getBytes(enc);
122            int o = ig.next(100);
123            byte[] b2 = new byte[b.length + o];
124            System.arraycopy(b, 0, b2, o, b.length);
125            String t = new String(b2, o, b.length, enc);
126            if (! s.equals(t))
127                fail("Conversion failure");
128            i++;
129        }
130
131        /* Substrings */
132        log.println("-- Substrings");
133        i = 0;
134        cg = new CharGenerator(ig, 0x20, 0x7e);
135        sg = new StringGenerator(ig, cg, limit);
136        while ((s = sg.next()) != null) {
137            log.println("[" + i + "] \"" + s + "\"");
138            int o = ig.next(s.length() - 1);
139            int n = ig.next(s.length() - o);
140            String s2 = s.substring(o, o + n);
141            byte[] b = s2.getBytes();
142            String t = new String(b);
143            if (! s2.equals(t))
144                fail("Conversion failure");
145            i++;
146        }
147
148    }
149
150}
151