xref: /llvm-project/libc/test/UnitTest/TestLogger.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1f5dcab0dSGuillaume Chatelet #include "test/UnitTest/TestLogger.h"
2f5dcab0dSGuillaume Chatelet #include "src/__support/CPP/string.h"
3f5dcab0dSGuillaume Chatelet #include "src/__support/CPP/string_view.h"
4f5dcab0dSGuillaume Chatelet #include "src/__support/OSUtil/io.h"               // write_to_stderr
509efe848SGuillaume Chatelet #include "src/__support/big_int.h"                 // is_big_int
6*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
723c397c7SGuillaume Chatelet #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
809efe848SGuillaume Chatelet #include "src/__support/uint128.h"
9f5dcab0dSGuillaume Chatelet 
10dcf296b5SSiva Chandra Reddy #include <stdint.h>
11dcf296b5SSiva Chandra Reddy 
12*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
13f5dcab0dSGuillaume Chatelet namespace testing {
14f5dcab0dSGuillaume Chatelet 
15f5dcab0dSGuillaume Chatelet // cpp::string_view specialization
16f5dcab0dSGuillaume Chatelet template <>
17f5dcab0dSGuillaume Chatelet TestLogger &TestLogger::operator<< <cpp::string_view>(cpp::string_view str) {
18b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::write_to_stderr(str);
19f5dcab0dSGuillaume Chatelet   return *this;
20f5dcab0dSGuillaume Chatelet }
21f5dcab0dSGuillaume Chatelet 
22f5dcab0dSGuillaume Chatelet // cpp::string specialization
23f5dcab0dSGuillaume Chatelet template <> TestLogger &TestLogger::operator<< <cpp::string>(cpp::string str) {
24f5dcab0dSGuillaume Chatelet   return *this << static_cast<cpp::string_view>(str);
25f5dcab0dSGuillaume Chatelet }
26f5dcab0dSGuillaume Chatelet 
27f5dcab0dSGuillaume Chatelet // const char* specialization
28f5dcab0dSGuillaume Chatelet template <> TestLogger &TestLogger::operator<< <const char *>(const char *str) {
29f5dcab0dSGuillaume Chatelet   return *this << cpp::string_view(str);
30f5dcab0dSGuillaume Chatelet }
31f5dcab0dSGuillaume Chatelet 
32dcf296b5SSiva Chandra Reddy // char* specialization
33dcf296b5SSiva Chandra Reddy template <> TestLogger &TestLogger::operator<< <char *>(char *str) {
34dcf296b5SSiva Chandra Reddy   return *this << cpp::string_view(str);
35dcf296b5SSiva Chandra Reddy }
36dcf296b5SSiva Chandra Reddy 
37f5dcab0dSGuillaume Chatelet // char specialization
38f5dcab0dSGuillaume Chatelet template <> TestLogger &TestLogger::operator<<(char ch) {
39f5dcab0dSGuillaume Chatelet   return *this << cpp::string_view(&ch, 1);
40f5dcab0dSGuillaume Chatelet }
41f5dcab0dSGuillaume Chatelet 
429902fc8dSGuillaume Chatelet // bool specialization
439902fc8dSGuillaume Chatelet template <> TestLogger &TestLogger::operator<<(bool cond) {
449902fc8dSGuillaume Chatelet   return *this << (cond ? "true" : "false");
459902fc8dSGuillaume Chatelet }
469902fc8dSGuillaume Chatelet 
47dcf296b5SSiva Chandra Reddy // void * specialization
48dcf296b5SSiva Chandra Reddy template <> TestLogger &TestLogger::operator<<(void *addr) {
49dcf296b5SSiva Chandra Reddy   return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));
50dcf296b5SSiva Chandra Reddy }
51dcf296b5SSiva Chandra Reddy 
52f5dcab0dSGuillaume Chatelet template <typename T> TestLogger &TestLogger::operator<<(T t) {
536a8e6c9aSGuillaume Chatelet   if constexpr (is_big_int_v<T> ||
54245d669fSGuillaume Chatelet                 (cpp::is_integral_v<T> && cpp::is_unsigned_v<T> &&
55245d669fSGuillaume Chatelet                  (sizeof(T) > sizeof(uint64_t)))) {
569902fc8dSGuillaume Chatelet     static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
57b555912eSGuillaume Chatelet     const IntegerToString<T, radix::Hex::WithPrefix> buffer(t);
58b555912eSGuillaume Chatelet     return *this << buffer.view();
599902fc8dSGuillaume Chatelet   } else {
60f5dcab0dSGuillaume Chatelet     return *this << cpp::to_string(t);
61f5dcab0dSGuillaume Chatelet   }
629902fc8dSGuillaume Chatelet }
63f5dcab0dSGuillaume Chatelet 
64f5dcab0dSGuillaume Chatelet // is_integral specializations
659902fc8dSGuillaume Chatelet // char is already specialized to handle character
669902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <short>(short);
67f5dcab0dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <int>(int);
68bdb07c98SGuillaume Chatelet template TestLogger &TestLogger::operator<< <long>(long);
69bdb07c98SGuillaume Chatelet template TestLogger &TestLogger::operator<< <long long>(long long);
709902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <unsigned char>(unsigned char);
719902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <unsigned short>(unsigned short);
729902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <unsigned int>(unsigned int);
739902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <unsigned long>(unsigned long);
74f5dcab0dSGuillaume Chatelet template TestLogger &
75f5dcab0dSGuillaume Chatelet     TestLogger::operator<< <unsigned long long>(unsigned long long);
76f5dcab0dSGuillaume Chatelet 
7723c397c7SGuillaume Chatelet #ifdef LIBC_TYPES_HAS_INT128
789902fc8dSGuillaume Chatelet template TestLogger &TestLogger::operator<< <__uint128_t>(__uint128_t);
7923c397c7SGuillaume Chatelet #endif // LIBC_TYPES_HAS_INT128
806a8e6c9aSGuillaume Chatelet template TestLogger &TestLogger::operator<< <UInt<128>>(UInt<128>);
816a8e6c9aSGuillaume Chatelet template TestLogger &TestLogger::operator<< <UInt<192>>(UInt<192>);
826a8e6c9aSGuillaume Chatelet template TestLogger &TestLogger::operator<< <UInt<256>>(UInt<256>);
836a8e6c9aSGuillaume Chatelet template TestLogger &TestLogger::operator<< <UInt<320>>(UInt<320>);
849902fc8dSGuillaume Chatelet 
85f5dcab0dSGuillaume Chatelet // TODO: Add floating point formatting once it's supported by StringStream.
86f5dcab0dSGuillaume Chatelet 
87f5dcab0dSGuillaume Chatelet TestLogger tlog;
88f5dcab0dSGuillaume Chatelet 
89f5dcab0dSGuillaume Chatelet } // namespace testing
90*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
91