xref: /aosp_15_r20/external/llvm-libc/test/UnitTest/LibcTest.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 "include/llvm-libc-macros/stdfix-macros.h"
12 #include "src/__support/CPP/string.h"
13 #include "src/__support/CPP/string_view.h"
14 #include "src/__support/fixed_point/fx_rep.h"
15 #include "src/__support/macros/config.h"
16 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
17 #include "src/__support/uint128.h"
18 #include "test/UnitTest/TestLogger.h"
19 
20 #if __STDC_HOSTED__
21 #include <time.h>
22 #define LIBC_TEST_USE_CLOCK
23 #elif defined(TARGET_SUPPORTS_CLOCK)
24 #include <time.h>
25 
26 #include "src/time/clock.h"
clock()27 extern "C" clock_t clock() noexcept { return LIBC_NAMESPACE::clock(); }
28 #define LIBC_TEST_USE_CLOCK
29 #endif
30 
31 namespace LIBC_NAMESPACE_DECL {
32 namespace testing {
33 
34 namespace internal {
35 
operator <<(TestLogger & logger,Location Loc)36 TestLogger &operator<<(TestLogger &logger, Location Loc) {
37   return logger << Loc.file << ":" << Loc.line << ": FAILURE\n";
38 }
39 
40 // When the value is UInt128, __uint128_t or wider, show its hexadecimal
41 // digits.
42 template <typename T>
43 cpp::enable_if_t<(cpp::is_integral_v<T> && (sizeof(T) > sizeof(uint64_t))) ||
44                      is_big_int_v<T>,
45                  cpp::string>
describeValue(T Value)46 describeValue(T Value) {
47   const IntegerToString<T, radix::Hex::WithPrefix> buffer(Value);
48   return buffer.view();
49 }
50 
51 // When the value is of a standard integral type, just display it as normal.
52 template <typename T>
53 cpp::enable_if_t<cpp::is_integral_v<T> && (sizeof(T) <= sizeof(uint64_t)),
54                  cpp::string>
describeValue(T Value)55 describeValue(T Value) {
56   return cpp::to_string(Value);
57 }
58 
59 #ifdef LIBC_COMPILER_HAS_FIXED_POINT
60 template <typename T>
describeValue(T Value)61 cpp::enable_if_t<cpp::is_fixed_point_v<T>, cpp::string> describeValue(T Value) {
62   using FXRep = fixed_point::FXRep<T>;
63   using comp_t = typename FXRep::CompType;
64 
65   return cpp::to_string(cpp::bit_cast<comp_t>(Value)) + " * 2^-" +
66          cpp::to_string(FXRep::FRACTION_LEN);
67 }
68 #endif // LIBC_COMPILER_HAS_FIXED_POINT
69 
describeValue(const cpp::string & Value)70 cpp::string_view describeValue(const cpp::string &Value) { return Value; }
describeValue(cpp::string_view Value)71 cpp::string_view describeValue(cpp::string_view Value) { return Value; }
72 
73 template <typename ValType>
test(RunContext * Ctx,TestCond Cond,ValType LHS,ValType RHS,const char * LHSStr,const char * RHSStr,Location Loc)74 bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS,
75           const char *LHSStr, const char *RHSStr, Location Loc) {
76   auto ExplainDifference = [=, &Ctx](bool Cond,
77                                      cpp::string_view OpString) -> bool {
78     if (Cond)
79       return true;
80     Ctx->markFail();
81     size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0;
82     cpp::string Offset(OffsetLength, ' ');
83     tlog << Loc;
84     tlog << Offset << "Expected: " << LHSStr << '\n'
85          << Offset << "Which is: " << describeValue(LHS) << '\n'
86          << "To be " << OpString << ": " << RHSStr << '\n'
87          << Offset << "Which is: " << describeValue(RHS) << '\n';
88     return false;
89   };
90 
91   switch (Cond) {
92   case TestCond::EQ:
93     return ExplainDifference(LHS == RHS, "equal to");
94   case TestCond::NE:
95     return ExplainDifference(LHS != RHS, "not equal to");
96   case TestCond::LT:
97     return ExplainDifference(LHS < RHS, "less than");
98   case TestCond::LE:
99     return ExplainDifference(LHS <= RHS, "less than or equal to");
100   case TestCond::GT:
101     return ExplainDifference(LHS > RHS, "greater than");
102   case TestCond::GE:
103     return ExplainDifference(LHS >= RHS, "greater than or equal to");
104   }
105   __builtin_unreachable();
106 }
107 
108 } // namespace internal
109 
110 Test *Test::Start = nullptr;
111 Test *Test::End = nullptr;
112 
113 int argc = 0;
114 char **argv = nullptr;
115 char **envp = nullptr;
116 
117 using internal::RunContext;
118 
addTest(Test * T)119 void Test::addTest(Test *T) {
120   if (End == nullptr) {
121     Start = T;
122     End = T;
123     return;
124   }
125 
126   End->Next = T;
127   End = T;
128 }
129 
getNumTests()130 int Test::getNumTests() {
131   int N = 0;
132   for (Test *T = Start; T; T = T->Next, ++N)
133     ;
134   return N;
135 }
136 
runTests(const TestOptions & Options)137 int Test::runTests(const TestOptions &Options) {
138   const char *green = Options.PrintColor ? "\033[32m" : "";
139   const char *red = Options.PrintColor ? "\033[31m" : "";
140   const char *reset = Options.PrintColor ? "\033[0m" : "";
141 
142   int TestCount = getNumTests();
143   if (TestCount) {
144     tlog << green << "[==========] " << reset << "Running " << TestCount
145          << " test";
146     if (TestCount > 1)
147       tlog << "s";
148     tlog << " from 1 test suite.\n";
149   }
150 
151   int FailCount = 0;
152   for (Test *T = Start; T != nullptr; T = T->Next) {
153     const char *TestName = T->getName();
154 
155     if (Options.TestFilter && cpp::string(TestName) != Options.TestFilter) {
156       --TestCount;
157       continue;
158     }
159 
160     tlog << green << "[ RUN      ] " << reset << TestName << '\n';
161     [[maybe_unused]] const uint64_t start_time = clock();
162     RunContext Ctx;
163     T->SetUp();
164     T->setContext(&Ctx);
165     T->Run();
166     T->TearDown();
167     [[maybe_unused]] const uint64_t end_time = clock();
168     switch (Ctx.status()) {
169     case RunContext::RunResult::Fail:
170       tlog << red << "[  FAILED  ] " << reset << TestName << '\n';
171       ++FailCount;
172       break;
173     case RunContext::RunResult::Pass:
174       tlog << green << "[       OK ] " << reset << TestName;
175 #ifdef LIBC_TEST_USE_CLOCK
176       tlog << " (";
177       if (start_time > end_time) {
178         tlog << "unknown - try rerunning)\n";
179       } else {
180         const auto duration = end_time - start_time;
181         const uint64_t duration_ms = (duration * 1000) / CLOCKS_PER_SEC;
182         const uint64_t duration_us = (duration * 1000 * 1000) / CLOCKS_PER_SEC;
183         const uint64_t duration_ns =
184             (duration * 1000 * 1000 * 1000) / CLOCKS_PER_SEC;
185         if (Options.TimeInMs || duration_ms != 0)
186           tlog << duration_ms << " ms)\n";
187         else if (duration_us != 0)
188           tlog << duration_us << " us)\n";
189         else
190           tlog << duration_ns << " ns)\n";
191       }
192 #else
193       tlog << '\n';
194 #endif
195       break;
196     }
197   }
198 
199   if (TestCount > 0) {
200     tlog << "Ran " << TestCount << " tests. "
201          << " PASS: " << TestCount - FailCount << ' ' << " FAIL: " << FailCount
202          << '\n';
203   } else {
204     tlog << "No tests run.\n";
205     if (Options.TestFilter) {
206       tlog << "No matching test for " << Options.TestFilter << '\n';
207     }
208   }
209 
210   return FailCount > 0 || TestCount == 0 ? 1 : 0;
211 }
212 
213 namespace internal {
214 
215 #define TEST_SPECIALIZATION(TYPE)                                              \
216   template bool test<TYPE>(RunContext * Ctx, TestCond Cond, TYPE LHS,          \
217                            TYPE RHS, const char *LHSStr, const char *RHSStr,   \
218                            Location Loc)
219 
220 TEST_SPECIALIZATION(char);
221 TEST_SPECIALIZATION(short);
222 TEST_SPECIALIZATION(int);
223 TEST_SPECIALIZATION(long);
224 TEST_SPECIALIZATION(long long);
225 
226 TEST_SPECIALIZATION(unsigned char);
227 TEST_SPECIALIZATION(unsigned short);
228 TEST_SPECIALIZATION(unsigned int);
229 TEST_SPECIALIZATION(unsigned long);
230 TEST_SPECIALIZATION(unsigned long long);
231 
232 TEST_SPECIALIZATION(bool);
233 
234 // We cannot just use a single UInt128 specialization as that resolves to only
235 // one type, UInt<128> or __uint128_t. We want both overloads as we want to
236 #ifdef LIBC_TYPES_HAS_INT128
237 // When builtin __uint128_t type is available, include its specialization
238 // also.
239 TEST_SPECIALIZATION(__uint128_t);
240 #endif // LIBC_TYPES_HAS_INT128
241 
242 TEST_SPECIALIZATION(LIBC_NAMESPACE::Int<128>);
243 
244 TEST_SPECIALIZATION(LIBC_NAMESPACE::UInt<96>);
245 TEST_SPECIALIZATION(LIBC_NAMESPACE::UInt<128>);
246 TEST_SPECIALIZATION(LIBC_NAMESPACE::UInt<192>);
247 TEST_SPECIALIZATION(LIBC_NAMESPACE::UInt<256>);
248 TEST_SPECIALIZATION(LIBC_NAMESPACE::UInt<320>);
249 
250 TEST_SPECIALIZATION(LIBC_NAMESPACE::cpp::string_view);
251 TEST_SPECIALIZATION(LIBC_NAMESPACE::cpp::string);
252 
253 #ifdef LIBC_COMPILER_HAS_FIXED_POINT
254 TEST_SPECIALIZATION(short fract);
255 TEST_SPECIALIZATION(fract);
256 TEST_SPECIALIZATION(long fract);
257 TEST_SPECIALIZATION(unsigned short fract);
258 TEST_SPECIALIZATION(unsigned fract);
259 TEST_SPECIALIZATION(unsigned long fract);
260 
261 TEST_SPECIALIZATION(short accum);
262 TEST_SPECIALIZATION(accum);
263 TEST_SPECIALIZATION(long accum);
264 TEST_SPECIALIZATION(unsigned short accum);
265 TEST_SPECIALIZATION(unsigned accum);
266 TEST_SPECIALIZATION(unsigned long accum);
267 #endif // LIBC_COMPILER_HAS_FIXED_POINT
268 
269 } // namespace internal
270 
testStrEq(const char * LHS,const char * RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)271 bool Test::testStrEq(const char *LHS, const char *RHS, const char *LHSStr,
272                      const char *RHSStr, internal::Location Loc) {
273   return internal::test(
274       Ctx, TestCond::EQ, LHS ? cpp::string_view(LHS) : cpp::string_view(),
275       RHS ? cpp::string_view(RHS) : cpp::string_view(), LHSStr, RHSStr, Loc);
276 }
277 
testStrNe(const char * LHS,const char * RHS,const char * LHSStr,const char * RHSStr,internal::Location Loc)278 bool Test::testStrNe(const char *LHS, const char *RHS, const char *LHSStr,
279                      const char *RHSStr, internal::Location Loc) {
280   return internal::test(
281       Ctx, TestCond::NE, LHS ? cpp::string_view(LHS) : cpp::string_view(),
282       RHS ? cpp::string_view(RHS) : cpp::string_view(), LHSStr, RHSStr, Loc);
283 }
284 
testMatch(bool MatchResult,MatcherBase & Matcher,const char * LHSStr,const char * RHSStr,internal::Location Loc)285 bool Test::testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,
286                      const char *RHSStr, internal::Location Loc) {
287   if (MatchResult)
288     return true;
289 
290   Ctx->markFail();
291   if (!Matcher.is_silent()) {
292     tlog << Loc;
293     tlog << "Failed to match " << LHSStr << " against " << RHSStr << ".\n";
294     Matcher.explainError();
295   }
296   return false;
297 }
298 
299 } // namespace testing
300 } // namespace LIBC_NAMESPACE_DECL
301