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
)
234 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
235 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
239 fesetenv (FE_NOMASK_ENV
);
240 feraiseexcept (fe_exc
);
247 printf (" Fail: Could not fork.\n");
251 printf (" `fork' not implemented, test ignored.\n");
254 if (waitpid (pid
, &status
, 0) != pid
)
256 printf (" Fail: waitpid call failed.\n");
259 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
260 printf (" Pass: Process received SIGFPE.\n");
263 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
270 /* Test that program doesn't abort with default environment */
272 feenv_mask_test (const char *flag_name
, int fe_exc
)
277 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
278 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
282 fesetenv (FE_DFL_ENV
);
283 feraiseexcept (fe_exc
);
290 printf (" Fail: Could not fork.\n");
294 printf (" `fork' not implemented, test ignored.\n");
297 if (waitpid (pid
, &status
, 0) != pid
)
299 printf (" Fail: waitpid call failed.\n");
302 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
303 printf (" Pass: Process exited normally.\n");
306 printf (" Fail: Process exited abnormally with status %d.\n",
320 feenv_nomask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
321 feenv_mask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
324 feenv_nomask_test ("FE_INVALID", FE_INVALID
);
325 feenv_mask_test ("FE_INVALID", FE_INVALID
);
328 feenv_nomask_test ("FE_INEXACT", FE_INEXACT
);
329 feenv_mask_test ("FE_INEXACT", FE_INEXACT
);
332 feenv_nomask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
333 feenv_mask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
336 feenv_nomask_test ("FE_OVERFLOW", FE_OVERFLOW
);
337 feenv_mask_test ("FE_OVERFLOW", FE_OVERFLOW
);
339 fesetenv (FE_DFL_ENV
);
343 /* IEC 559 and ISO C 9X define a default startup environment */
347 test_exceptions ("Initially all exceptions should be cleared",
349 test_rounding ("Rounding direction should be initalized to nearest",
362 printf ("\n%d errors occured.\n", count_errors
);
365 printf ("\n All tests passed successfully.\n");