1 /* Copyright (C) 1997, 1998, 2000, 2001, 2003, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de> and
5 Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 /* Tests for ISO C99 7.6: Floating-point environment */
40 #include <sys/resource.h>
43 Since not all architectures might define all exceptions, we define
44 a private set and map accordingly.
47 #define INEXACT_EXC 0x1
48 #define DIVBYZERO_EXC 0x2
49 #define UNDERFLOW_EXC 0x04
50 #define OVERFLOW_EXC 0x08
51 #define INVALID_EXC 0x10
53 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
56 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
);
90 test_exceptions (const char *test_name
, short int exception
,
93 printf ("Test: %s\n", test_name
);
95 test_single_exception (exception
, DIVBYZERO_EXC
, FE_DIVBYZERO
,
99 test_single_exception (exception
, INVALID_EXC
, FE_INVALID
,
104 test_single_exception (exception
, INEXACT_EXC
, FE_INEXACT
,
108 test_single_exception (exception
, UNDERFLOW_EXC
, FE_UNDERFLOW
,
112 test_single_exception (exception
, OVERFLOW_EXC
, FE_OVERFLOW
,
118 print_rounding (int rounding
)
125 printf ("TONEAREST");
140 printf ("TOWARDZERO");
149 test_rounding (const char *test_name
, int rounding_mode
)
151 int curr_rounding
= fegetround ();
153 printf ("Test: %s\n", test_name
);
154 if (curr_rounding
== rounding_mode
)
156 printf (" Pass: Rounding mode is ");
157 print_rounding (curr_rounding
);
162 printf (" Fail: Rounding mode is ");
163 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);
203 /* clear all exceptions and test if all are cleared */
204 feclearexcept (FE_ALL_EXCEPT
);
205 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
208 /* raise all exceptions and test if all are raised */
209 feraiseexcept (FE_ALL_EXCEPT
);
210 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
212 feclearexcept (FE_ALL_EXCEPT
);
215 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC
, FE_DIVBYZERO
);
218 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC
, FE_INVALID
);
221 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC
, FE_INEXACT
);
224 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC
, FE_UNDERFLOW
);
227 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC
, FE_OVERFLOW
);
231 /* Test that program aborts with no masked interrupts */
233 feenv_nomask_test (const char *flag_name
, int fe_exc
)
235 #if defined FE_NOMASK_ENV
242 fesetenv (FE_NOMASK_ENV
);
245 if (status
== ENOSYS
)
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 printf ("Test: after fedisableexcept (%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
)
476 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name
);
478 /* First disable all exceptions. */
479 if (fedisableexcept (FE_ALL_EXCEPT
) == -1)
481 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
483 /* If this fails, the other tests don't make sense. */
486 excepts
= fegetexcept ();
489 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
494 excepts
= feenableexcept (fe_exc
);
497 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
503 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
508 excepts
= fegetexcept ();
509 if (excepts
!= fe_exc
)
511 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
512 flag_name
, fe_exc
, excepts
);
516 /* And now disable the exception again. */
517 excepts
= fedisableexcept (fe_exc
);
520 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
524 if (excepts
!= fe_exc
)
526 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
527 flag_name
, fe_exc
, excepts
);
531 excepts
= fegetexcept ();
534 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
539 /* Now the other way round: Enable all exceptions and disable just this one. */
540 if (feenableexcept (FE_ALL_EXCEPT
) == -1)
542 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
544 /* If this fails, the other tests don't make sense. */
548 excepts
= fegetexcept ();
549 if (excepts
!= FE_ALL_EXCEPT
)
551 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
552 flag_name
, FE_ALL_EXCEPT
, excepts
);
556 excepts
= fedisableexcept (fe_exc
);
559 printf ("Test: fedisableexcept (%s) failed\n", flag_name
);
563 if (excepts
!= FE_ALL_EXCEPT
)
565 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
570 excepts
= fegetexcept ();
571 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
573 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
574 flag_name
, (FE_ALL_EXCEPT
& ~fe_exc
), excepts
);
578 /* And now enable the exception again. */
579 excepts
= feenableexcept (fe_exc
);
582 printf ("Test: feenableexcept (%s) failed\n", flag_name
);
586 if (excepts
!= (FE_ALL_EXCEPT
& ~fe_exc
))
588 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
593 excepts
= fegetexcept ();
594 if (excepts
!= FE_ALL_EXCEPT
)
596 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
597 flag_name
, FE_ALL_EXCEPT
, excepts
);
600 feexcp_nomask_test (flag_name
, fe_exc
);
601 feexcp_mask_test (flag_name
, fe_exc
);
607 fe_single_test (const char *flag_name
, int fe_exc
)
609 feenv_nomask_test (flag_name
, fe_exc
);
610 feenv_mask_test (flag_name
, fe_exc
);
611 feenable_test (flag_name
, fe_exc
);
618 /* We might have some exceptions still set. */
619 feclearexcept (FE_ALL_EXCEPT
);
622 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
625 fe_single_test ("FE_INVALID", FE_INVALID
);
628 fe_single_test ("FE_INEXACT", FE_INEXACT
);
631 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
634 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW
);
636 fesetenv (FE_DFL_ENV
);
641 feholdexcept_tests (void)
643 fenv_t saved
, saved2
;
646 feclearexcept (FE_ALL_EXCEPT
);
647 fedisableexcept (FE_ALL_EXCEPT
);
649 feraiseexcept (FE_DIVBYZERO
);
651 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
653 res
= feholdexcept (&saved
);
656 printf ("feholdexcept failed: %d\n", res
);
659 #if defined FE_TONEAREST && defined FE_TOWARDZERO
660 res
= fesetround (FE_TOWARDZERO
);
663 printf ("fesetround failed: %d\n", res
);
667 test_exceptions ("feholdexcept_tests 0 test", NO_EXC
, 0);
668 feraiseexcept (FE_INVALID
);
669 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
);
688 feraiseexcept (FE_INVALID
);
689 #if defined FE_TONEAREST && defined FE_UPWARD
690 res
= fesetround (FE_UPWARD
);
693 printf ("fesetround failed: %d\n", res
);
697 res
= feholdexcept (&saved2
);
700 printf ("feholdexcept failed: %d\n", res
);
703 #if defined FE_TONEAREST && defined FE_UPWARD
704 res
= fesetround (FE_TONEAREST
);
707 printf ("fesetround failed: %d\n", res
);
711 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC
, 0);
712 feraiseexcept (FE_INEXACT
);
713 test_exceptions ("feholdexcept_tests FE_INEXACT test",
715 res
= feupdateenv (&saved2
);
718 printf ("feupdateenv failed: %d\n", res
);
721 #if defined FE_TONEAREST && defined FE_UPWARD
723 if (res
!= FE_UPWARD
)
725 printf ("feupdateenv didn't restore rounding mode: %d\n", res
);
728 fesetround (FE_TONEAREST
);
730 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
731 INVALID_EXC
| INEXACT_EXC
, 0);
732 feclearexcept (FE_ALL_EXCEPT
);
736 /* IEC 559 and ISO C99 define a default startup environment */
740 test_exceptions ("Initially all exceptions should be cleared",
743 test_rounding ("Rounding direction should be initalized to nearest",
754 feholdexcept_tests ();
758 printf ("\n%d errors occurred.\n", count_errors
);
761 printf ("\n All tests passed successfully.\n");