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

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