lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1997-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Andreas Jaeger <aj@suse.de> and |
| 4 | Ulrich Drepper <drepper@cygnus.com>, 1997. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | /* Tests for ISO C99 7.6: Floating-point environment */ |
| 21 | |
| 22 | #ifndef _GNU_SOURCE |
| 23 | # define _GNU_SOURCE |
| 24 | #endif |
| 25 | |
| 26 | #include <complex.h> |
| 27 | #include <math.h> |
| 28 | #include <float.h> |
| 29 | #include <fenv.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <math-tests.h> |
| 40 | |
| 41 | /* |
| 42 | Since not all architectures might define all exceptions, we define |
| 43 | a private set and map accordingly. |
| 44 | */ |
| 45 | #define NO_EXC 0 |
| 46 | #define INEXACT_EXC 0x1 |
| 47 | #define DIVBYZERO_EXC 0x2 |
| 48 | #define UNDERFLOW_EXC 0x04 |
| 49 | #define OVERFLOW_EXC 0x08 |
| 50 | #define INVALID_EXC 0x10 |
| 51 | #define ALL_EXC \ |
| 52 | (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \ |
| 53 | INVALID_EXC) |
| 54 | |
| 55 | static int count_errors; |
| 56 | |
| 57 | #if FE_ALL_EXCEPT |
| 58 | /* Test whether a given exception was raised. */ |
| 59 | static void |
| 60 | test_single_exception (short int exception, |
| 61 | short int exc_flag, |
| 62 | fexcept_t fe_flag, |
| 63 | const char *flag_name) |
| 64 | { |
| 65 | if (exception & exc_flag) |
| 66 | { |
| 67 | if (fetestexcept (fe_flag)) |
| 68 | printf (" Pass: Exception \"%s\" is set\n", flag_name); |
| 69 | else |
| 70 | { |
| 71 | printf (" Fail: Exception \"%s\" is not set\n", flag_name); |
| 72 | ++count_errors; |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if (fetestexcept (fe_flag)) |
| 78 | { |
| 79 | printf (" Fail: Exception \"%s\" is set\n", flag_name); |
| 80 | ++count_errors; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | printf (" Pass: Exception \"%s\" is not set\n", flag_name); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | static void |
| 91 | test_exceptions (const char *test_name, short int exception, |
| 92 | int ignore_inexact) |
| 93 | { |
| 94 | printf ("Test: %s\n", test_name); |
| 95 | #ifdef FE_DIVBYZERO |
| 96 | test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO, |
| 97 | "DIVBYZERO"); |
| 98 | #endif |
| 99 | #ifdef FE_INVALID |
| 100 | test_single_exception (exception, INVALID_EXC, FE_INVALID, |
| 101 | "INVALID"); |
| 102 | #endif |
| 103 | #ifdef FE_INEXACT |
| 104 | if (!ignore_inexact) |
| 105 | test_single_exception (exception, INEXACT_EXC, FE_INEXACT, |
| 106 | "INEXACT"); |
| 107 | #endif |
| 108 | #ifdef FE_UNDERFLOW |
| 109 | test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW, |
| 110 | "UNDERFLOW"); |
| 111 | #endif |
| 112 | #ifdef FE_OVERFLOW |
| 113 | test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW, |
| 114 | "OVERFLOW"); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | print_rounding (int rounding) |
| 120 | { |
| 121 | |
| 122 | switch (rounding) |
| 123 | { |
| 124 | #ifdef FE_TONEAREST |
| 125 | case FE_TONEAREST: |
| 126 | printf ("TONEAREST"); |
| 127 | break; |
| 128 | #endif |
| 129 | #ifdef FE_UPWARD |
| 130 | case FE_UPWARD: |
| 131 | printf ("UPWARD"); |
| 132 | break; |
| 133 | #endif |
| 134 | #ifdef FE_DOWNWARD |
| 135 | case FE_DOWNWARD: |
| 136 | printf ("DOWNWARD"); |
| 137 | break; |
| 138 | #endif |
| 139 | #ifdef FE_TOWARDZERO |
| 140 | case FE_TOWARDZERO: |
| 141 | printf ("TOWARDZERO"); |
| 142 | break; |
| 143 | #endif |
| 144 | } |
| 145 | printf (".\n"); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
| 150 | test_rounding (const char *test_name, int rounding_mode) |
| 151 | { |
| 152 | int curr_rounding = fegetround (); |
| 153 | |
| 154 | printf ("Test: %s\n", test_name); |
| 155 | if (curr_rounding == rounding_mode) |
| 156 | { |
| 157 | printf (" Pass: Rounding mode is "); |
| 158 | print_rounding (curr_rounding); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | ++count_errors; |
| 163 | printf (" Fail: Rounding mode is "); |
| 164 | print_rounding (curr_rounding); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | #if FE_ALL_EXCEPT |
| 170 | static void |
| 171 | set_single_exc (const char *test_name, int fe_exc, fexcept_t exception) |
| 172 | { |
| 173 | char str[200]; |
| 174 | /* The standard allows the inexact exception to be set together with the |
| 175 | underflow and overflow exceptions. So ignore the inexact flag if the |
| 176 | others are raised. */ |
| 177 | int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0; |
| 178 | |
| 179 | strcpy (str, test_name); |
| 180 | strcat (str, ": set flag, with rest not set"); |
| 181 | feclearexcept (FE_ALL_EXCEPT); |
| 182 | feraiseexcept (exception); |
| 183 | test_exceptions (str, fe_exc, ignore_inexact); |
| 184 | |
| 185 | strcpy (str, test_name); |
| 186 | strcat (str, ": clear flag, rest also unset"); |
| 187 | feclearexcept (exception); |
| 188 | test_exceptions (str, NO_EXC, ignore_inexact); |
| 189 | |
| 190 | strcpy (str, test_name); |
| 191 | strcat (str, ": set flag, with rest set"); |
| 192 | feraiseexcept (FE_ALL_EXCEPT ^ exception); |
| 193 | feraiseexcept (exception); |
| 194 | test_exceptions (str, ALL_EXC, 0); |
| 195 | |
| 196 | strcpy (str, test_name); |
| 197 | strcat (str, ": clear flag, leave rest set"); |
| 198 | feclearexcept (exception); |
| 199 | test_exceptions (str, ALL_EXC ^ fe_exc, 0); |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | static void |
| 204 | fe_tests (void) |
| 205 | { |
| 206 | /* clear all exceptions and test if all are cleared */ |
| 207 | feclearexcept (FE_ALL_EXCEPT); |
| 208 | test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions", |
| 209 | NO_EXC, 0); |
| 210 | |
| 211 | /* raise all exceptions and test if all are raised */ |
| 212 | feraiseexcept (FE_ALL_EXCEPT); |
| 213 | test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions", |
| 214 | ALL_EXC, 0); |
| 215 | feclearexcept (FE_ALL_EXCEPT); |
| 216 | |
| 217 | #ifdef FE_DIVBYZERO |
| 218 | set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO); |
| 219 | #endif |
| 220 | #ifdef FE_INVALID |
| 221 | set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID); |
| 222 | #endif |
| 223 | #ifdef FE_INEXACT |
| 224 | set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT); |
| 225 | #endif |
| 226 | #ifdef FE_UNDERFLOW |
| 227 | set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW); |
| 228 | #endif |
| 229 | #ifdef FE_OVERFLOW |
| 230 | set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW); |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | #if FE_ALL_EXCEPT |
| 235 | /* Test that program aborts with no masked interrupts */ |
| 236 | static void |
| 237 | feenv_nomask_test (const char *flag_name, int fe_exc) |
| 238 | { |
| 239 | # if defined FE_NOMASK_ENV |
| 240 | int status; |
| 241 | pid_t pid; |
| 242 | |
| 243 | if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT) |
| 244 | && fesetenv (FE_NOMASK_ENV) != 0) |
| 245 | { |
| 246 | printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n"); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n"); |
| 251 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
| 252 | pid = fork (); |
| 253 | if (pid == 0) |
| 254 | { |
| 255 | # ifdef RLIMIT_CORE |
| 256 | /* Try to avoid dumping core. */ |
| 257 | struct rlimit core_limit; |
| 258 | core_limit.rlim_cur = 0; |
| 259 | core_limit.rlim_max = 0; |
| 260 | setrlimit (RLIMIT_CORE, &core_limit); |
| 261 | # endif |
| 262 | |
| 263 | fesetenv (FE_NOMASK_ENV); |
| 264 | feraiseexcept (fe_exc); |
| 265 | exit (2); |
| 266 | } |
| 267 | else if (pid < 0) |
| 268 | { |
| 269 | if (errno != ENOSYS) |
| 270 | { |
| 271 | printf (" Fail: Could not fork.\n"); |
| 272 | ++count_errors; |
| 273 | } |
| 274 | else |
| 275 | printf (" `fork' not implemented, test ignored.\n"); |
| 276 | } |
| 277 | else { |
| 278 | if (waitpid (pid, &status, 0) != pid) |
| 279 | { |
| 280 | printf (" Fail: waitpid call failed.\n"); |
| 281 | ++count_errors; |
| 282 | } |
| 283 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE) |
| 284 | printf (" Pass: Process received SIGFPE.\n"); |
| 285 | else |
| 286 | { |
| 287 | printf (" Fail: Process didn't receive signal and exited with status %d.\n", |
| 288 | status); |
| 289 | ++count_errors; |
| 290 | } |
| 291 | } |
| 292 | # endif |
| 293 | } |
| 294 | |
| 295 | /* Test that program doesn't abort with default environment */ |
| 296 | static void |
| 297 | feenv_mask_test (const char *flag_name, int fe_exc) |
| 298 | { |
| 299 | int status; |
| 300 | pid_t pid; |
| 301 | |
| 302 | printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n"); |
| 303 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
| 304 | pid = fork (); |
| 305 | if (pid == 0) |
| 306 | { |
| 307 | #ifdef RLIMIT_CORE |
| 308 | /* Try to avoid dumping core. */ |
| 309 | struct rlimit core_limit; |
| 310 | core_limit.rlim_cur = 0; |
| 311 | core_limit.rlim_max = 0; |
| 312 | setrlimit (RLIMIT_CORE, &core_limit); |
| 313 | #endif |
| 314 | |
| 315 | fesetenv (FE_DFL_ENV); |
| 316 | feraiseexcept (fe_exc); |
| 317 | exit (2); |
| 318 | } |
| 319 | else if (pid < 0) |
| 320 | { |
| 321 | if (errno != ENOSYS) |
| 322 | { |
| 323 | printf (" Fail: Could not fork.\n"); |
| 324 | ++count_errors; |
| 325 | } |
| 326 | else |
| 327 | printf (" `fork' not implemented, test ignored.\n"); |
| 328 | } |
| 329 | else { |
| 330 | if (waitpid (pid, &status, 0) != pid) |
| 331 | { |
| 332 | printf (" Fail: waitpid call failed.\n"); |
| 333 | ++count_errors; |
| 334 | } |
| 335 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2) |
| 336 | printf (" Pass: Process exited normally.\n"); |
| 337 | else |
| 338 | { |
| 339 | printf (" Fail: Process exited abnormally with status %d.\n", |
| 340 | status); |
| 341 | ++count_errors; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* Test that program aborts with no masked interrupts */ |
| 347 | static void |
| 348 | feexcp_nomask_test (const char *flag_name, int fe_exc) |
| 349 | { |
| 350 | int status; |
| 351 | pid_t pid; |
| 352 | |
| 353 | if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1) |
| 354 | { |
| 355 | printf ("Test: not testing feenableexcept, it isn't implemented.\n"); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | printf ("Test: after feenableexcept (%s) processes will abort\n", |
| 360 | flag_name); |
| 361 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
| 362 | pid = fork (); |
| 363 | if (pid == 0) |
| 364 | { |
| 365 | #ifdef RLIMIT_CORE |
| 366 | /* Try to avoid dumping core. */ |
| 367 | struct rlimit core_limit; |
| 368 | core_limit.rlim_cur = 0; |
| 369 | core_limit.rlim_max = 0; |
| 370 | setrlimit (RLIMIT_CORE, &core_limit); |
| 371 | #endif |
| 372 | |
| 373 | fedisableexcept (FE_ALL_EXCEPT); |
| 374 | feenableexcept (fe_exc); |
| 375 | feraiseexcept (fe_exc); |
| 376 | exit (2); |
| 377 | } |
| 378 | else if (pid < 0) |
| 379 | { |
| 380 | if (errno != ENOSYS) |
| 381 | { |
| 382 | printf (" Fail: Could not fork.\n"); |
| 383 | ++count_errors; |
| 384 | } |
| 385 | else |
| 386 | printf (" `fork' not implemented, test ignored.\n"); |
| 387 | } |
| 388 | else { |
| 389 | if (waitpid (pid, &status, 0) != pid) |
| 390 | { |
| 391 | printf (" Fail: waitpid call failed.\n"); |
| 392 | ++count_errors; |
| 393 | } |
| 394 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE) |
| 395 | printf (" Pass: Process received SIGFPE.\n"); |
| 396 | else |
| 397 | { |
| 398 | printf (" Fail: Process didn't receive signal and exited with status %d.\n", |
| 399 | status); |
| 400 | ++count_errors; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* Test that program doesn't abort with exception. */ |
| 406 | static void |
| 407 | feexcp_mask_test (const char *flag_name, int fe_exc) |
| 408 | { |
| 409 | int status; |
| 410 | int exception; |
| 411 | pid_t pid; |
| 412 | |
| 413 | printf ("Test: after fedisableexcept (%s) processes will not abort\n", |
| 414 | flag_name); |
| 415 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
| 416 | pid = fork (); |
| 417 | if (pid == 0) |
| 418 | { |
| 419 | #ifdef RLIMIT_CORE |
| 420 | /* Try to avoid dumping core. */ |
| 421 | struct rlimit core_limit; |
| 422 | core_limit.rlim_cur = 0; |
| 423 | core_limit.rlim_max = 0; |
| 424 | setrlimit (RLIMIT_CORE, &core_limit); |
| 425 | #endif |
| 426 | feenableexcept (FE_ALL_EXCEPT); |
| 427 | exception = fe_exc; |
| 428 | #ifdef FE_INEXACT |
| 429 | /* The standard allows the inexact exception to be set together with the |
| 430 | underflow and overflow exceptions. So add FE_INEXACT to the set of |
| 431 | exceptions to be disabled if we will be raising underflow or |
| 432 | overflow. */ |
| 433 | # ifdef FE_OVERFLOW |
| 434 | if (fe_exc & FE_OVERFLOW) |
| 435 | exception |= FE_INEXACT; |
| 436 | # endif |
| 437 | # ifdef FE_UNDERFLOW |
| 438 | if (fe_exc & FE_UNDERFLOW) |
| 439 | exception |= FE_INEXACT; |
| 440 | # endif |
| 441 | #endif |
| 442 | fedisableexcept (exception); |
| 443 | feraiseexcept (fe_exc); |
| 444 | exit (2); |
| 445 | } |
| 446 | else if (pid < 0) |
| 447 | { |
| 448 | if (errno != ENOSYS) |
| 449 | { |
| 450 | printf (" Fail: Could not fork.\n"); |
| 451 | ++count_errors; |
| 452 | } |
| 453 | else |
| 454 | printf (" `fork' not implemented, test ignored.\n"); |
| 455 | } |
| 456 | else { |
| 457 | if (waitpid (pid, &status, 0) != pid) |
| 458 | { |
| 459 | printf (" Fail: waitpid call failed.\n"); |
| 460 | ++count_errors; |
| 461 | } |
| 462 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2) |
| 463 | printf (" Pass: Process exited normally.\n"); |
| 464 | else |
| 465 | { |
| 466 | printf (" Fail: Process exited abnormally with status %d.\n", |
| 467 | status); |
| 468 | ++count_errors; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* Tests for feenableexcept/fedisableexcept/fegetexcept. */ |
| 475 | static void |
| 476 | feenable_test (const char *flag_name, int fe_exc) |
| 477 | { |
| 478 | int excepts; |
| 479 | |
| 480 | printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name); |
| 481 | |
| 482 | /* First disable all exceptions. */ |
| 483 | if (fedisableexcept (FE_ALL_EXCEPT) == -1) |
| 484 | { |
| 485 | printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n"); |
| 486 | ++count_errors; |
| 487 | /* If this fails, the other tests don't make sense. */ |
| 488 | return; |
| 489 | } |
| 490 | excepts = fegetexcept (); |
| 491 | if (excepts != 0) |
| 492 | { |
| 493 | printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n", |
| 494 | flag_name, excepts); |
| 495 | ++count_errors; |
| 496 | } |
| 497 | excepts = feenableexcept (fe_exc); |
| 498 | if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && excepts == -1) |
| 499 | { |
| 500 | printf ("Test: not testing feenableexcept, it isn't implemented.\n"); |
| 501 | return; |
| 502 | } |
| 503 | if (excepts == -1) |
| 504 | { |
| 505 | printf ("Test: feenableexcept (%s) failed\n", flag_name); |
| 506 | ++count_errors; |
| 507 | return; |
| 508 | } |
| 509 | if (excepts != 0) |
| 510 | { |
| 511 | printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n", |
| 512 | flag_name, excepts); |
| 513 | ++count_errors; |
| 514 | } |
| 515 | |
| 516 | excepts = fegetexcept (); |
| 517 | if (excepts != fe_exc) |
| 518 | { |
| 519 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", |
| 520 | flag_name, fe_exc, excepts); |
| 521 | ++count_errors; |
| 522 | } |
| 523 | |
| 524 | /* And now disable the exception again. */ |
| 525 | excepts = fedisableexcept (fe_exc); |
| 526 | if (excepts == -1) |
| 527 | { |
| 528 | printf ("Test: fedisableexcept (%s) failed\n", flag_name); |
| 529 | ++count_errors; |
| 530 | return; |
| 531 | } |
| 532 | if (excepts != fe_exc) |
| 533 | { |
| 534 | printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n", |
| 535 | flag_name, fe_exc, excepts); |
| 536 | ++count_errors; |
| 537 | } |
| 538 | |
| 539 | excepts = fegetexcept (); |
| 540 | if (excepts != 0) |
| 541 | { |
| 542 | printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n", |
| 543 | flag_name, excepts); |
| 544 | ++count_errors; |
| 545 | } |
| 546 | |
| 547 | /* Now the other way round: Enable all exceptions and disable just this one. */ |
| 548 | if (feenableexcept (FE_ALL_EXCEPT) == -1) |
| 549 | { |
| 550 | printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n"); |
| 551 | ++count_errors; |
| 552 | /* If this fails, the other tests don't make sense. */ |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | excepts = fegetexcept (); |
| 557 | if (excepts != FE_ALL_EXCEPT) |
| 558 | { |
| 559 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", |
| 560 | flag_name, FE_ALL_EXCEPT, excepts); |
| 561 | ++count_errors; |
| 562 | } |
| 563 | |
| 564 | excepts = fedisableexcept (fe_exc); |
| 565 | if (excepts == -1) |
| 566 | { |
| 567 | printf ("Test: fedisableexcept (%s) failed\n", flag_name); |
| 568 | ++count_errors; |
| 569 | return; |
| 570 | } |
| 571 | if (excepts != FE_ALL_EXCEPT) |
| 572 | { |
| 573 | printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n", |
| 574 | flag_name, excepts); |
| 575 | ++count_errors; |
| 576 | } |
| 577 | |
| 578 | excepts = fegetexcept (); |
| 579 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc)) |
| 580 | { |
| 581 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", |
| 582 | flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts); |
| 583 | ++count_errors; |
| 584 | } |
| 585 | |
| 586 | /* And now enable the exception again. */ |
| 587 | excepts = feenableexcept (fe_exc); |
| 588 | if (excepts == -1) |
| 589 | { |
| 590 | printf ("Test: feenableexcept (%s) failed\n", flag_name); |
| 591 | ++count_errors; |
| 592 | return; |
| 593 | } |
| 594 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc)) |
| 595 | { |
| 596 | printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n", |
| 597 | flag_name, excepts); |
| 598 | ++count_errors; |
| 599 | } |
| 600 | |
| 601 | excepts = fegetexcept (); |
| 602 | if (excepts != FE_ALL_EXCEPT) |
| 603 | { |
| 604 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", |
| 605 | flag_name, FE_ALL_EXCEPT, excepts); |
| 606 | ++count_errors; |
| 607 | } |
| 608 | feexcp_nomask_test (flag_name, fe_exc); |
| 609 | feexcp_mask_test (flag_name, fe_exc); |
| 610 | |
| 611 | } |
| 612 | |
| 613 | |
| 614 | static void |
| 615 | fe_single_test (const char *flag_name, int fe_exc) |
| 616 | { |
| 617 | feenv_nomask_test (flag_name, fe_exc); |
| 618 | feenv_mask_test (flag_name, fe_exc); |
| 619 | feenable_test (flag_name, fe_exc); |
| 620 | } |
| 621 | #endif |
| 622 | |
| 623 | |
| 624 | static void |
| 625 | feenv_tests (void) |
| 626 | { |
| 627 | /* We might have some exceptions still set. */ |
| 628 | feclearexcept (FE_ALL_EXCEPT); |
| 629 | |
| 630 | #ifdef FE_DIVBYZERO |
| 631 | fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO); |
| 632 | #endif |
| 633 | #ifdef FE_INVALID |
| 634 | fe_single_test ("FE_INVALID", FE_INVALID); |
| 635 | #endif |
| 636 | #ifdef FE_INEXACT |
| 637 | fe_single_test ("FE_INEXACT", FE_INEXACT); |
| 638 | #endif |
| 639 | #ifdef FE_UNDERFLOW |
| 640 | fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW); |
| 641 | #endif |
| 642 | #ifdef FE_OVERFLOW |
| 643 | fe_single_test ("FE_OVERFLOW", FE_OVERFLOW); |
| 644 | #endif |
| 645 | fesetenv (FE_DFL_ENV); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | static void |
| 650 | feholdexcept_tests (void) |
| 651 | { |
| 652 | fenv_t saved, saved2; |
| 653 | int res; |
| 654 | |
| 655 | feclearexcept (FE_ALL_EXCEPT); |
| 656 | fedisableexcept (FE_ALL_EXCEPT); |
| 657 | #ifdef FE_DIVBYZERO |
| 658 | feraiseexcept (FE_DIVBYZERO); |
| 659 | #endif |
| 660 | test_exceptions ("feholdexcept_tests FE_DIVBYZERO test", |
| 661 | DIVBYZERO_EXC, 0); |
| 662 | res = feholdexcept (&saved); |
| 663 | if (res != 0) |
| 664 | { |
| 665 | printf ("feholdexcept failed: %d\n", res); |
| 666 | ++count_errors; |
| 667 | } |
| 668 | #if defined FE_TONEAREST && defined FE_TOWARDZERO |
| 669 | res = fesetround (FE_TOWARDZERO); |
| 670 | if (res != 0) |
| 671 | { |
| 672 | printf ("fesetround failed: %d\n", res); |
| 673 | ++count_errors; |
| 674 | } |
| 675 | #endif |
| 676 | test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0); |
| 677 | #ifdef FE_INVALID |
| 678 | feraiseexcept (FE_INVALID); |
| 679 | test_exceptions ("feholdexcept_tests FE_INVALID test", |
| 680 | INVALID_EXC, 0); |
| 681 | #endif |
| 682 | res = feupdateenv (&saved); |
| 683 | if (res != 0) |
| 684 | { |
| 685 | printf ("feupdateenv failed: %d\n", res); |
| 686 | ++count_errors; |
| 687 | } |
| 688 | #if defined FE_TONEAREST && defined FE_TOWARDZERO |
| 689 | res = fegetround (); |
| 690 | if (res != FE_TONEAREST) |
| 691 | { |
| 692 | printf ("feupdateenv didn't restore rounding mode: %d\n", res); |
| 693 | ++count_errors; |
| 694 | } |
| 695 | #endif |
| 696 | test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test", |
| 697 | DIVBYZERO_EXC | INVALID_EXC, 0); |
| 698 | feclearexcept (FE_ALL_EXCEPT); |
| 699 | #ifdef FE_INVALID |
| 700 | feraiseexcept (FE_INVALID); |
| 701 | #endif |
| 702 | #if defined FE_TONEAREST && defined FE_UPWARD |
| 703 | res = fesetround (FE_UPWARD); |
| 704 | if (res != 0) |
| 705 | { |
| 706 | printf ("fesetround failed: %d\n", res); |
| 707 | ++count_errors; |
| 708 | } |
| 709 | #endif |
| 710 | res = feholdexcept (&saved2); |
| 711 | if (res != 0) |
| 712 | { |
| 713 | printf ("feholdexcept failed: %d\n", res); |
| 714 | ++count_errors; |
| 715 | } |
| 716 | #if defined FE_TONEAREST && defined FE_UPWARD |
| 717 | res = fesetround (FE_TONEAREST); |
| 718 | if (res != 0) |
| 719 | { |
| 720 | printf ("fesetround failed: %d\n", res); |
| 721 | ++count_errors; |
| 722 | } |
| 723 | #endif |
| 724 | test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0); |
| 725 | #ifdef FE_INEXACT |
| 726 | feraiseexcept (FE_INEXACT); |
| 727 | test_exceptions ("feholdexcept_tests FE_INEXACT test", |
| 728 | INEXACT_EXC, 0); |
| 729 | #endif |
| 730 | res = feupdateenv (&saved2); |
| 731 | if (res != 0) |
| 732 | { |
| 733 | printf ("feupdateenv failed: %d\n", res); |
| 734 | ++count_errors; |
| 735 | } |
| 736 | #if defined FE_TONEAREST && defined FE_UPWARD |
| 737 | res = fegetround (); |
| 738 | if (res != FE_UPWARD) |
| 739 | { |
| 740 | printf ("feupdateenv didn't restore rounding mode: %d\n", res); |
| 741 | ++count_errors; |
| 742 | } |
| 743 | fesetround (FE_TONEAREST); |
| 744 | #endif |
| 745 | test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test", |
| 746 | INVALID_EXC | INEXACT_EXC, 0); |
| 747 | feclearexcept (FE_ALL_EXCEPT); |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /* IEC 559 and ISO C99 define a default startup environment */ |
| 752 | static void |
| 753 | initial_tests (void) |
| 754 | { |
| 755 | test_exceptions ("Initially all exceptions should be cleared", |
| 756 | NO_EXC, 0); |
| 757 | #ifdef FE_TONEAREST |
| 758 | test_rounding ("Rounding direction should be initalized to nearest", |
| 759 | FE_TONEAREST); |
| 760 | #endif |
| 761 | } |
| 762 | |
| 763 | int |
| 764 | main (void) |
| 765 | { |
| 766 | initial_tests (); |
| 767 | fe_tests (); |
| 768 | feenv_tests (); |
| 769 | feholdexcept_tests (); |
| 770 | |
| 771 | if (count_errors) |
| 772 | { |
| 773 | printf ("\n%d errors occurred.\n", count_errors); |
| 774 | exit (1); |
| 775 | } |
| 776 | printf ("\n All tests passed successfully.\n"); |
| 777 | return 0; |
| 778 | } |