1 /* Copyright (C) 1997, 1998 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 C99 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
)
124 printf ("TONEAREST");
139 printf ("TOWARDZERO");
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
);
161 printf (" Fail: Rounding mode is ");
162 print_rounding (curr_rounding
);
168 set_single_exc (const char *test_name
, int fe_exc
, fexcept_t exception
)
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);
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",
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",
211 feclearexcept (FE_ALL_EXCEPT
);
214 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC
, FE_DIVBYZERO
);
217 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC
, FE_INVALID
);
220 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC
, FE_INEXACT
);
223 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC
, FE_UNDERFLOW
);
226 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC
, FE_OVERFLOW
);
230 /* Test that program aborts with no masked interrupts */
232 feenv_nomask_test (const char *flag_name
, int fe_exc
)
234 #if defined FE_NOMASK_ENV
241 fesetenv (FE_NOMASK_ENV
);
244 if (status
== ENOSYS
)
246 printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
250 printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n");
251 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
263 fesetenv (FE_NOMASK_ENV
);
264 feraiseexcept (fe_exc
);
271 printf (" Fail: Could not fork.\n");
275 printf (" `fork' not implemented, test ignored.\n");
278 if (waitpid (pid
, &status
, 0) != pid
)
280 printf (" Fail: waitpid call failed.\n");
283 else if (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGFPE
)
284 printf (" Pass: Process received SIGFPE.\n");
287 printf (" Fail: Process didn't receive signal and exited with status %d.\n",
295 /* Test that program doesn't abort with default environment */
297 feenv_mask_test (const char *flag_name
, int fe_exc
)
302 printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n");
303 printf (" when feraiseexcept (%s) is called.\n", flag_name
);
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
);
315 fesetenv (FE_DFL_ENV
);
316 feraiseexcept (fe_exc
);
323 printf (" Fail: Could not fork.\n");
327 printf (" `fork' not implemented, test ignored.\n");
330 if (waitpid (pid
, &status
, 0) != pid
)
332 printf (" Fail: waitpid call failed.\n");
335 else if (WIFEXITED (status
) && WEXITSTATUS (status
) == 2)
336 printf (" Pass: Process exited normally.\n");
339 printf (" Fail: Process exited abnormally with status %d.\n",
353 feenv_nomask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
354 feenv_mask_test ("FE_DIVBYZERO", FE_DIVBYZERO
);
357 feenv_nomask_test ("FE_INVALID", FE_INVALID
);
358 feenv_mask_test ("FE_INVALID", FE_INVALID
);
361 feenv_nomask_test ("FE_INEXACT", FE_INEXACT
);
362 feenv_mask_test ("FE_INEXACT", FE_INEXACT
);
365 feenv_nomask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
366 feenv_mask_test ("FE_UNDERFLOW", FE_UNDERFLOW
);
369 feenv_nomask_test ("FE_OVERFLOW", FE_OVERFLOW
);
370 feenv_mask_test ("FE_OVERFLOW", FE_OVERFLOW
);
372 fesetenv (FE_DFL_ENV
);
376 /* IEC 559 and ISO C99 define a default startup environment */
380 test_exceptions ("Initially all exceptions should be cleared",
382 test_rounding ("Rounding direction should be initalized to nearest",
395 printf ("\n%d errors occurred.\n", count_errors
);
398 printf ("\n All tests passed successfully.\n");