From 46b4909c0868ae7ba6ad89a0cc02df73c6761c5f Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Wed, 6 Apr 2011 22:26:16 +0100 Subject: [PATCH] Added Malcolm Noyes' test cases --- Test/TestMain.cpp | 2 +- Test/TrickyTests.cpp | 108 +++++++++++++++++++++++++++++++++++++ internal/catch_capture.hpp | 42 +++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) diff --git a/Test/TestMain.cpp b/Test/TestMain.cpp index da22e917..1771757c 100644 --- a/Test/TestMain.cpp +++ b/Test/TestMain.cpp @@ -50,7 +50,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.getSuccessCount() == 218 ); + CHECK( runner.getSuccessCount() == 224 ); CHECK( runner.getFailureCount() == 0 ); } diff --git a/Test/TrickyTests.cpp b/Test/TrickyTests.cpp index 6a2f69bc..1fc50876 100644 --- a/Test/TrickyTests.cpp +++ b/Test/TrickyTests.cpp @@ -120,3 +120,111 @@ TEST_CASE REQUIRE( i++ == 8 ); } + +namespace A { + struct X + { + X() : a(4), b(2), c(7) {} + X(int v) : a(v), b(2), c(7) {} + int a; + int b; + int c; + }; +} + +namespace B { + struct Y + { + Y() : a(4), b(2), c(7) {} + Y(int v) : a(v), b(2), c(7) {} + int a; + int b; + int c; + }; +} +bool operator==(const A::X& lhs, const B::Y& rhs) +{ + return (lhs.a == rhs.a); +} + +bool operator==(const B::Y& lhs, const A::X& rhs) +{ + return (lhs.a == rhs.a); +} + +/////////////////////////////////////////////////////////////////////////////// +TEST_CASE +( + "./succeeding/koenig", + "Operators at different namespace levels not hijacked by Koenig lookup" +) +{ + A::X x; + B::Y y; + REQUIRE( x == y ); +} + + +namespace ObjectWithConversions +{ + struct Object + { + operator unsigned int() {return 0xc0000000;} + }; + + /////////////////////////////////////////////////////////////////////////////// + TEST_CASE + ( + "./succeeding/koenig", + "Operators at different namespace levels not hijacked by Koenig lookup" + ) + { + + Object o; + + bool ok = (0xc0000000 == o); // ok + REQUIRE(ok); + REQUIRE(0xc0000000 == o ); // doesn't compile (VC or GCC) + } +} + +namespace ObjectWithNonConstEqualityOperator +{ + struct Test + { + Test( unsigned int v ) + : m_value(v) + {} + + bool operator==( const Test&rhs ) + { + return (m_value == rhs.m_value); + } + bool operator==( const Test&rhs ) const + { + return (m_value != rhs.m_value); + } + unsigned int m_value; + }; + + TEST_CASE("./succeeding/non-const==", "Demonstrate that a non-const == is not used") + { + Test t( 1 ); + + bool ok = (t == 1); // ok + REQUIRE(ok); + REQUIRE( t == 1 ); // doesn't compile (VC or GCC) + } +} + +namespace EnumBitFieldTests +{ + enum Bits {bit0 = 0x0001, bit1 = 0x0002, bit2 = 0x0004, bit3 = 0x0008, bit1and2 = 0x0006, + bit30 = 0x40000000, bit31 = 0x80000000, + bit30and31 = 0xc0000000}; + + TEST_CASE("./succeeding/enum/bits", "Test enum bit values") + { + REQUIRE( 0xc0000000 == bit30and31 ); + } +} diff --git a/internal/catch_capture.hpp b/internal/catch_capture.hpp index 539129d6..34728c76 100644 --- a/internal/catch_capture.hpp +++ b/internal/catch_capture.hpp @@ -107,6 +107,20 @@ inline std::string toString return "\"" + value + "\""; } +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + const std::wstring& value +) +{ + std::ostringstream oss; + oss << "\""; + for(size_t i = 0; i < value.size(); ++i ) + oss << static_cast( value[i] <= 0xff ? value[i] : '?'); + oss << "\""; + return oss.str(); +} + /////////////////////////////////////////////////////////////////////////////// inline std::string toString ( @@ -136,6 +150,34 @@ inline std::string toString return oss.str(); } +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + unsigned int value +) +{ + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + unsigned long value +) +{ + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + /////////////////////////////////////////////////////////////////////////////// inline std::string toString (