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
11namespace LIBC_NAMESPACE {
12namespace testing {
13
14// cpp::string_view specialization
15template <>
16TestLogger &TestLogger::operator<< <cpp::string_view>(cpp::string_view str) {
17 LIBC_NAMESPACE::write_to_stderr(msg: str);
18 return *this;
19}
20
21// cpp::string specialization
22template <> TestLogger &TestLogger::operator<< <cpp::string>(cpp::string str) {
23 return *this << static_cast<cpp::string_view>(str);
24}
25
26// const char* specialization
27template <> TestLogger &TestLogger::operator<< <const char *>(const char *str) {
28 return *this << cpp::string_view(str);
29}
30
31// char* specialization
32template <> TestLogger &TestLogger::operator<< <char *>(char *str) {
33 return *this << cpp::string_view(str);
34}
35
36// char specialization
37template <> TestLogger &TestLogger::operator<<(char ch) {
38 return *this << cpp::string_view(&ch, 1);
39}
40
41// bool specialization
42template <> TestLogger &TestLogger::operator<<(bool cond) {
43 return *this << (cond ? "true" : "false");
44}
45
46// void * specialization
47template <> TestLogger &TestLogger::operator<<(void *addr) {
48 return *this << "0x" << cpp::to_string(value: 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 specializations
64// char is already specialized to handle character
65template 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_INT128
77template TestLogger &TestLogger::operator<< <__uint128_t>(__uint128_t);
78#endif // LIBC_TYPES_HAS_INT128
79template 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 testing
89} // namespace LIBC_NAMESPACE
90

source code of libc/test/UnitTest/TestLogger.cpp