15 #include "absl/random/uniform_int_distribution.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "absl/base/internal/raw_logging.h"
27 #include "absl/random/internal/chi_square.h"
28 #include "absl/random/internal/distribution_test_util.h"
29 #include "absl/random/internal/pcg_engine.h"
30 #include "absl/random/internal/sequence_urbg.h"
31 #include "absl/random/random.h"
32 #include "absl/strings/str_cat.h"
36 template <
typename IntType>
43 TYPED_TEST(UniformIntDistributionTest, ParamSerializeTest) {
46 using Limits = std::numeric_limits<TypeParam>;
52 constexpr
int kCount = 1000;
54 for (
const auto& param : {
58 param_type(kMin, 115),
64 const auto a = param.a();
65 const auto b = param.b();
96 auto sample_min =
after.max();
97 auto sample_max =
after.min();
98 for (
int i = 0;
i < kCount;
i++) {
102 if (sample > sample_max) {
105 if (sample < sample_min) {
114 TYPED_TEST(UniformIntDistributionTest, ViolatesPreconditionsDeathTest) {
115 #if GTEST_HAS_DEATH_TEST
119 #endif // GTEST_HAS_DEATH_TEST
132 TYPED_TEST(UniformIntDistributionTest, TestMoments) {
133 constexpr
int kSize = 100000;
134 using Limits = std::numeric_limits<TypeParam>;
144 for (
const auto& param :
145 {param_type(0,
Limits::max()), param_type(13, 127)}) {
148 const auto sample = dist(rng);
155 const double a = dist.param().a();
156 const double b = dist.param().b();
157 const double n = (
b -
a + 1);
158 const double mean = (
a +
b) / 2;
159 const double var = ((
b -
a + 1) * (
b -
a + 1) - 1) / 12;
160 const double kurtosis = 3 - 6 * (
n *
n + 1) / (5 * (
n *
n - 1));
167 EXPECT_NEAR(kurtosis, moments.kurtosis, 0.02 * kurtosis);
171 TYPED_TEST(UniformIntDistributionTest, ChiSquaredTest50) {
174 constexpr
size_t kTrials = 1000;
175 constexpr
int kBuckets = 50;
176 constexpr
double kExpected =
177 static_cast<double>(kTrials) /
static_cast<double>(kBuckets);
180 const int kThreshold =
184 const TypeParam
max =
min + kBuckets;
193 std::vector<int32_t> counts(kBuckets + 1, 0);
194 for (
size_t i = 0;
i < kTrials;
i++) {
200 if (chi_square > kThreshold) {
206 for (
const auto&
a : counts) {
217 TEST(UniformIntDistributionTest, StabilityTest) {
220 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
221 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
222 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
223 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
225 std::vector<int>
output(12);
234 EXPECT_THAT(
output,
testing::ElementsAre(4, 4, 3, 2, 1, 0, 1, 4, 3, 1, 3, 1));
244 EXPECT_THAT(
output,
testing::ElementsAre(97, 86, 75, 41, 36, 16, 38, 92, 67,
256 3813, 9195, 6641, 2986, 7956, 3765));