[RDF] Add RegisterRef::idx and make toUnitId constexpr
[llvm-project.git] / libc / test / UnitTest / LibcTest.h
blob270f5d1da025a09d33cc2abf08690d5ee3f99e30
1 //===-- Base class for libc unittests ---------------------------*- C++ -*-===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
10 #define LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
12 // This is defined as a simple macro in test.h so that it exists for platforms
13 // that don't use our test infrastructure. It's defined as a proper function
14 // below.
15 #ifdef libc_make_test_file_path
16 #undef libc_make_test_file_path
17 #endif // libc_make_test_file_path
19 // This is defined as a macro here to avoid namespace issues.
20 #define libc_make_test_file_path(file_name) \
21 (__llvm_libc::testing::libc_make_test_file_path_func(file_name))
23 // This file can only include headers from src/__support/ or test/UnitTest. No
24 // other headers should be included.
26 #include "PlatformDefs.h"
28 #include "src/__support/CPP/string.h"
29 #include "src/__support/CPP/string_view.h"
30 #include "src/__support/CPP/type_traits.h"
31 #include "src/__support/c_string.h"
32 #include "test/UnitTest/ExecuteFunction.h"
33 #include "test/UnitTest/TestLogger.h"
35 namespace __llvm_libc {
36 namespace testing {
38 // Only the following conditions are supported. Notice that we do not have
39 // a TRUE or FALSE condition. That is because, C library functions do not
40 // return boolean values, but use integral return values to indicate true or
41 // false conditions. Hence, it is more appropriate to use the other comparison
42 // conditions for such cases.
43 enum class TestCond { EQ, NE, LT, LE, GT, GE };
45 namespace internal {
47 struct Location {
48 Location(const char *file, int line) : file(file), line(line) {}
49 const char *file;
50 int line;
53 TestLogger &operator<<(TestLogger &logger, Location Loc);
55 #define LOC() __llvm_libc::testing::internal::Location(__FILE__, __LINE__)
57 struct RunContext {
58 enum class RunResult : bool { Pass, Fail };
60 RunResult status() const { return Status; }
62 void markFail() { Status = RunResult::Fail; }
64 private:
65 RunResult Status = RunResult::Pass;
68 template <typename ValType>
69 bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS,
70 const char *LHSStr, const char *RHSStr, Location Loc);
72 } // namespace internal
74 struct MatcherBase {
75 virtual ~MatcherBase() {}
76 virtual void explainError() { tlog << "unknown error\n"; }
77 // Override and return true to skip `explainError` step.
78 virtual bool is_silent() const { return false; }
81 template <typename T> struct Matcher : public MatcherBase {
82 bool match(T &t);
85 // NOTE: One should not create instances and call methods on them directly. One
86 // should use the macros TEST or TEST_F to write test cases.
87 class Test {
88 Test *Next = nullptr;
89 internal::RunContext *Ctx = nullptr;
91 void setContext(internal::RunContext *C) { Ctx = C; }
93 public:
94 virtual ~Test() {}
95 virtual void SetUp() {}
96 virtual void TearDown() {}
98 static int runTests(const char *);
100 protected:
101 static void addTest(Test *T);
103 // We make use of a template function, with |LHS| and |RHS| as explicit
104 // parameters, for enhanced type checking. Other gtest like unittest
105 // frameworks have a similar function which takes a boolean argument
106 // instead of the explicit |LHS| and |RHS| arguments. This boolean argument
107 // is the result of the |Cond| operation on |LHS| and |RHS|. Though not bad,
108 // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because
109 // of type promotion.
110 template <typename ValType,
111 cpp::enable_if_t<cpp::is_integral_v<ValType>, int> = 0>
112 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
113 const char *RHSStr, internal::Location Loc) {
114 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
117 template <typename ValType,
118 cpp::enable_if_t<cpp::is_enum_v<ValType>, int> = 0>
119 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
120 const char *RHSStr, internal::Location Loc) {
121 return internal::test(Ctx, Cond, (long long)LHS, (long long)RHS, LHSStr,
122 RHSStr, Loc);
125 template <typename ValType,
126 cpp::enable_if_t<cpp::is_pointer_v<ValType>, ValType> = nullptr>
127 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
128 const char *RHSStr, internal::Location Loc) {
129 return internal::test(Ctx, Cond, (unsigned long long)LHS,
130 (unsigned long long)RHS, LHSStr, RHSStr, Loc);
133 template <
134 typename ValType,
135 cpp::enable_if_t<cpp::is_same_v<ValType, __llvm_libc::cpp::string_view>,
136 int> = 0>
137 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
138 const char *RHSStr, internal::Location Loc) {
139 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
142 template <typename ValType,
143 cpp::enable_if_t<cpp::is_same_v<ValType, __llvm_libc::cpp::string>,
144 int> = 0>
145 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
146 const char *RHSStr, internal::Location Loc) {
147 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
150 bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
151 const char *RHSStr, internal::Location Loc);
153 bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
154 const char *RHSStr, internal::Location Loc);
156 bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
157 const char *RHSStr, internal::Location Loc);
159 bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
160 const char *LHSStr, const char *RHSStr,
161 internal::Location Loc);
163 bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,
164 const char *LHSStr, const char *RHSStr,
165 internal::Location Loc);
167 template <typename Func> testutils::FunctionCaller *createCallable(Func f) {
168 struct Callable : public testutils::FunctionCaller {
169 Func f;
170 Callable(Func f) : f(f) {}
171 void operator()() override { f(); }
174 return new Callable(f);
177 private:
178 virtual void Run() = 0;
179 virtual const char *getName() const = 0;
181 static Test *Start;
182 static Test *End;
185 extern int argc;
186 extern char **argv;
187 extern char **envp;
189 namespace internal {
191 constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {
192 for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)
193 if (*lhs != *rhs)
194 return false;
195 return true;
198 constexpr bool valid_prefix(char const *lhs) {
199 return same_prefix(lhs, "LlvmLibc", 8);
202 // 'str' is a null terminated string of the form
203 // "const char *__llvm_libc::testing::internal::GetTypeName() [ParamType = XXX]"
204 // We return the substring that start at character '[' or a default message.
205 constexpr char const *GetPrettyFunctionParamType(char const *str) {
206 for (const char *ptr = str; *ptr != '\0'; ++ptr)
207 if (*ptr == '[')
208 return ptr;
209 return "UNSET : declare with REGISTER_TYPE_NAME";
212 // This function recovers ParamType at compile time by using __PRETTY_FUNCTION__
213 // It can be customized by using the REGISTER_TYPE_NAME macro below.
214 template <typename ParamType> static constexpr const char *GetTypeName() {
215 return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
218 template <typename T>
219 static inline void GenerateName(char *buffer, int buffer_size,
220 const char *prefix) {
221 if (buffer_size == 0)
222 return;
224 // Make sure string is null terminated.
225 --buffer_size;
226 buffer[buffer_size] = '\0';
228 const auto AppendChar = [&](char c) {
229 if (buffer_size > 0) {
230 *buffer = c;
231 ++buffer;
232 --buffer_size;
235 const auto AppendStr = [&](const char *str) {
236 for (; str && *str != '\0'; ++str)
237 AppendChar(*str);
240 AppendStr(prefix);
241 AppendChar(' ');
242 AppendStr(GetTypeName<T>());
243 AppendChar('\0');
246 // TestCreator implements a linear hierarchy of test instances, effectively
247 // instanciating all tests with Types in a single object.
248 template <template <typename> class TemplatedTestClass, typename... Types>
249 struct TestCreator;
251 template <template <typename> class TemplatedTestClass, typename Head,
252 typename... Tail>
253 struct TestCreator<TemplatedTestClass, Head, Tail...>
254 : private TestCreator<TemplatedTestClass, Tail...> {
255 TemplatedTestClass<Head> instance;
258 template <template <typename> class TemplatedTestClass>
259 struct TestCreator<TemplatedTestClass> {};
261 // A type list to declare the set of types to instantiate the tests with.
262 template <typename... Types> struct TypeList {
263 template <template <typename> class TemplatedTestClass> struct Tests {
264 using type = TestCreator<TemplatedTestClass, Types...>;
268 } // namespace internal
270 // Make TypeList visible in __llvm_libc::testing.
271 template <typename... Types> using TypeList = internal::TypeList<Types...>;
273 CString libc_make_test_file_path_func(const char *file_name);
275 } // namespace testing
276 } // namespace __llvm_libc
278 // For TYPED_TEST and TYPED_TEST_F below we need to display which type was used
279 // to run the test. The default will return the fully qualified canonical type
280 // but it can be difficult to read. We provide the following macro to allow the
281 // client to register the type name as they see it in the code.
282 #define REGISTER_TYPE_NAME(TYPE) \
283 template <> \
284 constexpr const char *__llvm_libc::testing::internal::GetTypeName<TYPE>() { \
285 return "[ParamType = " #TYPE "]"; \
288 #define TYPED_TEST(SuiteName, TestName, TypeList) \
289 static_assert( \
290 __llvm_libc::testing::internal::valid_prefix(#SuiteName), \
291 "All LLVM-libc TYPED_TEST suite names must start with 'LlvmLibc'."); \
292 template <typename T> \
293 class SuiteName##_##TestName : public __llvm_libc::testing::Test { \
294 public: \
295 using ParamType = T; \
296 char name[256]; \
297 SuiteName##_##TestName() { \
298 addTest(this); \
299 __llvm_libc::testing::internal::GenerateName<T>( \
300 name, sizeof(name), #SuiteName "." #TestName); \
302 void Run() override; \
303 const char *getName() const override { return name; } \
304 }; \
305 TypeList::Tests<SuiteName##_##TestName>::type \
306 SuiteName##_##TestName##_Instance; \
307 template <typename T> void SuiteName##_##TestName<T>::Run()
309 #define TYPED_TEST_F(SuiteClass, TestName, TypeList) \
310 static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteClass), \
311 "All LLVM-libc TYPED_TEST_F suite class names must start " \
312 "with 'LlvmLibc'."); \
313 template <typename T> class SuiteClass##_##TestName : public SuiteClass<T> { \
314 public: \
315 using ParamType = T; \
316 char name[256]; \
317 SuiteClass##_##TestName() { \
318 SuiteClass<T>::addTest(this); \
319 __llvm_libc::testing::internal::GenerateName<T>( \
320 name, sizeof(name), #SuiteClass "." #TestName); \
322 void Run() override; \
323 const char *getName() const override { return name; } \
324 }; \
325 TypeList::Tests<SuiteClass##_##TestName>::type \
326 SuiteClass##_##TestName##_Instance; \
327 template <typename T> void SuiteClass##_##TestName<T>::Run()
329 #define TEST(SuiteName, TestName) \
330 static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteName), \
331 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
332 class SuiteName##_##TestName : public __llvm_libc::testing::Test { \
333 public: \
334 SuiteName##_##TestName() { addTest(this); } \
335 void Run() override; \
336 const char *getName() const override { return #SuiteName "." #TestName; } \
337 }; \
338 SuiteName##_##TestName SuiteName##_##TestName##_Instance; \
339 void SuiteName##_##TestName::Run()
341 #define TEST_F(SuiteClass, TestName) \
342 static_assert( \
343 __llvm_libc::testing::internal::valid_prefix(#SuiteClass), \
344 "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'."); \
345 class SuiteClass##_##TestName : public SuiteClass { \
346 public: \
347 SuiteClass##_##TestName() { addTest(this); } \
348 void Run() override; \
349 const char *getName() const override { return #SuiteClass "." #TestName; } \
350 }; \
351 SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \
352 void SuiteClass##_##TestName::Run()
354 #define EXPECT_EQ(LHS, RHS) \
355 this->test(__llvm_libc::testing::TestCond::EQ, (LHS), (RHS), #LHS, #RHS, \
356 LOC())
357 #define ASSERT_EQ(LHS, RHS) \
358 if (!EXPECT_EQ(LHS, RHS)) \
359 return
361 #define EXPECT_NE(LHS, RHS) \
362 this->test(__llvm_libc::testing::TestCond::NE, (LHS), (RHS), #LHS, #RHS, \
363 LOC())
364 #define ASSERT_NE(LHS, RHS) \
365 if (!EXPECT_NE(LHS, RHS)) \
366 return
368 #define EXPECT_LT(LHS, RHS) \
369 this->test(__llvm_libc::testing::TestCond::LT, (LHS), (RHS), #LHS, #RHS, \
370 LOC())
371 #define ASSERT_LT(LHS, RHS) \
372 if (!EXPECT_LT(LHS, RHS)) \
373 return
375 #define EXPECT_LE(LHS, RHS) \
376 this->test(__llvm_libc::testing::TestCond::LE, (LHS), (RHS), #LHS, #RHS, \
377 LOC())
378 #define ASSERT_LE(LHS, RHS) \
379 if (!EXPECT_LE(LHS, RHS)) \
380 return
382 #define EXPECT_GT(LHS, RHS) \
383 this->test(__llvm_libc::testing::TestCond::GT, (LHS), (RHS), #LHS, #RHS, \
384 LOC())
385 #define ASSERT_GT(LHS, RHS) \
386 if (!EXPECT_GT(LHS, RHS)) \
387 return
389 #define EXPECT_GE(LHS, RHS) \
390 this->test(__llvm_libc::testing::TestCond::GE, (LHS), (RHS), #LHS, #RHS, \
391 LOC())
392 #define ASSERT_GE(LHS, RHS) \
393 if (!EXPECT_GE(LHS, RHS)) \
394 return
396 #define EXPECT_STREQ(LHS, RHS) this->testStrEq((LHS), (RHS), #LHS, #RHS, LOC())
397 #define ASSERT_STREQ(LHS, RHS) \
398 if (!EXPECT_STREQ(LHS, RHS)) \
399 return
401 #define EXPECT_STRNE(LHS, RHS) this->testStrNe((LHS), (RHS), #LHS, #RHS, LOC())
402 #define ASSERT_STRNE(LHS, RHS) \
403 if (!EXPECT_STRNE(LHS, RHS)) \
404 return
406 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true)
408 #define ASSERT_TRUE(VAL) \
409 if (!EXPECT_TRUE(VAL)) \
410 return
412 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false)
414 #define ASSERT_FALSE(VAL) \
415 if (!EXPECT_FALSE(VAL)) \
416 return
418 #ifdef ENABLE_SUBPROCESS_TESTS
420 #define EXPECT_EXITS(FUNC, EXIT) \
421 this->testProcessExits(__llvm_libc::testing::Test::createCallable(FUNC), \
422 EXIT, #FUNC, #EXIT, LOC())
424 #define ASSERT_EXITS(FUNC, EXIT) \
425 if (!EXPECT_EXITS(FUNC, EXIT)) \
426 return
428 #define EXPECT_DEATH(FUNC, SIG) \
429 this->testProcessKilled(__llvm_libc::testing::Test::createCallable(FUNC), \
430 SIG, #FUNC, #SIG, LOC())
432 #define ASSERT_DEATH(FUNC, EXIT) \
433 if (!EXPECT_DEATH(FUNC, EXIT)) \
434 return
436 #endif // ENABLE_SUBPROCESS_TESTS
438 #define __CAT1(a, b) a##b
439 #define __CAT(a, b) __CAT1(a, b)
440 #define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__)
442 #define EXPECT_THAT(MATCH, MATCHER) \
443 [&]() -> bool { \
444 auto UNIQUE_VAR(__matcher) = (MATCHER); \
445 return this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \
446 UNIQUE_VAR(__matcher), #MATCH, #MATCHER, LOC()); \
449 #define ASSERT_THAT(MATCH, MATCHER) \
450 do { \
451 if (!EXPECT_THAT(MATCH, MATCHER)) \
452 return; \
453 } while (0)
455 #define WITH_SIGNAL(X) X
457 #endif // LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H