catch_tostring.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Created by Phil on 23/4/2014.
  3. * Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #if defined(__clang__)
  9. # pragma clang diagnostic push
  10. # pragma clang diagnostic ignored "-Wexit-time-destructors"
  11. # pragma clang diagnostic ignored "-Wglobal-constructors"
  12. #endif
  13. // Enable specific decls locally
  14. #if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER)
  15. #define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
  16. #endif
  17. #include "catch_tostring.h"
  18. #include "catch_interfaces_config.h"
  19. #include "catch_context.h"
  20. #include "catch_polyfills.hpp"
  21. #include <cmath>
  22. #include <iomanip>
  23. namespace Catch {
  24. namespace Detail {
  25. const std::string unprintableString = "{?}";
  26. namespace {
  27. const int hexThreshold = 255;
  28. struct Endianness {
  29. enum Arch { Big, Little };
  30. static Arch which() {
  31. int one = 1;
  32. // If the lowest byte we read is non-zero, we can assume
  33. // that little endian format is used.
  34. auto value = *reinterpret_cast<char*>(&one);
  35. return value ? Little : Big;
  36. }
  37. };
  38. }
  39. std::string rawMemoryToString( const void *object, std::size_t size ) {
  40. // Reverse order for little endian architectures
  41. int i = 0, end = static_cast<int>( size ), inc = 1;
  42. if( Endianness::which() == Endianness::Little ) {
  43. i = end-1;
  44. end = inc = -1;
  45. }
  46. unsigned char const *bytes = static_cast<unsigned char const *>(object);
  47. ReusableStringStream rss;
  48. rss << "0x" << std::setfill('0') << std::hex;
  49. for( ; i != end; i += inc )
  50. rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
  51. return rss.str();
  52. }
  53. }
  54. template<typename T>
  55. std::string fpToString( T value, int precision ) {
  56. if (Catch::isnan(value)) {
  57. return "nan";
  58. }
  59. ReusableStringStream rss;
  60. rss << std::setprecision( precision )
  61. << std::fixed
  62. << value;
  63. std::string d = rss.str();
  64. std::size_t i = d.find_last_not_of( '0' );
  65. if( i != std::string::npos && i != d.size()-1 ) {
  66. if( d[i] == '.' )
  67. i++;
  68. d = d.substr( 0, i+1 );
  69. }
  70. return d;
  71. }
  72. //// ======================================================= ////
  73. //
  74. // Out-of-line defs for full specialization of StringMaker
  75. //
  76. //// ======================================================= ////
  77. std::string StringMaker<std::string>::convert(const std::string& str) {
  78. if (!getCurrentContext().getConfig()->showInvisibles()) {
  79. return '"' + str + '"';
  80. }
  81. std::string s("\"");
  82. for (char c : str) {
  83. switch (c) {
  84. case '\n':
  85. s.append("\\n");
  86. break;
  87. case '\t':
  88. s.append("\\t");
  89. break;
  90. default:
  91. s.push_back(c);
  92. break;
  93. }
  94. }
  95. s.append("\"");
  96. return s;
  97. }
  98. #ifdef CATCH_CONFIG_CPP17_STRING_VIEW
  99. std::string StringMaker<std::string_view>::convert(std::string_view str) {
  100. return ::Catch::Detail::stringify(std::string{ str });
  101. }
  102. #endif
  103. std::string StringMaker<char const*>::convert(char const* str) {
  104. if (str) {
  105. return ::Catch::Detail::stringify(std::string{ str });
  106. } else {
  107. return{ "{null string}" };
  108. }
  109. }
  110. std::string StringMaker<char*>::convert(char* str) {
  111. if (str) {
  112. return ::Catch::Detail::stringify(std::string{ str });
  113. } else {
  114. return{ "{null string}" };
  115. }
  116. }
  117. #ifdef CATCH_CONFIG_WCHAR
  118. std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) {
  119. std::string s;
  120. s.reserve(wstr.size());
  121. for (auto c : wstr) {
  122. s += (c <= 0xff) ? static_cast<char>(c) : '?';
  123. }
  124. return ::Catch::Detail::stringify(s);
  125. }
  126. # ifdef CATCH_CONFIG_CPP17_STRING_VIEW
  127. std::string StringMaker<std::wstring_view>::convert(std::wstring_view str) {
  128. return StringMaker<std::wstring>::convert(std::wstring(str));
  129. }
  130. # endif
  131. std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) {
  132. if (str) {
  133. return ::Catch::Detail::stringify(std::wstring{ str });
  134. } else {
  135. return{ "{null string}" };
  136. }
  137. }
  138. std::string StringMaker<wchar_t *>::convert(wchar_t * str) {
  139. if (str) {
  140. return ::Catch::Detail::stringify(std::wstring{ str });
  141. } else {
  142. return{ "{null string}" };
  143. }
  144. }
  145. #endif
  146. #if defined(CATCH_CONFIG_CPP17_BYTE)
  147. #include <cstddef>
  148. std::string StringMaker<std::byte>::convert(std::byte value) {
  149. return ::Catch::Detail::stringify(std::to_integer<unsigned long long>(value));
  150. }
  151. #endif // defined(CATCH_CONFIG_CPP17_BYTE)
  152. std::string StringMaker<int>::convert(int value) {
  153. return ::Catch::Detail::stringify(static_cast<long long>(value));
  154. }
  155. std::string StringMaker<long>::convert(long value) {
  156. return ::Catch::Detail::stringify(static_cast<long long>(value));
  157. }
  158. std::string StringMaker<long long>::convert(long long value) {
  159. ReusableStringStream rss;
  160. rss << value;
  161. if (value > Detail::hexThreshold) {
  162. rss << " (0x" << std::hex << value << ')';
  163. }
  164. return rss.str();
  165. }
  166. std::string StringMaker<unsigned int>::convert(unsigned int value) {
  167. return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
  168. }
  169. std::string StringMaker<unsigned long>::convert(unsigned long value) {
  170. return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
  171. }
  172. std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
  173. ReusableStringStream rss;
  174. rss << value;
  175. if (value > Detail::hexThreshold) {
  176. rss << " (0x" << std::hex << value << ')';
  177. }
  178. return rss.str();
  179. }
  180. std::string StringMaker<bool>::convert(bool b) {
  181. return b ? "true" : "false";
  182. }
  183. std::string StringMaker<signed char>::convert(signed char value) {
  184. if (value == '\r') {
  185. return "'\\r'";
  186. } else if (value == '\f') {
  187. return "'\\f'";
  188. } else if (value == '\n') {
  189. return "'\\n'";
  190. } else if (value == '\t') {
  191. return "'\\t'";
  192. } else if ('\0' <= value && value < ' ') {
  193. return ::Catch::Detail::stringify(static_cast<unsigned int>(value));
  194. } else {
  195. char chstr[] = "' '";
  196. chstr[1] = value;
  197. return chstr;
  198. }
  199. }
  200. std::string StringMaker<char>::convert(char c) {
  201. return ::Catch::Detail::stringify(static_cast<signed char>(c));
  202. }
  203. std::string StringMaker<unsigned char>::convert(unsigned char c) {
  204. return ::Catch::Detail::stringify(static_cast<char>(c));
  205. }
  206. std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
  207. return "nullptr";
  208. }
  209. int StringMaker<float>::precision = 5;
  210. std::string StringMaker<float>::convert(float value) {
  211. return fpToString(value, precision) + 'f';
  212. }
  213. int StringMaker<double>::precision = 10;
  214. std::string StringMaker<double>::convert(double value) {
  215. return fpToString(value, precision);
  216. }
  217. std::string ratio_string<std::atto>::symbol() { return "a"; }
  218. std::string ratio_string<std::femto>::symbol() { return "f"; }
  219. std::string ratio_string<std::pico>::symbol() { return "p"; }
  220. std::string ratio_string<std::nano>::symbol() { return "n"; }
  221. std::string ratio_string<std::micro>::symbol() { return "u"; }
  222. std::string ratio_string<std::milli>::symbol() { return "m"; }
  223. } // end namespace Catch
  224. #if defined(__clang__)
  225. # pragma clang diagnostic pop
  226. #endif