]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1997-2024 Free Software Foundation, Inc. |
762a2918 | 2 | This file is part of the GNU C Library. |
762a2918 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
762a2918 UD |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
762a2918 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
762a2918 | 17 | |
ec751a23 | 18 | /* Tests for ISO C99 7.6: Floating-point environment */ |
bca973bc UD |
19 | |
20 | #ifndef _GNU_SOURCE | |
21 | # define _GNU_SOURCE | |
22 | #endif | |
23 | ||
24 | #include <complex.h> | |
25 | #include <math.h> | |
26 | #include <float.h> | |
27 | #include <fenv.h> | |
28 | ||
29 | #include <errno.h> | |
762a2918 | 30 | #include <signal.h> |
bca973bc UD |
31 | #include <stdlib.h> |
32 | #include <stdio.h> | |
33 | #include <string.h> | |
762a2918 UD |
34 | #include <unistd.h> |
35 | #include <sys/wait.h> | |
74015205 | 36 | #include <sys/resource.h> |
423a7160 | 37 | #include <math-tests.h> |
bca973bc UD |
38 | |
39 | /* | |
40 | Since not all architectures might define all exceptions, we define | |
41 | a private set and map accordingly. | |
42 | */ | |
43 | #define NO_EXC 0 | |
44 | #define INEXACT_EXC 0x1 | |
45 | #define DIVBYZERO_EXC 0x2 | |
46 | #define UNDERFLOW_EXC 0x04 | |
47 | #define OVERFLOW_EXC 0x08 | |
48 | #define INVALID_EXC 0x10 | |
49 | #define ALL_EXC \ | |
34a5a146 JM |
50 | (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC \ |
51 | | INVALID_EXC) | |
bca973bc UD |
52 | |
53 | static int count_errors; | |
54 | ||
1ff950ad | 55 | #if FE_ALL_EXCEPT |
bca973bc UD |
56 | /* Test whether a given exception was raised. */ |
57 | static void | |
58 | test_single_exception (short int exception, | |
59 | short int exc_flag, | |
60 | fexcept_t fe_flag, | |
61 | const char *flag_name) | |
62 | { | |
63 | if (exception & exc_flag) | |
64 | { | |
65 | if (fetestexcept (fe_flag)) | |
66 | printf (" Pass: Exception \"%s\" is set\n", flag_name); | |
67 | else | |
68 | { | |
69 | printf (" Fail: Exception \"%s\" is not set\n", flag_name); | |
70 | ++count_errors; | |
71 | } | |
72 | } | |
73 | else | |
74 | { | |
75 | if (fetestexcept (fe_flag)) | |
76 | { | |
77 | printf (" Fail: Exception \"%s\" is set\n", flag_name); | |
78 | ++count_errors; | |
79 | } | |
80 | else | |
81 | { | |
82 | printf (" Pass: Exception \"%s\" is not set\n", flag_name); | |
83 | } | |
84 | } | |
85 | } | |
1ff950ad | 86 | #endif |
bca973bc UD |
87 | |
88 | static void | |
1522c368 UD |
89 | test_exceptions (const char *test_name, short int exception, |
90 | int ignore_inexact) | |
bca973bc UD |
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 | |
1522c368 UD |
102 | if (!ignore_inexact) |
103 | test_single_exception (exception, INEXACT_EXC, FE_INEXACT, | |
104 | "INEXACT"); | |
bca973bc UD |
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 | ||
762a2918 UD |
116 | static void |
117 | print_rounding (int rounding) | |
118 | { | |
119 | ||
d111572f UD |
120 | switch (rounding) |
121 | { | |
762a2918 | 122 | #ifdef FE_TONEAREST |
d111572f UD |
123 | case FE_TONEAREST: |
124 | printf ("TONEAREST"); | |
125 | break; | |
762a2918 UD |
126 | #endif |
127 | #ifdef FE_UPWARD | |
d111572f UD |
128 | case FE_UPWARD: |
129 | printf ("UPWARD"); | |
130 | break; | |
762a2918 UD |
131 | #endif |
132 | #ifdef FE_DOWNWARD | |
d111572f UD |
133 | case FE_DOWNWARD: |
134 | printf ("DOWNWARD"); | |
135 | break; | |
762a2918 UD |
136 | #endif |
137 | #ifdef FE_TOWARDZERO | |
d111572f UD |
138 | case FE_TOWARDZERO: |
139 | printf ("TOWARDZERO"); | |
140 | break; | |
762a2918 | 141 | #endif |
d111572f | 142 | } |
762a2918 UD |
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 | } | |
d111572f UD |
158 | else |
159 | { | |
160 | ++count_errors; | |
161 | printf (" Fail: Rounding mode is "); | |
162 | print_rounding (curr_rounding); | |
163 | } | |
762a2918 UD |
164 | } |
165 | ||
bca973bc | 166 | |
1ff950ad | 167 | #if FE_ALL_EXCEPT |
bca973bc UD |
168 | static void |
169 | set_single_exc (const char *test_name, int fe_exc, fexcept_t exception) | |
170 | { | |
171 | char str[200]; | |
1522c368 UD |
172 | /* The standard allows the inexact exception to be set together with the |
173 | underflow and overflow exceptions. So ignore the inexact flag if the | |
174 | others are raised. */ | |
175 | int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0; | |
bca973bc UD |
176 | |
177 | strcpy (str, test_name); | |
178 | strcat (str, ": set flag, with rest not set"); | |
179 | feclearexcept (FE_ALL_EXCEPT); | |
180 | feraiseexcept (exception); | |
1522c368 | 181 | test_exceptions (str, fe_exc, ignore_inexact); |
bca973bc UD |
182 | |
183 | strcpy (str, test_name); | |
184 | strcat (str, ": clear flag, rest also unset"); | |
185 | feclearexcept (exception); | |
1522c368 | 186 | test_exceptions (str, NO_EXC, ignore_inexact); |
bca973bc UD |
187 | |
188 | strcpy (str, test_name); | |
189 | strcat (str, ": set flag, with rest set"); | |
190 | feraiseexcept (FE_ALL_EXCEPT ^ exception); | |
191 | feraiseexcept (exception); | |
1522c368 | 192 | test_exceptions (str, ALL_EXC, 0); |
bca973bc UD |
193 | |
194 | strcpy (str, test_name); | |
195 | strcat (str, ": clear flag, leave rest set"); | |
196 | feclearexcept (exception); | |
1522c368 | 197 | test_exceptions (str, ALL_EXC ^ fe_exc, 0); |
bca973bc | 198 | } |
802aef27 AZ |
199 | |
200 | static void | |
201 | update_single_exc (const char *test_name, const fenv_t *envp, int fe_exc, | |
202 | int fe_exc_clear, int exception) | |
203 | { | |
204 | char str[200]; | |
205 | /* The standard allows the inexact exception to be set together with the | |
206 | underflow and overflow exceptions. So ignore the inexact flag if the | |
207 | others are raised. */ | |
208 | int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0; | |
209 | ||
210 | strcpy (str, test_name); | |
211 | strcat (str, ": set flag, with rest not set"); | |
212 | feclearexcept (FE_ALL_EXCEPT); | |
213 | feraiseexcept (exception); | |
214 | feupdateenv (envp); | |
215 | test_exceptions (str, fe_exc, ignore_inexact); | |
216 | ||
217 | strcpy (str, test_name); | |
218 | strcat (str, ": clear flag, rest also unset"); | |
219 | feclearexcept (exception); | |
220 | feupdateenv (envp); | |
221 | test_exceptions (str, fe_exc_clear, ignore_inexact); | |
222 | } | |
1ff950ad | 223 | #endif |
bca973bc UD |
224 | |
225 | static void | |
226 | fe_tests (void) | |
227 | { | |
228 | /* clear all exceptions and test if all are cleared */ | |
229 | feclearexcept (FE_ALL_EXCEPT); | |
230 | test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions", | |
1522c368 | 231 | NO_EXC, 0); |
bca973bc | 232 | |
c6be839e JM |
233 | /* Skip further tests here if exceptions not supported. */ |
234 | if (!EXCEPTION_TESTS (float) && FE_ALL_EXCEPT != 0) | |
235 | return; | |
bca973bc UD |
236 | /* raise all exceptions and test if all are raised */ |
237 | feraiseexcept (FE_ALL_EXCEPT); | |
238 | test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions", | |
1522c368 | 239 | ALL_EXC, 0); |
bca973bc UD |
240 | feclearexcept (FE_ALL_EXCEPT); |
241 | ||
bca973bc UD |
242 | #ifdef FE_DIVBYZERO |
243 | set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO); | |
244 | #endif | |
245 | #ifdef FE_INVALID | |
246 | set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID); | |
247 | #endif | |
248 | #ifdef FE_INEXACT | |
249 | set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT); | |
250 | #endif | |
251 | #ifdef FE_UNDERFLOW | |
252 | set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW); | |
253 | #endif | |
254 | #ifdef FE_OVERFLOW | |
255 | set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW); | |
256 | #endif | |
257 | } | |
258 | ||
1ff950ad | 259 | #if FE_ALL_EXCEPT |
802aef27 AZ |
260 | static const char * |
261 | funcname (int (*func)(const fenv_t *)) | |
262 | { | |
263 | if (func == fesetenv) | |
264 | return "fesetenv"; | |
265 | else if (func == feupdateenv) | |
266 | return "feupdateenv"; | |
267 | __builtin_unreachable (); | |
268 | } | |
269 | ||
762a2918 UD |
270 | /* Test that program aborts with no masked interrupts */ |
271 | static void | |
802aef27 | 272 | feenv_nomask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *)) |
762a2918 | 273 | { |
1ff950ad | 274 | # if defined FE_NOMASK_ENV |
762a2918 UD |
275 | int status; |
276 | pid_t pid; | |
423a7160 W |
277 | |
278 | if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT) | |
802aef27 | 279 | && func (FE_NOMASK_ENV) != 0) |
0413b54c UD |
280 | { |
281 | printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n"); | |
282 | return; | |
283 | } | |
762a2918 | 284 | |
802aef27 | 285 | printf ("Test: after %s (FE_NOMASK_ENV) processes will abort\n", funcname (func)); |
762a2918 | 286 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
d111572f | 287 | pid = fork (); |
762a2918 UD |
288 | if (pid == 0) |
289 | { | |
1ff950ad | 290 | # ifdef RLIMIT_CORE |
74015205 UD |
291 | /* Try to avoid dumping core. */ |
292 | struct rlimit core_limit; | |
293 | core_limit.rlim_cur = 0; | |
294 | core_limit.rlim_max = 0; | |
295 | setrlimit (RLIMIT_CORE, &core_limit); | |
1ff950ad | 296 | # endif |
74015205 | 297 | |
2d7da676 | 298 | fesetenv (FE_NOMASK_ENV); |
762a2918 UD |
299 | feraiseexcept (fe_exc); |
300 | exit (2); | |
301 | } | |
302 | else if (pid < 0) | |
303 | { | |
304 | if (errno != ENOSYS) | |
305 | { | |
306 | printf (" Fail: Could not fork.\n"); | |
307 | ++count_errors; | |
308 | } | |
309 | else | |
310 | printf (" `fork' not implemented, test ignored.\n"); | |
311 | } | |
312 | else { | |
313 | if (waitpid (pid, &status, 0) != pid) | |
314 | { | |
315 | printf (" Fail: waitpid call failed.\n"); | |
316 | ++count_errors; | |
317 | } | |
318 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE) | |
319 | printf (" Pass: Process received SIGFPE.\n"); | |
320 | else | |
321 | { | |
322 | printf (" Fail: Process didn't receive signal and exited with status %d.\n", | |
323 | status); | |
324 | ++count_errors; | |
325 | } | |
326 | } | |
1ff950ad | 327 | # endif |
762a2918 UD |
328 | } |
329 | ||
330 | /* Test that program doesn't abort with default environment */ | |
331 | static void | |
802aef27 | 332 | feenv_mask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *)) |
762a2918 UD |
333 | { |
334 | int status; | |
335 | pid_t pid; | |
336 | ||
802aef27 | 337 | printf ("Test: after %s (FE_DFL_ENV) processes will not abort\n", funcname (func)); |
762a2918 | 338 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
762a2918 UD |
339 | pid = fork (); |
340 | if (pid == 0) | |
341 | { | |
74015205 UD |
342 | #ifdef RLIMIT_CORE |
343 | /* Try to avoid dumping core. */ | |
344 | struct rlimit core_limit; | |
345 | core_limit.rlim_cur = 0; | |
346 | core_limit.rlim_max = 0; | |
347 | setrlimit (RLIMIT_CORE, &core_limit); | |
348 | #endif | |
349 | ||
802aef27 | 350 | func (FE_DFL_ENV); |
762a2918 UD |
351 | feraiseexcept (fe_exc); |
352 | exit (2); | |
353 | } | |
354 | else if (pid < 0) | |
355 | { | |
356 | if (errno != ENOSYS) | |
357 | { | |
358 | printf (" Fail: Could not fork.\n"); | |
359 | ++count_errors; | |
360 | } | |
361 | else | |
362 | printf (" `fork' not implemented, test ignored.\n"); | |
363 | } | |
364 | else { | |
365 | if (waitpid (pid, &status, 0) != pid) | |
366 | { | |
367 | printf (" Fail: waitpid call failed.\n"); | |
368 | ++count_errors; | |
369 | } | |
370 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2) | |
371 | printf (" Pass: Process exited normally.\n"); | |
372 | else | |
373 | { | |
374 | printf (" Fail: Process exited abnormally with status %d.\n", | |
375 | status); | |
376 | ++count_errors; | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
60f0e64b UD |
381 | /* Test that program aborts with no masked interrupts */ |
382 | static void | |
383 | feexcp_nomask_test (const char *flag_name, int fe_exc) | |
384 | { | |
385 | int status; | |
386 | pid_t pid; | |
762a2918 | 387 | |
423a7160 W |
388 | if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && feenableexcept (fe_exc) == -1) |
389 | { | |
390 | printf ("Test: not testing feenableexcept, it isn't implemented.\n"); | |
391 | return; | |
392 | } | |
393 | ||
394 | printf ("Test: after feenableexcept (%s) processes will abort\n", | |
cca4aa58 | 395 | flag_name); |
152461f3 | 396 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
60f0e64b UD |
397 | pid = fork (); |
398 | if (pid == 0) | |
399 | { | |
400 | #ifdef RLIMIT_CORE | |
401 | /* Try to avoid dumping core. */ | |
402 | struct rlimit core_limit; | |
403 | core_limit.rlim_cur = 0; | |
404 | core_limit.rlim_max = 0; | |
405 | setrlimit (RLIMIT_CORE, &core_limit); | |
406 | #endif | |
762a2918 | 407 | |
60f0e64b UD |
408 | fedisableexcept (FE_ALL_EXCEPT); |
409 | feenableexcept (fe_exc); | |
410 | feraiseexcept (fe_exc); | |
411 | exit (2); | |
412 | } | |
413 | else if (pid < 0) | |
414 | { | |
415 | if (errno != ENOSYS) | |
416 | { | |
417 | printf (" Fail: Could not fork.\n"); | |
418 | ++count_errors; | |
419 | } | |
420 | else | |
421 | printf (" `fork' not implemented, test ignored.\n"); | |
422 | } | |
423 | else { | |
424 | if (waitpid (pid, &status, 0) != pid) | |
425 | { | |
426 | printf (" Fail: waitpid call failed.\n"); | |
427 | ++count_errors; | |
428 | } | |
429 | else if (WIFSIGNALED (status) && WTERMSIG (status) == SIGFPE) | |
430 | printf (" Pass: Process received SIGFPE.\n"); | |
431 | else | |
432 | { | |
433 | printf (" Fail: Process didn't receive signal and exited with status %d.\n", | |
434 | status); | |
435 | ++count_errors; | |
436 | } | |
437 | } | |
438 | } | |
439 | ||
440 | /* Test that program doesn't abort with exception. */ | |
762a2918 | 441 | static void |
60f0e64b | 442 | feexcp_mask_test (const char *flag_name, int fe_exc) |
762a2918 | 443 | { |
60f0e64b | 444 | int status; |
c0ac34e4 | 445 | int exception; |
60f0e64b | 446 | pid_t pid; |
762a2918 | 447 | |
cca4aa58 UD |
448 | printf ("Test: after fedisableexcept (%s) processes will not abort\n", |
449 | flag_name); | |
152461f3 | 450 | printf (" when feraiseexcept (%s) is called.\n", flag_name); |
60f0e64b UD |
451 | pid = fork (); |
452 | if (pid == 0) | |
453 | { | |
454 | #ifdef RLIMIT_CORE | |
455 | /* Try to avoid dumping core. */ | |
456 | struct rlimit core_limit; | |
457 | core_limit.rlim_cur = 0; | |
458 | core_limit.rlim_max = 0; | |
459 | setrlimit (RLIMIT_CORE, &core_limit); | |
460 | #endif | |
461 | feenableexcept (FE_ALL_EXCEPT); | |
c0ac34e4 UD |
462 | exception = fe_exc; |
463 | #ifdef FE_INEXACT | |
464 | /* The standard allows the inexact exception to be set together with the | |
465 | underflow and overflow exceptions. So add FE_INEXACT to the set of | |
466 | exceptions to be disabled if we will be raising underflow or | |
467 | overflow. */ | |
468 | # ifdef FE_OVERFLOW | |
469 | if (fe_exc & FE_OVERFLOW) | |
470 | exception |= FE_INEXACT; | |
471 | # endif | |
472 | # ifdef FE_UNDERFLOW | |
473 | if (fe_exc & FE_UNDERFLOW) | |
474 | exception |= FE_INEXACT; | |
475 | # endif | |
476 | #endif | |
477 | fedisableexcept (exception); | |
60f0e64b UD |
478 | feraiseexcept (fe_exc); |
479 | exit (2); | |
480 | } | |
481 | else if (pid < 0) | |
482 | { | |
483 | if (errno != ENOSYS) | |
484 | { | |
485 | printf (" Fail: Could not fork.\n"); | |
486 | ++count_errors; | |
487 | } | |
488 | else | |
489 | printf (" `fork' not implemented, test ignored.\n"); | |
490 | } | |
491 | else { | |
492 | if (waitpid (pid, &status, 0) != pid) | |
493 | { | |
494 | printf (" Fail: waitpid call failed.\n"); | |
495 | ++count_errors; | |
496 | } | |
497 | else if (WIFEXITED (status) && WEXITSTATUS (status) == 2) | |
498 | printf (" Pass: Process exited normally.\n"); | |
499 | else | |
500 | { | |
501 | printf (" Fail: Process exited abnormally with status %d.\n", | |
502 | status); | |
503 | ++count_errors; | |
504 | } | |
505 | } | |
506 | } | |
507 | ||
508 | ||
509 | /* Tests for feenableexcept/fedisableexcept/fegetexcept. */ | |
510 | static void | |
511 | feenable_test (const char *flag_name, int fe_exc) | |
512 | { | |
513 | int excepts; | |
514 | ||
60f0e64b UD |
515 | printf ("Tests for feenableexcepts etc. with flag %s\n", flag_name); |
516 | ||
517 | /* First disable all exceptions. */ | |
518 | if (fedisableexcept (FE_ALL_EXCEPT) == -1) | |
519 | { | |
520 | printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n"); | |
521 | ++count_errors; | |
522 | /* If this fails, the other tests don't make sense. */ | |
523 | return; | |
524 | } | |
525 | excepts = fegetexcept (); | |
526 | if (excepts != 0) | |
527 | { | |
528 | printf ("Test: fegetexcept (%s) failed, return should be 0, is %d\n", | |
529 | flag_name, excepts); | |
530 | ++count_errors; | |
531 | } | |
60f0e64b | 532 | excepts = feenableexcept (fe_exc); |
423a7160 W |
533 | if (!EXCEPTION_ENABLE_SUPPORTED (fe_exc) && excepts == -1) |
534 | { | |
535 | printf ("Test: not testing feenableexcept, it isn't implemented.\n"); | |
536 | return; | |
537 | } | |
60f0e64b UD |
538 | if (excepts == -1) |
539 | { | |
540 | printf ("Test: feenableexcept (%s) failed\n", flag_name); | |
541 | ++count_errors; | |
542 | return; | |
543 | } | |
544 | if (excepts != 0) | |
545 | { | |
546 | printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n", | |
547 | flag_name, excepts); | |
548 | ++count_errors; | |
549 | } | |
550 | ||
551 | excepts = fegetexcept (); | |
552 | if (excepts != fe_exc) | |
553 | { | |
554 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", | |
555 | flag_name, fe_exc, excepts); | |
556 | ++count_errors; | |
557 | } | |
558 | ||
559 | /* And now disable the exception again. */ | |
560 | excepts = fedisableexcept (fe_exc); | |
561 | if (excepts == -1) | |
562 | { | |
563 | printf ("Test: fedisableexcept (%s) failed\n", flag_name); | |
564 | ++count_errors; | |
565 | return; | |
566 | } | |
567 | if (excepts != fe_exc) | |
568 | { | |
569 | printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n", | |
570 | flag_name, fe_exc, excepts); | |
571 | ++count_errors; | |
572 | } | |
573 | ||
574 | excepts = fegetexcept (); | |
575 | if (excepts != 0) | |
576 | { | |
577 | printf ("Test: fegetexcept (%s) failed, return should be 0, is 0x%x\n", | |
578 | flag_name, excepts); | |
579 | ++count_errors; | |
580 | } | |
581 | ||
582 | /* Now the other way round: Enable all exceptions and disable just this one. */ | |
583 | if (feenableexcept (FE_ALL_EXCEPT) == -1) | |
584 | { | |
585 | printf ("Test: feenableexcept (FE_ALL_EXCEPT) failed\n"); | |
586 | ++count_errors; | |
587 | /* If this fails, the other tests don't make sense. */ | |
588 | return; | |
589 | } | |
590 | ||
591 | excepts = fegetexcept (); | |
592 | if (excepts != FE_ALL_EXCEPT) | |
593 | { | |
594 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", | |
595 | flag_name, FE_ALL_EXCEPT, excepts); | |
596 | ++count_errors; | |
597 | } | |
598 | ||
599 | excepts = fedisableexcept (fe_exc); | |
600 | if (excepts == -1) | |
601 | { | |
602 | printf ("Test: fedisableexcept (%s) failed\n", flag_name); | |
603 | ++count_errors; | |
604 | return; | |
605 | } | |
606 | if (excepts != FE_ALL_EXCEPT) | |
607 | { | |
608 | printf ("Test: fedisableexcept (%s) failed, return should be 0, is 0x%x\n", | |
609 | flag_name, excepts); | |
610 | ++count_errors; | |
611 | } | |
612 | ||
613 | excepts = fegetexcept (); | |
614 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc)) | |
615 | { | |
616 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", | |
617 | flag_name, (FE_ALL_EXCEPT & ~fe_exc), excepts); | |
618 | ++count_errors; | |
619 | } | |
620 | ||
621 | /* And now enable the exception again. */ | |
622 | excepts = feenableexcept (fe_exc); | |
623 | if (excepts == -1) | |
624 | { | |
625 | printf ("Test: feenableexcept (%s) failed\n", flag_name); | |
626 | ++count_errors; | |
627 | return; | |
628 | } | |
629 | if (excepts != (FE_ALL_EXCEPT & ~fe_exc)) | |
630 | { | |
631 | printf ("Test: feenableexcept (%s) failed, return should be 0, is 0x%x\n", | |
632 | flag_name, excepts); | |
633 | ++count_errors; | |
634 | } | |
635 | ||
636 | excepts = fegetexcept (); | |
637 | if (excepts != FE_ALL_EXCEPT) | |
638 | { | |
639 | printf ("Test: fegetexcept (%s) failed, return should be 0x%x, is 0x%x\n", | |
640 | flag_name, FE_ALL_EXCEPT, excepts); | |
641 | ++count_errors; | |
642 | } | |
643 | feexcp_nomask_test (flag_name, fe_exc); | |
644 | feexcp_mask_test (flag_name, fe_exc); | |
152461f3 | 645 | |
60f0e64b UD |
646 | } |
647 | ||
648 | ||
649 | static void | |
650 | fe_single_test (const char *flag_name, int fe_exc) | |
651 | { | |
802aef27 AZ |
652 | feenv_nomask_test (flag_name, fe_exc, fesetenv); |
653 | feenv_mask_test (flag_name, fe_exc, fesetenv); | |
60f0e64b UD |
654 | feenable_test (flag_name, fe_exc); |
655 | } | |
802aef27 AZ |
656 | |
657 | ||
658 | static void | |
659 | feupdate_single_test (const char *flag_name, int fe_exc) | |
660 | { | |
661 | feenv_nomask_test (flag_name, fe_exc, feupdateenv); | |
497e4d50 | 662 | fesetenv (FE_DFL_ENV); |
802aef27 AZ |
663 | feenv_mask_test (flag_name, fe_exc, feupdateenv); |
664 | } | |
1ff950ad | 665 | #endif |
60f0e64b UD |
666 | |
667 | ||
668 | static void | |
669 | feenv_tests (void) | |
670 | { | |
152461f3 UD |
671 | /* We might have some exceptions still set. */ |
672 | feclearexcept (FE_ALL_EXCEPT); | |
673 | ||
762a2918 | 674 | #ifdef FE_DIVBYZERO |
60f0e64b | 675 | fe_single_test ("FE_DIVBYZERO", FE_DIVBYZERO); |
762a2918 UD |
676 | #endif |
677 | #ifdef FE_INVALID | |
60f0e64b | 678 | fe_single_test ("FE_INVALID", FE_INVALID); |
762a2918 UD |
679 | #endif |
680 | #ifdef FE_INEXACT | |
60f0e64b | 681 | fe_single_test ("FE_INEXACT", FE_INEXACT); |
762a2918 UD |
682 | #endif |
683 | #ifdef FE_UNDERFLOW | |
60f0e64b | 684 | fe_single_test ("FE_UNDERFLOW", FE_UNDERFLOW); |
762a2918 UD |
685 | #endif |
686 | #ifdef FE_OVERFLOW | |
60f0e64b | 687 | fe_single_test ("FE_OVERFLOW", FE_OVERFLOW); |
762a2918 UD |
688 | #endif |
689 | fesetenv (FE_DFL_ENV); | |
690 | } | |
691 | ||
802aef27 AZ |
692 | #if FE_ALL_EXCEPT |
693 | static void | |
694 | feupdateenv_single_test (const char *test_name, int fe_exc, int exception) | |
695 | { | |
696 | char str[100]; | |
697 | fenv_t env; | |
698 | int res; | |
699 | ||
700 | snprintf (str, sizeof str, "feupdateenv %s and FL_DFL_ENV", test_name); | |
701 | update_single_exc (str, FE_DFL_ENV, fe_exc, NO_EXC, exception); | |
702 | ||
703 | feraiseexcept (FE_ALL_EXCEPT); | |
704 | res = fegetenv (&env); | |
705 | if (res != 0) | |
706 | { | |
707 | printf ("fegetenv failed: %d\n", res); | |
708 | ++count_errors; | |
709 | return; | |
710 | } | |
711 | ||
712 | snprintf (str, sizeof str, "feupdateenv %s and FE_ALL_EXCEPT", test_name); | |
713 | update_single_exc (str, &env, ALL_EXC, ALL_EXC, exception); | |
714 | } | |
715 | #endif | |
716 | ||
717 | static void | |
718 | feupdateenv_tests (void) | |
719 | { | |
720 | /* We might have some exceptions still set. */ | |
721 | feclearexcept (FE_ALL_EXCEPT); | |
722 | ||
723 | #ifdef FE_DIVBYZERO | |
724 | feupdate_single_test ("FE_DIVBYZERO", FE_DIVBYZERO); | |
725 | #endif | |
726 | #ifdef FE_INVALID | |
727 | feupdate_single_test ("FE_INVALID", FE_INVALID); | |
728 | #endif | |
729 | #ifdef FE_INEXACT | |
730 | feupdate_single_test ("FE_INEXACT", FE_INEXACT); | |
731 | #endif | |
732 | #ifdef FE_UNDERFLOW | |
733 | feupdate_single_test ("FE_UNDERFLOW", FE_UNDERFLOW); | |
734 | #endif | |
735 | #ifdef FE_OVERFLOW | |
736 | feupdate_single_test ("FE_OVERFLOW", FE_OVERFLOW); | |
737 | #endif | |
738 | ||
739 | #ifdef FE_DIVBYZERO | |
740 | feupdateenv_single_test ("DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO); | |
741 | #endif | |
742 | #ifdef FE_INVALID | |
743 | feupdateenv_single_test ("INVALID", INVALID_EXC, FE_INVALID); | |
744 | #endif | |
745 | #ifdef FE_INEXACT | |
746 | feupdateenv_single_test ("INEXACT", INEXACT_EXC, FE_INEXACT); | |
747 | #endif | |
748 | #ifdef FE_UNDERFLOW | |
749 | feupdateenv_single_test ("UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW); | |
750 | #endif | |
751 | #ifdef FE_OVERFLOW | |
752 | feupdateenv_single_test ("OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW); | |
753 | #endif | |
754 | ||
755 | feupdateenv (FE_DFL_ENV); | |
756 | } | |
757 | ||
762a2918 | 758 | |
a8c79c40 UD |
759 | static void |
760 | feholdexcept_tests (void) | |
761 | { | |
762 | fenv_t saved, saved2; | |
763 | int res; | |
764 | ||
765 | feclearexcept (FE_ALL_EXCEPT); | |
766 | fedisableexcept (FE_ALL_EXCEPT); | |
767 | #ifdef FE_DIVBYZERO | |
768 | feraiseexcept (FE_DIVBYZERO); | |
769 | #endif | |
c6be839e JM |
770 | if (EXCEPTION_TESTS (float)) |
771 | test_exceptions ("feholdexcept_tests FE_DIVBYZERO test", | |
772 | DIVBYZERO_EXC, 0); | |
a8c79c40 UD |
773 | res = feholdexcept (&saved); |
774 | if (res != 0) | |
775 | { | |
776 | printf ("feholdexcept failed: %d\n", res); | |
777 | ++count_errors; | |
778 | } | |
779 | #if defined FE_TONEAREST && defined FE_TOWARDZERO | |
780 | res = fesetround (FE_TOWARDZERO); | |
c6be839e | 781 | if (res != 0 && ROUNDING_TESTS (float, FE_TOWARDZERO)) |
a8c79c40 UD |
782 | { |
783 | printf ("fesetround failed: %d\n", res); | |
784 | ++count_errors; | |
785 | } | |
786 | #endif | |
787 | test_exceptions ("feholdexcept_tests 0 test", NO_EXC, 0); | |
0af797de | 788 | #ifdef FE_INVALID |
a8c79c40 | 789 | feraiseexcept (FE_INVALID); |
c6be839e JM |
790 | if (EXCEPTION_TESTS (float)) |
791 | test_exceptions ("feholdexcept_tests FE_INVALID test", | |
792 | INVALID_EXC, 0); | |
0af797de | 793 | #endif |
a8c79c40 UD |
794 | res = feupdateenv (&saved); |
795 | if (res != 0) | |
796 | { | |
797 | printf ("feupdateenv failed: %d\n", res); | |
798 | ++count_errors; | |
799 | } | |
800 | #if defined FE_TONEAREST && defined FE_TOWARDZERO | |
801 | res = fegetround (); | |
802 | if (res != FE_TONEAREST) | |
803 | { | |
804 | printf ("feupdateenv didn't restore rounding mode: %d\n", res); | |
805 | ++count_errors; | |
806 | } | |
807 | #endif | |
c6be839e JM |
808 | if (EXCEPTION_TESTS (float)) |
809 | test_exceptions ("feholdexcept_tests FE_DIVBYZERO|FE_INVALID test", | |
810 | DIVBYZERO_EXC | INVALID_EXC, 0); | |
a8c79c40 | 811 | feclearexcept (FE_ALL_EXCEPT); |
0af797de | 812 | #ifdef FE_INVALID |
a8c79c40 | 813 | feraiseexcept (FE_INVALID); |
0af797de | 814 | #endif |
a8c79c40 UD |
815 | #if defined FE_TONEAREST && defined FE_UPWARD |
816 | res = fesetround (FE_UPWARD); | |
c6be839e | 817 | if (res != 0 && ROUNDING_TESTS (float, FE_UPWARD)) |
a8c79c40 UD |
818 | { |
819 | printf ("fesetround failed: %d\n", res); | |
820 | ++count_errors; | |
821 | } | |
822 | #endif | |
823 | res = feholdexcept (&saved2); | |
824 | if (res != 0) | |
825 | { | |
826 | printf ("feholdexcept failed: %d\n", res); | |
827 | ++count_errors; | |
828 | } | |
829 | #if defined FE_TONEAREST && defined FE_UPWARD | |
830 | res = fesetround (FE_TONEAREST); | |
831 | if (res != 0) | |
832 | { | |
833 | printf ("fesetround failed: %d\n", res); | |
834 | ++count_errors; | |
835 | } | |
836 | #endif | |
837 | test_exceptions ("feholdexcept_tests 0 2nd test", NO_EXC, 0); | |
0af797de | 838 | #ifdef FE_INEXACT |
a8c79c40 | 839 | feraiseexcept (FE_INEXACT); |
c6be839e JM |
840 | if (EXCEPTION_TESTS (float)) |
841 | test_exceptions ("feholdexcept_tests FE_INEXACT test", | |
842 | INEXACT_EXC, 0); | |
0af797de | 843 | #endif |
a8c79c40 UD |
844 | res = feupdateenv (&saved2); |
845 | if (res != 0) | |
846 | { | |
847 | printf ("feupdateenv failed: %d\n", res); | |
848 | ++count_errors; | |
849 | } | |
850 | #if defined FE_TONEAREST && defined FE_UPWARD | |
851 | res = fegetround (); | |
c6be839e | 852 | if (res != FE_UPWARD && ROUNDING_TESTS (float, FE_UPWARD)) |
a8c79c40 UD |
853 | { |
854 | printf ("feupdateenv didn't restore rounding mode: %d\n", res); | |
855 | ++count_errors; | |
856 | } | |
857 | fesetround (FE_TONEAREST); | |
858 | #endif | |
c6be839e JM |
859 | if (EXCEPTION_TESTS (float)) |
860 | test_exceptions ("feholdexcept_tests FE_INEXACT|FE_INVALID test", | |
861 | INVALID_EXC | INEXACT_EXC, 0); | |
a8c79c40 UD |
862 | feclearexcept (FE_ALL_EXCEPT); |
863 | } | |
864 | ||
865 | ||
63ae7b63 | 866 | /* IEC 559 and ISO C99 define a default startup environment */ |
762a2918 UD |
867 | static void |
868 | initial_tests (void) | |
869 | { | |
870 | test_exceptions ("Initially all exceptions should be cleared", | |
1522c368 | 871 | NO_EXC, 0); |
efff4ab3 | 872 | #ifdef FE_TONEAREST |
7f0d9e61 | 873 | test_rounding ("Rounding direction should be initialized to nearest", |
762a2918 | 874 | FE_TONEAREST); |
efff4ab3 | 875 | #endif |
762a2918 UD |
876 | } |
877 | ||
802aef27 AZ |
878 | static int |
879 | do_test (void) | |
bca973bc | 880 | { |
762a2918 | 881 | initial_tests (); |
bca973bc | 882 | fe_tests (); |
762a2918 | 883 | feenv_tests (); |
a8c79c40 | 884 | feholdexcept_tests (); |
802aef27 | 885 | feupdateenv_tests (); |
bca973bc UD |
886 | |
887 | if (count_errors) | |
888 | { | |
49c091e5 | 889 | printf ("\n%d errors occurred.\n", count_errors); |
bca973bc UD |
890 | exit (1); |
891 | } | |
892 | printf ("\n All tests passed successfully.\n"); | |
767b6275 | 893 | return 0; |
bca973bc | 894 | } |
802aef27 AZ |
895 | |
896 | #include <support/test-driver.c> |