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 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H
10 #define LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H
11
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 #include "src/__support/macros/config.h"
16 #ifdef libc_make_test_file_path
17 #undef libc_make_test_file_path
18 #endif // libc_make_test_file_path
19
20 // This is defined as a macro here to avoid namespace issues.
21 #define libc_make_test_file_path(file_name) \
22 (LIBC_NAMESPACE::testing::libc_make_test_file_path_func(file_name))
23
24 // This file can only include headers from src/__support/ or test/UnitTest. No
25 // other headers should be included.
26
27 #include "PlatformDefs.h"
28
29 #include "src/__support/CPP/string.h"
30 #include "src/__support/CPP/string_view.h"
31 #include "src/__support/CPP/type_traits.h"
32 #include "src/__support/c_string.h"
33 #include "test/UnitTest/ExecuteFunction.h"
34 #include "test/UnitTest/TestLogger.h"
35
36 namespace LIBC_NAMESPACE_DECL {
37 namespace testing {
38
39 // Only the following conditions are supported. Notice that we do not have
40 // a TRUE or FALSE condition. That is because, C library functions do not
41 // return boolean values, but use integral return values to indicate true or
42 // false conditions. Hence, it is more appropriate to use the other comparison
43 // conditions for such cases.
44 enum class TestCond { EQ, NE, LT, LE, GT, GE };
45
46 struct MatcherBase {
~MatcherBaseMatcherBase47 virtual ~MatcherBase() {}
explainErrorMatcherBase48 virtual void explainError() { tlog << "unknown error\n"; }
49 // Override and return true to skip `explainError` step.
is_silentMatcherBase50 virtual bool is_silent() const { return false; }
51 };
52
53 template <typename T> struct Matcher : public MatcherBase {
54 bool match(const T &t);
55 };
56
57 namespace internal {
58
59 // A simple location object to allow consistent passing of __FILE__ and
60 // __LINE__.
61 struct Location {
LocationLocation62 Location(const char *file, int line) : file(file), line(line) {}
63 const char *file;
64 int line;
65 };
66
67 // Supports writing a failing Location to tlog.
68 TestLogger &operator<<(TestLogger &logger, Location Loc);
69
70 #define LIBC_TEST_LOC_() \
71 LIBC_NAMESPACE::testing::internal::Location(__FILE__, __LINE__)
72
73 // Object to forward custom logging after the EXPECT / ASSERT macros.
74 struct Message {
75 template <typename T> Message &operator<<(T value) {
76 tlog << value;
77 return *this;
78 }
79 };
80
81 // A trivial object to catch the Message, this enables custom logging and
82 // returning from the test function, see LIBC_TEST_SCAFFOLDING_ below.
83 struct Failure {
84 void operator=(Message msg) {}
85 };
86
87 struct RunContext {
88 enum class RunResult : bool { Pass, Fail };
89
statusRunContext90 RunResult status() const { return Status; }
91
markFailRunContext92 void markFail() { Status = RunResult::Fail; }
93
94 private:
95 RunResult Status = RunResult::Pass;
96 };
97
98 template <typename ValType>
99 bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS,
100 const char *LHSStr, const char *RHSStr, Location Loc);
101
102 } // namespace internal
103
104 struct TestOptions {
105 // If set, then just this one test from the suite will be run.
106 const char *TestFilter = nullptr;
107 // Should the test results print color codes to stdout?
108 bool PrintColor = true;
109 // Should the test results print timing only in milliseconds, as GTest does?
110 bool TimeInMs = false;
111 };
112
113 // NOTE: One should not create instances and call methods on them directly. One
114 // should use the macros TEST or TEST_F to write test cases.
115 class Test {
116 Test *Next = nullptr;
117 internal::RunContext *Ctx = nullptr;
118
setContext(internal::RunContext * C)119 void setContext(internal::RunContext *C) { Ctx = C; }
120 static int getNumTests();
121
122 public:
~Test()123 virtual ~Test() {}
SetUp()124 virtual void SetUp() {}
TearDown()125 virtual void TearDown() {}
126
127 static int runTests(const TestOptions &Options);
128
129 protected:
130 static void addTest(Test *T);
131
132 // We make use of a template function, with |LHS| and |RHS| as explicit
133 // parameters, for enhanced type checking. Other gtest like unittest
134 // frameworks have a similar function which takes a boolean argument
135 // instead of the explicit |LHS| and |RHS| arguments. This boolean argument
136 // is the result of the |Cond| operation on |LHS| and |RHS|. Though not bad,
137 // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because
138 // of type promotion.
139 template <
140 typename ValType,
141 cpp::enable_if_t<cpp::is_integral_v<ValType> || is_big_int_v<ValType> ||
142 cpp::is_fixed_point_v<ValType>,
143 int> = 0>
test(TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)144 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
145 const char *RHSStr, internal::Location Loc) {
146 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
147 }
148
149 template <typename ValType,
150 cpp::enable_if_t<cpp::is_enum_v<ValType>, int> = 0>
test(TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)151 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
152 const char *RHSStr, internal::Location Loc) {
153 return internal::test(Ctx, Cond, (long long)LHS, (long long)RHS, LHSStr,
154 RHSStr, Loc);
155 }
156
157 template <typename ValType,
158 cpp::enable_if_t<cpp::is_pointer_v<ValType>, ValType> = nullptr>
test(TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)159 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
160 const char *RHSStr, internal::Location Loc) {
161 return internal::test(Ctx, Cond, (unsigned long long)LHS,
162 (unsigned long long)RHS, LHSStr, RHSStr, Loc);
163 }
164
165 // Helper to allow macro invocations like `ASSERT_EQ(foo, nullptr)`.
166 template <typename ValType,
167 cpp::enable_if_t<cpp::is_pointer_v<ValType>, ValType> = nullptr>
test(TestCond Cond,ValType LHS,cpp::nullptr_t,const char * LHSStr,const char * RHSStr,internal::Location Loc)168 bool test(TestCond Cond, ValType LHS, cpp::nullptr_t, const char *LHSStr,
169 const char *RHSStr, internal::Location Loc) {
170 return test(Cond, LHS, static_cast<ValType>(nullptr), LHSStr, RHSStr, Loc);
171 }
172
173 template <
174 typename ValType,
175 cpp::enable_if_t<
176 cpp::is_same_v<ValType, LIBC_NAMESPACE::cpp::string_view>, int> = 0>
test(TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)177 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
178 const char *RHSStr, internal::Location Loc) {
179 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
180 }
181
182 template <typename ValType,
183 cpp::enable_if_t<
184 cpp::is_same_v<ValType, LIBC_NAMESPACE::cpp::string>, int> = 0>
test(TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)185 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,
186 const char *RHSStr, internal::Location Loc) {
187 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);
188 }
189
190 bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
191 const char *RHSStr, internal::Location Loc);
192
193 bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
194 const char *RHSStr, internal::Location Loc);
195
196 bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
197 const char *RHSStr, internal::Location Loc);
198
199 template <typename MatcherT, typename ValType>
matchAndExplain(MatcherT && Matcher,ValType Value,const char * MatcherStr,const char * ValueStr,internal::Location Loc)200 bool matchAndExplain(MatcherT &&Matcher, ValType Value,
201 const char *MatcherStr, const char *ValueStr,
202 internal::Location Loc) {
203 return testMatch(Matcher.match(Value), Matcher, ValueStr, MatcherStr, Loc);
204 }
205
206 bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
207 const char *LHSStr, const char *RHSStr,
208 internal::Location Loc);
209
210 bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,
211 const char *LHSStr, const char *RHSStr,
212 internal::Location Loc);
213
createCallable(Func f)214 template <typename Func> testutils::FunctionCaller *createCallable(Func f) {
215 struct Callable : public testutils::FunctionCaller {
216 Func f;
217 Callable(Func f) : f(f) {}
218 void operator()() override { f(); }
219 };
220
221 return new Callable(f);
222 }
223
224 private:
225 virtual void Run() = 0;
226 virtual const char *getName() const = 0;
227
228 static Test *Start;
229 static Test *End;
230 };
231
232 extern int argc;
233 extern char **argv;
234 extern char **envp;
235
236 namespace internal {
237
same_prefix(char const * lhs,char const * rhs,int const len)238 constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {
239 for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)
240 if (*lhs != *rhs)
241 return false;
242 return true;
243 }
244
valid_prefix(char const * lhs)245 constexpr bool valid_prefix(char const *lhs) {
246 return same_prefix(lhs, "LlvmLibc", 8);
247 }
248
249 // 'str' is a null terminated string of the form
250 // "const char *LIBC_NAMESPACE::testing::internal::GetTypeName() [ParamType =
251 // XXX]" We return the substring that start at character '[' or a default
252 // message.
GetPrettyFunctionParamType(char const * str)253 constexpr char const *GetPrettyFunctionParamType(char const *str) {
254 for (const char *ptr = str; *ptr != '\0'; ++ptr)
255 if (*ptr == '[')
256 return ptr;
257 return "UNSET : declare with REGISTER_TYPE_NAME";
258 }
259
260 // This function recovers ParamType at compile time by using __PRETTY_FUNCTION__
261 // It can be customized by using the REGISTER_TYPE_NAME macro below.
GetTypeName()262 template <typename ParamType> static constexpr const char *GetTypeName() {
263 return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
264 }
265
266 template <typename T>
GenerateName(char * buffer,int buffer_size,const char * prefix)267 static inline void GenerateName(char *buffer, int buffer_size,
268 const char *prefix) {
269 if (buffer_size == 0)
270 return;
271
272 // Make sure string is null terminated.
273 --buffer_size;
274 buffer[buffer_size] = '\0';
275
276 const auto AppendChar = [&](char c) {
277 if (buffer_size > 0) {
278 *buffer = c;
279 ++buffer;
280 --buffer_size;
281 }
282 };
283 const auto AppendStr = [&](const char *str) {
284 for (; str && *str != '\0'; ++str)
285 AppendChar(*str);
286 };
287
288 AppendStr(prefix);
289 AppendChar(' ');
290 AppendStr(GetTypeName<T>());
291 AppendChar('\0');
292 }
293
294 // TestCreator implements a linear hierarchy of test instances, effectively
295 // instanciating all tests with Types in a single object.
296 template <template <typename> class TemplatedTestClass, typename... Types>
297 struct TestCreator;
298
299 template <template <typename> class TemplatedTestClass, typename Head,
300 typename... Tail>
301 struct TestCreator<TemplatedTestClass, Head, Tail...>
302 : private TestCreator<TemplatedTestClass, Tail...> {
303 TemplatedTestClass<Head> instance;
304 };
305
306 template <template <typename> class TemplatedTestClass>
307 struct TestCreator<TemplatedTestClass> {};
308
309 // A type list to declare the set of types to instantiate the tests with.
310 template <typename... Types> struct TypeList {
311 template <template <typename> class TemplatedTestClass> struct Tests {
312 using type = TestCreator<TemplatedTestClass, Types...>;
313 };
314 };
315
316 } // namespace internal
317
318 // Make TypeList visible in LIBC_NAMESPACE::testing.
319 template <typename... Types> using TypeList = internal::TypeList<Types...>;
320
321 CString libc_make_test_file_path_func(const char *file_name);
322
323 } // namespace testing
324 } // namespace LIBC_NAMESPACE_DECL
325
326 // For TYPED_TEST and TYPED_TEST_F below we need to display which type was used
327 // to run the test. The default will return the fully qualified canonical type
328 // but it can be difficult to read. We provide the following macro to allow the
329 // client to register the type name as they see it in the code.
330 #define REGISTER_TYPE_NAME(TYPE) \
331 template <> \
332 constexpr const char * \
333 LIBC_NAMESPACE::testing::internal::GetTypeName<TYPE>() { \
334 return "[ParamType = " #TYPE "]"; \
335 }
336
337 #define TYPED_TEST(SuiteName, TestName, TypeList) \
338 static_assert( \
339 LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteName), \
340 "All LLVM-libc TYPED_TEST suite names must start with 'LlvmLibc'."); \
341 template <typename T> \
342 class SuiteName##_##TestName : public LIBC_NAMESPACE::testing::Test { \
343 public: \
344 using ParamType = T; \
345 char name[256]; \
346 SuiteName##_##TestName() { \
347 addTest(this); \
348 LIBC_NAMESPACE::testing::internal::GenerateName<T>( \
349 name, sizeof(name), #SuiteName "." #TestName); \
350 } \
351 void Run() override; \
352 const char *getName() const override { return name; } \
353 }; \
354 TypeList::Tests<SuiteName##_##TestName>::type \
355 SuiteName##_##TestName##_Instance; \
356 template <typename T> void SuiteName##_##TestName<T>::Run()
357
358 #define TYPED_TEST_F(SuiteClass, TestName, TypeList) \
359 static_assert(LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteClass), \
360 "All LLVM-libc TYPED_TEST_F suite class names must start " \
361 "with 'LlvmLibc'."); \
362 template <typename T> class SuiteClass##_##TestName : public SuiteClass<T> { \
363 public: \
364 using ParamType = T; \
365 char name[256]; \
366 SuiteClass##_##TestName() { \
367 SuiteClass<T>::addTest(this); \
368 LIBC_NAMESPACE::testing::internal::GenerateName<T>( \
369 name, sizeof(name), #SuiteClass "." #TestName); \
370 } \
371 void Run() override; \
372 const char *getName() const override { return name; } \
373 }; \
374 TypeList::Tests<SuiteClass##_##TestName>::type \
375 SuiteClass##_##TestName##_Instance; \
376 template <typename T> void SuiteClass##_##TestName<T>::Run()
377
378 #define TEST(SuiteName, TestName) \
379 static_assert(LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteName), \
380 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
381 class SuiteName##_##TestName : public LIBC_NAMESPACE::testing::Test { \
382 public: \
383 SuiteName##_##TestName() { addTest(this); } \
384 void Run() override; \
385 const char *getName() const override { return #SuiteName "." #TestName; } \
386 }; \
387 SuiteName##_##TestName SuiteName##_##TestName##_Instance; \
388 void SuiteName##_##TestName::Run()
389
390 #define TEST_F(SuiteClass, TestName) \
391 static_assert( \
392 LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteClass), \
393 "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'."); \
394 class SuiteClass##_##TestName : public SuiteClass { \
395 public: \
396 SuiteClass##_##TestName() { addTest(this); } \
397 void Run() override; \
398 const char *getName() const override { return #SuiteClass "." #TestName; } \
399 }; \
400 SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \
401 void SuiteClass##_##TestName::Run()
402
403 // If RET_OR_EMPTY is the 'return' keyword we perform an early return which
404 // corresponds to an assert. If it is empty the execution continues, this
405 // corresponds to an expect.
406 //
407 // The 'else' clause must not be enclosed into braces so that the << operator
408 // can be used to fill the Message.
409 //
410 // TEST is usually implemented as a function performing checking logic and
411 // returning a boolean. This expression is responsible for logging the
412 // diagnostic in case of failure.
413 #define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \
414 if (TEST) \
415 ; \
416 else \
417 RET_OR_EMPTY LIBC_NAMESPACE::testing::internal::Failure() = \
418 LIBC_NAMESPACE::testing::internal::Message()
419
420 #define LIBC_TEST_BINOP_(COND, LHS, RHS, RET_OR_EMPTY) \
421 LIBC_TEST_SCAFFOLDING_(test(LIBC_NAMESPACE::testing::TestCond::COND, LHS, \
422 RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \
423 RET_OR_EMPTY)
424
425 ////////////////////////////////////////////////////////////////////////////////
426 // Binary operations corresponding to the TestCond enum.
427
428 #define EXPECT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, )
429 #define ASSERT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, return)
430
431 #define EXPECT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, )
432 #define ASSERT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, return)
433
434 #define EXPECT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, )
435 #define ASSERT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, return)
436
437 #define EXPECT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, )
438 #define ASSERT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, return)
439
440 #define EXPECT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, )
441 #define ASSERT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, return)
442
443 #define EXPECT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, )
444 #define ASSERT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, return)
445
446 ////////////////////////////////////////////////////////////////////////////////
447 // Boolean checks are handled as comparison to the true / false values.
448
449 #define EXPECT_TRUE(VAL) EXPECT_EQ(VAL, true)
450 #define ASSERT_TRUE(VAL) ASSERT_EQ(VAL, true)
451
452 #define EXPECT_FALSE(VAL) EXPECT_EQ(VAL, false)
453 #define ASSERT_FALSE(VAL) ASSERT_EQ(VAL, false)
454
455 ////////////////////////////////////////////////////////////////////////////////
456 // String checks.
457
458 #define LIBC_TEST_STR_(TEST_FUNC, LHS, RHS, RET_OR_EMPTY) \
459 LIBC_TEST_SCAFFOLDING_(TEST_FUNC(LHS, RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \
460 RET_OR_EMPTY)
461
462 #define EXPECT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, )
463 #define ASSERT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, return)
464
465 #define EXPECT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, )
466 #define ASSERT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, return)
467
468 ////////////////////////////////////////////////////////////////////////////////
469 // Subprocess checks.
470
471 #ifdef ENABLE_SUBPROCESS_TESTS
472
473 #define LIBC_TEST_PROCESS_(TEST_FUNC, FUNC, VALUE, RET_OR_EMPTY) \
474 LIBC_TEST_SCAFFOLDING_( \
475 TEST_FUNC(LIBC_NAMESPACE::testing::Test::createCallable(FUNC), VALUE, \
476 #FUNC, #VALUE, LIBC_TEST_LOC_()), \
477 RET_OR_EMPTY)
478
479 #define EXPECT_EXITS(FUNC, EXIT) \
480 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, )
481 #define ASSERT_EXITS(FUNC, EXIT) \
482 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, return)
483
484 #define EXPECT_DEATH(FUNC, SIG) \
485 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, )
486 #define ASSERT_DEATH(FUNC, SIG) \
487 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, return)
488
489 #endif // ENABLE_SUBPROCESS_TESTS
490
491 ////////////////////////////////////////////////////////////////////////////////
492 // Custom matcher checks.
493
494 #define LIBC_TEST_MATCH_(MATCHER, MATCH, MATCHER_STR, MATCH_STR, RET_OR_EMPTY) \
495 LIBC_TEST_SCAFFOLDING_(matchAndExplain(MATCHER, MATCH, MATCHER_STR, \
496 MATCH_STR, LIBC_TEST_LOC_()), \
497 RET_OR_EMPTY)
498
499 #define EXPECT_THAT(MATCH, MATCHER) \
500 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, )
501 #define ASSERT_THAT(MATCH, MATCHER) \
502 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, return)
503
504 #define WITH_SIGNAL(X) X
505
506 #define LIBC_TEST_HAS_MATCHERS() (1)
507
508 #endif // LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H
509