1 //===-- Unittests for fegetenv and fesetenv -------------------------------===//
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 "hdr/types/fenv_t.h"
10 #include "src/fenv/fegetenv.h"
11 #include "src/fenv/fegetround.h"
12 #include "src/fenv/fesetenv.h"
13 #include "src/fenv/fesetround.h"
15 #include "src/__support/FPUtil/FEnvImpl.h"
16 #include "src/__support/macros/properties/os.h"
17 #include "test/UnitTest/FEnvSafeTest.h"
18 #include "test/UnitTest/Test.h"
22 using LlvmLibcFEnvTest
= LIBC_NAMESPACE::testing::FEnvSafeTest
;
24 #ifndef LIBC_TARGET_OS_IS_WINDOWS
25 TEST_F(LlvmLibcFEnvTest
, GetEnvAndSetEnv
) {
26 // We will disable all exceptions to prevent invocation of the exception
28 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT
);
30 for (int e
: EXCEPTS
) {
31 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT
);
33 // Save the cleared environment.
35 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env
), 0);
37 LIBC_NAMESPACE::fputil::raise_except(e
);
38 // Make sure that the exception is raised.
39 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT
) & e
, 0);
41 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&env
), 0);
42 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT
) & e
, 0);
46 TEST_F(LlvmLibcFEnvTest
, Set_FE_DFL_ENV
) {
47 // We will disable all exceptions to prevent invocation of the exception
49 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT
);
51 int excepts
[] = {FE_DIVBYZERO
, FE_INVALID
, FE_INEXACT
, FE_OVERFLOW
,
54 for (int e
: excepts
) {
55 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT
);
57 // Save the cleared environment.
59 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env
), 0);
61 LIBC_NAMESPACE::fputil::raise_except(e
);
62 // Make sure that the exception is raised.
63 ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT
) & e
, 0);
65 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV
), 0);
66 // Setting the default env should clear all exceptions.
67 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT
) & e
, 0);
70 ASSERT_EQ(LIBC_NAMESPACE::fesetround(FE_DOWNWARD
), 0);
71 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV
), 0);
72 // Setting the default env should set rounding mode to FE_TONEAREST.
73 int rm
= LIBC_NAMESPACE::fegetround();
74 EXPECT_EQ(rm
, FE_TONEAREST
);
78 #ifdef LIBC_TARGET_OS_IS_WINDOWS
79 TEST_F(LlvmLibcFEnvTest
, Windows_Set_Get_Test
) {
80 // If a valid fenv_t is written, then reading it back out should be identical.
81 fenv_t setEnv
= {0x7e00053e, 0x0f00000f};
83 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&setEnv
), 0);
84 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&getEnv
), 0);
86 ASSERT_EQ(setEnv
._Fe_ctl
, getEnv
._Fe_ctl
);
87 ASSERT_EQ(setEnv
._Fe_stat
, getEnv
._Fe_stat
);