From a80605972682699ee774f45fc78492160e47dd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 10 Dec 2023 16:26:07 +0100 Subject: [PATCH] Fix uniform_floating_point_distribution for unit ranges --- .../catch_random_floating_point_helpers.hpp | 4 ++-- ...ch_uniform_floating_point_distribution.hpp | 2 +- .../RandomNumberGeneration.tests.cpp | 20 +++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/catch2/internal/catch_random_floating_point_helpers.hpp b/src/catch2/internal/catch_random_floating_point_helpers.hpp index d75b42182f..c59c053916 100644 --- a/src/catch2/internal/catch_random_floating_point_helpers.hpp +++ b/src/catch2/internal/catch_random_floating_point_helpers.hpp @@ -31,7 +31,7 @@ namespace Catch { "gamma returns the largest ULP magnitude within " "floating point range [a, b]. This only makes sense " "for floating point types" ); - assert( a < b ); + assert( a <= b ); const auto gamma_up = Catch::nextafter( a, std::numeric_limits::infinity() ) - a; const auto gamma_down = b - Catch::nextafter( b, -std::numeric_limits::infinity() ); @@ -69,7 +69,7 @@ namespace Catch { template DistanceType count_equidistant_floats( FloatType a, FloatType b, FloatType distance ) { - assert( a < b ); + assert( a <= b ); // We get distance as gamma for our uniform float distribution, // so this will round perfectly. const auto ag = a / distance; diff --git a/src/catch2/internal/catch_uniform_floating_point_distribution.hpp b/src/catch2/internal/catch_uniform_floating_point_distribution.hpp index a1cdfe43fc..23d03b43c3 100644 --- a/src/catch2/internal/catch_uniform_floating_point_distribution.hpp +++ b/src/catch2/internal/catch_uniform_floating_point_distribution.hpp @@ -97,7 +97,7 @@ class uniform_floating_point_distribution { m_max_steps_in_one_go( Detail::calculate_max_steps_in_one_go(m_ulp_magnitude)), m_a_has_leq_magnitude(std::fabs(m_a) <= std::fabs(m_b)) { - assert( a < b ); + assert( a <= b ); } template diff --git a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp index 379f8b2322..03be6c9cad 100644 --- a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -568,3 +569,22 @@ TEMPLATE_TEST_CASE( "uniform_floating_point_distribution is reproducible", REQUIRE_THAT( generated, Catch::Matchers::RangeEquals( uniform_fp_test_params::expected ) ); } + +TEMPLATE_TEST_CASE( "uniform_floating_point_distribution can handle unitary ranges", + "[rng][distribution][floating-point][approvals]", + float, + double ) { + std::random_device rd; + auto seed = rd(); + CAPTURE( seed ); + Catch::SimplePcg32 pcg( seed ); + + const auto highest = uniform_fp_test_params::highest; + Catch::uniform_floating_point_distribution dist( highest, + highest ); + + constexpr auto iters = 20; + for (int i = 0; i < iters; ++i) { + REQUIRE( Catch::Detail::directCompare( dist( pcg ), highest ) ); + } +}