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 "src/__support/FPUtil/FEnvImpl.h" 13 #include "test/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 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT); 21 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); 22 23 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW, 24 FE_UNDERFLOW}; 25 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); 33 34 fexcept_t eflags; 35 ASSERT_EQ(LIBC_NAMESPACE::fegetexceptflag(&eflags, FE_ALL_EXCEPT), 0); 36 37 LIBC_NAMESPACE::fputil::clear_except(e); 38 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 39 40 ASSERT_EQ(LIBC_NAMESPACE::fesetexceptflag(&eflags, FE_ALL_EXCEPT), 0); 41 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 42 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); 46 } 47 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); 58 } 59