90 lines · cpp
1#include "test/UnitTest/TestLogger.h"2#include "hdr/stdint_proxy.h"3#include "src/__support/CPP/string.h"4#include "src/__support/CPP/string_view.h"5#include "src/__support/OSUtil/io.h" // write_to_stderr6#include "src/__support/big_int.h" // is_big_int7#include "src/__support/macros/config.h"8#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT1289#include "src/__support/uint128.h"10 11namespace LIBC_NAMESPACE_DECL {12namespace testing {13 14// cpp::string_view specialization15template <>16TestLogger &TestLogger::operator<< <cpp::string_view>(cpp::string_view str) {17 LIBC_NAMESPACE::write_to_stderr(str);18 return *this;19}20 21// cpp::string specialization22template <> TestLogger &TestLogger::operator<< <cpp::string>(cpp::string str) {23 return *this << static_cast<cpp::string_view>(str);24}25 26// const char* specialization27template <> TestLogger &TestLogger::operator<< <const char *>(const char *str) {28 return *this << cpp::string_view(str);29}30 31// char* specialization32template <> TestLogger &TestLogger::operator<< <char *>(char *str) {33 return *this << cpp::string_view(str);34}35 36// char specialization37template <> TestLogger &TestLogger::operator<<(char ch) {38 return *this << cpp::string_view(&ch, 1);39}40 41// bool specialization42template <> TestLogger &TestLogger::operator<<(bool cond) {43 return *this << (cond ? "true" : "false");44}45 46// void * specialization47template <> TestLogger &TestLogger::operator<<(void *addr) {48 return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));49}50 51template <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 specializations64// char is already specialized to handle character65template TestLogger &TestLogger::operator<< <short>(short);66template TestLogger &TestLogger::operator<< <int>(int);67template TestLogger &TestLogger::operator<< <long>(long);68template TestLogger &TestLogger::operator<< <long long>(long long);69template TestLogger &TestLogger::operator<< <unsigned char>(unsigned char);70template TestLogger &TestLogger::operator<< <unsigned short>(unsigned short);71template TestLogger &TestLogger::operator<< <unsigned int>(unsigned int);72template TestLogger &TestLogger::operator<< <unsigned long>(unsigned long);73template TestLogger &74 TestLogger::operator<< <unsigned long long>(unsigned long long);75 76#ifdef LIBC_TYPES_HAS_INT12877template TestLogger &TestLogger::operator<< <__uint128_t>(__uint128_t);78#endif // LIBC_TYPES_HAS_INT12879template TestLogger &TestLogger::operator<< <UInt<128>>(UInt<128>);80template TestLogger &TestLogger::operator<< <UInt<192>>(UInt<192>);81template TestLogger &TestLogger::operator<< <UInt<256>>(UInt<256>);82template TestLogger &TestLogger::operator<< <UInt<320>>(UInt<320>);83 84// TODO: Add floating point formatting once it's supported by StringStream.85 86TestLogger tlog;87 88} // namespace testing89} // namespace LIBC_NAMESPACE_DECL90