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_UTILS_UNITTEST_LIBCTEST_H
10 #define LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
11 
12 // This file can only include headers from src/__support/CPP/ or
13 // utils/testutils. No other headers should be included.
14 
15 #include "PlatformDefs.h"
16 
17 #include "src/__support/CPP/TypeTraits.h"
18 #include "utils/testutils/ExecuteFunction.h"
19 #include "utils/testutils/StreamWrapper.h"
20 
21 namespace __llvm_libc {
22 namespace testing {
23 
24 class RunContext;
25 
26 // Only the following conditions are supported. Notice that we do not have
27 // a TRUE or FALSE condition. That is because, C library funtions do not
28 // return boolean values, but use integral return values to indicate true or
29 // false conditions. Hence, it is more appropriate to use the other comparison
30 // conditions for such cases.
31 enum TestCondition {
32   Cond_None,
33   Cond_EQ,
34   Cond_NE,
35   Cond_LT,
36   Cond_LE,
37   Cond_GT,
38   Cond_GE,
39 };
40 
41 namespace internal {
42 
43 template <typename ValType>
44 bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
45           const char *LHSStr, const char *RHSStr, const char *File,
46           unsigned long Line);
47 
48 } // namespace internal
49 
50 struct MatcherBase {
~MatcherBaseMatcherBase51   virtual ~MatcherBase() {}
explainErrorMatcherBase52   virtual void explainError(testutils::StreamWrapper &OS) {
53     OS << "unknown error\n";
54   }
55 };
56 
57 template <typename T> struct Matcher : public MatcherBase { bool match(T &t); };
58 
59 // NOTE: One should not create instances and call methods on them directly. One
60 // should use the macros TEST or TEST_F to write test cases.
61 class Test {
62 private:
63   Test *Next = nullptr;
64   RunContext *Ctx = nullptr;
65 
setContext(RunContext * C)66   void setContext(RunContext *C) { Ctx = C; }
67 
68 public:
~Test()69   virtual ~Test() {}
SetUp()70   virtual void SetUp() {}
TearDown()71   virtual void TearDown() {}
72 
73   static int runTests(const char *);
74 
75 protected:
76   static void addTest(Test *T);
77 
78   // We make use of a template function, with |LHS| and |RHS| as explicit
79   // parameters, for enhanced type checking. Other gtest like unittest
80   // frameworks have a similar function which takes a boolean argument
81   // instead of the explicit |LHS| and |RHS| arguments. This boolean argument
82   // is the result of the |Cond| operation on |LHS| and |RHS|. Though not bad,
83   // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because
84   // of type promotion.
85   template <typename ValType,
86             cpp::EnableIfType<cpp::IsIntegral<ValType>::Value, int> = 0>
test(TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)87   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
88             const char *RHSStr, const char *File, unsigned long Line) {
89     return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, File, Line);
90   }
91 
92   template <typename ValType,
93             cpp::EnableIfType<cpp::IsEnum<ValType>::Value, int> = 0>
test(TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)94   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
95             const char *RHSStr, const char *File, unsigned long Line) {
96     return internal::test(Ctx, Cond, (long long)LHS, (long long)RHS, LHSStr,
97                           RHSStr, File, Line);
98   }
99 
100   template <
101       typename ValType,
102       cpp::EnableIfType<cpp::IsPointerType<ValType>::Value, ValType> = nullptr>
test(TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)103   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
104             const char *RHSStr, const char *File, unsigned long Line) {
105     return internal::test(Ctx, Cond, (unsigned long long)LHS,
106                           (unsigned long long)RHS, LHSStr, RHSStr, File, Line);
107   }
108 
109   bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
110                  const char *RHSStr, const char *File, unsigned long Line);
111 
112   bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
113                  const char *RHSStr, const char *File, unsigned long Line);
114 
115   bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
116                  const char *RHSStr, const char *File, unsigned long Line);
117 
118   bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
119                         const char *LHSStr, const char *RHSStr,
120                         const char *File, unsigned long Line);
121 
122   bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,
123                          const char *LHSStr, const char *RHSStr,
124                          const char *File, unsigned long Line);
125 
createCallable(Func f)126   template <typename Func> testutils::FunctionCaller *createCallable(Func f) {
127     struct Callable : public testutils::FunctionCaller {
128       Func f;
129       Callable(Func f) : f(f) {}
130       void operator()() override { f(); }
131     };
132 
133     return new Callable(f);
134   }
135 
136 private:
137   virtual void Run() = 0;
138   virtual const char *getName() const = 0;
139 
140   static Test *Start;
141   static Test *End;
142 };
143 
144 namespace internal {
145 
same_prefix(char const * lhs,char const * rhs,int const len)146 constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {
147   for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)
148     if (*lhs != *rhs)
149       return false;
150   return true;
151 }
152 
valid_prefix(char const * lhs)153 constexpr bool valid_prefix(char const *lhs) {
154   return same_prefix(lhs, "LlvmLibc", 8);
155 }
156 
157 // 'str' is a null terminated string of the form
158 // "const char *__llvm_libc::testing::internal::GetTypeName() [ParamType = XXX]"
159 // We return the substring that start at character '[' or a default message.
GetPrettyFunctionParamType(char const * str)160 constexpr char const *GetPrettyFunctionParamType(char const *str) {
161   for (const char *ptr = str; *ptr != '\0'; ++ptr)
162     if (*ptr == '[')
163       return ptr;
164   return "UNSET : declare with REGISTER_TYPE_NAME";
165 }
166 
167 // This function recovers ParamType at compile time by using __PRETTY_FUNCTION__
168 // It can be customized by using the REGISTER_TYPE_NAME macro below.
GetTypeName()169 template <typename ParamType> static constexpr const char *GetTypeName() {
170   return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
171 }
172 
173 template <typename T>
GenerateName(char * buffer,int buffer_size,const char * prefix)174 static inline void GenerateName(char *buffer, int buffer_size,
175                                 const char *prefix) {
176   if (buffer_size == 0)
177     return;
178 
179   // Make sure string is null terminated.
180   --buffer_size;
181   buffer[buffer_size] = '\0';
182 
183   const auto AppendChar = [&](char c) {
184     if (buffer_size > 0) {
185       *buffer = c;
186       ++buffer;
187       --buffer_size;
188     }
189   };
190   const auto AppendStr = [&](const char *str) {
191     for (; str && *str != '\0'; ++str)
192       AppendChar(*str);
193   };
194 
195   AppendStr(prefix);
196   AppendChar(' ');
197   AppendStr(GetTypeName<T>());
198   AppendChar('\0');
199 }
200 
201 // TestCreator implements a linear hierarchy of test instances, effectively
202 // instanciating all tests with Types in a single object.
203 template <template <typename> class TemplatedTestClass, typename... Types>
204 struct TestCreator;
205 
206 template <template <typename> class TemplatedTestClass, typename Head,
207           typename... Tail>
208 struct TestCreator<TemplatedTestClass, Head, Tail...>
209     : private TestCreator<TemplatedTestClass, Tail...> {
210   TemplatedTestClass<Head> instance;
211 };
212 
213 template <template <typename> class TemplatedTestClass>
214 struct TestCreator<TemplatedTestClass> {};
215 
216 // A type list to declare the set of types to instantiate the tests with.
217 template <typename... Types> struct TypeList {
218   template <template <typename> class TemplatedTestClass> struct Tests {
219     using type = TestCreator<TemplatedTestClass, Types...>;
220   };
221 };
222 
223 } // namespace internal
224 
225 // Make TypeList visible in __llvm_libc::testing.
226 template <typename... Types> using TypeList = internal::TypeList<Types...>;
227 
228 } // namespace testing
229 } // namespace __llvm_libc
230 
231 // For TYPED_TEST and TYPED_TEST_F below we need to display which type was used
232 // to run the test. The default will return the fully qualified canonical type
233 // but it can be difficult to read. We provide the following macro to allow the
234 // client to register the type name as they see it in the code.
235 #define REGISTER_TYPE_NAME(TYPE)                                               \
236   template <>                                                                  \
237   constexpr const char *__llvm_libc::testing::internal::GetTypeName<TYPE>() {  \
238     return "[ParamType = " #TYPE "]";                                          \
239   }
240 
241 #define TYPED_TEST(SuiteName, TestName, TypeList)                              \
242   static_assert(                                                               \
243       __llvm_libc::testing::internal::valid_prefix(#SuiteName),                \
244       "All LLVM-libc TYPED_TEST suite names must start with 'LlvmLibc'.");     \
245   template <typename T>                                                        \
246   class SuiteName##_##TestName : public __llvm_libc::testing::Test {           \
247   public:                                                                      \
248     using ParamType = T;                                                       \
249     char name[256];                                                            \
250     SuiteName##_##TestName() {                                                 \
251       addTest(this);                                                           \
252       __llvm_libc::testing::internal::GenerateName<T>(                         \
253           name, sizeof(name), #SuiteName "." #TestName);                       \
254     }                                                                          \
255     void Run() override;                                                       \
256     const char *getName() const override { return name; }                      \
257   };                                                                           \
258   TypeList::Tests<SuiteName##_##TestName>::type                                \
259       SuiteName##_##TestName##_Instance;                                       \
260   template <typename T> void SuiteName##_##TestName<T>::Run()
261 
262 #define TYPED_TEST_F(SuiteClass, TestName, TypeList)                           \
263   static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteClass),     \
264                 "All LLVM-libc TYPED_TEST_F suite class names must start "     \
265                 "with 'LlvmLibc'.");                                           \
266   template <typename T> class SuiteClass##_##TestName : public SuiteClass<T> { \
267   public:                                                                      \
268     using ParamType = T;                                                       \
269     char name[256];                                                            \
270     SuiteClass##_##TestName() {                                                \
271       SuiteClass<T>::addTest(this);                                            \
272       __llvm_libc::testing::internal::GenerateName<T>(                         \
273           name, sizeof(name), #SuiteClass "." #TestName);                      \
274     }                                                                          \
275     void Run() override;                                                       \
276     const char *getName() const override { return name; }                      \
277   };                                                                           \
278   TypeList::Tests<SuiteClass##_##TestName>::type                               \
279       SuiteClass##_##TestName##_Instance;                                      \
280   template <typename T> void SuiteClass##_##TestName<T>::Run()
281 
282 #define TEST(SuiteName, TestName)                                              \
283   static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteName),      \
284                 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
285   class SuiteName##_##TestName : public __llvm_libc::testing::Test {           \
286   public:                                                                      \
287     SuiteName##_##TestName() { addTest(this); }                                \
288     void Run() override;                                                       \
289     const char *getName() const override { return #SuiteName "." #TestName; }  \
290   };                                                                           \
291   SuiteName##_##TestName SuiteName##_##TestName##_Instance;                    \
292   void SuiteName##_##TestName::Run()
293 
294 #define TEST_F(SuiteClass, TestName)                                           \
295   static_assert(                                                               \
296       __llvm_libc::testing::internal::valid_prefix(#SuiteClass),               \
297       "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'.");   \
298   class SuiteClass##_##TestName : public SuiteClass {                          \
299   public:                                                                      \
300     SuiteClass##_##TestName() { addTest(this); }                               \
301     void Run() override;                                                       \
302     const char *getName() const override { return #SuiteClass "." #TestName; } \
303   };                                                                           \
304   SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;                  \
305   void SuiteClass##_##TestName::Run()
306 
307 #define EXPECT_EQ(LHS, RHS)                                                    \
308   this->test(__llvm_libc::testing::Cond_EQ, (LHS), (RHS), #LHS, #RHS,          \
309              __FILE__, __LINE__)
310 #define ASSERT_EQ(LHS, RHS)                                                    \
311   if (!EXPECT_EQ(LHS, RHS))                                                    \
312   return
313 
314 #define EXPECT_NE(LHS, RHS)                                                    \
315   this->test(__llvm_libc::testing::Cond_NE, (LHS), (RHS), #LHS, #RHS,          \
316              __FILE__, __LINE__)
317 #define ASSERT_NE(LHS, RHS)                                                    \
318   if (!EXPECT_NE(LHS, RHS))                                                    \
319   return
320 
321 #define EXPECT_LT(LHS, RHS)                                                    \
322   this->test(__llvm_libc::testing::Cond_LT, (LHS), (RHS), #LHS, #RHS,          \
323              __FILE__, __LINE__)
324 #define ASSERT_LT(LHS, RHS)                                                    \
325   if (!EXPECT_LT(LHS, RHS))                                                    \
326   return
327 
328 #define EXPECT_LE(LHS, RHS)                                                    \
329   this->test(__llvm_libc::testing::Cond_LE, (LHS), (RHS), #LHS, #RHS,          \
330              __FILE__, __LINE__)
331 #define ASSERT_LE(LHS, RHS)                                                    \
332   if (!EXPECT_LE(LHS, RHS))                                                    \
333   return
334 
335 #define EXPECT_GT(LHS, RHS)                                                    \
336   this->test(__llvm_libc::testing::Cond_GT, (LHS), (RHS), #LHS, #RHS,          \
337              __FILE__, __LINE__)
338 #define ASSERT_GT(LHS, RHS)                                                    \
339   if (!EXPECT_GT(LHS, RHS))                                                    \
340   return
341 
342 #define EXPECT_GE(LHS, RHS)                                                    \
343   this->test(__llvm_libc::testing::Cond_GE, (LHS), (RHS), #LHS, #RHS,          \
344              __FILE__, __LINE__)
345 #define ASSERT_GE(LHS, RHS)                                                    \
346   if (!EXPECT_GE(LHS, RHS))                                                    \
347   return
348 
349 #define EXPECT_STREQ(LHS, RHS)                                                 \
350   this->testStrEq((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
351 #define ASSERT_STREQ(LHS, RHS)                                                 \
352   if (!EXPECT_STREQ(LHS, RHS))                                                 \
353   return
354 
355 #define EXPECT_STRNE(LHS, RHS)                                                 \
356   this->testStrNe((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
357 #define ASSERT_STRNE(LHS, RHS)                                                 \
358   if (!EXPECT_STRNE(LHS, RHS))                                                 \
359   return
360 
361 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true)
362 
363 #define ASSERT_TRUE(VAL)                                                       \
364   if (!EXPECT_TRUE(VAL))                                                       \
365   return
366 
367 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false)
368 
369 #define ASSERT_FALSE(VAL)                                                      \
370   if (!EXPECT_FALSE(VAL))                                                      \
371   return
372 
373 #ifdef ENABLE_SUBPROCESS_TESTS
374 
375 #define EXPECT_EXITS(FUNC, EXIT)                                               \
376   this->testProcessExits(__llvm_libc::testing::Test::createCallable(FUNC),     \
377                          EXIT, #FUNC, #EXIT, __FILE__, __LINE__)
378 
379 #define ASSERT_EXITS(FUNC, EXIT)                                               \
380   if (!EXPECT_EXITS(FUNC, EXIT))                                               \
381   return
382 
383 #define EXPECT_DEATH(FUNC, SIG)                                                \
384   this->testProcessKilled(__llvm_libc::testing::Test::createCallable(FUNC),    \
385                           SIG, #FUNC, #SIG, __FILE__, __LINE__)
386 
387 #define ASSERT_DEATH(FUNC, EXIT)                                               \
388   if (!EXPECT_DEATH(FUNC, EXIT))                                               \
389   return
390 
391 #endif // ENABLE_SUBPROCESS_TESTS
392 
393 #define __CAT1(a, b) a##b
394 #define __CAT(a, b) __CAT1(a, b)
395 #define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__)
396 
397 #define EXPECT_THAT(MATCH, MATCHER)                                            \
398   [&]() -> bool {                                                              \
399     auto UNIQUE_VAR(__matcher) = (MATCHER);                                    \
400     return this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)),               \
401                            UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__,  \
402                            __LINE__);                                          \
403   }()
404 
405 #define ASSERT_THAT(MATCH, MATCHER)                                            \
406   do {                                                                         \
407     if (!EXPECT_THAT(MATCH, MATCHER))                                          \
408       return;                                                                  \
409   } while (0)
410 
411 #define WITH_SIGNAL(X) X
412 
413 #endif // LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
414