CdmRandomGenerator meets UniformRandomBitGenerator named requirements.
[ Merge of http://go/wvgerrit/89766 ] Certain C++11 (and newer) standard library functions and classes which utilize random number generators require that the random number generators meet the specifications of C++11's UniformRandomBitGenerator name requirements. This is especially important as C++17 will remove support non-compliant alternatives. This change updates CdmRandomGenerator to meet these requirements. Bug: 143494945 Test: Linux and Android unit tests Change-Id: Ib6df44da4969ad7596b16d447c3f8bd9864698f6
This commit is contained in:
@@ -14,9 +14,18 @@ namespace wvcdm {
|
|||||||
// It's purpose is to simplified interface for C++11's <random> library.
|
// It's purpose is to simplified interface for C++11's <random> library.
|
||||||
// Some of the methods use a "device specific" random seed, if the
|
// Some of the methods use a "device specific" random seed, if the
|
||||||
// compiler/device does not support device specific randomizers, then the
|
// compiler/device does not support device specific randomizers, then the
|
||||||
// actual value supplied may not be random.
|
// actual value supplied may not be random. The generator is designed to
|
||||||
|
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
|
||||||
|
// be used with standard library functions / class which are designed to
|
||||||
|
// work with the standard library generators.
|
||||||
class CdmRandomGenerator {
|
class CdmRandomGenerator {
|
||||||
public:
|
public:
|
||||||
|
// Result type of operator().
|
||||||
|
using result_type = unsigned int;
|
||||||
|
// Inclusive boundaries of operator().
|
||||||
|
static constexpr unsigned int min() { return 0; }
|
||||||
|
static constexpr unsigned int max() { return RAND_MAX; }
|
||||||
|
|
||||||
// The maximum number of bytes that can be generated at once for
|
// The maximum number of bytes that can be generated at once for
|
||||||
// `RandomData()`.
|
// `RandomData()`.
|
||||||
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
|
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
|
||||||
@@ -44,12 +53,11 @@ class CdmRandomGenerator {
|
|||||||
|
|
||||||
// Returns a pseudo-random integer.
|
// Returns a pseudo-random integer.
|
||||||
// This is similar to `rand()` from the C standard library. The integer
|
// This is similar to `rand()` from the C standard library. The integer
|
||||||
// returned is in the range of [0, RAND_MAX].
|
// returned is in the range of [min(), max()].
|
||||||
int Rand();
|
unsigned int Rand();
|
||||||
|
|
||||||
// Allows for RNG to be callable, this is to make it similar to the
|
// Allows for RNG to be callable.
|
||||||
// C++11 generator interfaces.
|
unsigned int operator()() { return Rand(); }
|
||||||
int operator()() { return Rand(); }
|
|
||||||
|
|
||||||
// Returns a pseudo-random integer within the provided inclusive range.
|
// Returns a pseudo-random integer within the provided inclusive range.
|
||||||
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
|
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
|
||||||
@@ -79,7 +87,7 @@ class CdmRandomGenerator {
|
|||||||
// CdmRandomGenerator.
|
// CdmRandomGenerator.
|
||||||
class CdmRandom {
|
class CdmRandom {
|
||||||
public:
|
public:
|
||||||
static int Rand() { return GetInstance()->Rand(); }
|
static unsigned int Rand() { return GetInstance()->Rand(); }
|
||||||
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
|
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
|
||||||
return GetInstance()->RandomInRange(lower, upper);
|
return GetInstance()->RandomInRange(lower, upper);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,9 +46,9 @@ void CdmRandomGenerator::Seed(unsigned int s) {
|
|||||||
generator_.seed(s);
|
generator_.seed(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CdmRandomGenerator::Rand() {
|
unsigned int CdmRandomGenerator::Rand() {
|
||||||
CdmRandomLock lock(generator_lock_);
|
CdmRandomLock lock(generator_lock_);
|
||||||
std::uniform_int_distribution<int> dist(0, RAND_MAX);
|
std::uniform_int_distribution<unsigned int> dist(0, RAND_MAX);
|
||||||
return dist(generator_);
|
return dist(generator_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,13 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
@@ -30,6 +33,43 @@ constexpr unsigned int kSeeds[] = {0, 1337, 1565904109, 776964657};
|
|||||||
class CdmRandomGeneratorTest : public testing::TestWithParam<unsigned int> {};
|
class CdmRandomGeneratorTest : public testing::TestWithParam<unsigned int> {};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
// Checks that the class CdmRandomGenerator meets the requirements of
|
||||||
|
// UniformRandomBitGenerator.
|
||||||
|
// ref: https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator
|
||||||
|
TEST(CdmRandomGeneratorTest, UniformRandomBitGeneratorRequirements) {
|
||||||
|
// Let G represent class CdmRandomGenerator, and g represent an instance
|
||||||
|
// of CdmRandomGenerator.
|
||||||
|
// 1) G::result_type is an unsigned integer (unspecified precision).
|
||||||
|
EXPECT_TRUE(std::is_integral<CdmRandomGenerator::result_type>::value);
|
||||||
|
EXPECT_TRUE(std::is_unsigned<CdmRandomGenerator::result_type>::value);
|
||||||
|
// 2&3 a) G::min() and G::max() have the result type of G::result_type.
|
||||||
|
EXPECT_TRUE((std::is_same<CdmRandomGenerator::result_type,
|
||||||
|
decltype(CdmRandomGenerator::min())>::value));
|
||||||
|
EXPECT_TRUE((std::is_same<CdmRandomGenerator::result_type,
|
||||||
|
decltype(CdmRandomGenerator::max())>::value));
|
||||||
|
// 2&3 b) G::min() is strictly less than G::max().
|
||||||
|
EXPECT_LT(CdmRandomGenerator::min(), CdmRandomGenerator::max());
|
||||||
|
// 4 a) g() have the result type of G::result_type.
|
||||||
|
CdmRandomGenerator g;
|
||||||
|
EXPECT_TRUE(
|
||||||
|
(std::is_same<CdmRandomGenerator::result_type, decltype(g())>::value));
|
||||||
|
// 4 b) g() is within [G::min() G::max()]
|
||||||
|
std::vector<CdmRandomGenerator::result_type> values;
|
||||||
|
for (size_t i = 0; i < kRandomTrialCount; ++i) {
|
||||||
|
CdmRandomGenerator::result_type x = g();
|
||||||
|
EXPECT_LE(CdmRandomGenerator::min(), x);
|
||||||
|
EXPECT_GE(CdmRandomGenerator::max(), x);
|
||||||
|
values.push_back(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify compilation.
|
||||||
|
|
||||||
|
// std::shuffle(RandomIt first, RandomIt last, URBG&& g) requires the
|
||||||
|
// class URBG to meet "UniformRandomBitGenerator" requirements. This
|
||||||
|
// will fail to compile if the requirements are not met.
|
||||||
|
std::shuffle(values.begin(), values.end(), CdmRandomGenerator());
|
||||||
|
}
|
||||||
|
|
||||||
TEST_P(CdmRandomGeneratorTest, AllMethods) {
|
TEST_P(CdmRandomGeneratorTest, AllMethods) {
|
||||||
const unsigned int seed = GetParam();
|
const unsigned int seed = GetParam();
|
||||||
CdmRandomGenerator rng;
|
CdmRandomGenerator rng;
|
||||||
|
|||||||
Reference in New Issue
Block a user