DO NOT MERGE: Fix bug in random.

Test: ./run_test.py --bitness 32
Test: ./run_test.py --bitness 64
Test: ./run_test.py --bitness 64 --host
Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=994957
Bug: http://b/139690488
Change-Id: I71708114d7fc8ed90c30b4d32b01d3f3aef7600b
(cherry picked from commit a1d1caa3d831030ce802ed335a9743180911d553)
This commit is contained in:
Dan Albert
2019-09-10 21:52:53 -07:00
parent c2ca4c16d1
commit 01ed1e9c7b
3 changed files with 103 additions and 21 deletions

View File

@@ -29,6 +29,20 @@ sqr(T x)
return x * x;
}
struct Eng : std::mt19937 {
using Base = std::mt19937;
using Base::Base;
};
void test_small_inputs() {
Eng engine;
std::geometric_distribution<std::int16_t> distribution(5.45361e-311);
for (auto i=0; i < 1000; ++i) {
volatile auto res = distribution(engine);
((void)res);
}
}
void
test1()
{
@@ -295,4 +309,5 @@ int main()
test4();
test5();
test6();
test_small_inputs();
}

View File

@@ -29,6 +29,68 @@ sqr(T x)
return x * x;
}
void test_bad_ranges() {
// Test cases where the mean is around the largest representable integer for
// `result_type`. These cases don't generate valid poisson distributions, but
// at least they don't blow up.
std::mt19937 eng;
{
std::poisson_distribution<std::int16_t> distribution(32710.9);
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
std::poisson_distribution<std::int16_t> distribution(std::numeric_limits<std::int16_t>::max());
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
std::poisson_distribution<std::int16_t> distribution(
static_cast<double>(std::numeric_limits<std::int16_t>::max()) + 10);
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
std::poisson_distribution<std::int16_t> distribution(
static_cast<double>(std::numeric_limits<std::int16_t>::max()) * 2);
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
// We convert `INF` to `DBL_MAX` otherwise the distribution will hang.
std::poisson_distribution<std::int16_t> distribution(std::numeric_limits<double>::infinity());
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
std::poisson_distribution<std::int16_t> distribution(0);
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
{
// We convert `INF` to `DBL_MAX` otherwise the distribution will hang.
std::poisson_distribution<std::int16_t> distribution(-100);
for (int i=0; i < 1000; ++i) {
volatile std::int16_t res = distribution(eng);
((void)res);
}
}
}
int main()
{
{
@@ -148,4 +210,6 @@ int main()
assert(std::abs((skew - x_skew) / x_skew) < 0.01);
assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
}
test_bad_ranges();
}