1 /* Test femode_t functions: test handling of exception traps.
2    Copyright (C) 2016-2022 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_ee(int exc1,int exc2)24 test_ee (int exc1, int exc2)
25 {
26   int result = 0;
27   printf ("testing %x %x\n", (unsigned int) exc1, (unsigned int) exc2);
28 
29   fedisableexcept (FE_ALL_EXCEPT);
30   int ret = feenableexcept (exc1);
31   if (ret == -1)
32     {
33       if (EXCEPTION_ENABLE_SUPPORTED (exc1))
34 	{
35 	  puts ("first feenableexcept failed unexpectedly");
36 	  result = 1;
37 	}
38       else
39 	puts ("first feenableexcept failed, cannot test");
40       return result;
41     }
42   femode_t saved;
43   ret = fegetmode (&saved);
44   if (ret != 0)
45     {
46       puts ("fegetmode failed");
47       result = 1;
48       return result;
49     }
50   fedisableexcept (FE_ALL_EXCEPT);
51   ret = feenableexcept (exc2);
52   if (ret == -1)
53     {
54       if (EXCEPTION_ENABLE_SUPPORTED (exc2))
55 	{
56 	  puts ("second feenableexcept failed unexpectedly");
57 	  result = 1;
58 	}
59       else
60 	puts ("second feenableexcept failed, cannot test");
61       return result;
62     }
63   ret = fesetmode (&saved);
64   if (ret != 0)
65     {
66       puts ("fesetmode failed");
67       result = 1;
68       return result;
69     }
70   /* Verify that the set of enabled traps was restored.  */
71   ret = fegetexcept ();
72   if (ret != exc1)
73     {
74       printf ("restored enabled traps %x not %x\n", (unsigned int) ret,
75 	      (unsigned int) exc1);
76       result = 1;
77     }
78   /* Likewise, with default modes.  */
79   ret = fesetmode (FE_DFL_MODE);
80   if (ret != 0)
81     {
82       puts ("fesetmode (FE_DFL_MODE) failed");
83       result = 1;
84       return result;
85     }
86   ret = fegetexcept ();
87   if (ret != 0)
88     {
89       printf ("FE_DFL_MODE enabled traps %x not 0\n", (unsigned int) ret);
90       result = 1;
91     }
92 
93   return result;
94 }
95 
96 static int
test_e(int exc1)97 test_e (int exc1)
98 {
99   int result = 0;
100 
101   result |= test_ee (exc1, 0);
102   result |= test_ee (exc1, FE_ALL_EXCEPT);
103 #ifdef FE_DIVBYZERO
104   result |= test_ee (exc1, FE_DIVBYZERO);
105 #endif
106 #ifdef FE_INEXACT
107   result |= test_ee (exc1, FE_INEXACT);
108 #endif
109 #ifdef FE_INVALID
110   result |= test_ee (exc1, FE_INVALID);
111 #endif
112 #ifdef FE_OVERFLOW
113   result |= test_ee (exc1, FE_OVERFLOW);
114 #endif
115 #ifdef FE_UNDERFLOW
116   result |= test_ee (exc1, FE_UNDERFLOW);
117 #endif
118 
119   return result;
120 }
121 
122 static int
do_test(void)123 do_test (void)
124 {
125   int result = 0;
126 
127   result |= test_e (0);
128   result |= test_e (FE_ALL_EXCEPT);
129 #ifdef FE_DIVBYZERO
130   result |= test_e (FE_DIVBYZERO);
131 #endif
132 #ifdef FE_INEXACT
133   result |= test_e (FE_INEXACT);
134 #endif
135 #ifdef FE_INVALID
136   result |= test_e (FE_INVALID);
137 #endif
138 #ifdef FE_OVERFLOW
139   result |= test_e (FE_OVERFLOW);
140 #endif
141 #ifdef FE_UNDERFLOW
142   result |= test_e (FE_UNDERFLOW);
143 #endif
144 
145   return result;
146 }
147 
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"
150