blob: d9de99019ce098f1d5cbf51e4211bbd9fae18463 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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 "absl/random/internal/nonsecure_base.h"
16
17#include <algorithm>
18#include <iostream>
19#include <memory>
20#include <random>
21#include <sstream>
22
23#include "gtest/gtest.h"
24#include "absl/random/distributions.h"
25#include "absl/random/random.h"
26#include "absl/strings/str_cat.h"
27
28namespace {
29
30using ExampleNonsecureURBG =
31 absl::random_internal::NonsecureURBGBase<std::mt19937>;
32
33template <typename T>
34void Use(const T&) {}
35
36} // namespace
37
38TEST(NonsecureURBGBase, DefaultConstructorIsValid) {
39 ExampleNonsecureURBG urbg;
40}
41
42// Ensure that the recommended template-instantiations are valid.
43TEST(RecommendedTemplates, CanBeConstructed) {
44 absl::BitGen default_generator;
45 absl::InsecureBitGen insecure_generator;
46}
47
48TEST(RecommendedTemplates, CanDiscardValues) {
49 absl::BitGen default_generator;
50 absl::InsecureBitGen insecure_generator;
51
52 default_generator.discard(5);
53 insecure_generator.discard(5);
54}
55
56TEST(NonsecureURBGBase, StandardInterface) {
57 // Names after definition of [rand.req.urbg] in C++ standard.
58 // e us a value of E
59 // v is a lvalue of E
60 // x, y are possibly const values of E
61 // s is a value of T
62 // q is a value satisfying requirements of seed_sequence
63 // z is a value of type unsigned long long
64 // os is a some specialization of basic_ostream
65 // is is a some specialization of basic_istream
66
67 using E = absl::random_internal::NonsecureURBGBase<std::minstd_rand>;
68
69 using T = typename E::result_type;
70
71 static_assert(!std::is_copy_constructible<E>::value,
72 "NonsecureURBGBase should not be copy constructible");
73
74 static_assert(!absl::is_copy_assignable<E>::value,
75 "NonsecureURBGBase should not be copy assignable");
76
77 static_assert(std::is_move_constructible<E>::value,
78 "NonsecureURBGBase should be move constructible");
79
80 static_assert(absl::is_move_assignable<E>::value,
81 "NonsecureURBGBase should be move assignable");
82
83 static_assert(std::is_same<decltype(std::declval<E>()()), T>::value,
84 "return type of operator() must be result_type");
85
86 {
87 const E x, y;
88 Use(x);
89 Use(y);
90
91 static_assert(std::is_same<decltype(x == y), bool>::value,
92 "return type of operator== must be bool");
93
94 static_assert(std::is_same<decltype(x != y), bool>::value,
95 "return type of operator== must be bool");
96 }
97
98 E e;
99 std::seed_seq q{1, 2, 3};
100
101 E{};
102 E{q};
103
104 // Copy constructor not supported.
105 // E{x};
106
107 // result_type seed constructor not supported.
108 // E{T{1}};
109
110 // Move constructors are supported.
111 {
112 E tmp(q);
113 E m = std::move(tmp);
114 E n(std::move(m));
115 EXPECT_TRUE(e != n);
116 }
117
118 // Comparisons work.
119 {
120 // MSVC emits error 2718 when using EXPECT_EQ(e, x)
121 // * actual parameter with __declspec(align('#')) won't be aligned
122 E a(q);
123 E b(q);
124
125 EXPECT_TRUE(a != e);
126 EXPECT_TRUE(a == b);
127
128 a();
129 EXPECT_TRUE(a != b);
130 }
131
132 // e.seed(s) not supported.
133
134 // [rand.req.eng] specifies the parameter as 'unsigned long long'
135 // e.discard(unsigned long long) is supported.
136 unsigned long long z = 1; // NOLINT(runtime/int)
137 e.discard(z);
138}
139
140TEST(NonsecureURBGBase, SeedSeqConstructorIsValid) {
141 std::seed_seq seq;
142 ExampleNonsecureURBG rbg(seq);
143}
144
145TEST(NonsecureURBGBase, CompatibleWithDistributionUtils) {
146 ExampleNonsecureURBG rbg;
147
148 absl::Uniform(rbg, 0, 100);
149 absl::Uniform(rbg, 0.5, 0.7);
150 absl::Poisson<uint32_t>(rbg);
151 absl::Exponential<float>(rbg);
152}
153
154TEST(NonsecureURBGBase, CompatibleWithStdDistributions) {
155 ExampleNonsecureURBG rbg;
156
157 std::uniform_int_distribution<uint32_t>(0, 100)(rbg);
158 std::uniform_real_distribution<float>()(rbg);
159 std::bernoulli_distribution(0.2)(rbg);
160}
161
162TEST(NonsecureURBGBase, ConsecutiveDefaultInstancesYieldUniqueVariates) {
163 const size_t kNumSamples = 128;
164
165 ExampleNonsecureURBG rbg1;
166 ExampleNonsecureURBG rbg2;
167
168 for (size_t i = 0; i < kNumSamples; i++) {
169 EXPECT_NE(rbg1(), rbg2());
170 }
171}
172
173TEST(NonsecureURBGBase, EqualSeedSequencesYieldEqualVariates) {
174 std::seed_seq seq;
175
176 ExampleNonsecureURBG rbg1(seq);
177 ExampleNonsecureURBG rbg2(seq);
178
179 // ExampleNonsecureURBG rbg3({1, 2, 3}); // Should not compile.
180
181 for (uint32_t i = 0; i < 1000; i++) {
182 EXPECT_EQ(rbg1(), rbg2());
183 }
184
185 rbg1.discard(100);
186 rbg2.discard(100);
187
188 // The sequences should continue after discarding
189 for (uint32_t i = 0; i < 1000; i++) {
190 EXPECT_EQ(rbg1(), rbg2());
191 }
192}
193
194// This is a PRNG-compatible type specifically designed to test
195// that NonsecureURBGBase::Seeder can correctly handle iterators
196// to arbitrary non-uint32_t size types.
197template <typename T>
198struct SeederTestEngine {
199 using result_type = T;
200
201 static constexpr result_type(min)() {
202 return (std::numeric_limits<result_type>::min)();
203 }
204 static constexpr result_type(max)() {
205 return (std::numeric_limits<result_type>::max)();
206 }
207
208 template <class SeedSequence,
209 typename = typename absl::enable_if_t<
210 !std::is_same<SeedSequence, SeederTestEngine>::value>>
211 explicit SeederTestEngine(SeedSequence&& seq) {
212 seed(seq);
213 }
214
215 SeederTestEngine(const SeederTestEngine&) = default;
216 SeederTestEngine& operator=(const SeederTestEngine&) = default;
217 SeederTestEngine(SeederTestEngine&&) = default;
218 SeederTestEngine& operator=(SeederTestEngine&&) = default;
219
220 result_type operator()() { return state[0]; }
221
222 template <class SeedSequence>
223 void seed(SeedSequence&& seq) {
224 std::fill(std::begin(state), std::end(state), T(0));
225 seq.generate(std::begin(state), std::end(state));
226 }
227
228 T state[2];
229};
230
231TEST(NonsecureURBGBase, SeederWorksForU32) {
232 using U32 =
233 absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint32_t>>;
234 U32 x;
235 EXPECT_NE(0, x());
236}
237
238TEST(NonsecureURBGBase, SeederWorksForU64) {
239 using U64 =
240 absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint64_t>>;
241
242 U64 x;
243 EXPECT_NE(0, x());
244}