From e9a2230ad8e51b13b76eb955559b323151837a17 Mon Sep 17 00:00:00 2001 From: Malcolm Noyes Date: Sun, 10 Nov 2013 15:11:21 +0000 Subject: [PATCH] VS integration part 1 - allow catch_message.hpp to be included by more than 1 source file --- .gitignore | 2 + include/catch.hpp | 1 + include/internal/catch_message.h | 2 - include/internal/catch_message.hpp | 20 ++++--- .../SelfTest/MessageInstantiationTests1.cpp | 50 +++++++++++++++++ .../SelfTest/MessageInstantiationTests2.cpp | 49 +++++++++++++++++ projects/SelfTest/makefile | 4 +- .../TestCatch/TestCatch/TestCatch.vcxproj | 55 ++++++++++++++++--- 8 files changed, 165 insertions(+), 18 deletions(-) create mode 100644 projects/SelfTest/MessageInstantiationTests1.cpp create mode 100644 projects/SelfTest/MessageInstantiationTests2.cpp diff --git a/.gitignore b/.gitignore index 569f38d8..215fb7f9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,13 @@ *.pbxuser *.mode1v3 *.ncb +*.sdf *.suo Debug Release *.user *.xcuserstate +*.o .DS_Store xcuserdata CatchSelfTest.xcscheme diff --git a/include/catch.hpp b/include/catch.hpp index 600815f7..234ec631 100644 --- a/include/catch.hpp +++ b/include/catch.hpp @@ -38,6 +38,7 @@ #endif #if defined( CATCH_CONFIG_MAIN ) || defined( CATCH_CONFIG_RUNNER ) +#define INTERNAL_CATCH_INLINE #include "internal/catch_impl.hpp" #endif diff --git a/include/internal/catch_message.h b/include/internal/catch_message.h index d0886654..4e7d11d8 100644 --- a/include/internal/catch_message.h +++ b/include/internal/catch_message.h @@ -31,8 +31,6 @@ namespace Catch { bool operator < ( MessageInfo const& other ) const { return sequence < other.sequence; } - private: - static unsigned int globalCount; }; struct MessageBuilder { diff --git a/include/internal/catch_message.hpp b/include/internal/catch_message.hpp index 8f303f7a..f45eb508 100644 --- a/include/internal/catch_message.hpp +++ b/include/internal/catch_message.hpp @@ -12,28 +12,32 @@ namespace Catch { - MessageInfo::MessageInfo( std::string const& _macroName, + template + struct MessageInfoCounter { + // This may need protecting if threading support is added + static T globalCount; + }; + template + T MessageInfoCounter::globalCount = T(); + + INTERNAL_CATCH_INLINE MessageInfo::MessageInfo( std::string const& _macroName, SourceLineInfo const& _lineInfo, ResultWas::OfType _type ) : macroName( _macroName ), lineInfo( _lineInfo ), type( _type ), - sequence( ++globalCount ) + sequence( ++MessageInfoCounter::globalCount ) {} - // This may need protecting if threading support is added - unsigned int MessageInfo::globalCount = 0; - - //////////////////////////////////////////////////////////////////////////// - ScopedMessage::ScopedMessage( MessageBuilder const& builder ) + INTERNAL_CATCH_INLINE ScopedMessage::ScopedMessage( MessageBuilder const& builder ) : m_info( builder.m_info ) { m_info.message = builder.m_stream.str(); getResultCapture().pushScopedMessage( m_info ); } - ScopedMessage::~ScopedMessage() { + INTERNAL_CATCH_INLINE ScopedMessage::~ScopedMessage() { getResultCapture().popScopedMessage( m_info ); } diff --git a/projects/SelfTest/MessageInstantiationTests1.cpp b/projects/SelfTest/MessageInstantiationTests1.cpp new file mode 100644 index 00000000..c4605cc2 --- /dev/null +++ b/projects/SelfTest/MessageInstantiationTests1.cpp @@ -0,0 +1,50 @@ +/* + * Created by Phil on 09/11/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +#include "catch.hpp" +#undef INTERNAL_CATCH_INLINE +#define INTERNAL_CATCH_INLINE inline +#include "internal/catch_message.hpp" + +namespace Counter { +int g_haveCountedMessages = 0; +} + +// This test works with the equivalent in MessageInstantiationTests2.cpp +// The first test to reach this point will increment the MessageInfo counter. Subsequent tests +// just check the value. The purpose of this test is to verify that the compiler's +// greedy instantiation (or whatever process it uses) eliminate all other +// references to the globalCount + +TEST_CASE("message counting1","") +{ + if( Counter::g_haveCountedMessages > 0 ) { + REQUIRE( Catch::MessageInfoCounter::globalCount > 0 ); + } + else + { + ++Catch::MessageInfoCounter::globalCount; + Counter::g_haveCountedMessages = 1; + } +} + +namespace LongCounter { +int g_haveCountedMessagesLong = 0; +} + +TEST_CASE("long message counting1","") +{ + if( LongCounter::g_haveCountedMessagesLong > 0 ) { + REQUIRE( Catch::MessageInfoCounter::globalCount > 0 ); + } + else + { + ++Catch::MessageInfoCounter::globalCount; + LongCounter::g_haveCountedMessagesLong = 1; + } +} diff --git a/projects/SelfTest/MessageInstantiationTests2.cpp b/projects/SelfTest/MessageInstantiationTests2.cpp new file mode 100644 index 00000000..45a99f19 --- /dev/null +++ b/projects/SelfTest/MessageInstantiationTests2.cpp @@ -0,0 +1,49 @@ +/* + * Created by Phil on 09/11/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +#include "catch.hpp" +#define INTERNAL_CATCH_INLINE inline +#include "internal/catch_message.hpp" + +namespace Counter { +extern int g_haveCountedMessages; +} + +// This test works with the equivalent in MessageInstantiationTests1.cpp +// The first test to reach this point will increment the MessageInfo counter. Subsequent tests +// just check the value. The purpose of this test is to verify that the compiler's +// greedy instantiation (or whatever process it uses) eliminate all other +// references to the globalCount + +TEST_CASE("message counting2","") +{ + if( Counter::g_haveCountedMessages > 0 ) { + REQUIRE( Catch::MessageInfoCounter::globalCount > 0 ); + } + else + { + ++Catch::MessageInfoCounter::globalCount; + Counter::g_haveCountedMessages = 1; + } +} + +namespace LongCounter { +extern int g_haveCountedMessagesLong; +} + +TEST_CASE("long message counting2","") +{ + if( LongCounter::g_haveCountedMessagesLong > 0 ) { + REQUIRE( Catch::MessageInfoCounter::globalCount > 0 ); + } + else + { + ++Catch::MessageInfoCounter::globalCount; + LongCounter::g_haveCountedMessagesLong = 1; + } +} diff --git a/projects/SelfTest/makefile b/projects/SelfTest/makefile index e5dec0dc..9be05008 100644 --- a/projects/SelfTest/makefile +++ b/projects/SelfTest/makefile @@ -4,6 +4,8 @@ SOURCES = ApproxTests.cpp \ ExceptionTests.cpp \ GeneratorTests.cpp \ MessageTests.cpp \ + MessageInstantiationTests1.cpp \ + MessageInstantiationTests2.cpp \ MiscTests.cpp \ TestMain.cpp \ TrickyTests.cpp \ @@ -18,4 +20,4 @@ CatchSelfTest: $(OBJECTS) $(CXX) -o $@ $^ clean: - rm -f $(OBJECTS) + rm -f CatchSelfTest CatchSelfTest.exe $(OBJECTS) diff --git a/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj b/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj index f140739c..eb3b6eb8 100644 --- a/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj +++ b/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj @@ -94,6 +94,8 @@ + + @@ -108,42 +110,81 @@ - - - + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + - + +