From e1cb8f25f2c1da70f1a99ec842e20d83ea4340ff Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Mon, 7 May 2012 19:45:55 +0100 Subject: [PATCH] Fix for comparing (const) char* to NULL --- include/internal/catch_evaluate.hpp | 16 +++++++++++++--- projects/SelfTest/ConditionTests.cpp | 6 ++++++ projects/SelfTest/TestMain.cpp | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/internal/catch_evaluate.hpp b/include/internal/catch_evaluate.hpp index 90acad26..38e14f1a 100644 --- a/include/internal/catch_evaluate.hpp +++ b/include/internal/catch_evaluate.hpp @@ -174,16 +174,26 @@ namespace Internal template bool compare( long lhs, const T* rhs ) { - return Evaluator::evaluate( reinterpret_cast( NULL ), rhs ); - + return Evaluator::evaluate( reinterpret_cast( lhs ), rhs ); } template bool compare( long lhs, T* rhs ) { return Evaluator::evaluate( reinterpret_cast( lhs ), rhs ); - } + + template + bool compare( const T* lhs, long rhs ) + { + return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); + } + + template + bool compare( T* lhs, long rhs ) + { + return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); + } } // end of namespace Internal } // end of namespace Catch diff --git a/projects/SelfTest/ConditionTests.cpp b/projects/SelfTest/ConditionTests.cpp index f14da68d..e9a8f685 100644 --- a/projects/SelfTest/ConditionTests.cpp +++ b/projects/SelfTest/ConditionTests.cpp @@ -221,6 +221,9 @@ TEST_CASE( "succeeding/conditions/negative ints", } #endif +inline const char* returnsConstNull(){ return NULL; } +inline char* returnsNull(){ return NULL; } + TEST_CASE( "./succeeding/conditions/ptr", "Pointers can be compared to null" ) { @@ -241,6 +244,9 @@ TEST_CASE( "./succeeding/conditions/ptr", const TestData* const cpc = p; REQUIRE( cpc != NULL ); + REQUIRE( returnsNull() == NULL ); + REQUIRE( returnsConstNull() == NULL ); + // REQUIRE( NULL != p ); // gives warning, but should compile and run ok } diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index a95402dc..48932dca 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -43,7 +43,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" "Number of 'succeeding' tests is fixed" ) { runner.runMatching( "./succeeding/*" ); - CHECK( runner.getTotals().assertions.passed == 273 ); + CHECK( runner.getTotals().assertions.passed == 275 ); CHECK( runner.getTotals().assertions.failed == 0 ); }