summaryrefslogtreecommitdiff
path: root/absl/random/generators_test.cc
blob: 14fd24e9b692f0d280419bc0cf95f529ca79b9e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>
#include <cstdint>
#include <random>
#include <vector>

#include "gtest/gtest.h"
#include "absl/random/distributions.h"
#include "absl/random/random.h"

namespace {

template <typename URBG>
void TestUniform(URBG* gen) {
  // [a, b) default-semantics, inferred types.
  absl::Uniform(*gen, 0, 100);     // int
  absl::Uniform(*gen, 0, 1.0);     // Promoted to double
  absl::Uniform(*gen, 0.0f, 1.0);  // Promoted to double
  absl::Uniform(*gen, 0.0, 1.0);   // double
  absl::Uniform(*gen, -1, 1L);     // Promoted to long

  // Roll a die.
  absl::Uniform(absl::IntervalClosedClosed, *gen, 1, 6);

  // Get a fraction.
  absl::Uniform(absl::IntervalOpenOpen, *gen, 0.0, 1.0);

  // Assign a value to a random element.
  std::vector<int> elems = {10, 20, 30, 40, 50};
  elems[absl::Uniform(*gen, 0u, elems.size())] = 5;
  elems[absl::Uniform<size_t>(*gen, 0, elems.size())] = 3;

  // Choose some epsilon around zero.
  absl::Uniform(absl::IntervalOpenOpen, *gen, -1.0, 1.0);

  // (a, b) semantics, inferred types.
  absl::Uniform(absl::IntervalOpenOpen, *gen, 0, 1.0);  // Promoted to double

  // Explict overriding of types.
  absl::Uniform<int>(*gen, 0, 100);
  absl::Uniform<int8_t>(*gen, 0, 100);
  absl::Uniform<int16_t>(*gen, 0, 100);
  absl::Uniform<uint16_t>(*gen, 0, 100);
  absl::Uniform<int32_t>(*gen, 0, 1 << 10);
  absl::Uniform<uint32_t>(*gen, 0, 1 << 10);
  absl::Uniform<int64_t>(*gen, 0, 1 << 10);
  absl::Uniform<uint64_t>(*gen, 0, 1 << 10);

  absl::Uniform<float>(*gen, 0.0, 1.0);
  absl::Uniform<float>(*gen, 0, 1);
  absl::Uniform<float>(*gen, -1, 1);
  absl::Uniform<double>(*gen, 0.0, 1.0);

  absl::Uniform<float>(*gen, -1.0, 0);
  absl::Uniform<double>(*gen, -1.0, 0);

  // Tagged
  absl::Uniform<double>(absl::IntervalClosedClosed, *gen, 0, 1);
  absl::Uniform<double>(absl::IntervalClosedOpen, *gen, 0, 1);
  absl::Uniform<double>(absl::IntervalOpenOpen, *gen, 0, 1);
  absl::Uniform<double>(absl::IntervalOpenClosed, *gen, 0, 1);
  absl::Uniform<double>(absl::IntervalClosedClosed, *gen, 0, 1);
  absl::Uniform<double>(absl::IntervalOpenOpen, *gen, 0, 1);

  absl::Uniform<int>(absl::IntervalClosedClosed, *gen, 0, 100);
  absl::Uniform<int>(absl::IntervalClosedOpen, *gen, 0, 100);
  absl::Uniform<int>(absl::IntervalOpenOpen, *gen, 0, 100);
  absl::Uniform<int>(absl::IntervalOpenClosed, *gen, 0, 100);
  absl::Uniform<int>(absl::IntervalClosedClosed, *gen, 0, 100);
  absl::Uniform<int>(absl::IntervalOpenOpen, *gen, 0, 100);

  // With *generator as an R-value reference.
  absl::Uniform<int>(URBG(), 0, 100);
  absl::Uniform<double>(URBG(), 0.0, 1.0);
}

template <typename URBG>
void TestExponential(URBG* gen) {
  absl::Exponential<float>(*gen);
  absl::Exponential<double>(*gen);
  absl::Exponential<double>(URBG());
}

template <typename URBG>
void TestPoisson(URBG* gen) {
  // [rand.dist.pois] Indicates that the std::poisson_distribution
  // is parameterized by IntType, however MSVC does not allow 8-bit
  // types.
  absl::Poisson<int>(*gen);
  absl::Poisson<int16_t>(*gen);
  absl::Poisson<uint16_t>(*gen);
  absl::Poisson<int32_t>(*gen);
  absl::Poisson<uint32_t>(*gen);
  absl::Poisson<int64_t>(*gen);
  absl::Poisson<uint64_t>(*gen);
  absl::Poisson<uint64_t>(URBG());
  absl::Poisson<absl::int128>(*gen);
  absl::Poisson<absl::uint128>(*gen);
}

template <typename URBG>
void TestBernoulli(URBG* gen) {
  absl::Bernoulli(*gen, 0.5);
  absl::Bernoulli(*gen, 0.5);
}

template <typename URBG>
void TestZipf(URBG* gen) {
  absl::Zipf<int>(*gen, 100);
  absl::Zipf<int8_t>(*gen, 100);
  absl::Zipf<int16_t>(*gen, 100);
  absl::Zipf<uint16_t>(*gen, 100);
  absl::Zipf<int32_t>(*gen, 1 << 10);
  absl::Zipf<uint32_t>(*gen, 1 << 10);
  absl::Zipf<int64_t>(*gen, 1 << 10);
  absl::Zipf<uint64_t>(*gen, 1 << 10);
  absl::Zipf<uint64_t>(URBG(), 1 << 10);
  absl::Zipf<absl::int128>(*gen, 1 << 10);
  absl::Zipf<absl::uint128>(*gen, 1 << 10);
}

template <typename URBG>
void TestGaussian(URBG* gen) {
  absl::Gaussian<float>(*gen, 1.0, 1.0);
  absl::Gaussian<double>(*gen, 1.0, 1.0);
  absl::Gaussian<double>(URBG(), 1.0, 1.0);
}

template <typename URBG>
void TestLogNormal(URBG* gen) {
  absl::LogUniform<int>(*gen, 0, 100);
  absl::LogUniform<int8_t>(*gen, 0, 100);
  absl::LogUniform<int16_t>(*gen, 0, 100);
  absl::LogUniform<uint16_t>(*gen, 0, 100);
  absl::LogUniform<int32_t>(*gen, 0, 1 << 10);
  absl::LogUniform<uint32_t>(*gen, 0, 1 << 10);
  absl::LogUniform<int64_t>(*gen, 0, 1 << 10);
  absl::LogUniform<uint64_t>(*gen, 0, 1 << 10);
  absl::LogUniform<uint64_t>(URBG(), 0, 1 << 10);
  absl::LogUniform<absl::int128>(*gen, 0, 1 << 10);
  absl::LogUniform<absl::uint128>(*gen, 0, 1 << 10);
}

template <typename URBG>
void CompatibilityTest() {
  URBG gen;

  TestUniform(&gen);
  TestExponential(&gen);
  TestPoisson(&gen);
  TestBernoulli(&gen);
  TestZipf(&gen);
  TestGaussian(&gen);
  TestLogNormal(&gen);
}

TEST(std_mt19937_64, Compatibility) {
  // Validate with std::mt19937_64
  CompatibilityTest<std::mt19937_64>();
}

TEST(BitGen, Compatibility) {
  // Validate with absl::BitGen
  CompatibilityTest<absl::BitGen>();
}

TEST(InsecureBitGen, Compatibility) {
  // Validate with absl::InsecureBitGen
  CompatibilityTest<absl::InsecureBitGen>();
}

}  // namespace