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 junit.framework.Assert; 18 19 public class Main { 20 public static void main(String args[]) { 21 test_Double_doubleToRawLongBits(); 22 test_Double_longBitsToDouble(); 23 test_Float_floatToRawIntBits(); 24 test_Float_intBitsToFloat(); 25 test_Math_abs_I(); 26 test_Math_abs_J(); 27 test_Math_min(); 28 test_Math_max(); 29 test_StrictMath_abs_I(); 30 test_StrictMath_abs_J(); 31 test_StrictMath_min(); 32 test_StrictMath_max(); 33 test_String_charAt(); 34 test_String_compareTo(); 35 test_String_indexOf(); 36 test_String_isEmpty(); 37 test_String_length(); 38 } 39 40 public static void test_String_length() { 41 String str0 = ""; 42 String str1 = "x"; 43 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; 44 45 Assert.assertEquals(str0.length(), 0); 46 Assert.assertEquals(str1.length(), 1); 47 Assert.assertEquals(str80.length(), 80); 48 49 String strNull = null; 50 try { 51 strNull.length(); 52 Assert.fail(); 53 } catch (NullPointerException expected) { 54 } 55 } 56 57 public static void test_String_isEmpty() { 58 String str0 = ""; 59 String str1 = "x"; 60 61 Assert.assertTrue(str0.isEmpty()); 62 Assert.assertFalse(str1.isEmpty()); 63 64 String strNull = null; 65 try { 66 strNull.isEmpty(); 67 Assert.fail(); 68 } catch (NullPointerException expected) { 69 } 70 } 71 72 public static void test_String_charAt() { 73 String testStr = "Now is the time"; 74 75 Assert.assertEquals('N', testStr.charAt(0)); 76 Assert.assertEquals('o', testStr.charAt(1)); 77 Assert.assertEquals(' ', testStr.charAt(10)); 78 Assert.assertEquals('e', testStr.charAt(testStr.length()-1)); 79 80 try { 81 testStr.charAt(-1); 82 Assert.fail(); 83 } catch (StringIndexOutOfBoundsException expected) { 84 } 85 try { 86 testStr.charAt(80); 87 Assert.fail(); 88 } catch (StringIndexOutOfBoundsException expected) { 89 } 90 91 String strNull = null; 92 try { 93 strNull.charAt(0); 94 Assert.fail(); 95 } catch (NullPointerException expected) { 96 } 97 } 98 99 public static void test_String_indexOf() { 100 String str0 = ""; 101 String str3 = "abc"; 102 String str10 = "abcdefghij"; 103 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; 104 105 int supplementaryChar = 0x20b9f; 106 String surrogatePair = "\ud842\udf9f"; 107 String stringWithSurrogates = "hello " + surrogatePair + " world"; 108 109 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length()); 110 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length()); 111 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6); 112 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1); 113 114 Assert.assertEquals(str0.indexOf('a'), -1); 115 Assert.assertEquals(str3.indexOf('a'), 0); 116 Assert.assertEquals(str3.indexOf('b'), 1); 117 Assert.assertEquals(str3.indexOf('c'), 2); 118 Assert.assertEquals(str10.indexOf('j'), 9); 119 Assert.assertEquals(str40.indexOf('a'), 0); 120 Assert.assertEquals(str40.indexOf('b'), 38); 121 Assert.assertEquals(str40.indexOf('c'), 39); 122 Assert.assertEquals(str0.indexOf('a',20), -1); 123 Assert.assertEquals(str0.indexOf('a',0), -1); 124 Assert.assertEquals(str0.indexOf('a',-1), -1); 125 Assert.assertEquals(str3.indexOf('a',0), 0); 126 Assert.assertEquals(str3.indexOf('a',1), -1); 127 Assert.assertEquals(str3.indexOf('a',1234), -1); 128 Assert.assertEquals(str3.indexOf('b',0), 1); 129 Assert.assertEquals(str3.indexOf('b',1), 1); 130 Assert.assertEquals(str3.indexOf('c',2), 2); 131 Assert.assertEquals(str10.indexOf('j',5), 9); 132 Assert.assertEquals(str10.indexOf('j',9), 9); 133 Assert.assertEquals(str40.indexOf('a',10), 10); 134 Assert.assertEquals(str40.indexOf('b',40), -1); 135 136 String strNull = null; 137 try { 138 strNull.indexOf('a'); 139 Assert.fail(); 140 } catch (NullPointerException expected) { 141 } 142 try { 143 strNull.indexOf('a', 0); 144 Assert.fail(); 145 } catch (NullPointerException expected) { 146 } 147 try { 148 strNull.indexOf('a', -1); 149 Assert.fail(); 150 } catch (NullPointerException expected) { 151 } 152 } 153 154 public static void test_String_compareTo() { 155 String test = "0123456789"; 156 String test1 = new String("0123456789"); // different object 157 String test2 = new String("0123456780"); // different value 158 String offset = new String("xxx0123456789yyy"); 159 String sub = offset.substring(3, 13); 160 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 161 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy"; 162 String lc = "abcdefg"; 163 String uc = "ABCDEFG"; 164 Object blah = new Object(); 165 166 Assert.assertTrue(lc.toUpperCase().equals(uc)); 167 168 Assert.assertEquals(str32.compareTo(str33), -1); 169 Assert.assertEquals(str33.compareTo(str32), 1); 170 171 Assert.assertTrue(test.equals(test)); 172 Assert.assertTrue(test.equals(test1)); 173 Assert.assertFalse(test.equals(test2)); 174 175 Assert.assertEquals(test.compareTo(test1), 0); 176 Assert.assertTrue(test1.compareTo(test2) > 0); 177 Assert.assertTrue(test2.compareTo(test1) < 0); 178 179 // Compare string with a nonzero offset, in left/right side. 180 Assert.assertEquals(test.compareTo(sub), 0); 181 Assert.assertEquals(sub.compareTo(test), 0); 182 Assert.assertTrue(test.equals(sub)); 183 Assert.assertTrue(sub.equals(test)); 184 // Same base, one is a substring. 185 Assert.assertFalse(offset.equals(sub)); 186 Assert.assertFalse(sub.equals(offset)); 187 // Wrong class. 188 Assert.assertFalse(test.equals(blah)); 189 190 // Null lhs - throw. 191 try { 192 test.compareTo(null); 193 Assert.fail("didn't get expected npe"); 194 } catch (NullPointerException npe) { 195 } 196 // Null rhs - okay. 197 Assert.assertFalse(test.equals(null)); 198 199 test = test.substring(1); 200 Assert.assertTrue(test.equals("123456789")); 201 Assert.assertFalse(test.equals(test1)); 202 203 test = test.substring(1); 204 Assert.assertTrue(test.equals("23456789")); 205 206 test = test.substring(1); 207 Assert.assertTrue(test.equals("3456789")); 208 209 test = test.substring(1); 210 Assert.assertTrue(test.equals("456789")); 211 212 test = test.substring(3,5); 213 Assert.assertTrue(test.equals("78")); 214 215 test = "this/is/a/path"; 216 String[] strings = test.split("/"); 217 Assert.assertEquals(4, strings.length); 218 219 Assert.assertEquals("this is a path", test.replaceAll("/", " ")); 220 Assert.assertEquals("this is a path", test.replace("/", " ")); 221 } 222 223 public static void test_Math_abs_I() { 224 Assert.assertEquals(Math.abs(0), 0); 225 Assert.assertEquals(Math.abs(123), 123); 226 Assert.assertEquals(Math.abs(-123), 123); 227 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 228 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 229 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 230 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 231 } 232 233 public static void test_Math_abs_J() { 234 Assert.assertEquals(Math.abs(0L), 0L); 235 Assert.assertEquals(Math.abs(123L), 123L); 236 Assert.assertEquals(Math.abs(-123L), 123L); 237 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE); 238 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE); 239 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 240 } 241 242 public static void test_Math_min() { 243 Assert.assertEquals(Math.min(0, 0), 0); 244 Assert.assertEquals(Math.min(1, 0), 0); 245 Assert.assertEquals(Math.min(0, 1), 0); 246 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0); 247 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 248 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 249 } 250 251 public static void test_Math_max() { 252 Assert.assertEquals(Math.max(0, 0), 0); 253 Assert.assertEquals(Math.max(1, 0), 1); 254 Assert.assertEquals(Math.max(0, 1), 1); 255 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 256 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0); 257 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 258 } 259 260 public static void test_StrictMath_abs_I() { 261 Assert.assertEquals(StrictMath.abs(0), 0); 262 Assert.assertEquals(StrictMath.abs(123), 123); 263 Assert.assertEquals(StrictMath.abs(-123), 123); 264 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 265 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 266 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 267 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 268 } 269 270 public static void test_StrictMath_abs_J() { 271 Assert.assertEquals(StrictMath.abs(0L), 0L); 272 Assert.assertEquals(StrictMath.abs(123L), 123L); 273 Assert.assertEquals(StrictMath.abs(-123L), 123L); 274 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE); 275 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE); 276 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 277 } 278 279 public static void test_StrictMath_min() { 280 Assert.assertEquals(StrictMath.min(0, 0), 0); 281 Assert.assertEquals(StrictMath.min(1, 0), 0); 282 Assert.assertEquals(StrictMath.min(0, 1), 0); 283 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0); 284 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 285 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 286 } 287 288 public static void test_StrictMath_max() { 289 Assert.assertEquals(StrictMath.max(0, 0), 0); 290 Assert.assertEquals(StrictMath.max(1, 0), 1); 291 Assert.assertEquals(StrictMath.max(0, 1), 1); 292 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 293 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0); 294 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 295 } 296 297 public static void test_Float_floatToRawIntBits() { 298 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000); 299 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0); 300 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000); 301 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000); 302 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000); 303 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000); 304 } 305 306 public static void test_Float_intBitsToFloat() { 307 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f); 308 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f); 309 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f); 310 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN); 311 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY); 312 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY); 313 } 314 315 public static void test_Double_doubleToRawLongBits() { 316 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L); 317 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L); 318 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L); 319 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L); 320 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L); 321 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L); 322 } 323 324 public static void test_Double_longBitsToDouble() { 325 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0); 326 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0); 327 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0); 328 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN); 329 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY); 330 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY); 331 } 332 } 333