Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / fenv / exception_flags_test.cpp
blob434adc06b1a36a94fdf4f74b4abd6df1255ab2b7
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 //===----------------------------------------------------------------------===//
9 #include "src/fenv/fegetexceptflag.h"
10 #include "src/fenv/fesetexceptflag.h"
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "test/UnitTest/Test.h"
15 #include <fenv.h>
17 TEST(LlvmLibcFenvTest, GetExceptFlagAndSetExceptFlag) {
18 // We will disable all exceptions to prevent invocation of the exception
19 // handler.
20 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
21 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
23 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
24 FE_UNDERFLOW};
26 for (int e : excepts) {
27 // The overall idea is to raise an except and save the exception flags.
28 // Next, clear the flags and then set the saved exception flags. This
29 // should set the flag corresponding to the previously raised exception.
30 LIBC_NAMESPACE::fputil::raise_except(e);
31 // Make sure that the exception flag is set.
32 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
34 fexcept_t eflags;
35 ASSERT_EQ(LIBC_NAMESPACE::fegetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
37 LIBC_NAMESPACE::fputil::clear_except(e);
38 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
40 ASSERT_EQ(LIBC_NAMESPACE::fesetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
41 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
43 // Cleanup. We clear all excepts as raising excepts like FE_OVERFLOW
44 // can also raise FE_INEXACT.
45 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
48 // Next, we will raise one exception and save the flags.
49 LIBC_NAMESPACE::fputil::raise_except(FE_INVALID);
50 fexcept_t eflags;
51 LIBC_NAMESPACE::fegetexceptflag(&eflags, FE_ALL_EXCEPT);
52 // Clear all exceptions and raise two other exceptions.
53 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
54 LIBC_NAMESPACE::fputil::raise_except(FE_OVERFLOW | FE_INEXACT);
55 // When we set the flags and test, we should only see FE_INVALID.
56 LIBC_NAMESPACE::fesetexceptflag(&eflags, FE_ALL_EXCEPT);
57 EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT), FE_INVALID);