1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // This include (and the associated definition of __test_capture_signbit) 18 // must be placed before any files that include <cmath> (gtest.h in this case). 19 // 20 // <math.h> is required to define generic macros signbit, isfinite and 21 // several other such functions. 22 // 23 // <cmath> is required to undef declarations of these macros in the global 24 // namespace and make equivalent functions available in namespace std. Our 25 // stlport implementation does this only for signbit, isfinite, isinf and 26 // isnan. 27 // 28 // NOTE: We don't write our test using std::signbit because we want to be 29 // sure that we're testing the bionic version of signbit. The C++ libraries 30 // are free to reimplement signbit or delegate to compiler builtins if they 31 // please. 32 #include <math.h> 33 34 namespace { 35 template<typename T> inline int test_capture_signbit(const T in) { 36 return signbit(in); 37 } 38 template<typename T> inline int test_capture_isfinite(const T in) { 39 return isfinite(in); 40 } 41 template<typename T> inline int test_capture_isnan(const T in) { 42 return isnan(in); 43 } 44 template<typename T> inline int test_capture_isinf(const T in) { 45 return isinf(in); 46 } 47 } 48 49 #include <gtest/gtest.h> 50 51 #include <fenv.h> 52 #include <float.h> 53 #include <limits.h> 54 #include <stdint.h> 55 56 #include <private/ScopeGuard.h> 57 58 float float_subnormal() { 59 union { 60 float f; 61 uint32_t i; 62 } u; 63 u.i = 0x007fffff; 64 return u.f; 65 } 66 67 double double_subnormal() { 68 union { 69 double d; 70 uint64_t i; 71 } u; 72 u.i = 0x000fffffffffffffLL; 73 return u.d; 74 } 75 76 long double ldouble_subnormal() { 77 union { 78 long double e; 79 unsigned char c[sizeof(long double)]; 80 } u; 81 82 // Subnormals must have a zero exponent and non zero significand. 83 // On all supported representation the 17 bit (counting from either sides) 84 // is part of the significand so it should be enough to set that. 85 // It also applies for the case sizeof(double) = sizeof(long double) 86 for (unsigned int i = 0; i < sizeof(long double); i++) { 87 u.c[i] = 0x00; 88 } 89 u.c[sizeof(long double) - 3] = 0x80; 90 u.c[2] = 0x80; 91 92 return u.e; 93 } 94 95 TEST(math, fpclassify) { 96 ASSERT_EQ(FP_INFINITE, fpclassify(INFINITY)); 97 ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALF)); 98 ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VAL)); 99 ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALL)); 100 101 ASSERT_EQ(FP_NAN, fpclassify(nanf(""))); 102 ASSERT_EQ(FP_NAN, fpclassify(nan(""))); 103 ASSERT_EQ(FP_NAN, fpclassify(nanl(""))); 104 105 ASSERT_EQ(FP_NORMAL, fpclassify(1.0f)); 106 ASSERT_EQ(FP_NORMAL, fpclassify(1.0)); 107 ASSERT_EQ(FP_NORMAL, fpclassify(1.0L)); 108 109 ASSERT_EQ(FP_SUBNORMAL, fpclassify(float_subnormal())); 110 ASSERT_EQ(FP_SUBNORMAL, fpclassify(double_subnormal())); 111 ASSERT_EQ(FP_SUBNORMAL, fpclassify(ldouble_subnormal())); 112 113 ASSERT_EQ(FP_ZERO, fpclassify(0.0f)); 114 ASSERT_EQ(FP_ZERO, fpclassify(0.0)); 115 ASSERT_EQ(FP_ZERO, fpclassify(0.0L)); 116 } 117 118 TEST(math, isfinite) { 119 ASSERT_TRUE(test_capture_isfinite(123.0f)); 120 ASSERT_TRUE(test_capture_isfinite(123.0)); 121 ASSERT_TRUE(test_capture_isfinite(123.0L)); 122 ASSERT_FALSE(test_capture_isfinite(HUGE_VALF)); 123 ASSERT_FALSE(test_capture_isfinite(HUGE_VAL)); 124 ASSERT_FALSE(test_capture_isfinite(HUGE_VALL)); 125 } 126 127 TEST(math, isinf) { 128 ASSERT_FALSE(test_capture_isinf(123.0f)); 129 ASSERT_FALSE(test_capture_isinf(123.0)); 130 ASSERT_FALSE(test_capture_isinf(123.0L)); 131 ASSERT_TRUE(test_capture_isinf(HUGE_VALF)); 132 ASSERT_TRUE(test_capture_isinf(HUGE_VAL)); 133 ASSERT_TRUE(test_capture_isinf(HUGE_VALL)); 134 } 135 136 TEST(math, isnan) { 137 ASSERT_FALSE(test_capture_isnan(123.0f)); 138 ASSERT_FALSE(test_capture_isnan(123.0)); 139 ASSERT_FALSE(test_capture_isnan(123.0L)); 140 ASSERT_TRUE(test_capture_isnan(nanf(""))); 141 ASSERT_TRUE(test_capture_isnan(nan(""))); 142 ASSERT_TRUE(test_capture_isnan(nanl(""))); 143 } 144 145 TEST(math, isnormal) { 146 ASSERT_TRUE(isnormal(123.0f)); 147 ASSERT_TRUE(isnormal(123.0)); 148 ASSERT_TRUE(isnormal(123.0L)); 149 ASSERT_FALSE(isnormal(float_subnormal())); 150 ASSERT_FALSE(isnormal(double_subnormal())); 151 ASSERT_FALSE(isnormal(ldouble_subnormal())); 152 } 153 154 // TODO: isgreater, isgreaterequals, isless, islessequal, islessgreater, isunordered 155 TEST(math, signbit) { 156 ASSERT_EQ(0, test_capture_signbit(0.0f)); 157 ASSERT_EQ(0, test_capture_signbit(0.0)); 158 ASSERT_EQ(0, test_capture_signbit(0.0L)); 159 160 ASSERT_EQ(0, test_capture_signbit(1.0f)); 161 ASSERT_EQ(0, test_capture_signbit(1.0)); 162 ASSERT_EQ(0, test_capture_signbit(1.0L)); 163 164 ASSERT_NE(0, test_capture_signbit(-1.0f)); 165 ASSERT_NE(0, test_capture_signbit(-1.0)); 166 ASSERT_NE(0, test_capture_signbit(-1.0L)); 167 } 168 169 TEST(math, __fpclassifyd) { 170 #if defined(__BIONIC__) 171 ASSERT_EQ(FP_INFINITE, __fpclassifyd(HUGE_VAL)); 172 ASSERT_EQ(FP_NAN, __fpclassifyd(nan(""))); 173 ASSERT_EQ(FP_NORMAL, __fpclassifyd(1.0)); 174 ASSERT_EQ(FP_SUBNORMAL, __fpclassifyd(double_subnormal())); 175 ASSERT_EQ(FP_ZERO, __fpclassifyd(0.0)); 176 #else // __BIONIC__ 177 GTEST_LOG_(INFO) << "This test does nothing.\n"; 178 #endif // __BIONIC__ 179 } 180 181 TEST(math, __fpclassifyf) { 182 #if defined(__BIONIC__) 183 ASSERT_EQ(FP_INFINITE, __fpclassifyf(HUGE_VALF)); 184 ASSERT_EQ(FP_NAN, __fpclassifyf(nanf(""))); 185 ASSERT_EQ(FP_NORMAL, __fpclassifyf(1.0f)); 186 ASSERT_EQ(FP_SUBNORMAL, __fpclassifyf(float_subnormal())); 187 ASSERT_EQ(FP_ZERO, __fpclassifyf(0.0f)); 188 #else // __BIONIC__ 189 GTEST_LOG_(INFO) << "This test does nothing.\n"; 190 #endif // __BIONIC__ 191 } 192 193 TEST(math, __fpclassifyl) { 194 #if defined(__BIONIC__) 195 EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL)); 196 EXPECT_EQ(FP_NAN, __fpclassifyl(nanl(""))); 197 EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0L)); 198 EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(ldouble_subnormal())); 199 EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0L)); 200 #else // __BIONIC__ 201 GTEST_LOG_(INFO) << "This test does nothing.\n"; 202 #endif // __BIONIC__ 203 } 204 205 TEST(math, finitef) { 206 ASSERT_TRUE(finitef(123.0f)); 207 ASSERT_FALSE(finitef(HUGE_VALF)); 208 } 209 210 TEST(math, __isfinite) { 211 #if defined(__BIONIC__) 212 ASSERT_TRUE(__isfinite(123.0)); 213 ASSERT_FALSE(__isfinite(HUGE_VAL)); 214 #else // __BIONIC__ 215 GTEST_LOG_(INFO) << "This test does nothing.\n"; 216 #endif // __BIONIC__ 217 } 218 219 TEST(math, __isfinitef) { 220 #if defined(__BIONIC__) 221 ASSERT_TRUE(__isfinitef(123.0f)); 222 ASSERT_FALSE(__isfinitef(HUGE_VALF)); 223 #else // __BIONIC__ 224 GTEST_LOG_(INFO) << "This test does nothing.\n"; 225 #endif // __BIONIC__ 226 } 227 228 TEST(math, __isfinitel) { 229 #if defined(__BIONIC__) 230 ASSERT_TRUE(__isfinitel(123.0L)); 231 ASSERT_FALSE(__isfinitel(HUGE_VALL)); 232 #else // __BIONIC__ 233 GTEST_LOG_(INFO) << "This test does nothing.\n"; 234 #endif // __BIONIC__ 235 } 236 237 TEST(math, finite) { 238 ASSERT_TRUE(finite(123.0)); 239 ASSERT_FALSE(finite(HUGE_VAL)); 240 } 241 242 TEST(math, isinf_function) { 243 // The isinf macro deals with all three types; the isinf function is for doubles. 244 ASSERT_FALSE((isinf)(123.0)); 245 ASSERT_TRUE((isinf)(HUGE_VAL)); 246 } 247 248 TEST(math, __isinff) { 249 ASSERT_FALSE(__isinff(123.0f)); 250 ASSERT_TRUE(__isinff(HUGE_VALF)); 251 } 252 253 TEST(math, __isinfl) { 254 ASSERT_FALSE(__isinfl(123.0L)); 255 ASSERT_TRUE(__isinfl(HUGE_VALL)); 256 } 257 258 TEST(math, isnan_function) { 259 // The isnan macro deals with all three types; the isnan function is for doubles. 260 ASSERT_FALSE((isnan)(123.0)); 261 ASSERT_TRUE((isnan)(nan(""))); 262 } 263 264 TEST(math, __isnanf) { 265 ASSERT_FALSE(__isnanf(123.0f)); 266 ASSERT_TRUE(__isnanf(nanf(""))); 267 } 268 269 TEST(math, __isnanl) { 270 ASSERT_FALSE(__isnanl(123.0L)); 271 ASSERT_TRUE(__isnanl(nanl(""))); 272 } 273 274 TEST(math, isnanf) { 275 ASSERT_FALSE(isnanf(123.0f)); 276 ASSERT_TRUE(isnanf(nanf(""))); 277 } 278 279 TEST(math, __isnormal) { 280 #if defined(__BIONIC__) 281 ASSERT_TRUE(__isnormal(123.0)); 282 ASSERT_FALSE(__isnormal(double_subnormal())); 283 #else // __BIONIC__ 284 GTEST_LOG_(INFO) << "This test does nothing.\n"; 285 #endif // __BIONIC__ 286 } 287 288 TEST(math, __isnormalf) { 289 #if defined(__BIONIC__) 290 ASSERT_TRUE(__isnormalf(123.0f)); 291 ASSERT_FALSE(__isnormalf(float_subnormal())); 292 #else // __BIONIC__ 293 GTEST_LOG_(INFO) << "This test does nothing.\n"; 294 #endif // __BIONIC__ 295 } 296 297 TEST(math, __isnormall) { 298 #if defined(__BIONIC__) 299 ASSERT_TRUE(__isnormall(123.0L)); 300 ASSERT_FALSE(__isnormall(ldouble_subnormal())); 301 #else // __BIONIC__ 302 GTEST_LOG_(INFO) << "This test does nothing.\n"; 303 #endif // __BIONIC__ 304 } 305 306 TEST(math, __signbit) { 307 ASSERT_EQ(0, __signbit(0.0)); 308 ASSERT_EQ(0, __signbit(1.0)); 309 ASSERT_NE(0, __signbit(-1.0)); 310 } 311 312 TEST(math, __signbitf) { 313 ASSERT_EQ(0, __signbitf(0.0f)); 314 ASSERT_EQ(0, __signbitf(1.0f)); 315 ASSERT_NE(0, __signbitf(-1.0f)); 316 } 317 318 TEST(math, __signbitl) { 319 ASSERT_EQ(0L, __signbitl(0.0L)); 320 ASSERT_EQ(0L, __signbitl(1.0L)); 321 ASSERT_NE(0L, __signbitl(-1.0L)); 322 } 323 324 TEST(math, acos) { 325 ASSERT_DOUBLE_EQ(M_PI/2.0, acos(0.0)); 326 } 327 328 TEST(math, acosf) { 329 ASSERT_FLOAT_EQ(static_cast<float>(M_PI)/2.0f, acosf(0.0f)); 330 } 331 332 TEST(math, acosl) { 333 ASSERT_DOUBLE_EQ(M_PI/2.0L, acosl(0.0L)); 334 } 335 336 TEST(math, asin) { 337 ASSERT_DOUBLE_EQ(0.0, asin(0.0)); 338 } 339 340 TEST(math, asinf) { 341 ASSERT_FLOAT_EQ(0.0f, asinf(0.0f)); 342 } 343 344 TEST(math, asinl) { 345 ASSERT_DOUBLE_EQ(0.0L, asinl(0.0L)); 346 } 347 348 TEST(math, atan) { 349 ASSERT_DOUBLE_EQ(0.0, atan(0.0)); 350 } 351 352 TEST(math, atanf) { 353 ASSERT_FLOAT_EQ(0.0f, atanf(0.0f)); 354 } 355 356 TEST(math, atanl) { 357 ASSERT_DOUBLE_EQ(0.0L, atanl(0.0L)); 358 } 359 360 TEST(math, atan2) { 361 ASSERT_DOUBLE_EQ(0.0, atan2(0.0, 0.0)); 362 } 363 364 TEST(math, atan2f) { 365 ASSERT_FLOAT_EQ(0.0f, atan2f(0.0f, 0.0f)); 366 } 367 368 TEST(math, atan2l) { 369 ASSERT_DOUBLE_EQ(0.0L, atan2l(0.0L, 0.0L)); 370 } 371 372 TEST(math, cos) { 373 ASSERT_DOUBLE_EQ(1.0, cos(0.0)); 374 } 375 376 TEST(math, cosf) { 377 ASSERT_FLOAT_EQ(1.0f, cosf(0.0f)); 378 } 379 380 TEST(math, cosl) { 381 ASSERT_DOUBLE_EQ(1.0L, cosl(0.0L)); 382 } 383 384 TEST(math, sin) { 385 ASSERT_DOUBLE_EQ(0.0, sin(0.0)); 386 } 387 388 TEST(math, sinf) { 389 ASSERT_FLOAT_EQ(0.0f, sinf(0.0f)); 390 } 391 392 TEST(math, sinl) { 393 ASSERT_DOUBLE_EQ(0.0L, sinl(0.0L)); 394 } 395 396 TEST(math, tan) { 397 ASSERT_DOUBLE_EQ(0.0, tan(0.0)); 398 } 399 400 TEST(math, tanf) { 401 ASSERT_FLOAT_EQ(0.0f, tanf(0.0f)); 402 } 403 404 TEST(math, tanl) { 405 ASSERT_DOUBLE_EQ(0.0L, tanl(0.0L)); 406 } 407 408 TEST(math, acosh) { 409 ASSERT_DOUBLE_EQ(0.0, acosh(1.0)); 410 } 411 412 TEST(math, acoshf) { 413 ASSERT_FLOAT_EQ(0.0f, acoshf(1.0f)); 414 } 415 416 TEST(math, acoshl) { 417 ASSERT_DOUBLE_EQ(0.0L, acoshl(1.0L)); 418 } 419 420 TEST(math, asinh) { 421 ASSERT_DOUBLE_EQ(0.0, asinh(0.0)); 422 } 423 424 TEST(math, asinhf) { 425 ASSERT_FLOAT_EQ(0.0f, asinhf(0.0f)); 426 } 427 428 TEST(math, asinhl) { 429 ASSERT_DOUBLE_EQ(0.0L, asinhl(0.0L)); 430 } 431 432 TEST(math, atanh) { 433 ASSERT_DOUBLE_EQ(0.0, atanh(0.0)); 434 } 435 436 TEST(math, atanhf) { 437 ASSERT_FLOAT_EQ(0.0f, atanhf(0.0f)); 438 } 439 440 TEST(math, atanhl) { 441 ASSERT_DOUBLE_EQ(0.0L, atanhl(0.0L)); 442 } 443 444 TEST(math, cosh) { 445 ASSERT_DOUBLE_EQ(1.0, cosh(0.0)); 446 } 447 448 TEST(math, coshf) { 449 ASSERT_FLOAT_EQ(1.0f, coshf(0.0f)); 450 } 451 452 TEST(math, coshl) { 453 ASSERT_DOUBLE_EQ(1.0L, coshl(0.0L)); 454 } 455 456 TEST(math, sinh) { 457 ASSERT_DOUBLE_EQ(0.0, sinh(0.0)); 458 } 459 460 TEST(math, sinhf) { 461 ASSERT_FLOAT_EQ(0.0f, sinhf(0.0f)); 462 } 463 464 TEST(math, sinhl) { 465 ASSERT_DOUBLE_EQ(0.0L, sinhl(0.0L)); 466 } 467 468 TEST(math, tanh) { 469 ASSERT_DOUBLE_EQ(0.0, tanh(0.0)); 470 } 471 472 TEST(math, tanhf) { 473 ASSERT_FLOAT_EQ(0.0f, tanhf(0.0f)); 474 } 475 476 TEST(math, tanhl) { 477 ASSERT_DOUBLE_EQ(0.0L, tanhl(0.0L)); 478 } 479 480 TEST(math, log) { 481 ASSERT_DOUBLE_EQ(1.0, log(M_E)); 482 } 483 484 TEST(math, logf) { 485 ASSERT_FLOAT_EQ(1.0f, logf(static_cast<float>(M_E))); 486 } 487 488 TEST(math, logl) { 489 ASSERT_DOUBLE_EQ(1.0L, logl(M_E)); 490 } 491 492 TEST(math, log2) { 493 ASSERT_DOUBLE_EQ(12.0, log2(4096.0)); 494 } 495 496 TEST(math, log2f) { 497 ASSERT_FLOAT_EQ(12.0f, log2f(4096.0f)); 498 } 499 500 TEST(math, log2l) { 501 ASSERT_DOUBLE_EQ(12.0L, log2l(4096.0L)); 502 } 503 504 TEST(math, log10) { 505 ASSERT_DOUBLE_EQ(3.0, log10(1000.0)); 506 } 507 508 TEST(math, log10f) { 509 ASSERT_FLOAT_EQ(3.0f, log10f(1000.0f)); 510 } 511 512 TEST(math, log10l) { 513 ASSERT_DOUBLE_EQ(3.0L, log10l(1000.0L)); 514 } 515 516 TEST(math, cbrt) { 517 ASSERT_DOUBLE_EQ(3.0, cbrt(27.0)); 518 } 519 520 TEST(math, cbrtf) { 521 ASSERT_FLOAT_EQ(3.0f, cbrtf(27.0f)); 522 } 523 524 TEST(math, cbrtl) { 525 ASSERT_DOUBLE_EQ(3.0L, cbrtl(27.0L)); 526 } 527 528 TEST(math, sqrt) { 529 ASSERT_DOUBLE_EQ(2.0, sqrt(4.0)); 530 } 531 532 TEST(math, sqrtf) { 533 ASSERT_FLOAT_EQ(2.0f, sqrtf(4.0f)); 534 } 535 536 TEST(math, sqrtl) { 537 ASSERT_DOUBLE_EQ(2.0L, sqrtl(4.0L)); 538 } 539 540 TEST(math, exp) { 541 ASSERT_DOUBLE_EQ(1.0, exp(0.0)); 542 ASSERT_DOUBLE_EQ(M_E, exp(1.0)); 543 } 544 545 TEST(math, expf) { 546 ASSERT_FLOAT_EQ(1.0f, expf(0.0f)); 547 ASSERT_FLOAT_EQ(static_cast<float>(M_E), expf(1.0f)); 548 } 549 550 TEST(math, expl) { 551 ASSERT_DOUBLE_EQ(1.0L, expl(0.0L)); 552 ASSERT_DOUBLE_EQ(M_E, expl(1.0L)); 553 } 554 555 TEST(math, exp2) { 556 ASSERT_DOUBLE_EQ(8.0, exp2(3.0)); 557 } 558 559 TEST(math, exp2f) { 560 ASSERT_FLOAT_EQ(8.0f, exp2f(3.0f)); 561 } 562 563 TEST(math, exp2l) { 564 ASSERT_DOUBLE_EQ(8.0L, exp2l(3.0L)); 565 } 566 567 TEST(math, expm1) { 568 ASSERT_DOUBLE_EQ(M_E - 1.0, expm1(1.0)); 569 } 570 571 TEST(math, expm1f) { 572 ASSERT_FLOAT_EQ(static_cast<float>(M_E) - 1.0f, expm1f(1.0f)); 573 } 574 575 TEST(math, expm1l) { 576 ASSERT_DOUBLE_EQ(M_E - 1.0L, expm1l(1.0L)); 577 } 578 579 TEST(math, pow) { 580 ASSERT_TRUE(isnan(pow(nan(""), 3.0))); 581 ASSERT_DOUBLE_EQ(1.0, (pow(1.0, nan("")))); 582 ASSERT_TRUE(isnan(pow(2.0, nan("")))); 583 ASSERT_DOUBLE_EQ(8.0, pow(2.0, 3.0)); 584 } 585 586 TEST(math, powf) { 587 ASSERT_TRUE(isnanf(powf(nanf(""), 3.0f))); 588 ASSERT_FLOAT_EQ(1.0f, (powf(1.0f, nanf("")))); 589 ASSERT_TRUE(isnanf(powf(2.0f, nanf("")))); 590 ASSERT_FLOAT_EQ(8.0f, powf(2.0f, 3.0f)); 591 } 592 593 TEST(math, powl) { 594 ASSERT_TRUE(__isnanl(powl(nanl(""), 3.0L))); 595 ASSERT_DOUBLE_EQ(1.0L, (powl(1.0L, nanl("")))); 596 ASSERT_TRUE(__isnanl(powl(2.0L, nanl("")))); 597 ASSERT_DOUBLE_EQ(8.0L, powl(2.0L, 3.0L)); 598 } 599 600 TEST(math, ceil) { 601 ASSERT_DOUBLE_EQ(1.0, ceil(0.9)); 602 } 603 604 TEST(math, ceilf) { 605 ASSERT_FLOAT_EQ(1.0f, ceilf(0.9f)); 606 } 607 608 TEST(math, ceill) { 609 ASSERT_DOUBLE_EQ(1.0L, ceill(0.9L)); 610 } 611 612 TEST(math, floor) { 613 ASSERT_DOUBLE_EQ(1.0, floor(1.1)); 614 } 615 616 TEST(math, floorf) { 617 ASSERT_FLOAT_EQ(1.0f, floorf(1.1f)); 618 } 619 620 TEST(math, floorl) { 621 ASSERT_DOUBLE_EQ(1.0L, floorl(1.1L)); 622 } 623 624 TEST(math, fabs) { 625 ASSERT_DOUBLE_EQ(1.0, fabs(-1.0)); 626 } 627 628 TEST(math, fabsf) { 629 ASSERT_FLOAT_EQ(1.0f, fabsf(-1.0f)); 630 } 631 632 TEST(math, fabsl) { 633 ASSERT_DOUBLE_EQ(1.0L, fabsl(-1.0L)); 634 } 635 636 TEST(math, ldexp) { 637 ASSERT_DOUBLE_EQ(16.0, ldexp(2.0, 3.0)); 638 } 639 640 TEST(math, ldexpf) { 641 ASSERT_FLOAT_EQ(16.0f, ldexpf(2.0f, 3.0f)); 642 } 643 644 TEST(math, ldexpl) { 645 ASSERT_DOUBLE_EQ(16.0L, ldexpl(2.0L, 3.0)); 646 } 647 648 TEST(math, fmod) { 649 ASSERT_DOUBLE_EQ(2.0, fmod(12.0, 10.0)); 650 } 651 652 TEST(math, fmodf) { 653 ASSERT_FLOAT_EQ(2.0f, fmodf(12.0f, 10.0f)); 654 } 655 656 TEST(math, fmodl) { 657 ASSERT_DOUBLE_EQ(2.0L, fmodl(12.0L, 10.0L)); 658 } 659 660 TEST(math, remainder) { 661 ASSERT_DOUBLE_EQ(2.0, remainder(12.0, 10.0)); 662 } 663 664 TEST(math, remainderf) { 665 ASSERT_FLOAT_EQ(2.0f, remainderf(12.0f, 10.0f)); 666 } 667 668 TEST(math, remainderl) { 669 ASSERT_DOUBLE_EQ(2.0L, remainderl(12.0L, 10.0L)); 670 } 671 672 TEST(math, drem) { 673 ASSERT_DOUBLE_EQ(2.0, drem(12.0, 10.0)); 674 } 675 676 TEST(math, dremf) { 677 ASSERT_FLOAT_EQ(2.0f, dremf(12.0f, 10.0f)); 678 } 679 680 TEST(math, fmax) { 681 ASSERT_DOUBLE_EQ(12.0, fmax(12.0, 10.0)); 682 ASSERT_DOUBLE_EQ(12.0, fmax(12.0, nan(""))); 683 ASSERT_DOUBLE_EQ(12.0, fmax(nan(""), 12.0)); 684 } 685 686 TEST(math, fmaxf) { 687 ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, 10.0f)); 688 ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, nanf(""))); 689 ASSERT_FLOAT_EQ(12.0f, fmaxf(nanf(""), 12.0f)); 690 } 691 692 TEST(math, fmaxl) { 693 ASSERT_DOUBLE_EQ(12.0L, fmaxl(12.0L, 10.0L)); 694 ASSERT_DOUBLE_EQ(12.0L, fmaxl(12.0L, nanl(""))); 695 ASSERT_DOUBLE_EQ(12.0L, fmaxl(nanl(""), 12.0L)); 696 } 697 698 TEST(math, fmin) { 699 ASSERT_DOUBLE_EQ(10.0, fmin(12.0, 10.0)); 700 ASSERT_DOUBLE_EQ(12.0, fmin(12.0, nan(""))); 701 ASSERT_DOUBLE_EQ(12.0, fmin(nan(""), 12.0)); 702 } 703 704 TEST(math, fminf) { 705 ASSERT_FLOAT_EQ(10.0f, fminf(12.0f, 10.0f)); 706 ASSERT_FLOAT_EQ(12.0f, fminf(12.0f, nanf(""))); 707 ASSERT_FLOAT_EQ(12.0f, fminf(nanf(""), 12.0f)); 708 } 709 710 TEST(math, fminl) { 711 ASSERT_DOUBLE_EQ(10.0L, fminl(12.0L, 10.0L)); 712 ASSERT_DOUBLE_EQ(12.0L, fminl(12.0L, nanl(""))); 713 ASSERT_DOUBLE_EQ(12.0L, fminl(nanl(""), 12.0L)); 714 } 715 716 TEST(math, fma) { 717 ASSERT_DOUBLE_EQ(10.0, fma(2.0, 3.0, 4.0)); 718 } 719 720 TEST(math, fmaf) { 721 ASSERT_FLOAT_EQ(10.0f, fmaf(2.0f, 3.0f, 4.0f)); 722 } 723 724 TEST(math, fmal) { 725 ASSERT_DOUBLE_EQ(10.0L, fmal(2.0L, 3.0L, 4.0L)); 726 } 727 728 TEST(math, hypot) { 729 ASSERT_DOUBLE_EQ(5.0, hypot(3.0, 4.0)); 730 } 731 732 TEST(math, hypotf) { 733 ASSERT_FLOAT_EQ(5.0f, hypotf(3.0f, 4.0f)); 734 } 735 736 TEST(math, hypotl) { 737 ASSERT_DOUBLE_EQ(5.0L, hypotl(3.0L, 4.0L)); 738 } 739 740 TEST(math, erf) { 741 ASSERT_DOUBLE_EQ(0.84270079294971489, erf(1.0)); 742 } 743 744 TEST(math, erff) { 745 ASSERT_FLOAT_EQ(0.84270078f, erff(1.0f)); 746 } 747 748 TEST(math, erfl) { 749 ASSERT_DOUBLE_EQ(0.84270079294971489L, erfl(1.0L)); 750 } 751 752 TEST(math, erfc) { 753 ASSERT_DOUBLE_EQ(0.15729920705028513, erfc(1.0)); 754 } 755 756 TEST(math, erfcf) { 757 ASSERT_FLOAT_EQ(0.15729921f, erfcf(1.0f)); 758 } 759 760 TEST(math, erfcl) { 761 ASSERT_DOUBLE_EQ(0.15729920705028513l, erfcl(1.0L)); 762 } 763 764 TEST(math, lrint) { 765 auto guard = make_scope_guard([]() { 766 fesetenv(FE_DFL_ENV); 767 }); 768 769 fesetround(FE_UPWARD); // lrint/lrintf/lrintl obey the rounding mode. 770 ASSERT_EQ(1235, lrint(1234.01)); 771 ASSERT_EQ(1235, lrintf(1234.01f)); 772 ASSERT_EQ(1235, lrintl(1234.01L)); 773 fesetround(FE_TOWARDZERO); // lrint/lrintf/lrintl obey the rounding mode. 774 ASSERT_EQ(1234, lrint(1234.01)); 775 ASSERT_EQ(1234, lrintf(1234.01f)); 776 ASSERT_EQ(1234, lrintl(1234.01L)); 777 778 fesetround(FE_UPWARD); // llrint/llrintf/llrintl obey the rounding mode. 779 ASSERT_EQ(1235L, llrint(1234.01)); 780 ASSERT_EQ(1235L, llrintf(1234.01f)); 781 ASSERT_EQ(1235L, llrintl(1234.01L)); 782 fesetround(FE_TOWARDZERO); // llrint/llrintf/llrintl obey the rounding mode. 783 ASSERT_EQ(1234L, llrint(1234.01)); 784 ASSERT_EQ(1234L, llrintf(1234.01f)); 785 ASSERT_EQ(1234L, llrintl(1234.01L)); 786 } 787 788 TEST(math, rint) { 789 auto guard = make_scope_guard([]() { 790 fesetenv(FE_DFL_ENV); 791 }); 792 793 fesetround(FE_UPWARD); // rint/rintf/rintl obey the rounding mode. 794 feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag. 795 ASSERT_EQ(1234.0, rint(1234.0)); 796 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 797 ASSERT_EQ(1235.0, rint(1234.01)); 798 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0); 799 800 feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag. 801 ASSERT_EQ(1234.0f, rintf(1234.0f)); 802 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 803 ASSERT_EQ(1235.0f, rintf(1234.01f)); 804 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0); 805 806 feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag. 807 ASSERT_EQ(1234.0, rintl(1234.0L)); 808 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 809 ASSERT_EQ(1235.0, rintl(1234.01L)); 810 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0); 811 812 fesetround(FE_TOWARDZERO); // rint/rintf obey the rounding mode. 813 ASSERT_EQ(1234.0, rint(1234.01)); 814 ASSERT_EQ(1234.0f, rintf(1234.01f)); 815 ASSERT_EQ(1234.0, rintl(1234.01L)); 816 } 817 818 TEST(math, nearbyint) { 819 auto guard = make_scope_guard([]() { 820 fesetenv(FE_DFL_ENV); 821 }); 822 fesetround(FE_UPWARD); // nearbyint/nearbyintf/nearbyintl obey the rounding mode. 823 feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag. 824 ASSERT_EQ(1234.0, nearbyint(1234.0)); 825 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 826 ASSERT_EQ(1235.0, nearbyint(1234.01)); 827 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 828 829 feclearexcept(FE_ALL_EXCEPT); 830 ASSERT_EQ(1234.0f, nearbyintf(1234.0f)); 831 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 832 ASSERT_EQ(1235.0f, nearbyintf(1234.01f)); 833 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 834 835 feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag. 836 ASSERT_EQ(1234.0, nearbyintl(1234.0L)); 837 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 838 ASSERT_EQ(1235.0, nearbyintl(1234.01L)); 839 ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0); 840 841 fesetround(FE_TOWARDZERO); // nearbyint/nearbyintf/nearbyintl obey the rounding mode. 842 ASSERT_EQ(1234.0, nearbyint(1234.01)); 843 ASSERT_EQ(1234.0f, nearbyintf(1234.01f)); 844 ASSERT_EQ(1234.0, nearbyintl(1234.01L)); 845 } 846 847 TEST(math, lround) { 848 auto guard = make_scope_guard([]() { 849 fesetenv(FE_DFL_ENV); 850 }); 851 fesetround(FE_UPWARD); // lround ignores the rounding mode. 852 ASSERT_EQ(1234, lround(1234.01)); 853 ASSERT_EQ(1234, lroundf(1234.01f)); 854 ASSERT_EQ(1234, lroundl(1234.01L)); 855 } 856 857 TEST(math, llround) { 858 auto guard = make_scope_guard([]() { 859 fesetenv(FE_DFL_ENV); 860 }); 861 fesetround(FE_UPWARD); // llround ignores the rounding mode. 862 ASSERT_EQ(1234L, llround(1234.01)); 863 ASSERT_EQ(1234L, llroundf(1234.01f)); 864 ASSERT_EQ(1234L, llroundl(1234.01L)); 865 } 866 867 TEST(math, ilogb) { 868 ASSERT_EQ(FP_ILOGB0, ilogb(0.0)); 869 ASSERT_EQ(FP_ILOGBNAN, ilogb(nan(""))); 870 ASSERT_EQ(INT_MAX, ilogb(HUGE_VAL)); 871 ASSERT_EQ(0, ilogb(1.0)); 872 ASSERT_EQ(3, ilogb(10.0)); 873 } 874 875 TEST(math, ilogbf) { 876 ASSERT_EQ(FP_ILOGB0, ilogbf(0.0f)); 877 ASSERT_EQ(FP_ILOGBNAN, ilogbf(nanf(""))); 878 ASSERT_EQ(INT_MAX, ilogbf(HUGE_VALF)); 879 ASSERT_EQ(0, ilogbf(1.0f)); 880 ASSERT_EQ(3, ilogbf(10.0f)); 881 } 882 883 TEST(math, ilogbl) { 884 ASSERT_EQ(FP_ILOGB0, ilogbl(0.0L)); 885 ASSERT_EQ(FP_ILOGBNAN, ilogbl(nanl(""))); 886 ASSERT_EQ(INT_MAX, ilogbl(HUGE_VALL)); 887 ASSERT_EQ(0L, ilogbl(1.0L)); 888 ASSERT_EQ(3L, ilogbl(10.0L)); 889 } 890 891 TEST(math, logb) { 892 ASSERT_EQ(-HUGE_VAL, logb(0.0)); 893 ASSERT_TRUE(isnan(logb(nan("")))); 894 ASSERT_TRUE(isinf(logb(HUGE_VAL))); 895 ASSERT_EQ(0.0, logb(1.0)); 896 ASSERT_EQ(3.0, logb(10.0)); 897 } 898 899 TEST(math, logbf) { 900 ASSERT_EQ(-HUGE_VALF, logbf(0.0f)); 901 ASSERT_TRUE(isnanf(logbf(nanf("")))); 902 ASSERT_TRUE(__isinff(logbf(HUGE_VALF))); 903 ASSERT_EQ(0.0f, logbf(1.0f)); 904 ASSERT_EQ(3.0f, logbf(10.0f)); 905 } 906 907 TEST(math, logbl) { 908 ASSERT_EQ(-HUGE_VAL, logbl(0.0L)); 909 ASSERT_TRUE(isnan(logbl(nanl("")))); 910 ASSERT_TRUE(isinf(logbl(HUGE_VALL))); 911 ASSERT_EQ(0.0L, logbl(1.0L)); 912 ASSERT_EQ(3.0L, logbl(10.0L)); 913 } 914 915 TEST(math, log1p) { 916 ASSERT_EQ(-HUGE_VAL, log1p(-1.0)); 917 ASSERT_TRUE(isnan(log1p(nan("")))); 918 ASSERT_TRUE(isinf(log1p(HUGE_VAL))); 919 ASSERT_DOUBLE_EQ(1.0, log1p(M_E - 1.0)); 920 } 921 922 TEST(math, log1pf) { 923 ASSERT_EQ(-HUGE_VALF, log1pf(-1.0f)); 924 ASSERT_TRUE(isnanf(log1pf(nanf("")))); 925 ASSERT_TRUE(__isinff(log1pf(HUGE_VALF))); 926 ASSERT_FLOAT_EQ(1.0f, log1pf(static_cast<float>(M_E) - 1.0f)); 927 } 928 929 TEST(math, log1pl) { 930 ASSERT_EQ(-HUGE_VALL, log1pl(-1.0L)); 931 ASSERT_TRUE(isnan(log1pl(nanl("")))); 932 ASSERT_TRUE(isinf(log1pl(HUGE_VALL))); 933 ASSERT_DOUBLE_EQ(1.0L, log1pl(M_E - 1.0L)); 934 } 935 936 TEST(math, fdim) { 937 ASSERT_DOUBLE_EQ(0.0, fdim(1.0, 1.0)); 938 ASSERT_DOUBLE_EQ(1.0, fdim(2.0, 1.0)); 939 ASSERT_DOUBLE_EQ(0.0, fdim(1.0, 2.0)); 940 } 941 942 TEST(math, fdimf) { 943 ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 1.0f)); 944 ASSERT_FLOAT_EQ(1.0f, fdimf(2.0f, 1.0f)); 945 ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 2.0f)); 946 } 947 948 TEST(math, fdiml) { 949 ASSERT_DOUBLE_EQ(0.0L, fdiml(1.0L, 1.0L)); 950 ASSERT_DOUBLE_EQ(1.0L, fdiml(2.0L, 1.0L)); 951 ASSERT_DOUBLE_EQ(0.0L, fdiml(1.0L, 2.0L)); 952 } 953 954 TEST(math, round) { 955 auto guard = make_scope_guard([]() { 956 fesetenv(FE_DFL_ENV); 957 }); 958 fesetround(FE_TOWARDZERO); // round ignores the rounding mode and always rounds away from zero. 959 ASSERT_DOUBLE_EQ(1.0, round(0.5)); 960 ASSERT_DOUBLE_EQ(-1.0, round(-0.5)); 961 ASSERT_DOUBLE_EQ(0.0, round(0.0)); 962 ASSERT_DOUBLE_EQ(-0.0, round(-0.0)); 963 ASSERT_TRUE(isnan(round(nan("")))); 964 ASSERT_DOUBLE_EQ(HUGE_VAL, round(HUGE_VAL)); 965 } 966 967 TEST(math, roundf) { 968 auto guard = make_scope_guard([]() { 969 fesetenv(FE_DFL_ENV); 970 }); 971 fesetround(FE_TOWARDZERO); // roundf ignores the rounding mode and always rounds away from zero. 972 ASSERT_FLOAT_EQ(1.0f, roundf(0.5f)); 973 ASSERT_FLOAT_EQ(-1.0f, roundf(-0.5f)); 974 ASSERT_FLOAT_EQ(0.0f, roundf(0.0f)); 975 ASSERT_FLOAT_EQ(-0.0f, roundf(-0.0f)); 976 ASSERT_TRUE(isnanf(roundf(nanf("")))); 977 ASSERT_FLOAT_EQ(HUGE_VALF, roundf(HUGE_VALF)); 978 } 979 980 TEST(math, roundl) { 981 auto guard = make_scope_guard([]() { 982 fesetenv(FE_DFL_ENV); 983 }); 984 fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero. 985 ASSERT_DOUBLE_EQ(1.0L, roundl(0.5L)); 986 ASSERT_DOUBLE_EQ(-1.0L, roundl(-0.5L)); 987 ASSERT_DOUBLE_EQ(0.0L, roundl(0.0L)); 988 ASSERT_DOUBLE_EQ(-0.0L, roundl(-0.0L)); 989 ASSERT_TRUE(isnan(roundl(nanl("")))); 990 ASSERT_DOUBLE_EQ(HUGE_VALL, roundl(HUGE_VALL)); 991 } 992 993 TEST(math, trunc) { 994 auto guard = make_scope_guard([]() { 995 fesetenv(FE_DFL_ENV); 996 }); 997 fesetround(FE_UPWARD); // trunc ignores the rounding mode and always rounds toward zero. 998 ASSERT_DOUBLE_EQ(1.0, trunc(1.5)); 999 ASSERT_DOUBLE_EQ(-1.0, trunc(-1.5)); 1000 ASSERT_DOUBLE_EQ(0.0, trunc(0.0)); 1001 ASSERT_DOUBLE_EQ(-0.0, trunc(-0.0)); 1002 ASSERT_TRUE(isnan(trunc(nan("")))); 1003 ASSERT_DOUBLE_EQ(HUGE_VAL, trunc(HUGE_VAL)); 1004 } 1005 1006 TEST(math, truncf) { 1007 auto guard = make_scope_guard([]() { 1008 fesetenv(FE_DFL_ENV); 1009 }); 1010 fesetround(FE_UPWARD); // truncf ignores the rounding mode and always rounds toward zero. 1011 ASSERT_FLOAT_EQ(1.0f, truncf(1.5f)); 1012 ASSERT_FLOAT_EQ(-1.0f, truncf(-1.5f)); 1013 ASSERT_FLOAT_EQ(0.0f, truncf(0.0f)); 1014 ASSERT_FLOAT_EQ(-0.0f, truncf(-0.0f)); 1015 ASSERT_TRUE(isnan(truncf(nanf("")))); 1016 ASSERT_FLOAT_EQ(HUGE_VALF, truncf(HUGE_VALF)); 1017 } 1018 1019 TEST(math, truncl) { 1020 auto guard = make_scope_guard([]() { 1021 fesetenv(FE_DFL_ENV); 1022 }); 1023 fesetround(FE_UPWARD); // truncl ignores the rounding mode and always rounds toward zero. 1024 ASSERT_DOUBLE_EQ(1.0L, truncl(1.5L)); 1025 ASSERT_DOUBLE_EQ(-1.0L, truncl(-1.5L)); 1026 ASSERT_DOUBLE_EQ(0.0L, truncl(0.0L)); 1027 ASSERT_DOUBLE_EQ(-0.0L, truncl(-0.0L)); 1028 ASSERT_TRUE(isnan(truncl(nan("")))); 1029 ASSERT_DOUBLE_EQ(HUGE_VALL, truncl(HUGE_VALL)); 1030 } 1031 1032 TEST(math, nextafter) { 1033 ASSERT_DOUBLE_EQ(0.0, nextafter(0.0, 0.0)); 1034 ASSERT_DOUBLE_EQ(4.9406564584124654e-324, nextafter(0.0, 1.0)); 1035 ASSERT_DOUBLE_EQ(0.0, nextafter(0.0, -1.0)); 1036 } 1037 1038 TEST(math, nextafterf) { 1039 ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, 0.0f)); 1040 ASSERT_FLOAT_EQ(1.4012985e-45f, nextafterf(0.0f, 1.0f)); 1041 ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, -1.0f)); 1042 } 1043 1044 TEST(math, nextafterl) { 1045 ASSERT_DOUBLE_EQ(0.0L, nextafterl(0.0L, 0.0L)); 1046 // Use a runtime value to accomodate the case when 1047 // sizeof(double) == sizeof(long double) 1048 long double smallest_positive = ldexpl(1.0L, LDBL_MIN_EXP - LDBL_MANT_DIG); 1049 ASSERT_DOUBLE_EQ(smallest_positive, nextafterl(0.0L, 1.0L)); 1050 ASSERT_DOUBLE_EQ(0.0L, nextafterl(0.0L, -1.0L)); 1051 } 1052 1053 TEST(math, nexttoward) { 1054 ASSERT_DOUBLE_EQ(0.0, nexttoward(0.0, 0.0L)); 1055 ASSERT_DOUBLE_EQ(4.9406564584124654e-324, nexttoward(0.0, 1.0L)); 1056 ASSERT_DOUBLE_EQ(0.0, nexttoward(0.0, -1.0L)); 1057 } 1058 1059 TEST(math, nexttowardf) { 1060 ASSERT_FLOAT_EQ(0.0f, nexttowardf(0.0f, 0.0L)); 1061 ASSERT_FLOAT_EQ(1.4012985e-45f, nexttowardf(0.0f, 1.0L)); 1062 ASSERT_FLOAT_EQ(0.0f, nexttowardf(0.0f, -1.0L)); 1063 } 1064 1065 TEST(math, nexttowardl) { 1066 ASSERT_DOUBLE_EQ(0.0L, nexttowardl(0.0L, 0.0L)); 1067 // Use a runtime value to accomodate the case when 1068 // sizeof(double) == sizeof(long double) 1069 long double smallest_positive = ldexpl(1.0L, LDBL_MIN_EXP - LDBL_MANT_DIG); 1070 ASSERT_DOUBLE_EQ(smallest_positive, nexttowardl(0.0L, 1.0L)); 1071 ASSERT_DOUBLE_EQ(0.0L, nexttowardl(0.0L, -1.0L)); 1072 } 1073 1074 TEST(math, copysign) { 1075 ASSERT_DOUBLE_EQ(0.0, copysign(0.0, 1.0)); 1076 ASSERT_DOUBLE_EQ(-0.0, copysign(0.0, -1.0)); 1077 ASSERT_DOUBLE_EQ(2.0, copysign(2.0, 1.0)); 1078 ASSERT_DOUBLE_EQ(-2.0, copysign(2.0, -1.0)); 1079 } 1080 1081 TEST(math, copysignf) { 1082 ASSERT_FLOAT_EQ(0.0f, copysignf(0.0f, 1.0f)); 1083 ASSERT_FLOAT_EQ(-0.0f, copysignf(0.0f, -1.0f)); 1084 ASSERT_FLOAT_EQ(2.0f, copysignf(2.0f, 1.0f)); 1085 ASSERT_FLOAT_EQ(-2.0f, copysignf(2.0f, -1.0f)); 1086 } 1087 1088 TEST(math, copysignl) { 1089 ASSERT_DOUBLE_EQ(0.0L, copysignl(0.0L, 1.0L)); 1090 ASSERT_DOUBLE_EQ(-0.0L, copysignl(0.0L, -1.0L)); 1091 ASSERT_DOUBLE_EQ(2.0L, copysignl(2.0L, 1.0L)); 1092 ASSERT_DOUBLE_EQ(-2.0L, copysignl(2.0L, -1.0L)); 1093 } 1094 1095 TEST(math, significand) { 1096 ASSERT_DOUBLE_EQ(0.0, significand(0.0)); 1097 ASSERT_DOUBLE_EQ(1.2, significand(1.2)); 1098 ASSERT_DOUBLE_EQ(1.5375, significand(12.3)); 1099 } 1100 1101 TEST(math, significandf) { 1102 ASSERT_FLOAT_EQ(0.0f, significandf(0.0f)); 1103 ASSERT_FLOAT_EQ(1.2f, significandf(1.2f)); 1104 ASSERT_FLOAT_EQ(1.5375f, significandf(12.3f)); 1105 } 1106 1107 TEST(math, significandl) { 1108 ASSERT_DOUBLE_EQ(0.0L, significandl(0.0L)); 1109 ASSERT_DOUBLE_EQ(1.2L, significandl(1.2L)); 1110 ASSERT_DOUBLE_EQ(1.5375L, significandl(12.3L)); 1111 } 1112 1113 TEST(math, scalb) { 1114 ASSERT_DOUBLE_EQ(12.0, scalb(3.0, 2.0)); 1115 } 1116 1117 TEST(math, scalbf) { 1118 ASSERT_FLOAT_EQ(12.0f, scalbf(3.0f, 2.0f)); 1119 } 1120 1121 TEST(math, scalbln) { 1122 ASSERT_DOUBLE_EQ(12.0, scalbln(3.0, 2L)); 1123 } 1124 1125 TEST(math, scalblnf) { 1126 ASSERT_FLOAT_EQ(12.0f, scalblnf(3.0f, 2L)); 1127 } 1128 1129 TEST(math, scalblnl) { 1130 ASSERT_DOUBLE_EQ(12.0L, scalblnl(3.0L, 2L)); 1131 } 1132 1133 TEST(math, scalbn) { 1134 ASSERT_DOUBLE_EQ(12.0, scalbn(3.0, 2)); 1135 } 1136 1137 TEST(math, scalbnf) { 1138 ASSERT_FLOAT_EQ(12.0f, scalbnf(3.0f, 2)); 1139 } 1140 1141 TEST(math, scalbnl) { 1142 ASSERT_DOUBLE_EQ(12.0L, scalbnl(3.0L, 2)); 1143 } 1144 1145 TEST(math, gamma) { 1146 ASSERT_DOUBLE_EQ(log(24.0), gamma(5.0)); 1147 } 1148 1149 TEST(math, gammaf) { 1150 ASSERT_FLOAT_EQ(logf(24.0f), gammaf(5.0f)); 1151 } 1152 1153 TEST(math, gamma_r) { 1154 #if defined(__BIONIC__) 1155 int sign; 1156 ASSERT_DOUBLE_EQ(log(24.0), gamma_r(5.0, &sign)); 1157 ASSERT_EQ(1, sign); 1158 #else // __BIONIC__ 1159 GTEST_LOG_(INFO) << "This test does nothing.\n"; 1160 #endif // __BIONIC__ 1161 } 1162 1163 TEST(math, gammaf_r) { 1164 #if defined(__BIONIC__) 1165 int sign; 1166 ASSERT_FLOAT_EQ(logf(24.0f), gammaf_r(5.0f, &sign)); 1167 ASSERT_EQ(1, sign); 1168 #else // __BIONIC__ 1169 GTEST_LOG_(INFO) << "This test does nothing.\n"; 1170 #endif // __BIONIC__ 1171 } 1172 1173 TEST(math, lgamma) { 1174 ASSERT_DOUBLE_EQ(log(24.0), lgamma(5.0)); 1175 } 1176 1177 TEST(math, lgammaf) { 1178 ASSERT_FLOAT_EQ(logf(24.0f), lgammaf(5.0f)); 1179 } 1180 1181 TEST(math, lgammal) { 1182 ASSERT_DOUBLE_EQ(logl(24.0L), lgammal(5.0L)); 1183 } 1184 1185 TEST(math, lgamma_r) { 1186 int sign; 1187 ASSERT_DOUBLE_EQ(log(24.0), lgamma_r(5.0, &sign)); 1188 ASSERT_EQ(1, sign); 1189 } 1190 1191 TEST(math, lgammaf_r) { 1192 int sign; 1193 ASSERT_FLOAT_EQ(logf(24.0f), lgammaf_r(5.0f, &sign)); 1194 ASSERT_EQ(1, sign); 1195 } 1196 1197 TEST(math, tgamma) { 1198 ASSERT_DOUBLE_EQ(24.0, tgamma(5.0)); 1199 } 1200 1201 TEST(math, tgammaf) { 1202 ASSERT_FLOAT_EQ(24.0f, tgammaf(5.0f)); 1203 } 1204 1205 TEST(math, tgammal) { 1206 ASSERT_DOUBLE_EQ(24.0L, tgammal(5.0L)); 1207 } 1208 1209 TEST(math, j0) { 1210 ASSERT_DOUBLE_EQ(1.0, j0(0.0)); 1211 ASSERT_DOUBLE_EQ(0.76519768655796661, j0(1.0)); 1212 } 1213 1214 TEST(math, j0f) { 1215 ASSERT_FLOAT_EQ(1.0f, j0f(0.0f)); 1216 ASSERT_FLOAT_EQ(0.76519769f, j0f(1.0f)); 1217 } 1218 1219 TEST(math, j1) { 1220 ASSERT_DOUBLE_EQ(0.0, j1(0.0)); 1221 ASSERT_DOUBLE_EQ(0.44005058574493355, j1(1.0)); 1222 } 1223 1224 TEST(math, j1f) { 1225 ASSERT_FLOAT_EQ(0.0f, j1f(0.0f)); 1226 ASSERT_FLOAT_EQ(0.44005057f, j1f(1.0f)); 1227 } 1228 1229 TEST(math, jn) { 1230 ASSERT_DOUBLE_EQ(0.0, jn(4, 0.0)); 1231 ASSERT_DOUBLE_EQ(0.0024766389641099553, jn(4, 1.0)); 1232 } 1233 1234 TEST(math, jnf) { 1235 ASSERT_FLOAT_EQ(0.0f, jnf(4, 0.0f)); 1236 ASSERT_FLOAT_EQ(0.0024766389f, jnf(4, 1.0f)); 1237 } 1238 1239 TEST(math, y0) { 1240 ASSERT_DOUBLE_EQ(-HUGE_VAL, y0(0.0)); 1241 ASSERT_DOUBLE_EQ(0.08825696421567697, y0(1.0)); 1242 } 1243 1244 TEST(math, y0f) { 1245 ASSERT_FLOAT_EQ(-HUGE_VALF, y0f(0.0f)); 1246 ASSERT_FLOAT_EQ(0.088256963f, y0f(1.0f)); 1247 } 1248 1249 TEST(math, y1) { 1250 ASSERT_DOUBLE_EQ(-HUGE_VAL, y1(0.0)); 1251 ASSERT_DOUBLE_EQ(-0.78121282130028868, y1(1.0)); 1252 } 1253 1254 TEST(math, y1f) { 1255 ASSERT_FLOAT_EQ(-HUGE_VALF, y1f(0.0f)); 1256 ASSERT_FLOAT_EQ(-0.78121281f, y1f(1.0f)); 1257 } 1258 1259 TEST(math, yn) { 1260 ASSERT_DOUBLE_EQ(-HUGE_VAL, yn(4, 0.0)); 1261 ASSERT_DOUBLE_EQ(-33.278423028972114, yn(4, 1.0)); 1262 } 1263 1264 TEST(math, ynf) { 1265 ASSERT_FLOAT_EQ(-HUGE_VALF, ynf(4, 0.0f)); 1266 ASSERT_FLOAT_EQ(-33.278423f, ynf(4, 1.0f)); 1267 } 1268 1269 TEST(math, frexp) { 1270 int exp; 1271 double dr = frexp(1024.0, &exp); 1272 ASSERT_DOUBLE_EQ(1024.0, scalbn(dr, exp)); 1273 } 1274 1275 TEST(math, frexpf) { 1276 int exp; 1277 float fr = frexpf(1024.0f, &exp); 1278 ASSERT_FLOAT_EQ(1024.0f, scalbnf(fr, exp)); 1279 } 1280 1281 TEST(math, frexpl) { 1282 int exp; 1283 long double ldr = frexpl(1024.0L, &exp); 1284 ASSERT_DOUBLE_EQ(1024.0L, scalbnl(ldr, exp)); 1285 } 1286 1287 TEST(math, modf) { 1288 double di; 1289 double df = modf(123.75, &di); 1290 ASSERT_DOUBLE_EQ(123.0, di); 1291 ASSERT_DOUBLE_EQ(0.75, df); 1292 } 1293 1294 TEST(math, modff) { 1295 float fi; 1296 float ff = modff(123.75f, &fi); 1297 ASSERT_FLOAT_EQ(123.0f, fi); 1298 ASSERT_FLOAT_EQ(0.75f, ff); 1299 } 1300 1301 TEST(math, modfl) { 1302 long double ldi; 1303 long double ldf = modfl(123.75L, &ldi); 1304 ASSERT_DOUBLE_EQ(123.0L, ldi); 1305 ASSERT_DOUBLE_EQ(0.75L, ldf); 1306 } 1307 1308 TEST(math, remquo) { 1309 int q; 1310 double d = remquo(13.0, 4.0, &q); 1311 ASSERT_EQ(3, q); 1312 ASSERT_DOUBLE_EQ(1.0, d); 1313 } 1314 1315 TEST(math, remquof) { 1316 int q; 1317 float f = remquof(13.0f, 4.0f, &q); 1318 ASSERT_EQ(3, q); 1319 ASSERT_FLOAT_EQ(1.0, f); 1320 } 1321 1322 TEST(math, remquol) { 1323 int q; 1324 long double ld = remquol(13.0L, 4.0L, &q); 1325 ASSERT_DOUBLE_EQ(3L, q); 1326 ASSERT_DOUBLE_EQ(1.0L, ld); 1327 } 1328 1329 // https://code.google.com/p/android/issues/detail?id=6697 1330 TEST(math, frexpf_public_bug_6697) { 1331 int exp; 1332 float fr = frexpf(14.1f, &exp); 1333 ASSERT_FLOAT_EQ(14.1f, scalbnf(fr, exp)); 1334 } 1335 1336 TEST(math, exp2_STRICT_ALIGN_OpenBSD_bug) { 1337 // OpenBSD/x86's libm had a bug here, but it was already fixed in FreeBSD: 1338 // http://svnweb.FreeBSD.org/base/head/lib/msun/src/math_private.h?revision=240827&view=markup 1339 ASSERT_DOUBLE_EQ(5.0, exp2(log2(5))); 1340 ASSERT_FLOAT_EQ(5.0f, exp2f(log2f(5))); 1341 ASSERT_DOUBLE_EQ(5.0L, exp2l(log2l(5))); 1342 } 1343 1344 TEST(math, nextafterl_OpenBSD_bug) { 1345 // OpenBSD/x86's libm had a bug here. 1346 ASSERT_TRUE(nextafter(1.0, 0.0) - 1.0 < 0.0); 1347 ASSERT_TRUE(nextafterf(1.0f, 0.0f) - 1.0f < 0.0f); 1348 ASSERT_TRUE(nextafterl(1.0L, 0.0L) - 1.0L < 0.0L); 1349 } 1350