Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPhil Nash2011-03-18 14:39:58 +0000
committerPhil Nash2011-03-18 14:39:58 +0000
commitef611c65d9d5e9144e2b4d09e40ff9f2edd656d0 (patch)
tree878b9131c342b70fe4af3cf1f78a1c538ca1b35b
parent1e15669f2a84ecf47f76ea54610d5f362c072c2b (diff)
downloadcatch3-ef611c65d9d5e9144e2b4d09e40ff9f2edd656d0.tar.gz
Pointer comparisons and compare to NULL
-rw-r--r--Test/ConditionTests.cpp13
-rw-r--r--internal/catch_capture.hpp124
-rw-r--r--internal/catch_common.h3
3 files changed, 137 insertions, 3 deletions
diff --git a/Test/ConditionTests.cpp b/Test/ConditionTests.cpp
index 136fe81f..5a2c5751 100644
--- a/Test/ConditionTests.cpp
+++ b/Test/ConditionTests.cpp
@@ -214,6 +214,19 @@ TEST_CASE( "./failing/conditions/unsigned-negative", "Comparisons between negati
CHECK( negative == ui );
}
+TEST_CASE( "./succeeding/conditions/ptr", "Pointers can be compared to null" )
+{
+ TestData* p = NULL;
+ TestData* pNULL = NULL;
+
+ REQUIRE( p == NULL );
+ REQUIRE( p == pNULL );
+
+ TestData data;
+ p = &data;
+ REQUIRE( p != NULL );
+}
+
// Not (!) tests
// The problem with the ! operator is that it has right-to-left associativity.
// This means we can't isolate it when we decompose. The simple REQUIRE( !false ) form, therefore,
diff --git a/internal/catch_capture.hpp b/internal/catch_capture.hpp
index 0ba08d21..69753f7a 100644
--- a/internal/catch_capture.hpp
+++ b/internal/catch_capture.hpp
@@ -154,6 +154,39 @@ inline std::string toString
{
return value ? "true" : "false";
}
+
+///////////////////////////////////////////////////////////////////////////////
+inline std::string toString
+(
+ void* p
+)
+{
+ if( !p )
+ return INTERNAL_CATCH_STRINGIFY( NULL );
+ std::ostringstream oss;
+ oss << p;
+ return oss.str();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+template<typename T>
+inline std::string toString
+(
+ T* p
+)
+{
+ return toString( static_cast<void*>( p ) );
+}
+
+///////////////////////////////////////////////////////////////////////////////
+template<typename T>
+inline std::string toString
+(
+ const T* p
+)
+{
+ return toString( static_cast<void*>( const_cast<T*>( p ) ) );
+}
struct TestFailureException
{
@@ -226,8 +259,9 @@ public:
private:
friend class ResultBuilder;
- template<typename T>
- friend class Expression;
+ template<typename T> friend class Expression;
+
+ template<typename T> friend class PtrExpression;
///////////////////////////////////////////////////////////////////////////
MutableResultInfo& captureBoolExpression
@@ -361,6 +395,66 @@ private:
const T& m_lhs;
};
+ template<typename LhsT>
+ class PtrExpression
+ {
+ public:
+
+ ///////////////////////////////////////////////////////////////////////////
+ PtrExpression
+ (
+ MutableResultInfo& result,
+ const LhsT* lhs
+ )
+ : m_result( result ),
+ m_lhs( lhs )
+ {}
+
+ ///////////////////////////////////////////////////////////////////////////
+ template<typename RhsT>
+ MutableResultInfo& operator ==
+ (
+ const RhsT* rhs
+ )
+ {
+ return m_result.captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // This catches NULL
+ MutableResultInfo& operator ==
+ (
+ LhsT* rhs
+ )
+ {
+ return m_result.captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template<typename RhsT>
+ MutableResultInfo& operator !=
+ (
+ const RhsT* rhs
+ )
+ {
+ return m_result.captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // This catches NULL
+ MutableResultInfo& operator !=
+ (
+ LhsT* rhs
+ )
+ {
+ return m_result.captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
+ }
+
+ private:
+ MutableResultInfo& m_result;
+ const LhsT* m_lhs;
+ };
+
class ResultBuilder
{
public:
@@ -391,6 +485,30 @@ public:
///////////////////////////////////////////////////////////////////////////
template<typename T>
+ PtrExpression<T> operator->*
+ (
+ const T* operand
+ )
+ {
+ PtrExpression<T> expr( m_result, operand );
+
+ return expr;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template<typename T>
+ PtrExpression<T> operator->*
+ (
+ T* operand
+ )
+ {
+ PtrExpression<T> expr( m_result, operand );
+
+ return expr;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ template<typename T>
ResultBuilder& operator <<
(
const T & value
@@ -478,7 +596,7 @@ class Approx
public:
///////////////////////////////////////////////////////////////////////////
// !TBD more generic
- Approx
+ explicit Approx
(
double d
)
diff --git a/internal/catch_common.h b/internal/catch_common.h
index 3d010a13..d0181d19 100644
--- a/internal/catch_common.h
+++ b/internal/catch_common.h
@@ -17,6 +17,9 @@
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
#define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
+#define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
+#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
+
#ifdef __GNUC__
#define ATTRIBUTE_NORETURN __attribute__ ((noreturn))
#else