diff options
| author | Martin Hořeňovský | 2023-11-28 23:26:06 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2023-12-10 19:53:36 +0100 |
| commit | 04a829b0e13401049707c7c70e95c3cc0c73ba22 (patch) | |
| tree | d98617b090a90a067164899110b84952282e1f08 /tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp | |
| parent | ab1b079e4deb159a6f56c9168031876a697a7d2c (diff) | |
| download | catch3-04a829b0e13401049707c7c70e95c3cc0c73ba22.tar.gz | |
Add helpers for implementing uniform integer distribution
* Utility for extended mult n x n bits -> 2n bits
* Utility to adapt output from URBG to target (unsigned) integral
type
* Utility to reorder signed values into unsigned type while keeping
the order.
Diffstat (limited to 'tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp')
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp index ad44668d..d432b445 100644 --- a/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp @@ -8,6 +8,7 @@ #include <catch2/catch_test_macros.hpp> #include <catch2/catch_template_test_macros.hpp> +#include <catch2/internal/catch_random_integer_helpers.hpp> #include <catch2/internal/catch_random_number_generator.hpp> #include <catch2/internal/catch_random_seed_generation.hpp> #include <catch2/internal/catch_uniform_floating_point_distribution.hpp> @@ -77,3 +78,29 @@ TEMPLATE_TEST_CASE("uniform_floating_point_distribution never returns infs from REQUIRE_FALSE( std::isnan( ret ) ); } } + +TEST_CASE( "fillBitsFrom - shortening and stretching", "[rng][approvals]" ) { + using Catch::Detail::fillBitsFrom; + + // The seed is not important, but the numbers below have to be repeatable. + // They should also exhibit the same general pattern of being prefixes + Catch::SimplePcg32 pcg( 0xaabb'ccdd ); + + SECTION( "Shorten to 8 bits" ) { + // We cast the result to avoid dealing with char-like type in uint8_t + auto shortened = static_cast<uint32_t>( fillBitsFrom<uint8_t>( pcg ) ); + REQUIRE( shortened == 0xcc ); + } + SECTION( "Shorten to 16 bits" ) { + auto shortened = fillBitsFrom<uint16_t>( pcg ); + REQUIRE( shortened == 0xccbe ); + } + SECTION( "Keep at 32 bits" ) { + auto n = fillBitsFrom<uint32_t>( pcg ); + REQUIRE( n == 0xccbe'5f04 ); + } + SECTION( "Stretch to 64 bits" ) { + auto stretched = fillBitsFrom<uint64_t>( pcg ); + REQUIRE( stretched == 0xccbe'5f04'a424'a486 ); + } +} |