|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | | -// This file can only include headers from utils/CPP/. No other header should be |
10 | | -// included. |
| 9 | +#ifndef LLVM_LIBC_UTILS_UNITTEST_H |
| 10 | +#define LLVM_LIBC_UTILS_UNITTEST_H |
| 11 | + |
| 12 | +// This file can only include headers from utils/CPP/ or utils/testutils. No |
| 13 | +// other headers should be included. |
11 | 14 |
|
12 | 15 | #include "utils/CPP/TypeTraits.h" |
| 16 | +#include "utils/testutils/ExecuteFunction.h" |
13 | 17 |
|
14 | 18 | namespace __llvm_libc { |
15 | 19 | namespace testing { |
@@ -89,6 +93,26 @@ class Test { |
89 | 93 | const char *LHSStr, const char *RHSStr, |
90 | 94 | const char *File, unsigned long Line); |
91 | 95 |
|
| 96 | + static bool testProcessExits(RunContext &Ctx, testutils::FunctionCaller *Func, |
| 97 | + int ExitCode, const char *LHSStr, |
| 98 | + const char *RHSStr, const char *File, |
| 99 | + unsigned long Line); |
| 100 | + |
| 101 | + static bool testProcessKilled(RunContext &Ctx, |
| 102 | + testutils::FunctionCaller *Func, int Signal, |
| 103 | + const char *LHSStr, const char *RHSStr, |
| 104 | + const char *File, unsigned long Line); |
| 105 | + |
| 106 | + template <typename Func> testutils::FunctionCaller *createCallable(Func f) { |
| 107 | + struct Callable : public testutils::FunctionCaller { |
| 108 | + Func f; |
| 109 | + Callable(Func f) : f(f) {} |
| 110 | + void operator()() override { f(); } |
| 111 | + }; |
| 112 | + |
| 113 | + return new Callable(f); |
| 114 | + } |
| 115 | + |
92 | 116 | private: |
93 | 117 | virtual void Run(RunContext &Ctx) = 0; |
94 | 118 | virtual const char *getName() const = 0; |
@@ -187,3 +211,23 @@ class Test { |
187 | 211 | #define ASSERT_FALSE(VAL) \ |
188 | 212 | if (!EXPECT_FALSE(VAL)) \ |
189 | 213 | return |
| 214 | + |
| 215 | +#define EXPECT_EXITS(FUNC, EXIT) \ |
| 216 | + __llvm_libc::testing::Test::testProcessExits( \ |
| 217 | + Ctx, __llvm_libc::testing::Test::createCallable(FUNC), EXIT, #FUNC, \ |
| 218 | + #EXIT, __FILE__, __LINE__) |
| 219 | + |
| 220 | +#define ASSERT_EXITS(FUNC, EXIT) \ |
| 221 | + if (!EXPECT_EXITS(FUNC, EXIT)) \ |
| 222 | + return |
| 223 | + |
| 224 | +#define EXPECT_DEATH(FUNC, SIG) \ |
| 225 | + __llvm_libc::testing::Test::testProcessKilled( \ |
| 226 | + Ctx, __llvm_libc::testing::Test::createCallable(FUNC), SIG, #FUNC, #SIG, \ |
| 227 | + __FILE__, __LINE__) |
| 228 | + |
| 229 | +#define ASSERT_DEATH(FUNC, EXIT) \ |
| 230 | + if (!EXPECT_DEATH(FUNC, EXIT)) \ |
| 231 | + return |
| 232 | + |
| 233 | +#endif // LLVM_LIBC_UTILS_UNITTEST_H |
0 commit comments