This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
randen_test.cc
317 lines (272 loc) · 9.2 KB
/
randen_test.cc
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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
//
// http://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 "randen.h"
#include <stdio.h>
#include <algorithm>
#include <random> // seed_seq
#include <sstream>
#define UPDATE_GOLDEN 0
#define ENABLE_VERIFY 1
#define ENABLE_DUMP 0
namespace randen {
namespace {
#define STR(x) #x
#define ASSERT_TRUE(condition) \
do { \
if (!(condition)) { \
printf("Assertion [" STR(condition) "] failed on line %d\n", __LINE__); \
abort(); \
} \
} while (false)
using EngRanden = Randen<uint64_t>;
#if ENABLE_VERIFY
void VerifyReseedChangesAllValues() {
const size_t kNumOutputs = 127;
EngRanden engine;
std::seed_seq seq1{1, 2, 3, 4, 5, 6, 7};
engine.seed(seq1);
uint64_t out1[kNumOutputs];
for (size_t i = 0; i < kNumOutputs; ++i) {
out1[i] = engine();
}
std::seed_seq seq2{127, 255, 511};
engine.seed(seq2);
uint64_t out2[kNumOutputs];
engine.seed(seq2);
for (size_t i = 0; i < kNumOutputs; ++i) {
out2[i] = engine();
ASSERT_TRUE(out2[i] != out1[i]);
}
}
void VerifyDiscard() {
const int N = 56; // two buffer's worth
for (int num_used = 0; num_used < N; ++num_used) {
EngRanden engine_used;
for (int i = 0; i < num_used; ++i) {
(void)engine_used();
}
for (int num_discard = 0; num_discard < N; ++num_discard) {
EngRanden engine1 = engine_used;
EngRanden engine2 = engine_used;
for (int i = 0; i < num_discard; ++i) {
(void)engine1();
}
engine2.discard(num_discard);
for (int i = 0; i < N; ++i) {
const uint64_t r1 = engine1();
const uint64_t r2 = engine2();
ASSERT_TRUE(r1 == r2);
}
}
}
}
void VerifyGolden() {
// prime number => some buffer values unused.
const size_t kNumOutputs = 127;
#if UPDATE_GOLDEN
EngRanden engine;
for (size_t i = 0; i < kNumOutputs; ++i) {
printf("0x%016lx,\n", engine());
}
printf("\n");
#else
const uint64_t golden[kNumOutputs] = {
0xdda9f47cd90410ee, 0xc3c14f134e433977, 0xf0b780f545c72912,
0x887bf3087fd8ca10, 0x30ec63baff3c6d59, 0x15dbb1d37696599f,
0x02808a316f49a54c, 0xb29f73606f7f20a6, 0x9cbf605e3fd9de8a,
0x3b8feaf9d5c8e50e, 0xd8b2ffd356301ed5, 0xc970ae1a78183bbb,
0xcdfd8d76eb8f9a19, 0xf4b327fe0fc73c37, 0xd5af05dd3eff9556,
0xc3a506eb91420c9d, 0x7023920e0d6bfe8c, 0x48db1bb78f83c4a1,
0xed1ef4c26b87b840, 0x58d3575834956d42, 0x497cabf3431154fc,
0x8eef32a23e0b2df3, 0xd88b5749f090e5ea, 0x4e24370570029a8b,
0x78fcec2cbb6342f5, 0xc651a582a970692f, 0x352ee4ad1816afe3,
0x463cb745612f55db, 0x811ef0821c3de851, 0x026ff374c101da7e,
0xa0660379992d58fc, 0x6f7e616704c4fa59, 0x915f3445685da798,
0x04b0a374a3b795c7, 0x4663352533ce1882, 0x26802a8ac76571ce,
0x5588ba3a4d6e6c51, 0xb9fdefb4a24dc738, 0x607195a5e200f5fd,
0xa2101a42d35f1956, 0xe1e5e03c759c0709, 0x7e100308f3290764,
0xcbcf585399e432f1, 0x082572cc5da6606f, 0x0904469acbfee8f2,
0xe8a2be4f8335d8f1, 0x08e8a1f1a69da69a, 0xf08bd31b6daecd51,
0x2e9705bb053d6b46, 0x6542a20aad57bff5, 0x78e3a810213b6ffb,
0xda2fc9db0713c391, 0xc0932718cd55781f, 0xdc16a59cdd85f8a6,
0xb97289c1be0f2f9c, 0xb9bfb29c2b20bfe5, 0x5524bb834771435b,
0xc0a2a0e403a892d4, 0xff4af3ab8d1b78c5, 0x8265da3d39d1a750,
0x66e455f627495189, 0xf0ec5f424bcad77f, 0x3424e47dc22596e3,
0xc82d3120b57e3270, 0xc191c595afc4dcbf, 0xbc0c95129ccedcdd,
0x7f90650ea6cd6ab4, 0x120392bd2bb70939, 0xa7c8fac5a7917eb0,
0x7287491832695ad3, 0x7c1bf9839c7c1ce5, 0xd088cb9418be0361,
0x78565cdefd28c4ad, 0xe2e991fa58e1e79e, 0x2a9eac28b08c96bf,
0x7351b9fef98bafad, 0x13a685861bab87e0, 0x6c4f179696cb2225,
0x30537425cac70991, 0x64c6de5aa0501971, 0x7e05e3aa8ec720dc,
0x01590d9dc6c532b7, 0x738184388f3bc1d2, 0x74a07d9c54e3e63f,
0x6bcdf185561f255f, 0x26ffdc5067be3acb, 0x171df81934f68604,
0xa0eaf2e1cf99b1c6, 0x5d1cb02075ba1cea, 0x7ea5a21665683e5a,
0xba6364eff80de02f, 0x957f38cbd2123fdf, 0x892d8317de82f7a2,
0x606e0a0e41d452ee, 0x4eb28826766fcf5b, 0xe707b1db50f7b43e,
0x6ee217df16527d78, 0x5a362d56e80a0951, 0x443e63857d4076ca,
0xf6737962ba6b23dd, 0xd796b052151ee94d, 0x790d9a5f048adfeb,
0x8b833ff84893da5d, 0x033ed95c12b04a03, 0x9877c4225061ca76,
0x3d6724b1bb15eab9, 0x42e5352fe30ce989, 0xd68d6810adf74fb3,
0x3cdbf7e358df4b8b, 0x265b565a7431fde7, 0x52d2242f65b37f88,
0x2922a47f6d3e8779, 0x29d40f00566d5e26, 0x5d836d6e2958d6b5,
0x6c056608b7d9c1b6, 0x288db0e1124b14a0, 0x8fb946504faa6c9d,
0x0b9471bdb8f19d32, 0xfd1fe27d144a09e0, 0x8943a9464540251c,
0x8048f217633fce36, 0xea6ac458da141bda, 0x4334b8b02ff7612f,
0xfeda1384ade74d31, 0x096d119a3605c85b, 0xdbc8441f5227e216,
0x541ad7efa6ddc1d3};
EngRanden engine;
for (size_t i = 0; i < kNumOutputs; ++i) {
ASSERT_TRUE(golden[i] == engine());
}
#endif
}
#endif // ENABLE_VERIFY
void VerifyRandReqEngine() {
// Validates that Randen satisfies [rand.req.engine].
// Names after definition of [rand.req.engine] in C++ standard.
// e is a value of E
// v is a lvalue of E
// x, y are possibly const values of E
// s is a value of T
// q is a value satisfying requirements of seed_sequence
// z is a value of type unsigned long long
// os is a some specialization of basic_ostream
// is is a some specialization of basic_istream
using E = EngRanden;
using T = typename EngRanden::result_type;
static_assert(std::is_copy_constructible<E>::value,
"Randen must be copy constructible");
static_assert(std::is_copy_assignable<E>::value,
"Randen must be copy assignable");
E e, v;
const E x, y;
T s = 1;
std::seed_seq q{1, 2, 3};
unsigned long long z = 1; // NOLINT(runtime/int)
std::wostringstream os;
std::wistringstream is;
E{};
E{x};
E{s};
E{q};
// Verify that seed() and default-construct is identical.
e.seed();
{
E f;
ASSERT_TRUE(e == f);
}
// Verify the seed() result type.
static_assert(std::is_same<decltype(e.seed(s)), void>::value,
"return type of seed() must be void");
static_assert(std::is_same<decltype(e.seed(q)), void>::value,
"return type of seed() must be void");
// verify that seed via seed_sequence and construct via seed_sequence
// is identical.
e.seed(q);
{
E f{q};
ASSERT_TRUE(e == f);
}
// Verify the operator() result type.
static_assert(std::is_same<decltype(e()), T>::value,
"return type of operator() must be result_type");
// Verify that once the state has advanced that the engines
// are no longer equal.
e();
{
E f{q};
ASSERT_TRUE(e != f);
}
{
E f;
ASSERT_TRUE(e != f);
}
// Verify discard.
e.discard(z);
{
// The state equivalence should change.
E f, g;
f.discard(2);
ASSERT_TRUE(f != g);
g();
g();
ASSERT_TRUE(f == g);
}
// Verify operator == result types.
static_assert(std::is_same<decltype(x == y), bool>::value,
"return type of operator== must be bool");
static_assert(std::is_same<decltype(x != y), bool>::value,
"return type of operator!= must be bool");
// Verify operator<<() result.
{
auto& os2 = (os << e);
ASSERT_TRUE(&os2 == &os);
}
// Verify operator>>() result.
{
auto& is2 = (is >> e);
ASSERT_TRUE(&is2 == &is);
}
}
void VerifyStreamOperators() {
EngRanden engine1(171);
EngRanden engine2;
{
std::stringstream stream;
stream << engine1;
stream >> engine2;
}
const int N = 56; // two buffer's worth
for (int i = 0; i < N; ++i) {
const uint64_t r1 = engine1();
const uint64_t r2 = engine2();
ASSERT_TRUE(r1 == r2);
}
}
void Verify() {
#if ENABLE_VERIFY
VerifyReseedChangesAllValues();
VerifyDiscard();
VerifyGolden();
VerifyRandReqEngine();
VerifyStreamOperators();
#endif
}
void DumpOutput() {
#if ENABLE_DUMP
const size_t kNumOutputs = 1500 * 1000 * 1000;
std::vector<uint64_t> outputs(kNumOutputs);
EngRanden engine;
for (size_t i = 0; i < kNumOutputs; ++i) {
outputs[i] = engine();
}
FILE* f = fopen("/tmp/randen.bin", "wb");
if (f != nullptr) {
fwrite(outputs.data(), kNumOutputs, 8, f);
fclose(f);
}
#endif // ENABLE_DUMP
}
void RunAll() {
// Immediately output any results (for non-local runs).
setvbuf(stdout, nullptr, _IONBF, 0);
Verify();
DumpOutput();
}
} // namespace
} // namespace randen
int main(int argc, char* argv[]) {
randen::RunAll();
return 0;
}