1 /* Copyright (C) 1997-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Tests for ISO C99 7.6: Floating-point environment */
36 #include <sys/resource.h>
37 #include <math-tests.h>
40 Since not all architectures might define all exceptions, we define
41 a private set and map accordingly.
44 #define INEXACT_EXC 0x1
45 #define DIVBYZERO_EXC 0x2
46 #define UNDERFLOW_EXC 0x04
47 #define OVERFLOW_EXC 0x08
48 #define INVALID_EXC 0x10
50 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC \
53 static int count_errors;
56 /* Test whether a given exception was raised. */
58 test_single_exception (short int exception,
61 const char *flag_name)
63 if (exception & exc_flag)
65 if (fetestexcept (fe_flag))
66 printf (" Pass: Exception \"%s\" is set\n", flag_name);
69 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
75 if (fetestexcept (fe_flag))
77 printf (" Fail: Exception \"%s\" is set\n", flag_name);
82 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
89 test_exceptions (const char *test_name, short int exception,
92 printf ("Test: %s\n", test_name);
94 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
98 test_single_exception (exception, INVALID_EXC, FE_INVALID,
103 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
107 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
111 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
117 print_rounding (int rounding)
124 printf ("TONEAREST");
139 printf ("TOWARDZERO");
148 test_rounding (const char *test_name, int rounding_mode)
150 int curr_rounding = fegetround ();
152 printf ("Test: %s\n", test_name);
153 if (curr_rounding == rounding_mode)
155 printf (" Pass: Rounding mode is ");
156 print_rounding (curr_rounding);
161 printf (" Fail: Rounding mode is ");
162 print_rounding (curr_rounding);
169 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
172 /* The standard allows the inexact exception to be set together with the
173 underflow and overflow exceptions. So ignore the inexact flag if the
174 others are raised. */
175 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
177 strcpy (str, test_name);
178 strcat (str, ": set flag, with rest not set");
179 feclearexcept (FE_ALL_EXCEPT);
180 feraiseexcept (exception);
181 test_exceptions (str, fe_exc, ignore_inexact);
183 strcpy (str, test_name);
184 strcat (str, ": clear flag, rest also unset");
185 feclearexcept (exception);
186 test_exceptions (str, NO_EXC, ignore_inexact);
188 strcpy (str, test_name);
189 strcat (str, ": set flag, with rest set");
190 feraiseexcept (FE_ALL_EXCEPT ^ exception);
191 feraiseexcept (exception);
192 test_exceptions (str, ALL_EXC, 0);
194 strcpy (str, test_name);
195 strcat (str, ": clear flag, leave rest set");
196 feclearexcept (exception);
197 test_exceptions (str, ALL_EXC ^ fe_exc, 0);
204 /* clear all exceptions and test if all are cleared */
205 feclearexcept (FE_ALL_EXCEPT);
206 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
209 /* Skip further tests here if exceptions not supported. */
210 if (!EXCEPTION_TESTS (float) && FE_ALL_EXCEPT != 0)
212 /* raise all exceptions and test if all are raised */
213 feraiseexcept (FE_ALL_EXCEPT);
214 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
216 feclearexcept (FE_ALL_EXCEPT);
219 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
222 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
225 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
228 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
231 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
236 /* Test that program aborts with no masked interrupts */
238 feenv_nomask_test (const char *flag_name, int fe_exc)
240 # if defined FE_NOMASK_ENV
244 if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT)
245 && fesetenv (FE_NOMASK_ENV) != 0)
247 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
251 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
252 printf (" when feraiseexcept (%s) is called.\n", flag_name);
257 /* Try to avoid dumping core. */
258 struct rlimit core_limit;
259 core_limit.rlim_cur = 0;
260 core_limit.rlim_max = 0;
261 setrlimit (RLIMIT_CORE, &core_limit);
264 fesetenv (FE_NOMASK_ENV);
265 feraiseexcept (fe_exc);
272 printf (" Fail: Could not fork.\n");
276 printf (" `fork' not implemented, test ignored.\n");
279 if (waitpid (pid, &status, 0) != pid)
281 printf (" Fail: waitpid call failed.\n");
284 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
285 printf (" Pass: Process received SIGFPE.\n");
288 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
296 /* Test that program doesn't abort with default environment */
298 feenv_mask_test (const char *flag_name, int fe_exc)
303 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
304 printf (" when feraiseexcept (%s) is called.\n", flag_name);
309 /* Try to avoid dumping core. */
310 struct rlimit core_limit;
311 core_limit.rlim_cur = 0;
312 core_limit.rlim_max = 0;
313 setrlimit (RLIMIT_CORE, &core_limit);
316 fesetenv (FE_DFL_ENV);
317 feraiseexcept (fe_exc);
324 printf (" Fail: Could not fork.\n");
328 printf (" `fork' not implemented, test ignored.\n");
331 if (waitpid (pid, &status, 0) != pid)
333 printf (" Fail: waitpid call failed.\n");
336 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
337 printf (" Pass: Process exited normally.\n");
340 printf (" Fail: Process exited abnormally with status %d.\n",
347 /* Test that program aborts with no masked interrupts */
349 feexcp_nomask_test (const char *flag_name, int fe_exc)
354 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1)
356 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
360 printf ("Test: after feenableexcept (%s) processes will abort\n",
362 printf (" when feraiseexcept (%s) is called.\n", flag_name);
367 /* Try to avoid dumping core. */
368 struct rlimit core_limit;
369 core_limit.rlim_cur = 0;
370 core_limit.rlim_max = 0;
371 setrlimit (RLIMIT_CORE, &core_limit);
374 fedisableexcept (FE_ALL_EXCEPT);
375 feenableexcept (fe_exc);
376 feraiseexcept (fe_exc);
383 printf (" Fail: Could not fork.\n");
387 printf (" `fork' not implemented, test ignored.\n");
390 if (waitpid (pid, &status, 0) != pid)
392 printf (" Fail: waitpid call failed.\n");
395 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
396 printf (" Pass: Process received SIGFPE.\n");
399 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
406 /* Test that program doesn't abort with exception. */
408 feexcp_mask_test (const char *flag_name, int fe_exc)
414 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
416 printf (" when feraiseexcept (%s) is called.\n", flag_name);
421 /* Try to avoid dumping core. */
422 struct rlimit core_limit;
423 core_limit.rlim_cur = 0;
424 core_limit.rlim_max = 0;
425 setrlimit (RLIMIT_CORE, &core_limit);
427 feenableexcept (FE_ALL_EXCEPT);
430 /* The standard allows the inexact exception to be set together with the
431 underflow and overflow exceptions. So add FE_INEXACT to the set of
432 exceptions to be disabled if we will be raising underflow or
435 if (fe_exc & FE_OVERFLOW)
436 exception |= FE_INEXACT;
439 if (fe_exc & FE_UNDERFLOW)
440 exception |= FE_INEXACT;
443 fedisableexcept (exception);
444 feraiseexcept (fe_exc);
451 printf (" Fail: Could not fork.\n");
455 printf (" `fork' not implemented, test ignored.\n");
458 if (waitpid (pid, &status, 0) != pid)
460 printf (" Fail: waitpid call failed.\n");
463 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
464 printf (" Pass: Process exited normally.\n");
467 printf (" Fail: Process exited abnormally with status %d.\n",
475 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
477 feenable_test (const char *flag_name, int fe_exc)
481 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
483 /* First disable all exceptions. */
484 if (fedisableexcept (FE_ALL_EXCEPT) == -1)
486 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
488 /* If this fails, the other tests don't make sense. */
491 excepts = fegetexcept ();
494 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
498 excepts = feenableexcept (fe_exc);
499 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && excepts == -1)
501 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
506 printf ("Test: feenableexcept (%s) failed\n", flag_name);
512 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
517 excepts = fegetexcept ();
518 if (excepts != fe_exc)
520 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
521 flag_name, fe_exc, excepts);
525 /* And now disable the exception again. */
526 excepts = fedisableexcept (fe_exc);
529 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
533 if (excepts != fe_exc)
535 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
536 flag_name, fe_exc, excepts);
540 excepts = fegetexcept ();
543 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
548 /* Now the other way round: Enable all exceptions and disable just this one. */
549 if (feenableexcept (FE_ALL_EXCEPT) == -1)
551 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
553 /* If this fails, the other tests don't make sense. */
557 excepts = fegetexcept ();
558 if (excepts != FE_ALL_EXCEPT)
560 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
561 flag_name, FE_ALL_EXCEPT, excepts);
565 excepts = fedisableexcept (fe_exc);
568 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
572 if (excepts != FE_ALL_EXCEPT)
574 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
579 excepts = fegetexcept ();
580 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
582 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
583 flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
587 /* And now enable the exception again. */
588 excepts = feenableexcept (fe_exc);
591 printf ("Test: feenableexcept (%s) failed\n", flag_name);
595 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
597 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
602 excepts = fegetexcept ();
603 if (excepts != FE_ALL_EXCEPT)
605 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
606 flag_name, FE_ALL_EXCEPT, excepts);
609 feexcp_nomask_test (flag_name, fe_exc);
610 feexcp_mask_test (flag_name, fe_exc);
616 fe_single_test (const char *flag_name, int fe_exc)
618 feenv_nomask_test (flag_name, fe_exc);
619 feenv_mask_test (flag_name, fe_exc);
620 feenable_test (flag_name, fe_exc);
628 /* We might have some exceptions still set. */
629 feclearexcept (FE_ALL_EXCEPT);
632 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
635 fe_single_test ("FE_INVALID", FE_INVALID);
638 fe_single_test ("FE_INEXACT", FE_INEXACT);
641 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
644 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
646 fesetenv (FE_DFL_ENV);
651 feholdexcept_tests (void)
653 fenv_t saved, saved2;
656 feclearexcept (FE_ALL_EXCEPT);
657 fedisableexcept (FE_ALL_EXCEPT);
659 feraiseexcept (FE_DIVBYZERO);
661 if (EXCEPTION_TESTS (float))
662 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
664 res = feholdexcept (&saved);
667 printf ("feholdexcept failed: %d\n", res);
670 #if defined FE_TONEAREST && defined FE_TOWARDZERO
671 res = fesetround (FE_TOWARDZERO);
672 if (res != 0 && ROUNDING_TESTS (float, FE_TOWARDZERO))
674 printf ("fesetround failed: %d\n", res);
678 test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0);
680 feraiseexcept (FE_INVALID);
681 if (EXCEPTION_TESTS (float))
682 test_exceptions ("feholdexcept_tests FE_INVALID test",
685 res = feupdateenv (&saved);
688 printf ("feupdateenv failed: %d\n", res);
691 #if defined FE_TONEAREST && defined FE_TOWARDZERO
693 if (res != FE_TONEAREST)
695 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
699 if (EXCEPTION_TESTS (float))
700 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
701 DIVBYZERO_EXC | INVALID_EXC, 0);
702 feclearexcept (FE_ALL_EXCEPT);
704 feraiseexcept (FE_INVALID);
706 #if defined FE_TONEAREST && defined FE_UPWARD
707 res = fesetround (FE_UPWARD);
708 if (res != 0 && ROUNDING_TESTS (float, FE_UPWARD))
710 printf ("fesetround failed: %d\n", res);
714 res = feholdexcept (&saved2);
717 printf ("feholdexcept failed: %d\n", res);
720 #if defined FE_TONEAREST && defined FE_UPWARD
721 res = fesetround (FE_TONEAREST);
724 printf ("fesetround failed: %d\n", res);
728 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0);
730 feraiseexcept (FE_INEXACT);
731 if (EXCEPTION_TESTS (float))
732 test_exceptions ("feholdexcept_tests FE_INEXACT test",
735 res = feupdateenv (&saved2);
738 printf ("feupdateenv failed: %d\n", res);
741 #if defined FE_TONEAREST && defined FE_UPWARD
743 if (res != FE_UPWARD && ROUNDING_TESTS (float, FE_UPWARD))
745 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
748 fesetround (FE_TONEAREST);
750 if (EXCEPTION_TESTS (float))
751 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
752 INVALID_EXC | INEXACT_EXC, 0);
753 feclearexcept (FE_ALL_EXCEPT);
757 /* IEC 559 and ISO C99 define a default startup environment */
761 test_exceptions ("Initially all exceptions should be cleared",
764 test_rounding ("Rounding direction should be initalized to nearest",
775 feholdexcept_tests ();
779 printf ("\n%d errors occurred.\n", count_errors);
782 printf ("\n All tests passed successfully.\n");