1 /* Test fegetexceptflag and fesetexceptflag.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 #include <math-tests.h>
23 /* Like feraiseexcept, but raise exactly the specified exceptions EXC,
24 without possibly raising "inexact" together with "overflow" or
25 "underflow" as permitted by ISO C. (This is not used with traps
26 enabled, so side-effects from raising and then clearing "inexact"
30 feraiseexcept_exact (int exc
)
40 if ((exc
& FE_INEXACT
) != 0
42 || fetestexcept (FE_INEXACT
) != 0)
43 return feraiseexcept (exc
);
44 int ret
= feraiseexcept (exc
);
45 feclearexcept (FE_INEXACT
);
48 return feraiseexcept (exc
);
53 test_set (int initial
, const fexcept_t
*saved
, int mask
, int expected
)
56 feclearexcept (FE_ALL_EXCEPT
);
57 printf ("Testing set: initial exceptions %x, mask %x, expected %x\n",
58 (unsigned int) initial
, (unsigned int) mask
,
59 (unsigned int) expected
);
60 int ret
= feraiseexcept_exact (initial
);
63 puts ("feraiseexcept failed");
67 ret
= fesetexceptflag (saved
, mask
);
70 puts ("fesetexceptflag failed");
74 puts ("fesetexceptflag succeeded");
75 ret
= fetestexcept (FE_ALL_EXCEPT
);
78 printf ("raised exceptions %x, expected %x\n",
79 (unsigned int) ret
, (unsigned int) expected
);
86 test_except (int exc
, const char *exc_name
)
90 printf ("Testing %s\n", exc_name
);
91 feclearexcept (FE_ALL_EXCEPT
);
93 fexcept_t clear_saved_exc
, clear_saved_all
;
94 int ret
= fegetexceptflag (&clear_saved_exc
, exc
);
96 printf ("fegetexceptflag (%s) succeeded\n", exc_name
);
99 printf ("fegetexceptflag (%s) failed\n", exc_name
);
103 ret
= fegetexceptflag (&clear_saved_all
, FE_ALL_EXCEPT
);
105 puts ("fegetexceptflag (FE_ALL_EXCEPT) succeeded");
108 puts ("fegetexceptflag (FE_ALL_EXCEPT) failed");
113 ret
= feraiseexcept_exact (exc
);
115 printf ("feraiseexcept (%s) succeeded\n", exc_name
);
118 printf ("feraiseexcept (%s) failed\n", exc_name
);
119 if (exc
== 0 || EXCEPTION_TESTS (float))
121 puts ("failure of feraiseexcept was unexpected");
125 puts ("failure of feraiseexcept OK, skipping further tests");
129 fexcept_t set_saved_exc
, set_saved_all
;
130 ret
= fegetexceptflag (&set_saved_exc
, exc
);
132 printf ("fegetexceptflag (%s) succeeded\n", exc_name
);
135 printf ("fegetexceptflag (%s) failed\n", exc_name
);
139 ret
= fegetexceptflag (&set_saved_all
, FE_ALL_EXCEPT
);
141 puts ("fegetexceptflag (FE_ALL_EXCEPT) succeeded");
144 puts ("fegetexceptflag (FE_ALL_EXCEPT) failed");
149 result
|= test_set (0, &set_saved_exc
, exc
, exc
);
150 result
|= test_set (0, &set_saved_all
, exc
, exc
);
151 result
|= test_set (0, &set_saved_all
, FE_ALL_EXCEPT
, exc
);
152 result
|= test_set (0, &clear_saved_exc
, exc
, 0);
153 result
|= test_set (0, &clear_saved_all
, exc
, 0);
154 result
|= test_set (0, &clear_saved_all
, FE_ALL_EXCEPT
, 0);
155 result
|= test_set (exc
, &set_saved_exc
, exc
, exc
);
156 result
|= test_set (exc
, &set_saved_all
, exc
, exc
);
157 result
|= test_set (exc
, &set_saved_all
, FE_ALL_EXCEPT
, exc
);
158 result
|= test_set (exc
, &clear_saved_exc
, exc
, 0);
159 result
|= test_set (exc
, &clear_saved_all
, exc
, 0);
160 result
|= test_set (exc
, &clear_saved_all
, FE_ALL_EXCEPT
, 0);
161 result
|= test_set (FE_ALL_EXCEPT
, &set_saved_exc
, exc
, FE_ALL_EXCEPT
);
162 result
|= test_set (FE_ALL_EXCEPT
, &set_saved_all
, exc
, FE_ALL_EXCEPT
);
163 result
|= test_set (FE_ALL_EXCEPT
, &set_saved_all
, FE_ALL_EXCEPT
, exc
);
164 result
|= test_set (FE_ALL_EXCEPT
, &clear_saved_exc
, exc
,
165 FE_ALL_EXCEPT
& ~exc
);
166 result
|= test_set (FE_ALL_EXCEPT
, &clear_saved_all
, exc
,
167 FE_ALL_EXCEPT
& ~exc
);
168 result
|= test_set (FE_ALL_EXCEPT
, &clear_saved_all
, FE_ALL_EXCEPT
, 0);
178 result
|= test_except (0, "0");
179 result
|= test_except (FE_ALL_EXCEPT
, "FE_ALL_EXCEPT");
181 result
|= test_except (FE_DIVBYZERO
, "FE_DIVBYZERO");
184 result
|= test_except (FE_INEXACT
, "FE_INEXACT");
187 result
|= test_except (FE_INVALID
, "FE_INVALID");
190 result
|= test_except (FE_OVERFLOW
, "FE_OVERFLOW");
193 result
|= test_except (FE_UNDERFLOW
, "FE_UNDERFLOW");
199 #define TEST_FUNCTION do_test ()
200 #include "../test-skeleton.c"