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