xref: /llvm-project/libc/test/UnitTest/LibcTest.cpp (revision 5a55dfb80c944451f944d100d363c4db2e49547f)
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 "src/__support/CPP/string_view.h"
12 #include "src/__support/UInt128.h"
13 #include "utils/testutils/ExecuteFunction.h"
14 #include <cassert>
15 #include <iostream>
16 #include <string>
17 
18 namespace __llvm_libc {
19 namespace testing {
20 
21 // This need not be a class as all it has is a single read-write state variable.
22 // But, we make it class as then its implementation can be hidden from the
23 // header file.
24 class RunContext {
25 public:
26   enum RunResult { Result_Pass = 1, Result_Fail = 2 };
27 
28   RunResult status() const { return Status; }
29 
30   void markFail() { Status = Result_Fail; }
31 
32 private:
33   RunResult Status = Result_Pass;
34 };
35 
36 namespace internal {
37 
38 // When the value is UInt128 or __uint128_t, show its hexadecimal digits.
39 // We cannot just use a UInt128 specialization as that resolves to only
40 // one type, UInt<128> or __uint128_t. We want both overloads as we want to
41 // be able to unittest UInt<128> on platforms where UInt128 resolves to
42 // UInt128.
43 template <typename T>
44 cpp::enable_if_t<cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, std::string>
45 describeValueUInt(T Value) {
46   static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
47   std::string S(sizeof(T) * 2, '0');
48 
49   constexpr char HEXADECIMALS[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
50                                      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
51 
52   for (auto I = S.rbegin(), End = S.rend(); I != End; ++I, Value >>= 8) {
53     unsigned char Mod = static_cast<unsigned char>(Value) & 0xFF;
54     *(I++) = HEXADECIMALS[Mod & 0x0F];
55     *I = HEXADECIMALS[Mod >> 4];
56   }
57 
58   return "0x" + S;
59 }
60 
61 // When the value is of integral type, just display it as normal.
62 template <typename ValType>
63 cpp::enable_if_t<cpp::is_integral_v<ValType>, std::string>
64 describeValue(ValType Value) {
65   if constexpr (sizeof(ValType) <= sizeof(uint64_t)) {
66     return std::to_string(Value);
67   } else {
68     return describeValueUInt(Value);
69   }
70 }
71 
72 std::string describeValue(std::string Value) { return std::string(Value); }
73 std::string describeValue(cpp::string_view Value) {
74   return std::string(Value.data(), Value.size());
75 }
76 
77 template <typename ValType>
78 void explainDifference(ValType LHS, ValType RHS, const char *LHSStr,
79                        const char *RHSStr, const char *File, unsigned long Line,
80                        std::string OpString) {
81   size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0;
82   std::string Offset(OffsetLength, ' ');
83 
84   std::cout << File << ":" << Line << ": FAILURE\n"
85             << Offset << "Expected: " << LHSStr << '\n'
86             << Offset << "Which is: " << describeValue(LHS) << '\n'
87             << "To be " << OpString << ": " << RHSStr << '\n'
88             << Offset << "Which is: " << describeValue(RHS) << '\n';
89 }
90 
91 template <typename ValType>
92 bool test(RunContext *Ctx, TestCondition Cond, ValType LHS, ValType RHS,
93           const char *LHSStr, const char *RHSStr, const char *File,
94           unsigned long Line) {
95   auto ExplainDifference = [=](std::string OpString) {
96     explainDifference(LHS, RHS, LHSStr, RHSStr, File, Line, OpString);
97   };
98 
99   switch (Cond) {
100   case Cond_EQ:
101     if (LHS == RHS)
102       return true;
103 
104     Ctx->markFail();
105     ExplainDifference("equal to");
106     return false;
107   case Cond_NE:
108     if (LHS != RHS)
109       return true;
110 
111     Ctx->markFail();
112     ExplainDifference("not equal to");
113     return false;
114   case Cond_LT:
115     if (LHS < RHS)
116       return true;
117 
118     Ctx->markFail();
119     ExplainDifference("less than");
120     return false;
121   case Cond_LE:
122     if (LHS <= RHS)
123       return true;
124 
125     Ctx->markFail();
126     ExplainDifference("less than or equal to");
127     return false;
128   case Cond_GT:
129     if (LHS > RHS)
130       return true;
131 
132     Ctx->markFail();
133     ExplainDifference("greater than");
134     return false;
135   case Cond_GE:
136     if (LHS >= RHS)
137       return true;
138 
139     Ctx->markFail();
140     ExplainDifference("greater than or equal to");
141     return false;
142   default:
143     Ctx->markFail();
144     std::cout << "Unexpected test condition.\n";
145     return false;
146   }
147 }
148 
149 } // namespace internal
150 
151 Test *Test::Start = nullptr;
152 Test *Test::End = nullptr;
153 
154 void Test::addTest(Test *T) {
155   if (End == nullptr) {
156     Start = T;
157     End = T;
158     return;
159   }
160 
161   End->Next = T;
162   End = T;
163 }
164 
165 int Test::runTests(const char *TestFilter) {
166   int TestCount = 0;
167   int FailCount = 0;
168   for (Test *T = Start; T != nullptr; T = T->Next) {
169     const char *TestName = T->getName();
170     std::string StrTestName(TestName);
171     constexpr auto GREEN = "\033[32m";
172     constexpr auto RED = "\033[31m";
173     constexpr auto RESET = "\033[0m";
174     if ((TestFilter != nullptr) && (StrTestName != TestFilter)) {
175       continue;
176     }
177     std::cout << GREEN << "[ RUN      ] " << RESET << TestName << '\n';
178     RunContext Ctx;
179     T->SetUp();
180     T->setContext(&Ctx);
181     T->Run();
182     T->TearDown();
183     auto Result = Ctx.status();
184     switch (Result) {
185     case RunContext::Result_Fail:
186       std::cout << RED << "[  FAILED  ] " << RESET << TestName << '\n';
187       ++FailCount;
188       break;
189     case RunContext::Result_Pass:
190       std::cout << GREEN << "[       OK ] " << RESET << TestName << '\n';
191       break;
192     }
193     ++TestCount;
194   }
195 
196   if (TestCount > 0) {
197     std::cout << "Ran " << TestCount << " tests. "
198               << " PASS: " << TestCount - FailCount << ' '
199               << " FAIL: " << FailCount << '\n';
200   } else {
201     std::cout << "No tests run.\n";
202     if (TestFilter) {
203       std::cout << "No matching test for " << TestFilter << '\n';
204     }
205   }
206 
207   return FailCount > 0 || TestCount == 0 ? 1 : 0;
208 }
209 
210 namespace internal {
211 
212 template bool test<char>(RunContext *Ctx, TestCondition Cond, char LHS,
213                          char RHS, const char *LHSStr, const char *RHSStr,
214                          const char *File, unsigned long Line);
215 
216 template bool test<short>(RunContext *Ctx, TestCondition Cond, short LHS,
217                           short RHS, const char *LHSStr, const char *RHSStr,
218                           const char *File, unsigned long Line);
219 
220 template bool test<int>(RunContext *Ctx, TestCondition Cond, int LHS, int RHS,
221                         const char *LHSStr, const char *RHSStr,
222                         const char *File, unsigned long Line);
223 
224 template bool test<long>(RunContext *Ctx, TestCondition Cond, long LHS,
225                          long RHS, const char *LHSStr, const char *RHSStr,
226                          const char *File, unsigned long Line);
227 
228 template bool test<long long>(RunContext *Ctx, TestCondition Cond,
229                               long long LHS, long long RHS, const char *LHSStr,
230                               const char *RHSStr, const char *File,
231                               unsigned long Line);
232 
233 template bool test<unsigned char>(RunContext *Ctx, TestCondition Cond,
234                                   unsigned char LHS, unsigned char RHS,
235                                   const char *LHSStr, const char *RHSStr,
236                                   const char *File, unsigned long Line);
237 
238 template bool test<unsigned short>(RunContext *Ctx, TestCondition Cond,
239                                    unsigned short LHS, unsigned short RHS,
240                                    const char *LHSStr, const char *RHSStr,
241                                    const char *File, unsigned long Line);
242 
243 template bool test<unsigned int>(RunContext *Ctx, TestCondition Cond,
244                                  unsigned int LHS, unsigned int RHS,
245                                  const char *LHSStr, const char *RHSStr,
246                                  const char *File, unsigned long Line);
247 
248 template bool test<unsigned long>(RunContext *Ctx, TestCondition Cond,
249                                   unsigned long LHS, unsigned long RHS,
250                                   const char *LHSStr, const char *RHSStr,
251                                   const char *File, unsigned long Line);
252 
253 template bool test<bool>(RunContext *Ctx, TestCondition Cond, bool LHS,
254                          bool RHS, const char *LHSStr, const char *RHSStr,
255                          const char *File, unsigned long Line);
256 
257 template bool test<unsigned long long>(RunContext *Ctx, TestCondition Cond,
258                                        unsigned long long LHS,
259                                        unsigned long long RHS,
260                                        const char *LHSStr, const char *RHSStr,
261                                        const char *File, unsigned long Line);
262 
263 // We cannot just use a single UInt128 specialization as that resolves to only
264 // one type, UInt<128> or __uint128_t. We want both overloads as we want to
265 // be able to unittest UInt<128> on platforms where UInt128 resolves to
266 // UInt128.
267 #ifdef __SIZEOF_INT128__
268 // When builtin __uint128_t type is available, include its specialization
269 // also.
270 template bool test<__uint128_t>(RunContext *Ctx, TestCondition Cond,
271                                 __uint128_t LHS, __uint128_t RHS,
272                                 const char *LHSStr, const char *RHSStr,
273                                 const char *File, unsigned long Line);
274 #endif
275 
276 template bool test<__llvm_libc::cpp::UInt<128>>(
277     RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<128> LHS,
278     __llvm_libc::cpp::UInt<128> RHS, const char *LHSStr, const char *RHSStr,
279     const char *File, unsigned long Line);
280 
281 template bool test<__llvm_libc::cpp::UInt<192>>(
282     RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<192> LHS,
283     __llvm_libc::cpp::UInt<192> RHS, const char *LHSStr, const char *RHSStr,
284     const char *File, unsigned long Line);
285 
286 template bool test<__llvm_libc::cpp::UInt<256>>(
287     RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<256> LHS,
288     __llvm_libc::cpp::UInt<256> RHS, const char *LHSStr, const char *RHSStr,
289     const char *File, unsigned long Line);
290 
291 template bool test<__llvm_libc::cpp::UInt<320>>(
292     RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::UInt<320> LHS,
293     __llvm_libc::cpp::UInt<320> RHS, const char *LHSStr, const char *RHSStr,
294     const char *File, unsigned long Line);
295 
296 template bool test<__llvm_libc::cpp::string_view>(
297     RunContext *Ctx, TestCondition Cond, __llvm_libc::cpp::string_view LHS,
298     __llvm_libc::cpp::string_view RHS, const char *LHSStr, const char *RHSStr,
299     const char *File, unsigned long Line);
300 
301 } // namespace internal
302 
303 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
304                      const char *RHSStr, const char *File, unsigned long Line) {
305   return internal::test(Ctx, Cond_EQ, LHS ? std::string(LHS) : std::string(),
306                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
307                         File, Line);
308 }
309 
310 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
311                      const char *RHSStr, const char *File, unsigned long Line) {
312   return internal::test(Ctx, Cond_NE, LHS ? std::string(LHS) : std::string(),
313                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
314                         File, Line);
315 }
316 
317 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
318                      const char *RHSStr, const char *File, unsigned long Line) {
319   if (MatchResult)
320     return true;
321 
322   Ctx->markFail();
323   if (!Matcher.is_silent()) {
324     std::cout << File << ":" << Line << ": FAILURE\n"
325               << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
326     testutils::StreamWrapper OutsWrapper = testutils::outs();
327     Matcher.explainError(OutsWrapper);
328   }
329   return false;
330 }
331 
332 #ifdef ENABLE_SUBPROCESS_TESTS
333 
334 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
335                              const char *LHSStr, const char *RHSStr,
336                              const char *File, unsigned long Line) {
337   testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 500);
338 
339   if (const char *error = Result.get_error()) {
340     Ctx->markFail();
341     std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n';
342     return false;
343   }
344 
345   if (Result.timed_out()) {
346     Ctx->markFail();
347     std::cout << File << ":" << Line << ": FAILURE\n"
348               << "Process timed out after " << 500 << " milliseconds.\n";
349     return false;
350   }
351 
352   if (Result.exited_normally()) {
353     Ctx->markFail();
354     std::cout << File << ":" << Line << ": FAILURE\n"
355               << "Expected " << LHSStr
356               << " to be killed by a signal\nBut it exited normally!\n";
357     return false;
358   }
359 
360   int KilledBy = Result.get_fatal_signal();
361   assert(KilledBy != 0 && "Not killed by any signal");
362   if (Signal == -1 || KilledBy == Signal)
363     return true;
364 
365   using testutils::signal_as_string;
366   Ctx->markFail();
367   std::cout << File << ":" << Line << ": FAILURE\n"
368             << "              Expected: " << LHSStr << '\n'
369             << "To be killed by signal: " << Signal << '\n'
370             << "              Which is: " << signal_as_string(Signal) << '\n'
371             << "  But it was killed by: " << KilledBy << '\n'
372             << "              Which is: " << signal_as_string(KilledBy) << '\n';
373   return false;
374 }
375 
376 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
377                             const char *LHSStr, const char *RHSStr,
378                             const char *File, unsigned long Line) {
379   testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 500);
380 
381   if (const char *error = Result.get_error()) {
382     Ctx->markFail();
383     std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n';
384     return false;
385   }
386 
387   if (Result.timed_out()) {
388     Ctx->markFail();
389     std::cout << File << ":" << Line << ": FAILURE\n"
390               << "Process timed out after " << 500 << " milliseconds.\n";
391     return false;
392   }
393 
394   if (!Result.exited_normally()) {
395     Ctx->markFail();
396     std::cout << File << ":" << Line << ": FAILURE\n"
397               << "Expected " << LHSStr << '\n'
398               << "to exit with exit code " << ExitCode << '\n'
399               << "But it exited abnormally!\n";
400     return false;
401   }
402 
403   int ActualExit = Result.get_exit_code();
404   if (ActualExit == ExitCode)
405     return true;
406 
407   Ctx->markFail();
408   std::cout << File << ":" << Line << ": FAILURE\n"
409             << "Expected exit code of: " << LHSStr << '\n'
410             << "             Which is: " << ActualExit << '\n'
411             << "       To be equal to: " << RHSStr << '\n'
412             << "             Which is: " << ExitCode << '\n';
413   return false;
414 }
415 
416 #endif // ENABLE_SUBPROCESS_TESTS
417 } // namespace testing
418 } // namespace __llvm_libc
419