mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-03 13:55:39 +02:00
Make the user-facing random Generators reproducible
Thanks to the new distributions, this is almost trivial change.
This commit is contained in:
@@ -7,7 +7,35 @@
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
|
||||
#include <catch2/generators/catch_generators_random.hpp>
|
||||
|
||||
#include <catch2/internal/catch_context.hpp>
|
||||
|
||||
std::uint32_t Catch::Generators::Detail::getSeed() { return sharedRng()(); }
|
||||
#include <random>
|
||||
|
||||
namespace Catch {
|
||||
namespace Generators {
|
||||
namespace Detail {
|
||||
std::uint32_t getSeed() { return sharedRng()(); }
|
||||
} // namespace Detail
|
||||
|
||||
struct RandomFloatingGenerator<long double>::PImpl {
|
||||
PImpl( long double a, long double b, uint32_t seed ):
|
||||
rng( seed ), dist( a, b ) {}
|
||||
|
||||
Catch::SimplePcg32 rng;
|
||||
std::uniform_real_distribution<long double> dist;
|
||||
};
|
||||
|
||||
RandomFloatingGenerator<long double>::RandomFloatingGenerator(
|
||||
long double a, long double b, std::uint32_t seed) :
|
||||
m_pimpl(Catch::Detail::make_unique<PImpl>(a, b, seed)) {
|
||||
static_cast<void>( next() );
|
||||
}
|
||||
|
||||
RandomFloatingGenerator<long double>::~RandomFloatingGenerator() =
|
||||
default;
|
||||
bool RandomFloatingGenerator<long double>::next() {
|
||||
m_current_number = m_pimpl->dist( m_pimpl->rng );
|
||||
return true;
|
||||
}
|
||||
} // namespace Generators
|
||||
} // namespace Catch
|
||||
|
@@ -11,8 +11,9 @@
|
||||
#include <catch2/internal/catch_context.hpp>
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
#include <catch2/internal/catch_random_number_generator.hpp>
|
||||
|
||||
#include <random>
|
||||
#include <catch2/internal/catch_uniform_integer_distribution.hpp>
|
||||
#include <catch2/internal/catch_uniform_floating_point_distribution.hpp>
|
||||
#include <catch2/internal/catch_unique_ptr.hpp>
|
||||
|
||||
namespace Catch {
|
||||
namespace Generators {
|
||||
@@ -26,7 +27,7 @@ namespace Detail {
|
||||
template <typename Float>
|
||||
class RandomFloatingGenerator final : public IGenerator<Float> {
|
||||
Catch::SimplePcg32 m_rng;
|
||||
std::uniform_real_distribution<Float> m_dist;
|
||||
Catch::uniform_floating_point_distribution<Float> m_dist;
|
||||
Float m_current_number;
|
||||
public:
|
||||
RandomFloatingGenerator( Float a, Float b, std::uint32_t seed ):
|
||||
@@ -44,10 +45,27 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class RandomFloatingGenerator<long double> final : public IGenerator<long double> {
|
||||
// We still rely on <random> for this specialization, but we don't
|
||||
// want to drag it into the header.
|
||||
struct PImpl;
|
||||
Catch::Detail::unique_ptr<PImpl> m_pimpl;
|
||||
long double m_current_number;
|
||||
|
||||
public:
|
||||
RandomFloatingGenerator( long double a, long double b, std::uint32_t seed );
|
||||
|
||||
long double const& get() const override { return m_current_number; }
|
||||
bool next() override;
|
||||
|
||||
~RandomFloatingGenerator() override; // = default
|
||||
};
|
||||
|
||||
template <typename Integer>
|
||||
class RandomIntegerGenerator final : public IGenerator<Integer> {
|
||||
Catch::SimplePcg32 m_rng;
|
||||
std::uniform_int_distribution<Integer> m_dist;
|
||||
Catch::uniform_integer_distribution<Integer> m_dist;
|
||||
Integer m_current_number;
|
||||
public:
|
||||
RandomIntegerGenerator( Integer a, Integer b, std::uint32_t seed ):
|
||||
@@ -68,14 +86,6 @@ public:
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral<T>::value, GeneratorWrapper<T>>
|
||||
random(T a, T b) {
|
||||
static_assert(
|
||||
!std::is_same<T, char>::value &&
|
||||
!std::is_same<T, int8_t>::value &&
|
||||
!std::is_same<T, uint8_t>::value &&
|
||||
!std::is_same<T, signed char>::value &&
|
||||
!std::is_same<T, unsigned char>::value &&
|
||||
!std::is_same<T, bool>::value,
|
||||
"The requested type is not supported by the underlying random distributions from std" );
|
||||
return GeneratorWrapper<T>(
|
||||
Catch::Detail::make_unique<RandomIntegerGenerator<T>>(a, b, Detail::getSeed())
|
||||
);
|
||||
|
Reference in New Issue
Block a user