15 #include "absl/random/distributions.h"
22 #include "gtest/gtest.h"
23 #include "absl/random/internal/distribution_test_util.h"
24 #include "absl/random/random.h"
28 constexpr
int kSize = 400000;
35 template <
typename A,
typename B>
36 auto InferredUniformReturnT(
int)
37 -> decltype(
absl::Uniform(std::declval<absl::InsecureBitGen&>(),
38 std::declval<A>(), std::declval<B>()));
40 template <
typename,
typename>
41 Invalid InferredUniformReturnT(...);
43 template <
typename TagType,
typename A,
typename B>
44 auto InferredTaggedUniformReturnT(
int)
46 std::declval<absl::InsecureBitGen&>(),
47 std::declval<A>(), std::declval<B>()));
49 template <
typename,
typename,
typename>
50 Invalid InferredTaggedUniformReturnT(...);
76 template <
typename A,
typename B,
typename Expect>
77 void CheckArgsInferType() {
80 std::is_same<
Expect, decltype(InferredUniformReturnT<A, B>(0))>,
82 decltype(InferredUniformReturnT<B, A>(0))>>::
value,
86 std::is_same<
Expect, decltype(InferredTaggedUniformReturnT<
89 decltype(InferredTaggedUniformReturnT<
94 template <
typename A,
typename B,
typename ExplicitRet>
95 auto ExplicitUniformReturnT(
int) -> decltype(
96 absl::Uniform<ExplicitRet>(*std::declval<absl::InsecureBitGen*>(),
97 std::declval<A>(), std::declval<B>()));
99 template <
typename,
typename,
typename ExplicitRet>
100 Invalid ExplicitUniformReturnT(...);
102 template <
typename TagType,
typename A,
typename B,
typename ExplicitRet>
103 auto ExplicitTaggedUniformReturnT(
int) -> decltype(absl::Uniform<ExplicitRet>(
104 std::declval<TagType>(), *std::declval<absl::InsecureBitGen*>(),
105 std::declval<A>(), std::declval<B>()));
107 template <
typename,
typename,
typename,
typename ExplicitRet>
108 Invalid ExplicitTaggedUniformReturnT(...);
117 template <
typename A,
typename B,
typename Expect>
118 void CheckArgsReturnExpectedType() {
122 decltype(ExplicitUniformReturnT<A, B, Expect>(0))>,
123 std::is_same<
Expect, decltype(ExplicitUniformReturnT<B, A, Expect>(
129 decltype(ExplicitTaggedUniformReturnT<
131 std::is_same<
Expect, decltype(ExplicitTaggedUniformReturnT<
137 TEST_F(RandomDistributionsTest, UniformTypeInference) {
139 CheckArgsInferType<uint16_t, uint16_t, uint16_t>();
140 CheckArgsInferType<uint32_t, uint32_t, uint32_t>();
141 CheckArgsInferType<uint64_t, uint64_t, uint64_t>();
142 CheckArgsInferType<int16_t, int16_t, int16_t>();
143 CheckArgsInferType<int32_t, int32_t, int32_t>();
144 CheckArgsInferType<int64_t, int64_t, int64_t>();
145 CheckArgsInferType<float, float, float>();
146 CheckArgsInferType<double, double, double>();
149 CheckArgsReturnExpectedType<int16_t, int16_t, int32_t>();
150 CheckArgsReturnExpectedType<uint16_t, uint16_t, int32_t>();
151 CheckArgsReturnExpectedType<int16_t, int16_t, int64_t>();
152 CheckArgsReturnExpectedType<int16_t, int32_t, int64_t>();
153 CheckArgsReturnExpectedType<int16_t, int32_t, double>();
154 CheckArgsReturnExpectedType<float, float, double>();
155 CheckArgsReturnExpectedType<int, int, int16_t>();
158 CheckArgsInferType<uint16_t, uint32_t, uint32_t>();
159 CheckArgsInferType<uint16_t, uint64_t, uint64_t>();
160 CheckArgsInferType<uint16_t, int32_t, int32_t>();
161 CheckArgsInferType<uint16_t, int64_t, int64_t>();
162 CheckArgsInferType<uint16_t, float, float>();
163 CheckArgsInferType<uint16_t, double, double>();
166 CheckArgsInferType<int16_t, int32_t, int32_t>();
167 CheckArgsInferType<int16_t, int64_t, int64_t>();
168 CheckArgsInferType<int16_t, float, float>();
169 CheckArgsInferType<int16_t, double, double>();
173 CheckArgsInferType<uint16_t, int16_t, Invalid>();
174 CheckArgsInferType<int16_t, uint32_t, Invalid>();
175 CheckArgsInferType<int16_t, uint64_t, Invalid>();
178 CheckArgsInferType<uint32_t, uint64_t, uint64_t>();
179 CheckArgsInferType<uint32_t, int64_t, int64_t>();
180 CheckArgsInferType<uint32_t, double, double>();
183 CheckArgsInferType<int32_t, int64_t, int64_t>();
184 CheckArgsInferType<int32_t, double, double>();
187 CheckArgsInferType<uint32_t, int32_t, Invalid>();
188 CheckArgsInferType<int32_t, uint64_t, Invalid>();
189 CheckArgsInferType<int32_t, float, Invalid>();
190 CheckArgsInferType<uint32_t, float, Invalid>();
193 CheckArgsInferType<uint64_t, int64_t, Invalid>();
194 CheckArgsInferType<int64_t, float, Invalid>();
195 CheckArgsInferType<int64_t, double, Invalid>();
198 CheckArgsInferType<float, double, double>();
201 TEST_F(RandomDistributionsTest, UniformExamples) {
210 EXPECT_NE(1, absl::Uniform<double>(absl::IntervalOpenOpen,
gen, -1, 1));
211 EXPECT_NE(1, absl::Uniform<float>(absl::IntervalOpenOpen,
gen, 0, 1));
215 TEST_F(RandomDistributionsTest, UniformNoBounds) {
218 absl::Uniform<uint8_t>(
gen);
219 absl::Uniform<uint16_t>(
gen);
220 absl::Uniform<uint32_t>(
gen);
221 absl::Uniform<uint64_t>(
gen);
224 TEST_F(RandomDistributionsTest, UniformNonsenseRanges) {
232 EXPECT_EQ(0, absl::Uniform<uint64_t>(absl::IntervalOpenOpen,
gen, 0, 0));
233 EXPECT_EQ(1, absl::Uniform<uint64_t>(absl::IntervalOpenOpen,
gen, 1, 0));
247 EXPECT_EQ(0, absl::Uniform<int64_t>(absl::IntervalOpenOpen,
gen, 0, 0));
248 EXPECT_EQ(1, absl::Uniform<int64_t>(absl::IntervalOpenOpen,
gen, 1, 0));
263 const double e = std::nextafter(1.0, 2.0);
264 const double f = std::nextafter(1.0, 0.0);
265 const double g = std::numeric_limits<double>::denorm_min();
277 TEST_F(RandomDistributionsTest, UniformReal) {
293 TEST_F(RandomDistributionsTest, UniformInt) {
301 values[
i] =
static_cast<double>(
j) /
static_cast<double>(kMax);
341 TEST_F(RandomDistributionsTest, PoissonDefault) {
357 TEST_F(RandomDistributionsTest, PoissonLarge) {
358 constexpr
double kMean = 100000000.0;
363 values[
i] = absl::Poisson<int64_t>(
gen, kMean);
369 EXPECT_NEAR(kMean, moments.variance, kMean * 0.015);
370 EXPECT_NEAR(std::sqrt(kMean), moments.skewness, kMean * 0.02);
375 constexpr
double kP = 0.5151515151;
389 constexpr
double kAlpha = 2.0;
390 constexpr
double kBeta = 3.0;
416 EXPECT_NEAR(6.5944, moments.mean, 2000) << moments;
440 values[
i] = absl::LogUniform<int64_t>(
gen, 0, (1 << 10) - 1);
446 const double mean = (0 + 1 + 1 + 2 + 3 + 4 + 7 + 8 + 15 + 16 + 31 + 32 + 63 +
447 64 + 127 + 128 + 255 + 256 + 511 + 512 + 1023) /