From d53777a810bbe22bbffc1b3f94236900281dac70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 10 Dec 2023 12:10:19 +0100 Subject: [PATCH] WIP: test fmt --- ...ch_uniform_floating_point_distribution.hpp | 5 +- .../RandomNumberGeneration.tests.cpp | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/catch2/internal/catch_uniform_floating_point_distribution.hpp b/src/catch2/internal/catch_uniform_floating_point_distribution.hpp index 9a52bcc739..a1cdfe43fc 100644 --- a/src/catch2/internal/catch_uniform_floating_point_distribution.hpp +++ b/src/catch2/internal/catch_uniform_floating_point_distribution.hpp @@ -10,10 +10,10 @@ #define CATCH_UNIFORM_FLOATING_POINT_DISTRIBUTION_HPP_INCLUDED #include +#include #include #include -#include namespace Catch { @@ -76,8 +76,7 @@ class uniform_floating_point_distribution { FloatType m_a, m_b; FloatType m_ulp_magnitude; WidthType m_floats_in_range; - // TODO: we want to eventually replace this distribution with our own for reproducibility - std::uniform_int_distribution m_int_dist; + uniform_integer_distribution m_int_dist; // In specific cases, we can overflow into `inf` when computing the // `steps * g` offset. To avoid this, we don't offset by more than this diff --git a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp index 5c17fb2b8f..b310c75d4d 100644 --- a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp @@ -16,6 +16,12 @@ #include #include +#include + + +#include +#include + TEST_CASE("Our PCG implementation provides expected results for known seeds", "[rng]") { Catch::SimplePcg32 rng; SECTION("Default seeded") { @@ -489,3 +495,52 @@ TEMPLATE_TEST_CASE( "uniform_integer_distribution is reproducible", REQUIRE_THAT(generated, Catch::Matchers::RangeEquals(uniform_integer_test_params::expected)); } + + +namespace { + template + struct uniform_fp_test_params; + + template<> + struct uniform_fp_test_params { + // We picked these to be exactly representable + static constexpr float lowest = -256.125f; + static constexpr float highest = 385.125f; + static constexpr float expected[] = { 0.0f }; + }; + template <> + struct uniform_fp_test_params { + static constexpr double lowest = 111; + static constexpr double highest = 123; + static constexpr double expected[] = { 0.0 }; + }; + +// We need these definitions for C++14 and earlier, but +// GCC will complain about them in newer C++ standards +#if __cplusplus <= 201402L + constexpr float uniform_fp_test_params::expected[]; + constexpr double uniform_fp_test_params::expected[]; +#endif +} // namespace + +TEMPLATE_TEST_CASE( "uniform_floating_point_distribution is reproducible", + "[rng][distribution][floating-point][approvals]", + float, + double ) { + Catch::SimplePcg32 pcg( 0xaabb'aabb ); + + const auto lowest = uniform_fp_test_params::lowest; + const auto highest = uniform_fp_test_params::highest; + Catch::uniform_floating_point_distribution dist( lowest, highest ); + + constexpr auto iters = 15; + std::array generated; + for ( int i = 0; i < iters; ++i ) { + generated[i] = dist( pcg ); + std::cout << std::format( "{}\n", generated[i] ); + } + + + +// REQUIRE_THAT( generated, Catch::Matchers::RangeEquals( uniform_fp_test_params::expected ) ); +}