From 5dc0189a160a9ab4d6c9c2865fc8ff5d7ca32d69 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 28 Mar 2017 16:37:25 +0200 Subject: FastRandom: Added test of class re-creation. (#3648) Tests for the precondition of #2935 (re-created cFastRandom generates the same sequence of numbers). --- tests/FastRandom/FastRandomTest.cpp | 62 ++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/tests/FastRandom/FastRandomTest.cpp b/tests/FastRandom/FastRandomTest.cpp index e7729382d..9ab775936 100644 --- a/tests/FastRandom/FastRandomTest.cpp +++ b/tests/FastRandom/FastRandomTest.cpp @@ -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(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"); } -- cgit v1.2.3