2 * Copyright (C) 1996-2020 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 "HttpReply.h"
12 #include "sbuf/Algorithms.h"
13 #include "sbuf/SBuf.h"
14 #include "sbuf/Stream.h"
15 #include "tests/SBufFindTest.h"
16 #include "tests/testSBuf.h"
17 #include "unitTestMain.h"
21 #include <unordered_map>
23 CPPUNIT_TEST_SUITE_REGISTRATION( testSBuf
);
25 /* let this test link sanely */
27 #include "MemObject.h"
29 eventAdd(const char *name
, EVH
* func
, void *arg
, double when
, int, bool cbdata
)
32 MemObject::endOffset() const
37 static char fox
[]="The quick brown fox jumped over the lazy dog";
38 static char fox1
[]="The quick brown fox ";
39 static char fox2
[]="jumped over the lazy dog";
41 // TEST: globals variables (default/empty and with contents) are
42 // created outside and before any unit tests and memory subsystem
43 // initialization. Check for correct constructor operation.
45 SBuf
literal("The quick brown fox jumped over the lazy dog");
48 testSBuf::testSBufConstructDestruct()
50 /* NOTE: Do not initialize memory here because we need
51 * to test correct operation before and after Mem::Init
54 // XXX: partial demo below of how to do constructor unit-test. use scope to ensure each test
55 // is working on local-scope variables constructed fresh for the test, and destructed when
56 // scope exists. use nested scopes to test destructor affects on copied data (MemBlob etc)
58 // TEST: default constructor (implicit destructor non-crash test)
59 // test accessors on empty SBuf.
62 CPPUNIT_ASSERT_EQUAL(0U,s1
.length());
63 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1
);
64 CPPUNIT_ASSERT_EQUAL(empty_sbuf
,s1
);
65 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1
.c_str()));
68 // TEST: copy-construct NULL string (implicit destructor non-crash test)
71 CPPUNIT_ASSERT_EQUAL(0U,s1
.length());
72 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1
);
73 CPPUNIT_ASSERT_EQUAL(empty_sbuf
,s1
);
74 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1
.c_str()));
77 // TEST: copy-construct empty string (implicit destructor non-crash test)
80 CPPUNIT_ASSERT_EQUAL(0U,s1
.length());
81 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1
);
82 CPPUNIT_ASSERT_EQUAL(empty_sbuf
,s1
);
83 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1
.c_str()));
86 // TEST: copy-construct from a SBuf
89 CPPUNIT_ASSERT_EQUAL(0U,s1
.length());
90 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1
);
91 CPPUNIT_ASSERT_EQUAL(empty_sbuf
,s1
);
92 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1
.c_str()));
95 CPPUNIT_ASSERT_EQUAL(literal
,s5
);
97 CPPUNIT_ASSERT_EQUAL(literal
,s6
);
98 // XXX: other state checks. expected result of calling any state accessor on s4 ?
101 // TEST: check that COW doesn't happen upon copy-construction
103 SBuf
s1(empty_sbuf
), s2(s1
);
104 CPPUNIT_ASSERT_EQUAL(s1
.rawContent(), s2
.rawContent());
105 SBuf
s3(literal
), s4(literal
);
106 CPPUNIT_ASSERT_EQUAL(s3
.rawContent(), s4
.rawContent());
109 // TEST: sub-string copy
111 SBuf s1
=SBuf(fox
+4), s2(fox
);
112 SBuf s3
=s2
.substr(4,s2
.length()); //n is out-of-bounds
113 CPPUNIT_ASSERT_EQUAL(s1
,s3
);
116 CPPUNIT_ASSERT_EQUAL(s4
,s3
);
119 // TEST: go via std::string adapter.
121 std::string
str(fox
);
123 CPPUNIT_ASSERT_EQUAL(literal
,s1
);
128 testSBuf::testSBufConstructDestructAfterMemInit()
131 testSBufConstructDestruct();
135 testSBuf::testEqualityTest()
137 SBuf
s1(fox
),s2(fox
);
138 CPPUNIT_ASSERT_EQUAL(s1
,s1
); //self-equality
139 CPPUNIT_ASSERT_EQUAL(s1
,s2
); //same contents
140 s2
.assign("The quick brown fox jumped over the lazy doe");
141 CPPUNIT_ASSERT(!(s1
== s2
)); //same length, different contents
143 CPPUNIT_ASSERT(!(s1
== s2
)); //different length and contents
144 CPPUNIT_ASSERT(s1
!= s2
); //while we're ready, let's test inequality
146 CPPUNIT_ASSERT(!(s1
== s2
)); //null and not-null
147 CPPUNIT_ASSERT(s1
!= s2
); //while we're ready, let's test inequality
149 CPPUNIT_ASSERT_EQUAL(s1
,s2
); //null and null
153 testSBuf::testAppendSBuf()
155 const SBuf
appendix(fox1
);
156 const char * const rawAppendix
= appendix
.rawContent();
158 // check whether the optimization that prevents copying when append()ing to
159 // default-constructed SBuf actually works
162 CPPUNIT_ASSERT_EQUAL(s0
.rawContent(), appendix
.rawContent());
163 CPPUNIT_ASSERT_EQUAL(s0
, appendix
);
165 // paranoid: check that the above code can actually detect copies
168 CPPUNIT_ASSERT(s1
.rawContent() != appendix
.rawContent());
169 CPPUNIT_ASSERT(s1
!= appendix
);
170 CPPUNIT_ASSERT_EQUAL(rawAppendix
, appendix
.rawContent());
174 testSBuf::testPrintf()
177 s1
.Printf("%s:%d:%03.3f","fox",10,12345.67);
178 s2
.assign("fox:10:12345.670");
179 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
183 testSBuf::testAppendCString()
187 CPPUNIT_ASSERT_EQUAL(s1
,literal
);
191 testSBuf::testAppendStdString()
193 const char *alphabet
="abcdefghijklmnopqrstuvwxyz";
195 SBuf
alpha(alphabet
), s
;
196 s
.append(alphabet
,5).append(alphabet
+5);
197 CPPUNIT_ASSERT_EQUAL(alpha
,s
);
202 s
.append(alphabet
,5).append("\0",1).append(alphabet
+6,SBuf::npos
);
203 control
.append(alphabet
,5).append(1,'\0').append(alphabet
,6,std::string::npos
);
204 SBuf
scontrol(control
); // we need this to test the equality. sigh.
205 CPPUNIT_ASSERT_EQUAL(scontrol
,s
);
208 const char *alphazero
="abcdefghijk\0mnopqrstuvwxyz";
209 SBuf
s(alphazero
,26);
210 std::string
str(alphazero
,26);
211 CPPUNIT_ASSERT_EQUAL(0,memcmp(str
.data(),s
.rawContent(),26));
216 testSBuf::testAppendf()
219 s1
.appendf("%s:%d:%03.2f",fox
,1234,1234.56);
220 s2
.assign("The quick brown fox jumped over the lazy dog:1234:1234.56");
221 CPPUNIT_ASSERT_EQUAL(s2
,s1
);
225 testSBuf::testDumpStats()
227 SBuf::GetStats().dump(std::cout
);
228 MemBlob::GetStats().dump(std::cout
);
229 std::cout
<< "sizeof(SBuf): " << sizeof(SBuf
) << std::endl
;
230 std::cout
<< "sizeof(MemBlob): " << sizeof(MemBlob
) << std::endl
;
234 testSBuf::testSubscriptOp()
237 CPPUNIT_ASSERT_EQUAL(chg
[5],'u');
239 CPPUNIT_ASSERT_EQUAL(literal
[5],'u');
240 CPPUNIT_ASSERT_EQUAL(chg
[5],'e');
243 // note: can't use cppunit's CPPUNIT_TEST_EXCEPTION because TextException asserts, and
244 // so the test can't be properly completed.
246 testSBuf::testSubscriptOpFail()
249 c
=literal
.at(literal
.length()); //out of bounds by 1
251 std::cout
<< c
<< std::endl
;
254 static int sign(int v
)
264 testComparisonStdFull(const char *left
, const char *right
)
266 if (sign(strcmp(left
, right
)) != sign(SBuf(left
).cmp(SBuf(right
))))
267 std::cerr
<< std::endl
<< " cmp(SBuf) npos " << left
<< " ?= " << right
<< std::endl
;
268 CPPUNIT_ASSERT_EQUAL(sign(strcmp(left
, right
)), sign(SBuf(left
).cmp(SBuf(right
))));
270 if (sign(strcmp(left
, right
)) != sign(SBuf(left
).cmp(right
)))
271 std::cerr
<< std::endl
<< " cmp(char*) npos " << left
<< " ?= " << right
<< std::endl
;
272 CPPUNIT_ASSERT_EQUAL(sign(strcmp(left
, right
)), sign(SBuf(left
).cmp(right
)));
274 if (sign(strcasecmp(left
, right
)) != sign(SBuf(left
).caseCmp(SBuf(right
))))
275 std::cerr
<< std::endl
<< " caseCmp(SBuf) npos " << left
<< " ?= " << right
<< std::endl
;
276 CPPUNIT_ASSERT_EQUAL(sign(strcasecmp(left
, right
)), sign(SBuf(left
).caseCmp(SBuf(right
))));
278 if (sign(strcasecmp(left
, right
)) != sign(SBuf(left
).caseCmp(right
)))
279 std::cerr
<< std::endl
<< " caseCmp(char*) npos " << left
<< " ?= " << right
<< std::endl
;
280 CPPUNIT_ASSERT_EQUAL(sign(strcasecmp(left
, right
)), sign(SBuf(left
).caseCmp(right
)));
284 testComparisonStdN(const char *left
, const char *right
, const size_t n
)
286 if (sign(strncmp(left
, right
, n
)) != sign(SBuf(left
).cmp(SBuf(right
), n
)))
287 std::cerr
<< std::endl
<< " cmp(SBuf) " << n
<< ' ' << left
<< " ?= " << right
<< std::endl
;
288 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left
, right
, n
)), sign(SBuf(left
).cmp(SBuf(right
), n
)));
290 if (sign(strncmp(left
, right
, n
)) != sign(SBuf(left
).cmp(right
, n
)))
291 std::cerr
<< std::endl
<< " cmp(char*) " << n
<< ' ' << SBuf(left
) << " ?= " << right
<< std::endl
;
292 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left
, right
, n
)), sign(SBuf(left
).cmp(right
, n
)));
294 if (sign(strncasecmp(left
, right
, n
)) != sign(SBuf(left
).caseCmp(SBuf(right
), n
)))
295 std::cerr
<< std::endl
<< " caseCmp(SBuf) " << n
<< ' ' << left
<< " ?= " << right
<< std::endl
;
296 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left
, right
, n
)), sign(SBuf(left
).caseCmp(SBuf(right
), n
)));
298 if (sign(strncasecmp(left
, right
, n
)) != sign(SBuf(left
).caseCmp(right
, n
)))
299 std::cerr
<< std::endl
<< " caseCmp(char*) " << n
<< ' ' << SBuf(left
) << " ?= " << right
<< std::endl
;
300 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left
, right
, n
)), sign(SBuf(left
).caseCmp(right
, n
)));
304 testComparisonStdOneWay(const char *left
, const char *right
)
306 testComparisonStdFull(left
, right
);
307 const size_t maxN
= 2 + min(strlen(left
), strlen(right
));
308 for (size_t n
= 0; n
<= maxN
; ++n
) {
309 testComparisonStdN(left
, right
, n
);
314 testComparisonStd(const char *s1
, const char *s2
)
316 testComparisonStdOneWay(s1
, s2
);
317 testComparisonStdOneWay(s2
, s1
);
321 testSBuf::testComparisons()
324 SBuf
s1("foo"),s2("foe");
325 CPPUNIT_ASSERT(s1
.cmp(s2
)>0);
326 CPPUNIT_ASSERT(s1
.caseCmp(s2
)>0);
327 CPPUNIT_ASSERT(s2
.cmp(s1
)<0);
328 CPPUNIT_ASSERT_EQUAL(0,s1
.cmp(s2
,2));
329 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
,2));
330 CPPUNIT_ASSERT(s1
> s2
);
331 CPPUNIT_ASSERT(s2
< s1
);
332 CPPUNIT_ASSERT_EQUAL(sign(s1
.cmp(s2
)),sign(strcmp(s1
.c_str(),s2
.c_str())));
336 CPPUNIT_ASSERT(s1
.cmp(s2
)<0);
337 CPPUNIT_ASSERT_EQUAL(sign(s1
.cmp(s2
)),sign(strcmp(s1
.c_str(),s2
.c_str())));
338 CPPUNIT_ASSERT(s1
< s2
);
339 // specifying the max-length and overhanging size
340 CPPUNIT_ASSERT_EQUAL(1,SBuf("foolong").caseCmp(SBuf("foo"), 5));
341 // case-insensive comaprison
344 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
));
345 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
,2));
346 // \0-clenliness test
347 s1
.assign("f\0oo",4);
348 s2
.assign("f\0Oo",4);
349 CPPUNIT_ASSERT(s1
.cmp(s2
) > 0);
350 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
));
351 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
,3));
352 CPPUNIT_ASSERT_EQUAL(0,s1
.caseCmp(s2
,2));
353 CPPUNIT_ASSERT_EQUAL(0,s1
.cmp(s2
,2));
355 testComparisonStd("foo", "fooz");
356 testComparisonStd("foo", "foo");
357 testComparisonStd("foo", "f");
358 testComparisonStd("foo", "bar");
360 testComparisonStd("foo", "FOOZ");
361 testComparisonStd("foo", "FOO");
362 testComparisonStd("foo", "F");
364 testComparisonStdOneWay("", "");
366 // rare case C-string input matching SBuf with N>strlen(s)
368 char *right
= xstrdup("foo34567890123456789012345678");
369 SBuf
left("fooZYXWVUTSRQPONMLKJIHGFEDCBA");
370 // is 3 bytes in length. NEVER more.
374 // pick another spot to truncate at if something goes horribly wrong.
376 left
.setAt(14, '\0');
378 const SBuf::size_type maxN
= 20 + min(left
.length(), static_cast<SBuf::size_type
>(strlen(right
)));
379 for (SBuf::size_type n
= 0; n
<= maxN
; ++n
) {
380 if (sign(strncmp(left
.rawContent(), right
, n
)) != sign(left
.cmp(right
, n
)) )
381 std::cerr
<< std::endl
<< " cmp(char*) " << n
<< ' ' << left
<< " ?= " << right
;
382 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left
.rawContent(), right
, n
)), sign(left
.cmp(right
, n
)));
383 if (sign(strncasecmp(left
.rawContent(), right
, n
)) != sign(left
.caseCmp(right
, n
)))
384 std::cerr
<< std::endl
<< " caseCmp(char*) " << n
<< ' ' << left
<< " ?= " << right
;
385 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left
.rawContent(), right
, n
)), sign(left
.caseCmp(right
, n
)));
392 testSBuf::testConsume()
394 SBuf
s1(literal
),s2
,s3
;
397 CPPUNIT_ASSERT_EQUAL(s2
,s3
);
398 s3
.assign("quick brown fox jumped over the lazy dog");
399 CPPUNIT_ASSERT_EQUAL(s1
,s3
);
401 CPPUNIT_ASSERT_EQUAL(s1
,SBuf());
405 testSBuf::testRawContent()
411 foo
= s1
.rawContent();
412 CPPUNIT_ASSERT_EQUAL(0,strncmp(fox
,foo
,s1
.length()));
414 CPPUNIT_ASSERT(!strcmp(fox
,foo
));
418 testSBuf::testRawSpace()
422 char *rb
=s2
.rawAppendStart(strlen(fox2
)+1);
424 s2
.rawAppendFinish(rb
, strlen(fox2
));
425 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
434 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
438 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
439 const char *alphabet
="abcdefghijklmnopqrstuvwxyz";
441 std::string
s(alphabet
); // TODO
446 CPPUNIT_ASSERT_EQUAL(ref
,b
);
450 b
.chop(b
.length()-3);
452 CPPUNIT_ASSERT_EQUAL(ref
,b
);
454 { // chop at beginning
458 CPPUNIT_ASSERT_EQUAL(ref
,b
);
460 { // chop to zero length
464 CPPUNIT_ASSERT_EQUAL(ref
,b
);
466 { // chop beyond end (at npos)
468 b
.chop(SBuf::npos
,4);
470 CPPUNIT_ASSERT_EQUAL(ref
,b
);
474 b
.chop(b
.length()+2,4);
476 CPPUNIT_ASSERT_EQUAL(ref
,b
);
480 b
.chop(0,b
.length());
482 CPPUNIT_ASSERT_EQUAL(ref
,b
);
484 { // overflow chopped area
486 b
.chop(b
.length()-3,b
.length());
488 CPPUNIT_ASSERT_EQUAL(ref
,b
);
493 testSBuf::testChomp()
495 SBuf
s1("complete string");
498 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
499 s2
.assign(" complete string ,");
501 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
502 s1
.assign(", complete string ,");
505 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
508 // inspired by SBufFindTest; to be expanded.
509 class SBufSubstrAutoTest
512 std::string fullReference
, str
;
514 void performEqualityTest() {
516 CPPUNIT_ASSERT_EQUAL(ref
,sb
);
518 SBufSubstrAutoTest() : fullString(fox
), fullReference(fox
) {
519 for (int offset
=fullString
.length()-1; offset
>= 0; --offset
) {
520 for (int length
=fullString
.length()-1-offset
; length
>= 0; --length
) {
521 sb
=fullString
.substr(offset
,length
);
522 str
=fullReference
.substr(offset
,length
);
523 performEqualityTest();
530 testSBuf::testSubstr()
532 SBuf
s1(literal
),s2
,s3
;
535 CPPUNIT_ASSERT_EQUAL(s2
,s3
);
537 CPPUNIT_ASSERT_EQUAL(s1
,s2
);
538 SBufSubstrAutoTest sat
; // work done in the constructor
542 testSBuf::testFindChar()
544 const char *alphabet
="abcdefghijklmnopqrstuvwxyz";
547 SBuf::size_type nposResult
=SBuf::npos
;
550 // needle in haystack
552 CPPUNIT_ASSERT_EQUAL(3U,idx
);
553 CPPUNIT_ASSERT_EQUAL('d',s1
[idx
]);
555 // needle not present in haystack
556 idx
=s1
.find(' '); //fails
557 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
561 CPPUNIT_ASSERT_EQUAL(4U,idx
);
563 // char not in searched portion
565 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
567 // invalid start position
568 idx
=s1
.find('d',SBuf::npos
);
569 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
571 // search outside of haystack
572 idx
=s1
.find('d',s1
.length()+1);
573 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
576 // needle in haystack
578 CPPUNIT_ASSERT_EQUAL(3U, idx
);
579 CPPUNIT_ASSERT_EQUAL('d', s1
[idx
]);
581 // needle not present in haystack
582 idx
=s1
.rfind(' '); //fails
583 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
587 CPPUNIT_ASSERT_EQUAL(4U,idx
);
589 // char not in searched portion
591 CPPUNIT_ASSERT_EQUAL(nposResult
,idx
);
593 // overlong haystack specification
594 idx
=s1
.rfind('d',s1
.length()+1);
595 CPPUNIT_ASSERT_EQUAL(3U,idx
);
599 testSBuf::testFindSBuf()
601 const char *alphabet
="abcdefghijklmnopqrstuvwxyz";
602 SBuf
haystack(alphabet
);
604 SBuf::size_type nposResult
=SBuf::npos
;
607 // needle in haystack
608 idx
= haystack
.find(SBuf("def"));
609 CPPUNIT_ASSERT_EQUAL(3U,idx
);
611 idx
= haystack
.find(SBuf("xyz"));
612 CPPUNIT_ASSERT_EQUAL(23U,idx
);
614 // needle not in haystack, no initial char match
615 idx
= haystack
.find(SBuf(" eq"));
616 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
618 // needle not in haystack, initial sequence match
619 idx
= haystack
.find(SBuf("deg"));
620 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
622 // needle past end of haystack
623 idx
= haystack
.find(SBuf("xyz1"));
624 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
626 // search in portion: needle not in searched part
627 idx
= haystack
.find(SBuf("def"),7);
628 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
630 // search in portion: overhang
631 idx
= haystack
.find(SBuf("def"),4);
632 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
634 // invalid start position
635 idx
= haystack
.find(SBuf("def"),SBuf::npos
);
636 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
638 // needle bigger than haystack
639 idx
= SBuf("def").find(haystack
);
640 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
642 // search in a double-matching haystack
647 idx
= h2
.find(SBuf("def"));
648 CPPUNIT_ASSERT_EQUAL(3U,idx
);
650 idx
= h2
.find(SBuf("xyzab"));
651 CPPUNIT_ASSERT_EQUAL(23U,idx
);
655 // needle in haystack
656 idx
= haystack
.rfind(SBuf("def"));
657 CPPUNIT_ASSERT_EQUAL(3U,idx
);
659 idx
= haystack
.rfind(SBuf("xyz"));
660 CPPUNIT_ASSERT_EQUAL(23U,idx
);
662 // needle not in haystack, no initial char match
663 idx
= haystack
.rfind(SBuf(" eq"));
664 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
666 // needle not in haystack, initial sequence match
667 idx
= haystack
.rfind(SBuf("deg"));
668 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
670 // needle past end of haystack
671 idx
= haystack
.rfind(SBuf("xyz1"));
672 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
674 // search in portion: needle in searched part
675 idx
= haystack
.rfind(SBuf("def"),7);
676 CPPUNIT_ASSERT_EQUAL(3U, idx
);
678 // search in portion: needle not in searched part
679 idx
= haystack
.rfind(SBuf("mno"),3);
680 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
682 // search in portion: overhang
683 idx
= haystack
.rfind(SBuf("def"),4);
684 CPPUNIT_ASSERT_EQUAL(3U, idx
);
686 // npos start position
687 idx
= haystack
.rfind(SBuf("def"),SBuf::npos
);
688 CPPUNIT_ASSERT_EQUAL(3U, idx
);
690 // needle bigger than haystack
691 idx
= SBuf("def").rfind(haystack
);
692 CPPUNIT_ASSERT_EQUAL(nposResult
, idx
);
694 // search in a double-matching haystack
699 idx
= h2
.rfind(SBuf("def"));
700 CPPUNIT_ASSERT_EQUAL(29U,idx
);
702 idx
= h2
.find(SBuf("xyzab"));
703 CPPUNIT_ASSERT_EQUAL(23U,idx
);
708 testSBuf::testRFindChar()
713 CPPUNIT_ASSERT_EQUAL(40U,idx
);
714 CPPUNIT_ASSERT_EQUAL(' ',s1
[idx
]);
718 testSBuf::testRFindSBuf()
720 SBuf
haystack(literal
),afox("fox");
721 SBuf
goobar("goobar");
724 // corner case: search for a zero-length SBuf
725 idx
=haystack
.rfind(SBuf(""));
726 CPPUNIT_ASSERT_EQUAL(haystack
.length(),idx
);
728 // corner case: search for a needle longer than the haystack
729 idx
=afox
.rfind(SBuf(" "));
730 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
732 idx
=haystack
.rfind(SBuf("fox"));
733 CPPUNIT_ASSERT_EQUAL(16U,idx
);
735 // needle not found, no match for first char
736 idx
=goobar
.rfind(SBuf("foo"));
737 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
739 // needle not found, match for first char but no match for SBuf
740 idx
=haystack
.rfind(SBuf("foe"));
741 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
743 SBuf
g("g"); //match at the last char
744 idx
=haystack
.rfind(g
);
745 CPPUNIT_ASSERT_EQUAL(43U,idx
);
746 CPPUNIT_ASSERT_EQUAL('g',haystack
[idx
]);
748 idx
=haystack
.rfind(SBuf("The"));
749 CPPUNIT_ASSERT_EQUAL(0U,idx
);
751 haystack
.append("The");
752 idx
=haystack
.rfind(SBuf("The"));
753 CPPUNIT_ASSERT_EQUAL(44U,idx
);
756 haystack
="The quick brown fox";
757 SBuf
needle("foxy lady");
758 idx
=haystack
.rfind(needle
);
759 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
763 testSBuf::testSBufLength()
766 CPPUNIT_ASSERT_EQUAL(strlen(fox
),(size_t)s
.length());
772 char buf
[40]; //shorter than literal()
774 CPPUNIT_ASSERT_EQUAL(s
.length(),s
.copy(buf
,40));
775 CPPUNIT_ASSERT_EQUAL(0,strncmp(s
.rawContent(),buf
,s
.length()));
777 CPPUNIT_ASSERT_EQUAL(40U,s
.copy(buf
,40));
780 CPPUNIT_ASSERT_EQUAL(s2
,s
);
784 testSBuf::testStringOps()
786 SBuf
sng(ToLower(literal
)),
787 ref("the quick brown fox jumped over the lazy dog");
788 CPPUNIT_ASSERT_EQUAL(ref
,sng
);
790 CPPUNIT_ASSERT_EQUAL(0,sng
.compare(ref
,caseInsensitive
));
791 // max-size comparison
792 CPPUNIT_ASSERT_EQUAL(0,ref
.compare(SBuf("THE"),caseInsensitive
,3));
793 CPPUNIT_ASSERT_EQUAL(1,ref
.compare(SBuf("THE"),caseInsensitive
,6));
794 CPPUNIT_ASSERT_EQUAL(0,SBuf("the").compare(SBuf("THE"),caseInsensitive
,6));
802 const char *ref
=t
.rawContent();
803 t
.reserveCapacity(10240);
804 const char *match
=t
.rawContent();
805 CPPUNIT_ASSERT(match
!=ref
);
807 t
.append(literal
).append(literal
).append(literal
).append(literal
).append(literal
);
808 t
.append(t
).append(t
).append(t
).append(t
).append(t
);
809 CPPUNIT_ASSERT_EQUAL(ref
,match
);
813 testSBuf::testReserve()
815 SBufReservationRequirements requirements
;
816 // use unusual numbers to ensure we do not hit a lucky boundary situation
817 requirements
.minSpace
= 10;
818 requirements
.idealSpace
= 82;
819 requirements
.maxCapacity
= 259;
820 requirements
.allowShared
= true;
822 // for each possible starting buffer length within the capacity
823 for (SBuf::size_type startLength
= 0; startLength
<= requirements
.maxCapacity
; ++startLength
) {
826 b
.reserveCapacity(startLength
);
827 CPPUNIT_ASSERT_EQUAL(b
.length(), static_cast<unsigned int>(0));
828 CPPUNIT_ASSERT_EQUAL(b
.spaceSize(), startLength
);
830 // check that it never grows outside capacity.
831 // do 5 excess cycles to check that.
832 for (SBuf::size_type filled
= 0; filled
< requirements
.maxCapacity
+5; ++filled
) {
833 CPPUNIT_ASSERT_EQUAL(b
.length(), min(filled
, requirements
.maxCapacity
));
834 auto x
= b
.reserve(requirements
);
835 // the amount of space advertized must not cause users to exceed capacity
836 CPPUNIT_ASSERT(x
<= requirements
.maxCapacity
- filled
);
837 CPPUNIT_ASSERT(b
.spaceSize() <= requirements
.maxCapacity
- filled
);
838 // the total size of buffer must not cause users to exceed capacity
839 CPPUNIT_ASSERT(b
.length() + b
.spaceSize() <= requirements
.maxCapacity
);
845 // the minimal space requirement should overwrite idealSpace preferences
846 requirements
.minSpace
= 10;
847 for (const int delta
: {-1,0,+1}) {
848 requirements
.idealSpace
= requirements
.minSpace
+ delta
;
850 buffer
.reserve(requirements
);
851 CPPUNIT_ASSERT(buffer
.spaceSize() >= requirements
.minSpace
);
856 testSBuf::testStartsWith()
858 static SBuf
casebuf("THE QUICK");
859 CPPUNIT_ASSERT(literal
.startsWith(SBuf(fox1
)));
860 CPPUNIT_ASSERT(!SBuf("The quick brown").startsWith(SBuf(fox1
))); //too short
861 CPPUNIT_ASSERT(!literal
.startsWith(SBuf(fox2
))); //different contents
863 // case-insensitive checks
864 CPPUNIT_ASSERT(literal
.startsWith(casebuf
,caseInsensitive
));
865 casebuf
=ToUpper(SBuf(fox1
));
866 CPPUNIT_ASSERT(literal
.startsWith(casebuf
,caseInsensitive
));
867 CPPUNIT_ASSERT(literal
.startsWith(SBuf(fox1
),caseInsensitive
));
868 casebuf
= "tha quick";
869 CPPUNIT_ASSERT_EQUAL(false,literal
.startsWith(casebuf
,caseInsensitive
));
873 testSBuf::testSBufStream()
875 SBuf
b("const.string, int 10 and a float 10.5");
877 ss
<< "const.string, int " << 10 << " and a float " << 10.5;
879 CPPUNIT_ASSERT_EQUAL(b
,o
);
882 CPPUNIT_ASSERT_EQUAL(SBuf(),o
);
886 CPPUNIT_ASSERT_EQUAL(ss2
.buf(),literal
);
887 CPPUNIT_ASSERT_EQUAL(f1
,SBuf(fox1
));
891 testSBuf::testFindFirstOf()
893 SBuf
haystack(literal
);
897 idx
=haystack
.findFirstOf(CharacterSet("t1","ADHRWYP"));
898 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
900 // found at beginning
901 idx
=haystack
.findFirstOf(CharacterSet("t2","THANDF"));
902 CPPUNIT_ASSERT_EQUAL(0U,idx
);
904 //found at end of haystack
905 idx
=haystack
.findFirstOf(CharacterSet("t3","QWERYVg"));
906 CPPUNIT_ASSERT_EQUAL(haystack
.length()-1,idx
);
908 //found in the middle of haystack
909 idx
=haystack
.findFirstOf(CharacterSet("t4","QWERqYV"));
910 CPPUNIT_ASSERT_EQUAL(4U,idx
);
914 testSBuf::testFindFirstNotOf()
916 SBuf
haystack(literal
);
919 // all chars from the set
920 idx
=haystack
.findFirstNotOf(CharacterSet("t1",literal
.c_str()));
921 CPPUNIT_ASSERT_EQUAL(SBuf::npos
,idx
);
923 // found at beginning
924 idx
=haystack
.findFirstNotOf(CharacterSet("t2","a"));
925 CPPUNIT_ASSERT_EQUAL(0U,idx
);
927 //found at end of haystack
928 idx
=haystack
.findFirstNotOf(CharacterSet("t3",literal
.substr(0,literal
.length()-1).c_str()));
929 CPPUNIT_ASSERT_EQUAL(haystack
.length()-1,idx
);
931 //found in the middle of haystack
932 idx
=haystack
.findFirstNotOf(CharacterSet("t4","The"));
933 CPPUNIT_ASSERT_EQUAL(3U,idx
);
937 testSBuf::testAutoFind()
944 testSBuf::testStdStringOps()
946 const char *alphabet
="abcdefghijklmnopqrstuvwxyz";
947 std::string
astr(alphabet
);
949 CPPUNIT_ASSERT_EQUAL(astr
,sb
.toStdString());
953 testSBuf::testIterators()
955 SBuf
text("foo"), text2("foo");
956 CPPUNIT_ASSERT(text
.begin() == text
.begin());
957 CPPUNIT_ASSERT(text
.begin() != text
.end());
958 CPPUNIT_ASSERT(text
.begin() != text2
.begin());
960 auto i
= text
.begin();
962 CPPUNIT_ASSERT_EQUAL('f', *i
);
963 CPPUNIT_ASSERT(i
!= e
);
965 CPPUNIT_ASSERT_EQUAL('o', *i
);
966 CPPUNIT_ASSERT(i
!= e
);
968 CPPUNIT_ASSERT_EQUAL('o', *i
);
969 CPPUNIT_ASSERT(i
!= e
);
971 CPPUNIT_ASSERT(i
== e
);
974 auto i
= text
.rbegin();
975 auto e
= text
.rend();
976 CPPUNIT_ASSERT_EQUAL('o', *i
);
977 CPPUNIT_ASSERT(i
!= e
);
979 CPPUNIT_ASSERT_EQUAL('o', *i
);
980 CPPUNIT_ASSERT(i
!= e
);
982 CPPUNIT_ASSERT_EQUAL('f', *i
);
983 CPPUNIT_ASSERT(i
!= e
);
985 CPPUNIT_ASSERT(i
== e
);
990 testSBuf::testSBufHash()
992 // same SBuf must have same hash
993 auto hasher
=std::hash
<SBuf
>();
994 CPPUNIT_ASSERT_EQUAL(hasher(literal
),hasher(literal
));
996 // same content must have same hash
997 CPPUNIT_ASSERT_EQUAL(hasher(literal
),hasher(SBuf(fox
)));
998 CPPUNIT_ASSERT_EQUAL(hasher(SBuf(fox
)),hasher(SBuf(fox
)));
1000 //different content should have different hash
1001 CPPUNIT_ASSERT(hasher(SBuf(fox
)) != hasher(SBuf(fox1
)));
1004 std::unordered_map
<SBuf
, int> um
;
1005 um
[SBuf("one")] = 1;
1006 um
[SBuf("two")] = 2;
1008 auto i
= um
.find(SBuf("one"));
1009 CPPUNIT_ASSERT(i
!= um
.end());
1010 CPPUNIT_ASSERT(i
->second
== 1);
1012 i
= um
.find(SBuf("eleventy"));
1013 CPPUNIT_ASSERT(i
== um
.end());