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 android.core; 19 20import junit.framework.Assert; 21import junit.framework.TestCase; 22import android.test.suitebuilder.annotation.SmallTest; 23import android.test.suitebuilder.annotation.MediumTest; 24 25public class MathTest extends TestCase { 26 27 private final double HYP = Math.sqrt(2.0); 28 29 private final double OPP = 1.0; 30 31 private final double ADJ = 1.0; 32 33 /* Required to make previous preprocessor flags work - do not remove */ 34 int unused = 0; 35 36 public static void assertEquals(String message, double expected, double actual, double delta) { 37 if (delta == 0D) { 38 Assert.assertEquals(message, expected, actual, Math.ulp(expected)); 39 } else { 40 Assert.assertEquals(message, expected, actual, delta); 41 } 42 } 43 44 public static void assertEquals(String message, float expected, float actual, float delta) { 45 if (delta == 0F) { 46 Assert.assertEquals(message, expected, actual, Math.ulp(expected)); 47 } else { 48 Assert.assertEquals(message, expected, actual, delta); 49 } 50 } 51 52 /** 53 * @tests java.lang.Math#abs(double) 54 */ 55 @SmallTest 56 public void testAbsD() { 57 // Test for method double java.lang.Math.abs(double) 58 59 assertTrue("Incorrect double abs value", 60 (Math.abs(-1908.8976) == 1908.8976)); 61 assertTrue("Incorrect double abs value", 62 (Math.abs(1908.8976) == 1908.8976)); 63 } 64 65 /** 66 * @tests java.lang.Math#abs(float) 67 */ 68 @SmallTest 69 public void testAbsF() { 70 // Test for method float java.lang.Math.abs(float) 71 assertTrue("Incorrect float abs value", 72 (Math.abs(-1908.8976f) == 1908.8976f)); 73 assertTrue("Incorrect float abs value", 74 (Math.abs(1908.8976f) == 1908.8976f)); 75 } 76 77 /** 78 * @tests java.lang.Math#abs(int) 79 */ 80 @SmallTest 81 public void testAbsI() { 82 // Test for method int java.lang.Math.abs(int) 83 assertTrue("Incorrect int abs value", (Math.abs(-1908897) == 1908897)); 84 assertTrue("Incorrect int abs value", (Math.abs(1908897) == 1908897)); 85 } 86 87 /** 88 * @tests java.lang.Math#abs(long) 89 */ 90 @SmallTest 91 public void testAbsJ() { 92 // Test for method long java.lang.Math.abs(long) 93 assertTrue("Incorrect long abs value", 94 (Math.abs(-19088976000089L) == 19088976000089L)); 95 assertTrue("Incorrect long abs value", 96 (Math.abs(19088976000089L) == 19088976000089L)); 97 } 98 99 /** 100 * @tests java.lang.Math#acos(double) 101 */ 102 @SmallTest 103 public void testAcosD() { 104 // Test for method double java.lang.Math.acos(double) 105 double r = Math.cos(Math.acos(ADJ / HYP)); 106 long lr = Double.doubleToLongBits(r); 107 long t = Double.doubleToLongBits(ADJ / HYP); 108 assertTrue("Returned incorrect arc cosine", lr == t || (lr + 1) == t 109 || (lr - 1) == t); 110 } 111 112 /** 113 * @tests java.lang.Math#asin(double) 114 */ 115 @SmallTest 116 public void testAsinD() { 117 // Test for method double java.lang.Math.asin(double) 118 double r = Math.sin(Math.asin(OPP / HYP)); 119 long lr = Double.doubleToLongBits(r); 120 long t = Double.doubleToLongBits(OPP / HYP); 121 assertTrue("Returned incorrect arc sine", lr == t || (lr + 1) == t 122 || (lr - 1) == t); 123 } 124 125 /** 126 * @tests java.lang.Math#atan(double) 127 */ 128 @SmallTest 129 public void testAtanD() { 130 // Test for method double java.lang.Math.atan(double) 131 double answer = Math.tan(Math.atan(1.0)); 132 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 133 && answer >= 9.9999999999999983E-1); 134 } 135 136 /** 137 * @tests java.lang.Math#atan2(double, double) 138 */ 139 @SmallTest 140 public void testAtan2DD() { 141 // Test for method double java.lang.Math.atan2(double, double) 142 double answer = Math.atan(Math.tan(1.0)); 143 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 144 && answer >= 9.9999999999999983E-1); 145 } 146 147 /** 148 * @tests java.lang.Math#cbrt(double) 149 */ 150 @SmallTest 151 public void testCbrtD() { 152 //Test for special situations 153 assertTrue("Should return Double.NaN", Double.isNaN(Math 154 .cbrt(Double.NaN))); 155 assertEquals("Should return Double.POSITIVE_INFINITY", 156 Double.POSITIVE_INFINITY, Math 157 .cbrt(Double.POSITIVE_INFINITY), 0D); 158 assertEquals("Should return Double.NEGATIVE_INFINITY", 159 Double.NEGATIVE_INFINITY, Math 160 .cbrt(Double.NEGATIVE_INFINITY), 0D); 161 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 162 .cbrt(0.0))); 163 assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math 164 .cbrt(+0.0))); 165 assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math 166 .cbrt(-0.0))); 167 168 assertEquals("Should return 3.0", 3.0, Math.cbrt(27.0), 0D); 169 assertEquals("Should return 23.111993172558684", 23.111993172558684, 170 Math.cbrt(12345.6), 0D); 171 assertEquals("Should return 5.643803094122362E102", 172 5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); 173 assertEquals("Should return 0.01", 0.01, Math.cbrt(0.000001), 0D); 174 175 assertEquals("Should return -3.0", -3.0, Math.cbrt(-27.0), 0D); 176 assertEquals("Should return -23.111993172558684", -23.111993172558684, 177 Math.cbrt(-12345.6), 0D); 178 assertEquals("Should return 1.7031839360032603E-108", 179 1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); 180 assertEquals("Should return -0.01", -0.01, Math.cbrt(-0.000001), 0D); 181 } 182 183 /** 184 * @tests java.lang.Math#ceil(double) 185 */ 186 @SmallTest 187 public void testCeilD() { 188 // Test for method double java.lang.Math.ceil(double) 189 assertEquals("Incorrect ceiling for double", 190 79, Math.ceil(78.89), 0); 191 assertEquals("Incorrect ceiling for double", 192 -78, Math.ceil(-78.89), 0); 193 } 194 195 /** 196 * @tests java.lang.Math#cos(double) 197 */ 198 @SmallTest 199 public void testCosD() { 200 // Test for method double java.lang.Math.cos(double) 201 assertEquals("Incorrect answer", 1.0, Math.cos(0), 0D); 202 assertEquals("Incorrect answer", 0.5403023058681398, Math.cos(1), 0D); 203 } 204 205 /** 206 * @tests java.lang.Math#cosh(double) 207 */ 208 @SmallTest 209 public void testCoshD() { 210 // Test for special situations 211 assertTrue(Double.isNaN(Math.cosh(Double.NaN))); 212 assertEquals("Should return POSITIVE_INFINITY", 213 Double.POSITIVE_INFINITY, Math.cosh(Double.POSITIVE_INFINITY), 0D); 214 assertEquals("Should return POSITIVE_INFINITY", 215 Double.POSITIVE_INFINITY, Math.cosh(Double.NEGATIVE_INFINITY), 0D); 216 assertEquals("Should return 1.0", 1.0, Math.cosh(+0.0), 0D); 217 assertEquals("Should return 1.0", 1.0, Math.cosh(-0.0), 0D); 218 219 assertEquals("Should return POSITIVE_INFINITY", 220 Double.POSITIVE_INFINITY, Math.cosh(1234.56), 0D); 221 assertEquals("Should return POSITIVE_INFINITY", 222 Double.POSITIVE_INFINITY, Math.cosh(-1234.56), 0D); 223 assertEquals("Should return 1.0000000000005", 1.0000000000005, Math 224 .cosh(0.000001), 0D); 225 assertEquals("Should return 1.0000000000005", 1.0000000000005, Math 226 .cosh(-0.000001), 0D); 227 assertEquals("Should return 5.212214351945598", 5.212214351945598, Math 228 .cosh(2.33482), 0D); 229 230 assertEquals("Should return POSITIVE_INFINITY", 231 Double.POSITIVE_INFINITY, Math.cosh(Double.MAX_VALUE), 0D); 232 assertEquals("Should return 1.0", 1.0, Math.cosh(Double.MIN_VALUE), 0D); 233 } 234 235 /** 236 * @tests java.lang.Math#exp(double) 237 */ 238 @SmallTest 239 public void testExpD() { 240 // Test for method double java.lang.Math.exp(double) 241 assertTrue("Incorrect answer returned for simple power", Math.abs(Math 242 .exp(4D) 243 - Math.E * Math.E * Math.E * Math.E) < 0.1D); 244 assertTrue("Incorrect answer returned for larger power", Math.log(Math 245 .abs(Math.exp(5.5D)) - 5.5D) < 10.0D); 246 } 247 248 /** 249 * @tests java.lang.Math#expm1(double) 250 */ 251 @SmallTest 252 public void testExpm1D() { 253 // Test for special cases 254 assertTrue("Should return NaN", Double.isNaN(Math.expm1(Double.NaN))); 255 assertEquals("Should return POSITIVE_INFINITY", 256 Double.POSITIVE_INFINITY, Math.expm1(Double.POSITIVE_INFINITY), 0D); 257 assertEquals("Should return -1.0", -1.0, Math 258 .expm1(Double.NEGATIVE_INFINITY), 0D); 259 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 260 .expm1(0.0))); 261 assertEquals(Double.doubleToLongBits(+0.0), Double 262 .doubleToLongBits(Math.expm1(+0.0))); 263 assertEquals(Double.doubleToLongBits(-0.0), Double 264 .doubleToLongBits(Math.expm1(-0.0))); 265 266 assertEquals("Should return -9.999950000166666E-6", 267 -9.999950000166666E-6, Math.expm1(-0.00001), 0D); 268 assertEquals("Should return 1.0145103074469635E60", 269 1.0145103074469635E60, Math.expm1(138.16951162), 0D); 270 assertEquals("Should return POSITIVE_INFINITY", 271 Double.POSITIVE_INFINITY, Math 272 .expm1(123456789123456789123456789.4521584223), 0D); 273 assertEquals("Should return POSITIVE_INFINITY", 274 Double.POSITIVE_INFINITY, Math.expm1(Double.MAX_VALUE), 0D); 275 assertEquals("Should return MIN_VALUE", Double.MIN_VALUE, Math 276 .expm1(Double.MIN_VALUE), 0D); 277 } 278 279 /** 280 * @tests java.lang.Math#floor(double) 281 */ 282 @SmallTest 283 public void testFloorD() { 284 // Test for method double java.lang.Math.floor(double) 285 assertEquals("Incorrect floor for double", 286 78, Math.floor(78.89), 0); 287 assertEquals("Incorrect floor for double", 288 -79, Math.floor(-78.89), 0); 289 } 290 291 /** 292 * @tests java.lang.Math#hypot(double, double) 293 */ 294 @SmallTest 295 public void testHypotDD() { 296 // Test for special cases 297 assertEquals("Should return POSITIVE_INFINITY", 298 Double.POSITIVE_INFINITY, Math.hypot(Double.POSITIVE_INFINITY, 299 1.0), 0D); 300 assertEquals("Should return POSITIVE_INFINITY", 301 Double.POSITIVE_INFINITY, Math.hypot(Double.NEGATIVE_INFINITY, 302 123.324), 0D); 303 assertEquals("Should return POSITIVE_INFINITY", 304 Double.POSITIVE_INFINITY, Math.hypot(-758.2587, 305 Double.POSITIVE_INFINITY), 0D); 306 assertEquals("Should return POSITIVE_INFINITY", 307 Double.POSITIVE_INFINITY, Math.hypot(5687.21, 308 Double.NEGATIVE_INFINITY), 0D); 309 assertEquals("Should return POSITIVE_INFINITY", 310 Double.POSITIVE_INFINITY, Math.hypot(Double.POSITIVE_INFINITY, 311 Double.NEGATIVE_INFINITY), 0D); 312 assertEquals("Should return POSITIVE_INFINITY", 313 Double.POSITIVE_INFINITY, Math.hypot(Double.NEGATIVE_INFINITY, 314 Double.POSITIVE_INFINITY), 0D); 315 assertTrue("Should be NaN", Double.isNaN(Math.hypot(Double.NaN, 316 2342301.89843))); 317 assertTrue("Should be NaN", Double.isNaN(Math.hypot(-345.2680, 318 Double.NaN))); 319 320 assertEquals("Should return 2396424.905416697", 2396424.905416697, Math 321 .hypot(12322.12, -2396393.2258), 0D); 322 assertEquals("Should return 138.16958070558556", 138.16958070558556, 323 Math.hypot(-138.16951162, 0.13817035864), 0D); 324 assertEquals("Should return 1.7976931348623157E308", 325 1.7976931348623157E308, Math.hypot(Double.MAX_VALUE, 211370.35), 0D); 326 assertEquals("Should return 5413.7185", 5413.7185, Math.hypot( 327 -5413.7185, Double.MIN_VALUE), 0D); 328 } 329 330 /** 331 * @tests java.lang.Math#IEEEremainder(double, double) 332 */ 333 @SmallTest 334 public void testIEEEremainderDD() { 335 // Test for method double java.lang.Math.IEEEremainder(double, double) 336 assertEquals("Incorrect remainder returned", 337 0.0, Math.IEEEremainder(1.0, 1.0), 0D); 338 assertTrue("Incorrect remainder returned", Math.IEEEremainder(1.32, 339 89.765) >= 1.4705063220631647E-2 340 || Math.IEEEremainder(1.32, 89.765) >= 1.4705063220631649E-2); 341 } 342 343 /** 344 * @tests java.lang.Math#log(double) 345 */ 346 @SmallTest 347 public void testLogD() { 348 // Test for method double java.lang.Math.log(double) 349 for (double d = 10; d >= -10; d -= 0.5) { 350 double answer = Math.log(Math.exp(d)); 351 assertTrue("Answer does not equal expected answer for d = " + d 352 + " answer = " + answer, Math.abs(answer - d) <= Math 353 .abs(d * 0.00000001)); 354 } 355 } 356 357 /** 358 * @tests java.lang.Math#log10(double) 359 */ 360 @SuppressWarnings("boxing") 361 @SmallTest 362 public void testLog10D() { 363 // Test for special cases 364 assertTrue(Double.isNaN(Math.log10(Double.NaN))); 365 assertTrue(Double.isNaN(Math.log10(-2541.05745687234187532))); 366 assertTrue(Double.isNaN(Math.log10(-0.1))); 367 assertEquals(Double.POSITIVE_INFINITY, Math.log10(Double.POSITIVE_INFINITY)); 368 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(0.0)); 369 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(+0.0)); 370 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(-0.0)); 371 372 assertEquals(3.0, Math.log10(1000.0)); 373 assertEquals(14.0, Math.log10(Math.pow(10, 14))); 374 assertEquals(3.7389561269540406, Math.log10(5482.2158)); 375 assertEquals(14.661551142893833, Math.log10(458723662312872.125782332587)); 376 assertEquals(-0.9083828622192334, Math.log10(0.12348583358871)); 377 assertEquals(308.25471555991675, Math.log10(Double.MAX_VALUE)); 378 assertEquals(-323.3062153431158, Math.log10(Double.MIN_VALUE)); 379 } 380 381 /** 382 * @tests java.lang.Math#log1p(double) 383 */ 384 @SmallTest 385 public void testLog1pD() { 386 // Test for special cases 387 assertTrue("Should return NaN", Double.isNaN(Math.log1p(Double.NaN))); 388 assertTrue("Should return NaN", Double.isNaN(Math.log1p(-32.0482175))); 389 assertEquals("Should return POSITIVE_INFINITY", 390 Double.POSITIVE_INFINITY, Math.log1p(Double.POSITIVE_INFINITY), 0D); 391 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 392 .log1p(0.0))); 393 assertEquals(Double.doubleToLongBits(+0.0), Double 394 .doubleToLongBits(Math.log1p(+0.0))); 395 assertEquals(Double.doubleToLongBits(-0.0), Double 396 .doubleToLongBits(Math.log1p(-0.0))); 397 398 assertEquals("Should return -0.2941782295312541", -0.2941782295312541, 399 Math.log1p(-0.254856327), 0D); 400 assertEquals("Should return 7.368050685564151", 7.368050685564151, Math 401 .log1p(1583.542), 0D); 402 assertEquals("Should return 0.4633708685409921", 0.4633708685409921, 403 Math.log1p(0.5894227), 0D); 404 assertEquals("Should return 709.782712893384", 709.782712893384, Math 405 .log1p(Double.MAX_VALUE), 0D); 406 assertEquals("Should return Double.MIN_VALUE", Double.MIN_VALUE, Math 407 .log1p(Double.MIN_VALUE), 0D); 408 } 409 410 /** 411 * @tests java.lang.Math#max(double, double) 412 */ 413 @SmallTest 414 public void testMaxDD() { 415 // Test for method double java.lang.Math.max(double, double) 416 assertEquals("Incorrect double max value", 1908897.6000089, Math.max(-1908897.6000089, 417 1908897.6000089), 0D); 418 assertEquals("Incorrect double max value", 419 1908897.6000089, Math.max(2.0, 1908897.6000089), 0D); 420 assertEquals("Incorrect double max value", -2.0, Math.max(-2.0, 421 -1908897.6000089), 0D); 422 423 } 424 425 /** 426 * @tests java.lang.Math#max(float, float) 427 */ 428 @SmallTest 429 public void testMaxFF() { 430 // Test for method float java.lang.Math.max(float, float) 431 assertTrue("Incorrect float max value", Math.max(-1908897.600f, 432 1908897.600f) == 1908897.600f); 433 assertTrue("Incorrect float max value", 434 Math.max(2.0f, 1908897.600f) == 1908897.600f); 435 assertTrue("Incorrect float max value", 436 Math.max(-2.0f, -1908897.600f) == -2.0f); 437 } 438 439 /** 440 * @tests java.lang.Math#max(int, int) 441 */ 442 @SmallTest 443 public void testMaxII() { 444 // Test for method int java.lang.Math.max(int, int) 445 assertEquals("Incorrect int max value", 446 19088976, Math.max(-19088976, 19088976)); 447 assertEquals("Incorrect int max value", 448 19088976, Math.max(20, 19088976)); 449 assertEquals("Incorrect int max value", -20, Math.max(-20, -19088976)); 450 } 451 452 /** 453 * @tests java.lang.Math#max(long, long) 454 */ 455 @SmallTest 456 public void testMaxJJ() { 457 // Test for method long java.lang.Math.max(long, long) 458 assertEquals("Incorrect long max value", 19088976000089L, Math.max(-19088976000089L, 459 19088976000089L)); 460 assertEquals("Incorrect long max value", 461 19088976000089L, Math.max(20, 19088976000089L)); 462 assertEquals("Incorrect long max value", 463 -20, Math.max(-20, -19088976000089L)); 464 } 465 466 /** 467 * @tests java.lang.Math#min(double, double) 468 */ 469 @SmallTest 470 public void testMinDD() { 471 // Test for method double java.lang.Math.min(double, double) 472 assertEquals("Incorrect double min value", -1908897.6000089, Math.min(-1908897.6000089, 473 1908897.6000089), 0D); 474 assertEquals("Incorrect double min value", 475 2.0, Math.min(2.0, 1908897.6000089), 0D); 476 assertEquals("Incorrect double min value", -1908897.6000089, Math.min(-2.0, 477 -1908897.6000089), 0D); 478 } 479 480 /** 481 * @tests java.lang.Math#min(float, float) 482 */ 483 @SmallTest 484 public void testMinFF() { 485 // Test for method float java.lang.Math.min(float, float) 486 assertTrue("Incorrect float min value", Math.min(-1908897.600f, 487 1908897.600f) == -1908897.600f); 488 assertTrue("Incorrect float min value", 489 Math.min(2.0f, 1908897.600f) == 2.0f); 490 assertTrue("Incorrect float min value", 491 Math.min(-2.0f, -1908897.600f) == -1908897.600f); 492 } 493 494 /** 495 * @tests java.lang.Math#min(int, int) 496 */ 497 @SmallTest 498 public void testMinII() { 499 // Test for method int java.lang.Math.min(int, int) 500 assertEquals("Incorrect int min value", 501 -19088976, Math.min(-19088976, 19088976)); 502 assertEquals("Incorrect int min value", 20, Math.min(20, 19088976)); 503 assertEquals("Incorrect int min value", 504 -19088976, Math.min(-20, -19088976)); 505 506 } 507 508 /** 509 * @tests java.lang.Math#min(long, long) 510 */ 511 @SmallTest 512 public void testMinJJ() { 513 // Test for method long java.lang.Math.min(long, long) 514 assertEquals("Incorrect long min value", -19088976000089L, Math.min(-19088976000089L, 515 19088976000089L)); 516 assertEquals("Incorrect long min value", 517 20, Math.min(20, 19088976000089L)); 518 assertEquals("Incorrect long min value", 519 -19088976000089L, Math.min(-20, -19088976000089L)); 520 } 521 522 /** 523 * @tests java.lang.Math#pow(double, double) 524 */ 525 @SmallTest 526 public void testPowDD() { 527 // Test for method double java.lang.Math.pow(double, double) 528 assertTrue("pow returned incorrect value", 529 (long) Math.pow(2, 8) == 256l); 530 assertTrue("pow returned incorrect value", 531 Math.pow(2, -8) == 0.00390625d); 532 assertEquals("Incorrect root returned1", 533 2, Math.sqrt(Math.pow(Math.sqrt(2), 4)), 0); 534 } 535 536 /** 537 * @tests java.lang.Math#rint(double) 538 */ 539 @SmallTest 540 public void testRintD() { 541 // Test for method double java.lang.Math.rint(double) 542 assertEquals("Failed to round properly - up to odd", 543 3.0, Math.rint(2.9), 0D); 544 assertTrue("Failed to round properly - NaN", Double.isNaN(Math 545 .rint(Double.NaN))); 546 assertEquals("Failed to round properly down to even", 547 2.0, Math.rint(2.1), 0D); 548 assertTrue("Failed to round properly " + 2.5 + " to even", Math 549 .rint(2.5) == 2.0); 550 } 551 552 /** 553 * @tests java.lang.Math#round(double) 554 */ 555 @SmallTest 556 public void testRoundD() { 557 // Test for method long java.lang.Math.round(double) 558 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89d)); 559 } 560 561 /** 562 * @tests java.lang.Math#round(float) 563 */ 564 @SmallTest 565 public void testRoundF() { 566 // Test for method int java.lang.Math.round(float) 567 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89f)); 568 } 569 570 /** 571 * @tests java.lang.Math#signum(double) 572 */ 573 @SmallTest 574 public void testSignumD() { 575 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 576 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 577 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 578 .signum(0.0))); 579 assertEquals(Double.doubleToLongBits(+0.0), Double 580 .doubleToLongBits(Math.signum(+0.0))); 581 assertEquals(Double.doubleToLongBits(-0.0), Double 582 .doubleToLongBits(Math.signum(-0.0))); 583 584 assertEquals(1.0, Math.signum(253681.2187962), 0D); 585 assertEquals(-1.0, Math.signum(-125874693.56), 0D); 586 assertEquals(1.0, Math.signum(1.2587E-308), 0D); 587 assertEquals(-1.0, Math.signum(-1.2587E-308), 0D); 588 589 assertEquals(1.0, Math.signum(Double.MAX_VALUE), 0D); 590 assertEquals(1.0, Math.signum(Double.MIN_VALUE), 0D); 591 assertEquals(-1.0, Math.signum(-Double.MAX_VALUE), 0D); 592 assertEquals(-1.0, Math.signum(-Double.MIN_VALUE), 0D); 593 assertEquals(1.0, Math.signum(Double.POSITIVE_INFINITY), 0D); 594 assertEquals(-1.0, Math.signum(Double.NEGATIVE_INFINITY), 0D); 595 } 596 597 /** 598 * @tests java.lang.Math#signum(float) 599 */ 600 @SmallTest 601 public void testSignumF() { 602 assertTrue(Float.isNaN(Math.signum(Float.NaN))); 603 assertEquals(Float.floatToIntBits(0.0f), Float 604 .floatToIntBits(Math.signum(0.0f))); 605 assertEquals(Float.floatToIntBits(+0.0f), Float 606 .floatToIntBits(Math.signum(+0.0f))); 607 assertEquals(Float.floatToIntBits(-0.0f), Float 608 .floatToIntBits(Math.signum(-0.0f))); 609 610 assertEquals(1.0f, Math.signum(253681.2187962f), 0f); 611 assertEquals(-1.0f, Math.signum(-125874693.56f), 0f); 612 assertEquals(1.0f, Math.signum(1.2587E-11f), 0f); 613 assertEquals(-1.0f, Math.signum(-1.2587E-11f), 0f); 614 615 assertEquals(1.0f, Math.signum(Float.MAX_VALUE), 0f); 616 assertEquals(1.0f, Math.signum(Float.MIN_VALUE), 0f); 617 assertEquals(-1.0f, Math.signum(-Float.MAX_VALUE), 0f); 618 assertEquals(-1.0f, Math.signum(-Float.MIN_VALUE), 0f); 619 assertEquals(1.0f, Math.signum(Float.POSITIVE_INFINITY), 0f); 620 assertEquals(-1.0f, Math.signum(Float.NEGATIVE_INFINITY), 0f); 621 } 622 623 /** 624 * @tests java.lang.Math#sin(double) 625 */ 626 @SmallTest 627 public void testSinD() { 628 // Test for method double java.lang.Math.sin(double) 629 assertEquals("Incorrect answer", 0.0, Math.sin(0), 0D); 630 assertEquals("Incorrect answer", 0.8414709848078965, Math.sin(1), 0D); 631 } 632 633 /** 634 * @tests java.lang.Math#sinh(double) 635 */ 636 @SmallTest 637 public void testSinhD() { 638 // Test for special situations 639 assertTrue("Should return NaN", Double.isNaN(Math.sinh(Double.NaN))); 640 assertEquals("Should return POSITIVE_INFINITY", 641 Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); 642 assertEquals("Should return NEGATIVE_INFINITY", 643 Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); 644 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 645 .sinh(0.0))); 646 assertEquals(Double.doubleToLongBits(+0.0), Double 647 .doubleToLongBits(Math.sinh(+0.0))); 648 assertEquals(Double.doubleToLongBits(-0.0), Double 649 .doubleToLongBits(Math.sinh(-0.0))); 650 651 assertEquals("Should return POSITIVE_INFINITY", 652 Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); 653 assertEquals("Should return NEGATIVE_INFINITY", 654 Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); 655 assertEquals("Should return 1.0000000000001666E-6", 656 1.0000000000001666E-6, Math.sinh(0.000001), 0D); 657 assertEquals("Should return -1.0000000000001666E-6", 658 -1.0000000000001666E-6, Math.sinh(-0.000001), 0D); 659 assertEquals("Should return 5.115386441963859", 5.115386441963859, Math 660 .sinh(2.33482), 0D); 661 assertEquals("Should return POSITIVE_INFINITY", 662 Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); 663 assertEquals("Should return 4.9E-324", 4.9E-324, Math 664 .sinh(Double.MIN_VALUE), 0D); 665 } 666 667 /** 668 * @tests java.lang.Math#sqrt(double) 669 */ 670 @SmallTest 671 public void testSqrtD() { 672 // Test for method double java.lang.Math.sqrt(double) 673 assertEquals("Incorrect root returned2", 7, Math.sqrt(49), 0); 674 } 675 676 /** 677 * @tests java.lang.Math#tan(double) 678 */ 679 @SmallTest 680 public void testTanD() { 681 // Test for method double java.lang.Math.tan(double) 682 assertEquals("Incorrect answer", 0.0, Math.tan(0), 0D); 683 assertEquals("Incorrect answer", 1.5574077246549023, Math.tan(1), 0D); 684 685 } 686 687 /** 688 * @tests java.lang.Math#tanh(double) 689 */ 690 @SmallTest 691 public void testTanhD() { 692 // Test for special situations 693 assertTrue("Should return NaN", Double.isNaN(Math.tanh(Double.NaN))); 694 assertEquals("Should return +1.0", +1.0, Math 695 .tanh(Double.POSITIVE_INFINITY), 0D); 696 assertEquals("Should return -1.0", -1.0, Math 697 .tanh(Double.NEGATIVE_INFINITY), 0D); 698 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 699 .tanh(0.0))); 700 assertEquals(Double.doubleToLongBits(+0.0), Double 701 .doubleToLongBits(Math.tanh(+0.0))); 702 assertEquals(Double.doubleToLongBits(-0.0), Double 703 .doubleToLongBits(Math.tanh(-0.0))); 704 705 assertEquals("Should return 1.0", 1.0, Math.tanh(1234.56), 0D); 706 assertEquals("Should return -1.0", -1.0, Math.tanh(-1234.56), 0D); 707 assertEquals("Should return 9.999999999996666E-7", 708 9.999999999996666E-7, Math.tanh(0.000001), 0D); 709 assertEquals("Should return 0.981422884124941", 0.981422884124941, Math 710 .tanh(2.33482), 0D); 711 assertEquals("Should return 1.0", 1.0, Math.tanh(Double.MAX_VALUE), 0D); 712 assertEquals("Should return 4.9E-324", 4.9E-324, Math 713 .tanh(Double.MIN_VALUE), 0D); 714 } 715 716 /** 717 * @tests java.lang.Math#random() 718 */ 719 @MediumTest 720 public void testRandom() { 721 // There isn't a place for these tests so just stick them here 722 assertEquals("Wrong value E", 723 4613303445314885481L, Double.doubleToLongBits(Math.E)); 724 assertEquals("Wrong value PI", 725 4614256656552045848L, Double.doubleToLongBits(Math.PI)); 726 727 for (int i = 500; i >= 0; i--) { 728 double d = Math.random(); 729 assertTrue("Generated number is out of range: " + d, d >= 0.0 730 && d < 1.0); 731 } 732 } 733 734 /** 735 * @tests java.lang.Math#toRadians(double) 736 */ 737 @MediumTest 738 public void testToRadiansD() { 739 for (double d = 500; d >= 0; d -= 1.0) { 740 double converted = Math.toDegrees(Math.toRadians(d)); 741 assertTrue("Converted number not equal to original. d = " + d, 742 converted >= d * 0.99999999 && converted <= d * 1.00000001); 743 } 744 } 745 746 /** 747 * @tests java.lang.Math#toDegrees(double) 748 */ 749 @MediumTest 750 public void testToDegreesD() { 751 for (double d = 500; d >= 0; d -= 1.0) { 752 double converted = Math.toRadians(Math.toDegrees(d)); 753 assertTrue("Converted number not equal to original. d = " + d, 754 converted >= d * 0.99999999 && converted <= d * 1.00000001); 755 } 756 } 757 758 /** 759 * @tests java.lang.Math#ulp(double) 760 */ 761 @SuppressWarnings("boxing") 762 @SmallTest 763 public void testUlpD() { 764 // Test for special cases 765 assertTrue("Should return NaN", Double.isNaN(Math.ulp(Double.NaN))); 766 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 767 .ulp(Double.POSITIVE_INFINITY), 0D); 768 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 769 .ulp(Double.NEGATIVE_INFINITY), 0D); 770 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 771 .ulp(0.0), 0D); 772 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 773 .ulp(+0.0), 0D); 774 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 775 .ulp(-0.0), 0D); 776 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 777 .ulp(Double.MAX_VALUE), 0D); 778 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 779 .ulp(-Double.MAX_VALUE), 0D); 780 781 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 782 .ulp(Double.MIN_VALUE), 0D); 783 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 784 .ulp(-Double.MIN_VALUE), 0D); 785 786 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 787 .ulp(1.0), 0D); 788 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 789 .ulp(-1.0), 0D); 790 assertEquals("Returned incorrect value", 2.2737367544323206E-13, Math 791 .ulp(1153.0), 0D); 792 } 793 794 /** 795 * @tests java.lang.Math#ulp(float) 796 */ 797 @SuppressWarnings("boxing") 798 @SmallTest 799 public void testUlpf() { 800 // Test for special cases 801 assertTrue("Should return NaN", Float.isNaN(Math.ulp(Float.NaN))); 802 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 803 .ulp(Float.POSITIVE_INFINITY), 0f); 804 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 805 .ulp(Float.NEGATIVE_INFINITY), 0f); 806 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 807 .ulp(0.0f), 0f); 808 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 809 .ulp(+0.0f), 0f); 810 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 811 .ulp(-0.0f), 0f); 812 assertEquals("Returned incorrect value", 2.028241E31f, Math 813 .ulp(Float.MAX_VALUE), 0f); 814 assertEquals("Returned incorrect value", 2.028241E31f, Math 815 .ulp(-Float.MAX_VALUE), 0f); 816 817 assertEquals("Returned incorrect value", 1.4E-45f, Math 818 .ulp(Float.MIN_VALUE), 0f); 819 assertEquals("Returned incorrect value", 1.4E-45f, Math 820 .ulp(-Float.MIN_VALUE), 0f); 821 822 assertEquals("Returned incorrect value", 1.1920929E-7f, Math.ulp(1.0f), 823 0f); 824 assertEquals("Returned incorrect value", 1.1920929E-7f, 825 Math.ulp(-1.0f), 0f); 826 assertEquals("Returned incorrect value", 1.2207031E-4f, Math 827 .ulp(1153.0f), 0f); 828 assertEquals("Returned incorrect value", 5.6E-45f, Math 829 .ulp(9.403954E-38f), 0f); 830 } 831} 832