libcruft-util/test/rand.cpp

69 lines
1.6 KiB
C++
Raw Normal View History

2015-04-13 16:45:56 +10:00
#include "random.hpp"
#include "debug.hpp"
#include "tap.hpp"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
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)
{
static const unsigned ITERATIONS = 8192;
static const unsigned THRESHOLD = ITERATIONS / 10;
2015-04-13 18:06:08 +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");
}
// TODO: Use a more robust test like Kolmogorov-Smirnov
void
2016-01-19 18:31:49 +11:00
test_float (util::TAP::logger &tap)
{
static const unsigned BUCKETS = 8;
static const unsigned ITERATIONS = 8912;
static const unsigned EXPECTED = ITERATIONS / BUCKETS;
static const float THRESHOLD = EXPECTED / 10;
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;
for (unsigned c: counts) {
unsigned diff = EXPECTED > c ?
EXPECTED - c :
c - EXPECTED;
2016-01-19 18:31:49 +11:00
success = success && diff < THRESHOLD;
}
2016-01-19 18:31:49 +11:00
tap.expect (success, "approximately equal float buckets");
}
int
main (int, char **) {
2016-01-19 18:31:49 +11:00
util::TAP::logger tap;
srand (time (NULL));
2016-01-19 18:31:49 +11:00
test_bool (tap);
test_float (tap);
return tap.status ();
}