• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
14 
15 #include "src/__support/FPUtil/FEnvImpl.h"
16 #include "test/UnitTest/FEnvSafeTest.h"
17 #include "test/UnitTest/Test.h"
18 
19 #include "excepts.h"
20 
21 using LlvmLibcFEnvTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
22 
TEST_F(LlvmLibcFEnvTest,GetEnvAndSetEnv)23 TEST_F(LlvmLibcFEnvTest, GetEnvAndSetEnv) {
24   // We will disable all exceptions to prevent invocation of the exception
25   // handler.
26   LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
27 
28   for (int e : EXCEPTS) {
29     LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
30 
31     // Save the cleared environment.
32     fenv_t env;
33     ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);
34 
35     LIBC_NAMESPACE::fputil::raise_except(e);
36     // Make sure that the exception is raised.
37     ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
38 
39     ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&env), 0);
40     ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
41   }
42 }
43 
TEST_F(LlvmLibcFEnvTest,Set_FE_DFL_ENV)44 TEST_F(LlvmLibcFEnvTest, Set_FE_DFL_ENV) {
45   // We will disable all exceptions to prevent invocation of the exception
46   // handler.
47   LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
48 
49   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
50                    FE_UNDERFLOW};
51 
52   for (int e : excepts) {
53     LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
54 
55     // Save the cleared environment.
56     fenv_t env;
57     ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);
58 
59     LIBC_NAMESPACE::fputil::raise_except(e);
60     // Make sure that the exception is raised.
61     ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
62 
63     ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0);
64     // Setting the default env should clear all exceptions.
65     ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
66   }
67 
68   ASSERT_EQ(LIBC_NAMESPACE::fesetround(FE_DOWNWARD), 0);
69   ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0);
70   // Setting the default env should set rounding mode to FE_TONEAREST.
71   int rm = LIBC_NAMESPACE::fegetround();
72   EXPECT_EQ(rm, FE_TONEAREST);
73 }
74 
75 #ifdef _WIN32
TEST_F(LlvmLibcFEnvTest,Windows_Set_Get_Test)76 TEST_F(LlvmLibcFEnvTest, Windows_Set_Get_Test) {
77   // If a valid fenv_t is written, then reading it back out should be identical.
78   fenv_t setEnv = {0x7e00053e, 0x0f00000f};
79   fenv_t getEnv;
80   ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&setEnv), 0);
81   ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&getEnv), 0);
82 
83   ASSERT_EQ(setEnv._Fe_ctl, getEnv._Fe_ctl);
84   ASSERT_EQ(setEnv._Fe_stat, getEnv._Fe_stat);
85 }
86 #endif
87