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_H 10 #define LLVM_LIBC_UTILS_UNITTEST_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 { ~MatcherBaseMatcherBase49 virtual ~MatcherBase() {} explainErrorMatcherBase50 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 setContext(RunContext * C)64 void setContext(RunContext *C) { Ctx = C; } 65 66 public: ~Test()67 virtual ~Test() {} SetUp()68 virtual void SetUp() {} TearDown()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> test(TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)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> test(TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)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 createCallable(Func f)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 #define TEST(SuiteName, TestName) \ 138 class SuiteName##_##TestName : public __llvm_libc::testing::Test { \ 139 public: \ 140 SuiteName##_##TestName() { addTest(this); } \ 141 void Run() override; \ 142 const char *getName() const override { return #SuiteName "." #TestName; } \ 143 }; \ 144 SuiteName##_##TestName SuiteName##_##TestName##_Instance; \ 145 void SuiteName##_##TestName::Run() 146 147 #define TEST_F(SuiteClass, TestName) \ 148 class SuiteClass##_##TestName : public SuiteClass { \ 149 public: \ 150 SuiteClass##_##TestName() { addTest(this); } \ 151 void Run() override; \ 152 const char *getName() const override { return #SuiteClass "." #TestName; } \ 153 }; \ 154 SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \ 155 void SuiteClass##_##TestName::Run() 156 157 #define EXPECT_EQ(LHS, RHS) \ 158 this->test(__llvm_libc::testing::Cond_EQ, (LHS), (RHS), #LHS, #RHS, \ 159 __FILE__, __LINE__) 160 #define ASSERT_EQ(LHS, RHS) \ 161 if (!EXPECT_EQ(LHS, RHS)) \ 162 return 163 164 #define EXPECT_NE(LHS, RHS) \ 165 this->test(__llvm_libc::testing::Cond_NE, (LHS), (RHS), #LHS, #RHS, \ 166 __FILE__, __LINE__) 167 #define ASSERT_NE(LHS, RHS) \ 168 if (!EXPECT_NE(LHS, RHS)) \ 169 return 170 171 #define EXPECT_LT(LHS, RHS) \ 172 this->test(__llvm_libc::testing::Cond_LT, (LHS), (RHS), #LHS, #RHS, \ 173 __FILE__, __LINE__) 174 #define ASSERT_LT(LHS, RHS) \ 175 if (!EXPECT_LT(LHS, RHS)) \ 176 return 177 178 #define EXPECT_LE(LHS, RHS) \ 179 this->test(__llvm_libc::testing::Cond_LE, (LHS), (RHS), #LHS, #RHS, \ 180 __FILE__, __LINE__) 181 #define ASSERT_LE(LHS, RHS) \ 182 if (!EXPECT_LE(LHS, RHS)) \ 183 return 184 185 #define EXPECT_GT(LHS, RHS) \ 186 this->test(__llvm_libc::testing::Cond_GT, (LHS), (RHS), #LHS, #RHS, \ 187 __FILE__, __LINE__) 188 #define ASSERT_GT(LHS, RHS) \ 189 if (!EXPECT_GT(LHS, RHS)) \ 190 return 191 192 #define EXPECT_GE(LHS, RHS) \ 193 this->test(__llvm_libc::testing::Cond_GE, (LHS), (RHS), #LHS, #RHS, \ 194 __FILE__, __LINE__) 195 #define ASSERT_GE(LHS, RHS) \ 196 if (!EXPECT_GE(LHS, RHS)) \ 197 return 198 199 #define EXPECT_STREQ(LHS, RHS) \ 200 this->testStrEq((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__) 201 #define ASSERT_STREQ(LHS, RHS) \ 202 if (!EXPECT_STREQ(LHS, RHS)) \ 203 return 204 205 #define EXPECT_STRNE(LHS, RHS) \ 206 this->testStrNe((LHS), (RHS), #LHS, #RHS, __FILE__, __LINE__) 207 #define ASSERT_STRNE(LHS, RHS) \ 208 if (!EXPECT_STRNE(LHS, RHS)) \ 209 return 210 211 #define EXPECT_TRUE(VAL) EXPECT_EQ((VAL), true) 212 213 #define ASSERT_TRUE(VAL) \ 214 if (!EXPECT_TRUE(VAL)) \ 215 return 216 217 #define EXPECT_FALSE(VAL) EXPECT_EQ((VAL), false) 218 219 #define ASSERT_FALSE(VAL) \ 220 if (!EXPECT_FALSE(VAL)) \ 221 return 222 223 #define EXPECT_EXITS(FUNC, EXIT) \ 224 this->testProcessExits(__llvm_libc::testing::Test::createCallable(FUNC), \ 225 EXIT, #FUNC, #EXIT, __FILE__, __LINE__) 226 227 #define ASSERT_EXITS(FUNC, EXIT) \ 228 if (!EXPECT_EXITS(FUNC, EXIT)) \ 229 return 230 231 #define EXPECT_DEATH(FUNC, SIG) \ 232 this->testProcessKilled(__llvm_libc::testing::Test::createCallable(FUNC), \ 233 SIG, #FUNC, #SIG, __FILE__, __LINE__) 234 235 #define ASSERT_DEATH(FUNC, EXIT) \ 236 if (!EXPECT_DEATH(FUNC, EXIT)) \ 237 return 238 239 #define __CAT1(a, b) a##b 240 #define __CAT(a, b) __CAT1(a, b) 241 #define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__) 242 243 #define EXPECT_THAT(MATCH, MATCHER) \ 244 do { \ 245 auto UNIQUE_VAR(__matcher) = (MATCHER); \ 246 this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \ 247 UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__, \ 248 __LINE__); \ 249 } while (0) 250 251 #define ASSERT_THAT(MATCH, MATCHER) \ 252 do { \ 253 auto UNIQUE_VAR(__matcher) = (MATCHER); \ 254 if (!this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \ 255 UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__, \ 256 __LINE__)) \ 257 return; \ 258 } while (0) 259 260 #endif // LLVM_LIBC_UTILS_UNITTEST_H 261