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.
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
;
58 /* Test whether a given exception was raised. */
60 test_single_exception (short int exception
,
63 const char *flag_name
)
65 if (exception
& exc_flag
)
67 if (fetestexcept (fe_flag
))
68 printf (" Pass: Exception \"%s\" is set\n", flag_name
);
71 printf (" Fail: Exception \"%s\" is not set\n", flag_name
);
77 if (fetestexcept (fe_flag
))
79 printf (" Fail: Exception \"%s\" is set\n", flag_name
);
84 printf (" Pass: Exception \"%s\" is not set\n", flag_name
);
91 test_exceptions (const char *test_name
, short int exception
,
94 printf ("Test: %s\n", test_name
);
96 test_single_exception (exception
, DIVBYZERO_EXC
, FE_DIVBYZERO
,
100 test_single_exception (exception
, INVALID_EXC
, FE_INVALID
,
105 test_single_exception (exception
, INEXACT_EXC
, FE_INEXACT
,
109 test_single_exception (exception
, UNDERFLOW_EXC
, FE_UNDERFLOW
,
113 test_single_exception (exception
, OVERFLOW_EXC
, FE_OVERFLOW
,
119 print_rounding (int rounding
)
126 printf ("TONEAREST");
141 printf ("TOWARDZERO");
150 test_rounding (const char *test_name
, int rounding_mode
)
152 int curr_rounding
= fegetround ();
154 printf ("Test: %s\n", test_name
);
155 if (curr_rounding
== rounding_mode
)
157 printf (" Pass: Rounding mode is ");
158 print_rounding (curr_rounding
);
163 printf (" Fail: Rounding mode is ");
164 print_rounding (curr_rounding
);
171 set_single_exc (const char *test_name
, int fe_exc
, fexcept_t exception
)
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;
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
);
185 strcpy (str
, test_name
);
186 strcat (str
, ": clear flag, rest also unset");
187 feclearexcept (exception
);
188 test_exceptions (str
, NO_EXC
, ignore_inexact
);
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);
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);
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",
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",
215 feclearexcept (FE_ALL_EXCEPT
);
218 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC
, FE_DIVBYZERO
);
221 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC
, FE_INVALID
);
224 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC
, FE_INEXACT
);
227 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC
, FE_UNDERFLOW
);
230 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC
, FE_OVERFLOW
);
235 /* Test that program aborts with no masked interrupts */
237 feenv_nomask_test (const char *flag_name
, int fe_exc
)
239 # if defined FE_NOMASK_ENV
243 if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT
)
244 && fesetenv (FE_NOMASK_ENV
) != 0)
246 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
250 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
251 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
263 fesetenv (FE_NOMASK_ENV
);
264 feraiseexcept (fe_exc
);
271 printf (" Fail: Could not fork.\n");
275 printf (" `fork' not implemented, test ignored.\n");
278 if (waitpid (pid
, &status
, 0) != pid
)
280 printf (" Fail: waitpid call failed.\n");
283 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
284 printf (" Pass: Process received SIGFPE.\n");
287 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
295 /* Test that program doesn't abort with default environment */
297 feenv_mask_test (const char *flag_name
, int fe_exc
)
302 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
303 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
315 fesetenv (FE_DFL_ENV
);
316 feraiseexcept (fe_exc
);
323 printf (" Fail: Could not fork.\n");
327 printf (" `fork' not implemented, test ignored.\n");
330 if (waitpid (pid
, &status
, 0) != pid
)
332 printf (" Fail: waitpid call failed.\n");
335 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
336 printf (" Pass: Process exited normally.\n");
339 printf (" Fail: Process exited abnormally with status %d.\n",
346 /* Test that program aborts with no masked interrupts */
348 feexcp_nomask_test (const char *flag_name
, int fe_exc
)
353 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc
) && feenableexcept (fe_exc
) == -1)
355 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
359 printf ("Test: after feenableexcept (%s) processes will abort\n",
361 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
373 fedisableexcept (FE_ALL_EXCEPT
);
374 feenableexcept (fe_exc
);
375 feraiseexcept (fe_exc
);
382 printf (" Fail: Could not fork.\n");
386 printf (" `fork' not implemented, test ignored.\n");
389 if (waitpid (pid
, &status
, 0) != pid
)
391 printf (" Fail: waitpid call failed.\n");
394 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
395 printf (" Pass: Process received SIGFPE.\n");
398 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
405 /* Test that program doesn't abort with exception. */
407 feexcp_mask_test (const char *flag_name
, int fe_exc
)
413 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
415 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
426 feenableexcept (FE_ALL_EXCEPT
);
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
434 if (fe_exc
& FE_OVERFLOW
)
435 exception
|= FE_INEXACT
;
438 if (fe_exc
& FE_UNDERFLOW
)
439 exception
|= FE_INEXACT
;
442 fedisableexcept (exception
);
443 feraiseexcept (fe_exc
);
450 printf (" Fail: Could not fork.\n");
454 printf (" `fork' not implemented, test ignored.\n");
457 if (waitpid (pid
, &status
, 0) != pid
)
459 printf (" Fail: waitpid call failed.\n");
462 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
463 printf (" Pass: Process exited normally.\n");
466 printf (" Fail: Process exited abnormally with status %d.\n",
474 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
476 feenable_test (const char *flag_name
, int fe_exc
)
480 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name
);
482 /* First disable all exceptions. */
483 if (fedisableexcept (FE_ALL_EXCEPT
) == -1)
485 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
487 /* If this fails, the other tests don't make sense. */
490 excepts
= fegetexcept ();
493 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
497 excepts
= feenableexcept (fe_exc
);
498 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc
) && excepts
== -1)
500 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
505 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
511 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
516 excepts
= fegetexcept ();
517 if (excepts
!= fe_exc
)
519 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
520 flag_name
, fe_exc
, excepts
);
524 /* And now disable the exception again. */
525 excepts
= fedisableexcept (fe_exc
);
528 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
532 if (excepts
!= fe_exc
)
534 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
535 flag_name
, fe_exc
, excepts
);
539 excepts
= fegetexcept ();
542 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
547 /* Now the other way round: Enable all exceptions and disable just this one. */
548 if (feenableexcept (FE_ALL_EXCEPT
) == -1)
550 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
552 /* If this fails, the other tests don't make sense. */
556 excepts
= fegetexcept ();
557 if (excepts
!= FE_ALL_EXCEPT
)
559 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
560 flag_name
, FE_ALL_EXCEPT
, excepts
);
564 excepts
= fedisableexcept (fe_exc
);
567 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
571 if (excepts
!= FE_ALL_EXCEPT
)
573 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
578 excepts
= fegetexcept ();
579 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
581 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
582 flag_name
, (FE_ALL_EXCEPT
& ~fe_exc
), excepts
);
586 /* And now enable the exception again. */
587 excepts
= feenableexcept (fe_exc
);
590 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
594 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
596 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
601 excepts
= fegetexcept ();
602 if (excepts
!= FE_ALL_EXCEPT
)
604 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
605 flag_name
, FE_ALL_EXCEPT
, excepts
);
608 feexcp_nomask_test (flag_name
, fe_exc
);
609 feexcp_mask_test (flag_name
, fe_exc
);
615 fe_single_test (const char *flag_name
, int fe_exc
)
617 feenv_nomask_test (flag_name
, fe_exc
);
618 feenv_mask_test (flag_name
, fe_exc
);
619 feenable_test (flag_name
, fe_exc
);
627 /* We might have some exceptions still set. */
628 feclearexcept (FE_ALL_EXCEPT
);
631 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
634 fe_single_test ("FE_INVALID", FE_INVALID
);
637 fe_single_test ("FE_INEXACT", FE_INEXACT
);
640 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
643 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW
);
645 fesetenv (FE_DFL_ENV
);
650 feholdexcept_tests (void)
652 fenv_t saved
, saved2
;
655 feclearexcept (FE_ALL_EXCEPT
);
656 fedisableexcept (FE_ALL_EXCEPT
);
658 feraiseexcept (FE_DIVBYZERO
);
660 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
662 res
= feholdexcept (&saved
);
665 printf ("feholdexcept failed: %d\n", res
);
668 #if defined FE_TONEAREST && defined FE_TOWARDZERO
669 res
= fesetround (FE_TOWARDZERO
);
672 printf ("fesetround failed: %d\n", res
);
676 test_exceptions ("feholdexcept_tests 0 test", NO_EXC
, 0);
678 feraiseexcept (FE_INVALID
);
679 test_exceptions ("feholdexcept_tests FE_INVALID test",
682 res
= feupdateenv (&saved
);
685 printf ("feupdateenv failed: %d\n", res
);
688 #if defined FE_TONEAREST && defined FE_TOWARDZERO
690 if (res
!= FE_TONEAREST
)
692 printf ("feupdateenv didn't restore rounding mode: %d\n", res
);
696 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
697 DIVBYZERO_EXC
| INVALID_EXC
, 0);
698 feclearexcept (FE_ALL_EXCEPT
);
700 feraiseexcept (FE_INVALID
);
702 #if defined FE_TONEAREST && defined FE_UPWARD
703 res
= fesetround (FE_UPWARD
);
706 printf ("fesetround failed: %d\n", res
);
710 res
= feholdexcept (&saved2
);
713 printf ("feholdexcept failed: %d\n", res
);
716 #if defined FE_TONEAREST && defined FE_UPWARD
717 res
= fesetround (FE_TONEAREST
);
720 printf ("fesetround failed: %d\n", res
);
724 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC
, 0);
726 feraiseexcept (FE_INEXACT
);
727 test_exceptions ("feholdexcept_tests FE_INEXACT test",
730 res
= feupdateenv (&saved2
);
733 printf ("feupdateenv failed: %d\n", res
);
736 #if defined FE_TONEAREST && defined FE_UPWARD
738 if (res
!= FE_UPWARD
)
740 printf ("feupdateenv didn't restore rounding mode: %d\n", res
);
743 fesetround (FE_TONEAREST
);
745 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
746 INVALID_EXC
| INEXACT_EXC
, 0);
747 feclearexcept (FE_ALL_EXCEPT
);
751 /* IEC 559 and ISO C99 define a default startup environment */
755 test_exceptions ("Initially all exceptions should be cleared",
758 test_rounding ("Rounding direction should be initalized to nearest",
769 feholdexcept_tests ();
773 printf ("\n%d errors occurred.\n", count_errors
);
776 printf ("\n All tests passed successfully.\n");