1/* Test fesetexcept.
2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 <https://www.gnu.org/licenses/>. */
18
19#include <fenv.h>
20#include <stdio.h>
21#include <math-tests.h>
22
23static int
24test_fesetexcept (int exc, const char *exc_name)
25{
26 int result = 0;
27
28 printf (format: "Testing %s\n", exc_name);
29 feclearexcept (FE_ALL_EXCEPT);
30 int ret = fesetexcept (excepts: exc);
31 if (ret == 0)
32 printf (format: "fesetexcept (%s) succeeded\n", exc_name);
33 else
34 {
35 printf (format: "fesetexcept (%s) failed\n", exc_name);
36 if (exc == 0 || EXCEPTION_TESTS (float))
37 {
38 puts (s: "failure of fesetexcept was unexpected");
39 result = 1;
40 }
41 else
42 puts (s: "failure of fesetexcept OK, skipping further tests");
43 return result;
44 }
45 ret = fetestexcept (FE_ALL_EXCEPT);
46 if (ret != exc)
47 {
48 printf (format: "raised exceptions %x, expected %x\n",
49 (unsigned int) ret, (unsigned int) exc);
50 result = 1;
51 }
52
53 ret = feraiseexcept (FE_ALL_EXCEPT);
54 if (ret != 0)
55 {
56 if (exc == 0 && !EXCEPTION_TESTS (float))
57 {
58 puts (s: "feraiseexcept (FE_ALL_EXCEPT) failed, skipping further tests");
59 return result;
60 }
61 puts (s: "feraiseexcept (FE_ALL_EXCEPT) unexpectedly failed");
62 result = 1;
63 }
64 ret = fesetexcept (excepts: exc);
65 if (ret != 0)
66 {
67 puts (s: "fesetexcept (second test) unexpectedly failed");
68 result = 1;
69 }
70 ret = fetestexcept (FE_ALL_EXCEPT);
71 if (ret != FE_ALL_EXCEPT)
72 {
73 printf (format: "raised exceptions (second test) %x, expected %x\n",
74 (unsigned int) ret, (unsigned int) FE_ALL_EXCEPT);
75 result = 1;
76 }
77
78 feclearexcept (FE_ALL_EXCEPT);
79 ret = feraiseexcept (FE_ALL_EXCEPT & ~exc);
80 if (ret != 0)
81 {
82 puts (s: "feraiseexcept (third test) unexpectedly failed");
83 result = 1;
84 }
85 ret = fesetexcept (excepts: exc);
86 if (ret != 0)
87 {
88 puts (s: "fesetexcept (third test) unexpectedly failed");
89 result = 1;
90 }
91 ret = fetestexcept (FE_ALL_EXCEPT);
92 if (ret != FE_ALL_EXCEPT)
93 {
94 printf (format: "raised exceptions (third test) %x, expected %x\n",
95 (unsigned int) ret, (unsigned int) FE_ALL_EXCEPT);
96 result = 1;
97 }
98
99 return result;
100}
101
102static int
103do_test (void)
104{
105 int result = 0;
106
107 result |= test_fesetexcept (exc: 0, exc_name: "0");
108 result |= test_fesetexcept (FE_ALL_EXCEPT, exc_name: "FE_ALL_EXCEPT");
109#ifdef FE_DIVBYZERO
110 result |= test_fesetexcept (FE_DIVBYZERO, exc_name: "FE_DIVBYZERO");
111#endif
112#ifdef FE_INEXACT
113 result |= test_fesetexcept (FE_INEXACT, exc_name: "FE_INEXACT");
114#endif
115#ifdef FE_INVALID
116 result |= test_fesetexcept (FE_INVALID, exc_name: "FE_INVALID");
117#endif
118#ifdef FE_OVERFLOW
119 result |= test_fesetexcept (FE_OVERFLOW, exc_name: "FE_OVERFLOW");
120#endif
121#ifdef FE_UNDERFLOW
122 result |= test_fesetexcept (FE_UNDERFLOW, exc_name: "FE_UNDERFLOW");
123#endif
124
125 return result;
126}
127
128#define TEST_FUNCTION do_test ()
129#include "../test-skeleton.c"
130

source code of glibc/math/test-fesetexcept.c