1//===-- Unittests for feclearexcept, feraiseexcept, fetestexpect ----------===//
2//===-- and fesetexcept ---------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#include "src/fenv/feclearexcept.h"
11#include "src/fenv/feraiseexcept.h"
12#include "src/fenv/fesetexcept.h"
13#include "src/fenv/fetestexcept.h"
14
15#include "src/__support/FPUtil/FEnvImpl.h"
16#include "test/UnitTest/FEnvSafeTest.h"
17#include "test/UnitTest/Test.h"
18
19#include "hdr/fenv_macros.h"
20
21#include "excepts.h"
22
23using LlvmLibcExceptionStatusTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
24
25TEST_F(LlvmLibcExceptionStatusTest, RaiseAndTest) {
26 // This test raises a set of exceptions and checks that the exception
27 // status flags are updated. The intention is really not to invoke the
28 // exception handler. Hence, we will disable all exceptions at the
29 // beginning.
30 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
31
32 for (int e : EXCEPTS) {
33 int r = LIBC_NAMESPACE::feraiseexcept(e);
34 ASSERT_EQ(r, 0);
35 int s = LIBC_NAMESPACE::fetestexcept(e);
36 ASSERT_EQ(s, e);
37
38 r = LIBC_NAMESPACE::feclearexcept(e);
39 ASSERT_EQ(r, 0);
40 s = LIBC_NAMESPACE::fetestexcept(e);
41 ASSERT_EQ(s, 0);
42
43 r = LIBC_NAMESPACE::fesetexcept(excepts: e);
44 ASSERT_EQ(r, 0);
45 s = LIBC_NAMESPACE::fetestexcept(e);
46 ASSERT_EQ(s, e);
47 }
48
49 for (int e1 : EXCEPTS) {
50 for (int e2 : EXCEPTS) {
51 int e = e1 | e2;
52 int r = LIBC_NAMESPACE::feraiseexcept(e);
53 ASSERT_EQ(r, 0);
54 int s = LIBC_NAMESPACE::fetestexcept(e);
55 ASSERT_EQ(s, e);
56
57 r = LIBC_NAMESPACE::feclearexcept(e);
58 ASSERT_EQ(r, 0);
59 s = LIBC_NAMESPACE::fetestexcept(e);
60 ASSERT_EQ(s, 0);
61
62 r = LIBC_NAMESPACE::fesetexcept(excepts: e);
63 ASSERT_EQ(r, 0);
64 s = LIBC_NAMESPACE::fetestexcept(e);
65 ASSERT_EQ(s, e);
66 }
67 }
68
69 for (int e1 : EXCEPTS) {
70 for (int e2 : EXCEPTS) {
71 for (int e3 : EXCEPTS) {
72 int e = e1 | e2 | e3;
73 int r = LIBC_NAMESPACE::feraiseexcept(e);
74 ASSERT_EQ(r, 0);
75 int s = LIBC_NAMESPACE::fetestexcept(e);
76 ASSERT_EQ(s, e);
77
78 r = LIBC_NAMESPACE::feclearexcept(e);
79 ASSERT_EQ(r, 0);
80 s = LIBC_NAMESPACE::fetestexcept(e);
81 ASSERT_EQ(s, 0);
82
83 r = LIBC_NAMESPACE::fesetexcept(excepts: e);
84 ASSERT_EQ(r, 0);
85 s = LIBC_NAMESPACE::fetestexcept(e);
86 ASSERT_EQ(s, e);
87 }
88 }
89 }
90
91 for (int e1 : EXCEPTS) {
92 for (int e2 : EXCEPTS) {
93 for (int e3 : EXCEPTS) {
94 for (int e4 : EXCEPTS) {
95 int e = e1 | e2 | e3 | e4;
96 int r = LIBC_NAMESPACE::feraiseexcept(e);
97 ASSERT_EQ(r, 0);
98 int s = LIBC_NAMESPACE::fetestexcept(e);
99 ASSERT_EQ(s, e);
100
101 r = LIBC_NAMESPACE::feclearexcept(e);
102 ASSERT_EQ(r, 0);
103 s = LIBC_NAMESPACE::fetestexcept(e);
104 ASSERT_EQ(s, 0);
105
106 r = LIBC_NAMESPACE::fesetexcept(excepts: e);
107 ASSERT_EQ(r, 0);
108 s = LIBC_NAMESPACE::fetestexcept(e);
109 ASSERT_EQ(s, e);
110 }
111 }
112 }
113 }
114
115 for (int e1 : EXCEPTS) {
116 for (int e2 : EXCEPTS) {
117 for (int e3 : EXCEPTS) {
118 for (int e4 : EXCEPTS) {
119 for (int e5 : EXCEPTS) {
120 int e = e1 | e2 | e3 | e4 | e5;
121 int r = LIBC_NAMESPACE::feraiseexcept(e);
122 ASSERT_EQ(r, 0);
123 int s = LIBC_NAMESPACE::fetestexcept(e);
124 ASSERT_EQ(s, e);
125
126 r = LIBC_NAMESPACE::feclearexcept(e);
127 ASSERT_EQ(r, 0);
128 s = LIBC_NAMESPACE::fetestexcept(e);
129 ASSERT_EQ(s, 0);
130
131 r = LIBC_NAMESPACE::fesetexcept(excepts: e);
132 ASSERT_EQ(r, 0);
133 s = LIBC_NAMESPACE::fetestexcept(e);
134 ASSERT_EQ(s, e);
135 }
136 }
137 }
138 }
139 }
140
141 int r = LIBC_NAMESPACE::feraiseexcept(ALL_EXCEPTS);
142 ASSERT_EQ(r, 0);
143 int s = LIBC_NAMESPACE::fetestexcept(ALL_EXCEPTS);
144 ASSERT_EQ(s, ALL_EXCEPTS);
145
146 r = LIBC_NAMESPACE::fesetexcept(excepts: ALL_EXCEPTS);
147 ASSERT_EQ(r, 0);
148 s = LIBC_NAMESPACE::fetestexcept(ALL_EXCEPTS);
149 ASSERT_EQ(s, ALL_EXCEPTS);
150}
151

source code of libc/test/src/fenv/exception_status_test.cpp