2015-04-13 16:45:56 +10:00
|
|
|
#include "random.hpp"
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "tap.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
|
2016-01-19 18:31:49 +11:00
|
|
|
test_bool (util::TAP::logger &tap)
|
|
|
|
{
|
2014-04-10 21:04:56 +10:00
|
|
|
static const unsigned ITERATIONS = 8192;
|
2014-12-16 15:29:26 +11:00
|
|
|
static const unsigned THRESHOLD = ITERATIONS / 10;
|
2015-04-13 18:06:08 +10:00
|
|
|
|
2013-07-30 15:10:10 +10:00
|
|
|
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];
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_lt (diff, THRESHOLD, "approximately even bool distribution");
|
2013-07-30 15:10:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Use a more robust test like Kolmogorov-Smirnov
|
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_float (util::TAP::logger &tap)
|
|
|
|
{
|
2013-07-30 15:10:10 +10:00
|
|
|
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;
|
2014-12-16 15:29:26 +11:00
|
|
|
static const float THRESHOLD = EXPECTED / 10;
|
2013-07-30 15:10:10 +10:00
|
|
|
|
|
|
|
unsigned counts[BUCKETS] = { 0 };
|
|
|
|
for (unsigned i = 0; i < ITERATIONS; ++i)
|
|
|
|
++counts[unsigned (util::random<float> () * BUCKETS)];
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
bool success = true;
|
2013-07-30 15:10:10 +10:00
|
|
|
for (unsigned c: counts) {
|
|
|
|
unsigned diff = EXPECTED > c ?
|
|
|
|
EXPECTED - c :
|
|
|
|
c - EXPECTED;
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
success = success && diff < THRESHOLD;
|
2013-07-30 15:10:10 +10:00
|
|
|
}
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
tap.expect (success, "approximately equal float buckets");
|
2013-07-30 15:10:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int, char **) {
|
2016-01-19 18:31:49 +11:00
|
|
|
util::TAP::logger tap;
|
|
|
|
|
2013-07-30 15:10:10 +10:00
|
|
|
srand (time (NULL));
|
2013-08-05 16:45:03 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
test_bool (tap);
|
|
|
|
test_float (tap);
|
|
|
|
|
|
|
|
return tap.status ();
|
2013-07-30 15:10:10 +10:00
|
|
|
}
|