90 lines · cpp
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-exception6//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 "src/__support/macros/properties/os.h"17#include "test/UnitTest/FEnvSafeTest.h"18#include "test/UnitTest/Test.h"19 20#include "excepts.h"21 22using LlvmLibcFEnvTest = LIBC_NAMESPACE::testing::FEnvSafeTest;23 24#ifndef LIBC_TARGET_OS_IS_WINDOWS25TEST_F(LlvmLibcFEnvTest, GetEnvAndSetEnv) {26 // We will disable all exceptions to prevent invocation of the exception27 // handler.28 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);29 30 for (int e : EXCEPTS) {31 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);32 33 // Save the cleared environment.34 fenv_t env;35 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);36 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);40 41 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&env), 0);42 ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);43 }44}45 46TEST_F(LlvmLibcFEnvTest, Set_FE_DFL_ENV) {47 // We will disable all exceptions to prevent invocation of the exception48 // handler.49 LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);50 51 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,52 FE_UNDERFLOW};53 54 for (int e : excepts) {55 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);56 57 // Save the cleared environment.58 fenv_t env;59 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);60 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);64 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);68 }69 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);75}76#endif77 78#ifdef LIBC_TARGET_OS_IS_WINDOWS79TEST_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};82 fenv_t getEnv;83 ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&setEnv), 0);84 ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&getEnv), 0);85 86 ASSERT_EQ(setEnv._Fe_ctl, getEnv._Fe_ctl);87 ASSERT_EQ(setEnv._Fe_stat, getEnv._Fe_stat);88}89#endif90