catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Use std::shuffle instead of (deprecated) std::random_shuffle if C++14 detected
- Commit
be3570ef22a450e7e040913822828468f70d7f81- Author
- Phil Nash ·
- Parents
a74d760d7454
Changes
include/internal/catch_compiler_capabilities.h | 12 ++++++-- include/internal/catch_test_case_registry_impl.hpp | 33 ++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/internal/catch_compiler_capabilities.h b/include/internal/catch_compiler_capabilities.h index ed4aa52b..5af1e76a 100644 --- a/include/internal/catch_compiler_capabilities.h +++ b/include/internal/catch_compiler_capabilities.h @@ -36,8 +36,16 @@ // All the C++11 features can be disabled with CATCH_CONFIG_NO_CPP11 -#if defined(__cplusplus) && __cplusplus >= 201103L -# define CATCH_CPP11_OR_GREATER +#ifdef __cplusplus + +# if __cplusplus >= 201103L +# define CATCH_CPP11_OR_GREATER +# endif + +# if __cplusplus >= 201402L +# define CATCH_CPP14_OR_GREATER +# endif + #endif #ifdef __clang__ diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index cc48d917..7f49d692 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -19,13 +19,30 @@ #include <iostream> #include <algorithm> -namespace Catch { +#ifdef CATCH_CPP14_OR_GREATER +#include <random> +#endif - struct LexSort { - bool operator() (TestCase i,TestCase j) const { return (i<j);} - }; +namespace Catch { + struct RandomNumberGenerator { - int operator()( int n ) const { return std::rand() % n; } + typedef int result_type; + + result_type operator()( result_type n ) const { return std::rand() % n; } + +#ifdef CATCH_CPP14_OR_GREATER + static constexpr result_type min() { return 0; } + static constexpr result_type max() { return 1000000; } + result_type operator()() const { return std::rand() % max(); } +#endif + template<typename V> + static void shuffle( V& vector ) { +#ifdef CATCH_CPP14_OR_GREATER + std::shuffle( vector.begin(), vector.end(), RandomNumberGenerator() ); +#else + std::random_shuffle( vector.begin(), vector.end(), RandomNumberGenerator() ); +#endif + } }; inline std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) { @@ -34,14 +51,12 @@ namespace Catch { switch( config.runOrder() ) { case RunTests::InLexicographicalOrder: - std::sort( sorted.begin(), sorted.end(), LexSort() ); + std::sort( sorted.begin(), sorted.end() ); break; case RunTests::InRandomOrder: { seedRng( config ); - - RandomNumberGenerator rng; - std::random_shuffle( sorted.begin(), sorted.end(), rng ); + RandomNumberGenerator::shuffle( sorted ); } break; case RunTests::InDeclarationOrder: