-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc][test] adds errno clearer test fixture, gtest-style errno and fp except assertions #91608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
b78e0a6
f058e2f
c3e6484
a215bcf
56e8d88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===-- ErrnoSafeTestFixture.h ---------------------------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_TEST_UNITTEST_ERRNOSAFETEST_H | ||
#define LLVM_LIBC_TEST_UNITTEST_ERRNOSAFETEST_H | ||
|
||
#include "src/__support/CPP/utility.h" | ||
#include "src/errno/libc_errno.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
namespace LIBC_NAMESPACE::testing { | ||
|
||
// This is a test fixture for clearing errno before the start of a test case. | ||
class ErrnoSafeTest : virtual public Test { | ||
public: | ||
void SetUp() override { LIBC_NAMESPACE::libc_errno = 0; } | ||
}; | ||
|
||
} // namespace LIBC_NAMESPACE::testing | ||
|
||
#endif // LLVM_LIBC_TEST_UNITTEST_ERRNOSAFETEST_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ namespace LIBC_NAMESPACE::testing { | |
// This provides a test fixture (or base class for other test fixtures) that | ||
// asserts that each test does not leave the FPU state represented by `fenv_t` | ||
// (aka `FPState`) perturbed from its initial state. | ||
class FEnvSafeTest : public Test { | ||
class FEnvSafeTest : virtual public Test { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this to virtual to let FPTest subclass both fixtures:
|
||
public: | ||
void TearDown() override; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#ifndef LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H | ||
#define LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H | ||
|
||
#include "hdr/fenv_macros.h" | ||
#include "test/UnitTest/Test.h" | ||
#include "test/UnitTest/TestLogger.h" | ||
|
||
|
@@ -17,9 +18,71 @@ | |
namespace LIBC_NAMESPACE { | ||
namespace testing { | ||
|
||
// Used to compare FP exception flag states with nice error printing | ||
class FPExceptMatcher : public Matcher<int> { | ||
const int expected; | ||
int actual; | ||
|
||
public: | ||
explicit FPExceptMatcher(int expected) : expected(expected) {} | ||
|
||
void explainError() override { | ||
tlog << "Expected floating point exceptions: " << expected << ' '; | ||
printExcepts(expected); | ||
tlog << '\n'; | ||
|
||
tlog << "Actual floating point exceptions: " << actual << ' '; | ||
printExcepts(actual); | ||
tlog << '\n'; | ||
} | ||
|
||
bool match(int got) { | ||
actual = got; | ||
return got == expected; | ||
} | ||
|
||
private: | ||
void printExcepts(int excepts) { | ||
if (!excepts) { | ||
tlog << "(no exceptions)"; | ||
return; | ||
} | ||
|
||
bool firstPrinted = false; | ||
auto printWithPipe = [&](const char *name) { | ||
if (firstPrinted) | ||
tlog << " | "; | ||
|
||
tlog << name; | ||
|
||
firstPrinted = true; | ||
}; | ||
|
||
tlog << '('; | ||
|
||
if (FE_DIVBYZERO & excepts) | ||
printWithPipe("FE_DIVBYZERO"); | ||
|
||
if (FE_INEXACT & excepts) | ||
printWithPipe("FE_INEXACT"); | ||
|
||
if (FE_INVALID & excepts) | ||
printWithPipe("FE_INVALID"); | ||
|
||
if (FE_OVERFLOW & excepts) | ||
printWithPipe("FE_OVERFLOW"); | ||
|
||
if (FE_UNDERFLOW & excepts) | ||
printWithPipe("FE_UNDERFLOW"); | ||
|
||
tlog << ')'; | ||
} | ||
}; | ||
|
||
// TODO: Make the matcher match specific exceptions instead of just identifying | ||
// that an exception was raised. | ||
class FPExceptMatcher : public Matcher<bool> { | ||
// Used in death tests for fenv | ||
class FPExceptCallableMatcher : public Matcher<bool> { | ||
bool exceptionRaised; | ||
|
||
public: | ||
|
@@ -40,7 +103,7 @@ class FPExceptMatcher : public Matcher<bool> { | |
} | ||
|
||
// Takes ownership of func. | ||
explicit FPExceptMatcher(FunctionCaller *func); | ||
explicit FPExceptCallableMatcher(FunctionCaller *func); | ||
|
||
bool match(bool unused) { return exceptionRaised; } | ||
|
||
|
@@ -53,11 +116,42 @@ class FPExceptMatcher : public Matcher<bool> { | |
} // namespace testing | ||
} // namespace LIBC_NAMESPACE | ||
|
||
// Matches on the FP exception flag `expected` being *equal* to FP exception | ||
// flag `actual` | ||
#define EXPECT_FP_EXCEPT_EQUAL(expected, actual) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd prefer to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the changes in 56e8d88. I've also changed all the other macros with both |
||
EXPECT_THAT((actual), LIBC_NAMESPACE::testing::FPExceptMatcher((expected))) | ||
|
||
#define ASSERT_FP_EXCEPT_EQUAL(expected, actual) \ | ||
ASSERT_THAT((actual), LIBC_NAMESPACE::testing::FPExceptMatcher((expected))) | ||
|
||
#define ASSERT_RAISES_FP_EXCEPT(func) \ | ||
ASSERT_THAT( \ | ||
true, \ | ||
LIBC_NAMESPACE::testing::FPExceptMatcher( \ | ||
LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func))) | ||
LIBC_NAMESPACE::testing::FPExceptCallableMatcher( \ | ||
LIBC_NAMESPACE::testing::FPExceptCallableMatcher::getFunctionCaller( \ | ||
func))) | ||
|
||
// Does not return the value of `expr_or_statement`, i.e., intended usage | ||
// is: `EXPECT_FP_EXCEPT(FE_INVALID, EXPECT_FP_EQ(..., ...));` or | ||
// ``` | ||
// EXPECT_FP_EXCEPT(FE_ALL_EXCEPT, { | ||
// stmt; | ||
// ... | ||
// }); | ||
// ``` | ||
// Ensures that fp excepts are cleared before executing `expr_or_statement` | ||
// Checking (expected = 0) should ensure that no exceptions were set | ||
#define EXPECT_FP_EXCEPT(expected, expr_or_statement) \ | ||
do { \ | ||
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \ | ||
expr_or_statement; \ | ||
int expected_ = (expected); \ | ||
int mask_ = expected_ ? expected_ : FE_ALL_EXCEPT; \ | ||
if (math_errhandling & MATH_ERREXCEPT) { \ | ||
EXPECT_FP_EXCEPT_EQUAL(expected_, \ | ||
LIBC_NAMESPACE::fputil::test_except(mask_)); \ | ||
} \ | ||
} while (0) | ||
|
||
#else // !LIBC_TEST_HAS_MATCHERS() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually add
.ErrnoSetterMatcher
as relative paths. Is it better to unify the style?cc @lntue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm down to make the change. Should we change the other names in this file too? lmk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @SchrodingerZhu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry for missing the reply. I am fine with unifying all of them at once.