1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Tests for ISO C 9X 7.6: Floating-point environment */
39 #include <sys/resource.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;
57 /* Test whether a given exception was raised. */
59 test_single_exception (short int exception,
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);
70 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
76 if (fetestexcept (fe_flag))
78 printf (" Fail: Exception \"%s\" is set\n", flag_name);
83 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
89 test_exceptions (const char *test_name, short int exception,
92 printf ("Test: %s\n", test_name);
94 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
98 test_single_exception (exception, INVALID_EXC, FE_INVALID,
103 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
107 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
111 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
117 print_rounding (int rounding)
123 printf ("TONEAREST");
138 printf ("TOWARDZERO");
147 test_rounding (const char *test_name, int rounding_mode)
149 int curr_rounding = fegetround ();
151 printf ("Test: %s\n", test_name);
152 if (curr_rounding == rounding_mode)
154 printf (" Pass: Rounding mode is ");
155 print_rounding (curr_rounding);
159 printf (" Fail: Rounding mode is ");
160 print_rounding (curr_rounding);
166 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
169 /* The standard allows the inexact exception to be set together with the
170 underflow and overflow exceptions. So ignore the inexact flag if the
171 others are raised. */
172 int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0;
174 strcpy (str, test_name);
175 strcat (str, ": set flag, with rest not set");
176 feclearexcept (FE_ALL_EXCEPT);
177 feraiseexcept (exception);
178 test_exceptions (str, fe_exc, ignore_inexact);
180 strcpy (str, test_name);
181 strcat (str, ": clear flag, rest also unset");
182 feclearexcept (exception);
183 test_exceptions (str, NO_EXC, ignore_inexact);
185 strcpy (str, test_name);
186 strcat (str, ": set flag, with rest set");
187 feraiseexcept (FE_ALL_EXCEPT ^ exception);
188 feraiseexcept (exception);
189 test_exceptions (str, ALL_EXC, 0);
191 strcpy (str, test_name);
192 strcat (str, ": clear flag, leave rest set");
193 feclearexcept (exception);
194 test_exceptions (str, ALL_EXC ^ fe_exc, 0);
200 /* clear all exceptions and test if all are cleared */
201 feclearexcept (FE_ALL_EXCEPT);
202 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
205 /* raise all exceptions and test if all are raised */
206 feraiseexcept (FE_ALL_EXCEPT);
207 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
209 feclearexcept (FE_ALL_EXCEPT);
212 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
215 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
218 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
221 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
224 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
228 /* Test that program aborts with no masked interrupts */
230 feenv_nomask_test (const char *flag_name, int fe_exc)
232 #if defined FE_NOMASK_ENV
239 fesetenv (FE_NOMASK_ENV);
242 if (status == ENOSYS)
244 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
248 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
249 printf (" when feraiseexcept (%s) is called.\n", flag_name);
254 /* Try to avoid dumping core. */
255 struct rlimit core_limit;
256 core_limit.rlim_cur = 0;
257 core_limit.rlim_max = 0;
258 setrlimit (RLIMIT_CORE, &core_limit);
261 fesetenv (FE_NOMASK_ENV);
262 feraiseexcept (fe_exc);
269 printf (" Fail: Could not fork.\n");
273 printf (" `fork' not implemented, test ignored.\n");
276 if (waitpid (pid, &status, 0) != pid)
278 printf (" Fail: waitpid call failed.\n");
281 else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
282 printf (" Pass: Process received SIGFPE.\n");
285 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
293 /* Test that program doesn't abort with default environment */
295 feenv_mask_test (const char *flag_name, int fe_exc)
300 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
301 printf (" when feraiseexcept (%s) is called.\n", flag_name);
306 /* Try to avoid dumping core. */
307 struct rlimit core_limit;
308 core_limit.rlim_cur = 0;
309 core_limit.rlim_max = 0;
310 setrlimit (RLIMIT_CORE, &core_limit);
313 fesetenv (FE_DFL_ENV);
314 feraiseexcept (fe_exc);
321 printf (" Fail: Could not fork.\n");
325 printf (" `fork' not implemented, test ignored.\n");
328 if (waitpid (pid, &status, 0) != pid)
330 printf (" Fail: waitpid call failed.\n");
333 else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
334 printf (" Pass: Process exited normally.\n");
337 printf (" Fail: Process exited abnormally with status %d.\n",
351 feenv_nomask_test ("FE_DIVBYZERO", FE_DIVBYZERO);
352 feenv_mask_test ("FE_DIVBYZERO", FE_DIVBYZERO);
355 feenv_nomask_test ("FE_INVALID", FE_INVALID);
356 feenv_mask_test ("FE_INVALID", FE_INVALID);
359 feenv_nomask_test ("FE_INEXACT", FE_INEXACT);
360 feenv_mask_test ("FE_INEXACT", FE_INEXACT);
363 feenv_nomask_test ("FE_UNDERFLOW", FE_UNDERFLOW);
364 feenv_mask_test ("FE_UNDERFLOW", FE_UNDERFLOW);
367 feenv_nomask_test ("FE_OVERFLOW", FE_OVERFLOW);
368 feenv_mask_test ("FE_OVERFLOW", FE_OVERFLOW);
370 fesetenv (FE_DFL_ENV);
374 /* IEC 559 and ISO C 9X define a default startup environment */
378 test_exceptions ("Initially all exceptions should be cleared",
380 test_rounding ("Rounding direction should be initalized to nearest",
393 printf ("\n%d errors occured.\n", count_errors);
396 printf ("\n All tests passed successfully.\n");