1 | /* Copyright (C) 1997, 1998, 2000, 2001, 2003 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.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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. */
|
---|
20 |
|
---|
21 | /* Tests for ISO C99 7.6: Floating-point environment */
|
---|
22 |
|
---|
23 | #ifndef _GNU_SOURCE
|
---|
24 | # define _GNU_SOURCE
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <complex.h>
|
---|
28 | #include <math.h>
|
---|
29 | #include <float.h>
|
---|
30 | #include <fenv.h>
|
---|
31 |
|
---|
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>
|
---|
40 |
|
---|
41 | /*
|
---|
42 | Since not all architectures might define all exceptions, we define
|
---|
43 | a private set and map accordingly.
|
---|
44 | */
|
---|
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)
|
---|
54 |
|
---|
55 | static int count_errors;
|
---|
56 |
|
---|
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)
|
---|
63 | {
|
---|
64 | if (exception & exc_flag)
|
---|
65 | {
|
---|
66 | if (fetestexcept (fe_flag))
|
---|
67 | printf (" Pass: Exception \"%s\" is set\n", flag_name);
|
---|
68 | else
|
---|
69 | {
|
---|
70 | printf (" Fail: Exception \"%s\" is not set\n", flag_name);
|
---|
71 | ++count_errors;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | if (fetestexcept (fe_flag))
|
---|
77 | {
|
---|
78 | printf (" Fail: Exception \"%s\" is set\n", flag_name);
|
---|
79 | ++count_errors;
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | printf (" Pass: Exception \"%s\" is not set\n", flag_name);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void
|
---|
89 | test_exceptions (const char *test_name, short int exception,
|
---|
90 | int ignore_inexact)
|
---|
91 | {
|
---|
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
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void
|
---|
117 | print_rounding (int rounding)
|
---|
118 | {
|
---|
119 |
|
---|
120 | switch (rounding)
|
---|
121 | {
|
---|
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
|
---|
142 | }
|
---|
143 | printf (".\n");
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | static void
|
---|
148 | test_rounding (const char *test_name, int rounding_mode)
|
---|
149 | {
|
---|
150 | int curr_rounding = fegetround ();
|
---|
151 |
|
---|
152 | printf ("Test: %s\n", test_name);
|
---|
153 | if (curr_rounding == rounding_mode)
|
---|
154 | {
|
---|
155 | printf (" Pass: Rounding mode is ");
|
---|
156 | print_rounding (curr_rounding);
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | ++count_errors;
|
---|
161 | printf (" Fail: Rounding mode is ");
|
---|
162 | print_rounding (curr_rounding);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | static void
|
---|
168 | set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
|
---|
169 | {
|
---|
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;
|
---|
175 |
|
---|
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);
|
---|
181 |
|
---|
182 | strcpy (str, test_name);
|
---|
183 | strcat (str, ": clear flag, rest also unset");
|
---|
184 | feclearexcept (exception);
|
---|
185 | test_exceptions (str, NO_EXC, ignore_inexact);
|
---|
186 |
|
---|
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);
|
---|
192 |
|
---|
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);
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void
|
---|
200 | fe_tests (void)
|
---|
201 | {
|
---|
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);
|
---|
206 |
|
---|
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);
|
---|
212 |
|
---|
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
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Test that program aborts with no masked interrupts */
|
---|
231 | static void
|
---|
232 | feenv_nomask_test (const char *flag_name, int fe_exc)
|
---|
233 | {
|
---|
234 | #if defined FE_NOMASK_ENV
|
---|
235 | int status;
|
---|
236 | pid_t pid;
|
---|
237 | fenv_t saved;
|
---|
238 |
|
---|
239 | fegetenv (&saved);
|
---|
240 | errno = 0;
|
---|
241 | fesetenv (FE_NOMASK_ENV);
|
---|
242 | status = errno;
|
---|
243 | fesetenv (&saved);
|
---|
244 | if (status == ENOSYS)
|
---|
245 | {
|
---|
246 | printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n");
|
---|
247 | return;
|
---|
248 | }
|
---|
249 |
|
---|
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)
|
---|
254 | {
|
---|
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
|
---|
262 |
|
---|
263 | fesetenv (FE_NOMASK_ENV);
|
---|
264 | feraiseexcept (fe_exc);
|
---|
265 | exit (2);
|
---|
266 | }
|
---|
267 | else if (pid < 0)
|
---|
268 | {
|
---|
269 | if (errno != ENOSYS)
|
---|
270 | {
|
---|
271 | printf (" Fail: Could not fork.\n");
|
---|
272 | ++count_errors;
|
---|
273 | }
|
---|
274 | else
|
---|
275 | printf (" `fork' not implemented, test ignored.\n");
|
---|
276 | }
|
---|
277 | else {
|
---|
278 | if (waitpid (pid, &status, 0) != pid)
|
---|
279 | {
|
---|
280 | printf (" Fail: waitpid call failed.\n");
|
---|
281 | ++count_errors;
|
---|
282 | }
|
---|
283 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
|
---|
284 | printf (" Pass: Process received SIGFPE.\n");
|
---|
285 | else
|
---|
286 | {
|
---|
287 | printf (" Fail: Process didn't receive signal and exited with status %d.\n",
|
---|
288 | status);
|
---|
289 | ++count_errors;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | #endif
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Test that program doesn't abort with default environment */
|
---|
296 | static void
|
---|
297 | feenv_mask_test (const char *flag_name, int fe_exc)
|
---|
298 | {
|
---|
299 | int status;
|
---|
300 | pid_t pid;
|
---|
301 |
|
---|
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)
|
---|
306 | {
|
---|
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
|
---|
314 |
|
---|
315 | fesetenv (FE_DFL_ENV);
|
---|
316 | feraiseexcept (fe_exc);
|
---|
317 | exit (2);
|
---|
318 | }
|
---|
319 | else if (pid < 0)
|
---|
320 | {
|
---|
321 | if (errno != ENOSYS)
|
---|
322 | {
|
---|
323 | printf (" Fail: Could not fork.\n");
|
---|
324 | ++count_errors;
|
---|
325 | }
|
---|
326 | else
|
---|
327 | printf (" `fork' not implemented, test ignored.\n");
|
---|
328 | }
|
---|
329 | else {
|
---|
330 | if (waitpid (pid, &status, 0) != pid)
|
---|
331 | {
|
---|
332 | printf (" Fail: waitpid call failed.\n");
|
---|
333 | ++count_errors;
|
---|
334 | }
|
---|
335 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
|
---|
336 | printf (" Pass: Process exited normally.\n");
|
---|
337 | else
|
---|
338 | {
|
---|
339 | printf (" Fail: Process exited abnormally with status %d.\n",
|
---|
340 | status);
|
---|
341 | ++count_errors;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* Test that program aborts with no masked interrupts */
|
---|
347 | static void
|
---|
348 | feexcp_nomask_test (const char *flag_name, int fe_exc)
|
---|
349 | {
|
---|
350 | int status;
|
---|
351 | pid_t pid;
|
---|
352 |
|
---|
353 | printf ("Test: after fedisableexcept (%s) processes will abort\n",
|
---|
354 | flag_name);
|
---|
355 | printf (" when feraiseexcept (%s) is called.\n", flag_name);
|
---|
356 | pid = fork ();
|
---|
357 | if (pid == 0)
|
---|
358 | {
|
---|
359 | #ifdef RLIMIT_CORE
|
---|
360 | /* Try to avoid dumping core. */
|
---|
361 | struct rlimit core_limit;
|
---|
362 | core_limit.rlim_cur = 0;
|
---|
363 | core_limit.rlim_max = 0;
|
---|
364 | setrlimit (RLIMIT_CORE, &core_limit);
|
---|
365 | #endif
|
---|
366 |
|
---|
367 | fedisableexcept (FE_ALL_EXCEPT);
|
---|
368 | feenableexcept (fe_exc);
|
---|
369 | feraiseexcept (fe_exc);
|
---|
370 | exit (2);
|
---|
371 | }
|
---|
372 | else if (pid < 0)
|
---|
373 | {
|
---|
374 | if (errno != ENOSYS)
|
---|
375 | {
|
---|
376 | printf (" Fail: Could not fork.\n");
|
---|
377 | ++count_errors;
|
---|
378 | }
|
---|
379 | else
|
---|
380 | printf (" `fork' not implemented, test ignored.\n");
|
---|
381 | }
|
---|
382 | else {
|
---|
383 | if (waitpid (pid, &status, 0) != pid)
|
---|
384 | {
|
---|
385 | printf (" Fail: waitpid call failed.\n");
|
---|
386 | ++count_errors;
|
---|
387 | }
|
---|
388 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE)
|
---|
389 | printf (" Pass: Process received SIGFPE.\n");
|
---|
390 | else
|
---|
391 | {
|
---|
392 | printf (" Fail: Process didn't receive signal and exited with status %d.\n",
|
---|
393 | status);
|
---|
394 | ++count_errors;
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* Test that program doesn't abort with exception. */
|
---|
400 | static void
|
---|
401 | feexcp_mask_test (const char *flag_name, int fe_exc)
|
---|
402 | {
|
---|
403 | int status;
|
---|
404 | int exception;
|
---|
405 | pid_t pid;
|
---|
406 |
|
---|
407 | printf ("Test: after fedisableexcept (%s) processes will not abort\n",
|
---|
408 | flag_name);
|
---|
409 | printf (" when feraiseexcept (%s) is called.\n", flag_name);
|
---|
410 | pid = fork ();
|
---|
411 | if (pid == 0)
|
---|
412 | {
|
---|
413 | #ifdef RLIMIT_CORE
|
---|
414 | /* Try to avoid dumping core. */
|
---|
415 | struct rlimit core_limit;
|
---|
416 | core_limit.rlim_cur = 0;
|
---|
417 | core_limit.rlim_max = 0;
|
---|
418 | setrlimit (RLIMIT_CORE, &core_limit);
|
---|
419 | #endif
|
---|
420 | feenableexcept (FE_ALL_EXCEPT);
|
---|
421 | exception = fe_exc;
|
---|
422 | #ifdef FE_INEXACT
|
---|
423 | /* The standard allows the inexact exception to be set together with the
|
---|
424 | underflow and overflow exceptions. So add FE_INEXACT to the set of
|
---|
425 | exceptions to be disabled if we will be raising underflow or
|
---|
426 | overflow. */
|
---|
427 | # ifdef FE_OVERFLOW
|
---|
428 | if (fe_exc & FE_OVERFLOW)
|
---|
429 | exception |= FE_INEXACT;
|
---|
430 | # endif
|
---|
431 | # ifdef FE_UNDERFLOW
|
---|
432 | if (fe_exc & FE_UNDERFLOW)
|
---|
433 | exception |= FE_INEXACT;
|
---|
434 | # endif
|
---|
435 | #endif
|
---|
436 | fedisableexcept (exception);
|
---|
437 | feraiseexcept (fe_exc);
|
---|
438 | exit (2);
|
---|
439 | }
|
---|
440 | else if (pid < 0)
|
---|
441 | {
|
---|
442 | if (errno != ENOSYS)
|
---|
443 | {
|
---|
444 | printf (" Fail: Could not fork.\n");
|
---|
445 | ++count_errors;
|
---|
446 | }
|
---|
447 | else
|
---|
448 | printf (" `fork' not implemented, test ignored.\n");
|
---|
449 | }
|
---|
450 | else {
|
---|
451 | if (waitpid (pid, &status, 0) != pid)
|
---|
452 | {
|
---|
453 | printf (" Fail: waitpid call failed.\n");
|
---|
454 | ++count_errors;
|
---|
455 | }
|
---|
456 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2)
|
---|
457 | printf (" Pass: Process exited normally.\n");
|
---|
458 | else
|
---|
459 | {
|
---|
460 | printf (" Fail: Process exited abnormally with status %d.\n",
|
---|
461 | status);
|
---|
462 | ++count_errors;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /* Tests for feenableexcept/fedisableexcept/fegetexcept. */
|
---|
469 | static void
|
---|
470 | feenable_test (const char *flag_name, int fe_exc)
|
---|
471 | {
|
---|
472 | int excepts;
|
---|
473 |
|
---|
474 |
|
---|
475 | printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name);
|
---|
476 |
|
---|
477 | /* First disable all exceptions. */
|
---|
478 | if (fedisableexcept (FE_ALL_EXCEPT) == -1)
|
---|
479 | {
|
---|
480 | printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
|
---|
481 | ++count_errors;
|
---|
482 | /* If this fails, the other tests don't make sense. */
|
---|
483 | return;
|
---|
484 | }
|
---|
485 | excepts = fegetexcept ();
|
---|
486 | if (excepts != 0)
|
---|
487 | {
|
---|
488 | printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n",
|
---|
489 | flag_name, excepts);
|
---|
490 | ++count_errors;
|
---|
491 | }
|
---|
492 |
|
---|
493 | excepts = feenableexcept (fe_exc);
|
---|
494 | if (excepts == -1)
|
---|
495 | {
|
---|
496 | printf ("Test: feenableexcept (%s) failed\n", flag_name);
|
---|
497 | ++count_errors;
|
---|
498 | return;
|
---|
499 | }
|
---|
500 | if (excepts != 0)
|
---|
501 | {
|
---|
502 | printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
|
---|
503 | flag_name, excepts);
|
---|
504 | ++count_errors;
|
---|
505 | }
|
---|
506 |
|
---|
507 | excepts = fegetexcept ();
|
---|
508 | if (excepts != fe_exc)
|
---|
509 | {
|
---|
510 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
|
---|
511 | flag_name, fe_exc, excepts);
|
---|
512 | ++count_errors;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /* And now disable the exception again. */
|
---|
516 | excepts = fedisableexcept (fe_exc);
|
---|
517 | if (excepts == -1)
|
---|
518 | {
|
---|
519 | printf ("Test: fedisableexcept (%s) failed\n", flag_name);
|
---|
520 | ++count_errors;
|
---|
521 | return;
|
---|
522 | }
|
---|
523 | if (excepts != fe_exc)
|
---|
524 | {
|
---|
525 | printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
|
---|
526 | flag_name, fe_exc, excepts);
|
---|
527 | ++count_errors;
|
---|
528 | }
|
---|
529 |
|
---|
530 | excepts = fegetexcept ();
|
---|
531 | if (excepts != 0)
|
---|
532 | {
|
---|
533 | printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n",
|
---|
534 | flag_name, excepts);
|
---|
535 | ++count_errors;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /* Now the other way round: Enable all exceptions and disable just this one. */
|
---|
539 | if (feenableexcept (FE_ALL_EXCEPT) == -1)
|
---|
540 | {
|
---|
541 | printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n");
|
---|
542 | ++count_errors;
|
---|
543 | /* If this fails, the other tests don't make sense. */
|
---|
544 | return;
|
---|
545 | }
|
---|
546 |
|
---|
547 | excepts = fegetexcept ();
|
---|
548 | if (excepts != FE_ALL_EXCEPT)
|
---|
549 | {
|
---|
550 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
|
---|
551 | flag_name, FE_ALL_EXCEPT, excepts);
|
---|
552 | ++count_errors;
|
---|
553 | }
|
---|
554 |
|
---|
555 | excepts = fedisableexcept (fe_exc);
|
---|
556 | if (excepts == -1)
|
---|
557 | {
|
---|
558 | printf ("Test: fedisableexcept (%s) failed\n", flag_name);
|
---|
559 | ++count_errors;
|
---|
560 | return;
|
---|
561 | }
|
---|
562 | if (excepts != FE_ALL_EXCEPT)
|
---|
563 | {
|
---|
564 | printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n",
|
---|
565 | flag_name, excepts);
|
---|
566 | ++count_errors;
|
---|
567 | }
|
---|
568 |
|
---|
569 | excepts = fegetexcept ();
|
---|
570 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
|
---|
571 | {
|
---|
572 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
|
---|
573 | flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts);
|
---|
574 | ++count_errors;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* And now enable the exception again. */
|
---|
578 | excepts = feenableexcept (fe_exc);
|
---|
579 | if (excepts == -1)
|
---|
580 | {
|
---|
581 | printf ("Test: feenableexcept (%s) failed\n", flag_name);
|
---|
582 | ++count_errors;
|
---|
583 | return;
|
---|
584 | }
|
---|
585 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc))
|
---|
586 | {
|
---|
587 | printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n",
|
---|
588 | flag_name, excepts);
|
---|
589 | ++count_errors;
|
---|
590 | }
|
---|
591 |
|
---|
592 | excepts = fegetexcept ();
|
---|
593 | if (excepts != FE_ALL_EXCEPT)
|
---|
594 | {
|
---|
595 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n",
|
---|
596 | flag_name, FE_ALL_EXCEPT, excepts);
|
---|
597 | ++count_errors;
|
---|
598 | }
|
---|
599 | feexcp_nomask_test (flag_name, fe_exc);
|
---|
600 | feexcp_mask_test (flag_name, fe_exc);
|
---|
601 |
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | static void
|
---|
606 | fe_single_test (const char *flag_name, int fe_exc)
|
---|
607 | {
|
---|
608 | feenv_nomask_test (flag_name, fe_exc);
|
---|
609 | feenv_mask_test (flag_name, fe_exc);
|
---|
610 | feenable_test (flag_name, fe_exc);
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | static void
|
---|
615 | feenv_tests (void)
|
---|
616 | {
|
---|
617 | /* We might have some exceptions still set. */
|
---|
618 | feclearexcept (FE_ALL_EXCEPT);
|
---|
619 |
|
---|
620 | #ifdef FE_DIVBYZERO
|
---|
621 | fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO);
|
---|
622 | #endif
|
---|
623 | #ifdef FE_INVALID
|
---|
624 | fe_single_test ("FE_INVALID", FE_INVALID);
|
---|
625 | #endif
|
---|
626 | #ifdef FE_INEXACT
|
---|
627 | fe_single_test ("FE_INEXACT", FE_INEXACT);
|
---|
628 | #endif
|
---|
629 | #ifdef FE_UNDERFLOW
|
---|
630 | fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW);
|
---|
631 | #endif
|
---|
632 | #ifdef FE_OVERFLOW
|
---|
633 | fe_single_test ("FE_OVERFLOW", FE_OVERFLOW);
|
---|
634 | #endif
|
---|
635 | fesetenv (FE_DFL_ENV);
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | /* IEC 559 and ISO C99 define a default startup environment */
|
---|
640 | static void
|
---|
641 | initial_tests (void)
|
---|
642 | {
|
---|
643 | test_exceptions ("Initially all exceptions should be cleared",
|
---|
644 | NO_EXC, 0);
|
---|
645 | #ifdef FE_TONEAREST
|
---|
646 | test_rounding ("Rounding direction should be initalized to nearest",
|
---|
647 | FE_TONEAREST);
|
---|
648 | #endif
|
---|
649 | }
|
---|
650 |
|
---|
651 | int
|
---|
652 | main (void)
|
---|
653 | {
|
---|
654 | initial_tests ();
|
---|
655 | fe_tests ();
|
---|
656 | feenv_tests ();
|
---|
657 |
|
---|
658 | if (count_errors)
|
---|
659 | {
|
---|
660 | printf ("\n%d errors occurred.\n", count_errors);
|
---|
661 | exit (1);
|
---|
662 | }
|
---|
663 | printf ("\n All tests passed successfully.\n");
|
---|
664 | return 0;
|
---|
665 | }
|
---|