1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.lang; 19 20import static org.apache.harmony.tests.java.lang.MathTest.COPYSIGN_DD_CASES; 21import static org.apache.harmony.tests.java.lang.MathTest.COPYSIGN_FF_CASES; 22import static org.apache.harmony.tests.java.lang.MathTest.GETEXPONENT_D_CASES; 23import static org.apache.harmony.tests.java.lang.MathTest.GETEXPONENT_D_RESULTS; 24import static org.apache.harmony.tests.java.lang.MathTest.GETEXPONENT_F_CASES; 25import static org.apache.harmony.tests.java.lang.MathTest.GETEXPONENT_F_RESULTS; 26import static org.apache.harmony.tests.java.lang.MathTest.NEXTAFTER_DD_START_CASES; 27import static org.apache.harmony.tests.java.lang.MathTest.NEXTAFTER_DD_FD_DIRECTION_CASES; 28import static org.apache.harmony.tests.java.lang.MathTest.NEXTAFTER_FD_START_CASES; 29 30public class StrictMathTest extends junit.framework.TestCase { 31 32 private static final double HYP = StrictMath.sqrt(2.0); 33 34 private static final double OPP = 1.0; 35 36 private static final double ADJ = 1.0; 37 38 /* Required to make previous preprocessor flags work - do not remove */ 39 int unused = 0; 40 41 /** 42 * java.lang.StrictMath#abs(double) 43 */ 44 public void test_absD() { 45 // Test for method double java.lang.StrictMath.abs(double) 46 47 assertTrue("Incorrect double abs value", 48 (StrictMath.abs(-1908.8976) == 1908.8976)); 49 assertTrue("Incorrect double abs value", 50 (StrictMath.abs(1908.8976) == 1908.8976)); 51 } 52 53 /** 54 * java.lang.StrictMath#abs(float) 55 */ 56 public void test_absF() { 57 // Test for method float java.lang.StrictMath.abs(float) 58 assertTrue("Incorrect float abs value", 59 (StrictMath.abs(-1908.8976f) == 1908.8976f)); 60 assertTrue("Incorrect float abs value", 61 (StrictMath.abs(1908.8976f) == 1908.8976f)); 62 } 63 64 /** 65 * java.lang.StrictMath#abs(int) 66 */ 67 public void test_absI() { 68 // Test for method int java.lang.StrictMath.abs(int) 69 assertTrue("Incorrect int abs value", 70 (StrictMath.abs(-1908897) == 1908897)); 71 assertTrue("Incorrect int abs value", 72 (StrictMath.abs(1908897) == 1908897)); 73 } 74 75 /** 76 * java.lang.StrictMath#abs(long) 77 */ 78 public void test_absJ() { 79 // Test for method long java.lang.StrictMath.abs(long) 80 assertTrue("Incorrect long abs value", (StrictMath 81 .abs(-19088976000089L) == 19088976000089L)); 82 assertTrue("Incorrect long abs value", 83 (StrictMath.abs(19088976000089L) == 19088976000089L)); 84 } 85 86 /** 87 * java.lang.StrictMath#acos(double) 88 */ 89 public void test_acosD() { 90 // Test for method double java.lang.StrictMath.acos(double) 91 assertTrue("Returned incorrect arc cosine", StrictMath.cos(StrictMath 92 .acos(ADJ / HYP)) == ADJ / HYP); 93 } 94 95 /** 96 * java.lang.StrictMath#asin(double) 97 */ 98 public void test_asinD() { 99 // Test for method double java.lang.StrictMath.asin(double) 100 assertTrue("Returned incorrect arc sine", StrictMath.sin(StrictMath 101 .asin(OPP / HYP)) == OPP / HYP); 102 } 103 104 /** 105 * java.lang.StrictMath#atan(double) 106 */ 107 public void test_atanD() { 108 // Test for method double java.lang.StrictMath.atan(double) 109 double answer = StrictMath.tan(StrictMath.atan(1.0)); 110 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 111 && answer >= 9.9999999999999983E-1); 112 } 113 114 /** 115 * java.lang.StrictMath#atan2(double, double) 116 */ 117 public void test_atan2DD() { 118 // Test for method double java.lang.StrictMath.atan2(double, double) 119 double answer = StrictMath.atan(StrictMath.tan(1.0)); 120 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 121 && answer >= 9.9999999999999983E-1); 122 } 123 124 /** 125 * java.lang.StrictMath#cbrt(double) 126 */ 127 @SuppressWarnings("boxing") 128 public void test_cbrt_D() { 129 // Test for special situations 130 assertTrue("Should return Double.NaN", Double.isNaN(StrictMath 131 .cbrt(Double.NaN))); 132 assertEquals("Should return Double.POSITIVE_INFINITY", 133 Double.POSITIVE_INFINITY, StrictMath 134 .cbrt(Double.POSITIVE_INFINITY)); 135 assertEquals("Should return Double.NEGATIVE_INFINITY", 136 Double.NEGATIVE_INFINITY, StrictMath 137 .cbrt(Double.NEGATIVE_INFINITY)); 138 assertEquals(Double.doubleToLongBits(0.0), Double 139 .doubleToLongBits(StrictMath.cbrt(0.0))); 140 assertEquals(Double.doubleToLongBits(+0.0), Double 141 .doubleToLongBits(StrictMath.cbrt(+0.0))); 142 assertEquals(Double.doubleToLongBits(-0.0), Double 143 .doubleToLongBits(StrictMath.cbrt(-0.0))); 144 145 assertEquals("Should return 3.0", 3.0, StrictMath.cbrt(27.0)); 146 assertEquals("Should return 23.111993172558684", 23.111993172558684, 147 StrictMath.cbrt(12345.6)); 148 assertEquals("Should return 5.643803094122362E102", 149 5.643803094122362E102, StrictMath.cbrt(Double.MAX_VALUE)); 150 assertEquals("Should return 0.01", 0.01, StrictMath.cbrt(0.000001)); 151 152 assertEquals("Should return -3.0", -3.0, StrictMath.cbrt(-27.0)); 153 assertEquals("Should return -23.111993172558684", -23.111993172558684, 154 StrictMath.cbrt(-12345.6)); 155 assertEquals("Should return 1.7031839360032603E-108", 156 1.7031839360032603E-108, StrictMath.cbrt(Double.MIN_VALUE)); 157 assertEquals("Should return -0.01", -0.01, StrictMath.cbrt(-0.000001)); 158 159 try { 160 StrictMath.cbrt((Double) null); 161 fail("Should throw NullPointerException"); 162 } catch (NullPointerException e) { 163 //expected 164 } 165 } 166 167 /** 168 * java.lang.StrictMath#ceil(double) 169 */ 170 public void test_ceilD() { 171 // Test for method double java.lang.StrictMath.ceil(double) 172 assertEquals("Incorrect ceiling for double", 173 79, StrictMath.ceil(78.89), 0.0); 174 assertEquals("Incorrect ceiling for double", 175 -78, StrictMath.ceil(-78.89), 0.0); 176 } 177 178 /** 179 * {@link java.lang.StrictMath#copySign(double, double)} 180 * @since 1.6 181 */ 182 @SuppressWarnings("boxing") 183 public void test_copySign_DD() { 184 for (int i = 0; i < COPYSIGN_DD_CASES.length; i++) { 185 final double magnitude = COPYSIGN_DD_CASES[i]; 186 final long absMagnitudeBits = Double.doubleToLongBits(StrictMath 187 .abs(magnitude)); 188 final long negMagnitudeBits = Double.doubleToLongBits(-StrictMath 189 .abs(magnitude)); 190 191 // cases for NaN 192 assertEquals("If the sign is NaN, the result should be positive.", 193 absMagnitudeBits, Double.doubleToLongBits(StrictMath 194 .copySign(magnitude, Double.NaN))); 195 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 196 .copySign(Double.NaN, magnitude))); 197 198 for (int j = 0; j < COPYSIGN_DD_CASES.length; j++) { 199 final double sign = COPYSIGN_DD_CASES[j]; 200 final long resultBits = Double.doubleToLongBits(StrictMath 201 .copySign(magnitude, sign)); 202 203 if (sign > 0 || Double.valueOf(+0.0).equals(sign) 204 || Double.valueOf(0.0).equals(sign)) { 205 assertEquals( 206 "If the sign is positive, the result should be positive.", 207 absMagnitudeBits, resultBits); 208 } 209 if (sign < 0 || Double.valueOf(-0.0).equals(sign)) { 210 assertEquals( 211 "If the sign is negative, the result should be negative.", 212 negMagnitudeBits, resultBits); 213 } 214 } 215 } 216 217 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 218 .copySign(Double.NaN, Double.NaN))); 219 220 try { 221 StrictMath.copySign((Double) null, 2.3); 222 fail("Should throw NullPointerException"); 223 } catch (NullPointerException e) { 224 // Expected 225 } 226 try { 227 StrictMath.copySign(2.3, (Double) null); 228 fail("Should throw NullPointerException"); 229 } catch (NullPointerException e) { 230 // Expected 231 } 232 try { 233 StrictMath.copySign((Double) null, (Double) null); 234 fail("Should throw NullPointerException"); 235 } catch (NullPointerException e) { 236 // Expected 237 } 238 239 double d = Double.longBitsToDouble(0xfff8000000000000L); 240 assertEquals(1.0, StrictMath.copySign(1.0, d), 0d); 241 } 242 243 /** 244 * {@link java.lang.StrictMath#copySign(float, float)} 245 * @since 1.6 246 */ 247 @SuppressWarnings("boxing") 248 public void test_copySign_FF() { 249 for (int i = 0; i < COPYSIGN_FF_CASES.length; i++) { 250 final float magnitude = COPYSIGN_FF_CASES[i]; 251 final int absMagnitudeBits = Float.floatToIntBits(StrictMath 252 .abs(magnitude)); 253 final int negMagnitudeBits = Float.floatToIntBits(-StrictMath 254 .abs(magnitude)); 255 256 // cases for NaN 257 assertEquals("If the sign is NaN, the result should be positive.", 258 absMagnitudeBits, Float.floatToIntBits(StrictMath.copySign( 259 magnitude, Float.NaN))); 260 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 261 .copySign(Float.NaN, magnitude))); 262 263 for (int j = 0; j < COPYSIGN_FF_CASES.length; j++) { 264 final float sign = COPYSIGN_FF_CASES[j]; 265 final int resultBits = Float.floatToIntBits(StrictMath 266 .copySign(magnitude, sign)); 267 if (sign > 0 || Float.valueOf(+0.0f).equals(sign) 268 || Float.valueOf(0.0f).equals(sign)) { 269 assertEquals( 270 "If the sign is positive, the result should be positive.", 271 absMagnitudeBits, resultBits); 272 } 273 if (sign < 0 || Float.valueOf(-0.0f).equals(sign)) { 274 assertEquals( 275 "If the sign is negative, the result should be negative.", 276 negMagnitudeBits, resultBits); 277 } 278 } 279 } 280 281 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 282 .copySign(Float.NaN, Float.NaN))); 283 284 try { 285 StrictMath.copySign((Float) null, 2.3f); 286 fail("Should throw NullPointerException"); 287 } catch (NullPointerException e) { 288 // Expected 289 } 290 try { 291 StrictMath.copySign(2.3f, (Float) null); 292 fail("Should throw NullPointerException"); 293 } catch (NullPointerException e) { 294 // Expected 295 } 296 try { 297 StrictMath.copySign((Float) null, (Float) null); 298 fail("Should throw NullPointerException"); 299 } catch (NullPointerException e) { 300 // Expected 301 } 302 303 float f = Float.intBitsToFloat(0xffc00000); 304 assertEquals(1.0f, StrictMath.copySign(1.0f, f), 0f); 305 } 306 307 /** 308 * java.lang.StrictMath#cos(double) 309 */ 310 public void test_cosD() { 311 // Test for method double java.lang.StrictMath.cos(double) 312 313 assertTrue("Returned incorrect cosine", StrictMath.cos(StrictMath 314 .acos(ADJ / HYP)) == ADJ / HYP); 315 } 316 317 /** 318 * java.lang.StrictMath#cosh(double) 319 */ 320 @SuppressWarnings("boxing") 321 public void test_cosh_D() { 322 // Test for special situations 323 assertTrue("Should return NaN", Double.isNaN(StrictMath 324 .cosh(Double.NaN))); 325 assertEquals("Should return POSITIVE_INFINITY", 326 Double.POSITIVE_INFINITY, StrictMath 327 .cosh(Double.POSITIVE_INFINITY)); 328 assertEquals("Should return POSITIVE_INFINITY", 329 Double.POSITIVE_INFINITY, StrictMath 330 .cosh(Double.NEGATIVE_INFINITY)); 331 assertEquals("Should return 1.0", 1.0, StrictMath.cosh(+0.0)); 332 assertEquals("Should return 1.0", 1.0, StrictMath.cosh(-0.0)); 333 334 assertEquals("Should return POSITIVE_INFINITY", 335 Double.POSITIVE_INFINITY, StrictMath.cosh(1234.56)); 336 assertEquals("Should return POSITIVE_INFINITY", 337 Double.POSITIVE_INFINITY, StrictMath.cosh(-1234.56)); 338 assertEquals("Should return 1.0000000000005", 1.0000000000005, 339 StrictMath.cosh(0.000001)); 340 assertEquals("Should return 1.0000000000005", 1.0000000000005, 341 StrictMath.cosh(-0.000001)); 342 assertEquals("Should return 5.212214351945598", 5.212214351945598, 343 StrictMath.cosh(2.33482)); 344 345 assertEquals("Should return POSITIVE_INFINITY", 346 Double.POSITIVE_INFINITY, StrictMath.cosh(Double.MAX_VALUE)); 347 assertEquals("Should return 1.0", 1.0, StrictMath 348 .cosh(Double.MIN_VALUE)); 349 } 350 351 /** 352 * java.lang.StrictMath#exp(double) 353 */ 354 public void test_expD() { 355 // Test for method double java.lang.StrictMath.exp(double) 356 assertTrue("Incorrect answer returned for simple power", StrictMath 357 .abs(StrictMath.exp(4D) - StrictMath.E * StrictMath.E 358 * StrictMath.E * StrictMath.E) < 0.1D); 359 assertTrue("Incorrect answer returned for larger power", StrictMath 360 .log(StrictMath.abs(StrictMath.exp(5.5D)) - 5.5D) < 10.0D); 361 } 362 363 /** 364 * java.lang.StrictMath#expm1(double) 365 */ 366 @SuppressWarnings("boxing") 367 public void test_expm1_D() { 368 //Test for special cases 369 assertTrue("Should return NaN", Double.isNaN(StrictMath.expm1(Double.NaN))); 370 assertEquals("Should return POSITIVE_INFINITY", 371 Double.POSITIVE_INFINITY, StrictMath.expm1(Double.POSITIVE_INFINITY)); 372 assertEquals("Should return -1.0", -1.0, StrictMath 373 .expm1(Double.NEGATIVE_INFINITY)); 374 assertEquals(Double.doubleToLongBits(0.0), Double 375 .doubleToLongBits(StrictMath.expm1(0.0))); 376 assertEquals(Double.doubleToLongBits(+0.0), Double 377 .doubleToLongBits(StrictMath.expm1(+0.0))); 378 assertEquals(Double.doubleToLongBits(-0.0), Double 379 .doubleToLongBits(StrictMath.expm1(-0.0))); 380 381 assertEquals("Should return -9.999950000166666E-6", 382 -9.999950000166666E-6, StrictMath.expm1(-0.00001)); 383 assertEquals("Should return 1.0145103074469635E60", 384 1.0145103074469635E60, StrictMath.expm1(138.16951162)); 385 assertEquals("Should return POSITIVE_INFINITY", 386 Double.POSITIVE_INFINITY, StrictMath 387 .expm1(123456789123456789123456789.4521584223)); 388 assertEquals("Should return POSITIVE_INFINITY", 389 Double.POSITIVE_INFINITY, StrictMath.expm1(Double.MAX_VALUE)); 390 assertEquals("Should return MIN_VALUE", Double.MIN_VALUE, StrictMath 391 .expm1(Double.MIN_VALUE)); 392 393 } 394 395 /** 396 * java.lang.StrictMath#floor(double) 397 */ 398 public void test_floorD() { 399 // Test for method double java.lang.StrictMath.floor(double) 400 assertEquals("Incorrect floor for double", 401 78, StrictMath.floor(78.89), 0.0); 402 assertEquals("Incorrect floor for double", 403 -79, StrictMath.floor(-78.89), 0.0); 404 } 405 406 /** 407 * {@link java.lang.StrictMath#getExponent(double)} 408 * @since 1.6 409 */ 410 @SuppressWarnings("boxing") 411 public void test_getExponent_D() { 412 for (int i = 0; i < GETEXPONENT_D_CASES.length; i++) { 413 final double number = GETEXPONENT_D_CASES[i]; 414 final int result = GETEXPONENT_D_RESULTS[i]; 415 assertEquals("Wrong result of getExponent(double).", result, 416 StrictMath.getExponent(number)); 417 } 418 419 try { 420 StrictMath.getExponent((Double) null); 421 fail("Should throw NullPointerException"); 422 } catch (NullPointerException e) { 423 // Expected 424 } 425 } 426 427 /** 428 * {@link java.lang.StrictMath#getExponent(float)} 429 * @since 1.6 430 */ 431 @SuppressWarnings("boxing") 432 public void test_getExponent_F() { 433 for (int i = 0; i < GETEXPONENT_F_CASES.length; i++) { 434 final float number = GETEXPONENT_F_CASES[i]; 435 final int result = GETEXPONENT_F_RESULTS[i]; 436 assertEquals("Wrong result of getExponent(float).", result, 437 StrictMath.getExponent(number)); 438 } 439 try { 440 StrictMath.getExponent((Float) null); 441 fail("Should throw NullPointerException"); 442 } catch (NullPointerException e) { 443 // Expected 444 } 445 } 446 447 /** 448 * java.lang.StrictMath#hypot(double, double) 449 */ 450 @SuppressWarnings("boxing") 451 public void test_hypot_DD() { 452 // Test for special cases 453 assertEquals("Should return POSITIVE_INFINITY", 454 Double.POSITIVE_INFINITY, StrictMath.hypot(Double.POSITIVE_INFINITY, 455 1.0)); 456 assertEquals("Should return POSITIVE_INFINITY", 457 Double.POSITIVE_INFINITY, StrictMath.hypot(Double.NEGATIVE_INFINITY, 458 123.324)); 459 assertEquals("Should return POSITIVE_INFINITY", 460 Double.POSITIVE_INFINITY, StrictMath.hypot(-758.2587, 461 Double.POSITIVE_INFINITY)); 462 assertEquals("Should return POSITIVE_INFINITY", 463 Double.POSITIVE_INFINITY, StrictMath.hypot(5687.21, 464 Double.NEGATIVE_INFINITY)); 465 assertEquals("Should return POSITIVE_INFINITY", 466 Double.POSITIVE_INFINITY, StrictMath.hypot(Double.POSITIVE_INFINITY, 467 Double.NEGATIVE_INFINITY)); 468 assertEquals("Should return POSITIVE_INFINITY", 469 Double.POSITIVE_INFINITY, StrictMath.hypot(Double.NEGATIVE_INFINITY, 470 Double.POSITIVE_INFINITY)); 471 assertTrue("Should return NaN", Double.isNaN(StrictMath.hypot(Double.NaN, 472 2342301.89843))); 473 assertTrue("Should return NaN", Double.isNaN(StrictMath.hypot(-345.2680, 474 Double.NaN))); 475 476 assertEquals("Should return 2396424.905416697", 2396424.905416697, StrictMath 477 .hypot(12322.12, -2396393.2258)); 478 assertEquals("Should return 138.16958070558556", 138.16958070558556, 479 StrictMath.hypot(-138.16951162, 0.13817035864)); 480 assertEquals("Should return 1.7976931348623157E308", 481 1.7976931348623157E308, StrictMath.hypot(Double.MAX_VALUE, 211370.35)); 482 assertEquals("Should return 5413.7185", 5413.7185, StrictMath.hypot( 483 -5413.7185, Double.MIN_VALUE)); 484 485 } 486 487 /** 488 * java.lang.StrictMath#IEEEremainder(double, double) 489 */ 490 public void test_IEEEremainderDD() { 491 // Test for method double java.lang.StrictMath.IEEEremainder(double, 492 // double) 493 assertEquals("Incorrect remainder returned", 0.0, StrictMath.IEEEremainder( 494 1.0, 1.0), 0.0); 495 assertTrue( 496 "Incorrect remainder returned", 497 StrictMath.IEEEremainder(1.32, 89.765) >= 1.4705063220631647E-2 498 || StrictMath.IEEEremainder(1.32, 89.765) >= 1.4705063220631649E-2); 499 } 500 501 /** 502 * java.lang.StrictMath#log(double) 503 */ 504 public void test_logD() { 505 // Test for method double java.lang.StrictMath.log(double) 506 for (double d = 10; d >= -10; d -= 0.5) { 507 double answer = StrictMath.log(StrictMath.exp(d)); 508 assertTrue("Answer does not equal expected answer for d = " + d 509 + " answer = " + answer, 510 StrictMath.abs(answer - d) <= StrictMath 511 .abs(d * 0.00000001)); 512 } 513 } 514 515 /** 516 * java.lang.StrictMath#log10(double) 517 */ 518 @SuppressWarnings("boxing") 519 public void test_log10_D() { 520 // Test for special cases 521 assertTrue("Should return NaN", Double.isNaN(StrictMath 522 .log10(Double.NaN))); 523 assertTrue("Should return NaN", Double.isNaN(StrictMath 524 .log10(-2541.05745687234187532))); 525 assertEquals("Should return POSITIVE_INFINITY", 526 Double.POSITIVE_INFINITY, StrictMath 527 .log10(Double.POSITIVE_INFINITY)); 528 assertEquals("Should return NEGATIVE_INFINITY", 529 Double.NEGATIVE_INFINITY, StrictMath.log10(0.0)); 530 assertEquals("Should return NEGATIVE_INFINITY", 531 Double.NEGATIVE_INFINITY, StrictMath.log10(+0.0)); 532 assertEquals("Should return NEGATIVE_INFINITY", 533 Double.NEGATIVE_INFINITY, StrictMath.log10(-0.0)); 534 assertEquals("Should return 14.0", 14.0, StrictMath.log10(StrictMath 535 .pow(10, 14))); 536 537 assertEquals("Should return 3.7389561269540406", 3.7389561269540406, 538 StrictMath.log10(5482.2158)); 539 assertEquals("Should return 14.661551142893833", 14.661551142893833, 540 StrictMath.log10(458723662312872.125782332587)); 541 assertEquals("Should return -0.9083828622192334", -0.9083828622192334, 542 StrictMath.log10(0.12348583358871)); 543 assertEquals("Should return 308.25471555991675", 308.25471555991675, 544 StrictMath.log10(Double.MAX_VALUE)); 545 assertEquals("Should return -323.3062153431158", -323.3062153431158, 546 StrictMath.log10(Double.MIN_VALUE)); 547 } 548 549 /** 550 * java.lang.StrictMath#log1p(double) 551 */ 552 @SuppressWarnings("boxing") 553 public void test_log1p_D() { 554 // Test for special cases 555 assertTrue("Should return NaN", Double.isNaN(StrictMath 556 .log1p(Double.NaN))); 557 assertTrue("Should return NaN", Double.isNaN(StrictMath 558 .log1p(-32.0482175))); 559 assertEquals("Should return POSITIVE_INFINITY", 560 Double.POSITIVE_INFINITY, StrictMath 561 .log1p(Double.POSITIVE_INFINITY)); 562 assertEquals(Double.doubleToLongBits(0.0), Double 563 .doubleToLongBits(StrictMath.log1p(0.0))); 564 assertEquals(Double.doubleToLongBits(+0.0), Double 565 .doubleToLongBits(StrictMath.log1p(+0.0))); 566 assertEquals(Double.doubleToLongBits(-0.0), Double 567 .doubleToLongBits(StrictMath.log1p(-0.0))); 568 569 assertEquals("Should return -0.2941782295312541", -0.2941782295312541, 570 StrictMath.log1p(-0.254856327)); 571 assertEquals("Should return 7.368050685564151", 7.368050685564151, 572 StrictMath.log1p(1583.542)); 573 assertEquals("Should return 0.4633708685409921", 0.4633708685409921, 574 StrictMath.log1p(0.5894227)); 575 assertEquals("Should return 709.782712893384", 709.782712893384, 576 StrictMath.log1p(Double.MAX_VALUE)); 577 assertEquals("Should return Double.MIN_VALUE", Double.MIN_VALUE, 578 StrictMath.log1p(Double.MIN_VALUE)); 579 } 580 581 /** 582 * java.lang.StrictMath#max(double, double) 583 */ 584 public void test_maxDD() { 585 // Test for method double java.lang.StrictMath.max(double, double) 586 assertEquals("Incorrect double max value", 1908897.6000089, StrictMath.max( 587 -1908897.6000089, 1908897.6000089), 0D); 588 assertEquals("Incorrect double max value", 1908897.6000089, StrictMath.max(2.0, 589 1908897.6000089), 0D); 590 assertEquals("Incorrect double max value", -2.0, StrictMath.max(-2.0, 591 -1908897.6000089), 0D); 592 593 } 594 595 /** 596 * java.lang.StrictMath#max(float, float) 597 */ 598 public void test_maxFF() { 599 // Test for method float java.lang.StrictMath.max(float, float) 600 assertTrue("Incorrect float max value", StrictMath.max(-1908897.600f, 601 1908897.600f) == 1908897.600f); 602 assertTrue("Incorrect float max value", StrictMath.max(2.0f, 603 1908897.600f) == 1908897.600f); 604 assertTrue("Incorrect float max value", StrictMath.max(-2.0f, 605 -1908897.600f) == -2.0f); 606 } 607 608 /** 609 * java.lang.StrictMath#max(int, int) 610 */ 611 public void test_maxII() { 612 // Test for method int java.lang.StrictMath.max(int, int) 613 assertEquals("Incorrect int max value", 19088976, StrictMath.max(-19088976, 614 19088976)); 615 assertEquals("Incorrect int max value", 616 19088976, StrictMath.max(20, 19088976)); 617 assertEquals("Incorrect int max value", 618 -20, StrictMath.max(-20, -19088976)); 619 } 620 621 /** 622 * java.lang.StrictMath#max(long, long) 623 */ 624 public void test_maxJJ() { 625 // Test for method long java.lang.StrictMath.max(long, long) 626 assertEquals("Incorrect long max value", 19088976000089L, StrictMath.max(-19088976000089L, 627 19088976000089L)); 628 assertEquals("Incorrect long max value", 19088976000089L, StrictMath.max(20, 629 19088976000089L)); 630 assertEquals("Incorrect long max value", -20, StrictMath.max(-20, 631 -19088976000089L)); 632 } 633 634 /** 635 * java.lang.StrictMath#min(double, double) 636 */ 637 public void test_minDD() { 638 // Test for method double java.lang.StrictMath.min(double, double) 639 assertEquals("Incorrect double min value", -1908897.6000089, StrictMath.min( 640 -1908897.6000089, 1908897.6000089), 0D); 641 assertEquals("Incorrect double min value", 2.0, StrictMath.min(2.0, 642 1908897.6000089), 0D); 643 assertEquals("Incorrect double min value", -1908897.6000089, StrictMath.min(-2.0, 644 -1908897.6000089), 0D); 645 } 646 647 /** 648 * java.lang.StrictMath#min(float, float) 649 */ 650 public void test_minFF() { 651 // Test for method float java.lang.StrictMath.min(float, float) 652 assertTrue("Incorrect float min value", StrictMath.min(-1908897.600f, 653 1908897.600f) == -1908897.600f); 654 assertTrue("Incorrect float min value", StrictMath.min(2.0f, 655 1908897.600f) == 2.0f); 656 assertTrue("Incorrect float min value", StrictMath.min(-2.0f, 657 -1908897.600f) == -1908897.600f); 658 } 659 660 /** 661 * java.lang.StrictMath#min(int, int) 662 */ 663 public void test_minII() { 664 // Test for method int java.lang.StrictMath.min(int, int) 665 assertEquals("Incorrect int min value", -19088976, StrictMath.min(-19088976, 666 19088976)); 667 assertEquals("Incorrect int min value", 668 20, StrictMath.min(20, 19088976)); 669 assertEquals("Incorrect int min value", 670 -19088976, StrictMath.min(-20, -19088976)); 671 672 } 673 674 /** 675 * java.lang.StrictMath#min(long, long) 676 */ 677 public void test_minJJ() { 678 // Test for method long java.lang.StrictMath.min(long, long) 679 assertEquals("Incorrect long min value", -19088976000089L, StrictMath.min(-19088976000089L, 680 19088976000089L)); 681 assertEquals("Incorrect long min value", 20, StrictMath.min(20, 682 19088976000089L)); 683 assertEquals("Incorrect long min value", -19088976000089L, StrictMath.min(-20, 684 -19088976000089L)); 685 } 686 687 /** 688 * {@link java.lang.StrictMath#nextAfter(double, double)} 689 * @since 1.6 690 */ 691 @SuppressWarnings("boxing") 692 public void test_nextAfter_DD() { 693 // test for most cases without exception 694 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 695 final double start = NEXTAFTER_DD_START_CASES[i][0]; 696 final long nextUpBits = Double 697 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 698 final long nextDownBits = Double 699 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][2]); 700 701 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 702 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 703 final long resultBits = Double.doubleToLongBits(StrictMath 704 .nextAfter(start, direction)); 705 final long directionBits = Double.doubleToLongBits(direction); 706 if (direction > start) { 707 assertEquals("Result should be next up-number.", 708 nextUpBits, resultBits); 709 } else if (direction < start) { 710 assertEquals("Result should be next down-number.", 711 nextDownBits, resultBits); 712 } else { 713 assertEquals("Result should be direction.", directionBits, 714 resultBits); 715 } 716 } 717 } 718 719 // test for cases with NaN 720 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 721 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 722 .nextAfter(NEXTAFTER_DD_START_CASES[i][0], Double.NaN))); 723 } 724 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 725 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 726 .nextAfter(Double.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 727 } 728 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 729 .nextAfter(Double.NaN, Double.NaN))); 730 731 // test for exception 732 try { 733 StrictMath.nextAfter((Double) null, 2.3); 734 fail("Should throw NullPointerException"); 735 } catch (NullPointerException e) { 736 // Expected 737 } 738 try { 739 StrictMath.nextAfter(2.3, (Double) null); 740 fail("Should throw NullPointerException"); 741 } catch (NullPointerException e) { 742 // Expected 743 } 744 try { 745 StrictMath.nextAfter((Double) null, (Double) null); 746 fail("Should throw NullPointerException"); 747 } catch (NullPointerException e) { 748 // Expected 749 } 750 } 751 752 /** 753 * {@link java.lang.StrictMath#nextAfter(float, double)} 754 * @since 1.6 755 */ 756 @SuppressWarnings("boxing") 757 public void test_nextAfter_FD() { 758 // test for most cases without exception 759 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 760 final float start = NEXTAFTER_FD_START_CASES[i][0]; 761 final int nextUpBits = Float 762 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 763 final int nextDownBits = Float 764 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][2]); 765 766 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 767 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 768 final int resultBits = Float.floatToIntBits(StrictMath 769 .nextAfter(start, direction)); 770 if (direction > start) { 771 assertEquals("Result should be next up-number.", 772 nextUpBits, resultBits); 773 } else if (direction < start) { 774 assertEquals("Result should be next down-number.", 775 nextDownBits, resultBits); 776 } else { 777 final int equivalentBits = Float.floatToIntBits(new Float( 778 direction)); 779 assertEquals( 780 "Result should be a number equivalent to direction.", 781 equivalentBits, resultBits); 782 } 783 } 784 } 785 786 // test for cases with NaN 787 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 788 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 789 .nextAfter(NEXTAFTER_FD_START_CASES[i][0], Float.NaN))); 790 } 791 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 792 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 793 .nextAfter(Float.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 794 } 795 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 796 .nextAfter(Float.NaN, Float.NaN))); 797 798 // test for exception 799 try { 800 StrictMath.nextAfter((Float) null, 2.3); 801 fail("Should throw NullPointerException"); 802 } catch (NullPointerException e) { 803 // Expected 804 } 805 try { 806 StrictMath.nextAfter(2.3, (Float) null); 807 fail("Should throw NullPointerException"); 808 } catch (NullPointerException e) { 809 // Expected 810 } 811 try { 812 StrictMath.nextAfter((Float) null, (Float) null); 813 fail("Should throw NullPointerException"); 814 } catch (NullPointerException e) { 815 // Expected 816 } 817 } 818 819 /** 820 * {@link java.lang.StrictMath#nextUp(double)} 821 * @since 1.6 822 */ 823 @SuppressWarnings("boxing") 824 public void test_nextUp_D() { 825 // This method is semantically equivalent to nextAfter(d, 826 // Double.POSITIVE_INFINITY), 827 // so we use the data of test_nextAfter_DD 828 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 829 final double start = NEXTAFTER_DD_START_CASES[i][0]; 830 final long nextUpBits = Double 831 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 832 final long resultBits = Double.doubleToLongBits(StrictMath 833 .nextUp(start)); 834 assertEquals("Result should be next up-number.", nextUpBits, 835 resultBits); 836 } 837 838 // test for cases with NaN 839 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 840 .nextUp(Double.NaN))); 841 842 // test for exception 843 try { 844 StrictMath.nextUp((Double) null); 845 fail("Should throw NullPointerException"); 846 } catch (NullPointerException e) { 847 // Expected 848 } 849 } 850 851 /** 852 * {@link java.lang.StrictMath#nextUp(float)} 853 * @since 1.6 854 */ 855 @SuppressWarnings("boxing") 856 public void test_nextUp_F() { 857 // This method is semantically equivalent to nextAfter(f, 858 // Float.POSITIVE_INFINITY), 859 // so we use the data of test_nextAfter_FD 860 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 861 final float start = NEXTAFTER_FD_START_CASES[i][0]; 862 final int nextUpBits = Float 863 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 864 final int resultBits = Float.floatToIntBits(StrictMath 865 .nextUp(start)); 866 assertEquals("Result should be next up-number.", nextUpBits, 867 resultBits); 868 } 869 870 // test for cases with NaN 871 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 872 .nextUp(Float.NaN))); 873 874 // test for exception 875 try { 876 StrictMath.nextUp((Float) null); 877 fail("Should throw NullPointerException"); 878 } catch (NullPointerException e) { 879 // Expected 880 } 881 } 882 883 /** 884 * {@link java.lang.StrictMath#nextDown(double)} 885 * @since 1.8 886 */ 887 @SuppressWarnings("boxing") 888 public void test_nextDown_D() { 889 // This method is semantically equivalent to nextAfter(d, 890 // Double.NEGATIVE_INFINITY), 891 // so we use the data of test_nextAfter_DD 892 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 893 final double start = NEXTAFTER_DD_START_CASES[i][0]; 894 final long nextDownBits = Double 895 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][2]); 896 final long resultBits = Double.doubleToLongBits(StrictMath 897 .nextDown(start)); 898 assertEquals("Result should be next down-number.", nextDownBits, 899 resultBits); 900 } 901 902 // test for cases with NaN 903 assertTrue("The result should be NaN.", Double.isNaN(StrictMath 904 .nextDown(Double.NaN))); 905 906 // test for exception 907 try { 908 StrictMath.nextDown((Double) null); 909 fail("Should throw NullPointerException"); 910 } catch (NullPointerException e) { 911 // Expected 912 } 913 } 914 915 /** 916 * {@link java.lang.StrictMath#nextDown(float)} 917 * @since 1.8 918 */ 919 @SuppressWarnings("boxing") 920 public void test_nextDown_F() { 921 // This method is semantically equivalent to nextAfter(f, 922 // Float.NEGATIVE_INFINITY), 923 // so we use the data of test_nextAfter_FD 924 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 925 final float start = NEXTAFTER_FD_START_CASES[i][0]; 926 final int nextDownBits = Float 927 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][2]); 928 final int resultBits = Float.floatToIntBits(StrictMath 929 .nextDown(start)); 930 assertEquals("Result should be next down-number.", nextDownBits, 931 resultBits); 932 } 933 934 // test for cases with NaN 935 assertTrue("The result should be NaN.", Float.isNaN(StrictMath 936 .nextDown(Float.NaN))); 937 938 // test for exception 939 try { 940 StrictMath.nextDown((Float) null); 941 fail("Should throw NullPointerException"); 942 } catch (NullPointerException e) { 943 // Expected 944 } 945 } 946 947 /** 948 * java.lang.StrictMath#pow(double, double) 949 */ 950 public void test_powDD() { 951 // Test for method double java.lang.StrictMath.pow(double, double) 952 assertTrue("pow returned incorrect value", 953 (long) StrictMath.pow(2, 8) == 256l); 954 assertTrue("pow returned incorrect value", 955 StrictMath.pow(2, -8) == 0.00390625d); 956 } 957 958 /** 959 * java.lang.StrictMath#rint(double) 960 */ 961 public void test_rintD() { 962 // Test for method double java.lang.StrictMath.rint(double) 963 assertEquals("Failed to round properly - up to odd", 964 3.0, StrictMath.rint(2.9), 0D); 965 assertTrue("Failed to round properly - NaN", Double.isNaN(StrictMath 966 .rint(Double.NaN))); 967 assertEquals("Failed to round properly down to even", 2.0, StrictMath 968 .rint(2.1), 0D); 969 assertTrue("Failed to round properly " + 2.5 + " to even", StrictMath 970 .rint(2.5) == 2.0); 971 } 972 973 /** 974 * java.lang.StrictMath#round(double) 975 */ 976 public void test_roundD() { 977 // Test for method long java.lang.StrictMath.round(double) 978 assertEquals("Incorrect rounding of a float", 979 -91, StrictMath.round(-90.89d)); 980 } 981 982 /** 983 * java.lang.StrictMath#round(float) 984 */ 985 public void test_roundF() { 986 // Test for method int java.lang.StrictMath.round(float) 987 assertEquals("Incorrect rounding of a float", 988 -91, StrictMath.round(-90.89f)); 989 } 990 991 /** 992 * {@link java.lang.StrictMath#scalb(double, int)} 993 * @since 1.6 994 */ 995 @SuppressWarnings("boxing") 996 public void test_scalb_DI() { 997 // result is normal 998 assertEquals(4.1422946304E7, StrictMath.scalb(1.2345, 25)); 999 assertEquals(3.679096698760986E-8, StrictMath.scalb(1.2345, -25)); 1000 assertEquals(1.2345, StrictMath.scalb(1.2345, 0)); 1001 assertEquals(7868514.304, StrictMath.scalb(0.2345, 25)); 1002 1003 double normal = StrictMath.scalb(0.2345, -25); 1004 assertEquals(6.98864459991455E-9, normal); 1005 // precision kept 1006 assertEquals(0.2345, StrictMath.scalb(normal, 25)); 1007 1008 assertEquals(0.2345, StrictMath.scalb(0.2345, 0)); 1009 assertEquals(-4.1422946304E7, StrictMath.scalb(-1.2345, 25)); 1010 assertEquals(-6.98864459991455E-9, StrictMath.scalb(-0.2345, -25)); 1011 assertEquals(2.0, StrictMath.scalb(Double.MIN_NORMAL / 2, 1024)); 1012 assertEquals(64.0, StrictMath.scalb(Double.MIN_VALUE, 1080)); 1013 assertEquals(234, StrictMath.getExponent(StrictMath.scalb(1.0, 234))); 1014 assertEquals(3.9999999999999996, StrictMath.scalb(Double.MAX_VALUE, 1015 Double.MIN_EXPONENT)); 1016 1017 // result is near infinity 1018 double halfMax = StrictMath.scalb(1.0, Double.MAX_EXPONENT); 1019 assertEquals(8.98846567431158E307, halfMax); 1020 assertEquals(Double.MAX_VALUE, halfMax - StrictMath.ulp(halfMax) 1021 + halfMax); 1022 assertEquals(Double.POSITIVE_INFINITY, halfMax + halfMax); 1023 assertEquals(1.7976931348623155E308, StrictMath.scalb(1.0 - StrictMath 1024 .ulp(1.0), Double.MAX_EXPONENT + 1)); 1025 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1026 1.0 - StrictMath.ulp(1.0), Double.MAX_EXPONENT + 2)); 1027 1028 halfMax = StrictMath.scalb(-1.0, Double.MAX_EXPONENT); 1029 assertEquals(-8.98846567431158E307, halfMax); 1030 assertEquals(-Double.MAX_VALUE, halfMax + StrictMath.ulp(halfMax) 1031 + halfMax); 1032 assertEquals(Double.NEGATIVE_INFINITY, halfMax + halfMax); 1033 1034 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb(0.345, 1234)); 1035 assertEquals(Double.POSITIVE_INFINITY, StrictMath 1036 .scalb(44.345E102, 934)); 1037 assertEquals(Double.NEGATIVE_INFINITY, StrictMath.scalb(-44.345E102, 1038 934)); 1039 1040 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1041 Double.MIN_NORMAL / 2, 4000)); 1042 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1043 Double.MIN_VALUE, 8000)); 1044 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1045 Double.MAX_VALUE, 1)); 1046 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1047 Double.POSITIVE_INFINITY, 0)); 1048 assertEquals(Double.POSITIVE_INFINITY, StrictMath.scalb( 1049 Double.POSITIVE_INFINITY, -1)); 1050 assertEquals(Double.NEGATIVE_INFINITY, StrictMath.scalb( 1051 Double.NEGATIVE_INFINITY, -1)); 1052 assertEquals(Double.NEGATIVE_INFINITY, StrictMath.scalb( 1053 Double.NEGATIVE_INFINITY, Double.MIN_EXPONENT)); 1054 1055 // result is subnormal/zero 1056 long posZeroBits = Double.doubleToLongBits(+0.0); 1057 long negZeroBits = Double.doubleToLongBits(-0.0); 1058 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1059 +0.0, Integer.MAX_VALUE))); 1060 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1061 +0.0, -123))); 1062 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1063 +0.0, 0))); 1064 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1065 -0.0, 123))); 1066 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1067 -0.0, Integer.MIN_VALUE))); 1068 1069 assertEquals(Double.MIN_VALUE, StrictMath.scalb(1.0, -1074)); 1070 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb(1.0, 1071 -1075))); 1072 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1073 -1.0, -1075))); 1074 1075 // precision lost 1076 assertEquals(StrictMath.scalb(21.405, -1078), StrictMath.scalb(21.405, 1077 -1079)); 1078 assertEquals(Double.MIN_VALUE, StrictMath.scalb(21.405, -1079)); 1079 assertEquals(-Double.MIN_VALUE, StrictMath.scalb(-21.405, -1079)); 1080 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1081 21.405, -1080))); 1082 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1083 -21.405, -1080))); 1084 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1085 Double.MIN_VALUE, -1))); 1086 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1087 -Double.MIN_VALUE, -1))); 1088 assertEquals(Double.MIN_VALUE, StrictMath.scalb(Double.MIN_NORMAL, -52)); 1089 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1090 Double.MIN_NORMAL, -53))); 1091 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1092 -Double.MIN_NORMAL, -53))); 1093 assertEquals(Double.MIN_VALUE, StrictMath 1094 .scalb(Double.MAX_VALUE, -2098)); 1095 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1096 Double.MAX_VALUE, -2099))); 1097 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1098 -Double.MAX_VALUE, -2099))); 1099 assertEquals(Double.MIN_VALUE, StrictMath.scalb(Double.MIN_NORMAL / 3, 1100 -51)); 1101 assertEquals(posZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1102 Double.MIN_NORMAL / 3, -52))); 1103 assertEquals(negZeroBits, Double.doubleToLongBits(StrictMath.scalb( 1104 -Double.MIN_NORMAL / 3, -52))); 1105 double subnormal = StrictMath.scalb(Double.MIN_NORMAL / 3, -25); 1106 assertEquals(2.2104123E-316, subnormal); 1107 // precision lost 1108 assertFalse(Double.MIN_NORMAL / 3 == StrictMath.scalb(subnormal, 25)); 1109 1110 // NaN 1111 assertTrue(Double.isNaN(StrictMath.scalb(Double.NaN, 1))); 1112 assertTrue(Double.isNaN(StrictMath.scalb(Double.NaN, 0))); 1113 assertTrue(Double.isNaN(StrictMath.scalb(Double.NaN, -120))); 1114 1115 assertEquals(1283457024, Double.doubleToLongBits(StrictMath.scalb( 1116 Double.MIN_VALUE * 153, 23))); 1117 assertEquals(-9223372035571318784L, Double.doubleToLongBits(StrictMath 1118 .scalb(-Double.MIN_VALUE * 153, 23))); 1119 assertEquals(36908406321184768L, Double.doubleToLongBits(StrictMath 1120 .scalb(Double.MIN_VALUE * 153, 52))); 1121 assertEquals(-9186463630533591040L, Double.doubleToLongBits(StrictMath 1122 .scalb(-Double.MIN_VALUE * 153, 52))); 1123 1124 // test for exception 1125 try { 1126 StrictMath.scalb((Double) null, (Integer) null); 1127 fail("Should throw NullPointerException"); 1128 } catch (NullPointerException e) { 1129 // Expected 1130 } 1131 try { 1132 StrictMath.scalb(1.0, (Integer) null); 1133 fail("Should throw NullPointerException"); 1134 } catch (NullPointerException e) { 1135 // Expected 1136 } 1137 try { 1138 StrictMath.scalb((Double) null, 1); 1139 fail("Should throw NullPointerException"); 1140 } catch (NullPointerException e) { 1141 // Expected 1142 } 1143 } 1144 1145 /** 1146 * {@link java.lang.StrictMath#scalb(float, int)} 1147 * @since 1.6 1148 */ 1149 @SuppressWarnings("boxing") 1150 public void test_scalb_FI() { 1151 // result is normal 1152 assertEquals(4.1422946304E7f, StrictMath.scalb(1.2345f, 25)); 1153 assertEquals(3.679096698760986E-8f, StrictMath.scalb(1.2345f, -25)); 1154 assertEquals(1.2345f, StrictMath.scalb(1.2345f, 0)); 1155 assertEquals(7868514.304f, StrictMath.scalb(0.2345f, 25)); 1156 1157 float normal = StrictMath.scalb(0.2345f, -25); 1158 assertEquals(6.98864459991455E-9f, normal); 1159 // precision kept 1160 assertEquals(0.2345f, StrictMath.scalb(normal, 25)); 1161 1162 assertEquals(0.2345f, StrictMath.scalb(0.2345f, 0)); 1163 assertEquals(-4.1422946304E7f, StrictMath.scalb(-1.2345f, 25)); 1164 assertEquals(-6.98864459991455E-9f, StrictMath.scalb(-0.2345f, -25)); 1165 assertEquals(2.0f, StrictMath.scalb(Float.MIN_NORMAL / 2, 128)); 1166 assertEquals(64.0f, StrictMath.scalb(Float.MIN_VALUE, 155)); 1167 assertEquals(34, StrictMath.getExponent(StrictMath.scalb(1.0f, 34))); 1168 assertEquals(3.9999998f, StrictMath.scalb(Float.MAX_VALUE, 1169 Float.MIN_EXPONENT)); 1170 1171 // result is near infinity 1172 float halfMax = StrictMath.scalb(1.0f, Float.MAX_EXPONENT); 1173 assertEquals(1.7014118E38f, halfMax); 1174 assertEquals(Float.MAX_VALUE, halfMax - StrictMath.ulp(halfMax) 1175 + halfMax); 1176 assertEquals(Float.POSITIVE_INFINITY, halfMax + halfMax); 1177 assertEquals(3.4028233E38f, StrictMath.scalb(1.0f - StrictMath 1178 .ulp(1.0f), Float.MAX_EXPONENT + 1)); 1179 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb( 1180 1.0f - StrictMath.ulp(1.0f), Float.MAX_EXPONENT + 2)); 1181 1182 halfMax = StrictMath.scalb(-1.0f, Float.MAX_EXPONENT); 1183 assertEquals(-1.7014118E38f, halfMax); 1184 assertEquals(-Float.MAX_VALUE, halfMax + StrictMath.ulp(halfMax) 1185 + halfMax); 1186 assertEquals(Float.NEGATIVE_INFINITY, halfMax + halfMax); 1187 1188 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb(0.345f, 1234)); 1189 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb(44.345E10f, 934)); 1190 assertEquals(Float.NEGATIVE_INFINITY, StrictMath 1191 .scalb(-44.345E10f, 934)); 1192 1193 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb( 1194 Float.MIN_NORMAL / 2, 400)); 1195 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb(Float.MIN_VALUE, 1196 800)); 1197 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb(Float.MAX_VALUE, 1198 1)); 1199 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb( 1200 Float.POSITIVE_INFINITY, 0)); 1201 assertEquals(Float.POSITIVE_INFINITY, StrictMath.scalb( 1202 Float.POSITIVE_INFINITY, -1)); 1203 assertEquals(Float.NEGATIVE_INFINITY, StrictMath.scalb( 1204 Float.NEGATIVE_INFINITY, -1)); 1205 assertEquals(Float.NEGATIVE_INFINITY, StrictMath.scalb( 1206 Float.NEGATIVE_INFINITY, Float.MIN_EXPONENT)); 1207 1208 // result is subnormal/zero 1209 int posZeroBits = Float.floatToIntBits(+0.0f); 1210 int negZeroBits = Float.floatToIntBits(-0.0f); 1211 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb(+0.0f, 1212 Integer.MAX_VALUE))); 1213 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb(+0.0f, 1214 -123))); 1215 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb(+0.0f, 1216 0))); 1217 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb(-0.0f, 1218 123))); 1219 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb(-0.0f, 1220 Integer.MIN_VALUE))); 1221 1222 assertEquals(Float.MIN_VALUE, StrictMath.scalb(1.0f, -149)); 1223 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb(1.0f, 1224 -150))); 1225 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb(-1.0f, 1226 -150))); 1227 1228 // precision lost 1229 assertEquals(StrictMath.scalb(21.405f, -154), StrictMath.scalb(21.405f, 1230 -153)); 1231 assertEquals(Float.MIN_VALUE, StrictMath.scalb(21.405f, -154)); 1232 assertEquals(-Float.MIN_VALUE, StrictMath.scalb(-21.405f, -154)); 1233 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb( 1234 21.405f, -155))); 1235 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb( 1236 -21.405f, -155))); 1237 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb( 1238 Float.MIN_VALUE, -1))); 1239 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb( 1240 -Float.MIN_VALUE, -1))); 1241 assertEquals(Float.MIN_VALUE, StrictMath.scalb(Float.MIN_NORMAL, -23)); 1242 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb( 1243 Float.MIN_NORMAL, -24))); 1244 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb( 1245 -Float.MIN_NORMAL, -24))); 1246 assertEquals(Float.MIN_VALUE, StrictMath.scalb(Float.MAX_VALUE, -277)); 1247 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb( 1248 Float.MAX_VALUE, -278))); 1249 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb( 1250 -Float.MAX_VALUE, -278))); 1251 assertEquals(Float.MIN_VALUE, StrictMath.scalb(Float.MIN_NORMAL / 3, 1252 -22)); 1253 assertEquals(posZeroBits, Float.floatToIntBits(StrictMath.scalb( 1254 Float.MIN_NORMAL / 3, -23))); 1255 assertEquals(negZeroBits, Float.floatToIntBits(StrictMath.scalb( 1256 -Float.MIN_NORMAL / 3, -23))); 1257 float subnormal = StrictMath.scalb(Float.MIN_NORMAL / 3, -11); 1258 assertEquals(1.913E-42f, subnormal); 1259 // precision lost 1260 assertFalse(Float.MIN_NORMAL / 3 == StrictMath.scalb(subnormal, 11)); 1261 1262 assertEquals(68747264, Float.floatToIntBits(StrictMath.scalb( 1263 Float.MIN_VALUE * 153, 23))); 1264 assertEquals(-2078736384, Float.floatToIntBits(StrictMath.scalb( 1265 -Float.MIN_VALUE * 153, 23))); 1266 1267 assertEquals(4896, Float.floatToIntBits(StrictMath.scalb( 1268 Float.MIN_VALUE * 153, 5))); 1269 assertEquals(-2147478752, Float.floatToIntBits(StrictMath.scalb( 1270 -Float.MIN_VALUE * 153, 5))); 1271 1272 // NaN 1273 assertTrue(Float.isNaN(StrictMath.scalb(Float.NaN, 1))); 1274 assertTrue(Float.isNaN(StrictMath.scalb(Float.NaN, 0))); 1275 assertTrue(Float.isNaN(StrictMath.scalb(Float.NaN, -120))); 1276 1277 // test for exception 1278 try { 1279 StrictMath.scalb((Float) null, (Integer) null); 1280 fail("Should throw NullPointerException"); 1281 } catch (NullPointerException e) { 1282 // Expected 1283 } 1284 try { 1285 StrictMath.scalb(1.0f, (Integer) null); 1286 fail("Should throw NullPointerException"); 1287 } catch (NullPointerException e) { 1288 // Expected 1289 } 1290 try { 1291 StrictMath.scalb((Float) null, 1); 1292 fail("Should throw NullPointerException"); 1293 } catch (NullPointerException e) { 1294 // Expected 1295 } 1296 } 1297 1298 /** 1299 * java.lang.StrictMath#signum(double) 1300 */ 1301 public void test_signum_D() { 1302 assertTrue(Double.isNaN(StrictMath.signum(Double.NaN))); 1303 assertEquals(Double.doubleToLongBits(0.0), Double 1304 .doubleToLongBits(StrictMath.signum(0.0))); 1305 assertEquals(Double.doubleToLongBits(+0.0), Double 1306 .doubleToLongBits(StrictMath.signum(+0.0))); 1307 assertEquals(Double.doubleToLongBits(-0.0), Double 1308 .doubleToLongBits(StrictMath.signum(-0.0))); 1309 1310 assertEquals(1.0, StrictMath.signum(253681.2187962), 0D); 1311 assertEquals(-1.0, StrictMath.signum(-125874693.56), 0D); 1312 assertEquals(1.0, StrictMath.signum(1.2587E-308), 0D); 1313 assertEquals(-1.0, StrictMath.signum(-1.2587E-308), 0D); 1314 1315 assertEquals(1.0, StrictMath.signum(Double.MAX_VALUE), 0D); 1316 assertEquals(1.0, StrictMath.signum(Double.MIN_VALUE), 0D); 1317 assertEquals(-1.0, StrictMath.signum(-Double.MAX_VALUE), 0D); 1318 assertEquals(-1.0, StrictMath.signum(-Double.MIN_VALUE), 0D); 1319 assertEquals(1.0, StrictMath.signum(Double.POSITIVE_INFINITY), 0D); 1320 assertEquals(-1.0, StrictMath.signum(Double.NEGATIVE_INFINITY), 0D); 1321 1322 } 1323 1324 /** 1325 * java.lang.StrictMath#signum(float) 1326 */ 1327 public void test_signum_F() { 1328 assertTrue(Float.isNaN(StrictMath.signum(Float.NaN))); 1329 assertEquals(Float.floatToIntBits(0.0f), Float 1330 .floatToIntBits(StrictMath.signum(0.0f))); 1331 assertEquals(Float.floatToIntBits(+0.0f), Float 1332 .floatToIntBits(StrictMath.signum(+0.0f))); 1333 assertEquals(Float.floatToIntBits(-0.0f), Float 1334 .floatToIntBits(StrictMath.signum(-0.0f))); 1335 1336 assertEquals(1.0f, StrictMath.signum(253681.2187962f), 0f); 1337 assertEquals(-1.0f, StrictMath.signum(-125874693.56f), 0f); 1338 assertEquals(1.0f, StrictMath.signum(1.2587E-11f), 0f); 1339 assertEquals(-1.0f, StrictMath.signum(-1.2587E-11f), 0f); 1340 1341 assertEquals(1.0f, StrictMath.signum(Float.MAX_VALUE), 0f); 1342 assertEquals(1.0f, StrictMath.signum(Float.MIN_VALUE), 0f); 1343 assertEquals(-1.0f, StrictMath.signum(-Float.MAX_VALUE), 0f); 1344 assertEquals(-1.0f, StrictMath.signum(-Float.MIN_VALUE), 0f); 1345 assertEquals(1.0f, StrictMath.signum(Float.POSITIVE_INFINITY), 0f); 1346 assertEquals(-1.0f, StrictMath.signum(Float.NEGATIVE_INFINITY), 0f); 1347 } 1348 1349 /** 1350 * java.lang.StrictMath#sin(double) 1351 */ 1352 public void test_sinD() { 1353 // Test for method double java.lang.StrictMath.sin(double) 1354 assertTrue("Returned incorrect sine", StrictMath.sin(StrictMath 1355 .asin(OPP / HYP)) == OPP / HYP); 1356 } 1357 1358 /** 1359 * java.lang.StrictMath#sinh(double) 1360 */ 1361 public void test_sinh_D() { 1362 // Test for special situations 1363 assertTrue(Double.isNaN(StrictMath.sinh(Double.NaN))); 1364 assertEquals("Should return POSITIVE_INFINITY", 1365 Double.POSITIVE_INFINITY, StrictMath 1366 .sinh(Double.POSITIVE_INFINITY), 0D); 1367 assertEquals("Should return NEGATIVE_INFINITY", 1368 Double.NEGATIVE_INFINITY, StrictMath 1369 .sinh(Double.NEGATIVE_INFINITY), 0D); 1370 assertEquals(Double.doubleToLongBits(0.0), Double 1371 .doubleToLongBits(StrictMath.sinh(0.0))); 1372 assertEquals(Double.doubleToLongBits(+0.0), Double 1373 .doubleToLongBits(StrictMath.sinh(+0.0))); 1374 assertEquals(Double.doubleToLongBits(-0.0), Double 1375 .doubleToLongBits(StrictMath.sinh(-0.0))); 1376 1377 assertEquals("Should return POSITIVE_INFINITY", 1378 Double.POSITIVE_INFINITY, StrictMath.sinh(1234.56), 0D); 1379 assertEquals("Should return NEGATIVE_INFINITY", 1380 Double.NEGATIVE_INFINITY, StrictMath.sinh(-1234.56), 0D); 1381 assertEquals("Should return 1.0000000000001666E-6", 1382 1.0000000000001666E-6, StrictMath.sinh(0.000001), 0D); 1383 assertEquals("Should return -1.0000000000001666E-6", 1384 -1.0000000000001666E-6, StrictMath.sinh(-0.000001), 0D); 1385 assertEquals("Should return 5.115386441963859", 5.115386441963859, 1386 StrictMath.sinh(2.33482), 0D); 1387 assertEquals("Should return POSITIVE_INFINITY", 1388 Double.POSITIVE_INFINITY, StrictMath.sinh(Double.MAX_VALUE), 0D); 1389 assertEquals("Should return 4.9E-324", 4.9E-324, StrictMath 1390 .sinh(Double.MIN_VALUE), 0D); 1391 } 1392 1393 /** 1394 * java.lang.StrictMath#sqrt(double) 1395 */ 1396 public void test_sqrtD() { 1397 // Test for method double java.lang.StrictMath.sqrt(double) 1398 assertEquals("Incorrect root returned1", 1399 2, StrictMath.sqrt(StrictMath.pow(StrictMath.sqrt(2), 4)), 0.0); 1400 assertEquals("Incorrect root returned2", 7, StrictMath.sqrt(49), 0.0); 1401 } 1402 1403 /** 1404 * java.lang.StrictMath#tan(double) 1405 */ 1406 public void test_tanD() { 1407 // Test for method double java.lang.StrictMath.tan(double) 1408 assertTrue( 1409 "Returned incorrect tangent: ", 1410 StrictMath.tan(StrictMath.atan(1.0)) <= 1.0 1411 || StrictMath.tan(StrictMath.atan(1.0)) >= 9.9999999999999983E-1); 1412 } 1413 1414 /** 1415 * java.lang.StrictMath#tanh(double) 1416 */ 1417 public void test_tanh_D() { 1418 // Test for special situations 1419 assertTrue(Double.isNaN(StrictMath.tanh(Double.NaN))); 1420 assertEquals("Should return +1.0", +1.0, StrictMath 1421 .tanh(Double.POSITIVE_INFINITY), 0D); 1422 assertEquals("Should return -1.0", -1.0, StrictMath 1423 .tanh(Double.NEGATIVE_INFINITY), 0D); 1424 assertEquals(Double.doubleToLongBits(0.0), Double 1425 .doubleToLongBits(StrictMath.tanh(0.0))); 1426 assertEquals(Double.doubleToLongBits(+0.0), Double 1427 .doubleToLongBits(StrictMath.tanh(+0.0))); 1428 assertEquals(Double.doubleToLongBits(-0.0), Double 1429 .doubleToLongBits(StrictMath.tanh(-0.0))); 1430 1431 assertEquals("Should return 1.0", 1.0, StrictMath.tanh(1234.56), 0D); 1432 assertEquals("Should return -1.0", -1.0, StrictMath.tanh(-1234.56), 0D); 1433 assertEquals("Should return 9.999999999996666E-7", 1434 9.999999999996666E-7, StrictMath.tanh(0.000001), 0D); 1435 assertEquals("Should return 0.981422884124941", 0.981422884124941, 1436 StrictMath.tanh(2.33482), 0D); 1437 assertEquals("Should return 1.0", 1.0, StrictMath 1438 .tanh(Double.MAX_VALUE), 0D); 1439 assertEquals("Should return 4.9E-324", 4.9E-324, StrictMath 1440 .tanh(Double.MIN_VALUE), 0D); 1441 } 1442 1443 /** 1444 * java.lang.StrictMath#random() 1445 */ 1446 public void test_random() { 1447 // There isn't a place for these tests so just stick them here 1448 assertEquals("Wrong value E", 1449 4613303445314885481L, Double.doubleToLongBits(StrictMath.E)); 1450 assertEquals("Wrong value PI", 1451 4614256656552045848L, Double.doubleToLongBits(StrictMath.PI)); 1452 1453 for (int i = 500; i >= 0; i--) { 1454 double d = StrictMath.random(); 1455 assertTrue("Generated number is out of range: " + d, d >= 0.0 1456 && d < 1.0); 1457 } 1458 } 1459 1460 /** 1461 * java.lang.StrictMath#toRadians(double) 1462 */ 1463 public void test_toRadiansD() { 1464 for (double d = 500; d >= 0; d -= 1.0) { 1465 double converted = StrictMath.toDegrees(StrictMath.toRadians(d)); 1466 assertTrue("Converted number not equal to original. d = " + d, 1467 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1468 } 1469 } 1470 1471 /** 1472 * java.lang.StrictMath#toDegrees(double) 1473 */ 1474 public void test_toDegreesD() { 1475 for (double d = 500; d >= 0; d -= 1.0) { 1476 double converted = StrictMath.toRadians(StrictMath.toDegrees(d)); 1477 assertTrue("Converted number not equal to original. d = " + d, 1478 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1479 } 1480 } 1481 1482 /** 1483 * java.lang.StrictMath#ulp(double) 1484 */ 1485 @SuppressWarnings("boxing") 1486 public void test_ulp_D() { 1487 // Test for special cases 1488 assertTrue("Should return NaN", Double 1489 .isNaN(StrictMath.ulp(Double.NaN))); 1490 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, 1491 StrictMath.ulp(Double.POSITIVE_INFINITY), 0D); 1492 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, 1493 StrictMath.ulp(Double.NEGATIVE_INFINITY), 0D); 1494 assertEquals("Returned incorrect value", Double.MIN_VALUE, StrictMath 1495 .ulp(0.0), 0D); 1496 assertEquals("Returned incorrect value", Double.MIN_VALUE, StrictMath 1497 .ulp(+0.0), 0D); 1498 assertEquals("Returned incorrect value", Double.MIN_VALUE, StrictMath 1499 .ulp(-0.0), 0D); 1500 assertEquals("Returned incorrect value", StrictMath.pow(2, 971), 1501 StrictMath.ulp(Double.MAX_VALUE), 0D); 1502 assertEquals("Returned incorrect value", StrictMath.pow(2, 971), 1503 StrictMath.ulp(-Double.MAX_VALUE), 0D); 1504 1505 assertEquals("Returned incorrect value", Double.MIN_VALUE, StrictMath 1506 .ulp(Double.MIN_VALUE), 0D); 1507 assertEquals("Returned incorrect value", Double.MIN_VALUE, StrictMath 1508 .ulp(-Double.MIN_VALUE), 0D); 1509 1510 assertEquals("Returned incorrect value", 2.220446049250313E-16, 1511 StrictMath.ulp(1.0), 0D); 1512 assertEquals("Returned incorrect value", 2.220446049250313E-16, 1513 StrictMath.ulp(-1.0), 0D); 1514 assertEquals("Returned incorrect value", 2.2737367544323206E-13, 1515 StrictMath.ulp(1153.0), 0D); 1516 } 1517 1518 /** 1519 * java.lang.StrictMath#ulp(float) 1520 */ 1521 @SuppressWarnings("boxing") 1522 public void test_ulp_f() { 1523 // Test for special cases 1524 assertTrue("Should return NaN", Float.isNaN(StrictMath.ulp(Float.NaN))); 1525 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, 1526 StrictMath.ulp(Float.POSITIVE_INFINITY), 0f); 1527 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, 1528 StrictMath.ulp(Float.NEGATIVE_INFINITY), 0f); 1529 assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath 1530 .ulp(0.0f), 0f); 1531 assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath 1532 .ulp(+0.0f), 0f); 1533 assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath 1534 .ulp(-0.0f), 0f); 1535 assertEquals("Returned incorrect value", 2.028241E31f, StrictMath 1536 .ulp(Float.MAX_VALUE), 0f); 1537 assertEquals("Returned incorrect value", 2.028241E31f, StrictMath 1538 .ulp(-Float.MAX_VALUE), 0f); 1539 1540 assertEquals("Returned incorrect value", 1.4E-45f, StrictMath 1541 .ulp(Float.MIN_VALUE), 0f); 1542 assertEquals("Returned incorrect value", 1.4E-45f, StrictMath 1543 .ulp(-Float.MIN_VALUE), 0f); 1544 1545 assertEquals("Returned incorrect value", 1.1920929E-7f, StrictMath 1546 .ulp(1.0f), 0f); 1547 assertEquals("Returned incorrect value", 1.1920929E-7f, StrictMath 1548 .ulp(-1.0f), 0f); 1549 assertEquals("Returned incorrect value", 1.2207031E-4f, StrictMath 1550 .ulp(1153.0f), 0f); 1551 assertEquals("Returned incorrect value", 5.6E-45f, Math 1552 .ulp(9.403954E-38f), 0f); 1553 } 1554} 1555