15 #include "absl/random/uniform_real_distribution.h"
24 #include <type_traits>
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 #include "absl/base/internal/raw_logging.h"
30 #include "absl/numeric/internal/representation.h"
31 #include "absl/random/internal/chi_square.h"
32 #include "absl/random/internal/distribution_test_util.h"
33 #include "absl/random/internal/pcg_engine.h"
34 #include "absl/random/internal/sequence_urbg.h"
35 #include "absl/random/random.h"
36 #include "absl/strings/str_cat.h"
58 template <
typename RealType>
73 TYPED_TEST(UniformRealDistributionTest, ParamSerializeTest) {
74 #if (defined(__i386__) || defined(_M_IX86)) && FLT_EVAL_METHOD != 0
79 <<
"Skipping the test because we detected x87 floating-point semantics";
83 using param_type =
typename DistributionType::param_type;
88 std::numeric_limits<real_type>::epsilon();
90 std::numeric_limits<real_type>::lowest();
96 constexpr
const real_type kTwo60{1152921504606846976};
98 constexpr
int kCount = 1000;
100 for (
const auto& param : {
111 param_type(kTwo60,
real_type(1152921504606847040)),
113 param_type(kTwo60,
real_type(1152921504606847104)),
116 param_type(kTwo60,
real_type(1152921504606847232)),
119 param_type(kTwo60,
real_type(1152921573326323712)),
121 param_type(kTwo60,
real_type(1152921642045800448)),
124 param_type(kTwo60,
real_type(1152921779484753920)),
127 param_type(kLowest, 0),
129 param_type(0, kEpsilon),
130 param_type(-kEpsilon, kEpsilon),
131 param_type(0, kOneMinusE),
132 param_type(0, kDenormMax),
135 const auto a = param.a();
136 const auto b = param.b();
142 DistributionType via_param(param);
146 std::stringstream ss;
163 auto sample_min =
after.max();
164 auto sample_max =
after.min();
165 for (
int i = 0;
i < kCount;
i++) {
175 if (sample > sample_max) {
178 if (sample < sample_min) {
186 ", ",
static_cast<double>(sample_max));
193 #pragma warning(push)
194 #pragma warning(disable:4756) // Constant arithmetic overflow.
196 TYPED_TEST(UniformRealDistributionTest, ViolatesPreconditionsDeathTest) {
200 #if GTEST_HAS_DEATH_TEST
202 EXPECT_DEBUG_DEATH({ DistributionType dist(10.0, 1.0); },
"");
207 DistributionType dist(std::numeric_limits<real_type>::lowest(),
213 const auto kEpsilon = std::nexttoward(
225 DistributionType dist(std::numeric_limits<real_type>::lowest(),
230 #endif // GTEST_HAS_DEATH_TEST
236 DistributionType dist(10.0, 1.0);
241 DistributionType dist(std::numeric_limits<real_type>::lowest(),
250 #pragma warning(pop) // warning(disable:4756)
253 TYPED_TEST(UniformRealDistributionTest, TestMoments) {
256 constexpr
int kSize = 1000000;
264 DistributionType dist;
277 TYPED_TEST(UniformRealDistributionTest, ChiSquaredTest50) {
279 using param_type =
typename DistributionType::param_type;
283 constexpr
size_t kTrials = 100000;
284 constexpr
int kBuckets = 50;
285 constexpr
double kExpected =
286 static_cast<double>(kTrials) /
static_cast<double>(kBuckets);
291 const int kThreshold =
299 for (
const auto& param : {param_type(0, 1), param_type(5, 12),
300 param_type(-5, 13), param_type(-5, -2)}) {
301 const double min_val = param.a();
302 const double max_val = param.b();
303 const double factor = kBuckets / (max_val - min_val);
305 std::vector<int32_t> counts(kBuckets, 0);
306 DistributionType dist(param);
307 for (
size_t i = 0;
i < kTrials;
i++) {
309 auto bucket =
static_cast<size_t>((
x - min_val) * factor);
315 if (chi_square > kThreshold) {
321 for (
const auto&
a : counts) {
333 TYPED_TEST(UniformRealDistributionTest, StabilityTest) {
340 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
341 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
342 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
343 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
345 std::vector<int>
output(12);
347 DistributionType dist;
349 return static_cast<int>(
real_type(1000000) * dist(urbg));
355 77341, 12527, 708791, 834451, 932808));
358 TEST(UniformRealDistributionTest, AlgorithmBounds) {
364 double a = dist(urbg);
371 double a = dist(urbg);
377 double a = dist(urbg);
384 double a = dist(urbg);
390 double a = dist(urbg);