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