15 #include "absl/random/discrete_distribution.h"
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 #include "absl/base/internal/raw_logging.h"
30 #include "absl/random/internal/chi_square.h"
31 #include "absl/random/internal/distribution_test_util.h"
32 #include "absl/random/internal/pcg_engine.h"
33 #include "absl/random/internal/sequence_urbg.h"
34 #include "absl/random/random.h"
35 #include "absl/strings/str_cat.h"
36 #include "absl/strings/strip.h"
40 template <
typename IntType>
47 TYPED_TEST(DiscreteDistributionTypeTest, ParamSerializeTest) {
59 for (
const auto&
x :
before.probabilities()) {
67 std::vector<double>
data({1.0, 2.0, 1.0});
86 TYPED_TEST(DiscreteDistributionTypeTest, Constructor) {
87 auto fn = [](
double x) {
return x; };
106 std::vector<double>
p({1.0, 2.0, 3.0});
107 std::vector<std::pair<double, size_t>>
q =
120 std::vector<double>
p({1.0, 2.0, 3.0, 5.0, 2.0});
122 std::vector<std::pair<double, size_t>>
q =
133 constexpr
double b0 = 1.0 / 13.0 / 0.2;
134 constexpr
double b1 = 2.0 / 13.0 / 0.2;
135 constexpr
double b3 = (5.0 / 13.0 / 0.2) - ((1 - b0) + (1 -
b1) + (1 -
b1));
145 TEST(DiscreteDistributionTest, ChiSquaredTest50) {
148 constexpr
size_t kTrials = 10000;
149 constexpr
int kBuckets = 50;
154 const int kThreshold =
157 std::vector<double>
weights(kBuckets, 0);
166 std::vector<int32_t> counts(kBuckets, 0);
167 for (
size_t i = 0;
i < kTrials;
i++) {
178 x = kTrials * (
x /
sum);
185 if (chi_square > kThreshold) {
191 for (
size_t i = 0;
i < counts.size();
i++) {
202 TEST(DiscreteDistributionTest, StabilityTest) {
206 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
207 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
208 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
209 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
211 std::vector<int>
output(6);