1 /* Copyright (C) 1997-2014 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>
39 #include <math-tests.h>
42 Since not all architectures might define all exceptions, we define
43 a private set and map accordingly.
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
52 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
55 static int count_errors;
57 /* Test whether a given exception was raised. */
59 test_single_exception (short int exception,
62 const char *flag_name)
64 if (exception & exc_flag)
66 if (fetestexcept (fe_flag))
67 printf (" Pass: Exception \"%s\" is set\n", flag_name);
70 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
76 if (fetestexcept (fe_flag))
78 printf (" Fail: Exception \"%s\" is set\n", flag_name);
83 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);
168 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
171 /* The standard allows the inexact exception to be set together with the
172 underflow and overflow exceptions. So ignore the inexact flag if the
173 others are raised. */
174 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
176 strcpy (str, test_name);
177 strcat (str, ": set flag, with rest not set");
178 feclearexcept (FE_ALL_EXCEPT);
179 feraiseexcept (exception);
180 test_exceptions (str, fe_exc, ignore_inexact);
182 strcpy (str, test_name);
183 strcat (str, ": clear flag, rest also unset");
184 feclearexcept (exception);
185 test_exceptions (str, NO_EXC, ignore_inexact);
187 strcpy (str, test_name);
188 strcat (str, ": set flag, with rest set");
189 feraiseexcept (FE_ALL_EXCEPT ^ exception);
190 feraiseexcept (exception);
191 test_exceptions (str, ALL_EXC, 0);
193 strcpy (str, test_name);
194 strcat (str, ": clear flag, leave rest set");
195 feclearexcept (exception);
196 test_exceptions (str, ALL_EXC ^ fe_exc, 0);
202 /* clear all exceptions and test if all are cleared */
203 feclearexcept (FE_ALL_EXCEPT);
204 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
207 /* raise all exceptions and test if all are raised */
208 feraiseexcept (FE_ALL_EXCEPT);
209 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
211 feclearexcept (FE_ALL_EXCEPT);
214 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
217 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
220 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
223 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
226 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
230 /* Test that program aborts with no masked interrupts */
232 feenv_nomask_test (const char *flag_name, int fe_exc)
234 #if defined FE_NOMASK_ENV
238 if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT)
239 && fesetenv (FE_NOMASK_ENV) != 0)
241 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
245 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
246 printf (" when feraiseexcept (%s) is called.\n", flag_name);
251 /* Try to avoid dumping core. */
252 struct rlimit core_limit;
253 core_limit.rlim_cur = 0;
254 core_limit.rlim_max = 0;
255 setrlimit (RLIMIT_CORE, &core_limit);
258 fesetenv (FE_NOMASK_ENV);
259 feraiseexcept (fe_exc);
266 printf (" Fail: Could not fork.\n");
270 printf (" `fork' not implemented, test ignored.\n");
273 if (waitpid (pid, &status, 0) != pid)
275 printf (" Fail: waitpid call failed.\n");
278 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
279 printf (" Pass: Process received SIGFPE.\n");
282 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
290 /* Test that program doesn't abort with default environment */
292 feenv_mask_test (const char *flag_name, int fe_exc)
297 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
298 printf (" when feraiseexcept (%s) is called.\n", flag_name);
303 /* Try to avoid dumping core. */
304 struct rlimit core_limit;
305 core_limit.rlim_cur = 0;
306 core_limit.rlim_max = 0;
307 setrlimit (RLIMIT_CORE, &core_limit);
310 fesetenv (FE_DFL_ENV);
311 feraiseexcept (fe_exc);
318 printf (" Fail: Could not fork.\n");
322 printf (" `fork' not implemented, test ignored.\n");
325 if (waitpid (pid, &status, 0) != pid)
327 printf (" Fail: waitpid call failed.\n");
330 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
331 printf (" Pass: Process exited normally.\n");
334 printf (" Fail: Process exited abnormally with status %d.\n",
341 /* Test that program aborts with no masked interrupts */
343 feexcp_nomask_test (const char *flag_name, int fe_exc)
348 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1)
350 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
354 printf ("Test: after feenableexcept (%s) processes will abort\n",
356 printf (" when feraiseexcept (%s) is called.\n", flag_name);
361 /* Try to avoid dumping core. */
362 struct rlimit core_limit;
363 core_limit.rlim_cur = 0;
364 core_limit.rlim_max = 0;
365 setrlimit (RLIMIT_CORE, &core_limit);
368 fedisableexcept (FE_ALL_EXCEPT);
369 feenableexcept (fe_exc);
370 feraiseexcept (fe_exc);
377 printf (" Fail: Could not fork.\n");
381 printf (" `fork' not implemented, test ignored.\n");
384 if (waitpid (pid, &status, 0) != pid)
386 printf (" Fail: waitpid call failed.\n");
389 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
390 printf (" Pass: Process received SIGFPE.\n");
393 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
400 /* Test that program doesn't abort with exception. */
402 feexcp_mask_test (const char *flag_name, int fe_exc)
408 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
410 printf (" when feraiseexcept (%s) is called.\n", flag_name);
415 /* Try to avoid dumping core. */
416 struct rlimit core_limit;
417 core_limit.rlim_cur = 0;
418 core_limit.rlim_max = 0;
419 setrlimit (RLIMIT_CORE, &core_limit);
421 feenableexcept (FE_ALL_EXCEPT);
424 /* The standard allows the inexact exception to be set together with the
425 underflow and overflow exceptions. So add FE_INEXACT to the set of
426 exceptions to be disabled if we will be raising underflow or
429 if (fe_exc & FE_OVERFLOW)
430 exception |= FE_INEXACT;
433 if (fe_exc & FE_UNDERFLOW)
434 exception |= FE_INEXACT;
437 fedisableexcept (exception);
438 feraiseexcept (fe_exc);
445 printf (" Fail: Could not fork.\n");
449 printf (" `fork' not implemented, test ignored.\n");
452 if (waitpid (pid, &status, 0) != pid)
454 printf (" Fail: waitpid call failed.\n");
457 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
458 printf (" Pass: Process exited normally.\n");
461 printf (" Fail: Process exited abnormally with status %d.\n",
469 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
471 feenable_test (const char *flag_name, int fe_exc)
475 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
477 /* First disable all exceptions. */
478 if (fedisableexcept (FE_ALL_EXCEPT) == -1)
480 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
482 /* If this fails, the other tests don't make sense. */
485 excepts = fegetexcept ();
488 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
492 excepts = feenableexcept (fe_exc);
493 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && excepts == -1)
495 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
500 printf ("Test: feenableexcept (%s) failed\n", flag_name);
506 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
511 excepts = fegetexcept ();
512 if (excepts != fe_exc)
514 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
515 flag_name, fe_exc, excepts);
519 /* And now disable the exception again. */
520 excepts = fedisableexcept (fe_exc);
523 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
527 if (excepts != fe_exc)
529 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
530 flag_name, fe_exc, excepts);
534 excepts = fegetexcept ();
537 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
542 /* Now the other way round: Enable all exceptions and disable just this one. */
543 if (feenableexcept (FE_ALL_EXCEPT) == -1)
545 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
547 /* If this fails, the other tests don't make sense. */
551 excepts = fegetexcept ();
552 if (excepts != FE_ALL_EXCEPT)
554 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
555 flag_name, FE_ALL_EXCEPT, excepts);
559 excepts = fedisableexcept (fe_exc);
562 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
566 if (excepts != FE_ALL_EXCEPT)
568 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
573 excepts = fegetexcept ();
574 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
576 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
577 flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
581 /* And now enable the exception again. */
582 excepts = feenableexcept (fe_exc);
585 printf ("Test: feenableexcept (%s) failed\n", flag_name);
589 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
591 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
596 excepts = fegetexcept ();
597 if (excepts != FE_ALL_EXCEPT)
599 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
600 flag_name, FE_ALL_EXCEPT, excepts);
603 feexcp_nomask_test (flag_name, fe_exc);
604 feexcp_mask_test (flag_name, fe_exc);
610 fe_single_test (const char *flag_name, int fe_exc)
612 feenv_nomask_test (flag_name, fe_exc);
613 feenv_mask_test (flag_name, fe_exc);
614 feenable_test (flag_name, fe_exc);
621 /* We might have some exceptions still set. */
622 feclearexcept (FE_ALL_EXCEPT);
625 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
628 fe_single_test ("FE_INVALID", FE_INVALID);
631 fe_single_test ("FE_INEXACT", FE_INEXACT);
634 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
637 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
639 fesetenv (FE_DFL_ENV);
644 feholdexcept_tests (void)
646 fenv_t saved, saved2;
649 feclearexcept (FE_ALL_EXCEPT);
650 fedisableexcept (FE_ALL_EXCEPT);
652 feraiseexcept (FE_DIVBYZERO);
654 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
656 res = feholdexcept (&saved);
659 printf ("feholdexcept failed: %d\n", res);
662 #if defined FE_TONEAREST && defined FE_TOWARDZERO
663 res = fesetround (FE_TOWARDZERO);
666 printf ("fesetround failed: %d\n", res);
670 test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0);
672 feraiseexcept (FE_INVALID);
673 test_exceptions ("feholdexcept_tests FE_INVALID test",
676 res = feupdateenv (&saved);
679 printf ("feupdateenv failed: %d\n", res);
682 #if defined FE_TONEAREST && defined FE_TOWARDZERO
684 if (res != FE_TONEAREST)
686 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
690 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
691 DIVBYZERO_EXC | INVALID_EXC, 0);
692 feclearexcept (FE_ALL_EXCEPT);
694 feraiseexcept (FE_INVALID);
696 #if defined FE_TONEAREST && defined FE_UPWARD
697 res = fesetround (FE_UPWARD);
700 printf ("fesetround failed: %d\n", res);
704 res = feholdexcept (&saved2);
707 printf ("feholdexcept failed: %d\n", res);
710 #if defined FE_TONEAREST && defined FE_UPWARD
711 res = fesetround (FE_TONEAREST);
714 printf ("fesetround failed: %d\n", res);
718 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0);
720 feraiseexcept (FE_INEXACT);
721 test_exceptions ("feholdexcept_tests FE_INEXACT test",
724 res = feupdateenv (&saved2);
727 printf ("feupdateenv failed: %d\n", res);
730 #if defined FE_TONEAREST && defined FE_UPWARD
732 if (res != FE_UPWARD)
734 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
737 fesetround (FE_TONEAREST);
739 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
740 INVALID_EXC | INEXACT_EXC, 0);
741 feclearexcept (FE_ALL_EXCEPT);
745 /* IEC 559 and ISO C99 define a default startup environment */
749 test_exceptions ("Initially all exceptions should be cleared",
752 test_rounding ("Rounding direction should be initalized to nearest",
763 feholdexcept_tests ();
767 printf ("\n%d errors occurred.\n", count_errors);
770 printf ("\n All tests passed successfully.\n");