Add basic randomness checks for random generators
This commit is contained in:
parent
43a8fe8c2d
commit
8de0495b8a
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@ -13,6 +13,7 @@
|
|||||||
/md[245]*
|
/md[245]*
|
||||||
/options
|
/options
|
||||||
/pool*
|
/pool*
|
||||||
|
/random*
|
||||||
/range*
|
/range*
|
||||||
/region*
|
/region*
|
||||||
/sha1*
|
/sha1*
|
||||||
|
@ -22,6 +22,7 @@ TEST_BIN = \
|
|||||||
md5 \
|
md5 \
|
||||||
options \
|
options \
|
||||||
pool \
|
pool \
|
||||||
|
random \
|
||||||
range \
|
range \
|
||||||
region \
|
region \
|
||||||
signal \
|
signal \
|
||||||
|
56
test/random.cpp
Normal file
56
test/random.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include "random.hpp"
|
||||||
|
#include "debug.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Use a more robust test like Chi-Square
|
||||||
|
void
|
||||||
|
test_bool (void) {
|
||||||
|
static const unsigned ITERATIONS = 1024;
|
||||||
|
static const unsigned THRESHOLD = ITERATIONS * 0.1;
|
||||||
|
|
||||||
|
unsigned counts[2] = { 0, 0 };
|
||||||
|
for (unsigned i = 0; i < ITERATIONS; ++i)
|
||||||
|
++counts[util::random<bool> () ? 0 : 1];
|
||||||
|
|
||||||
|
unsigned diff = counts[0] > counts[1] ?
|
||||||
|
counts[0] - counts[1] :
|
||||||
|
counts[1] - counts[0];
|
||||||
|
|
||||||
|
CHECK_LT (diff, THRESHOLD);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Use a more robust test like Kolmogorov-Smirnov
|
||||||
|
void
|
||||||
|
test_float (void) {
|
||||||
|
static const unsigned BUCKETS = 8;
|
||||||
|
static const unsigned ITERATIONS = 4096;
|
||||||
|
static const unsigned EXPECTED = ITERATIONS / BUCKETS;
|
||||||
|
static const float THRESHOLD = EXPECTED * 0.1;
|
||||||
|
|
||||||
|
unsigned counts[BUCKETS] = { 0 };
|
||||||
|
for (unsigned i = 0; i < ITERATIONS; ++i)
|
||||||
|
++counts[unsigned (util::random<float> () * BUCKETS)];
|
||||||
|
|
||||||
|
for (unsigned c: counts) {
|
||||||
|
unsigned diff = EXPECTED > c ?
|
||||||
|
EXPECTED - c :
|
||||||
|
c - EXPECTED;
|
||||||
|
|
||||||
|
CHECK_LT (diff, THRESHOLD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int, char **) {
|
||||||
|
srand (time (NULL));
|
||||||
|
test_bool ();
|
||||||
|
test_float ();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user