Split RNG related things into its own file

This further removes 2 function declarations from the common path
This commit is contained in:
Martin Hořeňovský 2017-08-30 15:32:44 +02:00
parent 48db47c737
commit 0673b9be35
9 changed files with 76 additions and 29 deletions

View File

@ -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

View File

@ -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"

View File

@ -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 << ')';

View File

@ -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

View File

@ -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 <cstdlib>
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();
}
}

View File

@ -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 <algorithm>
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<typename V>
static void shuffle( V& vector ) {
RandomNumberGenerator rng;
std::shuffle( vector.begin(), vector.end(), rng );
}
};
}
#endif // TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED

View File

@ -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 <cassert>

View File

@ -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<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {
std::vector<TestCase> sorted = unsortedTestCases;

View File

@ -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<typename V>
static void shuffle( V& vector ) {
RandomNumberGenerator rng;
std::shuffle( vector.begin(), vector.end(), rng );
}
};
std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases );
bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config );