1 /* Copyright (C) 1997-2013 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.
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.
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.
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/>. */
20 /* Tests for ISO C99 7.6: Floating-point environment */
38 #include <sys/resource.h>
41 Since not all architectures might define all exceptions, we define
42 a private set and map accordingly.
45 #define INEXACT_EXC 0x1
46 #define DIVBYZERO_EXC 0x2
47 #define UNDERFLOW_EXC 0x04
48 #define OVERFLOW_EXC 0x08
49 #define INVALID_EXC 0x10
51 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
54 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);
88 test_exceptions (const char *test_name, short int exception,
91 printf ("Test: %s\n", test_name);
93 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
97 test_single_exception (exception, INVALID_EXC, FE_INVALID,
102 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
106 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
110 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
116 print_rounding (int rounding)
123 printf ("TONEAREST");
138 printf ("TOWARDZERO");
147 test_rounding (const char *test_name, int rounding_mode)
149 int curr_rounding = fegetround ();
151 printf ("Test: %s\n", test_name);
152 if (curr_rounding == rounding_mode)
154 printf (" Pass: Rounding mode is ");
155 print_rounding (curr_rounding);
160 printf (" Fail: Rounding mode is ");
161 print_rounding (curr_rounding);
167 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
170 /* The standard allows the inexact exception to be set together with the
171 underflow and overflow exceptions. So ignore the inexact flag if the
172 others are raised. */
173 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
175 strcpy (str, test_name);
176 strcat (str, ": set flag, with rest not set");
177 feclearexcept (FE_ALL_EXCEPT);
178 feraiseexcept (exception);
179 test_exceptions (str, fe_exc, ignore_inexact);
181 strcpy (str, test_name);
182 strcat (str, ": clear flag, rest also unset");
183 feclearexcept (exception);
184 test_exceptions (str, NO_EXC, ignore_inexact);
186 strcpy (str, test_name);
187 strcat (str, ": set flag, with rest set");
188 feraiseexcept (FE_ALL_EXCEPT ^ exception);
189 feraiseexcept (exception);
190 test_exceptions (str, ALL_EXC, 0);
192 strcpy (str, test_name);
193 strcat (str, ": clear flag, leave rest set");
194 feclearexcept (exception);
195 test_exceptions (str, ALL_EXC ^ fe_exc, 0);
201 /* clear all exceptions and test if all are cleared */
202 feclearexcept (FE_ALL_EXCEPT);
203 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
206 /* raise all exceptions and test if all are raised */
207 feraiseexcept (FE_ALL_EXCEPT);
208 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
210 feclearexcept (FE_ALL_EXCEPT);
213 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
216 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
219 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
222 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
225 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
229 /* Test that program aborts with no masked interrupts */
231 feenv_nomask_test (const char *flag_name, int fe_exc)
233 #if defined FE_NOMASK_ENV
240 fesetenv (FE_NOMASK_ENV);
243 if (status == ENOSYS)
245 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
249 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
250 printf (" when feraiseexcept (%s) is called.\n", flag_name);
255 /* Try to avoid dumping core. */
256 struct rlimit core_limit;
257 core_limit.rlim_cur = 0;
258 core_limit.rlim_max = 0;
259 setrlimit (RLIMIT_CORE, &core_limit);
262 fesetenv (FE_NOMASK_ENV);
263 feraiseexcept (fe_exc);
270 printf (" Fail: Could not fork.\n");
274 printf (" `fork' not implemented, test ignored.\n");
277 if (waitpid (pid, &status, 0) != pid)
279 printf (" Fail: waitpid call failed.\n");
282 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
283 printf (" Pass: Process received SIGFPE.\n");
286 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
294 /* Test that program doesn't abort with default environment */
296 feenv_mask_test (const char *flag_name, int fe_exc)
301 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
302 printf (" when feraiseexcept (%s) is called.\n", flag_name);
307 /* Try to avoid dumping core. */
308 struct rlimit core_limit;
309 core_limit.rlim_cur = 0;
310 core_limit.rlim_max = 0;
311 setrlimit (RLIMIT_CORE, &core_limit);
314 fesetenv (FE_DFL_ENV);
315 feraiseexcept (fe_exc);
322 printf (" Fail: Could not fork.\n");
326 printf (" `fork' not implemented, test ignored.\n");
329 if (waitpid (pid, &status, 0) != pid)
331 printf (" Fail: waitpid call failed.\n");
334 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
335 printf (" Pass: Process exited normally.\n");
338 printf (" Fail: Process exited abnormally with status %d.\n",
345 /* Test that program aborts with no masked interrupts */
347 feexcp_nomask_test (const char *flag_name, int fe_exc)
352 printf ("Test: after fedisableexcept (%s) processes will abort\n",
354 printf (" when feraiseexcept (%s) is called.\n", flag_name);
359 /* Try to avoid dumping core. */
360 struct rlimit core_limit;
361 core_limit.rlim_cur = 0;
362 core_limit.rlim_max = 0;
363 setrlimit (RLIMIT_CORE, &core_limit);
366 fedisableexcept (FE_ALL_EXCEPT);
367 feenableexcept (fe_exc);
368 feraiseexcept (fe_exc);
375 printf (" Fail: Could not fork.\n");
379 printf (" `fork' not implemented, test ignored.\n");
382 if (waitpid (pid, &status, 0) != pid)
384 printf (" Fail: waitpid call failed.\n");
387 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
388 printf (" Pass: Process received SIGFPE.\n");
391 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
398 /* Test that program doesn't abort with exception. */
400 feexcp_mask_test (const char *flag_name, int fe_exc)
406 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
408 printf (" when feraiseexcept (%s) is called.\n", flag_name);
413 /* Try to avoid dumping core. */
414 struct rlimit core_limit;
415 core_limit.rlim_cur = 0;
416 core_limit.rlim_max = 0;
417 setrlimit (RLIMIT_CORE, &core_limit);
419 feenableexcept (FE_ALL_EXCEPT);
422 /* The standard allows the inexact exception to be set together with the
423 underflow and overflow exceptions. So add FE_INEXACT to the set of
424 exceptions to be disabled if we will be raising underflow or
427 if (fe_exc & FE_OVERFLOW)
428 exception |= FE_INEXACT;
431 if (fe_exc & FE_UNDERFLOW)
432 exception |= FE_INEXACT;
435 fedisableexcept (exception);
436 feraiseexcept (fe_exc);
443 printf (" Fail: Could not fork.\n");
447 printf (" `fork' not implemented, test ignored.\n");
450 if (waitpid (pid, &status, 0) != pid)
452 printf (" Fail: waitpid call failed.\n");
455 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
456 printf (" Pass: Process exited normally.\n");
459 printf (" Fail: Process exited abnormally with status %d.\n",
467 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
469 feenable_test (const char *flag_name, int fe_exc)
474 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
476 /* First disable all exceptions. */
477 if (fedisableexcept (FE_ALL_EXCEPT) == -1)
479 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
481 /* If this fails, the other tests don't make sense. */
484 excepts = fegetexcept ();
487 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
492 excepts = feenableexcept (fe_exc);
495 printf ("Test: feenableexcept (%s) failed\n", flag_name);
501 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
506 excepts = fegetexcept ();
507 if (excepts != fe_exc)
509 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
510 flag_name, fe_exc, excepts);
514 /* And now disable the exception again. */
515 excepts = fedisableexcept (fe_exc);
518 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
522 if (excepts != fe_exc)
524 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
525 flag_name, fe_exc, excepts);
529 excepts = fegetexcept ();
532 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
537 /* Now the other way round: Enable all exceptions and disable just this one. */
538 if (feenableexcept (FE_ALL_EXCEPT) == -1)
540 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
542 /* If this fails, the other tests don't make sense. */
546 excepts = fegetexcept ();
547 if (excepts != FE_ALL_EXCEPT)
549 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
550 flag_name, FE_ALL_EXCEPT, excepts);
554 excepts = fedisableexcept (fe_exc);
557 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
561 if (excepts != FE_ALL_EXCEPT)
563 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
568 excepts = fegetexcept ();
569 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
571 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
572 flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
576 /* And now enable the exception again. */
577 excepts = feenableexcept (fe_exc);
580 printf ("Test: feenableexcept (%s) failed\n", flag_name);
584 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
586 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
591 excepts = fegetexcept ();
592 if (excepts != FE_ALL_EXCEPT)
594 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
595 flag_name, FE_ALL_EXCEPT, excepts);
598 feexcp_nomask_test (flag_name, fe_exc);
599 feexcp_mask_test (flag_name, fe_exc);
605 fe_single_test (const char *flag_name, int fe_exc)
607 feenv_nomask_test (flag_name, fe_exc);
608 feenv_mask_test (flag_name, fe_exc);
609 feenable_test (flag_name, fe_exc);
616 /* We might have some exceptions still set. */
617 feclearexcept (FE_ALL_EXCEPT);
620 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
623 fe_single_test ("FE_INVALID", FE_INVALID);
626 fe_single_test ("FE_INEXACT", FE_INEXACT);
629 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
632 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
634 fesetenv (FE_DFL_ENV);
639 feholdexcept_tests (void)
641 fenv_t saved, saved2;
644 feclearexcept (FE_ALL_EXCEPT);
645 fedisableexcept (FE_ALL_EXCEPT);
647 feraiseexcept (FE_DIVBYZERO);
649 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
651 res = feholdexcept (&saved);
654 printf ("feholdexcept failed: %d\n", res);
657 #if defined FE_TONEAREST && defined FE_TOWARDZERO
658 res = fesetround (FE_TOWARDZERO);
661 printf ("fesetround failed: %d\n", res);
665 test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0);
667 feraiseexcept (FE_INVALID);
668 test_exceptions ("feholdexcept_tests FE_INVALID test",
671 res = feupdateenv (&saved);
674 printf ("feupdateenv failed: %d\n", res);
677 #if defined FE_TONEAREST && defined FE_TOWARDZERO
679 if (res != FE_TONEAREST)
681 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
685 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
686 DIVBYZERO_EXC | INVALID_EXC, 0);
687 feclearexcept (FE_ALL_EXCEPT);
689 feraiseexcept (FE_INVALID);
691 #if defined FE_TONEAREST && defined FE_UPWARD
692 res = fesetround (FE_UPWARD);
695 printf ("fesetround failed: %d\n", res);
699 res = feholdexcept (&saved2);
702 printf ("feholdexcept failed: %d\n", res);
705 #if defined FE_TONEAREST && defined FE_UPWARD
706 res = fesetround (FE_TONEAREST);
709 printf ("fesetround failed: %d\n", res);
713 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0);
715 feraiseexcept (FE_INEXACT);
716 test_exceptions ("feholdexcept_tests FE_INEXACT test",
719 res = feupdateenv (&saved2);
722 printf ("feupdateenv failed: %d\n", res);
725 #if defined FE_TONEAREST && defined FE_UPWARD
727 if (res != FE_UPWARD)
729 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
732 fesetround (FE_TONEAREST);
734 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
735 INVALID_EXC | INEXACT_EXC, 0);
736 feclearexcept (FE_ALL_EXCEPT);
740 /* IEC 559 and ISO C99 define a default startup environment */
744 test_exceptions ("Initially all exceptions should be cleared",
747 test_rounding ("Rounding direction should be initalized to nearest",
758 feholdexcept_tests ();
762 printf ("\n%d errors occurred.\n", count_errors);
765 printf ("\n All tests passed successfully.\n");