usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-fenv-env-2.c
blob98d4c7f001bae8e3152520514721de87846edc24
1 /* Test of controlling the floating-point environment.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
19 #include <config.h>
21 /* Specification. */
22 #include <fenv.h>
24 #include "macros.h"
26 /* Test the combination of fegetenv() with fesetenv(). */
28 /* On *BSD/powerpc systems, raising FE_INVALID also sets FE_VXSOFT. */
29 #ifndef FE_VXSOFT
30 # define FE_VXSOFT 0
31 #endif
33 int
34 main ()
36 #if defined __GLIBC__ && defined __arm__ && defined __SOFTFP__
37 fputs ("Skipping test: no floating-point environment exists on this machine\n", stderr);
38 return 77;
39 #else
40 fenv_t env1, env2;
42 /* Get to a known initial state. */
43 ASSERT (feclearexcept (FE_ALL_EXCEPT) == 0);
45 /* Save the current environment in env1. */
46 ASSERT (fegetenv (&env1) == 0);
48 /* Modify the current environment. */
49 fesetround (FE_UPWARD);
50 int supports_tracking = (feraiseexcept (FE_INVALID | FE_OVERFLOW | FE_INEXACT) == 0);
51 int supports_trapping = (feenableexcept (FE_DIVBYZERO) != -1);
53 /* Save the current environment in env2. */
54 ASSERT (fegetenv (&env2) == 0);
56 /* Check that the exception flags are unmodified. */
57 if (supports_tracking)
58 ASSERT ((fetestexcept (FE_ALL_EXCEPT) & ~FE_VXSOFT) == (FE_INVALID | FE_OVERFLOW | FE_INEXACT));
59 else
60 ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
61 /* Check that the exception trap bits are unmodified. */
62 ASSERT (fegetexcept () == (supports_trapping ? FE_DIVBYZERO : 0));
64 /* Go back to env1. */
65 ASSERT (fesetenv (&env1) == 0);
67 /* Check that the rounding direction has been restored. */
68 ASSERT (fegetround () == FE_TONEAREST);
69 /* Check that the exception flags have been restored. */
70 ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
71 /* Check that the exception trap bits have been restored. */
72 ASSERT (fegetexcept () == 0);
74 /* Modify the rounding direction, the exception flags, and the exception
75 trap bits again. */
76 fesetround (FE_DOWNWARD);
77 ASSERT (fegetround () == FE_DOWNWARD);
78 feclearexcept (FE_OVERFLOW);
79 feraiseexcept (FE_UNDERFLOW | FE_INEXACT);
80 ASSERT (fetestexcept (FE_ALL_EXCEPT) == (supports_tracking ? FE_UNDERFLOW | FE_INEXACT : 0));
81 feenableexcept (FE_INVALID);
82 ASSERT (fegetexcept () == (supports_trapping ? FE_INVALID : 0));
84 /* Go back to env2. */
85 ASSERT (fesetenv (&env2) == 0);
87 /* Check that the rounding direction has been restored. */
88 ASSERT (fegetround () == FE_UPWARD);
89 /* Check that the exception flags have been restored. */
90 if (supports_tracking)
91 ASSERT ((fetestexcept (FE_ALL_EXCEPT) & ~FE_VXSOFT) == (FE_INVALID | FE_OVERFLOW | FE_INEXACT));
92 else
93 ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
94 /* Check that the exception trap bits have been restored. */
95 ASSERT (fegetexcept () == (supports_trapping ? FE_DIVBYZERO : 0));
97 /* ======================================================================== */
98 /* FE_DFL_ENV */
100 /* Enable trapping on FE_INVALID. */
101 feclearexcept (FE_INVALID);
102 feenableexcept (FE_INVALID);
103 ASSERT (fetestexcept (FE_ALL_EXCEPT) == (supports_tracking ? FE_OVERFLOW | FE_INEXACT : 0));
105 /* Go back to the default environment. */
106 ASSERT (fesetenv (FE_DFL_ENV) == 0);
108 /* Check its contents. */
109 ASSERT (fegetround () == FE_TONEAREST);
110 ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
112 /* Check that it has trapping on FE_INVALID disabled. */
113 ASSERT (fegetexcept () == 0);
115 double volatile a;
116 _GL_UNUSED double volatile b;
117 a = 0; b = a / a;
120 /* ======================================================================== */
121 /* Check that fesetenv restores the trapping behaviour. */
123 /* Enable trapping on FE_INVALID. */
124 feclearexcept (FE_INVALID);
125 feenableexcept (FE_INVALID);
127 /* Go back to env1. */
128 ASSERT (fesetenv (&env1) == 0);
130 /* Check that it has disabled trapping on FE_INVALID. */
131 ASSERT (fegetexcept () == 0);
133 double volatile a;
134 _GL_UNUSED double volatile b;
135 a = 0; b = a / a;
138 return test_exit_status;
139 #endif