1 //===-- Unittests for feclearexcept, feraiseexcept and fetestexpect -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/fenv/feclearexcept.h"
10 #include "src/fenv/feraiseexcept.h"
11 #include "src/fenv/fetestexcept.h"
12 
13 #include "utils/UnitTest/Test.h"
14 
15 #include <fenv.h>
16 
17 TEST(ExceptionStatusTest, RaiseAndTest) {
18   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
19                    FE_UNDERFLOW};
20   for (int e : excepts) {
21     int r = __llvm_libc::feraiseexcept(e);
22     EXPECT_EQ(r, 0);
23     int s = __llvm_libc::fetestexcept(e);
24     EXPECT_EQ(s, e);
25 
26     r = __llvm_libc::feclearexcept(e);
27     EXPECT_EQ(r, 0);
28     s = __llvm_libc::fetestexcept(e);
29     EXPECT_EQ(s, 0);
30   }
31 
32   for (int e1 : excepts) {
33     for (int e2 : excepts) {
34       int e = e1 | e2;
35       int r = __llvm_libc::feraiseexcept(e);
36       EXPECT_EQ(r, 0);
37       int s = __llvm_libc::fetestexcept(e);
38       EXPECT_EQ(s, e);
39 
40       r = __llvm_libc::feclearexcept(e);
41       EXPECT_EQ(r, 0);
42       s = __llvm_libc::fetestexcept(e);
43       EXPECT_EQ(s, 0);
44     }
45   }
46 
47   for (int e1 : excepts) {
48     for (int e2 : excepts) {
49       for (int e3 : excepts) {
50         int e = e1 | e2 | e3;
51         int r = __llvm_libc::feraiseexcept(e);
52         EXPECT_EQ(r, 0);
53         int s = __llvm_libc::fetestexcept(e);
54         EXPECT_EQ(s, e);
55 
56         r = __llvm_libc::feclearexcept(e);
57         EXPECT_EQ(r, 0);
58         s = __llvm_libc::fetestexcept(e);
59         EXPECT_EQ(s, 0);
60       }
61     }
62   }
63 
64   for (int e1 : excepts) {
65     for (int e2 : excepts) {
66       for (int e3 : excepts) {
67         for (int e4 : excepts) {
68           int e = e1 | e2 | e3 | e4;
69           int r = __llvm_libc::feraiseexcept(e);
70           EXPECT_EQ(r, 0);
71           int s = __llvm_libc::fetestexcept(e);
72           EXPECT_EQ(s, e);
73 
74           r = __llvm_libc::feclearexcept(e);
75           EXPECT_EQ(r, 0);
76           s = __llvm_libc::fetestexcept(e);
77           EXPECT_EQ(s, 0);
78         }
79       }
80     }
81   }
82 
83   for (int e1 : excepts) {
84     for (int e2 : excepts) {
85       for (int e3 : excepts) {
86         for (int e4 : excepts) {
87           for (int e5 : excepts) {
88             int e = e1 | e2 | e3 | e4 | e5;
89             int r = __llvm_libc::feraiseexcept(e);
90             EXPECT_EQ(r, 0);
91             int s = __llvm_libc::fetestexcept(e);
92             EXPECT_EQ(s, e);
93 
94             r = __llvm_libc::feclearexcept(e);
95             EXPECT_EQ(r, 0);
96             s = __llvm_libc::fetestexcept(e);
97             EXPECT_EQ(s, 0);
98           }
99         }
100       }
101     }
102   }
103 
104   int r = __llvm_libc::feraiseexcept(FE_ALL_EXCEPT);
105   EXPECT_EQ(r, 0);
106   int s = __llvm_libc::fetestexcept(FE_ALL_EXCEPT);
107   EXPECT_EQ(s, FE_ALL_EXCEPT);
108 }
109