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
17/**
18 * Simple string test.
19 */
20public class Main {
21    public static void main(String args[]) {
22        basicTest();
23        indexTest();
24    }
25
26    public static void basicTest() {
27        String baseStr = "*** This is a very nice string!!!";
28        String testStr;
29        int i;
30
31        testStr = baseStr.substring(4, baseStr.length() - 3);
32        System.out.println("testStr is '" + testStr + "'");
33
34        /* sloppy for loop */
35        for (i = 0; i < testStr.length(); i++)
36            System.out.print(testStr.charAt(i));
37        System.out.print("\n");
38
39        String testStr2 = "This is a very nice strinG";
40        if (testStr.length() != testStr2.length())
41            System.out.println("WARNING: stringTest length mismatch");
42
43        System.out.println("Compare result is " + testStr.compareTo(testStr2));
44
45        // expected: -65302
46        String s1 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\u00e9\u00e9\u00e9";
47        String s2 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\uffff\uffff\uffff\u00e9\u00e9\u00e9";
48        System.out.println("Compare unicode: " + s1.compareTo(s2));
49
50        try {
51            testStr.charAt(500);
52            System.out.println("GLITCH: expected exception");
53        } catch (StringIndexOutOfBoundsException sioobe) {
54            System.out.println("Got expected exception");
55        }
56    }
57
58    public static void indexTest() {
59        String baseStr = "The quick brown fox jumps over the lazy dog!";
60        String subStr;
61
62        subStr = baseStr.substring(5, baseStr.length() - 4);
63        System.out.println("subStr is '" + subStr + "'");
64
65        System.out.println("Indexes are: " +
66            baseStr.indexOf('T') + ":" +
67            subStr.indexOf('T') + ":" +
68            subStr.indexOf('u') + ":" +
69            baseStr.indexOf('!') + ":" +
70            subStr.indexOf('y') + ":" +
71            subStr.indexOf('d') + ":" +
72            baseStr.indexOf('x') + ":" +
73            subStr.indexOf('x', 0) + ":" +
74            subStr.indexOf('x', -1) + ":" +
75            subStr.indexOf('x', 200) + ":" +
76            baseStr.indexOf('x', 17) + ":" +
77            baseStr.indexOf('x', 18) + ":" +
78            baseStr.indexOf('x', 19) + ":" +
79            subStr.indexOf('x', 13) + ":" +
80            subStr.indexOf('x', 14) + ":" +
81            subStr.indexOf('&') + ":" +
82            baseStr.indexOf(0x12341234));
83    }
84}
85