reseeds rng before each test case and provides access to seed through Catch::rngSeed() function

This commit is contained in:
Phil Nash 2015-07-02 23:02:35 +01:00
parent b971fe785b
commit d87e551efa
5 changed files with 18 additions and 1 deletions

View File

@ -198,7 +198,7 @@ namespace Catch {
if( m_configData.filenamesAsTags ) if( m_configData.filenamesAsTags )
applyFilenamesAsTags(); applyFilenamesAsTags();
std::srand( m_configData.rngSeed ); seedRng( *m_config );
Runner runner( m_config ); Runner runner( m_config );

View File

@ -22,6 +22,8 @@
#include "catch_compiler_capabilities.h" #include "catch_compiler_capabilities.h"
namespace Catch { namespace Catch {
struct IConfig;
class NonCopyable { class NonCopyable {
#ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS #ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS
@ -109,6 +111,9 @@ namespace Catch {
void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ); 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 // Use this in variadic streaming macros to allow
// >> +StreamEndStop // >> +StreamEndStop
// as well as // as well as

View File

@ -82,6 +82,14 @@ namespace Catch {
return line < other.line || ( line == other.line && file < other.file ); 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 ) { std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) {
#ifndef __GNUG__ #ifndef __GNUG__
os << info.file << "(" << info.line << ")"; os << info.file << "(" << info.line << ")";

View File

@ -259,6 +259,8 @@ namespace Catch {
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal ); m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal );
TestCaseTracker::Guard guard( *m_testCaseTracker ); TestCaseTracker::Guard guard( *m_testCaseTracker );
seedRng( *m_config );
Timer timer; Timer timer;
timer.start(); timer.start();
if( m_reporter->getPreferences().shouldRedirectStdOut ) { if( m_reporter->getPreferences().shouldRedirectStdOut ) {

View File

@ -90,6 +90,8 @@ namespace Catch {
break; break;
case RunTests::InRandomOrder: case RunTests::InRandomOrder:
{ {
seedRng( config );
RandomNumberGenerator rng; RandomNumberGenerator rng;
std::random_shuffle( matchingTestCases.begin(), matchingTestCases.end(), rng ); std::random_shuffle( matchingTestCases.begin(), matchingTestCases.end(), rng );
} }