• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <random>
18 #include <vector>
19 
20 #include "gtest/gtest.h"
21 #include "absl/random/distributions.h"
22 #include "absl/random/random.h"
23 
24 namespace {
25 
26 template <typename URBG>
TestUniform(URBG * gen)27 void TestUniform(URBG* gen) {
28   // [a, b) default-semantics, inferred types.
29   absl::Uniform(*gen, 0, 100);     // int
30   absl::Uniform(*gen, 0, 1.0);     // Promoted to double
31   absl::Uniform(*gen, 0.0f, 1.0);  // Promoted to double
32   absl::Uniform(*gen, 0.0, 1.0);   // double
33   absl::Uniform(*gen, -1, 1L);     // Promoted to long
34 
35   // Roll a die.
36   absl::Uniform(absl::IntervalClosedClosed, *gen, 1, 6);
37 
38   // Get a fraction.
39   absl::Uniform(absl::IntervalOpenOpen, *gen, 0.0, 1.0);
40 
41   // Assign a value to a random element.
42   std::vector<int> elems = {10, 20, 30, 40, 50};
43   elems[absl::Uniform(*gen, 0u, elems.size())] = 5;
44   elems[absl::Uniform<size_t>(*gen, 0, elems.size())] = 3;
45 
46   // Choose some epsilon around zero.
47   absl::Uniform(absl::IntervalOpenOpen, *gen, -1.0, 1.0);
48 
49   // (a, b) semantics, inferred types.
50   absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 1.0);  // Promoted to double
51 
52   // Explict overriding of types.
53   absl::Uniform<int>(*gen, 0, 100);
54   absl::Uniform<int8_t>(*gen, 0, 100);
55   absl::Uniform<int16_t>(*gen, 0, 100);
56   absl::Uniform<uint16_t>(*gen, 0, 100);
57   absl::Uniform<int32_t>(*gen, 0, 1 << 10);
58   absl::Uniform<uint32_t>(*gen, 0, 1 << 10);
59   absl::Uniform<int64_t>(*gen, 0, 1 << 10);
60   absl::Uniform<uint64_t>(*gen, 0, 1 << 10);
61 
62   absl::Uniform<float>(*gen, 0.0, 1.0);
63   absl::Uniform<float>(*gen, 0, 1);
64   absl::Uniform<float>(*gen, -1, 1);
65   absl::Uniform<double>(*gen, 0.0, 1.0);
66 
67   absl::Uniform<float>(*gen, -1.0, 0);
68   absl::Uniform<double>(*gen, -1.0, 0);
69 
70   // Tagged
71   absl::Uniform<double>(absl::IntervalClosedClosed, *gen, 0, 1);
72   absl::Uniform<double>(absl::IntervalClosedOpen, *gen, 0, 1);
73   absl::Uniform<double>(absl::IntervalOpenOpen, *gen, 0, 1);
74   absl::Uniform<double>(absl::IntervalOpenClosed, *gen, 0, 1);
75   absl::Uniform<double>(absl::IntervalClosedClosed, *gen, 0, 1);
76   absl::Uniform<double>(absl::IntervalOpenOpen, *gen, 0, 1);
77 
78   absl::Uniform<int>(absl::IntervalClosedClosed, *gen, 0, 100);
79   absl::Uniform<int>(absl::IntervalClosedOpen, *gen, 0, 100);
80   absl::Uniform<int>(absl::IntervalOpenOpen, *gen, 0, 100);
81   absl::Uniform<int>(absl::IntervalOpenClosed, *gen, 0, 100);
82   absl::Uniform<int>(absl::IntervalClosedClosed, *gen, 0, 100);
83   absl::Uniform<int>(absl::IntervalOpenOpen, *gen, 0, 100);
84 
85   // With *generator as an R-value reference.
86   absl::Uniform<int>(URBG(), 0, 100);
87   absl::Uniform<double>(URBG(), 0.0, 1.0);
88 }
89 
90 template <typename URBG>
TestExponential(URBG * gen)91 void TestExponential(URBG* gen) {
92   absl::Exponential<float>(*gen);
93   absl::Exponential<double>(*gen);
94   absl::Exponential<double>(URBG());
95 }
96 
97 template <typename URBG>
TestPoisson(URBG * gen)98 void TestPoisson(URBG* gen) {
99   // [rand.dist.pois] Indicates that the std::poisson_distribution
100   // is parameterized by IntType, however MSVC does not allow 8-bit
101   // types.
102   absl::Poisson<int>(*gen);
103   absl::Poisson<int16_t>(*gen);
104   absl::Poisson<uint16_t>(*gen);
105   absl::Poisson<int32_t>(*gen);
106   absl::Poisson<uint32_t>(*gen);
107   absl::Poisson<int64_t>(*gen);
108   absl::Poisson<uint64_t>(*gen);
109   absl::Poisson<uint64_t>(URBG());
110 }
111 
112 template <typename URBG>
TestBernoulli(URBG * gen)113 void TestBernoulli(URBG* gen) {
114   absl::Bernoulli(*gen, 0.5);
115   absl::Bernoulli(*gen, 0.5);
116 }
117 
118 template <typename URBG>
TestZipf(URBG * gen)119 void TestZipf(URBG* gen) {
120   absl::Zipf<int>(*gen, 100);
121   absl::Zipf<int8_t>(*gen, 100);
122   absl::Zipf<int16_t>(*gen, 100);
123   absl::Zipf<uint16_t>(*gen, 100);
124   absl::Zipf<int32_t>(*gen, 1 << 10);
125   absl::Zipf<uint32_t>(*gen, 1 << 10);
126   absl::Zipf<int64_t>(*gen, 1 << 10);
127   absl::Zipf<uint64_t>(*gen, 1 << 10);
128   absl::Zipf<uint64_t>(URBG(), 1 << 10);
129 }
130 
131 template <typename URBG>
TestGaussian(URBG * gen)132 void TestGaussian(URBG* gen) {
133   absl::Gaussian<float>(*gen, 1.0, 1.0);
134   absl::Gaussian<double>(*gen, 1.0, 1.0);
135   absl::Gaussian<double>(URBG(), 1.0, 1.0);
136 }
137 
138 template <typename URBG>
TestLogNormal(URBG * gen)139 void TestLogNormal(URBG* gen) {
140   absl::LogUniform<int>(*gen, 0, 100);
141   absl::LogUniform<int8_t>(*gen, 0, 100);
142   absl::LogUniform<int16_t>(*gen, 0, 100);
143   absl::LogUniform<uint16_t>(*gen, 0, 100);
144   absl::LogUniform<int32_t>(*gen, 0, 1 << 10);
145   absl::LogUniform<uint32_t>(*gen, 0, 1 << 10);
146   absl::LogUniform<int64_t>(*gen, 0, 1 << 10);
147   absl::LogUniform<uint64_t>(*gen, 0, 1 << 10);
148   absl::LogUniform<uint64_t>(URBG(), 0, 1 << 10);
149 }
150 
151 template <typename URBG>
CompatibilityTest()152 void CompatibilityTest() {
153   URBG gen;
154 
155   TestUniform(&gen);
156   TestExponential(&gen);
157   TestPoisson(&gen);
158   TestBernoulli(&gen);
159   TestZipf(&gen);
160   TestGaussian(&gen);
161   TestLogNormal(&gen);
162 }
163 
TEST(std_mt19937_64,Compatibility)164 TEST(std_mt19937_64, Compatibility) {
165   // Validate with std::mt19937_64
166   CompatibilityTest<std::mt19937_64>();
167 }
168 
TEST(BitGen,Compatibility)169 TEST(BitGen, Compatibility) {
170   // Validate with absl::BitGen
171   CompatibilityTest<absl::BitGen>();
172 }
173 
TEST(InsecureBitGen,Compatibility)174 TEST(InsecureBitGen, Compatibility) {
175   // Validate with absl::InsecureBitGen
176   CompatibilityTest<absl::InsecureBitGen>();
177 }
178 
179 }  // namespace
180