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