1 //===-- Implementation of the base class for libc unittests ---------------===// 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 #include "LibcTest.h" 10 11 #include "utils/testutils/ExecuteFunction.h" 12 #include <cassert> 13 #include <iostream> 14 #include <string> 15 16 namespace __llvm_libc { 17 namespace testing { 18 19 // This need not be a class as all it has is a single read-write state variable. 20 // But, we make it class as then its implementation can be hidden from the 21 // header file. 22 class RunContext { 23 public: 24 enum RunResult { Result_Pass = 1, Result_Fail = 2 }; 25 26 RunResult status() const { return Status; } 27 28 void markFail() { Status = Result_Fail; } 29 30 private: 31 RunResult Status = Result_Pass; 32 }; 33 34 namespace internal { 35 36 // When the value is of integral type, just display it as normal. 37 template <typename ValType> 38 cpp::EnableIfType<cpp::IsIntegral<ValType>::Value, std::string> 39 describeValue(ValType Value) { 40 return std::to_string(Value); 41 } 42 43 std::string describeValue(std::string Value) { return std::string(Value); } 44 45 // When the value is __uint128_t, also show its hexadecimal digits. 46 // Using template to force exact match, prevent ambiguous promotion. 47 template <> std::string describeValue<__uint128_t>(__uint128_t Value) { 48 std::string S(sizeof(__uint128_t) * 2, '0'); 49 50 for (auto I = S.rbegin(), End = S.rend(); I != End; ++I, Value >>= 4) { 51 unsigned char Mod = static_cast<unsigned char>(Value) & 15; 52 *I = Mod < 10 ? '0' + Mod : 'a' + Mod - 10; 53 } 54 55 return "0x" + S; 56 } 57 58 template <typename ValType> 59 void explainDifference(ValType LHS, ValType RHS, const char *LHSStr, 60 const char *RHSStr, const char *File, unsigned long Line, 61 std::string OpString) { 62 size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; 63 std::string Offset(OffsetLength, ' '); 64 65 std::cout << File << ":" << Line << ": FAILURE\n" 66 << Offset << "Expected: " << LHSStr << '\n' 67 << Offset << "Which is: " << describeValue(LHS) << '\n' 68 << "To be " << OpString << ": " << RHSStr << '\n' 69 << Offset << "Which is: " << describeValue(RHS) << '\n'; 70 } 71 72 template <typename ValType> 73 bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS, 74 const char *LHSStr, const char *RHSStr, const char *File, 75 unsigned long Line) { 76 auto ExplainDifference = [=](std::string OpString) { 77 explainDifference(LHS, RHS, LHSStr, RHSStr, File, Line, OpString); 78 }; 79 80 switch (Cond) { 81 case Cond_EQ: 82 if (LHS == RHS) 83 return true; 84 85 Ctx->markFail(); 86 ExplainDifference("equal to"); 87 return false; 88 case Cond_NE: 89 if (LHS != RHS) 90 return true; 91 92 Ctx->markFail(); 93 ExplainDifference("not equal to"); 94 return false; 95 case Cond_LT: 96 if (LHS < RHS) 97 return true; 98 99 Ctx->markFail(); 100 ExplainDifference("less than"); 101 return false; 102 case Cond_LE: 103 if (LHS <= RHS) 104 return true; 105 106 Ctx->markFail(); 107 ExplainDifference("less than or equal to"); 108 return false; 109 case Cond_GT: 110 if (LHS > RHS) 111 return true; 112 113 Ctx->markFail(); 114 ExplainDifference("greater than"); 115 return false; 116 case Cond_GE: 117 if (LHS >= RHS) 118 return true; 119 120 Ctx->markFail(); 121 ExplainDifference("greater than or equal to"); 122 return false; 123 default: 124 Ctx->markFail(); 125 std::cout << "Unexpected test condition.\n"; 126 return false; 127 } 128 } 129 130 } // namespace internal 131 132 Test *Test::Start = nullptr; 133 Test *Test::End = nullptr; 134 135 void Test::addTest(Test *T) { 136 if (End == nullptr) { 137 Start = T; 138 End = T; 139 return; 140 } 141 142 End->Next = T; 143 End = T; 144 } 145 146 int Test::runTests() { 147 int TestCount = 0; 148 int FailCount = 0; 149 for (Test *T = Start; T != nullptr; T = T->Next, ++TestCount) { 150 const char *TestName = T->getName(); 151 constexpr auto GREEN = "\033[32m"; 152 constexpr auto RED = "\033[31m"; 153 constexpr auto RESET = "\033[0m"; 154 std::cout << GREEN << "[ RUN ] " << RESET << TestName << '\n'; 155 RunContext Ctx; 156 T->SetUp(); 157 T->setContext(&Ctx); 158 T->Run(); 159 T->TearDown(); 160 auto Result = Ctx.status(); 161 switch (Result) { 162 case RunContext::Result_Fail: 163 std::cout << RED << "[ FAILED ] " << RESET << TestName << '\n'; 164 ++FailCount; 165 break; 166 case RunContext::Result_Pass: 167 std::cout << GREEN << "[ OK ] " << RESET << TestName << '\n'; 168 break; 169 } 170 } 171 172 std::cout << "Ran " << TestCount << " tests. " 173 << " PASS: " << TestCount - FailCount << ' ' 174 << " FAIL: " << FailCount << '\n'; 175 176 return FailCount > 0 ? 1 : 0; 177 } 178 179 template bool Test::test<char, 0>(TestCondition Cond, char LHS, char RHS, 180 const char *LHSStr, const char *RHSStr, 181 const char *File, unsigned long Line); 182 183 template bool Test::test<short, 0>(TestCondition Cond, short LHS, short RHS, 184 const char *LHSStr, const char *RHSStr, 185 const char *File, unsigned long Line); 186 187 template bool Test::test<int, 0>(TestCondition Cond, int LHS, int RHS, 188 const char *LHSStr, const char *RHSStr, 189 const char *File, unsigned long Line); 190 191 template bool Test::test<long, 0>(TestCondition Cond, long LHS, long RHS, 192 const char *LHSStr, const char *RHSStr, 193 const char *File, unsigned long Line); 194 195 template bool Test::test<long long, 0>(TestCondition Cond, long long LHS, 196 long long RHS, const char *LHSStr, 197 const char *RHSStr, const char *File, 198 unsigned long Line); 199 200 template bool Test::test<unsigned char, 0>(TestCondition Cond, 201 unsigned char LHS, unsigned char RHS, 202 const char *LHSStr, 203 const char *RHSStr, const char *File, 204 unsigned long Line); 205 206 template bool 207 Test::test<unsigned short, 0>(TestCondition Cond, unsigned short LHS, 208 unsigned short RHS, const char *LHSStr, 209 const char *RHSStr, const char *File, 210 unsigned long Line); 211 212 template bool Test::test<unsigned int, 0>(TestCondition Cond, unsigned int LHS, 213 unsigned int RHS, const char *LHSStr, 214 const char *RHSStr, const char *File, 215 unsigned long Line); 216 217 template bool Test::test<unsigned long, 0>(TestCondition Cond, 218 unsigned long LHS, unsigned long RHS, 219 const char *LHSStr, 220 const char *RHSStr, const char *File, 221 unsigned long Line); 222 223 template bool Test::test<bool, 0>(TestCondition Cond, bool LHS, bool RHS, 224 const char *LHSStr, const char *RHSStr, 225 const char *File, unsigned long Line); 226 227 template bool 228 Test::test<unsigned long long, 0>(TestCondition Cond, unsigned long long LHS, 229 unsigned long long RHS, const char *LHSStr, 230 const char *RHSStr, const char *File, 231 unsigned long Line); 232 233 template bool Test::test<__uint128_t, 0>(TestCondition Cond, __uint128_t LHS, 234 __uint128_t RHS, const char *LHSStr, 235 const char *RHSStr, const char *File, 236 unsigned long Line); 237 238 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr, 239 const char *RHSStr, const char *File, unsigned long Line) { 240 return internal::test(Ctx, Cond_EQ, LHS ? std::string(LHS) : std::string(), 241 RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr, 242 File, Line); 243 } 244 245 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr, 246 const char *RHSStr, const char *File, unsigned long Line) { 247 return internal::test(Ctx, Cond_NE, LHS ? std::string(LHS) : std::string(), 248 RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr, 249 File, Line); 250 } 251 252 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr, 253 const char *RHSStr, const char *File, unsigned long Line) { 254 if (MatchResult) 255 return true; 256 257 Ctx->markFail(); 258 std::cout << File << ":" << Line << ": FAILURE\n" 259 << "Failed to match " << LHSStr << " against " << RHSStr << ".\n"; 260 testutils::StreamWrapper OutsWrapper = testutils::outs(); 261 Matcher.explainError(OutsWrapper); 262 return false; 263 } 264 265 #ifdef ENABLE_SUBPROCESS_TESTS 266 267 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal, 268 const char *LHSStr, const char *RHSStr, 269 const char *File, unsigned long Line) { 270 testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500); 271 272 if (const char *error = Result.getError()) { 273 Ctx->markFail(); 274 std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n'; 275 return false; 276 } 277 278 if (Result.timedOut()) { 279 Ctx->markFail(); 280 std::cout << File << ":" << Line << ": FAILURE\n" 281 << "Process timed out after " << 500 << " milliseconds.\n"; 282 return false; 283 } 284 285 if (Result.exitedNormally()) { 286 Ctx->markFail(); 287 std::cout << File << ":" << Line << ": FAILURE\n" 288 << "Expected " << LHSStr 289 << " to be killed by a signal\nBut it exited normally!\n"; 290 return false; 291 } 292 293 int KilledBy = Result.getFatalSignal(); 294 assert(KilledBy != 0 && "Not killed by any signal"); 295 if (Signal == -1 || KilledBy == Signal) 296 return true; 297 298 using testutils::signalAsString; 299 Ctx->markFail(); 300 std::cout << File << ":" << Line << ": FAILURE\n" 301 << " Expected: " << LHSStr << '\n' 302 << "To be killed by signal: " << Signal << '\n' 303 << " Which is: " << signalAsString(Signal) << '\n' 304 << " But it was killed by: " << KilledBy << '\n' 305 << " Which is: " << signalAsString(KilledBy) << '\n'; 306 return false; 307 } 308 309 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode, 310 const char *LHSStr, const char *RHSStr, 311 const char *File, unsigned long Line) { 312 testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500); 313 314 if (const char *error = Result.getError()) { 315 Ctx->markFail(); 316 std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n'; 317 return false; 318 } 319 320 if (Result.timedOut()) { 321 Ctx->markFail(); 322 std::cout << File << ":" << Line << ": FAILURE\n" 323 << "Process timed out after " << 500 << " milliseconds.\n"; 324 return false; 325 } 326 327 if (!Result.exitedNormally()) { 328 Ctx->markFail(); 329 std::cout << File << ":" << Line << ": FAILURE\n" 330 << "Expected " << LHSStr << '\n' 331 << "to exit with exit code " << ExitCode << '\n' 332 << "But it exited abnormally!\n"; 333 return false; 334 } 335 336 int ActualExit = Result.getExitCode(); 337 if (ActualExit == ExitCode) 338 return true; 339 340 Ctx->markFail(); 341 std::cout << File << ":" << Line << ": FAILURE\n" 342 << "Expected exit code of: " << LHSStr << '\n' 343 << " Which is: " << ActualExit << '\n' 344 << " To be equal to: " << RHSStr << '\n' 345 << " Which is: " << ExitCode << '\n'; 346 return false; 347 } 348 349 #endif // ENABLE_SUBPROCESS_TESTS 350 } // namespace testing 351 } // namespace __llvm_libc 352 353 int main() { return __llvm_libc::testing::Test::runTests(); } 354