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
36namespace LIBC_NAMESPACE_DECL {
37namespace 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.
44enum class TestCond { EQ, NE, LT, LE, GT, GE };
45
46struct MatcherBase {
47 virtual ~MatcherBase() {}
48 virtual void explainError() { tlog << "unknown error\n"; }
49 // Override and return true to skip `explainError` step.
50 virtual bool is_silent() const { return false; }
51};
52
53template <typename T> struct Matcher : public MatcherBase {
54 bool match(const T &t);
55};
56
57namespace internal {
58
59// A simple location object to allow consistent passing of __FILE__ and
60// __LINE__.
61struct Location {
62 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.
68TestLogger &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.
74struct 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.
83struct Failure {
84 void operator=(Message msg) {}
85};
86
87struct RunContext {
88 enum class RunResult : bool { Pass, Fail };
89
90 RunResult status() const { return Status; }
91
92 void markFail() { Status = RunResult::Fail; }
93
94private:
95 RunResult Status = RunResult::Pass;
96};
97
98template <typename ValType>
99bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS,
100 const char *LHSStr, const char *RHSStr, Location Loc);
101
102} // namespace internal
103
104struct 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.
115class Test {
116 Test *Next = nullptr;
117 internal::RunContext *Ctx = nullptr;
118
119 void setContext(internal::RunContext *C) { Ctx = C; }
120 static int getNumTests();
121
122public:
123 virtual ~Test() {}
124 virtual void SetUp() {}
125 virtual void TearDown() {}
126
127 static int runTests(const TestOptions &Options);
128
129protected:
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>
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>
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>
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>
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>
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>
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>
200 bool matchAndExplain(MatcherT &&Matcher, ValType Value,
201 const char *MatcherStr, const char *ValueStr,
202 internal::Location Loc) {
203 return testMatch(MatchResult: Matcher.match(Value), Matcher, LHSStr: ValueStr, RHSStr: 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
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
224private:
225 virtual void Run() = 0;
226 virtual const char *getName() const = 0;
227
228 static Test *Start;
229 static Test *End;
230};
231
232extern int argc;
233extern char **argv;
234extern char **envp;
235
236namespace internal {
237
238constexpr 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
245constexpr bool valid_prefix(char const *lhs) {
246 return same_prefix(lhs, rhs: "LlvmLibc", len: 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.
253constexpr 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.
262template <typename ParamType> static constexpr const char *GetTypeName() {
263 return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
264}
265
266template <typename T>
267static 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.
296template <template <typename> class TemplatedTestClass, typename... Types>
297struct TestCreator;
298
299template <template <typename> class TemplatedTestClass, typename Head,
300 typename... Tail>
301struct TestCreator<TemplatedTestClass, Head, Tail...>
302 : private TestCreator<TemplatedTestClass, Tail...> {
303 TemplatedTestClass<Head> instance;
304};
305
306template <template <typename> class TemplatedTestClass>
307struct TestCreator<TemplatedTestClass> {};
308
309// A type list to declare the set of types to instantiate the tests with.
310template <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.
319template <typename... Types> using TypeList = internal::TypeList<Types...>;
320
321CString 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// Helper to trick the compiler into ignoring lack of braces on the else
404// branch. We cannot introduce braces at this point, since it would prevent
405// using `<< ...` after the test macro for additional failure output.
406#define LIBC_TEST_DISABLE_DANGLING_ELSE \
407 switch (0) \
408 case 0: \
409 default: // NOLINT
410
411// If RET_OR_EMPTY is the 'return' keyword we perform an early return which
412// corresponds to an assert. If it is empty the execution continues, this
413// corresponds to an expect.
414//
415// The 'else' clause must not be enclosed into braces so that the << operator
416// can be used to fill the Message.
417//
418// TEST is usually implemented as a function performing checking logic and
419// returning a boolean. This expression is responsible for logging the
420// diagnostic in case of failure.
421#define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \
422 LIBC_TEST_DISABLE_DANGLING_ELSE \
423 if (TEST) \
424 ; \
425 else \
426 RET_OR_EMPTY LIBC_NAMESPACE::testing::internal::Failure() = \
427 LIBC_NAMESPACE::testing::internal::Message()
428
429#define LIBC_TEST_BINOP_(COND, LHS, RHS, RET_OR_EMPTY) \
430 LIBC_TEST_SCAFFOLDING_(test(LIBC_NAMESPACE::testing::TestCond::COND, LHS, \
431 RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \
432 RET_OR_EMPTY)
433
434////////////////////////////////////////////////////////////////////////////////
435// Binary operations corresponding to the TestCond enum.
436
437#define EXPECT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, )
438#define ASSERT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, return)
439
440#define EXPECT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, )
441#define ASSERT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, return)
442
443#define EXPECT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, )
444#define ASSERT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, return)
445
446#define EXPECT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, )
447#define ASSERT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, return)
448
449#define EXPECT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, )
450#define ASSERT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, return)
451
452#define EXPECT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, )
453#define ASSERT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, return)
454
455////////////////////////////////////////////////////////////////////////////////
456// Boolean checks are handled as comparison to the true / false values.
457
458#define EXPECT_TRUE(VAL) EXPECT_EQ(VAL, true)
459#define ASSERT_TRUE(VAL) ASSERT_EQ(VAL, true)
460
461#define EXPECT_FALSE(VAL) EXPECT_EQ(VAL, false)
462#define ASSERT_FALSE(VAL) ASSERT_EQ(VAL, false)
463
464////////////////////////////////////////////////////////////////////////////////
465// String checks.
466
467#define LIBC_TEST_STR_(TEST_FUNC, LHS, RHS, RET_OR_EMPTY) \
468 LIBC_TEST_SCAFFOLDING_(TEST_FUNC(LHS, RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \
469 RET_OR_EMPTY)
470
471#define EXPECT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, )
472#define ASSERT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, return)
473
474#define EXPECT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, )
475#define ASSERT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, return)
476
477////////////////////////////////////////////////////////////////////////////////
478// Subprocess checks.
479
480#ifdef ENABLE_SUBPROCESS_TESTS
481
482#define LIBC_TEST_PROCESS_(TEST_FUNC, FUNC, VALUE, RET_OR_EMPTY) \
483 LIBC_TEST_SCAFFOLDING_( \
484 TEST_FUNC(LIBC_NAMESPACE::testing::Test::createCallable(FUNC), VALUE, \
485 #FUNC, #VALUE, LIBC_TEST_LOC_()), \
486 RET_OR_EMPTY)
487
488#define EXPECT_EXITS(FUNC, EXIT) \
489 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, )
490#define ASSERT_EXITS(FUNC, EXIT) \
491 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, return)
492
493#define EXPECT_DEATH(FUNC, SIG) \
494 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, )
495#define ASSERT_DEATH(FUNC, SIG) \
496 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, return)
497
498#endif // ENABLE_SUBPROCESS_TESTS
499
500////////////////////////////////////////////////////////////////////////////////
501// Custom matcher checks.
502
503#define LIBC_TEST_MATCH_(MATCHER, MATCH, MATCHER_STR, MATCH_STR, RET_OR_EMPTY) \
504 LIBC_TEST_SCAFFOLDING_(matchAndExplain(MATCHER, MATCH, MATCHER_STR, \
505 MATCH_STR, LIBC_TEST_LOC_()), \
506 RET_OR_EMPTY)
507
508#define EXPECT_THAT(MATCH, MATCHER) \
509 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, )
510#define ASSERT_THAT(MATCH, MATCHER) \
511 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, return)
512
513#define WITH_SIGNAL(X) X
514
515#define LIBC_TEST_HAS_MATCHERS() (1)
516
517#endif // LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H
518

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/test/UnitTest/LibcTest.h