Skip to content

Commit 2422972

Browse files
authored
[libc] Migrate FEnvSafeTest and FPTest to ErrnoCheckingTest. (#152633)
This would ensure that errno value is cleared out before test execution and tests pass even when LIBC_ERRNO_MODE_SYSTEM_INLINE is specified (and errno may be clobbered before test execution). A lot of the tests would fail, however, since errno would end up getting set to EDOM or ERANGE during test execution and never validated before the end of the test. This should be fixed - and errno should be explicitly checked or ignored in all of those cases, but for now add a TODO to address it later (see open issue #135320) and clear out errno in test fixture to avoid test failures.
1 parent 856a8b5 commit 2422972

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

libc/test/UnitTest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ add_unittest_framework_library(
125125
RoundingModeUtils.h
126126
DEPENDS
127127
LibcTest
128+
libc.test.UnitTest.ErrnoCheckingTest
128129
libc.test.UnitTest.string_utils
129130
libc.src.__support.CPP.array
130131
libc.src.__support.FPUtil.fp_bits

libc/test/UnitTest/FEnvSafeTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include "FEnvSafeTest.h"
1010

1111
#include "src/__support/FPUtil/FEnvImpl.h"
12+
#include "src/__support/libc_errno.h"
1213
#include "src/__support/macros/config.h"
1314
#include "src/__support/macros/properties/architectures.h"
15+
#include "test/UnitTest/ErrnoCheckingTest.h"
1416

1517
namespace LIBC_NAMESPACE_DECL {
1618
namespace testing {
@@ -25,6 +27,10 @@ void FEnvSafeTest::TearDown() {
2527
if (!should_be_unchanged) {
2628
restore_fenv();
2729
}
30+
// TODO (PR 135320): Remove this override once all FEnvSafeTest instances are
31+
// updated to validate or ignore errno.
32+
libc_errno = 0;
33+
ErrnoCheckingTest::TearDown();
2834
}
2935

3036
void FEnvSafeTest::get_fenv(fenv_t &fenv) {

libc/test/UnitTest/FEnvSafeTest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "hdr/types/fenv_t.h"
1313
#include "src/__support/CPP/utility.h"
1414
#include "src/__support/macros/config.h"
15+
#include "test/UnitTest/ErrnoCheckingTest.h"
1516
#include "test/UnitTest/Test.h"
1617

1718
namespace LIBC_NAMESPACE_DECL {
@@ -20,7 +21,7 @@ namespace testing {
2021
// This provides a test fixture (or base class for other test fixtures) that
2122
// asserts that each test does not leave the FPU state represented by `fenv_t`
2223
// (aka `FPState`) perturbed from its initial state.
23-
class FEnvSafeTest : public Test {
24+
class FEnvSafeTest : public ErrnoCheckingTest {
2425
public:
2526
void TearDown() override;
2627

libc/test/UnitTest/FPMatcher.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
#include "src/__support/FPUtil/FEnvImpl.h"
1515
#include "src/__support/FPUtil/FPBits.h"
1616
#include "src/__support/FPUtil/fpbits_str.h"
17+
#include "src/__support/libc_errno.h"
1718
#include "src/__support/macros/config.h"
1819
#include "src/__support/macros/properties/architectures.h"
20+
#include "test/UnitTest/ErrnoCheckingTest.h"
1921
#include "test/UnitTest/RoundingModeUtils.h"
2022
#include "test/UnitTest/StringUtils.h"
2123
#include "test/UnitTest/Test.h"
@@ -166,7 +168,7 @@ CFPMatcher<T, C> getMatcherComplex(T expectedValue) {
166168
return CFPMatcher<T, C>(expectedValue);
167169
}
168170

169-
template <typename T> struct FPTest : public Test {
171+
template <typename T> struct FPTest : public ErrnoCheckingTest {
170172
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
171173
using StorageType = typename FPBits::StorageType;
172174
static constexpr StorageType STORAGE_MAX =
@@ -191,6 +193,13 @@ template <typename T> struct FPTest : public Test {
191193
fputil::testing::RoundingMode::Downward,
192194
fputil::testing::RoundingMode::TowardZero,
193195
};
196+
197+
void TearDown() override {
198+
// TODO (PR 135320): Remove this override once all FPTest instances are
199+
// updated to validate or ignore errno.
200+
libc_errno = 0;
201+
ErrnoCheckingTest::TearDown();
202+
}
194203
};
195204

196205
// Add facility to test Flush-Denormal-To-Zero (FTZ) and Denormal-As-Zero (DAZ)

utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ libc_test_library(
2222
"//libc:__support_macros_properties_types",
2323
"//libc:__support_osutil_io",
2424
"//libc:__support_uint128",
25-
"//libc:hdr_stdint_proxy",
2625
"//libc:func_aligned_alloc",
2726
"//libc:func_free",
2827
"//libc:func_malloc",
2928
"//libc:func_realloc",
29+
"//libc:hdr_stdint_proxy",
3030
],
3131
)
3232

@@ -65,12 +65,12 @@ libc_test_library(
6565
"//libc:__support_macros_properties_types",
6666
"//libc:__support_stringutil",
6767
"//libc:__support_uint128",
68-
"//libc:hdr_stdint_proxy",
6968
"//libc:errno",
7069
"//libc:func_aligned_alloc",
7170
"//libc:func_free",
7271
"//libc:func_malloc",
7372
"//libc:func_realloc",
73+
"//libc:hdr_stdint_proxy",
7474
"//libc:llvm_libc_macros_stdfix_macros",
7575
"//llvm:Support",
7676
],
@@ -107,6 +107,7 @@ libc_test_library(
107107
],
108108
deps = [
109109
":LibcUnitTest",
110+
":errno_test_helpers",
110111
":string_utils",
111112
":test_logger",
112113
"//libc:__support_cpp_array",
@@ -119,6 +120,7 @@ libc_test_library(
119120
"//libc:__support_fputil_fp_bits",
120121
"//libc:__support_fputil_fpbits_str",
121122
"//libc:__support_fputil_rounding_mode",
123+
"//libc:__support_libc_errno",
122124
"//libc:__support_macros_config",
123125
"//libc:__support_macros_properties_architectures",
124126
"//libc:hdr_fenv_macros",

0 commit comments

Comments
 (0)