2013-08-05 21:43:16 +10:00
|
|
|
#include "../random.hpp"
|
|
|
|
#include "../debug.hpp"
|
2013-07-30 15:10:10 +10:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2013-08-05 16:45:03 +10:00
|
|
|
#include <ctime>
|
2013-07-30 15:10:10 +10:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Use a more robust test like Chi-Square
|
|
|
|
void
|
|
|
|
test_bool (void) {
|
2014-04-10 21:04:56 +10:00
|
|
|
static const unsigned ITERATIONS = 8192;
|
2013-07-30 15:10:10 +10:00
|
|
|
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;
|
2014-04-10 21:04:56 +10:00
|
|
|
static const unsigned ITERATIONS = 8912;
|
2013-07-30 15:10:10 +10:00
|
|
|
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 ();
|
2013-08-05 16:45:03 +10:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2013-07-30 15:10:10 +10:00
|
|
|
}
|