1 //===-- Unittests for fegetexceptflag and fesetexceptflag -----------------===//
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
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"
17 TEST(LlvmLibcFenvTest
, GetExceptFlagAndSetExceptFlag
) {
18 // We will disable all exceptions to prevent invocation of the exception
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
,
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);
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
);
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
);