1 //===-- Unittests for feholdexcept with exceptions enabled ----------------===//
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/feholdexcept.h"
10 
11 #include "utils/FPUtil/FEnv.h"
12 #include "utils/UnitTest/Test.h"
13 
14 #include <fenv.h>
15 #include <signal.h>
16 
17 TEST(LlvmLibcFEnvTest, RaiseAndCrash) {
18   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
19                    FE_UNDERFLOW};
20 
21   for (int e : excepts) {
22     fenv_t env;
23     __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT);
24     __llvm_libc::fputil::enableExcept(e);
25     ASSERT_EQ(__llvm_libc::fputil::clearExcept(FE_ALL_EXCEPT), 0);
26     ASSERT_EQ(__llvm_libc::feholdexcept(&env), 0);
27     // feholdexcept should disable all excepts so raising an exception
28     // should not crash/invoke the exception handler.
29     ASSERT_EQ(__llvm_libc::fputil::raiseExcept(e), 0);
30 
31     // When we put back the saved env which has the exception enabled, it
32     // should crash with SIGFPE.
33     __llvm_libc::fputil::setEnv(&env);
34     ASSERT_DEATH([=] { __llvm_libc::fputil::raiseExcept(e); },
35                  WITH_SIGNAL(SIGFPE));
36   }
37 }
38