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 "hdr/types/fexcept_t.h" 10 #include "src/fenv/fegetexceptflag.h" 11 #include "src/fenv/fesetexceptflag.h" 12 13 #include "src/__support/FPUtil/FEnvImpl.h" 14 #include "test/UnitTest/Test.h" 15 16 TEST(LlvmLibcFenvTest, GetExceptFlagAndSetExceptFlag) { 17 // We will disable all exceptions to prevent invocation of the exception 18 // handler. 19 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT); 20 LIBC_NAMESPACE::fputil::clear_except(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 LIBC_NAMESPACE::fputil::raise_except(e); 30 // Make sure that the exception flag is set. 31 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 32 33 fexcept_t eflags; 34 ASSERT_EQ(LIBC_NAMESPACE::fegetexceptflag(&eflags, FE_ALL_EXCEPT), 0); 35 36 LIBC_NAMESPACE::fputil::clear_except(e); 37 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 38 39 ASSERT_EQ(LIBC_NAMESPACE::fesetexceptflag(&eflags, FE_ALL_EXCEPT), 0); 40 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 41 42 // Cleanup. We clear all excepts as raising excepts like FE_OVERFLOW 43 // can also raise FE_INEXACT. 44 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); 45 } 46 47 // Next, we will raise one exception and save the flags. 48 LIBC_NAMESPACE::fputil::raise_except(FE_INVALID); 49 fexcept_t eflags; 50 LIBC_NAMESPACE::fegetexceptflag(&eflags, FE_ALL_EXCEPT); 51 // Clear all exceptions and raise two other exceptions. 52 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); 53 LIBC_NAMESPACE::fputil::raise_except(FE_OVERFLOW | FE_INEXACT); 54 // When we set the flags and test, we should only see FE_INVALID. 55 LIBC_NAMESPACE::fesetexceptflag(&eflags, FE_ALL_EXCEPT); 56 EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT), FE_INVALID); 57 } 58