21 #include <unordered_map>
37 static const char fox[] =
"The quick brown fox jumped over the lazy dog";
38 static const char fox1[] =
"The quick brown fox ";
39 static const char fox2[] =
"jumped over the lazy dog";
62 CPPUNIT_ASSERT_EQUAL(0U,
s1.length());
63 CPPUNIT_ASSERT_EQUAL(
SBuf(
""),
s1);
65 CPPUNIT_ASSERT_EQUAL(0,strcmp(
"",
s1.c_str()));
71 CPPUNIT_ASSERT_EQUAL(0U,
s1.length());
72 CPPUNIT_ASSERT_EQUAL(
SBuf(
""),
s1);
74 CPPUNIT_ASSERT_EQUAL(0,strcmp(
"",
s1.c_str()));
80 CPPUNIT_ASSERT_EQUAL(0U,
s1.length());
81 CPPUNIT_ASSERT_EQUAL(
SBuf(
""),
s1);
83 CPPUNIT_ASSERT_EQUAL(0,strcmp(
"",
s1.c_str()));
89 CPPUNIT_ASSERT_EQUAL(0U,
s1.length());
90 CPPUNIT_ASSERT_EQUAL(
SBuf(
""),
s1);
92 CPPUNIT_ASSERT_EQUAL(0,strcmp(
"",
s1.c_str()));
95 CPPUNIT_ASSERT_EQUAL(
literal,s5);
97 CPPUNIT_ASSERT_EQUAL(
literal,s6);
106 CPPUNIT_ASSERT_EQUAL(s3.rawContent(), s4.
rawContent());
113 CPPUNIT_ASSERT_EQUAL(
s1,s3);
116 CPPUNIT_ASSERT_EQUAL(s4,s3);
121 std::string str(
fox);
138 CPPUNIT_ASSERT_EQUAL(
s1,
s1);
139 CPPUNIT_ASSERT_EQUAL(
s1,s2);
140 s2.
assign(
"The quick brown fox jumped over the lazy doe");
141 CPPUNIT_ASSERT(!(
s1 == s2));
143 CPPUNIT_ASSERT(!(
s1 == s2));
144 CPPUNIT_ASSERT(
s1 != s2);
146 CPPUNIT_ASSERT(!(
s1 == s2));
147 CPPUNIT_ASSERT(
s1 != s2);
149 CPPUNIT_ASSERT_EQUAL(
s1,s2);
156 const char *
const rawAppendix = appendix.
rawContent();
163 CPPUNIT_ASSERT_EQUAL(s0, appendix);
168 CPPUNIT_ASSERT(
s1.rawContent() != appendix.
rawContent());
169 CPPUNIT_ASSERT(
s1 != appendix);
170 CPPUNIT_ASSERT_EQUAL(rawAppendix, appendix.
rawContent());
177 s1.Printf(
"%s:%d:%03.3f",
"fox",10,12345.67);
178 s2.
assign(
"fox:10:12345.670");
179 CPPUNIT_ASSERT_EQUAL(
s1,s2);
193 const char *alphabet=
"abcdefghijklmnopqrstuvwxyz";
197 CPPUNIT_ASSERT_EQUAL(
alpha,s);
203 control.append(alphabet,5).append(1,
'\0').append(alphabet,6,std::string::npos);
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));
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);
229 std::cout <<
"sizeof(SBuf): " <<
sizeof(
SBuf) << std::endl;
230 std::cout <<
"sizeof(MemBlob): " <<
sizeof(
MemBlob) << std::endl;
237 CPPUNIT_ASSERT_EQUAL(chg[5],
'u');
239 CPPUNIT_ASSERT_EQUAL(
literal[5],
'u');
240 CPPUNIT_ASSERT_EQUAL(chg[5],
'e');
251 std::cout << c << std::endl;
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)));
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)));
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)));
307 const size_t maxN = 2 +
min(strlen(left), strlen(right));
308 for (
size_t n = 0; n <= maxN; ++n) {
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);
336 CPPUNIT_ASSERT(
s1.cmp(s2)<0);
338 CPPUNIT_ASSERT(
s1 < s2);
340 CPPUNIT_ASSERT_EQUAL(1,
SBuf(
"foolong").caseCmp(
SBuf(
"foo"), 5));
344 CPPUNIT_ASSERT_EQUAL(0,
s1.caseCmp(s2));
345 CPPUNIT_ASSERT_EQUAL(0,
s1.caseCmp(s2,2));
347 s1.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));
368 char *right =
xstrdup(
"foo34567890123456789012345678");
369 SBuf left(
"fooZYXWVUTSRQPONMLKJIHGFEDCBA");
376 left.
setAt(14,
'\0');
381 std::cerr << std::endl <<
" cmp(char*) " << n <<
' ' << left <<
" ?= " << right;
384 std::cerr << std::endl <<
" caseCmp(char*) " << n <<
' ' << left <<
" ?= " << right;
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());
411 foo =
s1.rawContent();
412 CPPUNIT_ASSERT_EQUAL(0,strncmp(
fox,foo,
s1.length()));
414 CPPUNIT_ASSERT(!strcmp(
fox,foo));
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);
446 CPPUNIT_ASSERT_EQUAL(ref,b);
452 CPPUNIT_ASSERT_EQUAL(ref,b);
458 CPPUNIT_ASSERT_EQUAL(ref,b);
464 CPPUNIT_ASSERT_EQUAL(ref,b);
470 CPPUNIT_ASSERT_EQUAL(ref,b);
476 CPPUNIT_ASSERT_EQUAL(ref,b);
482 CPPUNIT_ASSERT_EQUAL(ref,b);
488 CPPUNIT_ASSERT_EQUAL(ref,b);
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);
516 CPPUNIT_ASSERT_EQUAL(ref,
sb);
535 CPPUNIT_ASSERT_EQUAL(s2,s3);
537 CPPUNIT_ASSERT_EQUAL(
s1,s2);
544 const char *alphabet=
"abcdefghijklmnopqrstuvwxyz";
552 CPPUNIT_ASSERT_EQUAL(3U,idx);
553 CPPUNIT_ASSERT_EQUAL(
'd',
s1[idx]);
557 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
561 CPPUNIT_ASSERT_EQUAL(4U,idx);
565 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
569 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
572 idx=
s1.find(
'd',
s1.length()+1);
573 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
578 CPPUNIT_ASSERT_EQUAL(3U, idx);
579 CPPUNIT_ASSERT_EQUAL(
'd',
s1[idx]);
583 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
587 CPPUNIT_ASSERT_EQUAL(4U,idx);
591 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
594 idx=
s1.rfind(
'd',
s1.length()+1);
595 CPPUNIT_ASSERT_EQUAL(3U,idx);
601 const char *alphabet=
"abcdefghijklmnopqrstuvwxyz";
602 SBuf haystack(alphabet);
609 CPPUNIT_ASSERT_EQUAL(3U,idx);
612 CPPUNIT_ASSERT_EQUAL(23U,idx);
616 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
620 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
624 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
628 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
632 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
636 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
640 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
648 CPPUNIT_ASSERT_EQUAL(3U,idx);
651 CPPUNIT_ASSERT_EQUAL(23U,idx);
657 CPPUNIT_ASSERT_EQUAL(3U,idx);
660 CPPUNIT_ASSERT_EQUAL(23U,idx);
664 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
668 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
672 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
676 CPPUNIT_ASSERT_EQUAL(3U, idx);
680 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
684 CPPUNIT_ASSERT_EQUAL(3U, idx);
688 CPPUNIT_ASSERT_EQUAL(3U, idx);
692 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
700 CPPUNIT_ASSERT_EQUAL(29U,idx);
703 CPPUNIT_ASSERT_EQUAL(23U,idx);
713 CPPUNIT_ASSERT_EQUAL(40U,idx);
714 CPPUNIT_ASSERT_EQUAL(
' ',
s1[idx]);
721 SBuf goobar(
"goobar");
725 idx=haystack.rfind(
SBuf(
""));
726 CPPUNIT_ASSERT_EQUAL(haystack.length(),idx);
732 idx=haystack.rfind(
SBuf(
"fox"));
733 CPPUNIT_ASSERT_EQUAL(16U,idx);
740 idx=haystack.rfind(
SBuf(
"foe"));
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);
766 CPPUNIT_ASSERT_EQUAL(strlen(
fox),(
size_t)s.
length());
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);
787 ref(
"the quick brown fox jumped over the lazy dog");
788 CPPUNIT_ASSERT_EQUAL(ref,sng);
805 CPPUNIT_ASSERT(
match!=ref);
809 CPPUNIT_ASSERT_EQUAL(ref,
match);
827 CPPUNIT_ASSERT_EQUAL(b.
length(),
static_cast<unsigned int>(0));
828 CPPUNIT_ASSERT_EQUAL(b.
spaceSize(), startLength);
834 auto x = b.
reserve(requirements);
836 CPPUNIT_ASSERT(x <= requirements.
maxCapacity - filled);
847 for (
const int delta: {-1,0,+1}) {
867 CPPUNIT_ASSERT(gap < buffer.
length());
868 const void *gapEnd = buffer.
rawContent() + gap;
870 CPPUNIT_ASSERT_EQUAL(gapEnd,
static_cast<const void*
>(buffer.
rawContent()));
873 const auto beforeSpaceSize = buffer.
spaceSize();
874 const void *
const beforePosition = buffer.
rawContent();
877 const void *
const afterPosition = buffer.
rawContent();
878 CPPUNIT_ASSERT_EQUAL(before.cowAvoided + 1, after.cowAvoided);
879 CPPUNIT_ASSERT_EQUAL(before.cowShift, after.cowShift);
880 CPPUNIT_ASSERT_EQUAL(before.cowJustAlloc, after.cowJustAlloc);
881 CPPUNIT_ASSERT_EQUAL(before.cowAllocCopy, after.cowAllocCopy);
882 CPPUNIT_ASSERT_EQUAL(beforeSpaceSize, buffer.
spaceSize());
883 CPPUNIT_ASSERT_EQUAL(beforePosition, afterPosition);
884 CPPUNIT_ASSERT(strcmp(
fox + gap, buffer.
c_str()) == 0);
893 const void *
const initialStorage = buffer.
rawContent();
897 CPPUNIT_ASSERT(gap < buffer.
length());
898 const void *gapEnd = buffer.
rawContent() + gap;
900 CPPUNIT_ASSERT_EQUAL(gapEnd,
static_cast<const void*
>(buffer.
rawContent()));
903 const auto beforeSpaceSize = buffer.
spaceSize();
906 const void *
const afterStorage = buffer.
rawContent();
907 CPPUNIT_ASSERT_EQUAL(before.cowAvoided, after.cowAvoided);
908 CPPUNIT_ASSERT_EQUAL(before.cowShift + 1, after.cowShift);
909 CPPUNIT_ASSERT_EQUAL(before.cowJustAlloc, after.cowJustAlloc);
910 CPPUNIT_ASSERT_EQUAL(before.cowAllocCopy, after.cowAllocCopy);
911 CPPUNIT_ASSERT_EQUAL(initialStorage, afterStorage);
912 CPPUNIT_ASSERT(beforeSpaceSize + gap <= buffer.
spaceSize());
913 CPPUNIT_ASSERT(strcmp(
fox + gap, buffer.
c_str()) == 0);
922 const void *
const initialStorage = buffer.
rawContent();
926 CPPUNIT_ASSERT(gap < buffer.
length());
927 const void *gapEnd = buffer.
rawContent() + gap;
929 CPPUNIT_ASSERT_EQUAL(gapEnd,
static_cast<const void*
>(buffer.
rawContent()));
932 const auto beforeSpaceSize = buffer.
spaceSize();
935 const void *
const afterStorage = buffer.
rawContent();
936 CPPUNIT_ASSERT_EQUAL(before.cowAvoided, after.cowAvoided);
937 CPPUNIT_ASSERT_EQUAL(before.cowShift + 1, after.cowShift);
938 CPPUNIT_ASSERT_EQUAL(before.cowJustAlloc, after.cowJustAlloc);
939 CPPUNIT_ASSERT_EQUAL(before.cowAllocCopy, after.cowAllocCopy);
940 CPPUNIT_ASSERT_EQUAL(initialStorage, afterStorage);
941 CPPUNIT_ASSERT(beforeSpaceSize + gap <= buffer.
spaceSize());
942 CPPUNIT_ASSERT(strcmp(
fox + gap, buffer.
c_str()) == 0);
949 static SBuf casebuf(
"THE QUICK");
951 CPPUNIT_ASSERT(!
SBuf(
"The quick brown").startsWith(
SBuf(
fox1)));
959 casebuf =
"tha quick";
966 SBuf b(
"const.string, int 10 and a float 10.5");
968 ss <<
"const.string, int " << 10 <<
" and a float " << 10.5;
970 CPPUNIT_ASSERT_EQUAL(b,o);
973 CPPUNIT_ASSERT_EQUAL(
SBuf(),o);
978 CPPUNIT_ASSERT_EQUAL(f1,
SBuf(
fox1));
993 CPPUNIT_ASSERT_EQUAL(0U,idx);
997 CPPUNIT_ASSERT_EQUAL(haystack.
length()-1,idx);
1001 CPPUNIT_ASSERT_EQUAL(4U,idx);
1016 CPPUNIT_ASSERT_EQUAL(0U,idx);
1020 CPPUNIT_ASSERT_EQUAL(haystack.
length()-1,idx);
1024 CPPUNIT_ASSERT_EQUAL(3U,idx);
1037 const char *alphabet=
"abcdefghijklmnopqrstuvwxyz";
1038 std::string astr(alphabet);
1040 CPPUNIT_ASSERT_EQUAL(astr,
sb.toStdString());
1047 CPPUNIT_ASSERT(
text.begin() ==
text.begin());
1048 CPPUNIT_ASSERT(
text.begin() !=
text.end());
1049 CPPUNIT_ASSERT(
text.begin() != text2.
begin());
1051 auto i =
text.begin();
1052 auto e =
text.end();
1053 CPPUNIT_ASSERT_EQUAL(
'f', *i);
1054 CPPUNIT_ASSERT(i != e);
1056 CPPUNIT_ASSERT_EQUAL(
'o', *i);
1057 CPPUNIT_ASSERT(i != e);
1059 CPPUNIT_ASSERT_EQUAL(
'o', *i);
1060 CPPUNIT_ASSERT(i != e);
1062 CPPUNIT_ASSERT(i == e);
1065 auto i =
text.rbegin();
1066 auto e =
text.rend();
1067 CPPUNIT_ASSERT_EQUAL(
'o', *i);
1068 CPPUNIT_ASSERT(i != e);
1070 CPPUNIT_ASSERT_EQUAL(
'o', *i);
1071 CPPUNIT_ASSERT(i != e);
1073 CPPUNIT_ASSERT_EQUAL(
'f', *i);
1074 CPPUNIT_ASSERT(i != e);
1076 CPPUNIT_ASSERT(i == e);
1095 std::unordered_map<SBuf, int> um;
1096 um[
SBuf(
"one")] = 1;
1097 um[
SBuf(
"two")] = 2;
1099 auto i = um.find(
SBuf(
"one"));
1100 CPPUNIT_ASSERT(i != um.end());
1101 CPPUNIT_ASSERT(i->second == 1);
1103 i = um.find(
SBuf(
"eleventy"));
1104 CPPUNIT_ASSERT(i == um.end());