1 //===-- Base class for libc unittests ---------------------------*- C++ -*-===//
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
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
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
{
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
};
48 Location(const char *file
, int line
) : file(file
), line(line
) {}
53 TestLogger
&operator<<(TestLogger
&logger
, Location Loc
);
55 #define LOC() __llvm_libc::testing::internal::Location(__FILE__, __LINE__)
58 enum class RunResult
: bool { Pass
, Fail
};
60 RunResult
status() const { return Status
; }
62 void markFail() { Status
= RunResult::Fail
; }
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
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
{
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.
89 internal::RunContext
*Ctx
= nullptr;
91 void setContext(internal::RunContext
*C
) { Ctx
= C
; }
95 virtual void SetUp() {}
96 virtual void TearDown() {}
98 static int runTests(const char *);
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
,
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
);
135 cpp::enable_if_t
<cpp::is_same_v
<ValType
, __llvm_libc::cpp::string_view
>,
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
>,
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
{
170 Callable(Func f
) : f(f
) {}
171 void operator()() override
{ f(); }
174 return new Callable(f
);
178 virtual void Run() = 0;
179 virtual const char *getName() const = 0;
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
)
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
)
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)
224 // Make sure string is null terminated.
226 buffer
[buffer_size
] = '\0';
228 const auto AppendChar
= [&](char c
) {
229 if (buffer_size
> 0) {
235 const auto AppendStr
= [&](const char *str
) {
236 for (; str
&& *str
!= '\0'; ++str
)
242 AppendStr(GetTypeName
<T
>());
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
>
251 template <template <typename
> class TemplatedTestClass
, typename Head
,
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) \
284 constexpr const char *__llvm_libc::testing::internal::GetTypeName<TYPE>() { \
285 return "[ParamType = " #TYPE "]"; \
288 #define TYPED_TEST(SuiteName, TestName, TypeList) \
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 { \
295 using ParamType = T; \
297 SuiteName##_##TestName() { \
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; } \
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> { \
315 using ParamType = T; \
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; } \
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 { \
334 SuiteName##_##TestName() { addTest(this); } \
335 void Run() override; \
336 const char *getName() const override { return #SuiteName "." #TestName; } \
338 SuiteName##_##TestName SuiteName##_##TestName##_Instance; \
339 void SuiteName##_##TestName::Run()
341 #define TEST_F(SuiteClass, TestName) \
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 { \
347 SuiteClass##_##TestName() { addTest(this); } \
348 void Run() override; \
349 const char *getName() const override { return #SuiteClass "." #TestName; } \
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, \
357 #define ASSERT_EQ(LHS, RHS) \
358 if (!EXPECT_EQ(LHS, RHS)) \
361 #define EXPECT_NE(LHS, RHS) \
362 this->test(__llvm_libc::testing::TestCond::NE, (LHS), (RHS), #LHS, #RHS, \
364 #define ASSERT_NE(LHS, RHS) \
365 if (!EXPECT_NE(LHS, RHS)) \
368 #define EXPECT_LT(LHS, RHS) \
369 this->test(__llvm_libc::testing::TestCond::LT, (LHS), (RHS), #LHS, #RHS, \
371 #define ASSERT_LT(LHS, RHS) \
372 if (!EXPECT_LT(LHS, RHS)) \
375 #define EXPECT_LE(LHS, RHS) \
376 this->test(__llvm_libc::testing::TestCond::LE, (LHS), (RHS), #LHS, #RHS, \
378 #define ASSERT_LE(LHS, RHS) \
379 if (!EXPECT_LE(LHS, RHS)) \
382 #define EXPECT_GT(LHS, RHS) \
383 this->test(__llvm_libc::testing::TestCond::GT, (LHS), (RHS), #LHS, #RHS, \
385 #define ASSERT_GT(LHS, RHS) \
386 if (!EXPECT_GT(LHS, RHS)) \
389 #define EXPECT_GE(LHS, RHS) \
390 this->test(__llvm_libc::testing::TestCond::GE, (LHS), (RHS), #LHS, #RHS, \
392 #define ASSERT_GE(LHS, RHS) \
393 if (!EXPECT_GE(LHS, RHS)) \
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)) \
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)) \
406 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true)
408 #define ASSERT_TRUE(VAL) \
409 if (!EXPECT_TRUE(VAL)) \
412 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false)
414 #define ASSERT_FALSE(VAL) \
415 if (!EXPECT_FALSE(VAL)) \
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)) \
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)) \
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) \
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) \
451 if (!EXPECT_THAT(MATCH, MATCHER)) \
455 #define WITH_SIGNAL(X) X
457 #endif // LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H