Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Pointer comparisons and compare to NULL

Commit
ef611c65d9d5e9144e2b4d09e40ff9f2edd656d0
Author
Phil Nash ·
Parents
1e15669f2a84

Files · Download .tar.gz

Changes

 Test/ConditionTests.cpp    |  13 +++++
 internal/catch_capture.hpp | 124 +++++++++++++++++++++++++++++++++++++++++++--
 internal/catch_common.h    |   3 ++
 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:
@@ -389,6 +483,30 @@ public:
         return expr;
     }
 
+    ///////////////////////////////////////////////////////////////////////////
+    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 <<
@@ -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