Main.java revision 1222c96fafe98061cfc57d3bd115f46edb64e624
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 17import junit.framework.Assert; 18import java.util.Arrays; 19import java.lang.reflect.Method; 20 21public class Main { 22 public static void main(String args[]) throws Exception { 23 test_Double_doubleToRawLongBits(); 24 test_Double_longBitsToDouble(); 25 test_Float_floatToRawIntBits(); 26 test_Float_intBitsToFloat(); 27 test_Math_abs_I(); 28 test_Math_abs_J(); 29 test_Math_min_I(); 30 test_Math_max_I(); 31 test_Math_min_J(); 32 test_Math_max_J(); 33 test_Math_min_F(); 34 test_Math_max_F(); 35 test_Math_min_D(); 36 test_Math_max_D(); 37 test_Short_reverseBytes(); 38 test_Integer_reverseBytes(); 39 test_Long_reverseBytes(); 40 test_Integer_reverse(); 41 test_Long_reverse(); 42 test_StrictMath_abs_I(); 43 test_StrictMath_abs_J(); 44 test_StrictMath_min_I(); 45 test_StrictMath_max_I(); 46 test_StrictMath_min_J(); 47 test_StrictMath_max_J(); 48 test_StrictMath_min_F(); 49 test_StrictMath_max_F(); 50 test_StrictMath_min_D(); 51 test_StrictMath_max_D(); 52 test_String_charAt(); 53 test_String_compareTo(); 54 test_String_indexOf(); 55 test_String_isEmpty(); 56 test_String_length(); 57 test_Thread_currentThread(); 58 initSupportMethodsForPeekPoke(); 59 test_Memory_peekByte(); 60 test_Memory_peekShort(); 61 test_Memory_peekInt(); 62 test_Memory_peekLong(); 63 test_Memory_pokeByte(); 64 test_Memory_pokeShort(); 65 test_Memory_pokeInt(); 66 test_Memory_pokeLong(); 67 } 68 69 /** 70 * Will test inlining Thread.currentThread(). 71 */ 72 public static void test_Thread_currentThread() { 73 // 1. Do not use result. 74 Thread.currentThread(); 75 76 // 2. Result should not be null. 77 Assert.assertNotNull(Thread.currentThread()); 78 } 79 80 public static void test_String_length() { 81 String str0 = ""; 82 String str1 = "x"; 83 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; 84 85 Assert.assertEquals(str0.length(), 0); 86 Assert.assertEquals(str1.length(), 1); 87 Assert.assertEquals(str80.length(), 80); 88 89 String strNull = null; 90 try { 91 strNull.length(); 92 Assert.fail(); 93 } catch (NullPointerException expected) { 94 } 95 } 96 97 public static void test_String_isEmpty() { 98 String str0 = ""; 99 String str1 = "x"; 100 101 Assert.assertTrue(str0.isEmpty()); 102 Assert.assertFalse(str1.isEmpty()); 103 104 String strNull = null; 105 try { 106 strNull.isEmpty(); 107 Assert.fail(); 108 } catch (NullPointerException expected) { 109 } 110 } 111 112 public static void test_String_charAt() { 113 String testStr = "Now is the time"; 114 115 Assert.assertEquals('N', testStr.charAt(0)); 116 Assert.assertEquals('o', testStr.charAt(1)); 117 Assert.assertEquals(' ', testStr.charAt(10)); 118 Assert.assertEquals('e', testStr.charAt(testStr.length()-1)); 119 120 try { 121 testStr.charAt(-1); 122 Assert.fail(); 123 } catch (StringIndexOutOfBoundsException expected) { 124 } 125 try { 126 testStr.charAt(80); 127 Assert.fail(); 128 } catch (StringIndexOutOfBoundsException expected) { 129 } 130 131 String strNull = null; 132 try { 133 strNull.charAt(0); 134 Assert.fail(); 135 } catch (NullPointerException expected) { 136 } 137 } 138 139 static int start; 140 private static int[] negIndex = { -100000 }; 141 public static void test_String_indexOf() { 142 String str0 = ""; 143 String str1 = "/"; 144 String str3 = "abc"; 145 String str10 = "abcdefghij"; 146 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; 147 148 int supplementaryChar = 0x20b9f; 149 String surrogatePair = "\ud842\udf9f"; 150 String stringWithSurrogates = "hello " + surrogatePair + " world"; 151 152 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length()); 153 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length()); 154 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6); 155 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1); 156 157 Assert.assertEquals(str0.indexOf('a'), -1); 158 Assert.assertEquals(str3.indexOf('a'), 0); 159 Assert.assertEquals(str3.indexOf('b'), 1); 160 Assert.assertEquals(str3.indexOf('c'), 2); 161 Assert.assertEquals(str10.indexOf('j'), 9); 162 Assert.assertEquals(str40.indexOf('a'), 0); 163 Assert.assertEquals(str40.indexOf('b'), 38); 164 Assert.assertEquals(str40.indexOf('c'), 39); 165 Assert.assertEquals(str0.indexOf('a',20), -1); 166 Assert.assertEquals(str0.indexOf('a',0), -1); 167 Assert.assertEquals(str0.indexOf('a',-1), -1); 168 Assert.assertEquals(str1.indexOf('/',++start), -1); 169 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1); 170 Assert.assertEquals(str3.indexOf('a',0), 0); 171 Assert.assertEquals(str3.indexOf('a',1), -1); 172 Assert.assertEquals(str3.indexOf('a',1234), -1); 173 Assert.assertEquals(str3.indexOf('b',0), 1); 174 Assert.assertEquals(str3.indexOf('b',1), 1); 175 Assert.assertEquals(str3.indexOf('c',2), 2); 176 Assert.assertEquals(str10.indexOf('j',5), 9); 177 Assert.assertEquals(str10.indexOf('j',9), 9); 178 Assert.assertEquals(str40.indexOf('a',10), 10); 179 Assert.assertEquals(str40.indexOf('b',40), -1); 180 181 String strNull = null; 182 try { 183 strNull.indexOf('a'); 184 Assert.fail(); 185 } catch (NullPointerException expected) { 186 } 187 try { 188 strNull.indexOf('a', 0); 189 Assert.fail(); 190 } catch (NullPointerException expected) { 191 } 192 try { 193 strNull.indexOf('a', -1); 194 Assert.fail(); 195 } catch (NullPointerException expected) { 196 } 197 } 198 199 public static void test_String_compareTo() { 200 String test = "0123456789"; 201 String test1 = new String("0123456789"); // different object 202 String test2 = new String("0123456780"); // different value 203 String offset = new String("xxx0123456789yyy"); 204 String sub = offset.substring(3, 13); 205 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 206 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy"; 207 String lc = "abcdefg"; 208 String uc = "ABCDEFG"; 209 Object blah = new Object(); 210 211 Assert.assertTrue(lc.toUpperCase().equals(uc)); 212 213 Assert.assertEquals(str32.compareTo(str33), -1); 214 Assert.assertEquals(str33.compareTo(str32), 1); 215 216 Assert.assertTrue(test.equals(test)); 217 Assert.assertTrue(test.equals(test1)); 218 Assert.assertFalse(test.equals(test2)); 219 220 Assert.assertEquals(test.compareTo(test1), 0); 221 Assert.assertTrue(test1.compareTo(test2) > 0); 222 Assert.assertTrue(test2.compareTo(test1) < 0); 223 224 // Compare string with a nonzero offset, in left/right side. 225 Assert.assertEquals(test.compareTo(sub), 0); 226 Assert.assertEquals(sub.compareTo(test), 0); 227 Assert.assertTrue(test.equals(sub)); 228 Assert.assertTrue(sub.equals(test)); 229 // Same base, one is a substring. 230 Assert.assertFalse(offset.equals(sub)); 231 Assert.assertFalse(sub.equals(offset)); 232 // Wrong class. 233 Assert.assertFalse(test.equals(blah)); 234 235 // Null lhs - throw. 236 try { 237 test.compareTo(null); 238 Assert.fail("didn't get expected npe"); 239 } catch (NullPointerException npe) { 240 } 241 // Null rhs - okay. 242 Assert.assertFalse(test.equals(null)); 243 244 test = test.substring(1); 245 Assert.assertTrue(test.equals("123456789")); 246 Assert.assertFalse(test.equals(test1)); 247 248 test = test.substring(1); 249 Assert.assertTrue(test.equals("23456789")); 250 251 test = test.substring(1); 252 Assert.assertTrue(test.equals("3456789")); 253 254 test = test.substring(1); 255 Assert.assertTrue(test.equals("456789")); 256 257 test = test.substring(3,5); 258 Assert.assertTrue(test.equals("78")); 259 260 test = "this/is/a/path"; 261 String[] strings = test.split("/"); 262 Assert.assertEquals(4, strings.length); 263 264 Assert.assertEquals("this is a path", test.replaceAll("/", " ")); 265 Assert.assertEquals("this is a path", test.replace("/", " ")); 266 } 267 268 public static void test_Math_abs_I() { 269 Assert.assertEquals(Math.abs(0), 0); 270 Assert.assertEquals(Math.abs(123), 123); 271 Assert.assertEquals(Math.abs(-123), 123); 272 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 273 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 274 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 275 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 276 } 277 278 public static void test_Math_abs_J() { 279 Assert.assertEquals(Math.abs(0L), 0L); 280 Assert.assertEquals(Math.abs(123L), 123L); 281 Assert.assertEquals(Math.abs(-123L), 123L); 282 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE); 283 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE); 284 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 285 } 286 287 public static void test_Math_min_I() { 288 Assert.assertEquals(Math.min(0, 0), 0); 289 Assert.assertEquals(Math.min(1, 0), 0); 290 Assert.assertEquals(Math.min(0, 1), 0); 291 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0); 292 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 293 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 294 } 295 296 public static void test_Math_max_I() { 297 Assert.assertEquals(Math.max(0, 0), 0); 298 Assert.assertEquals(Math.max(1, 0), 1); 299 Assert.assertEquals(Math.max(0, 1), 1); 300 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 301 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0); 302 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 303 } 304 305 public static void test_Math_min_J() { 306 Assert.assertEquals(Math.min(0L, 0L), 0L); 307 Assert.assertEquals(Math.min(1L, 0L), 0L); 308 Assert.assertEquals(Math.min(0L, 1L), 0L); 309 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L); 310 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); 311 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); 312 } 313 314 public static void test_Math_max_J() { 315 Assert.assertEquals(Math.max(0L, 0L), 0L); 316 Assert.assertEquals(Math.max(1L, 0L), 1L); 317 Assert.assertEquals(Math.max(0L, 1L), 1L); 318 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); 319 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L); 320 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); 321 } 322 323 public static void test_Math_min_F() { 324 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN))); 325 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.0f))); 326 Assert.assertEquals(Math.min(-0.0f, 0.0f), -0.0f); 327 Assert.assertEquals(Math.min(0.0f, -0.0f), -0.0f); 328 Assert.assertEquals(Math.min(-0.0f, -0.0f), -0.0f); 329 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f); 330 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f); 331 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f); 332 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f); 333 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f); 334 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE); 335 } 336 337 public static void test_Math_max_F() { 338 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN))); 339 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f))); 340 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f); 341 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f); 342 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f); 343 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f); 344 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f); 345 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f); 346 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE); 347 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE); 348 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE); 349 } 350 351 public static void test_Math_min_D() { 352 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN))); 353 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d))); 354 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d); 355 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d); 356 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d); 357 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d); 358 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d); 359 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d); 360 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d); 361 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d); 362 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE); 363 } 364 365 public static void test_Math_max_D() { 366 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN))); 367 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d))); 368 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d); 369 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d); 370 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d); 371 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d); 372 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d); 373 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d); 374 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE); 375 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE); 376 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE); 377 } 378 379 public static void test_StrictMath_abs_I() { 380 Assert.assertEquals(StrictMath.abs(0), 0); 381 Assert.assertEquals(StrictMath.abs(123), 123); 382 Assert.assertEquals(StrictMath.abs(-123), 123); 383 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 384 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 385 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 386 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 387 } 388 389 public static void test_StrictMath_abs_J() { 390 Assert.assertEquals(StrictMath.abs(0L), 0L); 391 Assert.assertEquals(StrictMath.abs(123L), 123L); 392 Assert.assertEquals(StrictMath.abs(-123L), 123L); 393 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE); 394 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE); 395 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 396 } 397 398 public static void test_StrictMath_min_I() { 399 Assert.assertEquals(StrictMath.min(0, 0), 0); 400 Assert.assertEquals(StrictMath.min(1, 0), 0); 401 Assert.assertEquals(StrictMath.min(0, 1), 0); 402 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0); 403 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 404 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 405 } 406 407 public static void test_StrictMath_max_I() { 408 Assert.assertEquals(StrictMath.max(0, 0), 0); 409 Assert.assertEquals(StrictMath.max(1, 0), 1); 410 Assert.assertEquals(StrictMath.max(0, 1), 1); 411 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 412 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0); 413 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 414 } 415 416 public static void test_StrictMath_min_J() { 417 Assert.assertEquals(StrictMath.min(0L, 0L), 0L); 418 Assert.assertEquals(StrictMath.min(1L, 0L), 0L); 419 Assert.assertEquals(StrictMath.min(0L, 1L), 0L); 420 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L); 421 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); 422 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); 423 } 424 425 public static void test_StrictMath_max_J() { 426 Assert.assertEquals(StrictMath.max(0L, 0L), 0L); 427 Assert.assertEquals(StrictMath.max(1L, 0L), 1L); 428 Assert.assertEquals(StrictMath.max(0L, 1L), 1L); 429 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); 430 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L); 431 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); 432 } 433 434 public static void test_StrictMath_min_F() { 435 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN))); 436 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f))); 437 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f); 438 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f); 439 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f); 440 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f); 441 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f); 442 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f); 443 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f); 444 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f); 445 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE); 446 } 447 448 public static void test_StrictMath_max_F() { 449 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN))); 450 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f))); 451 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f); 452 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f); 453 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f); 454 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f); 455 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f); 456 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f); 457 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE); 458 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE); 459 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE); 460 } 461 462 public static void test_StrictMath_min_D() { 463 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN))); 464 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d))); 465 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d); 466 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d); 467 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d); 468 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d); 469 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d); 470 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d); 471 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d); 472 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d); 473 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE); 474 } 475 476 public static void test_StrictMath_max_D() { 477 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN))); 478 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d))); 479 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d); 480 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d); 481 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d); 482 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d); 483 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d); 484 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d); 485 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE); 486 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE); 487 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE); 488 } 489 490 public static void test_Float_floatToRawIntBits() { 491 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000); 492 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0); 493 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000); 494 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000); 495 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000); 496 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000); 497 } 498 499 public static void test_Float_intBitsToFloat() { 500 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f); 501 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f); 502 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f); 503 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN); 504 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY); 505 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY); 506 } 507 508 public static void test_Double_doubleToRawLongBits() { 509 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L); 510 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L); 511 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L); 512 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L); 513 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L); 514 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L); 515 } 516 517 public static void test_Double_longBitsToDouble() { 518 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0); 519 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0); 520 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0); 521 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN); 522 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY); 523 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY); 524 } 525 526 public static void test_Short_reverseBytes() { 527 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000); 528 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff); 529 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080); 530 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000); 531 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301); 532 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745); 533 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89); 534 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd); 535 } 536 537 public static void test_Integer_reverseBytes() { 538 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000); 539 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff); 540 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080); 541 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000); 542 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301); 543 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89); 544 } 545 546 public static void test_Long_reverseBytes() { 547 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L); 548 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL); 549 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L); 550 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L); 551 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L); 552 } 553 554 public static void test_Integer_reverse() { 555 Assert.assertEquals(Integer.reverse(1), 0x80000000); 556 Assert.assertEquals(Integer.reverse(-1), 0xffffffff); 557 Assert.assertEquals(Integer.reverse(0), 0); 558 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48); 559 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1); 560 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe); 561 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1); 562 } 563 564 public static void test_Long_reverse() { 565 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L); 566 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL); 567 Assert.assertEquals(Long.reverse(0L), 0L); 568 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L); 569 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L); 570 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL); 571 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L); 572 } 573 574 static Object runtime; 575 static Method address_of; 576 static Method new_non_movable_array; 577 static Method peek_byte; 578 static Method peek_short; 579 static Method peek_int; 580 static Method peek_long; 581 static Method poke_byte; 582 static Method poke_short; 583 static Method poke_int; 584 static Method poke_long; 585 586 public static void initSupportMethodsForPeekPoke() throws Exception { 587 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); 588 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); 589 runtime = get_runtime.invoke(null); 590 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class); 591 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE); 592 593 Class<?> io_memory = Class.forName("libcore.io.Memory"); 594 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE); 595 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE); 596 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE); 597 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE); 598 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE); 599 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE); 600 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE); 601 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE); 602 } 603 604 public static void test_Memory_peekByte() throws Exception { 605 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2); 606 b[0] = 0x12; 607 b[1] = 0x11; 608 long address = (long)address_of.invoke(runtime, b); 609 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12); 610 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11); 611 } 612 613 public static void test_Memory_peekShort() throws Exception { 614 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3); 615 b[0] = 0x13; 616 b[1] = 0x12; 617 b[2] = 0x11; 618 long address = (long)address_of.invoke(runtime, b); 619 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read 620 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read 621 } 622 623 public static void test_Memory_peekInt() throws Exception { 624 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5); 625 b[0] = 0x15; 626 b[1] = 0x14; 627 b[2] = 0x13; 628 b[3] = 0x12; 629 b[4] = 0x11; 630 long address = (long)address_of.invoke(runtime, b); 631 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415); 632 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314); 633 } 634 635 public static void test_Memory_peekLong() throws Exception { 636 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9); 637 b[0] = 0x19; 638 b[1] = 0x18; 639 b[2] = 0x17; 640 b[3] = 0x16; 641 b[4] = 0x15; 642 b[5] = 0x14; 643 b[6] = 0x13; 644 b[7] = 0x12; 645 b[8] = 0x11; 646 long address = (long)address_of.invoke(runtime, b); 647 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L); 648 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L); 649 } 650 651 public static void test_Memory_pokeByte() throws Exception { 652 byte[] r = {0x11, 0x12}; 653 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2); 654 long address = (long)address_of.invoke(runtime, b); 655 poke_byte.invoke(null, address, (byte)0x11); 656 poke_byte.invoke(null, address + 1, (byte)0x12); 657 Assert.assertTrue(Arrays.equals(r, b)); 658 } 659 660 public static void test_Memory_pokeShort() throws Exception { 661 byte[] ra = {0x12, 0x11, 0x13}; 662 byte[] ru = {0x12, 0x22, 0x21}; 663 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3); 664 long address = (long)address_of.invoke(runtime, b); 665 666 // Aligned write 667 b[2] = 0x13; 668 poke_short.invoke(null, address, (short)0x1112, false); 669 Assert.assertTrue(Arrays.equals(ra, b)); 670 671 // Unaligned write 672 poke_short.invoke(null, address + 1, (short)0x2122, false); 673 Assert.assertTrue(Arrays.equals(ru, b)); 674 } 675 676 public static void test_Memory_pokeInt() throws Exception { 677 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15}; 678 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21}; 679 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5); 680 long address = (long)address_of.invoke(runtime, b); 681 682 b[4] = 0x15; 683 poke_int.invoke(null, address, (int)0x11121314, false); 684 Assert.assertTrue(Arrays.equals(ra, b)); 685 686 poke_int.invoke(null, address + 1, (int)0x21222324, false); 687 Assert.assertTrue(Arrays.equals(ru, b)); 688 } 689 690 public static void test_Memory_pokeLong() throws Exception { 691 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19}; 692 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21}; 693 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9); 694 long address = (long)address_of.invoke(runtime, b); 695 696 b[8] = 0x19; 697 poke_long.invoke(null, address, (long)0x1112131415161718L, false); 698 Assert.assertTrue(Arrays.equals(ra, b)); 699 700 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false); 701 Assert.assertTrue(Arrays.equals(ru, b)); 702 } 703} 704