blob: 698027fc6e0dd798ad695c03f59412da446494fb [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
Austin Schuhb4691e92020-12-31 12:37:18 -0800157 // Cast to void to suppress [[nodiscard]] warnings
158 static_cast<void>(std::uniform_int_distribution<uint32_t>(0, 100)(rbg));
159 static_cast<void>(std::uniform_real_distribution<float>()(rbg));
160 static_cast<void>(std::bernoulli_distribution(0.2)(rbg));
Austin Schuh36244a12019-09-21 17:52:38 -0700161}
162
163TEST(NonsecureURBGBase, ConsecutiveDefaultInstancesYieldUniqueVariates) {
164 const size_t kNumSamples = 128;
165
166 ExampleNonsecureURBG rbg1;
167 ExampleNonsecureURBG rbg2;
168
169 for (size_t i = 0; i < kNumSamples; i++) {
170 EXPECT_NE(rbg1(), rbg2());
171 }
172}
173
174TEST(NonsecureURBGBase, EqualSeedSequencesYieldEqualVariates) {
175 std::seed_seq seq;
176
177 ExampleNonsecureURBG rbg1(seq);
178 ExampleNonsecureURBG rbg2(seq);
179
180 // ExampleNonsecureURBG rbg3({1, 2, 3}); // Should not compile.
181
182 for (uint32_t i = 0; i < 1000; i++) {
183 EXPECT_EQ(rbg1(), rbg2());
184 }
185
186 rbg1.discard(100);
187 rbg2.discard(100);
188
189 // The sequences should continue after discarding
190 for (uint32_t i = 0; i < 1000; i++) {
191 EXPECT_EQ(rbg1(), rbg2());
192 }
193}
194
195// This is a PRNG-compatible type specifically designed to test
196// that NonsecureURBGBase::Seeder can correctly handle iterators
197// to arbitrary non-uint32_t size types.
198template <typename T>
199struct SeederTestEngine {
200 using result_type = T;
201
202 static constexpr result_type(min)() {
203 return (std::numeric_limits<result_type>::min)();
204 }
205 static constexpr result_type(max)() {
206 return (std::numeric_limits<result_type>::max)();
207 }
208
209 template <class SeedSequence,
210 typename = typename absl::enable_if_t<
211 !std::is_same<SeedSequence, SeederTestEngine>::value>>
212 explicit SeederTestEngine(SeedSequence&& seq) {
213 seed(seq);
214 }
215
216 SeederTestEngine(const SeederTestEngine&) = default;
217 SeederTestEngine& operator=(const SeederTestEngine&) = default;
218 SeederTestEngine(SeederTestEngine&&) = default;
219 SeederTestEngine& operator=(SeederTestEngine&&) = default;
220
221 result_type operator()() { return state[0]; }
222
223 template <class SeedSequence>
224 void seed(SeedSequence&& seq) {
225 std::fill(std::begin(state), std::end(state), T(0));
226 seq.generate(std::begin(state), std::end(state));
227 }
228
229 T state[2];
230};
231
232TEST(NonsecureURBGBase, SeederWorksForU32) {
233 using U32 =
234 absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint32_t>>;
235 U32 x;
236 EXPECT_NE(0, x());
237}
238
239TEST(NonsecureURBGBase, SeederWorksForU64) {
240 using U64 =
241 absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint64_t>>;
242
243 U64 x;
244 EXPECT_NE(0, x());
245}