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