mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
Make FastRandomContext support standard C++11 RNG interface
This makes it possible to plug it into the various standard C++11 random distribution algorithms and other functions like std::shuffle.
This commit is contained in:
@@ -8,6 +8,9 @@
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(random_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(osrandom_tests)
|
||||
@@ -57,4 +60,23 @@ BOOST_AUTO_TEST_CASE(fastrandom_randbits)
|
||||
}
|
||||
}
|
||||
|
||||
/** Does-it-compile test for compatibility with standard C++11 RNG interface. */
|
||||
BOOST_AUTO_TEST_CASE(stdrandom_test)
|
||||
{
|
||||
FastRandomContext ctx;
|
||||
std::uniform_int_distribution<int> distribution(3, 9);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
int x = distribution(ctx);
|
||||
BOOST_CHECK(x >= 3);
|
||||
BOOST_CHECK(x <= 9);
|
||||
|
||||
std::vector<int> test{1,2,3,4,5,6,7,8,9,10};
|
||||
std::shuffle(test.begin(), test.end(), ctx);
|
||||
for (int j = 1; j <= 10; ++j) {
|
||||
BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user