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 namespace internal {
199 
200 template bool test<char>(RunContext *Ctx, TestCondition Cond, char LHS,
201                          char RHS, const char *LHSStr, const char *RHSStr,
202                          const char *File, unsigned long Line);
203 
204 template bool test<short>(RunContext *Ctx, TestCondition Cond, short LHS,
205                           short RHS, const char *LHSStr, const char *RHSStr,
206                           const char *File, unsigned long Line);
207 
208 template bool test<int>(RunContext *Ctx, TestCondition Cond, int LHS, int RHS,
209                         const char *LHSStr, const char *RHSStr,
210                         const char *File, unsigned long Line);
211 
212 template bool test<long>(RunContext *Ctx, TestCondition Cond, long LHS,
213                          long RHS, const char *LHSStr, const char *RHSStr,
214                          const char *File, unsigned long Line);
215 
216 template bool test<long long>(RunContext *Ctx, TestCondition Cond,
217                               long long LHS, long long RHS, const char *LHSStr,
218                               const char *RHSStr, const char *File,
219                               unsigned long Line);
220 
221 template bool test<__int128_t>(RunContext *Ctx, TestCondition Cond,
222                                __int128_t LHS, __int128_t RHS,
223                                const char *LHSStr, const char *RHSStr,
224                                const char *File, unsigned long Line);
225 
226 template bool test<unsigned char>(RunContext *Ctx, TestCondition Cond,
227                                   unsigned char LHS, unsigned char RHS,
228                                   const char *LHSStr, const char *RHSStr,
229                                   const char *File, unsigned long Line);
230 
231 template bool test<unsigned short>(RunContext *Ctx, TestCondition Cond,
232                                    unsigned short LHS, unsigned short RHS,
233                                    const char *LHSStr, const char *RHSStr,
234                                    const char *File, unsigned long Line);
235 
236 template bool test<unsigned int>(RunContext *Ctx, TestCondition Cond,
237                                  unsigned int LHS, unsigned int RHS,
238                                  const char *LHSStr, const char *RHSStr,
239                                  const char *File, unsigned long Line);
240 
241 template bool test<unsigned long>(RunContext *Ctx, TestCondition Cond,
242                                   unsigned long LHS, unsigned long RHS,
243                                   const char *LHSStr, const char *RHSStr,
244                                   const char *File, unsigned long Line);
245 
246 template bool test<bool>(RunContext *Ctx, TestCondition Cond, bool LHS,
247                          bool RHS, const char *LHSStr, const char *RHSStr,
248                          const char *File, unsigned long Line);
249 
250 template bool test<unsigned long long>(RunContext *Ctx, TestCondition Cond,
251                                        unsigned long long LHS,
252                                        unsigned long long RHS,
253                                        const char *LHSStr, const char *RHSStr,
254                                        const char *File, unsigned long Line);
255 
256 template bool test<__uint128_t>(RunContext *Ctx, TestCondition Cond,
257                                 __uint128_t LHS, __uint128_t RHS,
258                                 const char *LHSStr, const char *RHSStr,
259                                 const char *File, unsigned long Line);
260 
261 } // namespace internal
262 
263 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
264                      const char *RHSStr, const char *File, unsigned long Line) {
265   return internal::test(Ctx, Cond_EQ, LHS ? std::string(LHS) : std::string(),
266                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
267                         File, Line);
268 }
269 
270 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
271                      const char *RHSStr, const char *File, unsigned long Line) {
272   return internal::test(Ctx, Cond_NE, LHS ? std::string(LHS) : std::string(),
273                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
274                         File, Line);
275 }
276 
277 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
278                      const char *RHSStr, const char *File, unsigned long Line) {
279   if (MatchResult)
280     return true;
281 
282   Ctx->markFail();
283   std::cout << File << ":" << Line << ": FAILURE\n"
284             << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
285   testutils::StreamWrapper OutsWrapper = testutils::outs();
286   Matcher.explainError(OutsWrapper);
287   return false;
288 }
289 
290 #ifdef ENABLE_SUBPROCESS_TESTS
291 
292 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
293                              const char *LHSStr, const char *RHSStr,
294                              const char *File, unsigned long Line) {
295   testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 500);
296 
297   if (const char *error = Result.get_error()) {
298     Ctx->markFail();
299     std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n';
300     return false;
301   }
302 
303   if (Result.timed_out()) {
304     Ctx->markFail();
305     std::cout << File << ":" << Line << ": FAILURE\n"
306               << "Process timed out after " << 500 << " milliseconds.\n";
307     return false;
308   }
309 
310   if (Result.exited_normally()) {
311     Ctx->markFail();
312     std::cout << File << ":" << Line << ": FAILURE\n"
313               << "Expected " << LHSStr
314               << " to be killed by a signal\nBut it exited normally!\n";
315     return false;
316   }
317 
318   int KilledBy = Result.get_fatal_signal();
319   assert(KilledBy != 0 && "Not killed by any signal");
320   if (Signal == -1 || KilledBy == Signal)
321     return true;
322 
323   using testutils::signal_as_string;
324   Ctx->markFail();
325   std::cout << File << ":" << Line << ": FAILURE\n"
326             << "              Expected: " << LHSStr << '\n'
327             << "To be killed by signal: " << Signal << '\n'
328             << "              Which is: " << signal_as_string(Signal) << '\n'
329             << "  But it was killed by: " << KilledBy << '\n'
330             << "              Which is: " << signal_as_string(KilledBy) << '\n';
331   return false;
332 }
333 
334 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
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 << '\n'
356               << "to exit with exit code " << ExitCode << '\n'
357               << "But it exited abnormally!\n";
358     return false;
359   }
360 
361   int ActualExit = Result.get_exit_code();
362   if (ActualExit == ExitCode)
363     return true;
364 
365   Ctx->markFail();
366   std::cout << File << ":" << Line << ": FAILURE\n"
367             << "Expected exit code of: " << LHSStr << '\n'
368             << "             Which is: " << ActualExit << '\n'
369             << "       To be equal to: " << RHSStr << '\n'
370             << "             Which is: " << ExitCode << '\n';
371   return false;
372 }
373 
374 #endif // ENABLE_SUBPROCESS_TESTS
375 } // namespace testing
376 } // namespace __llvm_libc
377