• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test/UnitTest/TestLogger.h"
2 #include "src/__support/CPP/string.h"
3 #include "src/__support/CPP/string_view.h"
4 #include "src/__support/OSUtil/io.h"               // write_to_stderr
5 #include "src/__support/big_int.h"                 // is_big_int
6 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
7 #include "src/__support/uint128.h"
8 
9 #include <stdint.h>
10 
11 namespace LIBC_NAMESPACE {
12 namespace testing {
13 
14 // cpp::string_view specialization
15 template <>
16 TestLogger &TestLogger::operator<< <cpp::string_view>(cpp::string_view str) {
17   LIBC_NAMESPACE::write_to_stderr(str);
18   return *this;
19 }
20 
21 // cpp::string specialization
22 template <> TestLogger &TestLogger::operator<< <cpp::string>(cpp::string str) {
23   return *this << static_cast<cpp::string_view>(str);
24 }
25 
26 // const char* specialization
27 template <> TestLogger &TestLogger::operator<< <const char *>(const char *str) {
28   return *this << cpp::string_view(str);
29 }
30 
31 // char* specialization
32 template <> TestLogger &TestLogger::operator<< <char *>(char *str) {
33   return *this << cpp::string_view(str);
34 }
35 
36 // char specialization
operator <<(char ch)37 template <> TestLogger &TestLogger::operator<<(char ch) {
38   return *this << cpp::string_view(&ch, 1);
39 }
40 
41 // bool specialization
operator <<(bool cond)42 template <> TestLogger &TestLogger::operator<<(bool cond) {
43   return *this << (cond ? "true" : "false");
44 }
45 
46 // void * specialization
operator <<(void * addr)47 template <> TestLogger &TestLogger::operator<<(void *addr) {
48   return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));
49 }
50 
operator <<(T t)51 template <typename T> TestLogger &TestLogger::operator<<(T t) {
52   if constexpr (is_big_int_v<T> ||
53                 (cpp::is_integral_v<T> && cpp::is_unsigned_v<T> &&
54                  (sizeof(T) > sizeof(uint64_t)))) {
55     static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
56     const IntegerToString<T, radix::Hex::WithPrefix> buffer(t);
57     return *this << buffer.view();
58   } else {
59     return *this << cpp::to_string(t);
60   }
61 }
62 
63 // is_integral specializations
64 // char is already specialized to handle character
65 template TestLogger &TestLogger::operator<< <short>(short);
66 template TestLogger &TestLogger::operator<< <int>(int);
67 template TestLogger &TestLogger::operator<< <long>(long);
68 template TestLogger &TestLogger::operator<< <long long>(long long);
69 template TestLogger &TestLogger::operator<< <unsigned char>(unsigned char);
70 template TestLogger &TestLogger::operator<< <unsigned short>(unsigned short);
71 template TestLogger &TestLogger::operator<< <unsigned int>(unsigned int);
72 template TestLogger &TestLogger::operator<< <unsigned long>(unsigned long);
73 template TestLogger &
74     TestLogger::operator<< <unsigned long long>(unsigned long long);
75 
76 #ifdef LIBC_TYPES_HAS_INT128
77 template TestLogger &TestLogger::operator<< <__uint128_t>(__uint128_t);
78 #endif // LIBC_TYPES_HAS_INT128
79 template TestLogger &TestLogger::operator<< <UInt<128>>(UInt<128>);
80 template TestLogger &TestLogger::operator<< <UInt<192>>(UInt<192>);
81 template TestLogger &TestLogger::operator<< <UInt<256>>(UInt<256>);
82 template TestLogger &TestLogger::operator<< <UInt<320>>(UInt<320>);
83 
84 // TODO: Add floating point formatting once it's supported by StringStream.
85 
86 TestLogger tlog;
87 
88 } // namespace testing
89 } // namespace LIBC_NAMESPACE
90