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 { main(String args[])24 public static void main(String args[]) { 25 basicTest(); 26 indexTest(); 27 constructorTest(); 28 copyTest(); 29 } 30 basicTest()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 int compareResult = testStr.compareTo(testStr2); 49 if (compareResult > 0) { 50 System.out.println("Compare result is greater than zero"); 51 } else if (compareResult == 0) { 52 System.out.println("Compare result is equal to zero"); 53 } else { 54 System.out.println("Compare result is less than zero"); 55 } 56 57 // expected: -65302 58 String s1 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\u00e9\u00e9\u00e9"; 59 String s2 = "\u0c6d\u0cb6\u0d00\u0000\u0080\u0080\u0080\u0000\u0002\u0002\u0002\u0000\uffff\uffff\uffff\u00e9\u00e9\u00e9"; 60 System.out.println("Compare unicode: " + s1.compareTo(s2)); 61 62 try { 63 testStr.charAt(500); 64 System.out.println("GLITCH: expected exception"); 65 } catch (StringIndexOutOfBoundsException sioobe) { 66 System.out.println("Got expected exception"); 67 } 68 } 69 indexTest()70 public static void indexTest() { 71 String baseStr = "The quick brown fox jumps over the lazy dog!"; 72 String subStr; 73 74 subStr = baseStr.substring(5, baseStr.length() - 4); 75 System.out.println("subStr is '" + subStr + "'"); 76 77 System.out.println("Indexes are: " + 78 baseStr.indexOf('T') + ":" + 79 subStr.indexOf('T') + ":" + 80 subStr.indexOf('u') + ":" + 81 baseStr.indexOf('!') + ":" + 82 subStr.indexOf('y') + ":" + 83 subStr.indexOf('d') + ":" + 84 baseStr.indexOf('x') + ":" + 85 subStr.indexOf('x', 0) + ":" + 86 subStr.indexOf('x', -1) + ":" + 87 subStr.indexOf('x', 200) + ":" + 88 baseStr.indexOf('x', 17) + ":" + 89 baseStr.indexOf('x', 18) + ":" + 90 baseStr.indexOf('x', 19) + ":" + 91 subStr.indexOf('x', 13) + ":" + 92 subStr.indexOf('x', 14) + ":" + 93 subStr.indexOf('&') + ":" + 94 baseStr.indexOf(0x12341234)); 95 } 96 constructorTest()97 public static void constructorTest() { 98 byte[] byteArray = "byteArray".getBytes(); 99 char[] charArray = new char[] { 'c', 'h', 'a', 'r', 'A', 'r', 'r', 'a', 'y' }; 100 String charsetName = "US-ASCII"; 101 Charset charset = Charset.forName("UTF-8"); 102 String string = "string"; 103 StringBuffer stringBuffer = new StringBuffer("stringBuffer"); 104 int [] codePoints = new int[] { 65, 66, 67, 68, 69 }; 105 StringBuilder stringBuilder = new StringBuilder("stringBuilder"); 106 107 String s1 = new String(); 108 String s2 = new String(byteArray); 109 String s3 = new String(byteArray, 1); 110 String s4 = new String(byteArray, 0, 4); 111 String s5 = new String(byteArray, 2, 4, 5); 112 113 try { 114 String s6 = new String(byteArray, 2, 4, charsetName); 115 String s7 = new String(byteArray, charsetName); 116 } catch (UnsupportedEncodingException e) { 117 System.out.println("Got unexpected UnsupportedEncodingException"); 118 } 119 String s8 = new String(byteArray, 3, 3, charset); 120 String s9 = new String(byteArray, charset); 121 String s10 = new String(charArray); 122 String s11 = new String(charArray, 0, 4); 123 String s12 = new String(string); 124 String s13 = new String(stringBuffer); 125 String s14 = new String(codePoints, 1, 3); 126 String s15 = new String(stringBuilder); 127 } 128 copyTest()129 public static void copyTest() { 130 String src = new String("Hello Android"); 131 char[] dst = new char[7]; 132 char[] tmp = null; 133 134 try { 135 src.getChars(2, 9, tmp, 0); 136 System.out.println("GLITCH: expected exception"); 137 } catch (NullPointerException npe) { 138 System.out.println("Got expected exception"); 139 } 140 141 try { 142 src.getChars(-1, 9, 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, 19, 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, 1, dst, 0); 157 System.out.println("GLITCH: expected exception"); 158 } catch (StringIndexOutOfBoundsException sioobe) { 159 System.out.println("Got expected exception"); 160 } 161 162 try { 163 src.getChars(2, 10, dst, 0); 164 System.out.println("GLITCH: expected exception"); 165 } catch (ArrayIndexOutOfBoundsException aioobe) { 166 System.out.println("Got expected exception"); 167 } 168 169 src.getChars(2, 9, dst, 0); 170 System.out.println(new String(dst)); 171 } 172 } 173