From d87e551efabb32bb08cd9abff51bb0c47fe6081c Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Thu, 2 Jul 2015 23:02:35 +0100 Subject: [PATCH] reseeds rng before each test case and provides access to seed through Catch::rngSeed() function --- include/catch_runner.hpp | 2 +- include/internal/catch_common.h | 5 +++++ include/internal/catch_common.hpp | 8 ++++++++ include/internal/catch_runner_impl.hpp | 2 ++ include/internal/catch_test_case_registry_impl.hpp | 2 ++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index 3a674148..18754483 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -198,7 +198,7 @@ namespace Catch { if( m_configData.filenamesAsTags ) applyFilenamesAsTags(); - std::srand( m_configData.rngSeed ); + seedRng( *m_config ); Runner runner( m_config ); diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h index 9f07e47c..cec7eba5 100644 --- a/include/internal/catch_common.h +++ b/include/internal/catch_common.h @@ -22,6 +22,8 @@ #include "catch_compiler_capabilities.h" namespace Catch { + + struct IConfig; class NonCopyable { #ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS @@ -109,6 +111,9 @@ namespace Catch { void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ); + void seedRng( IConfig const& config ); + unsigned int rngSeed(); + // Use this in variadic streaming macros to allow // >> +StreamEndStop // as well as diff --git a/include/internal/catch_common.hpp b/include/internal/catch_common.hpp index 8a42d0a0..69a64ad1 100644 --- a/include/internal/catch_common.hpp +++ b/include/internal/catch_common.hpp @@ -82,6 +82,14 @@ namespace Catch { return line < other.line || ( line == other.line && file < other.file ); } + void seedRng( IConfig const& config ) { + if( config.rngSeed() != 0 ) + std::srand( config.rngSeed() ); + } + unsigned int rngSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { #ifndef __GNUG__ os << info.file << "(" << info.line << ")"; diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 98f3dfd1..073f78d3 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -259,6 +259,8 @@ namespace Catch { m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal ); TestCaseTracker::Guard guard( *m_testCaseTracker ); + seedRng( *m_config ); + Timer timer; timer.start(); if( m_reporter->getPreferences().shouldRedirectStdOut ) { diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index 21165a30..b25ed9a2 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -90,6 +90,8 @@ namespace Catch { break; case RunTests::InRandomOrder: { + seedRng( config ); + RandomNumberGenerator rng; std::random_shuffle( matchingTestCases.begin(), matchingTestCases.end(), rng ); }