Fixes toString() for function pointers and added support for member function pointers.

- thanks to Malcolm Noyes for supplying sample code on which the solution here was based
This commit is contained in:
Phil Nash 2014-01-07 17:25:27 +00:00
parent 440a47011f
commit e091018514
7 changed files with 82 additions and 12 deletions

View File

@ -79,6 +79,25 @@ namespace Detail {
} }
}; };
// For display purposes only.
// Does not consider endian-ness
template<typename T>
std::string rawMemoryToString( T value ) {
union
{
T value;
unsigned char bytes[sizeof(T)];
} valueAsBuffer;
valueAsBuffer.value = value;
std::ostringstream oss;
oss << "0x";
for( unsigned char* cp = valueAsBuffer.bytes; cp < valueAsBuffer.bytes+sizeof(T); ++cp )
oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)*cp;
return oss.str();
}
} // end namespace Detail } // end namespace Detail
template<typename T> template<typename T>
@ -94,9 +113,18 @@ struct StringMaker<T*> {
static std::string convert( U* p ) { static std::string convert( U* p ) {
if( !p ) if( !p )
return INTERNAL_CATCH_STRINGIFY( NULL ); return INTERNAL_CATCH_STRINGIFY( NULL );
std::ostringstream oss; else
oss << "0x" << std::hex << reinterpret_cast<unsigned long>( p ); return Detail::rawMemoryToString( p );
return oss.str(); }
};
template<typename R, typename C>
struct StringMaker<R C::*> {
static std::string convert( R C::* p ) {
if( !p )
return INTERNAL_CATCH_STRINGIFY( NULL );
else
return Detail::rawMemoryToString( p );
} }
}; };

View File

@ -750,5 +750,5 @@ with expansion:
"first" == "second" "first" == "second"
=============================================================================== ===============================================================================
122 test cases - 36 failed (675 assertions - 91 failed) 123 test cases - 36 failed (676 assertions - 91 failed)

View File

@ -6366,7 +6366,21 @@ TrickyTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( a == &foo ) REQUIRE( a == &foo )
with expansion: with expansion:
1 == 1 0x<hex digits> == 0x<hex digits>
-------------------------------------------------------------------------------
Comparing member function pointers
-------------------------------------------------------------------------------
TrickyTests.cpp:<line number>
...............................................................................
TrickyTests.cpp:<line number>:
PASSED:
CHECK( m == &S::f )
with expansion:
0x<hex digits>
==
0x<hex digits>
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
pointer to class pointer to class
@ -7144,5 +7158,5 @@ with expansion:
true true
=============================================================================== ===============================================================================
122 test cases - 51 failed (694 assertions - 110 failed) 123 test cases - 51 failed (695 assertions - 110 failed)

View File

@ -1,5 +1,5 @@
<testsuites> <testsuites>
<testsuite name="~_" errors="10" failures="100" tests="694" hostname="tbd" time="{duration}" timestamp="tbd"> <testsuite name="~_" errors="10" failures="100" tests="695" hostname="tbd" time="{duration}" timestamp="tbd">
<testcase classname="global" name="Some simple comparisons between doubles" time="{duration}"/> <testcase classname="global" name="Some simple comparisons between doubles" time="{duration}"/>
<testcase classname="global" name="Approximate comparisons with different epsilons" time="{duration}"/> <testcase classname="global" name="Approximate comparisons with different epsilons" time="{duration}"/>
<testcase classname="global" name="Approximate comparisons with floats" time="{duration}"/> <testcase classname="global" name="Approximate comparisons with floats" time="{duration}"/>
@ -514,6 +514,7 @@ TrickyTests.cpp:<line number>
<testcase classname="Assertions then sections" name="A section/Another other section" time="{duration}"/> <testcase classname="Assertions then sections" name="A section/Another other section" time="{duration}"/>
<testcase classname="global" name="non streamable - with conv. op" time="{duration}"/> <testcase classname="global" name="non streamable - with conv. op" time="{duration}"/>
<testcase classname="global" name="Comparing function pointers" time="{duration}"/> <testcase classname="global" name="Comparing function pointers" time="{duration}"/>
<testcase classname="global" name="Comparing member function pointers" time="{duration}"/>
<testcase classname="global" name="pointer to class" time="{duration}"/> <testcase classname="global" name="pointer to class" time="{duration}"/>
<testcase classname="global" name="X/level/0/a" time="{duration}"/> <testcase classname="global" name="X/level/0/a" time="{duration}"/>
<testcase classname="global" name="X/level/0/b" time="{duration}"/> <testcase classname="global" name="X/level/0/b" time="{duration}"/>

View File

@ -6637,7 +6637,20 @@ there&quot;
a == &amp;foo a == &amp;foo
</Original> </Original>
<Expanded> <Expanded>
1 == 1 0x<hex digits> == 0x<hex digits>
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Comparing member function pointers">
<Expression success="true" filename="/Users/philnash/Dev/OSS/Catch/projects/SelfTest/TrickyTests.cpp" >
<Original>
m == &amp;S::f
</Original>
<Expanded>
0x<hex digits>
==
0x<hex digits>
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
@ -7405,7 +7418,7 @@ there&quot;
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="584" failures="110"/> <OverallResults successes="585" failures="110"/>
</Group> </Group>
<OverallResults successes="584" failures="110"/> <OverallResults successes="585" failures="110"/>
</Catch> </Catch>

View File

@ -356,6 +356,20 @@ TEST_CASE( "Comparing function pointers", "[Tricky][function pointer]" )
REQUIRE( a == &foo ); REQUIRE( a == &foo );
} }
struct S
{
void f() {}
};
TEST_CASE( "Comparing member function pointers", "[Tricky][member function pointer]" )
{
typedef void (S::*MF)();
MF m = &S::f;
CHECK( m == &S::f );
}
class ClassName {}; class ClassName {};
TEST_CASE( "pointer to class", "[Tricky]" ) TEST_CASE( "pointer to class", "[Tricky]" )

View File

@ -581,7 +581,7 @@
GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES; GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7; MACOSX_DEPLOYMENT_TARGET = "";
ONLY_ACTIVE_ARCH = YES; ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx; SDKROOT = macosx;
USER_HEADER_SEARCH_PATHS = "\"$(PROJECT_DIR)/../../../include\""; USER_HEADER_SEARCH_PATHS = "\"$(PROJECT_DIR)/../../../include\"";
@ -624,7 +624,7 @@
GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES; GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7; MACOSX_DEPLOYMENT_TARGET = "";
SDKROOT = macosx; SDKROOT = macosx;
USER_HEADER_SEARCH_PATHS = "\"$(PROJECT_DIR)/../../../include\""; USER_HEADER_SEARCH_PATHS = "\"$(PROJECT_DIR)/../../../include\"";
}; };