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 { main(String args[])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_sqrt(); 38 test_Math_ceil(); 39 test_Math_floor(); 40 test_Math_rint(); 41 test_Math_round_D(); 42 test_Math_round_F(); 43 test_Math_isNaN_D(); 44 test_Math_isNaN_F(); 45 test_Math_isInfinite_D(); 46 test_Math_isInfinite_F(); 47 test_Math_multiplyHigh(); 48 test_Short_reverseBytes(); 49 test_Integer_reverseBytes(); 50 test_Long_reverseBytes(); 51 test_Integer_reverse(); 52 test_Long_reverse(); 53 test_Integer_numberOfLeadingZeros(); 54 test_Long_numberOfLeadingZeros(); 55 test_StrictMath_abs_I(); 56 test_StrictMath_abs_J(); 57 test_StrictMath_min_I(); 58 test_StrictMath_max_I(); 59 test_StrictMath_min_J(); 60 test_StrictMath_max_J(); 61 test_StrictMath_min_F(); 62 test_StrictMath_max_F(); 63 test_StrictMath_min_D(); 64 test_StrictMath_max_D(); 65 test_StrictMath_sqrt(); 66 test_StrictMath_ceil(); 67 test_StrictMath_floor(); 68 test_StrictMath_rint(); 69 test_StrictMath_round_D(); 70 test_StrictMath_round_F(); 71 test_String_charAt(); 72 test_String_compareTo(); 73 test_String_indexOf(); 74 test_String_isEmpty(); 75 test_String_length(); 76 test_Thread_currentThread(); 77 initSupportMethodsForPeekPoke(); 78 test_Memory_peekByte(); 79 test_Memory_peekShort(); 80 test_Memory_peekInt(); 81 test_Memory_peekLong(); 82 test_Memory_pokeByte(); 83 test_Memory_pokeShort(); 84 test_Memory_pokeInt(); 85 test_Memory_pokeLong(); 86 test_Integer_divideUnsigned(); 87 test_Long_divideUnsigned(); 88 test_Integer_numberOfTrailingZeros(); 89 test_Long_numberOfTrailingZeros(); 90 test_Integer_rotateRight(); 91 test_Long_rotateRight(); 92 test_Integer_rotateLeft(); 93 test_Long_rotateLeft(); 94 test_Integer_rotateRightLeft(); 95 test_Long_rotateRightLeft(); 96 } 97 98 /** 99 * Will test inlining Thread.currentThread(). 100 */ test_Thread_currentThread()101 public static void test_Thread_currentThread() { 102 // 1. Do not use result. 103 Thread.currentThread(); 104 105 // 2. Result should not be null. 106 Assert.assertNotNull(Thread.currentThread()); 107 } 108 test_String_length()109 public static void test_String_length() { 110 String str0 = ""; 111 String str1 = "x"; 112 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; 113 114 Assert.assertEquals(str0.length(), 0); 115 Assert.assertEquals(str1.length(), 1); 116 Assert.assertEquals(str80.length(), 80); 117 118 String strNull = null; 119 try { 120 strNull.length(); 121 Assert.fail(); 122 } catch (NullPointerException expected) { 123 } 124 } 125 test_String_isEmpty()126 public static void test_String_isEmpty() { 127 String str0 = ""; 128 String str1 = "x"; 129 130 Assert.assertTrue(str0.isEmpty()); 131 Assert.assertFalse(str1.isEmpty()); 132 133 String strNull = null; 134 try { 135 strNull.isEmpty(); 136 Assert.fail(); 137 } catch (NullPointerException expected) { 138 } 139 } 140 141 // Break up the charAt tests. The optimizing compiler doesn't optimize methods with try-catch yet, 142 // so we need to separate out the tests that are expected to throw exception 143 test_String_charAt()144 public static void test_String_charAt() { 145 String testStr = "Now is the time to test some stuff"; 146 147 Assert.assertEquals(testStr.length() - 1, 33); // 33 = testStr.length()-1 as a constant. 148 Assert.assertEquals('f', testStr.charAt(33)); 149 150 test_String_charAt(testStr, 'N', 'o', ' ', 'f'); 151 test_String_charAt(testStr.substring(3,15), ' ', 'i', 'm', 'e'); 152 } test_String_charAt(String testStr, char a, char b, char c, char d)153 public static void test_String_charAt(String testStr, char a, char b, char c, char d) { 154 Assert.assertEquals(a, testStr.charAt(0)); 155 Assert.assertEquals(b, testStr.charAt(1)); 156 Assert.assertEquals(c, testStr.charAt(10)); 157 Assert.assertEquals(d, testStr.charAt(testStr.length()-1)); 158 159 test_String_charAtExc(testStr); 160 test_String_charAtExc2(testStr); 161 } 162 test_String_charAtExc(String testStr)163 private static void test_String_charAtExc(String testStr) { 164 try { 165 testStr.charAt(-1); 166 Assert.fail(); 167 } catch (StringIndexOutOfBoundsException expected) { 168 } 169 try { 170 testStr.charAt(80); 171 Assert.fail(); 172 } catch (StringIndexOutOfBoundsException expected) { 173 } 174 try { 175 if (testStr.length() == 34) { 176 testStr.charAt(34); // 34 = "Now is the time to test some stuff".length() 177 } else { 178 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length() 179 testStr.charAt(12); 180 } 181 Assert.fail(); 182 } catch (StringIndexOutOfBoundsException expected) { 183 } 184 try { 185 test_String_charAt_inner(testStr, -1); 186 Assert.fail(); 187 } catch (StringIndexOutOfBoundsException expected) { 188 } 189 try { 190 test_String_charAt_inner(testStr, 80); 191 Assert.fail(); 192 } catch (StringIndexOutOfBoundsException expected) { 193 } 194 try { 195 if (testStr.length() == 34) { 196 // 34 = "Now is the time to test some stuff".length() 197 test_String_charAt_inner(testStr, 34); 198 } else { 199 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length() 200 test_String_charAt_inner(testStr, 12); 201 } 202 Assert.fail(); 203 } catch (StringIndexOutOfBoundsException expected) { 204 } 205 206 String strEmpty = ""; 207 try { 208 strEmpty.charAt(0); 209 Assert.fail(); 210 } catch (StringIndexOutOfBoundsException expected) { 211 } 212 213 String strNull = null; 214 try { 215 strNull.charAt(0); 216 Assert.fail(); 217 } catch (NullPointerException expected) { 218 } 219 } 220 test_String_charAt_inner(String s, int index)221 private static char test_String_charAt_inner(String s, int index) { 222 // Using non-constant index here (assuming that this method wasn't inlined). 223 return s.charAt(index); 224 } 225 test_String_charAtExc2(String testStr)226 private static void test_String_charAtExc2(String testStr) { 227 try { 228 test_String_charAtExc3(testStr); 229 Assert.fail(); 230 } catch (StringIndexOutOfBoundsException expected) { 231 } 232 try { 233 test_String_charAtExc4(testStr); 234 Assert.fail(); 235 } catch (StringIndexOutOfBoundsException expected) { 236 } 237 } 238 test_String_charAtExc3(String testStr)239 private static void test_String_charAtExc3(String testStr) { 240 Assert.assertEquals('N', testStr.charAt(-1)); 241 } 242 test_String_charAtExc4(String testStr)243 private static void test_String_charAtExc4(String testStr) { 244 Assert.assertEquals('N', testStr.charAt(100)); 245 } 246 247 static int start; 248 private static int[] negIndex = { -100000 }; test_String_indexOf()249 public static void test_String_indexOf() { 250 String str0 = ""; 251 String str1 = "/"; 252 String str3 = "abc"; 253 String str10 = "abcdefghij"; 254 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; 255 256 Assert.assertEquals(str0.indexOf('a'), -1); 257 Assert.assertEquals(str3.indexOf('a'), 0); 258 Assert.assertEquals(str3.indexOf('b'), 1); 259 Assert.assertEquals(str3.indexOf('c'), 2); 260 Assert.assertEquals(str10.indexOf('j'), 9); 261 Assert.assertEquals(str40.indexOf('a'), 0); 262 Assert.assertEquals(str40.indexOf('b'), 38); 263 Assert.assertEquals(str40.indexOf('c'), 39); 264 Assert.assertEquals(str0.indexOf('a',20), -1); 265 Assert.assertEquals(str0.indexOf('a',0), -1); 266 Assert.assertEquals(str0.indexOf('a',-1), -1); 267 Assert.assertEquals(str1.indexOf('/',++start), -1); 268 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1); 269 Assert.assertEquals(str3.indexOf('a',0), 0); 270 Assert.assertEquals(str3.indexOf('a',1), -1); 271 Assert.assertEquals(str3.indexOf('a',1234), -1); 272 Assert.assertEquals(str3.indexOf('b',0), 1); 273 Assert.assertEquals(str3.indexOf('b',1), 1); 274 Assert.assertEquals(str3.indexOf('c',2), 2); 275 Assert.assertEquals(str10.indexOf('j',5), 9); 276 Assert.assertEquals(str10.indexOf('j',9), 9); 277 Assert.assertEquals(str40.indexOf('a',10), 10); 278 Assert.assertEquals(str40.indexOf('b',40), -1); 279 280 testIndexOfNull(); 281 282 // Same data as above, but stored so it's not a literal in the next test. -2 stands for 283 // indexOf(I) instead of indexOf(II). 284 start--; 285 int[][] searchData = { 286 { 'a', -2, -1 }, 287 { 'a', -2, 0 }, 288 { 'b', -2, 1 }, 289 { 'c', -2, 2 }, 290 { 'j', -2, 9 }, 291 { 'a', -2, 0 }, 292 { 'b', -2, 38 }, 293 { 'c', -2, 39 }, 294 { 'a', 20, -1 }, 295 { 'a', 0, -1 }, 296 { 'a', -1, -1 }, 297 { '/', ++start, -1 }, 298 { 'a', negIndex[0], -1 }, 299 { 'a', 0, 0 }, 300 { 'a', 1, -1 }, 301 { 'a', 1234, -1 }, 302 { 'b', 0, 1 }, 303 { 'b', 1, 1 }, 304 { 'c', 2, 2 }, 305 { 'j', 5, 9 }, 306 { 'j', 9, 9 }, 307 { 'a', 10, 10 }, 308 { 'b', 40, -1 }, 309 }; 310 testStringIndexOfChars(searchData); 311 312 testSurrogateIndexOf(); 313 } 314 testStringIndexOfChars(int[][] searchData)315 private static void testStringIndexOfChars(int[][] searchData) { 316 // Use a try-catch to avoid inlining. 317 try { 318 testStringIndexOfCharsImpl(searchData); 319 } catch (Exception e) { 320 System.out.println("Unexpected exception"); 321 } 322 } 323 testStringIndexOfCharsImpl(int[][] searchData)324 private static void testStringIndexOfCharsImpl(int[][] searchData) { 325 String str0 = ""; 326 String str1 = "/"; 327 String str3 = "abc"; 328 String str10 = "abcdefghij"; 329 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; 330 331 Assert.assertEquals(str0.indexOf(searchData[0][0]), searchData[0][2]); 332 Assert.assertEquals(str3.indexOf(searchData[1][0]), searchData[1][2]); 333 Assert.assertEquals(str3.indexOf(searchData[2][0]), searchData[2][2]); 334 Assert.assertEquals(str3.indexOf(searchData[3][0]), searchData[3][2]); 335 Assert.assertEquals(str10.indexOf(searchData[4][0]), searchData[4][2]); 336 Assert.assertEquals(str40.indexOf(searchData[5][0]), searchData[5][2]); 337 Assert.assertEquals(str40.indexOf(searchData[6][0]), searchData[6][2]); 338 Assert.assertEquals(str40.indexOf(searchData[7][0]), searchData[7][2]); 339 Assert.assertEquals(str0.indexOf(searchData[8][0], searchData[8][1]), searchData[8][2]); 340 Assert.assertEquals(str0.indexOf(searchData[9][0], searchData[9][1]), searchData[9][2]); 341 Assert.assertEquals(str0.indexOf(searchData[10][0], searchData[10][1]), searchData[10][2]); 342 Assert.assertEquals(str1.indexOf(searchData[11][0], searchData[11][1]), searchData[11][2]); 343 Assert.assertEquals(str1.indexOf(searchData[12][0], searchData[12][1]), searchData[12][2]); 344 Assert.assertEquals(str3.indexOf(searchData[13][0], searchData[13][1]), searchData[13][2]); 345 Assert.assertEquals(str3.indexOf(searchData[14][0], searchData[14][1]), searchData[14][2]); 346 Assert.assertEquals(str3.indexOf(searchData[15][0], searchData[15][1]), searchData[15][2]); 347 Assert.assertEquals(str3.indexOf(searchData[16][0], searchData[16][1]), searchData[16][2]); 348 Assert.assertEquals(str3.indexOf(searchData[17][0], searchData[17][1]), searchData[17][2]); 349 Assert.assertEquals(str3.indexOf(searchData[18][0], searchData[18][1]), searchData[18][2]); 350 Assert.assertEquals(str10.indexOf(searchData[19][0], searchData[19][1]), searchData[19][2]); 351 Assert.assertEquals(str10.indexOf(searchData[20][0], searchData[20][1]), searchData[20][2]); 352 Assert.assertEquals(str40.indexOf(searchData[21][0], searchData[21][1]), searchData[21][2]); 353 Assert.assertEquals(str40.indexOf(searchData[22][0], searchData[22][1]), searchData[22][2]); 354 } 355 testSurrogateIndexOf()356 private static void testSurrogateIndexOf() { 357 int supplementaryChar = 0x20b9f; 358 String surrogatePair = "\ud842\udf9f"; 359 String stringWithSurrogates = "hello " + surrogatePair + " world"; 360 361 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length()); 362 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length()); 363 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6); 364 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1); 365 366 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar - 0x10000), -1); 367 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar | 0x80000000), -1); 368 } 369 testIndexOfNull()370 private static void testIndexOfNull() { 371 String strNull = null; 372 try { 373 testNullIndex(strNull, 'a'); 374 Assert.fail(); 375 } catch (NullPointerException expected) { 376 } 377 try { 378 testNullIndex(strNull, 'a', 0); 379 Assert.fail(); 380 } catch (NullPointerException expected) { 381 } 382 try { 383 testNullIndex(strNull, 'a', -1); 384 Assert.fail(); 385 } catch (NullPointerException expected) { 386 } 387 } 388 testNullIndex(String strNull, int c)389 private static int testNullIndex(String strNull, int c) { 390 return strNull.indexOf(c); 391 } 392 testNullIndex(String strNull, int c, int startIndex)393 private static int testNullIndex(String strNull, int c, int startIndex) { 394 return strNull.indexOf(c, startIndex); 395 } 396 test_String_compareTo()397 public static void test_String_compareTo() { 398 String test = "0123456789"; 399 String test1 = new String("0123456789"); // different object 400 String test2 = new String("0123456780"); // different value 401 String offset = new String("xxx0123456789yyy"); 402 String sub = offset.substring(3, 13); 403 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 404 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy"; 405 String lc = "abcdefg"; 406 String uc = "ABCDEFG"; 407 Object blah = new Object(); 408 409 Assert.assertTrue(lc.toUpperCase().equals(uc)); 410 411 Assert.assertEquals(str32.compareTo(str33), -1); 412 Assert.assertEquals(str33.compareTo(str32), 1); 413 414 Assert.assertTrue(test.equals(test)); 415 Assert.assertTrue(test.equals(test1)); 416 Assert.assertFalse(test.equals(test2)); 417 418 Assert.assertEquals(test.compareTo(test1), 0); 419 Assert.assertTrue(test1.compareTo(test2) > 0); 420 Assert.assertTrue(test2.compareTo(test1) < 0); 421 422 // Compare string with a nonzero offset, in left/right side. 423 Assert.assertEquals(test.compareTo(sub), 0); 424 Assert.assertEquals(sub.compareTo(test), 0); 425 Assert.assertTrue(test.equals(sub)); 426 Assert.assertTrue(sub.equals(test)); 427 // Same base, one is a substring. 428 Assert.assertFalse(offset.equals(sub)); 429 Assert.assertFalse(sub.equals(offset)); 430 // Wrong class. 431 Assert.assertFalse(test.equals(blah)); 432 433 // Null lhs - throw. 434 try { 435 test.compareTo(null); 436 Assert.fail("didn't get expected npe"); 437 } catch (NullPointerException npe) { 438 } 439 // Null rhs - okay. 440 Assert.assertFalse(test.equals(null)); 441 442 test = test.substring(1); 443 Assert.assertTrue(test.equals("123456789")); 444 Assert.assertFalse(test.equals(test1)); 445 446 test = test.substring(1); 447 Assert.assertTrue(test.equals("23456789")); 448 449 test = test.substring(1); 450 Assert.assertTrue(test.equals("3456789")); 451 452 test = test.substring(1); 453 Assert.assertTrue(test.equals("456789")); 454 455 test = test.substring(3,5); 456 Assert.assertTrue(test.equals("78")); 457 458 test = "this/is/a/path"; 459 String[] strings = test.split("/"); 460 Assert.assertEquals(4, strings.length); 461 462 Assert.assertEquals("this is a path", test.replaceAll("/", " ")); 463 Assert.assertEquals("this is a path", test.replace("/", " ")); 464 } 465 466 public static void test_Math_abs_I() { 467 Math.abs(-1); 468 Assert.assertEquals(Math.abs(0), 0); 469 Assert.assertEquals(Math.abs(123), 123); 470 Assert.assertEquals(Math.abs(-123), 123); 471 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 472 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 473 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 474 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 475 } 476 477 public static void test_Math_abs_J() { 478 Math.abs(-1L); 479 Assert.assertEquals(Math.abs(0L), 0L); 480 Assert.assertEquals(Math.abs(123L), 123L); 481 Assert.assertEquals(Math.abs(-123L), 123L); 482 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE); 483 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE); 484 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 485 Assert.assertEquals(Math.abs(2147483648L), 2147483648L); 486 } 487 488 public static void test_Math_min_I() { 489 Math.min(1, 0); 490 Assert.assertEquals(Math.min(0, 0), 0); 491 Assert.assertEquals(Math.min(1, 0), 0); 492 Assert.assertEquals(Math.min(0, 1), 0); 493 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0); 494 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 495 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 496 } 497 498 public static void test_Math_max_I() { 499 Math.max(1, 0); 500 Assert.assertEquals(Math.max(0, 0), 0); 501 Assert.assertEquals(Math.max(1, 0), 1); 502 Assert.assertEquals(Math.max(0, 1), 1); 503 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 504 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0); 505 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 506 } 507 508 public static void test_Math_min_J() { 509 Math.min(1L, 0L); 510 Assert.assertEquals(Math.min(0L, 0L), 0L); 511 Assert.assertEquals(Math.min(1L, 0L), 0L); 512 Assert.assertEquals(Math.min(0L, 1L), 0L); 513 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L); 514 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); 515 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); 516 } 517 518 public static void test_Math_max_J() { 519 Math.max(1L, 0L); 520 Assert.assertEquals(Math.max(0L, 0L), 0L); 521 Assert.assertEquals(Math.max(1L, 0L), 1L); 522 Assert.assertEquals(Math.max(0L, 1L), 1L); 523 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); 524 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L); 525 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); 526 } 527 528 public static void test_Math_min_F() { 529 Math.min(1.0f, Float.NaN); 530 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN))); 531 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.0f))); 532 Assert.assertEquals(Math.min(-0.0f, 0.0f), -0.0f); 533 Assert.assertEquals(Math.min(0.0f, -0.0f), -0.0f); 534 Assert.assertEquals(Math.min(-0.0f, -0.0f), -0.0f); 535 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f); 536 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f); 537 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f); 538 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f); 539 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f); 540 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE); 541 // Should not have flush-to-zero behavior. 542 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MIN_VALUE), Float.MIN_VALUE); 543 } 544 545 public static void test_Math_max_F() { 546 Math.max(1.0f, Float.NaN); 547 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN))); 548 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f))); 549 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f); 550 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f); 551 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f); 552 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f); 553 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f); 554 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f); 555 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE); 556 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE); 557 // Should not have flush-to-zero behavior. 558 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE); 559 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MIN_VALUE), Float.MIN_VALUE); 560 } 561 562 public static void test_Math_min_D() { 563 Math.min(1.0d, Double.NaN); 564 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN))); 565 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d))); 566 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d); 567 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d); 568 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d); 569 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d); 570 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d); 571 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d); 572 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d); 573 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d); 574 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE); 575 // Should not have flush-to-zero behavior. 576 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MIN_VALUE), Double.MIN_VALUE); 577 } 578 579 public static void test_Math_max_D() { 580 Math.max(1.0d, Double.NaN); 581 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN))); 582 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d))); 583 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d); 584 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d); 585 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d); 586 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d); 587 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d); 588 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d); 589 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE); 590 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE); 591 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE); 592 // Should not have flush-to-zero behavior. 593 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE); 594 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MIN_VALUE), Double.MIN_VALUE); 595 } 596 597 public static void test_Math_sqrt() { 598 Math.sqrt(+4.0); 599 Assert.assertEquals(Math.sqrt(+4.0), +2.0d, 0.0); 600 Assert.assertEquals(Math.sqrt(+49.0), +7.0d, 0.0); 601 Assert.assertEquals(Math.sqrt(+1.44), +1.2d, 0.0); 602 } 603 604 public static void test_Math_ceil() { 605 Math.ceil(-0.9); 606 Assert.assertEquals(Math.ceil(+0.0), +0.0d, 0.0); 607 Assert.assertEquals(Math.ceil(-0.0), -0.0d, 0.0); 608 Assert.assertEquals(Math.ceil(-0.9), -0.0d, 0.0); 609 Assert.assertEquals(Math.ceil(-0.5), -0.0d, 0.0); 610 Assert.assertEquals(Math.ceil(0.0), -0.0d, 0.0); 611 Assert.assertEquals(Math.ceil(+2.0), +2.0d, 0.0); 612 Assert.assertEquals(Math.ceil(+2.1), +3.0d, 0.0); 613 Assert.assertEquals(Math.ceil(+2.5), +3.0d, 0.0); 614 Assert.assertEquals(Math.ceil(+2.9), +3.0d, 0.0); 615 Assert.assertEquals(Math.ceil(+3.0), +3.0d, 0.0); 616 Assert.assertEquals(Math.ceil(-2.0), -2.0d, 0.0); 617 Assert.assertEquals(Math.ceil(-2.1), -2.0d, 0.0); 618 Assert.assertEquals(Math.ceil(-2.5), -2.0d, 0.0); 619 Assert.assertEquals(Math.ceil(-2.9), -2.0d, 0.0); 620 Assert.assertEquals(Math.ceil(-3.0), -3.0d, 0.0); 621 // 2^52 - 1.5 622 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)), 623 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0); 624 // 2^52 - 0.5 625 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)), 626 Double.longBitsToDouble(0x4330000000000000l), 0.0); 627 // 2^52 628 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4330000000000000l)), 629 Double.longBitsToDouble(0x4330000000000000l), 0.0); 630 // 2^53 - 1 631 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)), 632 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0); 633 // 2^53 634 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4340000000000000l)), 635 Double.longBitsToDouble(0x4340000000000000l), 0.0); 636 // 2^63 - 2^10 637 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)), 638 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0); 639 // 2^63 640 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43E0000000000000l)), 641 Double.longBitsToDouble(0x43E0000000000000l), 0.0); 642 // 2^64 643 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43F0000000000000l)), 644 Double.longBitsToDouble(0x43F0000000000000l), 0.0); 645 // -(2^52 - 1.5) 646 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)), 647 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0); 648 // -(2^52 - 0.5) 649 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)), 650 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0); 651 // -2^52 652 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC330000000000000l)), 653 Double.longBitsToDouble(0xC330000000000000l), 0.0); 654 // -(2^53 - 1) 655 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)), 656 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0); 657 // -2^53 658 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC340000000000000l)), 659 Double.longBitsToDouble(0xC340000000000000l), 0.0); 660 // -(2^63 - 2^10) 661 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)), 662 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0); 663 // -2^63 664 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3E0000000000000l)), 665 Double.longBitsToDouble(0xC3E0000000000000l), 0.0); 666 // -2^64 667 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3F0000000000000l)), 668 Double.longBitsToDouble(0xC3F0000000000000l), 0.0); 669 Assert.assertEquals(Math.ceil(Double.NaN), Double.NaN, 0.0); 670 Assert.assertEquals(Math.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 671 Assert.assertEquals(Math.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 672 } 673 674 public static void test_Math_floor() { 675 Math.floor(+2.1); 676 Assert.assertEquals(Math.floor(+0.0), +0.0d, 0.0); 677 Assert.assertEquals(Math.floor(-0.0), -0.0d, 0.0); 678 Assert.assertEquals(Math.floor(+2.0), +2.0d, 0.0); 679 Assert.assertEquals(Math.floor(+2.1), +2.0d, 0.0); 680 Assert.assertEquals(Math.floor(+2.5), +2.0d, 0.0); 681 Assert.assertEquals(Math.floor(+2.9), +2.0d, 0.0); 682 Assert.assertEquals(Math.floor(+3.0), +3.0d, 0.0); 683 Assert.assertEquals(Math.floor(-2.0), -2.0d, 0.0); 684 Assert.assertEquals(Math.floor(-2.1), -3.0d, 0.0); 685 Assert.assertEquals(Math.floor(-2.5), -3.0d, 0.0); 686 Assert.assertEquals(Math.floor(-2.9), -3.0d, 0.0); 687 Assert.assertEquals(Math.floor(-3.0), -3.0d, 0.0); 688 // 2^52 - 1.5 689 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)), 690 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0); 691 // 2^52 - 0.5 692 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)), 693 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0); 694 // 2^52 695 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4330000000000000l)), 696 Double.longBitsToDouble(0x4330000000000000l), 0.0); 697 // 2^53 - 1 698 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)), 699 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0); 700 // 2^53 701 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4340000000000000l)), 702 Double.longBitsToDouble(0x4340000000000000l), 0.0); 703 // 2^63 - 2^10 704 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)), 705 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0); 706 // 2^63 707 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43E0000000000000l)), 708 Double.longBitsToDouble(0x43E0000000000000l), 0.0); 709 // 2^64 710 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43F0000000000000l)), 711 Double.longBitsToDouble(0x43F0000000000000l), 0.0); 712 // -(2^52 - 1.5) 713 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)), 714 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0); 715 // -(2^52 - 0.5) 716 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)), 717 Double.longBitsToDouble(0xC330000000000000l), 0.0); 718 // -2^52 719 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC330000000000000l)), 720 Double.longBitsToDouble(0xC330000000000000l), 0.0); 721 // -(2^53 - 1) 722 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)), 723 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0); 724 // -2^53 725 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC340000000000000l)), 726 Double.longBitsToDouble(0xC340000000000000l), 0.0); 727 // -(2^63 - 2^10) 728 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)), 729 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0); 730 // -2^63 731 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3E0000000000000l)), 732 Double.longBitsToDouble(0xC3E0000000000000l), 0.0); 733 // -2^64 734 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3F0000000000000l)), 735 Double.longBitsToDouble(0xC3F0000000000000l), 0.0); 736 Assert.assertEquals(Math.floor(Double.NaN), Double.NaN, 0.0); 737 Assert.assertEquals(Math.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 738 Assert.assertEquals(Math.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 739 } 740 741 public static void test_Math_rint() { 742 Math.rint(+2.1); 743 Assert.assertEquals(Math.rint(+0.0), +0.0d, 0.0); 744 Assert.assertEquals(Math.rint(-0.0), -0.0d, 0.0); 745 Assert.assertEquals(Math.rint(+0.5), +0.0d, 0.0); // expects tie-to-even 746 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0); 747 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0); 748 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0); // expects tie-to-even 749 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0); 750 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0); 751 Assert.assertEquals(Math.rint(+3.5), +4.0d, 0.0); // expects tie-to-even 752 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0); 753 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0); 754 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0); // expects tie-to-even 755 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0); 756 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0); 757 Assert.assertEquals(Math.rint(-3.5), -4.0d, 0.0); // expects tie-to-even 758 // 2^52 - 1.5 759 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)), 760 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0); 761 // 2^52 - 0.5 762 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)), 763 Double.longBitsToDouble(0x4330000000000000l), 0.0); 764 // 2^52 765 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4330000000000000l)), 766 Double.longBitsToDouble(0x4330000000000000l), 0.0); 767 // 2^53 - 1 768 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)), 769 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0); 770 // 2^53 771 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4340000000000000l)), 772 Double.longBitsToDouble(0x4340000000000000l), 0.0); 773 // 2^63 - 2^10 774 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)), 775 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0); 776 // 2^63 777 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43E0000000000000l)), 778 Double.longBitsToDouble(0x43E0000000000000l), 0.0); 779 // 2^64 780 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43F0000000000000l)), 781 Double.longBitsToDouble(0x43F0000000000000l), 0.0); 782 // -(2^52 - 1.5) 783 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)), 784 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0); 785 // -(2^52 - 0.5) 786 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)), 787 Double.longBitsToDouble(0xC330000000000000l), 0.0); 788 // -2^52 789 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC330000000000000l)), 790 Double.longBitsToDouble(0xC330000000000000l), 0.0); 791 // -(2^53 - 1) 792 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)), 793 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0); 794 // -2^53 795 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC340000000000000l)), 796 Double.longBitsToDouble(0xC340000000000000l), 0.0); 797 // -(2^63 - 2^10) 798 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)), 799 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0); 800 // -2^63 801 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3E0000000000000l)), 802 Double.longBitsToDouble(0xC3E0000000000000l), 0.0); 803 // -2^64 804 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3F0000000000000l)), 805 Double.longBitsToDouble(0xC3F0000000000000l), 0.0); 806 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0); 807 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 808 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 809 } 810 811 public static void test_Math_round_D() { 812 Math.round(2.1d); 813 Assert.assertEquals(Math.round(+0.0d), (long)+0.0); 814 Assert.assertEquals(Math.round(-0.0d), (long)+0.0); 815 Assert.assertEquals(Math.round(2.0d), 2l); 816 Assert.assertEquals(Math.round(2.1d), 2l); 817 Assert.assertEquals(Math.round(2.5d), 3l); 818 Assert.assertEquals(Math.round(2.9d), 3l); 819 Assert.assertEquals(Math.round(3.0d), 3l); 820 Assert.assertEquals(Math.round(-2.0d), -2l); 821 Assert.assertEquals(Math.round(-2.1d), -2l); 822 Assert.assertEquals(Math.round(-2.5d), -2l); 823 Assert.assertEquals(Math.round(-2.9d), -3l); 824 Assert.assertEquals(Math.round(-3.0d), -3l); 825 Assert.assertEquals(Math.round(0.49999999999999994d), 0l); 826 Assert.assertEquals(Math.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1 827 Assert.assertEquals(Math.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5 828 Assert.assertEquals(Math.round(4503599627370496.0d), 4503599627370496l); // 2^52 829 Assert.assertEquals(Math.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1) 830 Assert.assertEquals(Math.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5) 831 Assert.assertEquals(Math.round(-4503599627370496.0d), -4503599627370496l); // -2^52 832 Assert.assertEquals(Math.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1 833 Assert.assertEquals(Math.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1) 834 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d); 835 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE); 836 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE); 837 Assert.assertEquals(Math.round(Double.longBitsToDouble(0x43F0000000000000l)), 838 Long.MAX_VALUE); // 2^64 839 Assert.assertEquals(Math.round(Double.longBitsToDouble(0xC3F0000000000000l)), 840 Long.MIN_VALUE); // -2^64 841 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE); 842 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE); 843 } 844 845 public static void test_Math_round_F() { 846 Math.round(2.1f); 847 Assert.assertEquals(Math.round(+0.0f), (int)+0.0); 848 Assert.assertEquals(Math.round(-0.0f), (int)+0.0); 849 Assert.assertEquals(Math.round(2.0f), 2); 850 Assert.assertEquals(Math.round(2.1f), 2); 851 Assert.assertEquals(Math.round(2.5f), 3); 852 Assert.assertEquals(Math.round(2.9f), 3); 853 Assert.assertEquals(Math.round(3.0f), 3); 854 Assert.assertEquals(Math.round(-2.0f), -2); 855 Assert.assertEquals(Math.round(-2.1f), -2); 856 Assert.assertEquals(Math.round(-2.5f), -2); 857 Assert.assertEquals(Math.round(-2.9f), -3); 858 Assert.assertEquals(Math.round(-3.0f), -3); 859 // 0.4999999701976776123046875 860 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f); 861 Assert.assertEquals(Math.round(8388607.0f), 8388607); // 2^23 - 1 862 Assert.assertEquals(Math.round(8388607.5f), 8388608); // 2^23 - 0.5 863 Assert.assertEquals(Math.round(8388608.0f), 8388608); // 2^23 864 Assert.assertEquals(Math.round(-8388607.0f), -8388607); // -(2^23 - 1) 865 Assert.assertEquals(Math.round(-8388607.5f), -8388607); // -(2^23 - 0.5) 866 Assert.assertEquals(Math.round(-8388608.0f), -8388608); // -2^23 867 Assert.assertEquals(Math.round(16777215.0f), 16777215); // 2^24 - 1 868 Assert.assertEquals(Math.round(16777216.0f), 16777216); // 2^24 869 Assert.assertEquals(Math.round(-16777215.0f), -16777215); // -(2^24 - 1) 870 Assert.assertEquals(Math.round(-16777216.0f), -16777216); // -2^24 871 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f); 872 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE); 873 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE); 874 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x4F800000)), 875 Integer.MAX_VALUE); // 2^32 876 Assert.assertEquals(Math.round(Float.intBitsToFloat(0xCF800000)), 877 Integer.MIN_VALUE); // -2^32 878 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE); 879 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE); 880 } 881 882 public static void test_Math_isNaN_D() { 883 // Quiet NaN. 884 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF4000000000000l))); 885 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF4000000000000l))); 886 // Signaling NaN. 887 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF8000000000000l))); 888 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF8000000000000l))); 889 // Distinct from +/- infinity. 890 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FF0000000000000l))); 891 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFF0000000000000l))); 892 // Distinct from normal numbers. 893 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FE0000000000000l))); 894 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFE0000000000000l))); 895 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0010000000000000l))); 896 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8010000000000000l))); 897 // Distinct from +/- zero. 898 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000000l))); 899 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000000l))); 900 // Distinct from subnormal numbers. 901 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0008000000000000l))); 902 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8008000000000000l))); 903 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000001l))); 904 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000001l))); 905 } 906 907 public static void test_Math_isNaN_F() { 908 // Quiet NaN. 909 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FA00000))); 910 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFA00000))); 911 // Signaling NaN. 912 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FC00000))); 913 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFC00000))); 914 // Distinct from +/- infinity. 915 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F800000))); 916 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF800000))); 917 // Distinct from normal numbers. 918 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F000000))); 919 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF000000))); 920 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00800000))); 921 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80800000))); 922 // Distinct from +/- zero. 923 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000000))); 924 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000000))); 925 // Distinct from subnormal numbers. 926 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00400000))); 927 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80400000))); 928 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000001))); 929 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000001))); 930 } 931 932 public static void test_Math_isInfinite_D() { 933 // Distinct from Quiet NaN. 934 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF4000000000000l))); 935 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF4000000000000l))); 936 // Distinct from Signaling NaN. 937 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF8000000000000l))); 938 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF8000000000000l))); 939 // +/- infinity. 940 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0x7FF0000000000000l))); 941 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0xFFF0000000000000l))); 942 // Distinct from normal numbers. 943 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FE0000000000000l))); 944 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFE0000000000000l))); 945 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0010000000000000l))); 946 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8010000000000000l))); 947 // Distinct from +/- zero. 948 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000000l))); 949 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000000l))); 950 // Distinct from subnormal numbers. 951 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0008000000000000l))); 952 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8008000000000000l))); 953 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000001l))); 954 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000001l))); 955 } 956 957 public static void test_Math_isInfinite_F() { 958 // Distinct from Quiet NaN. 959 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FA00000))); 960 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFA00000))); 961 // Distinct from Signaling NaN. 962 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FC00000))); 963 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFC00000))); 964 // +/- infinity. 965 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0x7F800000))); 966 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0xFF800000))); 967 // Distinct from normal numbers. 968 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7F000000))); 969 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFF000000))); 970 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00800000))); 971 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80800000))); 972 // Distinct from +/- zero. 973 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000000))); 974 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000000))); 975 // Distinct from subnormal numbers. 976 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00400000))); 977 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80400000))); 978 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000001))); 979 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000001))); 980 } 981 982 public static void test_Math_multiplyHigh() { 983 Math.multiplyHigh(2L, 3L); 984 Assert.assertEquals(Math.multiplyHigh(2L, 3L), 0L); 985 Assert.assertEquals(Math.multiplyHigh(Long.MAX_VALUE, Long.MAX_VALUE), 4611686018427387903L); 986 } 987 988 public static void test_StrictMath_abs_I() { 989 StrictMath.abs(-1); 990 Assert.assertEquals(StrictMath.abs(0), 0); 991 Assert.assertEquals(StrictMath.abs(123), 123); 992 Assert.assertEquals(StrictMath.abs(-123), 123); 993 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); 994 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); 995 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); 996 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); 997 } 998 999 public static void test_StrictMath_abs_J() { 1000 StrictMath.abs(-1L); 1001 Assert.assertEquals(StrictMath.abs(0L), 0L); 1002 Assert.assertEquals(StrictMath.abs(123L), 123L); 1003 Assert.assertEquals(StrictMath.abs(-123L), 123L); 1004 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE); 1005 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE); 1006 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); 1007 } 1008 1009 public static void test_StrictMath_min_I() { 1010 StrictMath.min(1, 0); 1011 Assert.assertEquals(StrictMath.min(0, 0), 0); 1012 Assert.assertEquals(StrictMath.min(1, 0), 0); 1013 Assert.assertEquals(StrictMath.min(0, 1), 0); 1014 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0); 1015 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); 1016 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); 1017 } 1018 1019 public static void test_StrictMath_max_I() { 1020 StrictMath.max(1, 0); 1021 Assert.assertEquals(StrictMath.max(0, 0), 0); 1022 Assert.assertEquals(StrictMath.max(1, 0), 1); 1023 Assert.assertEquals(StrictMath.max(0, 1), 1); 1024 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); 1025 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0); 1026 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); 1027 } 1028 1029 public static void test_StrictMath_min_J() { 1030 StrictMath.min(1L, 0L); 1031 Assert.assertEquals(StrictMath.min(0L, 0L), 0L); 1032 Assert.assertEquals(StrictMath.min(1L, 0L), 0L); 1033 Assert.assertEquals(StrictMath.min(0L, 1L), 0L); 1034 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L); 1035 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); 1036 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); 1037 } 1038 1039 public static void test_StrictMath_max_J() { 1040 StrictMath.max(1L, 0L); 1041 Assert.assertEquals(StrictMath.max(0L, 0L), 0L); 1042 Assert.assertEquals(StrictMath.max(1L, 0L), 1L); 1043 Assert.assertEquals(StrictMath.max(0L, 1L), 1L); 1044 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); 1045 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L); 1046 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); 1047 } 1048 1049 public static void test_StrictMath_min_F() { 1050 StrictMath.min(1.0f, Float.NaN); 1051 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN))); 1052 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f))); 1053 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f); 1054 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f); 1055 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f); 1056 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f); 1057 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f); 1058 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f); 1059 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f); 1060 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f); 1061 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE); 1062 } 1063 1064 public static void test_StrictMath_max_F() { 1065 StrictMath.max(1.0f, Float.NaN); 1066 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN))); 1067 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f))); 1068 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f); 1069 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f); 1070 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f); 1071 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f); 1072 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f); 1073 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f); 1074 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE); 1075 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE); 1076 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE); 1077 } 1078 1079 public static void test_StrictMath_min_D() { 1080 StrictMath.min(1.0d, Double.NaN); 1081 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN))); 1082 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d))); 1083 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d); 1084 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d); 1085 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d); 1086 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d); 1087 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d); 1088 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d); 1089 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d); 1090 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d); 1091 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE); 1092 } 1093 1094 public static void test_StrictMath_max_D() { 1095 StrictMath.max(1.0d, Double.NaN); 1096 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN))); 1097 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d))); 1098 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d); 1099 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d); 1100 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d); 1101 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d); 1102 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d); 1103 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d); 1104 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE); 1105 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE); 1106 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE); 1107 } 1108 1109 public static void test_StrictMath_sqrt() { 1110 StrictMath.sqrt(+4.0); 1111 Assert.assertEquals(StrictMath.sqrt(+4.0), +2.0d, 0.0); 1112 Assert.assertEquals(StrictMath.sqrt(+49.0), +7.0d, 0.0); 1113 Assert.assertEquals(StrictMath.sqrt(+1.44), +1.2d, 0.0); 1114 } 1115 1116 public static void test_StrictMath_ceil() { 1117 StrictMath.ceil(-0.9); 1118 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0); 1119 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0); 1120 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0); 1121 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0); 1122 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0); 1123 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0); 1124 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0); 1125 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0); 1126 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0); 1127 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0); 1128 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0); 1129 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0); 1130 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0); 1131 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0); 1132 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0); 1133 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0); 1134 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 1135 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 1136 } 1137 1138 public static void test_StrictMath_floor() { 1139 StrictMath.floor(+2.1); 1140 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0); 1141 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0); 1142 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0); 1143 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0); 1144 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0); 1145 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0); 1146 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0); 1147 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0); 1148 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0); 1149 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0); 1150 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0); 1151 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0); 1152 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0); 1153 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 1154 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 1155 } 1156 1157 public static void test_StrictMath_rint() { 1158 StrictMath.rint(+2.1); 1159 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0); 1160 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0); 1161 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0); 1162 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0); 1163 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0); 1164 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0); 1165 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0); 1166 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0); 1167 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0); 1168 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0); 1169 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0); 1170 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0); 1171 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0); 1172 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0); 1173 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0); 1174 } 1175 1176 public static void test_StrictMath_round_D() { 1177 StrictMath.round(2.1d); 1178 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0); 1179 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0); 1180 Assert.assertEquals(StrictMath.round(2.0d), 2l); 1181 Assert.assertEquals(StrictMath.round(2.1d), 2l); 1182 Assert.assertEquals(StrictMath.round(2.5d), 3l); 1183 Assert.assertEquals(StrictMath.round(2.9d), 3l); 1184 Assert.assertEquals(StrictMath.round(3.0d), 3l); 1185 Assert.assertEquals(StrictMath.round(-2.0d), -2l); 1186 Assert.assertEquals(StrictMath.round(-2.1d), -2l); 1187 Assert.assertEquals(StrictMath.round(-2.5d), -2l); 1188 Assert.assertEquals(StrictMath.round(-2.9d), -3l); 1189 Assert.assertEquals(StrictMath.round(-3.0d), -3l); 1190 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 0l); 1191 Assert.assertEquals(StrictMath.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1 1192 Assert.assertEquals(StrictMath.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5 1193 Assert.assertEquals(StrictMath.round(4503599627370496.0d), 4503599627370496l); // 2^52 1194 Assert.assertEquals(StrictMath.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1) 1195 Assert.assertEquals(StrictMath.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5) 1196 Assert.assertEquals(StrictMath.round(-4503599627370496.0d), -4503599627370496l); // -2^52 1197 Assert.assertEquals(StrictMath.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1 1198 Assert.assertEquals(StrictMath.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1) 1199 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d); 1200 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE); 1201 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE); 1202 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0x43F0000000000000l)), 1203 Long.MAX_VALUE); // 2^64 1204 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0xC3F0000000000000l)), 1205 Long.MIN_VALUE); // -2^64 1206 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE); 1207 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE); 1208 } 1209 1210 public static void test_StrictMath_round_F() { 1211 StrictMath.round(2.1f); 1212 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0); 1213 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0); 1214 Assert.assertEquals(StrictMath.round(2.0f), 2); 1215 Assert.assertEquals(StrictMath.round(2.1f), 2); 1216 Assert.assertEquals(StrictMath.round(2.5f), 3); 1217 Assert.assertEquals(StrictMath.round(2.9f), 3); 1218 Assert.assertEquals(StrictMath.round(3.0f), 3); 1219 Assert.assertEquals(StrictMath.round(-2.0f), -2); 1220 Assert.assertEquals(StrictMath.round(-2.1f), -2); 1221 Assert.assertEquals(StrictMath.round(-2.5f), -2); 1222 Assert.assertEquals(StrictMath.round(-2.9f), -3); 1223 Assert.assertEquals(StrictMath.round(-3.0f), -3); 1224 // 0.4999999701976776123046875 1225 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f); 1226 Assert.assertEquals(StrictMath.round(8388607.0f), 8388607); // 2^23 - 1 1227 Assert.assertEquals(StrictMath.round(8388607.5f), 8388608); // 2^23 - 0.5 1228 Assert.assertEquals(StrictMath.round(8388608.0f), 8388608); // 2^23 1229 Assert.assertEquals(StrictMath.round(-8388607.0f), -8388607); // -(2^23 - 1) 1230 Assert.assertEquals(StrictMath.round(-8388607.5f), -8388607); // -(2^23 - 0.5) 1231 Assert.assertEquals(StrictMath.round(-8388608.0f), -8388608); // -2^23 1232 Assert.assertEquals(StrictMath.round(16777215.0f), 16777215); // 2^24 - 1 1233 Assert.assertEquals(StrictMath.round(16777216.0f), 16777216); // 2^24 1234 Assert.assertEquals(StrictMath.round(-16777215.0f), -16777215); // -(2^24 - 1) 1235 Assert.assertEquals(StrictMath.round(-16777216.0f), -16777216); // -2^24 1236 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f); 1237 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE); 1238 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE); 1239 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x4F800000)), 1240 Integer.MAX_VALUE); // 2^32 1241 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0xCF800000)), 1242 Integer.MIN_VALUE); // -2^32 1243 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE); 1244 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE); 1245 } 1246 1247 public static void test_Float_floatToRawIntBits() { 1248 Float.floatToRawIntBits(-1.0f); 1249 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000); 1250 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0); 1251 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000); 1252 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000); 1253 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000); 1254 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000); 1255 } 1256 1257 public static void test_Float_intBitsToFloat() { 1258 Float.intBitsToFloat(0xbf800000); 1259 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f); 1260 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f); 1261 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f); 1262 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN); 1263 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY); 1264 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY); 1265 } 1266 1267 public static void test_Double_doubleToRawLongBits() { 1268 Double.doubleToRawLongBits(-1.0); 1269 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L); 1270 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L); 1271 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L); 1272 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L); 1273 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L); 1274 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L); 1275 } 1276 1277 public static void test_Double_longBitsToDouble() { 1278 Double.longBitsToDouble(0xbff0000000000000L); 1279 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0); 1280 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0); 1281 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0); 1282 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN); 1283 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY); 1284 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY); 1285 } 1286 1287 public static void test_Short_reverseBytes() { 1288 Short.reverseBytes((short)0x1357); 1289 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000); 1290 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff); 1291 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080); 1292 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000); 1293 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301); 1294 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745); 1295 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89); 1296 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd); 1297 } 1298 1299 public static void test_Integer_reverseBytes() { 1300 Integer.reverseBytes(0x13579bdf); 1301 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000); 1302 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff); 1303 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080); 1304 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000); 1305 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301); 1306 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89); 1307 } 1308 1309 public static void test_Long_reverseBytes() { 1310 Long.reverseBytes(0x13579bdf2468ace0L); 1311 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L); 1312 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL); 1313 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L); 1314 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L); 1315 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L); 1316 } 1317 1318 public static void test_Integer_reverse() { 1319 Integer.reverse(0x12345678); 1320 Assert.assertEquals(Integer.reverse(1), 0x80000000); 1321 Assert.assertEquals(Integer.reverse(-1), 0xffffffff); 1322 Assert.assertEquals(Integer.reverse(0), 0); 1323 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48); 1324 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1); 1325 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe); 1326 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1); 1327 } 1328 1329 public static void test_Long_reverse() { 1330 Long.reverse(0x1234567812345678L); 1331 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L); 1332 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL); 1333 Assert.assertEquals(Long.reverse(0L), 0L); 1334 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L); 1335 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L); 1336 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL); 1337 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L); 1338 1339 Assert.assertEquals(test_Long_reverse_b22324327(0xaaaaaaaaaaaaaaaaL, 0x5555555555555555L), 1340 157472205507277347L); 1341 } 1342 1343 // A bit more complicated than the above. Use local variables to stress register allocation. 1344 private static long test_Long_reverse_b22324327(long l1, long l2) { 1345 // A couple of local integers. Use them in a loop, so they get promoted. 1346 int i1 = 0, i2 = 1, i3 = 2, i4 = 3, i5 = 4, i6 = 5, i7 = 6, i8 = 7; 1347 for (int k = 0; k < 10; k++) { 1348 i1 += 1; 1349 i2 += 2; 1350 i3 += 3; 1351 i4 += 4; 1352 i5 += 5; 1353 i6 += 6; 1354 i7 += 7; 1355 i8 += 8; 1356 } 1357 1358 // Do the Long.reverse() calls, save the results. 1359 long r1 = Long.reverse(l1); 1360 long r2 = Long.reverse(l2); 1361 1362 // Some more looping with the ints. 1363 for (int k = 0; k < 10; k++) { 1364 i1 += 1; 1365 i2 += 2; 1366 i3 += 3; 1367 i4 += 4; 1368 i5 += 5; 1369 i6 += 6; 1370 i7 += 7; 1371 i8 += 8; 1372 } 1373 1374 // Include everything in the result, so things are kept live. Try to be a little bit clever to 1375 // avoid things being folded somewhere. 1376 return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8; 1377 } 1378 1379 public static boolean doThrow = false; 1380 1381 public static int $noinline$return_int_zero() { 1382 if (doThrow) { 1383 throw new Error(); 1384 } 1385 return 0; 1386 } 1387 1388 public static void test_Integer_divideUnsigned() { 1389 Assert.assertEquals(Integer.divideUnsigned(100, 10), 10); 1390 Assert.assertEquals(Integer.divideUnsigned(100, 1), 100); 1391 Assert.assertEquals(Integer.divideUnsigned(1024, 128), 8); 1392 Assert.assertEquals(Integer.divideUnsigned(12345678, 264), 46763); 1393 Assert.assertEquals(Integer.divideUnsigned(13, 5), 2); 1394 Assert.assertEquals(Integer.divideUnsigned(-2, 2), Integer.MAX_VALUE); 1395 Assert.assertEquals(Integer.divideUnsigned(-1, 2), Integer.MAX_VALUE); 1396 Assert.assertEquals(Integer.divideUnsigned(100000, -1), 0); 1397 Assert.assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, -1), 0); 1398 Assert.assertEquals(Integer.divideUnsigned(-2, -1), 0); 1399 Assert.assertEquals(Integer.divideUnsigned(-1, -2), 1); 1400 Assert.assertEquals(Integer.divideUnsigned(-173448, 13), 330368757); 1401 Assert.assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, 2), (1 << 30)); 1402 Assert.assertEquals(Integer.divideUnsigned(-1, Integer.MIN_VALUE), 1); 1403 Assert.assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, Integer.MIN_VALUE), 0); 1404 Assert.assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE), 1); 1405 1406 try { 1407 Integer.divideUnsigned(1, 0); 1408 Assert.fail("Unreachable"); 1409 } catch (ArithmeticException expected) { 1410 } 1411 } 1412 1413 1414 private static final long BIG_LONG_VALUE = 739287620162442240L; 1415 1416 public static void test_Long_divideUnsigned() { 1417 Assert.assertEquals(Long.divideUnsigned(100L, 10L), 10L); 1418 Assert.assertEquals(Long.divideUnsigned(100L, 1L), 100L); 1419 Assert.assertEquals(Long.divideUnsigned(1024L, 128L), 8L); 1420 Assert.assertEquals(Long.divideUnsigned(12345678L, 264L), 46763L); 1421 Assert.assertEquals(Long.divideUnsigned(13L, 5L), 2L); 1422 Assert.assertEquals(Long.divideUnsigned(-2L, 2L), Long.MAX_VALUE); 1423 Assert.assertEquals(Long.divideUnsigned(-1L, 2L), Long.MAX_VALUE); 1424 Assert.assertEquals(Long.divideUnsigned(100000L, -1L), 0L); 1425 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, -1L), 0L); 1426 Assert.assertEquals(Long.divideUnsigned(-2L, -1L), 0L); 1427 Assert.assertEquals(Long.divideUnsigned(-1L, -2L), 1L); 1428 Assert.assertEquals(Long.divideUnsigned(-173448L, 13L), 1418980313362259859L); 1429 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 2L), (1L << 62)); 1430 Assert.assertEquals(Long.divideUnsigned(-1L, Long.MIN_VALUE), 1L); 1431 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, Long.MIN_VALUE), 0L); 1432 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, Long.MAX_VALUE), 1L); 1433 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, 1L), Long.MAX_VALUE); 1434 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 1L), Long.MIN_VALUE); 1435 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, BIG_LONG_VALUE), 1L); 1436 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1L), BIG_LONG_VALUE); 1437 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1024L), 721960566564885L); 1438 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 0x1FFFFFFFFL), 86064406L); 1439 1440 try { 1441 Long.divideUnsigned(1L, 0L); 1442 Assert.fail("Unreachable"); 1443 } catch (ArithmeticException expected) { 1444 } 1445 } 1446 1447 public static void test_Integer_numberOfLeadingZeros() { 1448 Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE); 1449 Assert.assertEquals(Integer.numberOfLeadingZeros(1), Integer.SIZE - 1); 1450 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << (Integer.SIZE-1)), 0); 1451 Assert.assertEquals(Integer.numberOfLeadingZeros($noinline$return_int_zero()), Integer.SIZE); 1452 for (int i = 0; i < Integer.SIZE; i++) { 1453 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i); 1454 Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i); 1455 Assert.assertEquals(Integer.numberOfLeadingZeros(0xFFFFFFFF >>> i), i); 1456 } 1457 } 1458 1459 public static long $noinline$return_long_zero() { 1460 if (doThrow) { 1461 throw new Error(); 1462 } 1463 return 0; 1464 } 1465 1466 public static void test_Long_numberOfLeadingZeros() { 1467 Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE); 1468 Assert.assertEquals(Long.numberOfLeadingZeros(1L), Long.SIZE - 1); 1469 Assert.assertEquals(Long.numberOfLeadingZeros(1L << ((Long.SIZE/2)-1)), Long.SIZE/2); 1470 Assert.assertEquals(Long.numberOfLeadingZeros(1L << (Long.SIZE-1)), 0); 1471 Assert.assertEquals(Long.numberOfLeadingZeros($noinline$return_long_zero()), Long.SIZE); 1472 for (int i = 0; i < Long.SIZE; i++) { 1473 Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i); 1474 Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i); 1475 Assert.assertEquals(Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL >>> i), i); 1476 } 1477 } 1478 1479 static Object runtime; 1480 static Method address_of; 1481 static Method new_non_movable_array; 1482 static Method peek_byte; 1483 static Method peek_short; 1484 static Method peek_int; 1485 static Method peek_long; 1486 static Method poke_byte; 1487 static Method poke_short; 1488 static Method poke_int; 1489 static Method poke_long; 1490 1491 public static void initSupportMethodsForPeekPoke() throws Exception { 1492 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); 1493 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); 1494 runtime = get_runtime.invoke(null); 1495 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class); 1496 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE); 1497 1498 Class<?> io_memory = Class.forName("libcore.io.Memory"); 1499 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE); 1500 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE); 1501 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE); 1502 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE); 1503 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE); 1504 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE); 1505 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE); 1506 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE); 1507 } 1508 1509 public static void test_Memory_peekByte() throws Exception { 1510 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2); 1511 b[0] = 0x12; 1512 b[1] = 0x11; 1513 long address = (long)address_of.invoke(runtime, b); 1514 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12); 1515 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11); 1516 } 1517 1518 public static void test_Memory_peekShort() throws Exception { 1519 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3); 1520 b[0] = 0x13; 1521 b[1] = 0x12; 1522 b[2] = 0x11; 1523 long address = (long)address_of.invoke(runtime, b); 1524 peek_short.invoke(null, address, false); 1525 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read 1526 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read 1527 } 1528 1529 public static void test_Memory_peekInt() throws Exception { 1530 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5); 1531 b[0] = 0x15; 1532 b[1] = 0x14; 1533 b[2] = 0x13; 1534 b[3] = 0x12; 1535 b[4] = 0x11; 1536 long address = (long)address_of.invoke(runtime, b); 1537 peek_int.invoke(null, address, false); 1538 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415); 1539 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314); 1540 } 1541 1542 public static void test_Memory_peekLong() throws Exception { 1543 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9); 1544 b[0] = 0x19; 1545 b[1] = 0x18; 1546 b[2] = 0x17; 1547 b[3] = 0x16; 1548 b[4] = 0x15; 1549 b[5] = 0x14; 1550 b[6] = 0x13; 1551 b[7] = 0x12; 1552 b[8] = 0x11; 1553 long address = (long)address_of.invoke(runtime, b); 1554 peek_long.invoke(null, address, false); 1555 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L); 1556 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L); 1557 } 1558 1559 public static void test_Memory_pokeByte() throws Exception { 1560 byte[] r = {0x11, 0x12}; 1561 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2); 1562 long address = (long)address_of.invoke(runtime, b); 1563 poke_byte.invoke(null, address, (byte)0x11); 1564 poke_byte.invoke(null, address + 1, (byte)0x12); 1565 Assert.assertTrue(Arrays.equals(r, b)); 1566 } 1567 1568 public static void test_Memory_pokeShort() throws Exception { 1569 byte[] ra = {0x12, 0x11, 0x13}; 1570 byte[] ru = {0x12, 0x22, 0x21}; 1571 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3); 1572 long address = (long)address_of.invoke(runtime, b); 1573 1574 // Aligned write 1575 b[2] = 0x13; 1576 poke_short.invoke(null, address, (short)0x1112, false); 1577 Assert.assertTrue(Arrays.equals(ra, b)); 1578 1579 // Unaligned write 1580 poke_short.invoke(null, address + 1, (short)0x2122, false); 1581 Assert.assertTrue(Arrays.equals(ru, b)); 1582 } 1583 1584 public static void test_Memory_pokeInt() throws Exception { 1585 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15}; 1586 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21}; 1587 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5); 1588 long address = (long)address_of.invoke(runtime, b); 1589 1590 b[4] = 0x15; 1591 poke_int.invoke(null, address, (int)0x11121314, false); 1592 Assert.assertTrue(Arrays.equals(ra, b)); 1593 1594 poke_int.invoke(null, address + 1, (int)0x21222324, false); 1595 Assert.assertTrue(Arrays.equals(ru, b)); 1596 } 1597 1598 public static void test_Memory_pokeLong() throws Exception { 1599 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19}; 1600 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21}; 1601 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9); 1602 long address = (long)address_of.invoke(runtime, b); 1603 1604 b[8] = 0x19; 1605 poke_long.invoke(null, address, (long)0x1112131415161718L, false); 1606 Assert.assertTrue(Arrays.equals(ra, b)); 1607 1608 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false); 1609 Assert.assertTrue(Arrays.equals(ru, b)); 1610 } 1611 1612 public static void test_Integer_numberOfTrailingZeros() { 1613 Assert.assertEquals(Integer.numberOfTrailingZeros(0), Integer.SIZE); 1614 for (int i = 0; i < Integer.SIZE; i++) { 1615 Assert.assertEquals( 1616 Integer.numberOfTrailingZeros(0x80000000 >> i), 1617 Integer.SIZE - 1 - i); 1618 Assert.assertEquals( 1619 Integer.numberOfTrailingZeros((0x80000000 >> i) | 0x80000000), 1620 Integer.SIZE - 1 - i); 1621 Assert.assertEquals(Integer.numberOfTrailingZeros(1 << i), i); 1622 } 1623 } 1624 1625 public static void test_Long_numberOfTrailingZeros() { 1626 Assert.assertEquals(Long.numberOfTrailingZeros(0), Long.SIZE); 1627 for (int i = 0; i < Long.SIZE; i++) { 1628 Assert.assertEquals( 1629 Long.numberOfTrailingZeros(0x8000000000000000L >> i), 1630 Long.SIZE - 1 - i); 1631 Assert.assertEquals( 1632 Long.numberOfTrailingZeros((0x8000000000000000L >> i) | 0x8000000000000000L), 1633 Long.SIZE - 1 - i); 1634 Assert.assertEquals(Long.numberOfTrailingZeros(1L << i), i); 1635 } 1636 } 1637 1638 public static void test_Integer_rotateRight() throws Exception { 1639 Assert.assertEquals(Integer.rotateRight(0x11, 0), 0x11); 1640 1641 Assert.assertEquals(Integer.rotateRight(0x11, 1), 0x80000008); 1642 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE - 1), 0x22); 1643 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE), 0x11); 1644 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE + 1), 0x80000008); 1645 1646 Assert.assertEquals(Integer.rotateRight(0x11, -1), 0x22); 1647 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE - 1)), 0x80000008); 1648 Assert.assertEquals(Integer.rotateRight(0x11, -Integer.SIZE), 0x11); 1649 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE + 1)), 0x22); 1650 1651 Assert.assertEquals(Integer.rotateRight(0x80000000, 1), 0x40000000); 1652 1653 for (int i = 0; i < Integer.SIZE; i++) { 1654 Assert.assertEquals( 1655 Integer.rotateRight(0xBBAAAADD, i), 1656 (0xBBAAAADD >>> i) | (0xBBAAAADD << (Integer.SIZE - i))); 1657 } 1658 } 1659 1660 public static void test_Long_rotateRight() throws Exception { 1661 Assert.assertEquals(Long.rotateRight(0x11, 0), 0x11); 1662 1663 Assert.assertEquals(Long.rotateRight(0x11, 1), 0x8000000000000008L); 1664 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE - 1), 0x22); 1665 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE), 0x11); 1666 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE + 1), 0x8000000000000008L); 1667 1668 Assert.assertEquals(Long.rotateRight(0x11, -1), 0x22); 1669 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE - 1)), 0x8000000000000008L); 1670 Assert.assertEquals(Long.rotateRight(0x11, -Long.SIZE), 0x11); 1671 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE + 1)), 0x22); 1672 1673 Assert.assertEquals(Long.rotateRight(0x8000000000000000L, 1), 0x4000000000000000L); 1674 1675 for (int i = 0; i < Long.SIZE; i++) { 1676 Assert.assertEquals( 1677 Long.rotateRight(0xBBAAAADDFF0000DDL, i), 1678 (0xBBAAAADDFF0000DDL >>> i) | (0xBBAAAADDFF0000DDL << (Long.SIZE - i))); 1679 } 1680 } 1681 1682 public static void test_Integer_rotateLeft() throws Exception { 1683 Assert.assertEquals(Integer.rotateLeft(0x11, 0), 0x11); 1684 1685 Assert.assertEquals(Integer.rotateLeft(0x11, 1), 0x22); 1686 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE - 1), 0x80000008); 1687 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE), 0x11); 1688 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE + 1), 0x22); 1689 1690 Assert.assertEquals(Integer.rotateLeft(0x11, -1), 0x80000008); 1691 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE - 1)), 0x22); 1692 Assert.assertEquals(Integer.rotateLeft(0x11, -Integer.SIZE), 0x11); 1693 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE + 1)), 0x80000008); 1694 1695 Assert.assertEquals(Integer.rotateLeft(0xC0000000, 1), 0x80000001); 1696 1697 for (int i = 0; i < Integer.SIZE; i++) { 1698 Assert.assertEquals( 1699 Integer.rotateLeft(0xBBAAAADD, i), 1700 (0xBBAAAADD << i) | (0xBBAAAADD >>> (Integer.SIZE - i))); 1701 } 1702 } 1703 1704 public static void test_Long_rotateLeft() throws Exception { 1705 Assert.assertEquals(Long.rotateLeft(0x11, 0), 0x11); 1706 1707 Assert.assertEquals(Long.rotateLeft(0x11, 1), 0x22); 1708 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE - 1), 0x8000000000000008L); 1709 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE), 0x11); 1710 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE + 1), 0x22); 1711 1712 Assert.assertEquals(Long.rotateLeft(0x11, -1), 0x8000000000000008L); 1713 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE - 1)), 0x22); 1714 Assert.assertEquals(Long.rotateLeft(0x11, -Long.SIZE), 0x11); 1715 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE + 1)), 0x8000000000000008L); 1716 1717 Assert.assertEquals(Long.rotateLeft(0xC000000000000000L, 1), 0x8000000000000001L); 1718 1719 for (int i = 0; i < Long.SIZE; i++) { 1720 Assert.assertEquals( 1721 Long.rotateLeft(0xBBAAAADDFF0000DDL, i), 1722 (0xBBAAAADDFF0000DDL << i) | (0xBBAAAADDFF0000DDL >>> (Long.SIZE - i))); 1723 } 1724 } 1725 1726 public static void test_Integer_rotateRightLeft() throws Exception { 1727 for (int i = 0; i < Integer.SIZE * 2; i++) { 1728 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, i), 1729 Integer.rotateRight(0xBBAAAADD, -i)); 1730 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, -i), 1731 Integer.rotateRight(0xBBAAAADD, i)); 1732 } 1733 } 1734 1735 public static void test_Long_rotateRightLeft() throws Exception { 1736 for (int i = 0; i < Long.SIZE * 2; i++) { 1737 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, i), 1738 Long.rotateRight(0xBBAAAADDFF0000DDL, -i)); 1739 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, -i), 1740 Long.rotateRight(0xBBAAAADDFF0000DDL, i)); 1741 } 1742 } 1743 } 1744