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 "utils/CPP/TypeTraits.h"
16 #include "utils/testutils/ExecuteFunction.h"
17 #include "utils/testutils/StreamWrapper.h"
18 
19 namespace __llvm_libc {
20 namespace testing {
21 
22 class RunContext;
23 
24 // Only the following conditions are supported. Notice that we do not have
25 // a TRUE or FALSE condition. That is because, C library funtions do not
26 // return boolean values, but use integral return values to indicate true or
27 // false conditions. Hence, it is more appropriate to use the other comparison
28 // conditions for such cases.
29 enum TestCondition {
30   Cond_None,
31   Cond_EQ,
32   Cond_NE,
33   Cond_LT,
34   Cond_LE,
35   Cond_GT,
36   Cond_GE,
37 };
38 
39 namespace internal {
40 
41 template <typename ValType>
42 bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
43           const char *LHSStr, const char *RHSStr, const char *File,
44           unsigned long Line);
45 
46 } // namespace internal
47 
48 struct MatcherBase {
49   virtual ~MatcherBase() {}
50   virtual void explainError(testutils::StreamWrapper &OS) {
51     OS << "unknown error\n";
52   }
53 };
54 
55 template <typename T> struct Matcher : public MatcherBase { bool match(T &t); };
56 
57 // NOTE: One should not create instances and call methods on them directly. One
58 // should use the macros TEST or TEST_F to write test cases.
59 class Test {
60 private:
61   Test *Next = nullptr;
62   RunContext *Ctx = nullptr;
63 
64   void setContext(RunContext *C) { Ctx = C; }
65 
66 public:
67   virtual ~Test() {}
68   virtual void SetUp() {}
69   virtual void TearDown() {}
70 
71   static int runTests();
72 
73 protected:
74   static void addTest(Test *T);
75 
76   // We make use of a template function, with |LHS| and |RHS| as explicit
77   // parameters, for enhanced type checking. Other gtest like unittest
78   // frameworks have a similar function which takes a boolean argument
79   // instead of the explicit |LHS| and |RHS| arguments. This boolean argument
80   // is the result of the |Cond| operation on |LHS| and |RHS|. Though not bad,
81   // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because
82   // of type promotion.
83   template <typename ValType,
84             cpp::EnableIfType<cpp::IsIntegral<ValType>::Value, int> = 0>
85   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
86             const char *RHSStr, const char *File, unsigned long Line) {
87     return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, File, Line);
88   }
89 
90   template <
91       typename ValType,
92       cpp::EnableIfType<cpp::IsPointerType<ValType>::Value, ValType> = nullptr>
93   bool test(TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr,
94             const char *RHSStr, const char *File, unsigned long Line) {
95     return internal::test(Ctx, Cond, (unsigned long long)LHS,
96                           (unsigned long long)RHS, LHSStr, RHSStr, File, Line);
97   }
98 
99   bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
100                  const char *RHSStr, const char *File, unsigned long Line);
101 
102   bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
103                  const char *RHSStr, const char *File, unsigned long Line);
104 
105   bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
106                  const char *RHSStr, const char *File, unsigned long Line);
107 
108   bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
109                         const char *LHSStr, const char *RHSStr,
110                         const char *File, unsigned long Line);
111 
112   bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,
113                          const char *LHSStr, const char *RHSStr,
114                          const char *File, unsigned long Line);
115 
116   template <typename Func> testutils::FunctionCaller *createCallable(Func f) {
117     struct Callable : public testutils::FunctionCaller {
118       Func f;
119       Callable(Func f) : f(f) {}
120       void operator()() override { f(); }
121     };
122 
123     return new Callable(f);
124   }
125 
126 private:
127   virtual void Run() = 0;
128   virtual const char *getName() const = 0;
129 
130   static Test *Start;
131   static Test *End;
132 };
133 
134 } // namespace testing
135 } // namespace __llvm_libc
136 
137 namespace __llvm_libc {
138 namespace internal {
139 constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {
140   for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)
141     if (*lhs != *rhs)
142       return false;
143   return true;
144 }
145 } // namespace internal
146 } // namespace __llvm_libc
147 
148 #define TEST(SuiteName, TestName)                                              \
149   static_assert(__llvm_libc::internal::same_prefix(#SuiteName, "LlvmLibc", 8), \
150                 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
151   class SuiteName##_##TestName : public __llvm_libc::testing::Test {           \
152   public:                                                                      \
153     SuiteName##_##TestName() { addTest(this); }                                \
154     void Run() override;                                                       \
155     const char *getName() const override { return #SuiteName "." #TestName; }  \
156   };                                                                           \
157   SuiteName##_##TestName SuiteName##_##TestName##_Instance;                    \
158   void SuiteName##_##TestName::Run()
159 
160 #define TEST_F(SuiteClass, TestName)                                           \
161   static_assert(                                                               \
162       __llvm_libc::internal::same_prefix(#SuiteClass, "LlvmLibc", 8),          \
163       "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'.");   \
164   class SuiteClass##_##TestName : public SuiteClass {                          \
165   public:                                                                      \
166     SuiteClass##_##TestName() { addTest(this); }                               \
167     void Run() override;                                                       \
168     const char *getName() const override { return #SuiteClass "." #TestName; } \
169   };                                                                           \
170   SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;                  \
171   void SuiteClass##_##TestName::Run()
172 
173 #define EXPECT_EQ(LHS, RHS)                                                    \
174   this->test(__llvm_libc::testing::Cond_EQ, (LHS), (RHS), #LHS, #RHS,          \
175              __FILE__, __LINE__)
176 #define ASSERT_EQ(LHS, RHS)                                                    \
177   if (!EXPECT_EQ(LHS, RHS))                                                    \
178   return
179 
180 #define EXPECT_NE(LHS, RHS)                                                    \
181   this->test(__llvm_libc::testing::Cond_NE, (LHS), (RHS), #LHS, #RHS,          \
182              __FILE__, __LINE__)
183 #define ASSERT_NE(LHS, RHS)                                                    \
184   if (!EXPECT_NE(LHS, RHS))                                                    \
185   return
186 
187 #define EXPECT_LT(LHS, RHS)                                                    \
188   this->test(__llvm_libc::testing::Cond_LT, (LHS), (RHS), #LHS, #RHS,          \
189              __FILE__, __LINE__)
190 #define ASSERT_LT(LHS, RHS)                                                    \
191   if (!EXPECT_LT(LHS, RHS))                                                    \
192   return
193 
194 #define EXPECT_LE(LHS, RHS)                                                    \
195   this->test(__llvm_libc::testing::Cond_LE, (LHS), (RHS), #LHS, #RHS,          \
196              __FILE__, __LINE__)
197 #define ASSERT_LE(LHS, RHS)                                                    \
198   if (!EXPECT_LE(LHS, RHS))                                                    \
199   return
200 
201 #define EXPECT_GT(LHS, RHS)                                                    \
202   this->test(__llvm_libc::testing::Cond_GT, (LHS), (RHS), #LHS, #RHS,          \
203              __FILE__, __LINE__)
204 #define ASSERT_GT(LHS, RHS)                                                    \
205   if (!EXPECT_GT(LHS, RHS))                                                    \
206   return
207 
208 #define EXPECT_GE(LHS, RHS)                                                    \
209   this->test(__llvm_libc::testing::Cond_GE, (LHS), (RHS), #LHS, #RHS,          \
210              __FILE__, __LINE__)
211 #define ASSERT_GE(LHS, RHS)                                                    \
212   if (!EXPECT_GE(LHS, RHS))                                                    \
213   return
214 
215 #define EXPECT_STREQ(LHS, RHS)                                                 \
216   this->testStrEq((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
217 #define ASSERT_STREQ(LHS, RHS)                                                 \
218   if (!EXPECT_STREQ(LHS, RHS))                                                 \
219   return
220 
221 #define EXPECT_STRNE(LHS, RHS)                                                 \
222   this->testStrNe((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__)
223 #define ASSERT_STRNE(LHS, RHS)                                                 \
224   if (!EXPECT_STRNE(LHS, RHS))                                                 \
225   return
226 
227 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true)
228 
229 #define ASSERT_TRUE(VAL)                                                       \
230   if (!EXPECT_TRUE(VAL))                                                       \
231   return
232 
233 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false)
234 
235 #define ASSERT_FALSE(VAL)                                                      \
236   if (!EXPECT_FALSE(VAL))                                                      \
237   return
238 
239 #define EXPECT_EXITS(FUNC, EXIT)                                               \
240   this->testProcessExits(__llvm_libc::testing::Test::createCallable(FUNC),     \
241                          EXIT, #FUNC, #EXIT, __FILE__, __LINE__)
242 
243 #define ASSERT_EXITS(FUNC, EXIT)                                               \
244   if (!EXPECT_EXITS(FUNC, EXIT))                                               \
245   return
246 
247 #define EXPECT_DEATH(FUNC, SIG)                                                \
248   this->testProcessKilled(__llvm_libc::testing::Test::createCallable(FUNC),    \
249                           SIG, #FUNC, #SIG, __FILE__, __LINE__)
250 
251 #define ASSERT_DEATH(FUNC, EXIT)                                               \
252   if (!EXPECT_DEATH(FUNC, EXIT))                                               \
253   return
254 
255 #define __CAT1(a, b) a##b
256 #define __CAT(a, b) __CAT1(a, b)
257 #define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__)
258 
259 #define EXPECT_THAT(MATCH, MATCHER)                                            \
260   do {                                                                         \
261     auto UNIQUE_VAR(__matcher) = (MATCHER);                                    \
262     this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)),                      \
263                     UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__,         \
264                     __LINE__);                                                 \
265   } while (0)
266 
267 #define ASSERT_THAT(MATCH, MATCHER)                                            \
268   do {                                                                         \
269     auto UNIQUE_VAR(__matcher) = (MATCHER);                                    \
270     if (!this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)),                 \
271                          UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__,    \
272                          __LINE__))                                            \
273       return;                                                                  \
274   } while (0)
275 
276 #define WITH_SIGNAL(X) X
277 
278 #endif // LLVM_LIBC_UTILS_UNITTEST_LIBCTEST_H
279