1 //===-- Unittests for fegetenv and fesetenv -------------------------------===// 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/fegetenv.h" 10 #include "src/fenv/fesetenv.h" 11 12 #include "utils/FPUtil/FEnv.h" 13 #include "utils/UnitTest/Test.h" 14 15 #include <fenv.h> 16 17 TEST(LlvmLibcFenvTest, GetEnvAndSetEnv) { 18 // We will disable all exceptions to prevent invocation of the exception 19 // handler. 20 __llvm_libc::fputil::disableExcept(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 __llvm_libc::fputil::clearExcept(FE_ALL_EXCEPT); 27 28 // Save the cleared environment. 29 fenv_t env; 30 ASSERT_EQ(__llvm_libc::fegetenv(&env), 0); 31 32 __llvm_libc::fputil::raiseExcept(e); 33 // Make sure that the exception is raised. 34 ASSERT_NE(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT) & e, 0); 35 36 ASSERT_EQ(__llvm_libc::fesetenv(&env), 0); 37 ASSERT_EQ(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT) & e, 0); 38 } 39 } 40