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:
parent
96663d35b2
commit
5dc0189a16
@ -7,6 +7,9 @@
|
|||||||
#include "FastRandom.h"
|
#include "FastRandom.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void TestInts(void)
|
static void TestInts(void)
|
||||||
{
|
{
|
||||||
cFastRandom rnd;
|
cFastRandom rnd;
|
||||||
@ -24,14 +27,17 @@ static void TestInts(void)
|
|||||||
sum += v;
|
sum += v;
|
||||||
}
|
}
|
||||||
double avg = static_cast<double>(sum) / ITER;
|
double avg = static_cast<double>(sum) / ITER;
|
||||||
printf("avg: %f\n", avg);
|
LOG("avg: %f", avg);
|
||||||
for (int i = 0; i < BUCKETS; i++)
|
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)
|
static void TestFloats(void)
|
||||||
{
|
{
|
||||||
cFastRandom rnd;
|
cFastRandom rnd;
|
||||||
@ -49,23 +55,65 @@ static void TestFloats(void)
|
|||||||
sum += v;
|
sum += v;
|
||||||
}
|
}
|
||||||
sum = sum / ITER;
|
sum = sum / ITER;
|
||||||
printf("avg: %f\n", sum);
|
LOG("avg: %f", sum);
|
||||||
for (int i = 0; i < BUCKETS; i++)
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
LOGD("FastRandom Test started");
|
LOG("FastRandom Test started");
|
||||||
|
|
||||||
LOGD("Testing ints");
|
LOG("Testing ints");
|
||||||
TestInts();
|
TestInts();
|
||||||
|
|
||||||
LOGD("Testing floats");
|
LOG("Testing floats");
|
||||||
TestFloats();
|
TestFloats();
|
||||||
|
|
||||||
|
LOG("Testing re-creation");
|
||||||
|
TestReCreation();
|
||||||
|
|
||||||
LOG("FastRandom test finished");
|
LOG("FastRandom test finished");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user