]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/SBufFindTest.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "base/CharacterSet.h"
11 #include "SBufFindTest.h"
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <cppunit/Message.h>
16 /* TODO: The whole SBufFindTest class is currently implemented as a single
17 CppUnit test case (because we do not want to register and report every one
18 of the thousands of generated test cases). Is there a better way to
19 integrate with CppUnit?
22 SBufFindTest::SBufFindTest():
23 caseLimit(std::numeric_limits
<int>::max()),
24 errorLimit(std::numeric_limits
<int>::max()),
29 thePlacement(placeEof
),
49 for (SBuf::size_type hayLen
= 0U; hayLen
<= maxHayLength
; nextLen(hayLen
, maxHayLength
)) {
50 const SBuf cleanHay
= RandomSBuf(hayLen
);
52 const SBuf::size_type maxNeedleLen
= hayLen
+ 10;
53 for (SBuf::size_type needleLen
= 0U; needleLen
<= maxNeedleLen
; nextLen(needleLen
, maxNeedleLen
)) {
54 theSBufNeedle
= RandomSBuf(needleLen
);
56 for (int i
= 0; i
< placeEof
; i
++) {
57 thePlacement
= Placement(i
);
58 placeNeedle(cleanHay
);
60 const SBuf::size_type maxArg
=
61 max(theSBufHay
.length(), theSBufNeedle
.length()) + 10;
62 for (thePos
= 0; thePos
<= maxArg
; nextLen(thePos
, maxArg
))
65 // the special npos value is not tested as the behavior is
66 // different from std::string (where the behavior is undefined)
67 // It is ad-hoc tested in testSBuf instead
68 //thePos = SBuf::npos;
75 std::cerr
<< "Generated SBuf test cases: " << caseCount
<< std::endl
;
76 std::cerr
<< "\tfailed cases: " << errorCount
<< std::endl
;
77 std::cerr
<< "\treported cases: " << reportCount
<< std::endl
;
78 std::cerr
<< "Asserting because some cases failed..." << std::endl
;
79 CPPUNIT_ASSERT(!SBufFindTest::errorCount
);
83 /// tests SBuf::find(string needle)
85 SBufFindTest::testFindDefs()
87 theFindString
= theBareNeedlePos
= theStringHay
.find(theStringNeedle
);
88 theFindSBuf
= theSBufHay
.find(theSBufNeedle
);
92 /// tests SBuf::rfind(string needle)
94 SBufFindTest::testRFindDefs()
96 theFindString
= theBareNeedlePos
= theStringHay
.rfind(theStringNeedle
);
97 theFindSBuf
= theSBufHay
.rfind(theSBufNeedle
);
98 checkResults("rfind");
101 /// tests SBuf::find(string needle, pos)
103 SBufFindTest::testFind()
105 theFindString
= theStringHay
.find(theStringNeedle
, thePos
);
106 theBareNeedlePos
= theStringHay
.find(theStringNeedle
);
107 theFindSBuf
= theSBufHay
.find(theSBufNeedle
, thePos
);
108 checkResults("find");
111 /// tests SBuf::findFirstOf(string needle, pos)
113 SBufFindTest::testFindFirstOf()
115 theFindString
= theStringHay
.find_first_of(theStringNeedle
, thePos
);
116 theBareNeedlePos
= theStringHay
.find_first_of(theStringNeedle
);
117 theFindSBuf
= theSBufHay
.findFirstOf(CharacterSet("cs",theSBufNeedle
.c_str()), thePos
);
118 checkResults("find_first_of");
121 /// tests SBuf::rfind(string needle, pos)
123 SBufFindTest::testRFind()
125 theFindString
= theStringHay
.rfind(theStringNeedle
, thePos
);
126 theBareNeedlePos
= theStringHay
.rfind(theStringNeedle
);
127 theFindSBuf
= theSBufHay
.rfind(theSBufNeedle
, thePos
);
128 checkResults("rfind");
131 /// tests SBuf::find(char needle)
133 SBufFindTest::testFindCharDefs()
135 const char c
= theStringNeedle
[0];
136 theFindString
= theBareNeedlePos
= theStringHay
.find(c
);
137 theFindSBuf
= theSBufHay
.find(c
);
138 checkResults("find");
141 /// tests SBuf::find(char needle, pos)
143 SBufFindTest::testFindChar()
145 const char c
= theStringNeedle
[0];
146 theFindString
= theStringHay
.find(c
, thePos
);
147 theBareNeedlePos
= theStringHay
.find(c
);
148 theFindSBuf
= theSBufHay
.find(c
, thePos
);
149 checkResults("find");
152 /// tests SBuf::rfind(char needle)
154 SBufFindTest::testRFindCharDefs()
156 const char c
= theStringNeedle
[0];
157 theFindString
= theBareNeedlePos
= theStringHay
.rfind(c
);
158 theFindSBuf
= theSBufHay
.rfind(c
);
159 checkResults("rfind");
162 /// tests SBuf::rfind(char needle, pos)
164 SBufFindTest::testRFindChar()
166 const char c
= theStringNeedle
[0];
167 theFindString
= theStringHay
.rfind(c
, thePos
);
168 theBareNeedlePos
= theStringHay
.rfind(c
);
169 theFindSBuf
= theSBufHay
.rfind(c
, thePos
);
170 checkResults("rfind");
173 /// whether the last SBuf and std::string find() results are the same
175 SBufFindTest::resultsMatch() const
177 // this method is needed because SBuf and std::string use different
178 // size_types (and npos values); comparing the result values directly
179 // would lead to bugs
181 if (theFindString
== std::string::npos
&& theFindSBuf
== SBuf::npos
)
182 return true; // both npos
184 // now safe to cast a non-negative SBuf result
185 return theFindString
== static_cast<std::string::size_type
>(theFindSBuf
);
188 /// called at the end of test case to update state, detect and report failures
190 SBufFindTest::checkResults(const char *method
)
194 handleFailure(method
);
197 /// helper function to convert "printable" Type to std::string
198 template<typename Type
>
200 AnyToString(const Type
&value
)
202 std::stringstream sbuf
;
208 /// helper function to convert SBuf position to a human-friendly string
210 PosToString(const SBuf::size_type pos
)
212 return pos
== SBuf::npos
? std::string("npos") : AnyToString(pos
);
216 /// helper function to convert std::string position to a human-friendly string
218 PosToString(const std::string::size_type pos
)
220 return pos
== std::string::npos
? std::string("npos") : AnyToString(pos
);
223 /// tests each supported SBuf::*find() method using generated hay, needle, pos
225 SBufFindTest::testAllMethods()
227 theStringHay
= std::string(theSBufHay
.rawContent(), theSBufHay
.length());
228 theStringNeedle
= std::string(theSBufNeedle
.rawContent(), theSBufNeedle
.length());
229 theBareNeedlePos
= std::string::npos
;
230 const std::string reportPos
= PosToString(thePos
);
232 // always test string search
234 theReportQuote
= '"';
235 theReportNeedle
= theStringNeedle
;
241 theReportPos
= reportPos
;
247 // if possible, test char search
248 if (!theStringNeedle
.empty()) {
249 theReportQuote
= '\'';
250 theReportNeedle
= theStringNeedle
[0];
256 theReportPos
= reportPos
;
262 /// helper function to format a length-based key (part of case category string)
264 lengthKey(const std::string
&str
)
266 if (str
.length() == 0)
268 if (str
.length() == 1)
273 /// formats position key (part of the case category string)
275 SBufFindTest::posKey() const
277 // the search position does not matter if needle is not in hay
278 if (theBareNeedlePos
== std::string::npos
)
279 return std::string();
281 if (thePos
== SBuf::npos
)
284 if (thePos
< theBareNeedlePos
)
285 return ",posL"; // to the Left of the needle
287 if (thePos
== theBareNeedlePos
)
288 return ",posB"; // Beginning of the needle
290 if (thePos
< theBareNeedlePos
+ theStringNeedle
.length())
291 return ",posM"; // in the Middle of the needle
293 if (thePos
== theBareNeedlePos
+ theStringNeedle
.length())
294 return ",posE"; // at the End of the needle
296 if (thePos
< theStringHay
.length())
297 return ",posR"; // to the Right of the needle
299 return ",posP"; // past the hay
302 /// formats placement key (part of the case category string)
304 SBufFindTest::placementKey() const
306 // Ignore thePlacement because theBareNeedlePos covers it better: we may
307 // try to place the needle somewhere, but hay limits the actual placement.
309 // the placent does not matter if needle is not in hay
310 if (theBareNeedlePos
== std::string::npos
)
311 return std::string();
313 if (theBareNeedlePos
== 0)
314 return "@B"; // at the beggining of the hay string
315 if (theBareNeedlePos
== theStringHay
.length()-theStringNeedle
.length())
316 return "@E"; // at the end of the hay string
317 return "@M"; // in the "middle" of the hay string
320 /// called when a test case fails; counts and possibly reports the failure
322 SBufFindTest::handleFailure(const char *method
)
324 // line break after "........." printed for previous tests
326 std::cerr
<< std::endl
;
330 if (errorCount
> errorLimit
) {
331 std::cerr
<< "Will stop generating SBuf test cases because the " <<
332 "number of failed ones is over the limit: " << errorCount
<<
333 " (after " << caseCount
<< " test cases)" << std::endl
;
334 CPPUNIT_ASSERT(errorCount
<= errorLimit
);
338 // format test case category; category allows us to hush failure reports
339 // for already seen categories with failed cases (to reduce output noise)
340 std::string category
= "hay" + lengthKey(theStringHay
) +
342 if (theReportQuote
== '"')
343 category
+= "needle" + lengthKey(theStringNeedle
);
346 category
+= placementKey();
347 category
+= posKey();
351 if (failedCats
.find(category
) != failedCats
.end())
352 return; // do not report another similar test case failure
353 failedCats
.insert(category
);
356 std::string reportPos
= theReportPos
;
357 if (!reportPos
.empty())
358 reportPos
= ", " + reportPos
;
360 std::cerr
<< "case" << caseCount
<< ": " <<
361 "SBuf(\"" << theStringHay
<< "\")." << method
<<
362 "(" << theReportQuote
<< theReportNeedle
<< theReportQuote
<<
363 reportPos
<< ") returns " << PosToString(theFindSBuf
) <<
364 " instead of " << PosToString(theFindString
) <<
366 " std::string(\"" << theStringHay
<< "\")." << method
<<
367 "(" << theReportQuote
<< theReportNeedle
<< theReportQuote
<<
368 reportPos
<< ") returns " << PosToString(theFindString
) <<
370 " category: " << category
<< std::endl
;
375 /// generates a random string of the specified length
377 SBufFindTest::RandomSBuf(const int length
)
379 static const char characters
[] =
381 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
382 "abcdefghijklomnpqrstuvwxyz";
383 // sizeof() counts the terminating zero at the end of characters
384 // TODO: add \0 character (needs reporting adjustments to print it as \0)
385 static const size_t charCount
= sizeof(characters
)-1;
388 for (int i
= 0; i
< length
; ++i
) {
389 const unsigned int pos
= random() % charCount
;
390 assert(pos
< sizeof(characters
));
391 assert(characters
[pos
] > 32);
392 buf
[i
] = characters
[random() % charCount
];
395 return SBuf(buf
, length
);
398 /// increments len to quickly cover [0, max] range, slowing down in risky areas
399 /// jumps to max+1 if caseLimit is reached
401 SBufFindTest::nextLen(SBuf::size_type
&len
, const SBuf::size_type max
)
405 if (caseCount
>= caseLimit
)
406 len
= max
+1; // avoid future test cases
408 ++len
; // move slowly at the beginning of the [0,max] range
409 else if (len
>= max
- 10)
410 ++len
; // move slowly at the end of the [0,max] range
412 // move fast in the middle of the [0,max] range
415 // but do not overshoot the interesting area at the end of the range
421 /// Places the needle into the hay using cleanHay as a starting point.
423 SBufFindTest::placeNeedle(const SBuf
&cleanHay
)
425 // For simplicity, we do not overwrite clean hay characters but use them as
426 // needle suffix and/or prefix. Should not matter since hay length varies?
428 // TODO: support two needles per hay (explicitly)
429 // TODO: better handle cases where clean hay already contains needle
430 switch (thePlacement
) {
432 theSBufHay
.assign(theSBufNeedle
).append(cleanHay
);
436 const SBuf firstHalf
= cleanHay
.substr(0, cleanHay
.length()/2);
437 const SBuf secondHalf
= cleanHay
.substr(cleanHay
.length()/2);
438 theSBufHay
.assign(firstHalf
).append(theSBufNeedle
).append(secondHalf
);
443 theSBufHay
.assign(cleanHay
).append(theSBufNeedle
);
447 theSBufHay
.assign(cleanHay
);
451 assert(false); // should not happen