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:
Alex Dale
2019-11-20 11:55:47 -08:00
parent 84061e93d6
commit 7ab69e7768
3 changed files with 57 additions and 9 deletions

View File

@@ -14,9 +14,18 @@ namespace wvcdm {
// 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
// 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 {
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
// `RandomData()`.
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
@@ -44,12 +53,11 @@ class CdmRandomGenerator {
// Returns a pseudo-random integer.
// This is similar to `rand()` from the C standard library. The integer
// returned is in the range of [0, RAND_MAX].
int Rand();
// returned is in the range of [min(), max()].
unsigned int Rand();
// Allows for RNG to be callable, this is to make it similar to the
// C++11 generator interfaces.
int operator()() { return Rand(); }
// Allows for RNG to be callable.
unsigned int operator()() { return Rand(); }
// Returns a pseudo-random integer within the provided inclusive range.
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
@@ -79,7 +87,7 @@ class CdmRandomGenerator {
// CdmRandomGenerator.
class CdmRandom {
public:
static int Rand() { return GetInstance()->Rand(); }
static unsigned int Rand() { return GetInstance()->Rand(); }
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
return GetInstance()->RandomInRange(lower, upper);
}

View File

@@ -46,9 +46,9 @@ void CdmRandomGenerator::Seed(unsigned int s) {
generator_.seed(s);
}
int CdmRandomGenerator::Rand() {
unsigned int CdmRandomGenerator::Rand() {
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_);
}

View File

@@ -4,10 +4,13 @@
#include <stdlib.h>
#include <algorithm>
#include <chrono>
#include <limits>
#include <string>
#include <thread>
#include <type_traits>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -30,6 +33,43 @@ constexpr unsigned int kSeeds[] = {0, 1337, 1565904109, 776964657};
class CdmRandomGeneratorTest : public testing::TestWithParam<unsigned int> {};
} // 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) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng;