Add script to build many glibc configurations.
[glibc.git] / math / test-fenv.c
blob8f9f407e134c7668c25ff7551ea37d14ff4ca06b
1 /* Copyright (C) 1997-2016 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 */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE
24 #endif
26 #include <complex.h>
27 #include <math.h>
28 #include <float.h>
29 #include <fenv.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/wait.h>
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.
45 #define NO_EXC 0
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
51 #define ALL_EXC \
52 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
53 INVALID_EXC)
55 static int count_errors;
57 #if FE_ALL_EXCEPT
58 /* Test whether a given exception was raised. */
59 static void
60 test_single_exception (short int exception,
61 short int exc_flag,
62 fexcept_t fe_flag,
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);
69 else
71 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
72 ++count_errors;
75 else
77 if (fetestexcept (fe_flag))
79 printf (" Fail: Exception \"%s\" is set\n", flag_name);
80 ++count_errors;
82 else
84 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
88 #endif
90 static void
91 test_exceptions (const char *test_name, short int exception,
92 int ignore_inexact)
94 printf ("Test: %s\n", test_name);
95 #ifdef FE_DIVBYZERO
96 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
97 "DIVBYZERO");
98 #endif
99 #ifdef FE_INVALID
100 test_single_exception (exception, INVALID_EXC, FE_INVALID,
101 "INVALID");
102 #endif
103 #ifdef FE_INEXACT
104 if (!ignore_inexact)
105 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
106 "INEXACT");
107 #endif
108 #ifdef FE_UNDERFLOW
109 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
110 "UNDERFLOW");
111 #endif
112 #ifdef FE_OVERFLOW
113 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
114 "OVERFLOW");
115 #endif
118 static void
119 print_rounding (int rounding)
122 switch (rounding)
124 #ifdef FE_TONEAREST
125 case FE_TONEAREST:
126 printf ("TONEAREST");
127 break;
128 #endif
129 #ifdef FE_UPWARD
130 case FE_UPWARD:
131 printf ("UPWARD");
132 break;
133 #endif
134 #ifdef FE_DOWNWARD
135 case FE_DOWNWARD:
136 printf ("DOWNWARD");
137 break;
138 #endif
139 #ifdef FE_TOWARDZERO
140 case FE_TOWARDZERO:
141 printf ("TOWARDZERO");
142 break;
143 #endif
145 printf (".\n");
149 static void
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);
160 else
162 ++count_errors;
163 printf (" Fail: Rounding mode is ");
164 print_rounding (curr_rounding);
169 #if FE_ALL_EXCEPT
170 static void
171 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
173 char str[200];
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);
201 #endif
203 static void
204 fe_tests (void)
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",
209 NO_EXC, 0);
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",
214 ALL_EXC, 0);
215 feclearexcept (FE_ALL_EXCEPT);
217 #ifdef FE_DIVBYZERO
218 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
219 #endif
220 #ifdef FE_INVALID
221 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
222 #endif
223 #ifdef FE_INEXACT
224 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
225 #endif
226 #ifdef FE_UNDERFLOW
227 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
228 #endif
229 #ifdef FE_OVERFLOW
230 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
231 #endif
234 #if FE_ALL_EXCEPT
235 /* Test that program aborts with no masked interrupts */
236 static void
237 feenv_nomask_test (const char *flag_name, int fe_exc)
239 # if defined FE_NOMASK_ENV
240 int status;
241 pid_t pid;
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");
247 return;
250 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
251 printf (" when feraiseexcept (%s) is called.\n", flag_name);
252 pid = fork ();
253 if (pid == 0)
255 # ifdef RLIMIT_CORE
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);
261 # endif
263 fesetenv (FE_NOMASK_ENV);
264 feraiseexcept (fe_exc);
265 exit (2);
267 else if (pid < 0)
269 if (errno != ENOSYS)
271 printf (" Fail: Could not fork.\n");
272 ++count_errors;
274 else
275 printf (" `fork' not implemented, test ignored.\n");
277 else {
278 if (waitpid (pid, &status, 0) != pid)
280 printf (" Fail: waitpid call failed.\n");
281 ++count_errors;
283 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
284 printf (" Pass: Process received SIGFPE.\n");
285 else
287 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
288 status);
289 ++count_errors;
292 # endif
295 /* Test that program doesn't abort with default environment */
296 static void
297 feenv_mask_test (const char *flag_name, int fe_exc)
299 int status;
300 pid_t pid;
302 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
303 printf (" when feraiseexcept (%s) is called.\n", flag_name);
304 pid = fork ();
305 if (pid == 0)
307 #ifdef RLIMIT_CORE
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);
313 #endif
315 fesetenv (FE_DFL_ENV);
316 feraiseexcept (fe_exc);
317 exit (2);
319 else if (pid < 0)
321 if (errno != ENOSYS)
323 printf (" Fail: Could not fork.\n");
324 ++count_errors;
326 else
327 printf (" `fork' not implemented, test ignored.\n");
329 else {
330 if (waitpid (pid, &status, 0) != pid)
332 printf (" Fail: waitpid call failed.\n");
333 ++count_errors;
335 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
336 printf (" Pass: Process exited normally.\n");
337 else
339 printf (" Fail: Process exited abnormally with status %d.\n",
340 status);
341 ++count_errors;
346 /* Test that program aborts with no masked interrupts */
347 static void
348 feexcp_nomask_test (const char *flag_name, int fe_exc)
350 int status;
351 pid_t pid;
353 if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1)
355 printf ("Test: not testing feenableexcept, it isn't implemented.\n");
356 return;
359 printf ("Test: after feenableexcept (%s) processes will abort\n",
360 flag_name);
361 printf (" when feraiseexcept (%s) is called.\n", flag_name);
362 pid = fork ();
363 if (pid == 0)
365 #ifdef RLIMIT_CORE
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);
371 #endif
373 fedisableexcept (FE_ALL_EXCEPT);
374 feenableexcept (fe_exc);
375 feraiseexcept (fe_exc);
376 exit (2);
378 else if (pid < 0)
380 if (errno != ENOSYS)
382 printf (" Fail: Could not fork.\n");
383 ++count_errors;
385 else
386 printf (" `fork' not implemented, test ignored.\n");
388 else {
389 if (waitpid (pid, &status, 0) != pid)
391 printf (" Fail: waitpid call failed.\n");
392 ++count_errors;
394 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
395 printf (" Pass: Process received SIGFPE.\n");
396 else
398 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
399 status);
400 ++count_errors;
405 /* Test that program doesn't abort with exception. */
406 static void
407 feexcp_mask_test (const char *flag_name, int fe_exc)
409 int status;
410 int exception;
411 pid_t pid;
413 printf ("Test: after fedisableexcept (%s) processes will not abort\n",
414 flag_name);
415 printf (" when feraiseexcept (%s) is called.\n", flag_name);
416 pid = fork ();
417 if (pid == 0)
419 #ifdef RLIMIT_CORE
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);
425 #endif
426 feenableexcept (FE_ALL_EXCEPT);
427 exception = fe_exc;
428 #ifdef FE_INEXACT
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
432 overflow. */
433 # ifdef FE_OVERFLOW
434 if (fe_exc & FE_OVERFLOW)
435 exception |= FE_INEXACT;
436 # endif
437 # ifdef FE_UNDERFLOW
438 if (fe_exc & FE_UNDERFLOW)
439 exception |= FE_INEXACT;
440 # endif
441 #endif
442 fedisableexcept (exception);
443 feraiseexcept (fe_exc);
444 exit (2);
446 else if (pid < 0)
448 if (errno != ENOSYS)
450 printf (" Fail: Could not fork.\n");
451 ++count_errors;
453 else
454 printf (" `fork' not implemented, test ignored.\n");
456 else {
457 if (waitpid (pid, &status, 0) != pid)
459 printf (" Fail: waitpid call failed.\n");
460 ++count_errors;
462 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
463 printf (" Pass: Process exited normally.\n");
464 else
466 printf (" Fail: Process exited abnormally with status %d.\n",
467 status);
468 ++count_errors;
474 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
475 static void
476 feenable_test (const char *flag_name, int fe_exc)
478 int excepts;
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");
486 ++count_errors;
487 /* If this fails, the other tests don't make sense. */
488 return;
490 excepts = fegetexcept ();
491 if (excepts != 0)
493 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
494 flag_name, excepts);
495 ++count_errors;
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");
501 return;
503 if (excepts == -1)
505 printf ("Test: feenableexcept (%s) failed\n", flag_name);
506 ++count_errors;
507 return;
509 if (excepts != 0)
511 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
512 flag_name, excepts);
513 ++count_errors;
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);
521 ++count_errors;
524 /* And now disable the exception again. */
525 excepts = fedisableexcept (fe_exc);
526 if (excepts == -1)
528 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
529 ++count_errors;
530 return;
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);
536 ++count_errors;
539 excepts = fegetexcept ();
540 if (excepts != 0)
542 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
543 flag_name, excepts);
544 ++count_errors;
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");
551 ++count_errors;
552 /* If this fails, the other tests don't make sense. */
553 return;
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);
561 ++count_errors;
564 excepts = fedisableexcept (fe_exc);
565 if (excepts == -1)
567 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
568 ++count_errors;
569 return;
571 if (excepts != FE_ALL_EXCEPT)
573 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
574 flag_name, excepts);
575 ++count_errors;
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);
583 ++count_errors;
586 /* And now enable the exception again. */
587 excepts = feenableexcept (fe_exc);
588 if (excepts == -1)
590 printf ("Test: feenableexcept (%s) failed\n", flag_name);
591 ++count_errors;
592 return;
594 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
596 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
597 flag_name, excepts);
598 ++count_errors;
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);
606 ++count_errors;
608 feexcp_nomask_test (flag_name, fe_exc);
609 feexcp_mask_test (flag_name, fe_exc);
614 static void
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);
621 #endif
624 static void
625 feenv_tests (void)
627 /* We might have some exceptions still set. */
628 feclearexcept (FE_ALL_EXCEPT);
630 #ifdef FE_DIVBYZERO
631 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
632 #endif
633 #ifdef FE_INVALID
634 fe_single_test ("FE_INVALID", FE_INVALID);
635 #endif
636 #ifdef FE_INEXACT
637 fe_single_test ("FE_INEXACT", FE_INEXACT);
638 #endif
639 #ifdef FE_UNDERFLOW
640 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
641 #endif
642 #ifdef FE_OVERFLOW
643 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
644 #endif
645 fesetenv (FE_DFL_ENV);
649 static void
650 feholdexcept_tests (void)
652 fenv_t saved, saved2;
653 int res;
655 feclearexcept (FE_ALL_EXCEPT);
656 fedisableexcept (FE_ALL_EXCEPT);
657 #ifdef FE_DIVBYZERO
658 feraiseexcept (FE_DIVBYZERO);
659 #endif
660 test_exceptions ("feholdexcept_tests FE_DIVBYZERO test",
661 DIVBYZERO_EXC, 0);
662 res = feholdexcept (&saved);
663 if (res != 0)
665 printf ("feholdexcept failed: %d\n", res);
666 ++count_errors;
668 #if defined FE_TONEAREST && defined FE_TOWARDZERO
669 res = fesetround (FE_TOWARDZERO);
670 if (res != 0)
672 printf ("fesetround failed: %d\n", res);
673 ++count_errors;
675 #endif
676 test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0);
677 #ifdef FE_INVALID
678 feraiseexcept (FE_INVALID);
679 test_exceptions ("feholdexcept_tests FE_INVALID test",
680 INVALID_EXC, 0);
681 #endif
682 res = feupdateenv (&saved);
683 if (res != 0)
685 printf ("feupdateenv failed: %d\n", res);
686 ++count_errors;
688 #if defined FE_TONEAREST && defined FE_TOWARDZERO
689 res = fegetround ();
690 if (res != FE_TONEAREST)
692 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
693 ++count_errors;
695 #endif
696 test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test",
697 DIVBYZERO_EXC | INVALID_EXC, 0);
698 feclearexcept (FE_ALL_EXCEPT);
699 #ifdef FE_INVALID
700 feraiseexcept (FE_INVALID);
701 #endif
702 #if defined FE_TONEAREST && defined FE_UPWARD
703 res = fesetround (FE_UPWARD);
704 if (res != 0)
706 printf ("fesetround failed: %d\n", res);
707 ++count_errors;
709 #endif
710 res = feholdexcept (&saved2);
711 if (res != 0)
713 printf ("feholdexcept failed: %d\n", res);
714 ++count_errors;
716 #if defined FE_TONEAREST && defined FE_UPWARD
717 res = fesetround (FE_TONEAREST);
718 if (res != 0)
720 printf ("fesetround failed: %d\n", res);
721 ++count_errors;
723 #endif
724 test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0);
725 #ifdef FE_INEXACT
726 feraiseexcept (FE_INEXACT);
727 test_exceptions ("feholdexcept_tests FE_INEXACT test",
728 INEXACT_EXC, 0);
729 #endif
730 res = feupdateenv (&saved2);
731 if (res != 0)
733 printf ("feupdateenv failed: %d\n", res);
734 ++count_errors;
736 #if defined FE_TONEAREST && defined FE_UPWARD
737 res = fegetround ();
738 if (res != FE_UPWARD)
740 printf ("feupdateenv didn't restore rounding mode: %d\n", res);
741 ++count_errors;
743 fesetround (FE_TONEAREST);
744 #endif
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 */
752 static void
753 initial_tests (void)
755 test_exceptions ("Initially all exceptions should be cleared",
756 NO_EXC, 0);
757 #ifdef FE_TONEAREST
758 test_rounding ("Rounding direction should be initalized to nearest",
759 FE_TONEAREST);
760 #endif
764 main (void)
766 initial_tests ();
767 fe_tests ();
768 feenv_tests ();
769 feholdexcept_tests ();
771 if (count_errors)
773 printf ("\n%d errors occurred.\n", count_errors);
774 exit (1);
776 printf ("\n All tests passed successfully.\n");
777 return 0;