1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.nio.charset.Charset;
18import java.io.UnsupportedEncodingException;
19
20/**
21 * Simple string test.
22 */
23public class Main {
24    public static void main(String args[]) {
25        basicTest();
26        indexTest();
27        constructorTest();
28    }
29
30    public static void basicTest() {
31        String baseStr = "*** This is a very nice string!!!";
32        String testStr;
33        int i;
34
35        testStr = baseStr.substring(4, baseStr.length() - 3);
36        System.out.println("testStr is '" + testStr + "'");
37
38        /* sloppy for loop */
39        for (i = 0; i < testStr.length(); i++)
40            System.out.print(testStr.charAt(i));
41        System.out.print("\n");
42
43        String testStr2 = "This is a very nice strinG";
44        if (testStr.length() != testStr2.length())
45            System.out.println("WARNING: stringTest length mismatch");
46
47        System.out.println("Compare result is " + testStr.compareTo(testStr2));
48
49        // expected: -65302
50        String s1 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\u00e9\u00e9\u00e9";
51        String s2 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\uffff\uffff\uffff\u00e9\u00e9\u00e9";
52        System.out.println("Compare unicode: " + s1.compareTo(s2));
53
54        try {
55            testStr.charAt(500);
56            System.out.println("GLITCH: expected exception");
57        } catch (StringIndexOutOfBoundsException sioobe) {
58            System.out.println("Got expected exception");
59        }
60    }
61
62    public static void indexTest() {
63        String baseStr = "The quick brown fox jumps over the lazy dog!";
64        String subStr;
65
66        subStr = baseStr.substring(5, baseStr.length() - 4);
67        System.out.println("subStr is '" + subStr + "'");
68
69        System.out.println("Indexes are: " +
70            baseStr.indexOf('T') + ":" +
71            subStr.indexOf('T') + ":" +
72            subStr.indexOf('u') + ":" +
73            baseStr.indexOf('!') + ":" +
74            subStr.indexOf('y') + ":" +
75            subStr.indexOf('d') + ":" +
76            baseStr.indexOf('x') + ":" +
77            subStr.indexOf('x', 0) + ":" +
78            subStr.indexOf('x', -1) + ":" +
79            subStr.indexOf('x', 200) + ":" +
80            baseStr.indexOf('x', 17) + ":" +
81            baseStr.indexOf('x', 18) + ":" +
82            baseStr.indexOf('x', 19) + ":" +
83            subStr.indexOf('x', 13) + ":" +
84            subStr.indexOf('x', 14) + ":" +
85            subStr.indexOf('&') + ":" +
86            baseStr.indexOf(0x12341234));
87    }
88
89    public static void constructorTest() {
90        byte[] byteArray = "byteArray".getBytes();
91        char[] charArray = new char[] { 'c', 'h', 'a', 'r', 'A', 'r', 'r', 'a', 'y' };
92        String charsetName = "US-ASCII";
93        Charset charset = Charset.forName("UTF-8");
94        String string = "string";
95        StringBuffer stringBuffer = new StringBuffer("stringBuffer");
96        int [] codePoints = new int[] { 65, 66, 67, 68, 69 };
97        StringBuilder stringBuilder = new StringBuilder("stringBuilder");
98
99        String s1 = new String();
100        String s2 = new String(byteArray);
101        String s3 = new String(byteArray, 1);
102        String s4 = new String(byteArray, 0, 4);
103        String s5 = new String(byteArray, 2, 4, 5);
104
105        try {
106            String s6 = new String(byteArray, 2, 4, charsetName);
107            String s7 = new String(byteArray, charsetName);
108        } catch (UnsupportedEncodingException e) {
109            System.out.println("Got unexpected UnsupportedEncodingException");
110        }
111        String s8 = new String(byteArray, 3, 3, charset);
112        String s9 = new String(byteArray, charset);
113        String s10 = new String(charArray);
114        String s11 = new String(charArray, 0, 4);
115        String s12 = new String(string);
116        String s13 = new String(stringBuffer);
117        String s14 = new String(codePoints, 1, 3);
118        String s15 = new String(stringBuilder);
119    }
120}
121