1
0

FastRandom: Added test of class re-creation. (#3648)

Tests for the precondition of #2935 (re-created cFastRandom generates the same sequence of numbers).
This commit is contained in:
Mattes D 2017-03-28 16:37:25 +02:00 committed by GitHub
parent 96663d35b2
commit 5dc0189a16

View File

@ -7,6 +7,9 @@
#include "FastRandom.h"
static void TestInts(void)
{
cFastRandom rnd;
@ -24,14 +27,17 @@ static void TestInts(void)
sum += v;
}
double avg = static_cast<double>(sum) / ITER;
printf("avg: %f\n", avg);
LOG("avg: %f", avg);
for (int i = 0; i < BUCKETS; i++)
{
printf(" bucket %d: %d\n", i, Counts[i]);
LOG(" bucket %d: %d", i, Counts[i]);
}
}
static void TestFloats(void)
{
cFastRandom rnd;
@ -49,23 +55,65 @@ static void TestFloats(void)
sum += v;
}
sum = sum / ITER;
printf("avg: %f\n", sum);
LOG("avg: %f", sum);
for (int i = 0; i < BUCKETS; i++)
{
printf(" bucket %d: %d\n", i, Counts[i]);
LOG(" bucket %d: %d", i, Counts[i]);
}
}
/** Checks whether re-creating the cFastRandom class produces the same initial number over and over (#2935) */
static void TestReCreation(void)
{
const int ITER = 10000;
int lastVal = 0;
int numSame = 0;
int maxNumSame = 0;
for (int i = 0; i < ITER; ++i)
{
cFastRandom rnd;
int val = rnd.NextInt(10);
if (val == lastVal)
{
numSame += 1;
}
else
{
if (numSame > maxNumSame)
{
maxNumSame = numSame;
}
numSame = 0;
lastVal = val;
}
}
if (numSame > maxNumSame)
{
maxNumSame = numSame;
}
LOG("Out of %d creations, there was a chain of at most %d same numbers generated.", ITER, maxNumSame);
}
int main(void)
{
LOGD("FastRandom Test started");
LOG("FastRandom Test started");
LOGD("Testing ints");
LOG("Testing ints");
TestInts();
LOGD("Testing floats");
LOG("Testing floats");
TestFloats();
LOG("Testing re-creation");
TestReCreation();
LOG("FastRandom test finished");
}