+/*
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
#include "squid.h"
-#include "SBufFindTest.h"
+#include "base/CharacterSet.h"
+#include "tests/SBufFindTest.h"
+
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/Message.h>
#include <limits>
+#include <random>
/* 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
*/
SBufFindTest::SBufFindTest():
- caseLimit(std::numeric_limits<int>::max()),
- errorLimit(std::numeric_limits<int>::max()),
- randomSeed(1),
- hushSimilar(true),
- maxHayLength(40),
- thePos(0),
- thePlacement(placeEof),
- theStringPos(0),
- theBareNeedlePos(0),
- caseCount(0),
- errorCount(0),
- reportCount(0)
+ caseLimit(std::numeric_limits<int>::max()),
+ errorLimit(std::numeric_limits<int>::max()),
+ hushSimilar(true),
+ maxHayLength(40),
+ thePos(0),
+ thePlacement(placeEof),
+ theStringPos(0),
+ theBareNeedlePos(0),
+ theFindString(0),
+ theFindSBuf(0),
+ theReportFunc(),
+ theReportNeedle(),
+ theReportPos(),
+ theReportQuote('"'),
+ caseCount(0),
+ errorCount(0),
+ reportCount(0)
{
}
void
SBufFindTest::run()
{
- srandom(randomSeed);
-
- for (SBuf::size_type hayLen = 0; hayLen <= maxHayLength; nextLen(hayLen, maxHayLength)) {
+ for (SBuf::size_type hayLen = 0U; hayLen <= maxHayLength; nextLen(hayLen, maxHayLength)) {
const SBuf cleanHay = RandomSBuf(hayLen);
const SBuf::size_type maxNeedleLen = hayLen + 10;
- for (SBuf::size_type needleLen = 0; needleLen <= maxNeedleLen; nextLen(needleLen, maxNeedleLen)) {
+ for (SBuf::size_type needleLen = 0U; needleLen <= maxNeedleLen; nextLen(needleLen, maxNeedleLen)) {
theSBufNeedle = RandomSBuf(needleLen);
for (int i = 0; i < placeEof; i++) {
checkResults("find");
}
-/// tests SBuf::find_first_of(string needle, pos)
+/// tests SBuf::findFirstOf(string needle, pos)
void
SBufFindTest::testFindFirstOf()
{
theFindString = theStringHay.find_first_of(theStringNeedle, thePos);
theBareNeedlePos = theStringHay.find_first_of(theStringNeedle);
- theFindSBuf = theSBufHay.find_first_of(theSBufNeedle, thePos);
+ theFindSBuf = theSBufHay.findFirstOf(CharacterSet("cs",theSBufNeedle.c_str()), thePos);
checkResults("find_first_of");
}
if (theFindString == std::string::npos && theFindSBuf == SBuf::npos)
return true; // both npos
- if (theFindSBuf < 0) // should not happen, treat as error
- return false;
-
// now safe to cast a non-negative SBuf result
return theFindString == static_cast<std::string::size_type>(theFindSBuf);
}
return sbuf.str();
}
+#if 0
/// helper function to convert SBuf position to a human-friendly string
inline std::string
PosToString(const SBuf::size_type pos)
{
return pos == SBuf::npos ? std::string("npos") : AnyToString(pos);
}
+#endif
/// helper function to convert std::string position to a human-friendly string
inline std::string
if (thePos == SBuf::npos)
return ",npos";
- if (thePos < 0)
- return ",posN"; // negative
-
- // we know Pos is not negative or special; avoid signed/unsigned warnings
- const std::string::size_type pos =
- static_cast<std::string::size_type>(thePos);
-
- if (pos < theBareNeedlePos)
+ if (thePos < theBareNeedlePos)
return ",posL"; // to the Left of the needle
- if (pos == theBareNeedlePos)
+
+ if (thePos == theBareNeedlePos)
return ",posB"; // Beginning of the needle
- if (pos < theBareNeedlePos + theStringNeedle.length())
+
+ if (thePos < theBareNeedlePos + theStringNeedle.length())
return ",posM"; // in the Middle of the needle
- if (pos == theBareNeedlePos + theStringNeedle.length())
+
+ if (thePos == theBareNeedlePos + theStringNeedle.length())
return ",posE"; // at the End of the needle
- if (pos < theStringHay.length())
+
+ if (thePos < theStringHay.length())
return ",posR"; // to the Right of the needle
+
return ",posP"; // past the hay
}
return std::string();
if (theBareNeedlePos == 0)
- return "@B"; // at the beggining of the hay string
+ return "@B"; // at the beginning of the hay string
if (theBareNeedlePos == theStringHay.length()-theStringNeedle.length())
return "@E"; // at the end of the hay string
return "@M"; // in the "middle" of the hay string
"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 xuniform_int_distribution<uint8_t> 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
/// jumps to max+1 if caseLimit is reached
void
-SBufFindTest::nextLen(int &len, const int max)
+SBufFindTest::nextLen(SBuf::size_type &len, const SBuf::size_type max)
{
assert(len <= max);
break;
}
}
+