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 utils/CPP/ or utils/testutils. No
13 // other headers should be included.
14 
15 #include "PlatformDefs.h"
16 
17 #include "utils/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 {
51   virtual ~MatcherBase() {}
52   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 
66   void setContext(RunContext *C) { Ctx = C; }
67 
68 public:
69   virtual ~Test() {}
70   virtual void SetUp() {}
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>
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 <
93       typename ValType,
94       cpp::EnableIfType<cpp::IsPointerType<ValType>::Value, ValType> = nullptr>
95   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
96             const char *RHSStr, const char *File, unsigned long Line) {
97     return internal::test(Ctx, Cond, (unsigned long long)LHS,
98                           (unsigned long long)RHS, LHSStr, RHSStr, File, Line);
99   }
100 
101   bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
102                  const char *RHSStr, const char *File, unsigned long Line);
103 
104   bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
105                  const char *RHSStr, const char *File, unsigned long Line);
106 
107   bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
108                  const char *RHSStr, const char *File, unsigned long Line);
109 
110   bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
111                         const char *LHSStr, const char *RHSStr,
112                         const char *File, unsigned long Line);
113 
114   bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,
115                          const char *LHSStr, const char *RHSStr,
116                          const char *File, unsigned long Line);
117 
118   template <typename Func> testutils::FunctionCaller *createCallable(Func f) {
119     struct Callable : public testutils::FunctionCaller {
120       Func f;
121       Callable(Func f) : f(f) {}
122       void operator()() override { f(); }
123     };
124 
125     return new Callable(f);
126   }
127 
128 private:
129   virtual void Run() = 0;
130   virtual const char *getName() const = 0;
131 
132   static Test *Start;
133   static Test *End;
134 };
135 
136 namespace internal {
137 
138 constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {
139   for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)
140     if (*lhs != *rhs)
141       return false;
142   return true;
143 }
144 
145 constexpr bool valid_prefix(char const *lhs) {
146   return same_prefix(lhs, "LlvmLibc", 8);
147 }
148 
149 // 'str' is a null terminated string of the form
150 // "const char *__llvm_libc::testing::internal::GetTypeName() [ParamType = XXX]"
151 // We return the substring that start at character '[' or a default message.
152 constexpr char const *GetPrettyFunctionParamType(char const *str) {
153   for (const char *ptr = str; *ptr != '\0'; ++ptr)
154     if (*ptr == '[')
155       return ptr;
156   return "UNSET : declare with REGISTER_TYPE_NAME";
157 }
158 
159 // This function recovers ParamType at compile time by using __PRETTY_FUNCTION__
160 // It can be customized by using the REGISTER_TYPE_NAME macro below.
161 template <typename ParamType> static constexpr const char *GetTypeName() {
162   return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
163 }
164 
165 template <typename T>
166 static inline void GenerateName(char *buffer, int buffer_size,
167                                 const char *prefix) {
168   if (buffer_size == 0)
169     return;
170 
171   // Make sure string is null terminated.
172   --buffer_size;
173   buffer[buffer_size] = '\0';
174 
175   const auto AppendChar = [&](char c) {
176     if (buffer_size > 0) {
177       *buffer = c;
178       ++buffer;
179       --buffer_size;
180     }
181   };
182   const auto AppendStr = [&](const char *str) {
183     for (; str && *str != '\0'; ++str)
184       AppendChar(*str);
185   };
186 
187   AppendStr(prefix);
188   AppendChar(' ');
189   AppendStr(GetTypeName<T>());
190   AppendChar('\0');
191 }
192 
193 // TestCreator implements a linear hierarchy of test instances, effectively
194 // instanciating all tests with Types in a single object.
195 template <template <typename> class TemplatedTestClass, typename... Types>
196 struct TestCreator;
197 
198 template <template <typename> class TemplatedTestClass, typename Head,
199           typename... Tail>
200 struct TestCreator<TemplatedTestClass, Head, Tail...>
201     : private TestCreator<TemplatedTestClass, Tail...> {
202   TemplatedTestClass<Head> instance;
203 };
204 
205 template <template <typename> class TemplatedTestClass>
206 struct TestCreator<TemplatedTestClass> {};
207 
208 // A type list to declare the set of types to instantiate the tests with.
209 template <typename... Types> struct TypeList {
210   template <template <typename> class TemplatedTestClass> struct Tests {
211     using type = TestCreator<TemplatedTestClass, Types...>;
212   };
213 };
214 
215 } // namespace internal
216 
217 // Make TypeList visible in __llvm_libc::testing.
218 template <typename... Types> using TypeList = internal::TypeList<Types...>;
219 
220 } // namespace testing
221 } // namespace __llvm_libc
222 
223 // For TYPED_TEST and TYPED_TEST_F below we need to display which type was used
224 // to run the test. The default will return the fully qualified canonical type
225 // but it can be difficult to read. We provide the following macro to allow the
226 // client to register the type name as they see it in the code.
227 #define REGISTER_TYPE_NAME(TYPE)                                               \
228   template <>                                                                  \
229   constexpr const char *__llvm_libc::testing::internal::GetTypeName<TYPE>() {  \
230     return "[ParamType = " #TYPE "]";                                          \
231   }
232 
233 #define TYPED_TEST(SuiteName, TestName, TypeList)                              \
234   static_assert(                                                               \
235       __llvm_libc::testing::internal::valid_prefix(#SuiteName),                \
236       "All LLVM-libc TYPED_TEST suite names must start with 'LlvmLibc'.");     \
237   template <typename T>                                                        \
238   class SuiteName##_##TestName : public __llvm_libc::testing::Test {           \
239   public:                                                                      \
240     using ParamType = T;                                                       \
241     char name[256];                                                            \
242     SuiteName##_##TestName() {                                                 \
243       addTest(this);                                                           \
244       __llvm_libc::testing::internal::GenerateName<T>(                         \
245           name, sizeof(name), #SuiteName "." #TestName);                       \
246     }                                                                          \
247     void Run() override;                                                       \
248     const char *getName() const override { return name; }                      \
249   };                                                                           \
250   TypeList::Tests<SuiteName##_##TestName>::type                                \
251       SuiteName##_##TestName##_Instance;                                       \
252   template <typename T> void SuiteName##_##TestName<T>::Run()
253 
254 #define TYPED_TEST_F(SuiteClass, TestName, TypeList)                           \
255   static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteClass),     \
256                 "All LLVM-libc TYPED_TEST_F suite class names must start "     \
257                 "with 'LlvmLibc'.");                                           \
258   template <typename T> class SuiteClass##_##TestName : public SuiteClass<T> { \
259   public:                                                                      \
260     using ParamType = T;                                                       \
261     char name[256];                                                            \
262     SuiteClass##_##TestName() {                                                \
263       SuiteClass<T>::addTest(this);                                            \
264       __llvm_libc::testing::internal::GenerateName<T>(                         \
265           name, sizeof(name), #SuiteClass "." #TestName);                      \
266     }                                                                          \
267     void Run() override;                                                       \
268     const char *getName() const override { return name; }                      \
269   };                                                                           \
270   TypeList::Tests<SuiteClass##_##TestName>::type                               \
271       SuiteClass##_##TestName##_Instance;                                      \
272   template <typename T> void SuiteClass##_##TestName<T>::Run()
273 
274 #define TEST(SuiteName, TestName)                                              \
275   static_assert(__llvm_libc::testing::internal::valid_prefix(#SuiteName),      \
276                 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
277   class SuiteName##_##TestName : public __llvm_libc::testing::Test {           \
278   public:                                                                      \
279     SuiteName##_##TestName() { addTest(this); }                                \
280     void Run() override;                                                       \
281     const char *getName() const override { return #SuiteName "." #TestName; }  \
282   };                                                                           \
283   SuiteName##_##TestName SuiteName##_##TestName##_Instance;                    \
284   void SuiteName##_##TestName::Run()
285 
286 #define TEST_F(SuiteClass, TestName)                                           \
287   static_assert(                                                               \
288       __llvm_libc::testing::internal::valid_prefix(#SuiteClass),               \
289       "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'.");   \
290   class SuiteClass##_##TestName : public SuiteClass {                          \
291   public:                                                                      \
292     SuiteClass##_##TestName() { addTest(this); }                               \
293     void Run() override;                                                       \
294     const char *getName() const override { return #SuiteClass "." #TestName; } \
295   };                                                                           \
296   SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;                  \
297   void SuiteClass##_##TestName::Run()
298 
299 #define EXPECT_EQ(LHS, RHS)                                                    \
300   this->test(__llvm_libc::testing::Cond_EQ, (LHS), (RHS), #LHS, #RHS,          \
301              __FILE__, __LINE__)
302 #define ASSERT_EQ(LHS, RHS)                                                    \
303   if (!EXPECT_EQ(LHS, RHS))                                                    \
304   return
305 
306 #define EXPECT_NE(LHS, RHS)                                                    \
307   this->test(__llvm_libc::testing::Cond_NE, (LHS), (RHS), #LHS, #RHS,          \
308              __FILE__, __LINE__)
309 #define ASSERT_NE(LHS, RHS)                                                    \
310   if (!EXPECT_NE(LHS, RHS))                                                    \
311   return
312 
313 #define EXPECT_LT(LHS, RHS)                                                    \
314   this->test(__llvm_libc::testing::Cond_LT, (LHS), (RHS), #LHS, #RHS,          \
315              __FILE__, __LINE__)
316 #define ASSERT_LT(LHS, RHS)                                                    \
317   if (!EXPECT_LT(LHS, RHS))                                                    \
318   return
319 
320 #define EXPECT_LE(LHS, RHS)                                                    \
321   this->test(__llvm_libc::testing::Cond_LE, (LHS), (RHS), #LHS, #RHS,          \
322              __FILE__, __LINE__)
323 #define ASSERT_LE(LHS, RHS)                                                    \
324   if (!EXPECT_LE(LHS, RHS))                                                    \
325   return
326 
327 #define EXPECT_GT(LHS, RHS)                                                    \
328   this->test(__llvm_libc::testing::Cond_GT, (LHS), (RHS), #LHS, #RHS,          \
329              __FILE__, __LINE__)
330 #define ASSERT_GT(LHS, RHS)                                                    \
331   if (!EXPECT_GT(LHS, RHS))                                                    \
332   return
333 
334 #define EXPECT_GE(LHS, RHS)                                                    \
335   this->test(__llvm_libc::testing::Cond_GE, (LHS), (RHS), #LHS, #RHS,          \
336              __FILE__, __LINE__)
337 #define ASSERT_GE(LHS, RHS)                                                    \
338   if (!EXPECT_GE(LHS, RHS))                                                    \
339   return
340 
341 #define EXPECT_STREQ(LHS, RHS)                                                 \
342   this->testStrEq((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
343 #define ASSERT_STREQ(LHS, RHS)                                                 \
344   if (!EXPECT_STREQ(LHS, RHS))                                                 \
345   return
346 
347 #define EXPECT_STRNE(LHS, RHS)                                                 \
348   this->testStrNe((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
349 #define ASSERT_STRNE(LHS, RHS)                                                 \
350   if (!EXPECT_STRNE(LHS, RHS))                                                 \
351   return
352 
353 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true)
354 
355 #define ASSERT_TRUE(VAL)                                                       \
356   if (!EXPECT_TRUE(VAL))                                                       \
357   return
358 
359 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false)
360 
361 #define ASSERT_FALSE(VAL)                                                      \
362   if (!EXPECT_FALSE(VAL))                                                      \
363   return
364 
365 #ifdef ENABLE_SUBPROCESS_TESTS
366 
367 #define EXPECT_EXITS(FUNC, EXIT)                                               \
368   this->testProcessExits(__llvm_libc::testing::Test::createCallable(FUNC),     \
369                          EXIT, #FUNC, #EXIT, __FILE__, __LINE__)
370 
371 #define ASSERT_EXITS(FUNC, EXIT)                                               \
372   if (!EXPECT_EXITS(FUNC, EXIT))                                               \
373   return
374 
375 #define EXPECT_DEATH(FUNC, SIG)                                                \
376   this->testProcessKilled(__llvm_libc::testing::Test::createCallable(FUNC),    \
377                           SIG, #FUNC, #SIG, __FILE__, __LINE__)
378 
379 #define ASSERT_DEATH(FUNC, EXIT)                                               \
380   if (!EXPECT_DEATH(FUNC, EXIT))                                               \
381   return
382 
383 #endif // ENABLE_SUBPROCESS_TESTS
384 
385 #define __CAT1(a, b) a##b
386 #define __CAT(a, b) __CAT1(a, b)
387 #define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__)
388 
389 #define EXPECT_THAT(MATCH, MATCHER)                                            \
390   do {                                                                         \
391     auto UNIQUE_VAR(__matcher) = (MATCHER);                                    \
392     this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)),                      \
393                     UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__,         \
394                     __LINE__);                                                 \
395   } while (0)
396 
397 #define ASSERT_THAT(MATCH, MATCHER)                                            \
398   do {                                                                         \
399     auto UNIQUE_VAR(__matcher) = (MATCHER);                                    \
400     if (!this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)),                 \
401                          UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__,    \
402                          __LINE__))                                            \
403       return;                                                                  \
404   } while (0)
405 
406 #define WITH_SIGNAL(X) X
407 
408 #endif // LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
409