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/UInt128.h"
9 namespace LIBC_NAMESPACE
{
12 // cpp::string_view specialization
14 TestLogger
&TestLogger::operator<< <cpp::string_view
>(cpp::string_view str
) {
15 LIBC_NAMESPACE::write_to_stderr(str
);
19 // cpp::string specialization
20 template <> TestLogger
&TestLogger::operator<< <cpp::string
>(cpp::string str
) {
21 return *this << static_cast<cpp::string_view
>(str
);
24 // const char* specialization
25 template <> TestLogger
&TestLogger::operator<< <const char *>(const char *str
) {
26 return *this << cpp::string_view(str
);
29 // char* specialization
30 template <> TestLogger
&TestLogger::operator<< <char *>(char *str
) {
31 return *this << cpp::string_view(str
);
34 // char specialization
35 template <> TestLogger
&TestLogger::operator<<(char ch
) {
36 return *this << cpp::string_view(&ch
, 1);
39 // bool specialization
40 template <> TestLogger
&TestLogger::operator<<(bool cond
) {
41 return *this << (cond
? "true" : "false");
44 // void * specialization
45 template <> TestLogger
&TestLogger::operator<<(void *addr
) {
46 return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr
));
49 template <typename T
> TestLogger
&TestLogger::operator<<(T t
) {
50 if constexpr (cpp::is_integral_v
<T
> && cpp::is_unsigned_v
<T
> &&
51 sizeof(T
) > sizeof(uint64_t)) {
52 static_assert(sizeof(T
) % 8 == 0, "Unsupported size of UInt");
53 const IntegerToString
<T
, radix::Hex::WithPrefix
> buffer(t
);
54 return *this << buffer
.view();
56 return *this << cpp::to_string(t
);
60 // is_integral specializations
61 // char is already specialized to handle character
62 template TestLogger
&TestLogger::operator<< <short>(short);
63 template TestLogger
&TestLogger::operator<< <int>(int);
64 template TestLogger
&TestLogger::operator<< <long>(long);
65 template TestLogger
&TestLogger::operator<< <long long>(long long);
66 template TestLogger
&TestLogger::operator<< <unsigned char>(unsigned char);
67 template TestLogger
&TestLogger::operator<< <unsigned short>(unsigned short);
68 template TestLogger
&TestLogger::operator<< <unsigned int>(unsigned int);
69 template TestLogger
&TestLogger::operator<< <unsigned long>(unsigned long);
71 TestLogger::operator<< <unsigned long long>(unsigned long long);
73 #ifdef __SIZEOF_INT128__
74 template TestLogger
&TestLogger::operator<< <__uint128_t
>(__uint128_t
);
76 template TestLogger
&TestLogger::operator<< <cpp::UInt
<128>>(cpp::UInt
<128>);
77 template TestLogger
&TestLogger::operator<< <cpp::UInt
<192>>(cpp::UInt
<192>);
78 template TestLogger
&TestLogger::operator<< <cpp::UInt
<256>>(cpp::UInt
<256>);
79 template TestLogger
&TestLogger::operator<< <cpp::UInt
<320>>(cpp::UInt
<320>);
81 // TODO: Add floating point formatting once it's supported by StringStream.
85 } // namespace testing
86 } // namespace LIBC_NAMESPACE