jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | * Test for Jit's handling of string inline-execute. Should be tested |
| 19 | * twice - once using self-cosimulation (if available) and once without. |
| 20 | * The non-self-cosimulation test ensures that the answer computed the first |
| 21 | * time through (via the interpreter) is the same after looping enough |
| 22 | * to trigger translation. |
| 23 | */ |
| 24 | |
| 25 | import junit.framework.Assert; |
| 26 | |
| 27 | public class Main { |
| 28 | public static void main(String args[]) { |
| 29 | int i; |
| 30 | stringLengthTest(); |
| 31 | stringCharAtTest(); |
| 32 | stringIndexOfTest(); |
| 33 | for (i = 0; i < 1000; i++) |
| 34 | stringCompareToTest(); |
| 35 | } |
| 36 | |
| 37 | public static void stringLengthTest() { |
| 38 | String str0 = ""; |
| 39 | String str1 = "x"; |
| 40 | String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; |
| 41 | int len0 = str0.length(); |
| 42 | int len1 = str1.length(); |
| 43 | int len80 = str80.length(); |
| 44 | int i; |
| 45 | |
| 46 | System.out.println("Length of " + str0 + " : " + len0); |
| 47 | System.out.println("Length of " + str1 + " : " + len1); |
| 48 | System.out.println("Length of " + str80 + " : " + len80); |
| 49 | |
| 50 | for (i = 0; i < 1000; i++) { |
| 51 | assert(str0.length() == len0); |
| 52 | assert(str1.length() == len1); |
| 53 | assert(str80.length() == len80); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static void stringCharAtTest() { |
| 58 | String testStr = "Now is the time"; |
| 59 | int under = -1; |
| 60 | int over = testStr.length(); |
| 61 | int numThrown = 0; |
| 62 | int numNotThrown = 0; |
| 63 | int at0 = testStr.charAt(0); |
| 64 | int at1 = testStr.charAt(1); |
| 65 | int at10 = testStr.charAt(10); |
| 66 | int atLast = testStr.charAt(testStr.length()-1); |
| 67 | int i; |
| 68 | |
| 69 | System.out.println(testStr + "[0] = \"" + (char)at0 + "\""); |
| 70 | System.out.println(testStr + "[1] = \"" + (char)at1 + "\""); |
| 71 | System.out.println(testStr + "[10] = \"" + (char)at10 + "\""); |
| 72 | System.out.println(testStr + "[last] = \"" + (char)atLast + "\""); |
| 73 | |
| 74 | for (i = 0; i < 1000; i++) { |
| 75 | assert(at0 == testStr.charAt(0)); |
| 76 | assert(at1 == testStr.charAt(1)); |
| 77 | assert(at10 == testStr.charAt(10)); |
| 78 | assert(atLast == testStr.charAt(testStr.length()-1)); |
| 79 | } |
| 80 | |
| 81 | for (i = 0; i < 1000; i++) { |
| 82 | try { |
| 83 | testStr.charAt(under); |
| 84 | numNotThrown++; |
| 85 | } catch (StringIndexOutOfBoundsException sioobe) { |
| 86 | numThrown++; |
| 87 | } |
| 88 | try { |
| 89 | testStr.charAt(over); |
| 90 | numNotThrown++; |
| 91 | } catch (StringIndexOutOfBoundsException sioobe) { |
| 92 | numThrown++; |
| 93 | } |
| 94 | } |
| 95 | assert(numNotThrown == 0); |
| 96 | System.out.println("Num throws " + numThrown); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | public static void stringIndexOfTest() { |
| 101 | String str0 = ""; |
| 102 | String str3 = "abc"; |
| 103 | String str10 = "abcdefghij"; |
| 104 | String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; |
| 105 | int i; |
| 106 | |
| 107 | for (i = 0; i < 1000; i++) { |
| 108 | assert(str0.indexOf('a') == -1); |
| 109 | assert(str3.indexOf('a') == 0); |
| 110 | assert(str3.indexOf('b') == 1); |
| 111 | assert(str3.indexOf('c') == 2); |
| 112 | assert(str10.indexOf('j') == 9); |
| 113 | assert(str40.indexOf('a') == 0); |
| 114 | assert(str40.indexOf('b') == 38); |
| 115 | assert(str40.indexOf('c') == 39); |
| 116 | assert(str0.indexOf('a',20) == -1); |
| 117 | assert(str0.indexOf('a',0) == -1); |
| 118 | assert(str0.indexOf('a',-1) == -1); |
| 119 | assert(str3.indexOf('a',0) == 0); |
| 120 | assert(str3.indexOf('a',1) == -1); |
| 121 | assert(str3.indexOf('a',1234) == -1); |
| 122 | assert(str3.indexOf('b',0) == 1); |
| 123 | assert(str3.indexOf('b',1) == 1); |
| 124 | assert(str3.indexOf('c',2) == 2); |
| 125 | assert(str10.indexOf('j',5) == 9); |
| 126 | assert(str10.indexOf('j',9) == 9); |
| 127 | assert(str40.indexOf('a',10) == 10); |
| 128 | assert(str40.indexOf('b',40) == -1); |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | public static void stringCompareToTest() { |
| 134 | String test = "0123456789"; |
| 135 | String test1 = new String("0123456789"); // different object |
| 136 | String test2 = new String("0123456780"); // different value |
| 137 | String offset = new String("xxx0123456789yyy"); |
| 138 | String sub = offset.substring(3, 13); |
| 139 | String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 140 | String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy"; |
| 141 | String lc = "abcdefg"; |
| 142 | String uc = "ABCDEFG"; |
| 143 | Object blah = new Object(); |
| 144 | |
| 145 | for (int i = 0; i < 100; i++) { |
| 146 | String y = lc.toUpperCase(); |
| 147 | Assert.assertTrue(y.equals(uc)); |
| 148 | } |
| 149 | |
| 150 | Assert.assertEquals(str32.compareTo(str33), -1); |
| 151 | Assert.assertEquals(str33.compareTo(str32), 1); |
| 152 | |
| 153 | Assert.assertTrue(test.equals(test)); |
| 154 | Assert.assertTrue(test.equals(test1)); |
| 155 | Assert.assertFalse(test.equals(test2)); |
| 156 | |
| 157 | Assert.assertEquals(test.compareTo(test1), 0); |
| 158 | Assert.assertTrue(test1.compareTo(test2) > 0); |
| 159 | Assert.assertTrue(test2.compareTo(test1) < 0); |
| 160 | |
| 161 | /* compare string with a nonzero offset, in left/right side */ |
| 162 | Assert.assertEquals(test.compareTo(sub), 0); |
| 163 | Assert.assertEquals(sub.compareTo(test), 0); |
| 164 | Assert.assertTrue(test.equals(sub)); |
| 165 | Assert.assertTrue(sub.equals(test)); |
| 166 | /* same base, one is a substring */ |
| 167 | Assert.assertFalse(offset.equals(sub)); |
| 168 | Assert.assertFalse(sub.equals(offset)); |
| 169 | /* wrong class */ |
| 170 | Assert.assertFalse(test.equals(blah)); |
| 171 | |
| 172 | /* null ptr - throw */ |
| 173 | try { |
| 174 | test.compareTo(null); |
| 175 | Assert.fail("didn't get expected npe"); |
| 176 | } catch (NullPointerException npe) { |
| 177 | } |
| 178 | /* null ptr - ok */ |
| 179 | Assert.assertFalse(test.equals(null)); |
| 180 | |
| 181 | test = test.substring(1); |
| 182 | Assert.assertTrue(test.equals("123456789")); |
| 183 | Assert.assertFalse(test.equals(test1)); |
| 184 | |
| 185 | test = test.substring(1); |
| 186 | Assert.assertTrue(test.equals("23456789")); |
| 187 | |
| 188 | test = test.substring(1); |
| 189 | Assert.assertTrue(test.equals("3456789")); |
| 190 | |
| 191 | test = test.substring(1); |
| 192 | Assert.assertTrue(test.equals("456789")); |
| 193 | |
| 194 | test = test.substring(3,5); |
| 195 | Assert.assertTrue(test.equals("78")); |
| 196 | |
| 197 | test = "this/is/a/path"; |
| 198 | String[] strings = test.split("/"); |
| 199 | Assert.assertEquals(4, strings.length); |
| 200 | |
| 201 | Assert.assertEquals("this is a path", test.replaceAll("/", " ")); |
| 202 | Assert.assertEquals("this is a path", test.replace("/", " ")); |
| 203 | } |
| 204 | |
| 205 | } |