localedata: dz_BT, bo_CN: convert to UTF-8
[glibc.git] / math / test-fenv.c
blob9db6789e64b475ec11bc052ec2817f9e7f451451
1 /* Copyright (C) 1997-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Tests for ISO C99 7.6: Floating-point environment */
20 #ifndef _GNU_SOURCE
21 # define _GNU_SOURCE
22 #endif
24 #include <complex.h>
25 #include <math.h>
26 #include <float.h>
27 #include <fenv.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/wait.h>
36 #include <sys/resource.h>
37 #include <math-tests.h>
40 Since not all architectures might define all exceptions, we define
41 a private set and map accordingly.
43 #define NO_EXC 0
44 #define INEXACT_EXC 0x1
45 #define DIVBYZERO_EXC 0x2
46 #define UNDERFLOW_EXC 0x04
47 #define OVERFLOW_EXC 0x08
48 #define INVALID_EXC 0x10
49 #define ALL_EXC \
50 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC \
51 | INVALID_EXC)
53 static int count_errors;
55 #if FE_ALL_EXCEPT
56 /* Test whether a given exception was raised. */
57 static void
58 test_single_exception (short int exception,
59 short int exc_flag,
60 fexcept_t fe_flag,
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);
67 else
69 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
70 ++count_errors;
73 else
75 if (fetestexcept (fe_flag))
77 printf (" Fail: Exception \"%s\" is set\n", flag_name);
78 ++count_errors;
80 else
82 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
86 #endif
88 static void
89 test_exceptions (const char *test_name, short int exception,
90 int ignore_inexact)
92 printf ("Test: %s\n", test_name);
93 #ifdef FE_DIVBYZERO
94 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
95 "DIVBYZERO");
96 #endif
97 #ifdef FE_INVALID
98 test_single_exception (exception, INVALID_EXC, FE_INVALID,
99 "INVALID");
100 #endif
101 #ifdef FE_INEXACT
102 if (!ignore_inexact)
103 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
104 "INEXACT");
105 #endif
106 #ifdef FE_UNDERFLOW
107 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
108 "UNDERFLOW");
109 #endif
110 #ifdef FE_OVERFLOW
111 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
112 "OVERFLOW");
113 #endif
116 static void
117 print_rounding (int rounding)
120 switch (rounding)
122 #ifdef FE_TONEAREST
123 case FE_TONEAREST:
124 printf ("TONEAREST");
125 break;
126 #endif
127 #ifdef FE_UPWARD
128 case FE_UPWARD:
129 printf ("UPWARD");
130 break;
131 #endif
132 #ifdef FE_DOWNWARD
133 case FE_DOWNWARD:
134 printf ("DOWNWARD");
135 break;
136 #endif
137 #ifdef FE_TOWARDZERO
138 case FE_TOWARDZERO:
139 printf ("TOWARDZERO");
140 break;
141 #endif
143 printf (".\n");
147 static void
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);
158 else
160 ++count_errors;
161 printf (" Fail: Rounding mode is ");
162 print_rounding (curr_rounding);
167 #if FE_ALL_EXCEPT
168 static void
169 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
171 char str[200];
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);
200 static void
201 update_single_exc (const char *test_name, const fenv_t *envp, int fe_exc,
202 int fe_exc_clear, int exception)
204 char str[200];
205 /* The standard allows the inexact exception to be set together with the
206 underflow and overflow exceptions. So ignore the inexact flag if the
207 others are raised. */
208 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
210 strcpy (str, test_name);
211 strcat (str, ": set flag, with rest not set");
212 feclearexcept (FE_ALL_EXCEPT);
213 feraiseexcept (exception);
214 feupdateenv (envp);
215 test_exceptions (str, fe_exc, ignore_inexact);
217 strcpy (str, test_name);
218 strcat (str, ": clear flag, rest also unset");
219 feclearexcept (exception);
220 feupdateenv (envp);
221 test_exceptions (str, fe_exc_clear, ignore_inexact);
223 #endif
225 static void
226 fe_tests (void)
228 /* clear all exceptions and test if all are cleared */
229 feclearexcept (FE_ALL_EXCEPT);
230 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
231 NO_EXC, 0);
233 /* Skip further tests here if exceptions not supported. */
234 if (!EXCEPTION_TESTS (float) && FE_ALL_EXCEPT != 0)
235 return;
236 /* raise all exceptions and test if all are raised */
237 feraiseexcept (FE_ALL_EXCEPT);
238 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
239 ALL_EXC, 0);
240 feclearexcept (FE_ALL_EXCEPT);
242 #ifdef FE_DIVBYZERO
243 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
244 #endif
245 #ifdef FE_INVALID
246 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
247 #endif
248 #ifdef FE_INEXACT
249 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
250 #endif
251 #ifdef FE_UNDERFLOW
252 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
253 #endif
254 #ifdef FE_OVERFLOW
255 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
256 #endif
259 #if FE_ALL_EXCEPT
260 static const char *
261 funcname (int (*func)(const fenv_t *))
263 if (func == fesetenv)
264 return "fesetenv";
265 else if (func == feupdateenv)
266 return "feupdateenv";
267 __builtin_unreachable ();
270 /* Test that program aborts with no masked interrupts */
271 static void
272 feenv_nomask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *))
274 # if defined FE_NOMASK_ENV
275 int status;
276 pid_t pid;
278 if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT)
279 && func (FE_NOMASK_ENV) != 0)
281 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
282 return;
285 printf ("Test: after %s (FE_NOMASK_ENV) processes will abort\n", funcname (func));
286 printf (" when feraiseexcept (%s) is called.\n", flag_name);
287 pid = fork ();
288 if (pid == 0)
290 # ifdef RLIMIT_CORE
291 /* Try to avoid dumping core. */
292 struct rlimit core_limit;
293 core_limit.rlim_cur = 0;
294 core_limit.rlim_max = 0;
295 setrlimit (RLIMIT_CORE, &core_limit);
296 # endif
298 fesetenv (FE_NOMASK_ENV);
299 feraiseexcept (fe_exc);
300 exit (2);
302 else if (pid < 0)
304 if (errno != ENOSYS)
306 printf (" Fail: Could not fork.\n");
307 ++count_errors;
309 else
310 printf (" `fork' not implemented, test ignored.\n");
312 else {
313 if (waitpid (pid, &status, 0) != pid)
315 printf (" Fail: waitpid call failed.\n");
316 ++count_errors;
318 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
319 printf (" Pass: Process received SIGFPE.\n");
320 else
322 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
323 status);
324 ++count_errors;
327 # endif
330 /* Test that program doesn't abort with default environment */
331 static void
332 feenv_mask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *))
334 int status;
335 pid_t pid;
337 printf ("Test: after %s (FE_DFL_ENV) processes will not abort\n", funcname (func));
338 printf (" when feraiseexcept (%s) is called.\n", flag_name);
339 pid = fork ();
340 if (pid == 0)
342 #ifdef RLIMIT_CORE
343 /* Try to avoid dumping core. */
344 struct rlimit core_limit;
345 core_limit.rlim_cur = 0;
346 core_limit.rlim_max = 0;
347 setrlimit (RLIMIT_CORE, &core_limit);
348 #endif
350 func (FE_DFL_ENV);
351 feraiseexcept (fe_exc);
352 exit (2);
354 else if (pid < 0)
356 if (errno != ENOSYS)
358 printf (" Fail: Could not fork.\n");
359 ++count_errors;
361 else
362 printf (" `fork' not implemented, test ignored.\n");
364 else {
365 if (waitpid (pid, &status, 0) != pid)
367 printf (" Fail: waitpid call failed.\n");
368 ++count_errors;
370 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
371 printf (" Pass: Process exited normally.\n");
372 else
374 printf (" Fail: Process exited abnormally with status %d.\n",
375 status);
376 ++count_errors;
381 /* Test that program aborts with no masked interrupts */
382 static void
383 feexcp_nomask_test (const char *flag_name, int fe_exc)
385 int status;
386 pid_t pid;
388 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1)
390 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
391 return;
394 printf ("Test: after feenableexcept (%s) processes will abort\n",
395 flag_name);
396 printf (" when feraiseexcept (%s) is called.\n", flag_name);
397 pid = fork ();
398 if (pid == 0)
400 #ifdef RLIMIT_CORE
401 /* Try to avoid dumping core. */
402 struct rlimit core_limit;
403 core_limit.rlim_cur = 0;
404 core_limit.rlim_max = 0;
405 setrlimit (RLIMIT_CORE, &core_limit);
406 #endif
408 fedisableexcept (FE_ALL_EXCEPT);
409 feenableexcept (fe_exc);
410 feraiseexcept (fe_exc);
411 exit (2);
413 else if (pid < 0)
415 if (errno != ENOSYS)
417 printf (" Fail: Could not fork.\n");
418 ++count_errors;
420 else
421 printf (" `fork' not implemented, test ignored.\n");
423 else {
424 if (waitpid (pid, &status, 0) != pid)
426 printf (" Fail: waitpid call failed.\n");
427 ++count_errors;
429 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
430 printf (" Pass: Process received SIGFPE.\n");
431 else
433 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
434 status);
435 ++count_errors;
440 /* Test that program doesn't abort with exception. */
441 static void
442 feexcp_mask_test (const char *flag_name, int fe_exc)
444 int status;
445 int exception;
446 pid_t pid;
448 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
449 flag_name);
450 printf (" when feraiseexcept (%s) is called.\n", flag_name);
451 pid = fork ();
452 if (pid == 0)
454 #ifdef RLIMIT_CORE
455 /* Try to avoid dumping core. */
456 struct rlimit core_limit;
457 core_limit.rlim_cur = 0;
458 core_limit.rlim_max = 0;
459 setrlimit (RLIMIT_CORE, &core_limit);
460 #endif
461 feenableexcept (FE_ALL_EXCEPT);
462 exception = fe_exc;
463 #ifdef FE_INEXACT
464 /* The standard allows the inexact exception to be set together with the
465 underflow and overflow exceptions. So add FE_INEXACT to the set of
466 exceptions to be disabled if we will be raising underflow or
467 overflow. */
468 # ifdef FE_OVERFLOW
469 if (fe_exc & FE_OVERFLOW)
470 exception |= FE_INEXACT;
471 # endif
472 # ifdef FE_UNDERFLOW
473 if (fe_exc & FE_UNDERFLOW)
474 exception |= FE_INEXACT;
475 # endif
476 #endif
477 fedisableexcept (exception);
478 feraiseexcept (fe_exc);
479 exit (2);
481 else if (pid < 0)
483 if (errno != ENOSYS)
485 printf (" Fail: Could not fork.\n");
486 ++count_errors;
488 else
489 printf (" `fork' not implemented, test ignored.\n");
491 else {
492 if (waitpid (pid, &status, 0) != pid)
494 printf (" Fail: waitpid call failed.\n");
495 ++count_errors;
497 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
498 printf (" Pass: Process exited normally.\n");
499 else
501 printf (" Fail: Process exited abnormally with status %d.\n",
502 status);
503 ++count_errors;
509 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
510 static void
511 feenable_test (const char *flag_name, int fe_exc)
513 int excepts;
515 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
517 /* First disable all exceptions. */
518 if (fedisableexcept (FE_ALL_EXCEPT) == -1)
520 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
521 ++count_errors;
522 /* If this fails, the other tests don't make sense. */
523 return;
525 excepts = fegetexcept ();
526 if (excepts != 0)
528 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
529 flag_name, excepts);
530 ++count_errors;
532 excepts = feenableexcept (fe_exc);
533 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && excepts == -1)
535 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
536 return;
538 if (excepts == -1)
540 printf ("Test: feenableexcept (%s) failed\n", flag_name);
541 ++count_errors;
542 return;
544 if (excepts != 0)
546 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
547 flag_name, excepts);
548 ++count_errors;
551 excepts = fegetexcept ();
552 if (excepts != fe_exc)
554 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
555 flag_name, fe_exc, excepts);
556 ++count_errors;
559 /* And now disable the exception again. */
560 excepts = fedisableexcept (fe_exc);
561 if (excepts == -1)
563 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
564 ++count_errors;
565 return;
567 if (excepts != fe_exc)
569 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
570 flag_name, fe_exc, excepts);
571 ++count_errors;
574 excepts = fegetexcept ();
575 if (excepts != 0)
577 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
578 flag_name, excepts);
579 ++count_errors;
582 /* Now the other way round: Enable all exceptions and disable just this one. */
583 if (feenableexcept (FE_ALL_EXCEPT) == -1)
585 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
586 ++count_errors;
587 /* If this fails, the other tests don't make sense. */
588 return;
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);
596 ++count_errors;
599 excepts = fedisableexcept (fe_exc);
600 if (excepts == -1)
602 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
603 ++count_errors;
604 return;
606 if (excepts != FE_ALL_EXCEPT)
608 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
609 flag_name, excepts);
610 ++count_errors;
613 excepts = fegetexcept ();
614 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
616 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
617 flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
618 ++count_errors;
621 /* And now enable the exception again. */
622 excepts = feenableexcept (fe_exc);
623 if (excepts == -1)
625 printf ("Test: feenableexcept (%s) failed\n", flag_name);
626 ++count_errors;
627 return;
629 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
631 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
632 flag_name, excepts);
633 ++count_errors;
636 excepts = fegetexcept ();
637 if (excepts != FE_ALL_EXCEPT)
639 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
640 flag_name, FE_ALL_EXCEPT, excepts);
641 ++count_errors;
643 feexcp_nomask_test (flag_name, fe_exc);
644 feexcp_mask_test (flag_name, fe_exc);
649 static void
650 fe_single_test (const char *flag_name, int fe_exc)
652 feenv_nomask_test (flag_name, fe_exc, fesetenv);
653 feenv_mask_test (flag_name, fe_exc, fesetenv);
654 feenable_test (flag_name, fe_exc);
658 static void
659 feupdate_single_test (const char *flag_name, int fe_exc)
661 feenv_nomask_test (flag_name, fe_exc, feupdateenv);
662 feenv_mask_test (flag_name, fe_exc, feupdateenv);
664 #endif
667 static void
668 feenv_tests (void)
670 /* We might have some exceptions still set. */
671 feclearexcept (FE_ALL_EXCEPT);
673 #ifdef FE_DIVBYZERO
674 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
675 #endif
676 #ifdef FE_INVALID
677 fe_single_test ("FE_INVALID", FE_INVALID);
678 #endif
679 #ifdef FE_INEXACT
680 fe_single_test ("FE_INEXACT", FE_INEXACT);
681 #endif
682 #ifdef FE_UNDERFLOW
683 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
684 #endif
685 #ifdef FE_OVERFLOW
686 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
687 #endif
688 fesetenv (FE_DFL_ENV);
691 #if FE_ALL_EXCEPT
692 static void
693 feupdateenv_single_test (const char *test_name, int fe_exc, int exception)
695 char str[100];
696 fenv_t env;
697 int res;
699 snprintf (str, sizeof str, "feupdateenv %s and FL_DFL_ENV", test_name);
700 update_single_exc (str, FE_DFL_ENV, fe_exc, NO_EXC, exception);
702 feraiseexcept (FE_ALL_EXCEPT);
703 res = fegetenv (&env);
704 if (res != 0)
706 printf ("fegetenv failed: %d\n", res);
707 ++count_errors;
708 return;
711 snprintf (str, sizeof str, "feupdateenv %s and FE_ALL_EXCEPT", test_name);
712 update_single_exc (str, &env, ALL_EXC, ALL_EXC, exception);
714 #endif
716 static void
717 feupdateenv_tests (void)
719 /* We might have some exceptions still set. */
720 feclearexcept (FE_ALL_EXCEPT);
722 #ifdef FE_DIVBYZERO
723 feupdate_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
724 #endif
725 #ifdef FE_INVALID
726 feupdate_single_test ("FE_INVALID", FE_INVALID);
727 #endif
728 #ifdef FE_INEXACT
729 feupdate_single_test ("FE_INEXACT", FE_INEXACT);
730 #endif
731 #ifdef FE_UNDERFLOW
732 feupdate_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
733 #endif
734 #ifdef FE_OVERFLOW
735 feupdate_single_test ("FE_OVERFLOW", FE_OVERFLOW);
736 #endif
738 #ifdef FE_DIVBYZERO
739 feupdateenv_single_test ("DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
740 #endif
741 #ifdef FE_INVALID
742 feupdateenv_single_test ("INVALID", INVALID_EXC, FE_INVALID);
743 #endif
744 #ifdef FE_INEXACT
745 feupdateenv_single_test ("INEXACT", INEXACT_EXC, FE_INEXACT);
746 #endif
747 #ifdef FE_UNDERFLOW
748 feupdateenv_single_test ("UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
749 #endif
750 #ifdef FE_OVERFLOW
751 feupdateenv_single_test ("OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
752 #endif
754 feupdateenv (FE_DFL_ENV);
758 static void
759 feholdexcept_tests (void)
761 fenv_t saved, saved2;
762 int res;
764 feclearexcept (FE_ALL_EXCEPT);
765 fedisableexcept (FE_ALL_EXCEPT);
766 #ifdef FE_DIVBYZERO
767 feraiseexcept (FE_DIVBYZERO);
768 #endif
769 if (EXCEPTION_TESTS (float))
770 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
771 DIVBYZERO_EXC, 0);
772 res = feholdexcept (&saved);
773 if (res != 0)
775 printf ("feholdexcept failed: %d\n", res);
776 ++count_errors;
778 #if defined FE_TONEAREST && defined FE_TOWARDZERO
779 res = fesetround (FE_TOWARDZERO);
780 if (res != 0 && ROUNDING_TESTS (float, FE_TOWARDZERO))
782 printf ("fesetround failed: %d\n", res);
783 ++count_errors;
785 #endif
786 test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0);
787 #ifdef FE_INVALID
788 feraiseexcept (FE_INVALID);
789 if (EXCEPTION_TESTS (float))
790 test_exceptions ("feholdexcept_tests FE_INVALID test",
791 INVALID_EXC, 0);
792 #endif
793 res = feupdateenv (&saved);
794 if (res != 0)
796 printf ("feupdateenv failed: %d\n", res);
797 ++count_errors;
799 #if defined FE_TONEAREST && defined FE_TOWARDZERO
800 res = fegetround ();
801 if (res != FE_TONEAREST)
803 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
804 ++count_errors;
806 #endif
807 if (EXCEPTION_TESTS (float))
808 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
809 DIVBYZERO_EXC | INVALID_EXC, 0);
810 feclearexcept (FE_ALL_EXCEPT);
811 #ifdef FE_INVALID
812 feraiseexcept (FE_INVALID);
813 #endif
814 #if defined FE_TONEAREST && defined FE_UPWARD
815 res = fesetround (FE_UPWARD);
816 if (res != 0 && ROUNDING_TESTS (float, FE_UPWARD))
818 printf ("fesetround failed: %d\n", res);
819 ++count_errors;
821 #endif
822 res = feholdexcept (&saved2);
823 if (res != 0)
825 printf ("feholdexcept failed: %d\n", res);
826 ++count_errors;
828 #if defined FE_TONEAREST && defined FE_UPWARD
829 res = fesetround (FE_TONEAREST);
830 if (res != 0)
832 printf ("fesetround failed: %d\n", res);
833 ++count_errors;
835 #endif
836 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0);
837 #ifdef FE_INEXACT
838 feraiseexcept (FE_INEXACT);
839 if (EXCEPTION_TESTS (float))
840 test_exceptions ("feholdexcept_tests FE_INEXACT test",
841 INEXACT_EXC, 0);
842 #endif
843 res = feupdateenv (&saved2);
844 if (res != 0)
846 printf ("feupdateenv failed: %d\n", res);
847 ++count_errors;
849 #if defined FE_TONEAREST && defined FE_UPWARD
850 res = fegetround ();
851 if (res != FE_UPWARD && ROUNDING_TESTS (float, FE_UPWARD))
853 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
854 ++count_errors;
856 fesetround (FE_TONEAREST);
857 #endif
858 if (EXCEPTION_TESTS (float))
859 test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test",
860 INVALID_EXC | INEXACT_EXC, 0);
861 feclearexcept (FE_ALL_EXCEPT);
865 /* IEC 559 and ISO C99 define a default startup environment */
866 static void
867 initial_tests (void)
869 test_exceptions ("Initially all exceptions should be cleared",
870 NO_EXC, 0);
871 #ifdef FE_TONEAREST
872 test_rounding ("Rounding direction should be initialized to nearest",
873 FE_TONEAREST);
874 #endif
877 static int
878 do_test (void)
880 initial_tests ();
881 fe_tests ();
882 feenv_tests ();
883 feholdexcept_tests ();
884 feupdateenv_tests ();
886 if (count_errors)
888 printf ("\n%d errors occurred.\n", count_errors);
889 exit (1);
891 printf ("\n All tests passed successfully.\n");
892 return 0;
895 #include <support/test-driver.c>