xref: /llvm-project/libc/test/src/fenv/exception_flags_test.cpp (revision b7e05c874b5b2a77c87df71ee3442abc2569cbb9)
1 //===-- Unittests for fegetexceptflag and fesetexceptflag -----------------===//
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/fegetexceptflag.h"
10 #include "src/fenv/fesetexceptflag.h"
11 
12 #include "utils/FPUtil/FEnv.h"
13 #include "utils/UnitTest/Test.h"
14 
15 #include <fenv.h>
16 
17 TEST(LlvmLibcFenvTest, GetExceptFlagAndSetExceptFlag) {
18   // We will disable all exceptions to prevent invocation of the exception
19   // handler.
20   __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT);
21 
22   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
23                    FE_UNDERFLOW};
24 
25   for (int e : excepts) {
26     // The overall idea is to raise an except and save the exception flags.
27     // Next, clear the flags and then set the saved exception flags. This
28     // should set the flag corresponding to the previously raised exception.
29     __llvm_libc::fputil::raiseExcept(e);
30     // Make sure that the exception flag is set.
31     ASSERT_NE(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT) & e, 0);
32 
33     fexcept_t eflags;
34     ASSERT_EQ(__llvm_libc::fegetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
35 
36     __llvm_libc::fputil::clearExcept(e);
37     ASSERT_EQ(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT) & e, 0);
38 
39     ASSERT_EQ(__llvm_libc::fesetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
40     ASSERT_NE(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT) & e, 0);
41 
42     // Cleanup
43     __llvm_libc::fputil::clearExcept(e);
44   }
45 }
46