-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
411 lines (362 loc) · 10.3 KB
/
tests.cpp
File metadata and controls
411 lines (362 loc) · 10.3 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Various tests and samples for entropy_converter.
#include "entropy_converter.hpp"
#include <random>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <numeric>
typedef long double LD;
// The expected maximum entropy loss from a conversion.
// Note that the entropy can exceed this but not on average.
template<typename T>
LD max_entropy_loss(T out, T in = 2)
{
LD p = LD(out)*LD(in) / LD(std::numeric_limits<T>::max()), q = 1.0 - p;
return (-p * std::log2(p) - q * std::log2(q)) / q;
}
// A slightly tighter bound on expected entropy loss.
// Assumes random targets which isn't quite true.
template<typename T>
LD expected_entropy_loss(T out, T in = 2)
{
LD limit = std::numeric_limits<T>::max();
LD k = limit + limit/in;
LD p = (k+2-out)/(k+1);
LD q = (out-1)/(k+1); // = 1-p
return (-p * std::log2(p) - q * std::log2(q)) / p;
}
// Loss if we never fail need to iterate
template<typename T>
LD best_entropy_loss(T out, T in = 2)
{
LD limit = std::numeric_limits<T>::max();
LD k = limit + limit / in;
return std::log2(LD(k + 1)) - std::log2(LD(k + 2 - out)); // -lg(p)
}
template<typename T>
LD min_efficiency(T out)
{
return std::log2(out) / (max_entropy_loss(out) + std::log2(out));
}
template<typename T>
LD max_shuffle_loss(T n)
{
LD loss = 0.0;
for (T i = 2; i <= n; ++i)
loss += max_entropy_loss(i);
return loss;
}
// How much entropy is required to shuffle a deck of size n?
LD shuffle_output_entropy(int n)
{
LD e = 0.0;
for (int i = 2; i <= n; ++i)
e += std::log2(i);
return e;
}
// What's the worst-case efficiency shuffling a deck of cards?
// This is quite a loose bound.
template<typename T>
LD shuffle_efficiency(T n)
{
auto se = shuffle_output_entropy(n);
return se / (se + max_shuffle_loss(n));
}
// How much entropy is stored inside the entropy converter?
// Use this mainly when measuring how efficient we are.
template<typename T>
LD buffered_entropy(const entropy_converter<T> & c)
{
return std::log2(c.get_buffered_range());
}
// A random device where we keep track of the quantity of entropy
// it has produced.
class MeasuringRandomDevice
{
public:
typedef unsigned result_type;
MeasuringRandomDevice() : count(0) { }
result_type operator()() { ++count; return d(); }
LD entropy() const { return LD(count*sizeof(result_type)*8); }
static std::size_t min() { return std::random_device::min(); }
static std::size_t max() { return std::random_device::max(); }
private:
std::random_device d;
unsigned count;
};
// Measures actual entropy consumed by a shuffle.
template<typename T>
void measure_shuffle()
{
int n = 10000;
int deck = 52;
entropy_converter<T> g;
MeasuringRandomDevice d;
// Measure actual entropy used generating n shuffles
LD outputEntropy = 0.0;
LD maxEntropyLoss = 0.0;
LD expectedEntropyLoss = 0.0;
LD bestEntropyLoss = 0.0;
for (int i = 0; i < n; ++i)
{
for (T t = 2; t <= deck; ++t)
{
g.convert(t, d);
outputEntropy += std::log2(LD(t));
maxEntropyLoss += max_entropy_loss(t);
expectedEntropyLoss += expected_entropy_loss(t);
bestEntropyLoss += best_entropy_loss(t);
}
}
LD inputEntropy = d.entropy() - buffered_entropy(g);
std::cout << std::setprecision(6);
std::cout
<< "| Shuffle " << deck
<< " | " << sizeof(T) * 8
<< " | " << bestEntropyLoss / n
<< " | " << expectedEntropyLoss / n
<< " | " << maxEntropyLoss / n
<< " | " << n
<< " | " << (inputEntropy - outputEntropy)/n
<< " | " << std::setprecision(15) << inputEntropy
<< " | " << outputEntropy
<< " |\n";
}
// Measure actual entropy used to convert numbers from one base to another.
template<typename T>
void measure_conversion(T from, T to)
{
entropy_converter<T> c1, c2;
std::random_device d;
int n = 10000;
LD inputEntropy = 0.0, outputEntropy = 0.0;
auto src = [&]() { inputEntropy += std::log2(from); return c1.convert(from, d); };
for (int i = 0; i < n; ++i)
{
c2.convert(T(1), to, T(0), T(from-1), src);
outputEntropy += std::log2(to);
}
inputEntropy -= buffered_entropy(c2);
auto loss = (inputEntropy - outputEntropy) / n;
if (loss < 0) loss = NAN;
std::cout << std::setprecision(6);
std::cout
<< "| Convert " << from << " to " << to
<< " | " << sizeof(T) * 8
<< " | " << best_entropy_loss(to,from)
<< " | " << expected_entropy_loss(to, from)
<< " | " << max_entropy_loss(to, from)
<< " | " << n
<< " | " << loss
<< " | " << std::setprecision(15) << inputEntropy
<< " | " << outputEntropy
<< " |\n";
}
// Measure how much entropy is lost in a random sequence of conversions.
template<typename T>
void measure_expected_entropy()
{
MeasuringRandomDevice d;
entropy_converter<T> c;
int n = 10000;
T t = 50, max = 1000, min = 5;
LD expectedLoss = 0, maxLoss = 0, bestLoss=0;
LD outputEntropy = 0;
for (int i = 0; i < n; ++i)
{
outputEntropy += std::log2(t);
expectedLoss += expected_entropy_loss(t);
maxLoss += max_entropy_loss(t);
bestLoss += best_entropy_loss(t);
auto r = c.convert(t, d);
t = 2 + r * 2;
if (t > max) t = max;
if (t < min) t = min;
}
auto inputEntropy = d.entropy() - buffered_entropy(c);
auto totalLoss = inputEntropy - outputEntropy;
if (totalLoss < 0) totalLoss = NAN;
std::cout << std::setprecision(6);
std::cout
<< "| Randomized sequence | " << sizeof(T) * 8
<< " | " << bestLoss
<< " | " << expectedLoss
<< " | " << maxLoss
<< " | " << n
<< " | " << totalLoss
<< " | " << std::setprecision(15) << inputEntropy
<< " | " << outputEntropy
<< " |\n";
}
void examples()
{
// Create a converter
entropy_converter<> c;
// Use a hardware random number generator
std::random_device d;
// Create a distribution
auto d6 = c.make_uniform(1, 6);
// Roll a die
std::cout << "You rolled a " << d6(d) << std::endl;
// Shuffle a deck of cards
{
entropy_converter<> c;
std::random_device d;
std::vector<int> cards(52);
std::iota(cards.begin(), cards.end(), 0);
std::random_shuffle(cards.begin(), cards.end(), c.with_generator(d));
for(auto card : cards)
std::cout << "A23456789TJQK"[card%13] << "SHCD"[card/13] << " ";
std::cout << std::endl;
}
}
// Ensure that a distribution is somewhat uniform.
// Test whether numbers are generated in correct proportion.
template<typename T>
void test_distribution_is_uniform(T range)
{
entropy_converter<T> c;
std::random_device d;
long double outputEntropy = 0.0;
std::vector<std::size_t> totals(range);
unsigned count = 0;
bool valid;
do
{
int n = 1000;
for (int i = 0; i < n; ++i)
{
auto x = c.convert(range, d);
assert(x >= 0 && x < range);
totals[x]++;
count++;
}
outputEntropy += n * std::log2((long double)range);
valid = true;
auto expectedCount = count / range;
for (T i = 0; i < range; ++i)
{
if (totals[i] < expectedCount * 9 / 10 || totals[i] > expectedCount * 11 / 10)
{
valid = false;
break;
}
}
} while (!valid);
}
// Check that entropy_converter uses the expected amount of entropy.
template<typename T>
void test_entropy_consumption(T target)
{
double loss = 0, expected = 0.01;
const int n = 1000;
do
{
MeasuringRandomDevice d;
entropy_converter<T> c;
for (int i = 0; i < n; ++i)
c.convert(target, d);
loss += d.entropy() - std::log2(c.get_buffered_range()) - n * std::log2(target);
expected += n * max_entropy_loss(target);
} while (loss > expected);
}
template<typename Fn>
void assert_throws(Fn fn)
{
try
{
fn();
assert(!"Expected exception not thrown");
}
catch (std::range_error)
{
}
}
void tests()
{
std::cout << "\nRunning tests\n";
std::random_device d;
// Constructors
entropy_converter<std::uint16_t> c16;
entropy_converter<std::uint32_t> c32;
entropy_converter<std::uint64_t> c64;
// Initial range = 1
assert(c16.get_buffered_range() == 1);
c16.convert(2, d);
assert(c16.get_buffered_range() > 1);
// Copying attempts
assert(c16.get_buffered_range() > 1);
auto c16b(std::move(c16));
assert(c16.get_buffered_range() == 1);
assert(c16b.get_buffered_range() > 1);
// Assignment
assert(c16b.get_buffered_range() > 1);
c16 = std::move(c16b);
assert(c16b.get_buffered_range() == 1);
// Test range checks
assert_throws([&]() {c16.convert(-1,d);});
assert_throws([&]() {c16.convert(0x8000, d);});
c16.convert(0x1000000, 0x10001000, d); // Range ok.
assert_throws([&]() { c16.convert(10, 5, d); });
auto gen1 = []() { return 1; };
assert_throws([&]() { c16.convert(1, 0x4000, 1, 0x4000, gen1); });
assert_throws([&]() { c16.convert(1, 100, 2, 10, gen1); });
assert_throws([&]() { c16.convert(1, 100, 2, 3, gen1); });
assert_throws([&]() { c16.convert(1, 100, 1, 1, gen1); });
assert_throws([&]() { c16.convert(1, 100, 2, 1, gen1); });
// Test the quality of the output
for (int i = 1; i < 100; ++i)
{
test_distribution_is_uniform<std::uint16_t>(i);
test_distribution_is_uniform<std::uint32_t>(i);
test_distribution_is_uniform<std::uint64_t>(i);
test_entropy_consumption<std::uint16_t>(i);
test_entropy_consumption<std::uint32_t>(i);
test_entropy_consumption<std::uint64_t>(i);
}
std::cout << "Tests passed\n";
}
void measure_conversions(int from, int to)
{
measure_conversion<std::uint16_t>(from, to);
measure_conversion<std::uint32_t>(from, to);
measure_conversion<std::uint64_t>(from, to);
}
void measurements()
{
#ifndef __clang__ // Not working on clang due to bug in library
{
int array[52];
MeasuringRandomDevice d;
std::shuffle(array, array + 52, d);
std::cout << "\nEntropy used by std::shuffle = " << d.entropy() << " bits\n";
}
{
MeasuringRandomDevice d;
std::uniform_int_distribution<> d6(1, 6);
int n = 1000;
for (int i = 0; i < n; ++i)
d6(d);
std::cout << "Entropy used by std::uniform_int_distribution(1,6) = " << d.entropy() / n << " bits\n\n";
}
#endif
std::cout << "| Test | Buffer size (bits) | Best loss (bits) | Estimated loss (bits) | Max loss (bits) | Iterations | Measured loss (bits) | Input entropy (bits) | Output entropy (bits) |\n";
std::cout << "|------|-------------------:|-----------------:|----------------------:|----------------:|-----------:|---------------------:|---------------------:|----------------------:|\n";
measure_shuffle<std::uint16_t>();
measure_shuffle<std::uint32_t>();
measure_shuffle<std::uint64_t>();
measure_conversions(2,6);
measure_conversions(10,9);
measure_conversions(10,11);
measure_expected_entropy<std::uint16_t>();
measure_expected_entropy<std::uint32_t>();
measure_expected_entropy<std::uint64_t>();
}
int main()
{
examples();
measurements();
tests();
}