From f73e64b4ea7e888d1827dee2017155b538ea9093 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sun, 8 Feb 2015 08:17:29 -0800 Subject: [PATCH] Convert testSBuf to C++11 random engine Also remove SBufTest::randomSeed as it would now only be used in a static function. --- src/tests/SBufFindTest.cc | 25 ++++++++++++------------- src/tests/SBufFindTest.h | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/tests/SBufFindTest.cc b/src/tests/SBufFindTest.cc index a9db2ed75d..a5245e5d82 100644 --- a/src/tests/SBufFindTest.cc +++ b/src/tests/SBufFindTest.cc @@ -9,9 +9,11 @@ #include "squid.h" #include "base/CharacterSet.h" #include "SBufFindTest.h" + #include #include #include +#include /* TODO: The whole SBufFindTest class is currently implemented as a single CppUnit test case (because we do not want to register and report every one @@ -22,7 +24,6 @@ SBufFindTest::SBufFindTest(): caseLimit(std::numeric_limits::max()), errorLimit(std::numeric_limits::max()), - randomSeed(1), hushSimilar(true), maxHayLength(40), thePos(0), @@ -44,8 +45,6 @@ SBufFindTest::SBufFindTest(): void SBufFindTest::run() { - srandom(randomSeed); - for (SBuf::size_type hayLen = 0U; hayLen <= maxHayLength; nextLen(hayLen, maxHayLength)) { const SBuf cleanHay = RandomSBuf(hayLen); @@ -380,19 +379,19 @@ SBufFindTest::RandomSBuf(const int length) "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklomnpqrstuvwxyz"; + + static std::mt19937 mt(time(0)); + // sizeof() counts the terminating zero at the end of characters + // and the distribution is an 'inclusive' value range, so -2 // TODO: add \0 character (needs reporting adjustments to print it as \0) - static const size_t charCount = sizeof(characters)-1; - - char buf[length]; - for (int i = 0; i < length; ++i) { - const unsigned int pos = random() % charCount; - assert(pos < sizeof(characters)); - assert(characters[pos] > 32); - buf[i] = characters[random() % charCount]; - } + static std::uniform_int_distribution dist(0, sizeof(characters)-2); - return SBuf(buf, length); + SBuf buf; + buf.reserveCapacity(length); + for (int i = 0; i < length; ++i) + buf.append(characters[dist(mt)]); + return buf; } /// increments len to quickly cover [0, max] range, slowing down in risky areas diff --git a/src/tests/SBufFindTest.h b/src/tests/SBufFindTest.h index 920a407425..5ed88709a0 100644 --- a/src/tests/SBufFindTest.h +++ b/src/tests/SBufFindTest.h @@ -26,7 +26,6 @@ public: /* test configuration parameters; can be optionally set before run() */ int caseLimit; ///< approximate caseCount limit int errorLimit; ///< errorCount limit - unsigned int randomSeed; ///< pseudo-random sequence choice /// whether to report only one failed test case per "category" bool hushSimilar; /// approximate maximum generated hay string length -- 2.47.2