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 
status() const26   RunResult status() const { return Status; }
27 
markFail()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>
describeValue(ValType Value)39 describeValue(ValType Value) {
40   return std::to_string(Value);
41 }
42 
describeValue(std::string Value)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.
describeValue(__uint128_t Value)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>
explainDifference(ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line,std::string OpString)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>
test(RunContext * Ctx,TestCondition Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)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 
addTest(Test * T)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 
runTests(const char * TestFilter)146 int Test::runTests(const char *TestFilter) {
147   int TestCount = 0;
148   int FailCount = 0;
149   for (Test *T = Start; T != nullptr; T = T->Next) {
150     const char *TestName = T->getName();
151     std::string StrTestName(TestName);
152     constexpr auto GREEN = "\033[32m";
153     constexpr auto RED = "\033[31m";
154     constexpr auto RESET = "\033[0m";
155     if ((TestFilter != nullptr) && (StrTestName != TestFilter)) {
156       continue;
157     }
158     std::cout << GREEN << "[ RUN      ] " << RESET << TestName << '\n';
159     RunContext Ctx;
160     T->SetUp();
161     T->setContext(&Ctx);
162     T->Run();
163     T->TearDown();
164     auto Result = Ctx.status();
165     switch (Result) {
166     case RunContext::Result_Fail:
167       std::cout << RED << "[  FAILED  ] " << RESET << TestName << '\n';
168       ++FailCount;
169       break;
170     case RunContext::Result_Pass:
171       std::cout << GREEN << "[       OK ] " << RESET << TestName << '\n';
172       break;
173     }
174     ++TestCount;
175   }
176 
177   if (TestCount > 0) {
178     std::cout << "Ran " << TestCount << " tests. "
179               << " PASS: " << TestCount - FailCount << ' '
180               << " FAIL: " << FailCount << '\n';
181   } else {
182     std::cout << "No tests run.\n";
183     if (TestFilter) {
184       std::cout << "No matching test for " << TestFilter << '\n';
185     }
186   }
187 
188   return FailCount > 0 || TestCount == 0 ? 1 : 0;
189 }
190 
191 template bool Test::test<char, 0>(TestCondition Cond, char LHS, char RHS,
192                                   const char *LHSStr, const char *RHSStr,
193                                   const char *File, unsigned long Line);
194 
195 template bool Test::test<short, 0>(TestCondition Cond, short LHS, short RHS,
196                                    const char *LHSStr, const char *RHSStr,
197                                    const char *File, unsigned long Line);
198 
199 template bool Test::test<int, 0>(TestCondition Cond, int LHS, int RHS,
200                                  const char *LHSStr, const char *RHSStr,
201                                  const char *File, unsigned long Line);
202 
203 template bool Test::test<long, 0>(TestCondition Cond, long LHS, long RHS,
204                                   const char *LHSStr, const char *RHSStr,
205                                   const char *File, unsigned long Line);
206 
207 template bool Test::test<long long, 0>(TestCondition Cond, long long LHS,
208                                        long long RHS, const char *LHSStr,
209                                        const char *RHSStr, const char *File,
210                                        unsigned long Line);
211 
212 template bool Test::test<unsigned char, 0>(TestCondition Cond,
213                                            unsigned char LHS, unsigned char RHS,
214                                            const char *LHSStr,
215                                            const char *RHSStr, const char *File,
216                                            unsigned long Line);
217 
218 template bool
219 Test::test<unsigned short, 0>(TestCondition Cond, unsigned short LHS,
220                               unsigned short RHS, const char *LHSStr,
221                               const char *RHSStr, const char *File,
222                               unsigned long Line);
223 
224 template bool Test::test<unsigned int, 0>(TestCondition Cond, unsigned int LHS,
225                                           unsigned int RHS, const char *LHSStr,
226                                           const char *RHSStr, const char *File,
227                                           unsigned long Line);
228 
229 template bool Test::test<unsigned long, 0>(TestCondition Cond,
230                                            unsigned long LHS, unsigned long RHS,
231                                            const char *LHSStr,
232                                            const char *RHSStr, const char *File,
233                                            unsigned long Line);
234 
235 template bool Test::test<bool, 0>(TestCondition Cond, bool LHS, bool RHS,
236                                   const char *LHSStr, const char *RHSStr,
237                                   const char *File, unsigned long Line);
238 
239 template bool
240 Test::test<unsigned long long, 0>(TestCondition Cond, unsigned long long LHS,
241                                   unsigned long long RHS, const char *LHSStr,
242                                   const char *RHSStr, const char *File,
243                                   unsigned long Line);
244 
245 template bool Test::test<__uint128_t, 0>(TestCondition Cond, __uint128_t LHS,
246                                          __uint128_t RHS, const char *LHSStr,
247                                          const char *RHSStr, const char *File,
248                                          unsigned long Line);
249 
testStrEq(const char * LHS,const char * RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)250 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
251                      const char *RHSStr, const char *File, unsigned long Line) {
252   return internal::test(Ctx, Cond_EQ, LHS ? std::string(LHS) : std::string(),
253                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
254                         File, Line);
255 }
256 
testStrNe(const char * LHS,const char * RHS,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)257 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
258                      const char *RHSStr, const char *File, unsigned long Line) {
259   return internal::test(Ctx, Cond_NE, LHS ? std::string(LHS) : std::string(),
260                         RHS ? std::string(RHS) : std::string(), LHSStr, RHSStr,
261                         File, Line);
262 }
263 
testMatch(bool MatchResult,MatcherBase & Matcher,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)264 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
265                      const char *RHSStr, const char *File, unsigned long Line) {
266   if (MatchResult)
267     return true;
268 
269   Ctx->markFail();
270   std::cout << File << ":" << Line << ": FAILURE\n"
271             << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
272   testutils::StreamWrapper OutsWrapper = testutils::outs();
273   Matcher.explainError(OutsWrapper);
274   return false;
275 }
276 
277 #ifdef ENABLE_SUBPROCESS_TESTS
278 
testProcessKilled(testutils::FunctionCaller * Func,int Signal,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)279 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
280                              const char *LHSStr, const char *RHSStr,
281                              const char *File, unsigned long Line) {
282   testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500);
283 
284   if (const char *error = Result.getError()) {
285     Ctx->markFail();
286     std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n';
287     return false;
288   }
289 
290   if (Result.timedOut()) {
291     Ctx->markFail();
292     std::cout << File << ":" << Line << ": FAILURE\n"
293               << "Process timed out after " << 500 << " milliseconds.\n";
294     return false;
295   }
296 
297   if (Result.exitedNormally()) {
298     Ctx->markFail();
299     std::cout << File << ":" << Line << ": FAILURE\n"
300               << "Expected " << LHSStr
301               << " to be killed by a signal\nBut it exited normally!\n";
302     return false;
303   }
304 
305   int KilledBy = Result.getFatalSignal();
306   assert(KilledBy != 0 && "Not killed by any signal");
307   if (Signal == -1 || KilledBy == Signal)
308     return true;
309 
310   using testutils::signalAsString;
311   Ctx->markFail();
312   std::cout << File << ":" << Line << ": FAILURE\n"
313             << "              Expected: " << LHSStr << '\n'
314             << "To be killed by signal: " << Signal << '\n'
315             << "              Which is: " << signalAsString(Signal) << '\n'
316             << "  But it was killed by: " << KilledBy << '\n'
317             << "              Which is: " << signalAsString(KilledBy) << '\n';
318   return false;
319 }
320 
testProcessExits(testutils::FunctionCaller * Func,int ExitCode,const char * LHSStr,const char * RHSStr,const char * File,unsigned long Line)321 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
322                             const char *LHSStr, const char *RHSStr,
323                             const char *File, unsigned long Line) {
324   testutils::ProcessStatus Result = testutils::invokeInSubprocess(Func, 500);
325 
326   if (const char *error = Result.getError()) {
327     Ctx->markFail();
328     std::cout << File << ":" << Line << ": FAILURE\n" << error << '\n';
329     return false;
330   }
331 
332   if (Result.timedOut()) {
333     Ctx->markFail();
334     std::cout << File << ":" << Line << ": FAILURE\n"
335               << "Process timed out after " << 500 << " milliseconds.\n";
336     return false;
337   }
338 
339   if (!Result.exitedNormally()) {
340     Ctx->markFail();
341     std::cout << File << ":" << Line << ": FAILURE\n"
342               << "Expected " << LHSStr << '\n'
343               << "to exit with exit code " << ExitCode << '\n'
344               << "But it exited abnormally!\n";
345     return false;
346   }
347 
348   int ActualExit = Result.getExitCode();
349   if (ActualExit == ExitCode)
350     return true;
351 
352   Ctx->markFail();
353   std::cout << File << ":" << Line << ": FAILURE\n"
354             << "Expected exit code of: " << LHSStr << '\n'
355             << "             Which is: " << ActualExit << '\n'
356             << "       To be equal to: " << RHSStr << '\n'
357             << "             Which is: " << ExitCode << '\n';
358   return false;
359 }
360 
361 #endif // ENABLE_SUBPROCESS_TESTS
362 } // namespace testing
363 } // namespace __llvm_libc
364