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 std::string describeValue128(__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 <> std::string describeValue<__int128_t>(__int128_t Value) { 59 return describeValue128(Value); 60 } 61 template <> std::string describeValue<__uint128_t>(__uint128_t Value) { 62 return describeValue128(Value); 63 } 64 65 template <typename ValType> 66 void explainDifference(ValType LHS, ValType RHS, const char *LHSStr, 67 const char *RHSStr, const char *File, unsigned long Line, 68 std::string OpString) { 69 size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; 70 std::string Offset(OffsetLength, ' '); 71 72 std::cout << File << ":" << Line << ": FAILURE\n" 73 << Offset << "Expected: " << LHSStr << '\n' 74 << Offset << "Which is: " << describeValue(LHS) << '\n' 75 << "To be " << OpString << ": " << RHSStr << '\n' 76 << Offset << "Which is: " << describeValue(RHS) << '\n'; 77 } 78 79 template <typename ValType> 80 bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS, 81 const char *LHSStr, const char *RHSStr, const char *File, 82 unsigned long Line) { 83 auto ExplainDifference = [=](std::string OpString) { 84 explainDifference(LHS, RHS, LHSStr, RHSStr, File, Line, OpString); 85 }; 86 87 switch (Cond) { 88 case Cond_EQ: 89 if (LHS == RHS) 90 return true; 91 92 Ctx->markFail(); 93 ExplainDifference("equal to"); 94 return false; 95 case Cond_NE: 96 if (LHS != RHS) 97 return true; 98 99 Ctx->markFail(); 100 ExplainDifference("not equal to"); 101 return false; 102 case Cond_LT: 103 if (LHS < RHS) 104 return true; 105 106 Ctx->markFail(); 107 ExplainDifference("less than"); 108 return false; 109 case Cond_LE: 110 if (LHS <= RHS) 111 return true; 112 113 Ctx->markFail(); 114 ExplainDifference("less than or equal to"); 115 return false; 116 case Cond_GT: 117 if (LHS > RHS) 118 return true; 119 120 Ctx->markFail(); 121 ExplainDifference("greater than"); 122 return false; 123 case Cond_GE: 124 if (LHS >= RHS) 125 return true; 126 127 Ctx->markFail(); 128 ExplainDifference("greater than or equal to"); 129 return false; 130 default: 131 Ctx->markFail(); 132 std::cout << "Unexpected test condition.\n"; 133 return false; 134 } 135 } 136 137 } // namespace internal 138 139 Test *Test::Start = nullptr; 140 Test *Test::End = nullptr; 141 142 void Test::addTest(Test *T) { 143 if (End == nullptr) { 144 Start = T; 145 End = T; 146 return; 147 } 148 149 End->Next = T; 150 End = T; 151 } 152 153 int Test::runTests(const char *TestFilter) { 154 int TestCount = 0; 155 int FailCount = 0; 156 for (Test *T = Start; T != nullptr; T = T->Next) { 157 const char *TestName = T->getName(); 158 std::string StrTestName(TestName); 159 constexpr auto GREEN = "\033[32m"; 160 constexpr auto RED = "\033[31m"; 161 constexpr auto RESET = "\033[0m"; 162 if ((TestFilter != nullptr) && (StrTestName != TestFilter)) { 163 continue; 164 } 165 std::cout << GREEN << "[ RUN ] " << RESET << TestName << '\n'; 166 RunContext Ctx; 167 T->SetUp(); 168 T->setContext(&Ctx); 169 T->Run(); 170 T->TearDown(); 171 auto Result = Ctx.status(); 172 switch (Result) { 173 case RunContext::Result_Fail: 174 std::cout << RED << "[ FAILED ] " << RESET << TestName << '\n'; 175 ++FailCount; 176 break; 177 case RunContext::Result_Pass: 178 std::cout << GREEN << "[ OK ] " << RESET << TestName << '\n'; 179 break; 180 } 181 ++TestCount; 182 } 183 184 if (TestCount > 0) { 185 std::cout << "Ran " << TestCount << " tests. " 186 << " PASS: " << TestCount - FailCount << ' ' 187 << " FAIL: " << FailCount << '\n'; 188 } else { 189 std::cout << "No tests run.\n"; 190 if (TestFilter) { 191 std::cout << "No matching test for " << TestFilter << '\n'; 192 } 193 } 194 195 return FailCount > 0 || TestCount == 0 ? 1 : 0; 196 } 197 198 template bool Test::test<char, 0>(TestCondition Cond, char LHS, char RHS, 199 const char *LHSStr, const char *RHSStr, 200 const char *File, unsigned long Line); 201 202 template bool Test::test<short, 0>(TestCondition Cond, short LHS, short RHS, 203 const char *LHSStr, const char *RHSStr, 204 const char *File, unsigned long Line); 205 206 template bool Test::test<int, 0>(TestCondition Cond, int LHS, int RHS, 207 const char *LHSStr, const char *RHSStr, 208 const char *File, unsigned long Line); 209 210 template bool Test::test<long, 0>(TestCondition Cond, long LHS, long RHS, 211 const char *LHSStr, const char *RHSStr, 212 const char *File, unsigned long Line); 213 214 template bool Test::test<long long, 0>(TestCondition Cond, long long LHS, 215 long long RHS, const char *LHSStr, 216 const char *RHSStr, const char *File, 217 unsigned long Line); 218 219 template bool Test::test<__int128_t, 0>(TestCondition Cond, __int128_t LHS, 220 __int128_t RHS, const char *LHSStr, 221 const char *RHSStr, const char *File, 222 unsigned long Line); 223 224 template bool Test::test<unsigned char, 0>(TestCondition Cond, 225 unsigned char LHS, unsigned char RHS, 226 const char *LHSStr, 227 const char *RHSStr, const char *File, 228 unsigned long Line); 229 230 template bool 231 Test::test<unsigned short, 0>(TestCondition Cond, unsigned short LHS, 232 unsigned short RHS, const char *LHSStr, 233 const char *RHSStr, const char *File, 234 unsigned long Line); 235 236 template bool Test::test<unsigned int, 0>(TestCondition Cond, unsigned int LHS, 237 unsigned int RHS, const char *LHSStr, 238 const char *RHSStr, const char *File, 239 unsigned long Line); 240 241 template bool Test::test<unsigned long, 0>(TestCondition Cond, 242 unsigned long LHS, unsigned long RHS, 243 const char *LHSStr, 244 const char *RHSStr, const char *File, 245 unsigned long Line); 246 247 template bool Test::test<bool, 0>(TestCondition Cond, bool LHS, bool RHS, 248 const char *LHSStr, const char *RHSStr, 249 const char *File, unsigned long Line); 250 251 template bool 252 Test::test<unsigned long long, 0>(TestCondition Cond, unsigned long long LHS, 253 unsigned long long RHS, const char *LHSStr, 254 const char *RHSStr, const char *File, 255 unsigned long Line); 256 257 template bool Test::test<__uint128_t, 0>(TestCondition Cond, __uint128_t LHS, 258 __uint128_t RHS, const char *LHSStr, 259 const char *RHSStr, const char *File, 260 unsigned long Line); 261 262 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr, 263 const char *RHSStr, const char *File, unsigned long Line) { 264 return internal::test(Ctx, Cond_EQ, LHS ? std::string(LHS) : std::string(), 265 RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr, 266 File, Line); 267 } 268 269 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr, 270 const char *RHSStr, const char *File, unsigned long Line) { 271 return internal::test(Ctx, Cond_NE, LHS ? std::string(LHS) : std::string(), 272 RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr, 273 File, Line); 274 } 275 276 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr, 277 const char *RHSStr, const char *File, unsigned long Line) { 278 if (MatchResult) 279 return true; 280 281 Ctx->markFail(); 282 std::cout << File << ":" << Line << ": FAILURE\n" 283 << "Failed to match " << LHSStr << " against " << RHSStr << ".\n"; 284 testutils::StreamWrapper OutsWrapper = testutils::outs(); 285 Matcher.explainError(OutsWrapper); 286 return false; 287 } 288 289 #ifdef ENABLE_SUBPROCESS_TESTS 290 291 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal, 292 const char *LHSStr, const char *RHSStr, 293 const char *File, unsigned long Line) { 294 testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500); 295 296 if (const char *error = Result.getError()) { 297 Ctx->markFail(); 298 std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n'; 299 return false; 300 } 301 302 if (Result.timedOut()) { 303 Ctx->markFail(); 304 std::cout << File << ":" << Line << ": FAILURE\n" 305 << "Process timed out after " << 500 << " milliseconds.\n"; 306 return false; 307 } 308 309 if (Result.exitedNormally()) { 310 Ctx->markFail(); 311 std::cout << File << ":" << Line << ": FAILURE\n" 312 << "Expected " << LHSStr 313 << " to be killed by a signal\nBut it exited normally!\n"; 314 return false; 315 } 316 317 int KilledBy = Result.getFatalSignal(); 318 assert(KilledBy != 0 && "Not killed by any signal"); 319 if (Signal == -1 || KilledBy == Signal) 320 return true; 321 322 using testutils::signalAsString; 323 Ctx->markFail(); 324 std::cout << File << ":" << Line << ": FAILURE\n" 325 << " Expected: " << LHSStr << '\n' 326 << "To be killed by signal: " << Signal << '\n' 327 << " Which is: " << signalAsString(Signal) << '\n' 328 << " But it was killed by: " << KilledBy << '\n' 329 << " Which is: " << signalAsString(KilledBy) << '\n'; 330 return false; 331 } 332 333 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode, 334 const char *LHSStr, const char *RHSStr, 335 const char *File, unsigned long Line) { 336 testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500); 337 338 if (const char *error = Result.getError()) { 339 Ctx->markFail(); 340 std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n'; 341 return false; 342 } 343 344 if (Result.timedOut()) { 345 Ctx->markFail(); 346 std::cout << File << ":" << Line << ": FAILURE\n" 347 << "Process timed out after " << 500 << " milliseconds.\n"; 348 return false; 349 } 350 351 if (!Result.exitedNormally()) { 352 Ctx->markFail(); 353 std::cout << File << ":" << Line << ": FAILURE\n" 354 << "Expected " << LHSStr << '\n' 355 << "to exit with exit code " << ExitCode << '\n' 356 << "But it exited abnormally!\n"; 357 return false; 358 } 359 360 int ActualExit = Result.getExitCode(); 361 if (ActualExit == ExitCode) 362 return true; 363 364 Ctx->markFail(); 365 std::cout << File << ":" << Line << ": FAILURE\n" 366 << "Expected exit code of: " << LHSStr << '\n' 367 << " Which is: " << ActualExit << '\n' 368 << " To be equal to: " << RHSStr << '\n' 369 << " Which is: " << ExitCode << '\n'; 370 return false; 371 } 372 373 #endif // ENABLE_SUBPROCESS_TESTS 374 } // namespace testing 375 } // namespace __llvm_libc 376