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