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

Tweaked rawMemoryToString() along lines of suggestion in #281

Commit
7cbf74061b5f5ec27b1959c55dfd59b15bafec7f
Author
Phil Nash ·
Parents
48fac9cf01fe

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.h   | 36 +++---------------------------------
 include/internal/catch_tostring.hpp | 36 ++++++++++++++++++++++++++++++++++++
 projects/SelfTest/MessageTests.cpp  |  7 +++++++
 3 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 25faa2a3..7268d156 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -80,41 +80,11 @@ namespace Detail {
         }
     };
 
-    struct Endianness {
-        enum Arch { Big, Little };
+    std::string rawMemoryToString( const void *object, std::size_t size );
 
-        static Arch which() {
-            union _{
-                int asInt;
-                char asChar[sizeof (int)];
-            } u;
-
-            u.asInt = 1;
-            return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
-        }
-    };
-
-    // Writes the raw memory into a string, considering endianness
     template<typename T>
-    std::string rawMemoryToString( T value ) {
-        union _ {
-            T typedValue;
-            unsigned char bytes[sizeof(T)];
-        } u;
-
-        u.typedValue = value;
-
-        std::ostringstream oss;
-        oss << "0x";
-
-        int i = 0, end = sizeof(T), inc = 1;
-        if( Endianness::which() == Endianness::Little ) {
-            i = end-1;
-            end = inc = -1;
-        }
-        for( ; i != end; i += inc )
-            oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)u.bytes[i];
-        return oss.str();
+    inline std::string rawMemoryToString( const T& object ) {
+      return rawMemoryToString( &object, sizeof(object) );
     }
 
 } // end namespace Detail
diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp
index 2eb94e63..0b35f9d6 100644
--- a/include/internal/catch_tostring.hpp
+++ b/include/internal/catch_tostring.hpp
@@ -13,6 +13,42 @@
 
 namespace Catch {
 
+namespace Detail {
+
+    namespace {
+        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();
+    }
+}
+
 std::string toString( std::string const& value ) {
     std::string s = value;
     if( getCurrentContext().getConfig()->showInvisibles() ) {
diff --git a/projects/SelfTest/MessageTests.cpp b/projects/SelfTest/MessageTests.cpp
index 8bcfe009..b6da5fee 100644
--- a/projects/SelfTest/MessageTests.cpp
+++ b/projects/SelfTest/MessageTests.cpp
@@ -123,3 +123,10 @@ TEST_CASE( "sends information to INFO", "[.][failing]" )
     CAPTURE( i );
     REQUIRE( false );
 }
+
+TEST_CASE( "Pointers can be converted to strings", "[messages][.]" )
+{
+    int p;
+    WARN( "actual address of p: " << &p );
+    WARN( "toString(p): " << Catch::toString( &p ) );
+}