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 */
41 Since not all architectures might define all exceptions, we define
42 a private set and map accordingly.
45 #define INEXACT_EXC 0x1
46 #define DIVBYZERO_EXC 0x2
47 #define UNDERFLOW_EXC 0x04
48 #define OVERFLOW_EXC 0x08
49 #define INVALID_EXC 0x10
51 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
54 static int count_errors
;
56 /* Test whether a given exception was raised. */
58 test_single_exception (short int exception
,
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
);
69 printf (" Fail: Exception \"%s\" is not set\n", flag_name
);
75 if (fetestexcept (fe_flag
))
77 printf (" Fail: Exception \"%s\" is set\n", flag_name
);
82 printf (" Pass: Exception \"%s\" is not set\n", flag_name
);
88 test_exceptions (const char *test_name
, short int exception
,
91 printf ("Test: %s\n", test_name
);
93 test_single_exception (exception
, DIVBYZERO_EXC
, FE_DIVBYZERO
,
97 test_single_exception (exception
, INVALID_EXC
, FE_INVALID
,
102 test_single_exception (exception
, INEXACT_EXC
, FE_INEXACT
,
106 test_single_exception (exception
, UNDERFLOW_EXC
, FE_UNDERFLOW
,
110 test_single_exception (exception
, OVERFLOW_EXC
, FE_OVERFLOW
,
116 print_rounding (int rounding
)
122 printf ("TONEAREST");
137 printf ("TOWARDZERO");
146 test_rounding (const char *test_name
, int rounding_mode
)
148 int curr_rounding
= fegetround ();
150 printf ("Test: %s\n", test_name
);
151 if (curr_rounding
== rounding_mode
)
153 printf (" Pass: Rounding mode is ");
154 print_rounding (curr_rounding
);
158 printf (" Fail: Rounding mode is ");
159 print_rounding (curr_rounding
);
165 set_single_exc (const char *test_name
, int fe_exc
, fexcept_t exception
)
168 /* The standard allows the inexact exception to be set together with the
169 underflow and overflow exceptions. So ignore the inexact flag if the
170 others are raised. */
171 int ignore_inexact
= (fe_exc
& (UNDERFLOW_EXC
| OVERFLOW_EXC
)) != 0;
173 strcpy (str
, test_name
);
174 strcat (str
, ": set flag, with rest not set");
175 feclearexcept (FE_ALL_EXCEPT
);
176 feraiseexcept (exception
);
177 test_exceptions (str
, fe_exc
, ignore_inexact
);
179 strcpy (str
, test_name
);
180 strcat (str
, ": clear flag, rest also unset");
181 feclearexcept (exception
);
182 test_exceptions (str
, NO_EXC
, ignore_inexact
);
184 strcpy (str
, test_name
);
185 strcat (str
, ": set flag, with rest set");
186 feraiseexcept (FE_ALL_EXCEPT
^ exception
);
187 feraiseexcept (exception
);
188 test_exceptions (str
, ALL_EXC
, 0);
190 strcpy (str
, test_name
);
191 strcat (str
, ": clear flag, leave rest set");
192 feclearexcept (exception
);
193 test_exceptions (str
, ALL_EXC
^ fe_exc
, 0);
199 /* clear all exceptions and test if all are cleared */
200 feclearexcept (FE_ALL_EXCEPT
);
201 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
204 /* raise all exceptions and test if all are raised */
205 feraiseexcept (FE_ALL_EXCEPT
);
206 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
208 feclearexcept (FE_ALL_EXCEPT
);
211 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC
, FE_DIVBYZERO
);
214 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC
, FE_INVALID
);
217 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC
, FE_INEXACT
);
220 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC
, FE_UNDERFLOW
);
223 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC
, FE_OVERFLOW
);
227 /* Test that program aborts with no masked interrupts */
229 feenv_nomask_test (const char *flag_name
, int fe_exc
)
231 #if defined FE_NOMASK_ENV
238 fesetenv (FE_NOMASK_ENV
);
241 if (status
== ENOSYS
)
243 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
247 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
248 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
252 fesetenv (FE_NOMASK_ENV
);
253 feraiseexcept (fe_exc
);
260 printf (" Fail: Could not fork.\n");
264 printf (" `fork' not implemented, test ignored.\n");
267 if (waitpid (pid
, &status
, 0) != pid
)
269 printf (" Fail: waitpid call failed.\n");
272 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
273 printf (" Pass: Process received SIGFPE.\n");
276 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
284 /* Test that program doesn't abort with default environment */
286 feenv_mask_test (const char *flag_name
, int fe_exc
)
291 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
292 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
296 fesetenv (FE_DFL_ENV
);
297 feraiseexcept (fe_exc
);
304 printf (" Fail: Could not fork.\n");
308 printf (" `fork' not implemented, test ignored.\n");
311 if (waitpid (pid
, &status
, 0) != pid
)
313 printf (" Fail: waitpid call failed.\n");
316 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
317 printf (" Pass: Process exited normally.\n");
320 printf (" Fail: Process exited abnormally with status %d.\n",
334 feenv_nomask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
335 feenv_mask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
338 feenv_nomask_test ("FE_INVALID", FE_INVALID
);
339 feenv_mask_test ("FE_INVALID", FE_INVALID
);
342 feenv_nomask_test ("FE_INEXACT", FE_INEXACT
);
343 feenv_mask_test ("FE_INEXACT", FE_INEXACT
);
346 feenv_nomask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
347 feenv_mask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
350 feenv_nomask_test ("FE_OVERFLOW", FE_OVERFLOW
);
351 feenv_mask_test ("FE_OVERFLOW", FE_OVERFLOW
);
353 fesetenv (FE_DFL_ENV
);
357 /* IEC 559 and ISO C 9X define a default startup environment */
361 test_exceptions ("Initially all exceptions should be cleared",
363 test_rounding ("Rounding direction should be initalized to nearest",
376 printf ("\n%d errors occured.\n", count_errors
);
379 printf ("\n All tests passed successfully.\n");