From 0673b9be35584ff35d3412134b9c9b58117ad7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 30 Aug 2017 15:32:44 +0200 Subject: [PATCH] Split RNG related things into its own file This further removes 2 function declarations from the common path --- CMakeLists.txt | 2 + include/catch_session.hpp | 1 + include/internal/catch_common.cpp | 8 ---- include/internal/catch_common.h | 3 -- .../catch_random_number_generator.cpp | 31 ++++++++++++++ .../internal/catch_random_number_generator.h | 40 +++++++++++++++++++ include/internal/catch_run_context.cpp | 1 + .../catch_test_case_registry_impl.cpp | 4 +- .../catch_test_case_registry_impl.hpp | 15 ------- 9 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 include/internal/catch_random_number_generator.cpp create mode 100644 include/internal/catch_random_number_generator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8248ed99..ad104328 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,6 +161,7 @@ set(INTERNAL_HEADERS ${HEADER_DIR}/internal/catch_objc_arc.hpp ${HEADER_DIR}/internal/catch_option.hpp ${HEADER_DIR}/internal/catch_platform.h + ${HEADER_DIR}/internal/catch_random_number_generator.h ${HEADER_DIR}/internal/catch_reenable_warnings.h ${HEADER_DIR}/internal/catch_reporter_registrars.hpp ${HEADER_DIR}/internal/catch_reporter_registry.hpp @@ -218,6 +219,7 @@ set(IMPL_SOURCES ${HEADER_DIR}/internal/catch_notimplemented_exception.cpp ${HEADER_DIR}/internal/catch_registry_hub.cpp ${HEADER_DIR}/internal/catch_interfaces_reporter.cpp + ${HEADER_DIR}/internal/catch_random_number_generator.cpp ${HEADER_DIR}/internal/catch_result_type.cpp ${HEADER_DIR}/internal/catch_run_context.cpp ${HEADER_DIR}/internal/catch_section.cpp diff --git a/include/catch_session.hpp b/include/catch_session.hpp index f02b93d9..ed1a826a 100644 --- a/include/catch_session.hpp +++ b/include/catch_session.hpp @@ -17,6 +17,7 @@ #include "internal/catch_test_spec.hpp" #include "internal/catch_version.h" #include "internal/catch_interfaces_reporter.h" +#include "internal/catch_random_number_generator.h" #include "internal/catch_startup_exception_registry.h" #include "internal/catch_text.h" diff --git a/include/internal/catch_common.cpp b/include/internal/catch_common.cpp index 01c18ba4..9c18b543 100644 --- a/include/internal/catch_common.cpp +++ b/include/internal/catch_common.cpp @@ -29,14 +29,6 @@ namespace Catch { return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0)); } - 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_common.h b/include/internal/catch_common.h index fa04044a..dfc0258b 100644 --- a/include/internal/catch_common.h +++ b/include/internal/catch_common.h @@ -67,9 +67,6 @@ namespace Catch { bool alwaysTrue(); bool alwaysFalse(); - 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_random_number_generator.cpp b/include/internal/catch_random_number_generator.cpp new file mode 100644 index 00000000..7149a58b --- /dev/null +++ b/include/internal/catch_random_number_generator.cpp @@ -0,0 +1,31 @@ +/* + * Created by Martin on 30/08/2017. + * + * 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_random_number_generator.h" +#include "catch_context.h" +#include "catch_interfaces_config.h" + +#include + +namespace Catch { + + void seedRng( IConfig const& config ) { + if( config.rngSeed() != 0 ) + std::srand( config.rngSeed() ); + } + unsigned int rngSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } + + RandomNumberGenerator::result_type RandomNumberGenerator::operator()( result_type n ) const { + return std::rand() % n; + } + RandomNumberGenerator::result_type RandomNumberGenerator::operator()() const { + return std::rand() % max(); + } + +} diff --git a/include/internal/catch_random_number_generator.h b/include/internal/catch_random_number_generator.h new file mode 100644 index 00000000..859e6cdf --- /dev/null +++ b/include/internal/catch_random_number_generator.h @@ -0,0 +1,40 @@ +/* + * Created by Martin on 30/08/2017. + * + * 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) + */ +#ifndef TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED +#define TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED + +#include "catch_random_number_generator.h" + +#include + +namespace Catch { + + struct IConfig; + + void seedRng( IConfig const& config ); + + unsigned int rngSeed(); + + struct RandomNumberGenerator { + using result_type = std::ptrdiff_t; + + static constexpr result_type min() { return 0; } + static constexpr result_type max() { return 1000000; } + + result_type operator()( result_type n ) const; + result_type operator()() const; + + template + static void shuffle( V& vector ) { + RandomNumberGenerator rng; + std::shuffle( vector.begin(), vector.end(), rng ); + } + }; + +} + +#endif // TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp index 5d640e48..8b22398c 100644 --- a/include/internal/catch_run_context.cpp +++ b/include/internal/catch_run_context.cpp @@ -1,6 +1,7 @@ #include "catch_run_context.hpp" #include "catch_context.h" #include "catch_enforce.h" +#include "catch_random_number_generator.h" #include "catch_stream.h" #include diff --git a/include/internal/catch_test_case_registry_impl.cpp b/include/internal/catch_test_case_registry_impl.cpp index 0bf0000f..85385a9c 100644 --- a/include/internal/catch_test_case_registry_impl.cpp +++ b/include/internal/catch_test_case_registry_impl.cpp @@ -10,6 +10,7 @@ #include "catch_context.h" #include "catch_enforce.h" #include "catch_interfaces_registry_hub.h" +#include "catch_random_number_generator.h" #include "catch_string_manip.h" #include "catch_test_case_info.h" @@ -17,9 +18,6 @@ namespace Catch { - RandomNumberGenerator::result_type RandomNumberGenerator::operator()( result_type n ) const { return std::rand() % n; } - RandomNumberGenerator::result_type RandomNumberGenerator::operator()() const { return std::rand() % max(); } - std::vector sortTests( IConfig const& config, std::vector const& unsortedTestCases ) { std::vector sorted = unsortedTestCases; diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index ee412d9e..f49ad970 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -22,21 +22,6 @@ namespace Catch { class TestCase; struct IConfig; - struct RandomNumberGenerator { - using result_type = std::ptrdiff_t; - - result_type operator()( result_type n ) const; - - static constexpr result_type min() { return 0; } - static constexpr result_type max() { return 1000000; } - result_type operator()() const; - template - static void shuffle( V& vector ) { - RandomNumberGenerator rng; - std::shuffle( vector.begin(), vector.end(), rng ); - } - }; - std::vector sortTests( IConfig const& config, std::vector const& unsortedTestCases ); bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config );