[!__ASSEMBLER__] (declare_symbol_1): Add missing comma to .type directive.
[glibc.git] / math / test-fenv.c
blob1fcefc732a2751dc5c9d3a23827733530cde6f3b
1 /* Copyright (C) 1997, 1998, 2000, 2001 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Tests for ISO C99 7.6: Floating-point environment */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE
25 #endif
27 #include <complex.h>
28 #include <math.h>
29 #include <float.h>
30 #include <fenv.h>
32 #include <errno.h>
33 #include <signal.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #include <sys/resource.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 /* Test whether a given exception was raised. */
58 static void
59 test_single_exception (short int exception,
60 short int exc_flag,
61 fexcept_t fe_flag,
62 const char *flag_name)
64 if (exception & exc_flag)
66 if (fetestexcept (fe_flag))
67 printf (" Pass: Exception \"%s\" is set\n", flag_name);
68 else
70 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
71 ++count_errors;
74 else
76 if (fetestexcept (fe_flag))
78 printf (" Fail: Exception \"%s\" is set\n", flag_name);
79 ++count_errors;
81 else
83 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
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 static void
168 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
170 char str[200];
171 /* The standard allows the inexact exception to be set together with the
172 underflow and overflow exceptions. So ignore the inexact flag if the
173 others are raised. */
174 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
176 strcpy (str, test_name);
177 strcat (str, ": set flag, with rest not set");
178 feclearexcept (FE_ALL_EXCEPT);
179 feraiseexcept (exception);
180 test_exceptions (str, fe_exc, ignore_inexact);
182 strcpy (str, test_name);
183 strcat (str, ": clear flag, rest also unset");
184 feclearexcept (exception);
185 test_exceptions (str, NO_EXC, ignore_inexact);
187 strcpy (str, test_name);
188 strcat (str, ": set flag, with rest set");
189 feraiseexcept (FE_ALL_EXCEPT ^ exception);
190 feraiseexcept (exception);
191 test_exceptions (str, ALL_EXC, 0);
193 strcpy (str, test_name);
194 strcat (str, ": clear flag, leave rest set");
195 feclearexcept (exception);
196 test_exceptions (str, ALL_EXC ^ fe_exc, 0);
199 static void
200 fe_tests (void)
202 /* clear all exceptions and test if all are cleared */
203 feclearexcept (FE_ALL_EXCEPT);
204 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
205 NO_EXC, 0);
207 /* raise all exceptions and test if all are raised */
208 feraiseexcept (FE_ALL_EXCEPT);
209 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
210 ALL_EXC, 0);
211 feclearexcept (FE_ALL_EXCEPT);
213 #ifdef FE_DIVBYZERO
214 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
215 #endif
216 #ifdef FE_INVALID
217 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
218 #endif
219 #ifdef FE_INEXACT
220 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
221 #endif
222 #ifdef FE_UNDERFLOW
223 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
224 #endif
225 #ifdef FE_OVERFLOW
226 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
227 #endif
230 /* Test that program aborts with no masked interrupts */
231 static void
232 feenv_nomask_test (const char *flag_name, int fe_exc)
234 #if defined FE_NOMASK_ENV
235 int status;
236 pid_t pid;
237 fenv_t saved;
239 fegetenv (&saved);
240 errno = 0;
241 fesetenv (FE_NOMASK_ENV);
242 status = errno;
243 fesetenv (&saved);
244 if (status == ENOSYS)
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 printf ("Test: after fedisable (%s) processes will abort\n", flag_name);
354 printf (" when feraiseexcept (%s) is called.\n", flag_name);
355 pid = fork ();
356 if (pid == 0)
358 #ifdef RLIMIT_CORE
359 /* Try to avoid dumping core. */
360 struct rlimit core_limit;
361 core_limit.rlim_cur = 0;
362 core_limit.rlim_max = 0;
363 setrlimit (RLIMIT_CORE, &core_limit);
364 #endif
366 fedisableexcept (FE_ALL_EXCEPT);
367 feenableexcept (fe_exc);
368 feraiseexcept (fe_exc);
369 exit (2);
371 else if (pid < 0)
373 if (errno != ENOSYS)
375 printf (" Fail: Could not fork.\n");
376 ++count_errors;
378 else
379 printf (" `fork' not implemented, test ignored.\n");
381 else {
382 if (waitpid (pid, &status, 0) != pid)
384 printf (" Fail: waitpid call failed.\n");
385 ++count_errors;
387 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
388 printf (" Pass: Process received SIGFPE.\n");
389 else
391 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
392 status);
393 ++count_errors;
398 /* Test that program doesn't abort with exception. */
399 static void
400 feexcp_mask_test (const char *flag_name, int fe_exc)
402 int status;
403 int exception;
404 pid_t pid;
406 printf ("Test: after fedisable (%s) processes will not abort\n", flag_name);
407 printf (" when feraiseexcept (%s) is called.\n", flag_name);
408 pid = fork ();
409 if (pid == 0)
411 #ifdef RLIMIT_CORE
412 /* Try to avoid dumping core. */
413 struct rlimit core_limit;
414 core_limit.rlim_cur = 0;
415 core_limit.rlim_max = 0;
416 setrlimit (RLIMIT_CORE, &core_limit);
417 #endif
418 feenableexcept (FE_ALL_EXCEPT);
419 exception = fe_exc;
420 #ifdef FE_INEXACT
421 /* The standard allows the inexact exception to be set together with the
422 underflow and overflow exceptions. So add FE_INEXACT to the set of
423 exceptions to be disabled if we will be raising underflow or
424 overflow. */
425 # ifdef FE_OVERFLOW
426 if (fe_exc & FE_OVERFLOW)
427 exception |= FE_INEXACT;
428 # endif
429 # ifdef FE_UNDERFLOW
430 if (fe_exc & FE_UNDERFLOW)
431 exception |= FE_INEXACT;
432 # endif
433 #endif
434 fedisableexcept (exception);
435 feraiseexcept (fe_exc);
436 exit (2);
438 else if (pid < 0)
440 if (errno != ENOSYS)
442 printf (" Fail: Could not fork.\n");
443 ++count_errors;
445 else
446 printf (" `fork' not implemented, test ignored.\n");
448 else {
449 if (waitpid (pid, &status, 0) != pid)
451 printf (" Fail: waitpid call failed.\n");
452 ++count_errors;
454 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
455 printf (" Pass: Process exited normally.\n");
456 else
458 printf (" Fail: Process exited abnormally with status %d.\n",
459 status);
460 ++count_errors;
466 /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
467 static void
468 feenable_test (const char *flag_name, int fe_exc)
470 int excepts;
473 printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
475 /* First disable all exceptions. */
476 if (fedisableexcept (FE_ALL_EXCEPT) == -1)
478 printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
479 ++count_errors;
480 /* If this fails, the other tests don't make sense. */
481 return;
483 excepts = fegetexcept ();
484 if (excepts != 0)
486 printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
487 flag_name, excepts);
488 ++count_errors;
491 excepts = feenableexcept (fe_exc);
492 if (excepts == -1)
494 printf ("Test: feenableexcept (%s) failed\n", flag_name);
495 ++count_errors;
496 return;
498 if (excepts != 0)
500 printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
501 flag_name, excepts);
502 ++count_errors;
505 excepts = fegetexcept ();
506 if (excepts != fe_exc)
508 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
509 flag_name, fe_exc, excepts);
510 ++count_errors;
513 /* And now disable the exception again. */
514 excepts = fedisableexcept (fe_exc);
515 if (excepts == -1)
517 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
518 ++count_errors;
519 return;
521 if (excepts != fe_exc)
523 printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
524 flag_name, fe_exc, excepts);
525 ++count_errors;
528 excepts = fegetexcept ();
529 if (excepts != 0)
531 printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
532 flag_name, excepts);
533 ++count_errors;
536 /* Now the other way round: Enable all exceptions and disable just this one. */
537 if (feenableexcept (FE_ALL_EXCEPT) == -1)
539 printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
540 ++count_errors;
541 /* If this fails, the other tests don't make sense. */
542 return;
545 excepts = fegetexcept ();
546 if (excepts != FE_ALL_EXCEPT)
548 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
549 flag_name, FE_ALL_EXCEPT, excepts);
550 ++count_errors;
553 excepts = fedisableexcept (fe_exc);
554 if (excepts == -1)
556 printf ("Test: fedisableexcept (%s) failed\n", flag_name);
557 ++count_errors;
558 return;
560 if (excepts != FE_ALL_EXCEPT)
562 printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
563 flag_name, excepts);
564 ++count_errors;
567 excepts = fegetexcept ();
568 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
570 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
571 flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
572 ++count_errors;
575 /* And now enable the exception again. */
576 excepts = feenableexcept (fe_exc);
577 if (excepts == -1)
579 printf ("Test: feenableexcept (%s) failed\n", flag_name);
580 ++count_errors;
581 return;
583 if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
585 printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
586 flag_name, excepts);
587 ++count_errors;
590 excepts = fegetexcept ();
591 if (excepts != FE_ALL_EXCEPT)
593 printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
594 flag_name, FE_ALL_EXCEPT, excepts);
595 ++count_errors;
597 feexcp_nomask_test (flag_name, fe_exc);
598 feexcp_mask_test (flag_name, fe_exc);
603 static void
604 fe_single_test (const char *flag_name, int fe_exc)
606 feenv_nomask_test (flag_name, fe_exc);
607 feenv_mask_test (flag_name, fe_exc);
608 feenable_test (flag_name, fe_exc);
612 static void
613 feenv_tests (void)
615 /* We might have some exceptions still set. */
616 feclearexcept (FE_ALL_EXCEPT);
618 #ifdef FE_DIVBYZERO
619 fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
620 #endif
621 #ifdef FE_INVALID
622 fe_single_test ("FE_INVALID", FE_INVALID);
623 #endif
624 #ifdef FE_INEXACT
625 fe_single_test ("FE_INEXACT", FE_INEXACT);
626 #endif
627 #ifdef FE_UNDERFLOW
628 fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
629 #endif
630 #ifdef FE_OVERFLOW
631 fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
632 #endif
633 fesetenv (FE_DFL_ENV);
637 /* IEC 559 and ISO C99 define a default startup environment */
638 static void
639 initial_tests (void)
641 test_exceptions ("Initially all exceptions should be cleared",
642 NO_EXC, 0);
643 #ifdef FE_TONEAREST
644 test_rounding ("Rounding direction should be initalized to nearest",
645 FE_TONEAREST);
646 #endif
650 main (void)
652 initial_tests ();
653 fe_tests ();
654 feenv_tests ();
656 if (count_errors)
658 printf ("\n%d errors occurred.\n", count_errors);
659 exit (1);
661 printf ("\n All tests passed successfully.\n");
662 return 0;