llvm-project/libc/test/src/fenv/getenv_and_setenv_test.cpp
Sirui Mu ded080152a
[libc] Add osutils for Windows and make libc and its tests build on Windows target (#104676)
This PR first adds osutils for Windows, and changes some libc code to
make libc and its tests build on the Windows target. It then temporarily
disables some libc tests that are currently problematic on Windows.

Specifically, the changes besides the addition of osutils include:

- Macro `LIBC_TYPES_HAS_FLOAT16` is disabled on Windows. `clang-cl`
generates calls to functions in `compiler-rt` to handle float16
arithmetic and these functions are currently not linked in on Windows.
- Macro `LIBC_TYPES_HAS_INT128` is disabled on Windows.
- The invocation to `::aligned_malloc` is changed to an invocation to
`::_aligned_malloc`.
- The following unit tests are temporarily disabled because they
currently fail on Windows:
  - `test.src.__support.big_int_test`
  - `test.src.__support.arg_list_test`
  - `test.src.fenv.getenv_and_setenv_test`
- Tests involving `__m128i`, `__m256i`, and `__m512i` in
`test.src.string.memory_utils.op_tests.cpp`
- `test_range_errors` in `libc/test/src/math/smoke/AddTest.h` and
`libc/test/src/math/smoke/SubTest.h`
2024-09-11 23:41:32 -04:00

90 lines
3.0 KiB
C++

//===-- Unittests for fegetenv and fesetenv -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "hdr/types/fenv_t.h"
#include "src/fenv/fegetenv.h"
#include "src/fenv/fegetround.h"
#include "src/fenv/fesetenv.h"
#include "src/fenv/fesetround.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/macros/properties/os.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/Test.h"
#include "excepts.h"
using LlvmLibcFEnvTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
#ifndef LIBC_TARGET_OS_IS_WINDOWS
TEST_F(LlvmLibcFEnvTest, GetEnvAndSetEnv) {
// We will disable all exceptions to prevent invocation of the exception
// handler.
LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
for (int e : EXCEPTS) {
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
// Save the cleared environment.
fenv_t env;
ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);
LIBC_NAMESPACE::fputil::raise_except(e);
// Make sure that the exception is raised.
ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&env), 0);
ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
}
}
TEST_F(LlvmLibcFEnvTest, Set_FE_DFL_ENV) {
// We will disable all exceptions to prevent invocation of the exception
// handler.
LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT);
int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
FE_UNDERFLOW};
for (int e : excepts) {
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
// Save the cleared environment.
fenv_t env;
ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0);
LIBC_NAMESPACE::fputil::raise_except(e);
// Make sure that the exception is raised.
ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0);
// Setting the default env should clear all exceptions.
ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
}
ASSERT_EQ(LIBC_NAMESPACE::fesetround(FE_DOWNWARD), 0);
ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0);
// Setting the default env should set rounding mode to FE_TONEAREST.
int rm = LIBC_NAMESPACE::fegetround();
EXPECT_EQ(rm, FE_TONEAREST);
}
#endif
#ifdef LIBC_TARGET_OS_IS_WINDOWS
TEST_F(LlvmLibcFEnvTest, Windows_Set_Get_Test) {
// If a valid fenv_t is written, then reading it back out should be identical.
fenv_t setEnv = {0x7e00053e, 0x0f00000f};
fenv_t getEnv;
ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&setEnv), 0);
ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&getEnv), 0);
ASSERT_EQ(setEnv._Fe_ctl, getEnv._Fe_ctl);
ASSERT_EQ(setEnv._Fe_stat, getEnv._Fe_stat);
}
#endif