Home | History | Annotate | Download | only in src
      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 import java.nio.charset.Charset;
     18 import java.io.UnsupportedEncodingException;
     19 
     20 /**
     21  * Simple string test.
     22  */
     23 public class Main {
     24     public static void main(String args[]) {
     25         basicTest();
     26         indexTest();
     27         constructorTest();
     28         copyTest();
     29     }
     30 
     31     public static void basicTest() {
     32         String baseStr = "*** This is a very nice string!!!";
     33         String testStr;
     34         int i;
     35 
     36         testStr = baseStr.substring(4, baseStr.length() - 3);
     37         System.out.println("testStr is '" + testStr + "'");
     38 
     39         /* sloppy for loop */
     40         for (i = 0; i < testStr.length(); i++)
     41             System.out.print(testStr.charAt(i));
     42         System.out.print("\n");
     43 
     44         String testStr2 = "This is a very nice strinG";
     45         if (testStr.length() != testStr2.length())
     46             System.out.println("WARNING: stringTest length mismatch");
     47 
     48         System.out.println("Compare result is " + testStr.compareTo(testStr2));
     49 
     50         // expected: -65302
     51         String s1 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\u00e9\u00e9\u00e9";
     52         String s2 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\uffff\uffff\uffff\u00e9\u00e9\u00e9";
     53         System.out.println("Compare unicode: " + s1.compareTo(s2));
     54 
     55         try {
     56             testStr.charAt(500);
     57             System.out.println("GLITCH: expected exception");
     58         } catch (StringIndexOutOfBoundsException sioobe) {
     59             System.out.println("Got expected exception");
     60         }
     61     }
     62 
     63     public static void indexTest() {
     64         String baseStr = "The quick brown fox jumps over the lazy dog!";
     65         String subStr;
     66 
     67         subStr = baseStr.substring(5, baseStr.length() - 4);
     68         System.out.println("subStr is '" + subStr + "'");
     69 
     70         System.out.println("Indexes are: " +
     71             baseStr.indexOf('T') + ":" +
     72             subStr.indexOf('T') + ":" +
     73             subStr.indexOf('u') + ":" +
     74             baseStr.indexOf('!') + ":" +
     75             subStr.indexOf('y') + ":" +
     76             subStr.indexOf('d') + ":" +
     77             baseStr.indexOf('x') + ":" +
     78             subStr.indexOf('x', 0) + ":" +
     79             subStr.indexOf('x', -1) + ":" +
     80             subStr.indexOf('x', 200) + ":" +
     81             baseStr.indexOf('x', 17) + ":" +
     82             baseStr.indexOf('x', 18) + ":" +
     83             baseStr.indexOf('x', 19) + ":" +
     84             subStr.indexOf('x', 13) + ":" +
     85             subStr.indexOf('x', 14) + ":" +
     86             subStr.indexOf('&') + ":" +
     87             baseStr.indexOf(0x12341234));
     88     }
     89 
     90     public static void constructorTest() {
     91         byte[] byteArray = "byteArray".getBytes();
     92         char[] charArray = new char[] { 'c', 'h', 'a', 'r', 'A', 'r', 'r', 'a', 'y' };
     93         String charsetName = "US-ASCII";
     94         Charset charset = Charset.forName("UTF-8");
     95         String string = "string";
     96         StringBuffer stringBuffer = new StringBuffer("stringBuffer");
     97         int [] codePoints = new int[] { 65, 66, 67, 68, 69 };
     98         StringBuilder stringBuilder = new StringBuilder("stringBuilder");
     99 
    100         String s1 = new String();
    101         String s2 = new String(byteArray);
    102         String s3 = new String(byteArray, 1);
    103         String s4 = new String(byteArray, 0, 4);
    104         String s5 = new String(byteArray, 2, 4, 5);
    105 
    106         try {
    107             String s6 = new String(byteArray, 2, 4, charsetName);
    108             String s7 = new String(byteArray, charsetName);
    109         } catch (UnsupportedEncodingException e) {
    110             System.out.println("Got unexpected UnsupportedEncodingException");
    111         }
    112         String s8 = new String(byteArray, 3, 3, charset);
    113         String s9 = new String(byteArray, charset);
    114         String s10 = new String(charArray);
    115         String s11 = new String(charArray, 0, 4);
    116         String s12 = new String(string);
    117         String s13 = new String(stringBuffer);
    118         String s14 = new String(codePoints, 1, 3);
    119         String s15 = new String(stringBuilder);
    120     }
    121 
    122     public static void copyTest() {
    123         String src = new String("Hello Android");
    124         char[] dst = new char[7];
    125         char[] tmp = null;
    126 
    127         try {
    128             src.getChars(2, 9, tmp, 0);
    129             System.out.println("GLITCH: expected exception");
    130         } catch (NullPointerException npe) {
    131             System.out.println("Got expected exception");
    132         }
    133 
    134         try {
    135             src.getChars(-1, 9, dst, 0);
    136             System.out.println("GLITCH: expected exception");
    137         } catch (StringIndexOutOfBoundsException sioobe) {
    138             System.out.println("Got expected exception");
    139         }
    140 
    141         try {
    142             src.getChars(2, 19, dst, 0);
    143             System.out.println("GLITCH: expected exception");
    144         } catch (StringIndexOutOfBoundsException sioobe) {
    145             System.out.println("Got expected exception");
    146         }
    147 
    148         try {
    149             src.getChars(2, 1, dst, 0);
    150             System.out.println("GLITCH: expected exception");
    151         } catch (StringIndexOutOfBoundsException sioobe) {
    152             System.out.println("Got expected exception");
    153         }
    154 
    155         try {
    156             src.getChars(2, 10, dst, 0);
    157             System.out.println("GLITCH: expected exception");
    158         } catch (ArrayIndexOutOfBoundsException aioobe) {
    159             System.out.println("Got expected exception");
    160         }
    161 
    162         src.getChars(2, 9, dst, 0);
    163         System.out.println(new String(dst));
    164     }
    165 }
    166