diff options
author | Pieter Wuille <pieter@wuille.net> | 2024-03-10 12:38:14 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2024-07-01 10:26:46 -0400 |
commit | ddb7d26cfd96c1f626def4755e0e1b5aaac94d3e (patch) | |
tree | 6a460803a2e6cf9ea92998fc3e8b19d5dea276d6 /src/test/random_tests.cpp | |
parent | 21ce9d8658fed0d3e4552e8b02a6902cb31c572e (diff) |
random: add RandomMixin::randbits with compile-known bits
In many cases, it is known at compile time how many bits are requested from
randbits. Provide a variant of randbits that accepts this number as a template,
to make sure the compiler can make use of this knowledge. This is used immediately
in rand32() and randbool(), and a few further call sites.
Diffstat (limited to 'src/test/random_tests.cpp')
-rw-r--r-- | src/test/random_tests.cpp | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/test/random_tests.cpp b/src/test/random_tests.cpp index 53c29f31ca..2617cc4a2a 100644 --- a/src/test/random_tests.cpp +++ b/src/test/random_tests.cpp @@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(fastrandom_randbits) BOOST_AUTO_TEST_CASE(randbits_test) { FastRandomContext ctx_lens; //!< RNG for producing the lengths requested from ctx_test. - FastRandomContext ctx_test; //!< The RNG being tested. + FastRandomContext ctx_test1(true), ctx_test2(true); //!< The RNGs being tested. int ctx_test_bitsleft{0}; //!< (Assumed value of) ctx_test::bitbuf_len // Run the entire test 5 times. @@ -122,7 +122,25 @@ BOOST_AUTO_TEST_CASE(randbits_test) // Decide on a number of bits to request (0 through 64, inclusive; don't use randbits/randrange). int bits = ctx_lens.rand64() % 65; // Generate that many bits. - uint64_t gen = ctx_test.randbits(bits); + uint64_t gen = ctx_test1.randbits(bits); + // For certain bits counts, also test randbits<Bits> and compare. + uint64_t gen2; + if (bits == 0) { + gen2 = ctx_test2.randbits<0>(); + } else if (bits == 1) { + gen2 = ctx_test2.randbits<1>(); + } else if (bits == 7) { + gen2 = ctx_test2.randbits<7>(); + } else if (bits == 32) { + gen2 = ctx_test2.randbits<32>(); + } else if (bits == 51) { + gen2 = ctx_test2.randbits<51>(); + } else if (bits == 64) { + gen2 = ctx_test2.randbits<64>(); + } else { + gen2 = ctx_test2.randbits(bits); + } + BOOST_CHECK_EQUAL(gen, gen2); // Make sure the result is in range. if (bits < 64) BOOST_CHECK_EQUAL(gen >> bits, 0); // Mark all the seen bits in the output. |