blob: 20091309b959b7f4a2c76186711481c97dccdfc7 [file] [log] [blame]
 // Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include #include #include #include "gtest/gtest.h" #include "absl/random/distributions.h" #include "absl/random/random.h" namespace { template void TestUniform(URBG* gen) { // [a, b) default-semantics, inferred types. absl::Uniform(*gen, 0, 100); // int absl::Uniform(*gen, 0, 1.0); // Promoted to double absl::Uniform(*gen, 0.0f, 1.0); // Promoted to double absl::Uniform(*gen, 0.0, 1.0); // double absl::Uniform(*gen, -1, 1L); // Promoted to long // Roll a die. absl::Uniform(absl::IntervalClosedClosed, *gen, 1, 6); // Get a fraction. absl::Uniform(absl::IntervalOpenOpen, *gen, 0.0, 1.0); // Assign a value to a random element. std::vector elems = {10, 20, 30, 40, 50}; elems[absl::Uniform(*gen, 0u, elems.size())] = 5; elems[absl::Uniform(*gen, 0, elems.size())] = 3; // Choose some epsilon around zero. absl::Uniform(absl::IntervalOpenOpen, *gen, -1.0, 1.0); // (a, b) semantics, inferred types. absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 1.0); // Promoted to double // Explicit overriding of types. absl::Uniform(*gen, 0, 100); absl::Uniform(*gen, 0, 100); absl::Uniform(*gen, 0, 100); absl::Uniform(*gen, 0, 100); absl::Uniform(*gen, 0, 1 << 10); absl::Uniform(*gen, 0, 1 << 10); absl::Uniform(*gen, 0, 1 << 10); absl::Uniform(*gen, 0, 1 << 10); absl::Uniform(*gen, 0.0, 1.0); absl::Uniform(*gen, 0, 1); absl::Uniform(*gen, -1, 1); absl::Uniform(*gen, 0.0, 1.0); absl::Uniform(*gen, -1.0, 0); absl::Uniform(*gen, -1.0, 0); // Tagged absl::Uniform(absl::IntervalClosedClosed, *gen, 0, 1); absl::Uniform(absl::IntervalClosedOpen, *gen, 0, 1); absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 1); absl::Uniform(absl::IntervalOpenClosed, *gen, 0, 1); absl::Uniform(absl::IntervalClosedClosed, *gen, 0, 1); absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 1); absl::Uniform(absl::IntervalClosedClosed, *gen, 0, 100); absl::Uniform(absl::IntervalClosedOpen, *gen, 0, 100); absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 100); absl::Uniform(absl::IntervalOpenClosed, *gen, 0, 100); absl::Uniform(absl::IntervalClosedClosed, *gen, 0, 100); absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 100); // With *generator as an R-value reference. absl::Uniform(URBG(), 0, 100); absl::Uniform(URBG(), 0.0, 1.0); } template void TestExponential(URBG* gen) { absl::Exponential(*gen); absl::Exponential(*gen); absl::Exponential(URBG()); } template void TestPoisson(URBG* gen) { // [rand.dist.pois] Indicates that the std::poisson_distribution // is parameterized by IntType, however MSVC does not allow 8-bit // types. absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(*gen); absl::Poisson(URBG()); absl::Poisson(*gen); absl::Poisson(*gen); } template void TestBernoulli(URBG* gen) { absl::Bernoulli(*gen, 0.5); absl::Bernoulli(*gen, 0.5); } template void TestZipf(URBG* gen) { absl::Zipf(*gen, 100); absl::Zipf(*gen, 100); absl::Zipf(*gen, 100); absl::Zipf(*gen, 100); absl::Zipf(*gen, 1 << 10); absl::Zipf(*gen, 1 << 10); absl::Zipf(*gen, 1 << 10); absl::Zipf(*gen, 1 << 10); absl::Zipf(URBG(), 1 << 10); absl::Zipf(*gen, 1 << 10); absl::Zipf(*gen, 1 << 10); } template void TestGaussian(URBG* gen) { absl::Gaussian(*gen, 1.0, 1.0); absl::Gaussian(*gen, 1.0, 1.0); absl::Gaussian(URBG(), 1.0, 1.0); } template void TestLogNormal(URBG* gen) { absl::LogUniform(*gen, 0, 100); absl::LogUniform(*gen, 0, 100); absl::LogUniform(*gen, 0, 100); absl::LogUniform(*gen, 0, 100); absl::LogUniform(*gen, 0, 1 << 10); absl::LogUniform(*gen, 0, 1 << 10); absl::LogUniform(*gen, 0, 1 << 10); absl::LogUniform(*gen, 0, 1 << 10); absl::LogUniform(URBG(), 0, 1 << 10); absl::LogUniform(*gen, 0, 1 << 10); absl::LogUniform(*gen, 0, 1 << 10); } template void CompatibilityTest() { URBG gen; TestUniform(&gen); TestExponential(&gen); TestPoisson(&gen); TestBernoulli(&gen); TestZipf(&gen); TestGaussian(&gen); TestLogNormal(&gen); } TEST(std_mt19937_64, Compatibility) { // Validate with std::mt19937_64 CompatibilityTest(); } TEST(BitGen, Compatibility) { // Validate with absl::BitGen CompatibilityTest(); } TEST(InsecureBitGen, Compatibility) { // Validate with absl::InsecureBitGen CompatibilityTest(); } } // namespace