1 /* Copyright (C) 1997-2019 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 /* Skip further tests here if exceptions not supported. */
212 if (!EXCEPTION_TESTS (float) && FE_ALL_EXCEPT
!= 0)
214 /* raise all exceptions and test if all are raised */
215 feraiseexcept (FE_ALL_EXCEPT
);
216 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
218 feclearexcept (FE_ALL_EXCEPT
);
221 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC
, FE_DIVBYZERO
);
224 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC
, FE_INVALID
);
227 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC
, FE_INEXACT
);
230 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC
, FE_UNDERFLOW
);
233 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC
, FE_OVERFLOW
);
238 /* Test that program aborts with no masked interrupts */
240 feenv_nomask_test (const char *flag_name
, int fe_exc
)
242 # if defined FE_NOMASK_ENV
246 if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT
)
247 && fesetenv (FE_NOMASK_ENV
) != 0)
249 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
253 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
254 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
259 /* Try to avoid dumping core. */
260 struct rlimit core_limit
;
261 core_limit
.rlim_cur
= 0;
262 core_limit
.rlim_max
= 0;
263 setrlimit (RLIMIT_CORE
, &core_limit
);
266 fesetenv (FE_NOMASK_ENV
);
267 feraiseexcept (fe_exc
);
274 printf (" Fail: Could not fork.\n");
278 printf (" `fork' not implemented, test ignored.\n");
281 if (waitpid (pid
, &status
, 0) != pid
)
283 printf (" Fail: waitpid call failed.\n");
286 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
287 printf (" Pass: Process received SIGFPE.\n");
290 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
298 /* Test that program doesn't abort with default environment */
300 feenv_mask_test (const char *flag_name
, int fe_exc
)
305 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
306 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
311 /* Try to avoid dumping core. */
312 struct rlimit core_limit
;
313 core_limit
.rlim_cur
= 0;
314 core_limit
.rlim_max
= 0;
315 setrlimit (RLIMIT_CORE
, &core_limit
);
318 fesetenv (FE_DFL_ENV
);
319 feraiseexcept (fe_exc
);
326 printf (" Fail: Could not fork.\n");
330 printf (" `fork' not implemented, test ignored.\n");
333 if (waitpid (pid
, &status
, 0) != pid
)
335 printf (" Fail: waitpid call failed.\n");
338 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
339 printf (" Pass: Process exited normally.\n");
342 printf (" Fail: Process exited abnormally with status %d.\n",
349 /* Test that program aborts with no masked interrupts */
351 feexcp_nomask_test (const char *flag_name
, int fe_exc
)
356 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc
) && feenableexcept (fe_exc
) == -1)
358 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
362 printf ("Test: after feenableexcept (%s) processes will abort\n",
364 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
369 /* Try to avoid dumping core. */
370 struct rlimit core_limit
;
371 core_limit
.rlim_cur
= 0;
372 core_limit
.rlim_max
= 0;
373 setrlimit (RLIMIT_CORE
, &core_limit
);
376 fedisableexcept (FE_ALL_EXCEPT
);
377 feenableexcept (fe_exc
);
378 feraiseexcept (fe_exc
);
385 printf (" Fail: Could not fork.\n");
389 printf (" `fork' not implemented, test ignored.\n");
392 if (waitpid (pid
, &status
, 0) != pid
)
394 printf (" Fail: waitpid call failed.\n");
397 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
398 printf (" Pass: Process received SIGFPE.\n");
401 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
408 /* Test that program doesn't abort with exception. */
410 feexcp_mask_test (const char *flag_name
, int fe_exc
)
416 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
418 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
423 /* Try to avoid dumping core. */
424 struct rlimit core_limit
;
425 core_limit
.rlim_cur
= 0;
426 core_limit
.rlim_max
= 0;
427 setrlimit (RLIMIT_CORE
, &core_limit
);
429 feenableexcept (FE_ALL_EXCEPT
);
432 /* The standard allows the inexact exception to be set together with the
433 underflow and overflow exceptions. So add FE_INEXACT to the set of
434 exceptions to be disabled if we will be raising underflow or
437 if (fe_exc
& FE_OVERFLOW
)
438 exception
|= FE_INEXACT
;
441 if (fe_exc
& FE_UNDERFLOW
)
442 exception
|= FE_INEXACT
;
445 fedisableexcept (exception
);
446 feraiseexcept (fe_exc
);
453 printf (" Fail: Could not fork.\n");
457 printf (" `fork' not implemented, test ignored.\n");
460 if (waitpid (pid
, &status
, 0) != pid
)
462 printf (" Fail: waitpid call failed.\n");
465 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
466 printf (" Pass: Process exited normally.\n");
469 printf (" Fail: Process exited abnormally with status %d.\n",
477 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
479 feenable_test (const char *flag_name
, int fe_exc
)
483 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name
);
485 /* First disable all exceptions. */
486 if (fedisableexcept (FE_ALL_EXCEPT
) == -1)
488 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
490 /* If this fails, the other tests don't make sense. */
493 excepts
= fegetexcept ();
496 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
500 excepts
= feenableexcept (fe_exc
);
501 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc
) && excepts
== -1)
503 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
508 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
514 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
519 excepts
= fegetexcept ();
520 if (excepts
!= fe_exc
)
522 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
523 flag_name
, fe_exc
, excepts
);
527 /* And now disable the exception again. */
528 excepts
= fedisableexcept (fe_exc
);
531 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
535 if (excepts
!= fe_exc
)
537 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
538 flag_name
, fe_exc
, excepts
);
542 excepts
= fegetexcept ();
545 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
550 /* Now the other way round: Enable all exceptions and disable just this one. */
551 if (feenableexcept (FE_ALL_EXCEPT
) == -1)
553 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
555 /* If this fails, the other tests don't make sense. */
559 excepts
= fegetexcept ();
560 if (excepts
!= FE_ALL_EXCEPT
)
562 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
563 flag_name
, FE_ALL_EXCEPT
, excepts
);
567 excepts
= fedisableexcept (fe_exc
);
570 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
574 if (excepts
!= FE_ALL_EXCEPT
)
576 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
581 excepts
= fegetexcept ();
582 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
584 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
585 flag_name
, (FE_ALL_EXCEPT
& ~fe_exc
), excepts
);
589 /* And now enable the exception again. */
590 excepts
= feenableexcept (fe_exc
);
593 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
597 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
599 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
604 excepts
= fegetexcept ();
605 if (excepts
!= FE_ALL_EXCEPT
)
607 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
608 flag_name
, FE_ALL_EXCEPT
, excepts
);
611 feexcp_nomask_test (flag_name
, fe_exc
);
612 feexcp_mask_test (flag_name
, fe_exc
);
618 fe_single_test (const char *flag_name
, int fe_exc
)
620 feenv_nomask_test (flag_name
, fe_exc
);
621 feenv_mask_test (flag_name
, fe_exc
);
622 feenable_test (flag_name
, fe_exc
);
630 /* We might have some exceptions still set. */
631 feclearexcept (FE_ALL_EXCEPT
);
634 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
637 fe_single_test ("FE_INVALID", FE_INVALID
);
640 fe_single_test ("FE_INEXACT", FE_INEXACT
);
643 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
646 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW
);
648 fesetenv (FE_DFL_ENV
);
653 feholdexcept_tests (void)
655 fenv_t saved
, saved2
;
658 feclearexcept (FE_ALL_EXCEPT
);
659 fedisableexcept (FE_ALL_EXCEPT
);
661 feraiseexcept (FE_DIVBYZERO
);
663 if (EXCEPTION_TESTS (float))
664 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
666 res
= feholdexcept (&saved
);
669 printf ("feholdexcept failed: %d\n", res
);
672 #if defined FE_TONEAREST && defined FE_TOWARDZERO
673 res
= fesetround (FE_TOWARDZERO
);
674 if (res
!= 0 && ROUNDING_TESTS (float, FE_TOWARDZERO
))
676 printf ("fesetround failed: %d\n", res
);
680 test_exceptions ("feholdexcept_tests 0 test", NO_EXC
, 0);
682 feraiseexcept (FE_INVALID
);
683 if (EXCEPTION_TESTS (float))
684 test_exceptions ("feholdexcept_tests FE_INVALID test",
687 res
= feupdateenv (&saved
);
690 printf ("feupdateenv failed: %d\n", res
);
693 #if defined FE_TONEAREST && defined FE_TOWARDZERO
695 if (res
!= FE_TONEAREST
)
697 printf ("feupdateenv didn't restore rounding mode: %d\n", res
);
701 if (EXCEPTION_TESTS (float))
702 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
703 DIVBYZERO_EXC
| INVALID_EXC
, 0);
704 feclearexcept (FE_ALL_EXCEPT
);
706 feraiseexcept (FE_INVALID
);
708 #if defined FE_TONEAREST && defined FE_UPWARD
709 res
= fesetround (FE_UPWARD
);
710 if (res
!= 0 && ROUNDING_TESTS (float, FE_UPWARD
))
712 printf ("fesetround failed: %d\n", res
);
716 res
= feholdexcept (&saved2
);
719 printf ("feholdexcept failed: %d\n", res
);
722 #if defined FE_TONEAREST && defined FE_UPWARD
723 res
= fesetround (FE_TONEAREST
);
726 printf ("fesetround failed: %d\n", res
);
730 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC
, 0);
732 feraiseexcept (FE_INEXACT
);
733 if (EXCEPTION_TESTS (float))
734 test_exceptions ("feholdexcept_tests FE_INEXACT test",
737 res
= feupdateenv (&saved2
);
740 printf ("feupdateenv failed: %d\n", res
);
743 #if defined FE_TONEAREST && defined FE_UPWARD
745 if (res
!= FE_UPWARD
&& ROUNDING_TESTS (float, FE_UPWARD
))
747 printf ("feupdateenv didn't restore rounding mode: %d\n", res
);
750 fesetround (FE_TONEAREST
);
752 if (EXCEPTION_TESTS (float))
753 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
754 INVALID_EXC
| INEXACT_EXC
, 0);
755 feclearexcept (FE_ALL_EXCEPT
);
759 /* IEC 559 and ISO C99 define a default startup environment */
763 test_exceptions ("Initially all exceptions should be cleared",
766 test_rounding ("Rounding direction should be initalized to nearest",
777 feholdexcept_tests ();
781 printf ("\n%d errors occurred.\n", count_errors
);
784 printf ("\n All tests passed successfully.\n");