2014-04-23 08:03:15 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 23/4/2014.
|
|
|
|
* Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wexit-time-destructors"
|
|
|
|
# pragma clang diagnostic ignored "-Wglobal-constructors"
|
|
|
|
#endif
|
|
|
|
|
2018-06-12 16:37:06 +02:00
|
|
|
// Enable specific decls locally
|
|
|
|
#if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER)
|
|
|
|
#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
#include "catch_tostring.h"
|
|
|
|
#include "catch_interfaces_config.h"
|
2017-07-06 22:28:42 +02:00
|
|
|
#include "catch_context.h"
|
2018-11-17 20:52:18 +01:00
|
|
|
#include "catch_polyfills.hpp"
|
2014-04-23 08:03:15 +02:00
|
|
|
|
2017-11-10 21:43:23 +01:00
|
|
|
#include <cmath>
|
2017-07-19 09:31:10 +02:00
|
|
|
#include <iomanip>
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
namespace Detail {
|
|
|
|
|
2015-07-23 20:03:33 +02:00
|
|
|
const std::string unprintableString = "{?}";
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
namespace {
|
2015-07-23 20:03:33 +02:00
|
|
|
const int hexThreshold = 255;
|
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
struct Endianness {
|
|
|
|
enum Arch { Big, Little };
|
|
|
|
|
|
|
|
static Arch which() {
|
|
|
|
union _{
|
|
|
|
int asInt;
|
|
|
|
char asChar[sizeof (int)];
|
|
|
|
} u;
|
|
|
|
|
|
|
|
u.asInt = 1;
|
|
|
|
return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string rawMemoryToString( const void *object, std::size_t size ) {
|
2014-05-23 19:41:02 +02:00
|
|
|
// Reverse order for little endian architectures
|
|
|
|
int i = 0, end = static_cast<int>( size ), inc = 1;
|
|
|
|
if( Endianness::which() == Endianness::Little ) {
|
|
|
|
i = end-1;
|
|
|
|
end = inc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const *bytes = static_cast<unsigned char const *>(object);
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << "0x" << std::setfill('0') << std::hex;
|
2014-05-23 19:41:02 +02:00
|
|
|
for( ; i != end; i += inc )
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
|
|
|
|
return rss.str();
|
2014-05-23 19:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string fpToString( T value, int precision ) {
|
2018-11-17 20:52:18 +01:00
|
|
|
if (Catch::isnan(value)) {
|
2017-11-10 21:43:23 +01:00
|
|
|
return "nan";
|
|
|
|
}
|
|
|
|
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << std::setprecision( precision )
|
2017-05-02 23:51:03 +02:00
|
|
|
<< std::fixed
|
|
|
|
<< value;
|
2017-11-07 19:01:10 +01:00
|
|
|
std::string d = rss.str();
|
2017-05-02 23:51:03 +02:00
|
|
|
std::size_t i = d.find_last_not_of( '0' );
|
|
|
|
if( i != std::string::npos && i != d.size()-1 ) {
|
|
|
|
if( d[i] == '.' )
|
|
|
|
i++;
|
|
|
|
d = d.substr( 0, i+1 );
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//// ======================================================= ////
|
|
|
|
//
|
|
|
|
// Out-of-line defs for full specialization of StringMaker
|
|
|
|
//
|
|
|
|
//// ======================================================= ////
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<std::string>::convert(const std::string& str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (!getCurrentContext().getConfig()->showInvisibles()) {
|
|
|
|
return '"' + str + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string s("\"");
|
|
|
|
for (char c : str) {
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
s.append("\\n");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
s.append("\\t");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.push_back(c);
|
|
|
|
break;
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
s.append("\"");
|
|
|
|
return s;
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 17:01:26 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP17_STRING_VIEW
|
|
|
|
std::string StringMaker<std::string_view>::convert(std::string_view str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2018-03-01 18:41:17 +01:00
|
|
|
#endif
|
2014-04-23 08:03:15 +02:00
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<char const*>::convert(char const* str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<char*>::convert(char* str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2018-09-04 17:01:26 +02:00
|
|
|
|
2018-03-01 18:41:17 +01:00
|
|
|
#ifdef CATCH_CONFIG_WCHAR
|
2018-09-04 17:01:26 +02:00
|
|
|
std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) {
|
|
|
|
std::string s;
|
|
|
|
s.reserve(wstr.size());
|
|
|
|
for (auto c : wstr) {
|
|
|
|
s += (c <= 0xff) ? static_cast<char>(c) : '?';
|
|
|
|
}
|
|
|
|
return ::Catch::Detail::stringify(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef CATCH_CONFIG_CPP17_STRING_VIEW
|
|
|
|
std::string StringMaker<std::wstring_view>::convert(std::wstring_view str) {
|
|
|
|
return StringMaker<std::wstring>::convert(std::wstring(str));
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::wstring{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-08-14 13:28:23 +02:00
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<wchar_t *>::convert(wchar_t * str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::wstring{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-08-14 13:28:23 +02:00
|
|
|
}
|
2018-03-01 18:41:17 +01:00
|
|
|
#endif
|
2014-08-14 13:28:23 +02:00
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<int>::convert(int value) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(static_cast<long long>(value));
|
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<long>::convert(long value) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(static_cast<long long>(value));
|
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<long long>::convert(long long value) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << value;
|
2017-05-02 23:51:03 +02:00
|
|
|
if (value > Detail::hexThreshold) {
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << " (0x" << std::hex << value << ')';
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2017-11-07 19:01:10 +01:00
|
|
|
return rss.str();
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<unsigned int>::convert(unsigned int value) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<unsigned long>::convert(unsigned long value) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
|
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << value;
|
2017-05-02 23:51:03 +02:00
|
|
|
if (value > Detail::hexThreshold) {
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << " (0x" << std::hex << value << ')';
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-11-07 19:01:10 +01:00
|
|
|
return rss.str();
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:16:40 +02:00
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<bool>::convert(bool b) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return b ? "true" : "false";
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 10:22:16 +02:00
|
|
|
std::string StringMaker<signed char>::convert(signed char value) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (value == '\r') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\r'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\f') {
|
2017-02-01 21:31:14 +01:00
|
|
|
return "'\\f'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\n') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\n'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\t') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\t'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if ('\0' <= value && value < ' ') {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned int>(value));
|
|
|
|
} else {
|
|
|
|
char chstr[] = "' '";
|
|
|
|
chstr[1] = value;
|
|
|
|
return chstr;
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2018-10-03 10:22:16 +02:00
|
|
|
std::string StringMaker<char>::convert(char c) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<signed char>(c));
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<unsigned char>::convert(unsigned char c) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(static_cast<char>(c));
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return "nullptr";
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:14:33 +02:00
|
|
|
int StringMaker<float>::m_precision = 5;
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<float>::convert(float value) {
|
2019-04-26 17:14:33 +02:00
|
|
|
return fpToString(value, m_precision) + 'f';
|
2015-07-23 20:03:33 +02:00
|
|
|
}
|
2019-04-26 17:14:33 +02:00
|
|
|
|
|
|
|
void StringMaker<float>::setPrecision(int precision) {
|
|
|
|
m_precision = precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StringMaker<float>::getPrecision() {
|
|
|
|
return m_precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StringMaker<double>::m_precision = 10;
|
|
|
|
|
2017-05-21 23:40:05 +02:00
|
|
|
std::string StringMaker<double>::convert(double value) {
|
2019-04-26 17:14:33 +02:00
|
|
|
return fpToString(value, m_precision);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringMaker<double>::setPrecision(int precision) {
|
|
|
|
m_precision = precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StringMaker<double>::getPrecision() {
|
|
|
|
return m_precision;
|
2015-07-23 20:03:33 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-19 14:54:52 +01:00
|
|
|
std::string ratio_string<std::atto>::symbol() { return "a"; }
|
|
|
|
std::string ratio_string<std::femto>::symbol() { return "f"; }
|
2018-07-03 09:25:34 +02:00
|
|
|
std::string ratio_string<std::pico>::symbol() { return "p"; }
|
|
|
|
std::string ratio_string<std::nano>::symbol() { return "n"; }
|
2017-11-19 14:54:52 +01:00
|
|
|
std::string ratio_string<std::micro>::symbol() { return "u"; }
|
|
|
|
std::string ratio_string<std::milli>::symbol() { return "m"; }
|
2014-04-23 08:03:15 +02:00
|
|
|
|
|
|
|
} // end namespace Catch
|
2017-09-07 16:51:33 +02:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|