1 /* Test fetestexceptflag.
2    Copyright (C) 2016-2021 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 
23 static int
test_one(int exc_test,int exc_set,int exc_save)24 test_one (int exc_test, int exc_set, int exc_save)
25 {
26   int result = 0;
27 
28   printf ("Individual test: %x %x %x\n", (unsigned int) exc_test,
29 	  (unsigned int) exc_set, (unsigned int) exc_save);
30 
31   feclearexcept (FE_ALL_EXCEPT);
32   int ret = fesetexcept (exc_set);
33   if (ret != 0)
34     {
35       puts ("fesetexcept failed");
36       if (exc_set == 0 || EXCEPTION_TESTS (float))
37 	{
38 	  puts ("failure of fesetexcept was unexpected");
39 	  result = 1;
40 	}
41       else
42 	puts ("failure of fesetexcept OK, skipping further tests");
43       return result;
44     }
45   fexcept_t saved;
46   ret = fegetexceptflag (&saved, exc_save);
47   if (ret == 0)
48     puts ("fegetexceptflag succeeded");
49   else
50     {
51       puts ("fegetexceptflag failed");
52       result = 1;
53       return result;
54     }
55   ret = fetestexceptflag (&saved, exc_test);
56   if (ret == (exc_set & exc_test))
57     puts ("fetestexceptflag result correct");
58   else
59     {
60       printf ("fetestexceptflag returned %x, expected %x\n", ret,
61 	      exc_set & exc_test);
62       result = 1;
63     }
64   if (exc_save == FE_ALL_EXCEPT)
65     {
66       /* Also test fetestexceptflag testing all exceptions but
67 	 possibly with only some set.  */
68       ret = fetestexceptflag (&saved, FE_ALL_EXCEPT);
69       if (ret == exc_set)
70 	puts ("fetestexceptflag (FE_ALL_EXCEPT) result correct");
71       else
72 	{
73 	  printf ("fetestexceptflag (FE_ALL_EXCEPT) returned %x, expected %x\n",
74 		  ret, exc_set);
75 	  result = 1;
76 	}
77     }
78   return result;
79 }
80 
81 static int
test_fetestexceptflag(int exc,const char * exc_name)82 test_fetestexceptflag (int exc, const char *exc_name)
83 {
84   int result = 0;
85 
86   printf ("Testing %s\n", exc_name);
87 
88   /* Test each case of: whether this exception is set or clear;
89      whether other exceptions are set or clear; whether the whole
90      state is saved or just the state for this exception.  */
91   result |= test_one (exc, 0, exc);
92   result |= test_one (exc, 0, FE_ALL_EXCEPT);
93   result |= test_one (exc, exc, exc);
94   result |= test_one (exc, exc, FE_ALL_EXCEPT);
95   result |= test_one (exc, FE_ALL_EXCEPT & ~exc, exc);
96   result |= test_one (exc, FE_ALL_EXCEPT & ~exc, FE_ALL_EXCEPT);
97   result |= test_one (exc, FE_ALL_EXCEPT, exc);
98   result |= test_one (exc, FE_ALL_EXCEPT, FE_ALL_EXCEPT);
99 
100   return result;
101 }
102 
103 static int
do_test(void)104 do_test (void)
105 {
106   int result = 0;
107 
108   result |= test_fetestexceptflag (0, "0");
109   result |= test_fetestexceptflag (FE_ALL_EXCEPT, "FE_ALL_EXCEPT");
110 #ifdef FE_DIVBYZERO
111   result |= test_fetestexceptflag (FE_DIVBYZERO, "FE_DIVBYZERO");
112 #endif
113 #ifdef FE_INEXACT
114   result |= test_fetestexceptflag (FE_INEXACT, "FE_INEXACT");
115 #endif
116 #ifdef FE_INVALID
117   result |= test_fetestexceptflag (FE_INVALID, "FE_INVALID");
118 #endif
119 #ifdef FE_OVERFLOW
120   result |= test_fetestexceptflag (FE_OVERFLOW, "FE_OVERFLOW");
121 #endif
122 #ifdef FE_UNDERFLOW
123   result |= test_fetestexceptflag (FE_UNDERFLOW, "FE_UNDERFLOW");
124 #endif
125 
126   return result;
127 }
128 
129 #define TEST_FUNCTION do_test ()
130 #include "../test-skeleton.c"
131