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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_tostring.h"
|
|
|
|
#include "catch_interfaces_config.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rawMemoryToString( const void *object, std::size_t size )
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "0x" << std::setfill('0') << std::hex;
|
|
|
|
for( ; i != end; i += inc )
|
|
|
|
os << std::setw(2) << static_cast<unsigned>(bytes[i]);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
std::string toString( std::string const& value ) {
|
|
|
|
std::string s = value;
|
|
|
|
if( getCurrentContext().getConfig()->showInvisibles() ) {
|
|
|
|
for(size_t i = 0; i < s.size(); ++i ) {
|
|
|
|
std::string subs;
|
|
|
|
switch( s[i] ) {
|
|
|
|
case '\n': subs = "\\n"; break;
|
|
|
|
case '\t': subs = "\\t"; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if( !subs.empty() ) {
|
|
|
|
s = s.substr( 0, i ) + subs + s.substr( i+1 );
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "\"" + s + "\"";
|
|
|
|
}
|
|
|
|
std::string toString( std::wstring const& value ) {
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
s.reserve( value.size() );
|
|
|
|
for(size_t i = 0; i < value.size(); ++i )
|
|
|
|
s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?';
|
2014-12-09 19:49:58 +01:00
|
|
|
return Catch::toString( s );
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( const char* const value ) {
|
|
|
|
return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( char* const value ) {
|
|
|
|
return Catch::toString( static_cast<const char*>( value ) );
|
|
|
|
}
|
|
|
|
|
2014-08-14 13:28:23 +02:00
|
|
|
std::string toString( const wchar_t* const value )
|
|
|
|
{
|
|
|
|
return value ? Catch::toString( std::wstring(value) ) : std::string( "{null string}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( wchar_t* const value )
|
|
|
|
{
|
|
|
|
return Catch::toString( static_cast<const wchar_t*>( value ) );
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
std::string toString( int value ) {
|
|
|
|
std::ostringstream oss;
|
2015-05-20 19:28:22 +02:00
|
|
|
oss << value;
|
2015-07-23 20:03:33 +02:00
|
|
|
if( value > Detail::hexThreshold )
|
2017-01-15 09:41:33 +01:00
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2014-04-23 08:03:15 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( unsigned long value ) {
|
|
|
|
std::ostringstream oss;
|
2015-05-20 19:28:22 +02:00
|
|
|
oss << value;
|
2015-07-23 20:03:33 +02:00
|
|
|
if( value > Detail::hexThreshold )
|
2017-01-15 09:41:33 +01:00
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2014-04-23 08:03:15 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( unsigned int value ) {
|
2014-12-09 19:49:58 +01:00
|
|
|
return Catch::toString( static_cast<unsigned long>( value ) );
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:16:40 +02:00
|
|
|
template<typename T>
|
|
|
|
std::string fpToString( T value, int precision ) {
|
2014-04-23 08:03:15 +02:00
|
|
|
std::ostringstream oss;
|
2014-07-09 19:16:40 +02:00
|
|
|
oss << std::setprecision( precision )
|
2014-04-23 08:03:15 +02:00
|
|
|
<< std::fixed
|
|
|
|
<< value;
|
|
|
|
std::string d = oss.str();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:16:40 +02:00
|
|
|
std::string toString( const double value ) {
|
|
|
|
return fpToString( value, 10 );
|
|
|
|
}
|
|
|
|
std::string toString( const float value ) {
|
|
|
|
return fpToString( value, 5 ) + "f";
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
std::string toString( bool value ) {
|
|
|
|
return value ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( char value ) {
|
|
|
|
return value < ' '
|
|
|
|
? toString( static_cast<unsigned int>( value ) )
|
|
|
|
: Detail::makeString( value );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( signed char value ) {
|
|
|
|
return toString( static_cast<char>( value ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString( unsigned char value ) {
|
|
|
|
return toString( static_cast<char>( value ) );
|
|
|
|
}
|
|
|
|
|
2015-07-23 20:03:33 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_LONG_LONG
|
|
|
|
std::string toString( long long value ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << value;
|
|
|
|
if( value > Detail::hexThreshold )
|
2017-01-15 09:41:33 +01:00
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2015-07-23 20:03:33 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
std::string toString( unsigned long long value ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << value;
|
|
|
|
if( value > Detail::hexThreshold )
|
2017-01-15 09:41:33 +01:00
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2015-07-23 20:03:33 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
|
|
|
std::string toString( std::nullptr_t ) {
|
|
|
|
return "nullptr";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __OBJC__
|
|
|
|
std::string toString( NSString const * const& nsstring ) {
|
|
|
|
if( !nsstring )
|
|
|
|
return "nil";
|
2014-06-20 19:05:31 +02:00
|
|
|
return "@" + toString([nsstring UTF8String]);
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) {
|
|
|
|
if( !nsstring )
|
|
|
|
return "nil";
|
2014-06-20 19:05:31 +02:00
|
|
|
return "@" + toString([nsstring UTF8String]);
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
std::string toString( NSObject* const& nsObject ) {
|
|
|
|
return toString( [nsObject description] );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|