]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testSBuf.cc
C++11: Remove GnuRegex and all -lregex related code
[thirdparty/squid.git] / src / tests / testSBuf.cc
1 /*
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #include "squid.h"
10 #include "base/CharacterSet.h"
11 #include "sbuf/Algorithms.h"
12 #include "sbuf/SBuf.h"
13 #include "sbuf/Stream.h"
14 #include "tests/SBufFindTest.h"
15 #include "tests/testSBuf.h"
16 #include "unitTestMain.h"
17
18 #include <iostream>
19 #include <stdexcept>
20 #include <unordered_map>
21
22 CPPUNIT_TEST_SUITE_REGISTRATION( testSBuf );
23
24 /* let this test link sanely */
25 #include "event.h"
26 #include "MemObject.h"
27 void
28 eventAdd(const char *name, EVH * func, void *arg, double when, int, bool cbdata)
29 {}
30 int64_t
31 MemObject::endOffset() const
32 { return 0; }
33 /* end of stubs */
34
35 // test string
36 static char fox[]="The quick brown fox jumped over the lazy dog";
37 static char fox1[]="The quick brown fox ";
38 static char fox2[]="jumped over the lazy dog";
39
40 // TEST: globals variables (default/empty and with contents) are
41 // created outside and before any unit tests and memory subsystem
42 // initialization. Check for correct constructor operation.
43 SBuf empty_sbuf;
44 SBuf literal("The quick brown fox jumped over the lazy dog");
45
46 void
47 testSBuf::testSBufConstructDestruct()
48 {
49 /* NOTE: Do not initialize memory here because we need
50 * to test correct operation before and after Mem::Init
51 */
52
53 // XXX: partial demo below of how to do constructor unit-test. use scope to ensure each test
54 // is working on local-scope variables constructed fresh for the test, and destructed when
55 // scope exists. use nested scopes to test destructor affects on copied data (MemBlob etc)
56
57 // TEST: default constructor (implicit destructor non-crash test)
58 // test accessors on empty SBuf.
59 {
60 SBuf s1;
61 CPPUNIT_ASSERT_EQUAL(0U,s1.length());
62 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
63 CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
64 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
65 }
66
67 // TEST: copy-construct NULL string (implicit destructor non-crash test)
68 {
69 SBuf s1(NULL);
70 CPPUNIT_ASSERT_EQUAL(0U,s1.length());
71 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
72 CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
73 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
74 }
75
76 // TEST: copy-construct empty string (implicit destructor non-crash test)
77 {
78 SBuf s1("");
79 CPPUNIT_ASSERT_EQUAL(0U,s1.length());
80 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
81 CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
82 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
83 }
84
85 // TEST: copy-construct from a SBuf
86 {
87 SBuf s1(empty_sbuf);
88 CPPUNIT_ASSERT_EQUAL(0U,s1.length());
89 CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
90 CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
91 CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
92
93 SBuf s5(literal);
94 CPPUNIT_ASSERT_EQUAL(literal,s5);
95 SBuf s6(fox);
96 CPPUNIT_ASSERT_EQUAL(literal,s6);
97 // XXX: other state checks. expected result of calling any state accessor on s4 ?
98 }
99
100 // TEST: check that COW doesn't happen upon copy-construction
101 {
102 SBuf s1(empty_sbuf), s2(s1);
103 CPPUNIT_ASSERT_EQUAL(s1.rawContent(), s2.rawContent());
104 SBuf s3(literal), s4(literal);
105 CPPUNIT_ASSERT_EQUAL(s3.rawContent(), s4.rawContent());
106 }
107
108 // TEST: sub-string copy
109 {
110 SBuf s1=SBuf(fox+4), s2(fox);
111 SBuf s3=s2.substr(4,s2.length()); //n is out-of-bounds
112 CPPUNIT_ASSERT_EQUAL(s1,s3);
113 SBuf s4=SBuf(fox,4);
114 s3=s2.substr(0,4);
115 CPPUNIT_ASSERT_EQUAL(s4,s3);
116 }
117
118 // TEST: go via std::string adapter.
119 {
120 std::string str(fox);
121 SBuf s1(str);
122 CPPUNIT_ASSERT_EQUAL(literal,s1);
123 }
124 }
125
126 void
127 testSBuf::testSBufConstructDestructAfterMemInit()
128 {
129 Mem::Init();
130 testSBufConstructDestruct();
131 }
132
133 void
134 testSBuf::testEqualityTest()
135 {
136 SBuf s1(fox),s2(fox);
137 CPPUNIT_ASSERT_EQUAL(s1,s1); //self-equality
138 CPPUNIT_ASSERT_EQUAL(s1,s2); //same contents
139 s2.assign("The quick brown fox jumped over the lazy doe");
140 CPPUNIT_ASSERT(!(s1 == s2)); //same length, different contents
141 s2.assign("foo");
142 CPPUNIT_ASSERT(!(s1 == s2)); //different length and contents
143 CPPUNIT_ASSERT(s1 != s2); //while we're ready, let's test inequality
144 s2.clear();
145 CPPUNIT_ASSERT(!(s1 == s2)); //null and not-null
146 CPPUNIT_ASSERT(s1 != s2); //while we're ready, let's test inequality
147 s1.clear();
148 CPPUNIT_ASSERT_EQUAL(s1,s2); //null and null
149 }
150
151 void
152 testSBuf::testAppendSBuf()
153 {
154 const SBuf appendix(fox1);
155 const char * const rawAppendix = appendix.rawContent();
156
157 // check whether the optimization that prevents copying when append()ing to
158 // default-constructed SBuf actually works
159 SBuf s0;
160 s0.append(appendix);
161 CPPUNIT_ASSERT_EQUAL(s0.rawContent(), appendix.rawContent());
162 CPPUNIT_ASSERT_EQUAL(s0, appendix);
163
164 // paranoid: check that the above code can actually detect copies
165 SBuf s1(fox1);
166 s1.append(appendix);
167 CPPUNIT_ASSERT(s1.rawContent() != appendix.rawContent());
168 CPPUNIT_ASSERT(s1 != appendix);
169 CPPUNIT_ASSERT_EQUAL(rawAppendix, appendix.rawContent());
170 }
171
172 void
173 testSBuf::testPrintf()
174 {
175 SBuf s1,s2;
176 s1.Printf("%s:%d:%03.3f","fox",10,12345.67);
177 s2.assign("fox:10:12345.670");
178 CPPUNIT_ASSERT_EQUAL(s1,s2);
179 }
180
181 void
182 testSBuf::testAppendCString()
183 {
184 SBuf s1(fox1);
185 s1.append(fox2);
186 CPPUNIT_ASSERT_EQUAL(s1,literal);
187 }
188
189 void
190 testSBuf::testAppendStdString()
191 {
192 const char *alphabet="abcdefghijklmnopqrstuvwxyz";
193 {
194 SBuf alpha(alphabet), s;
195 s.append(alphabet,5).append(alphabet+5);
196 CPPUNIT_ASSERT_EQUAL(alpha,s);
197 }
198 {
199 SBuf s;
200 std::string control;
201 s.append(alphabet,5).append("\0",1).append(alphabet+6,SBuf::npos);
202 control.append(alphabet,5).append(1,'\0').append(alphabet,6,std::string::npos);
203 SBuf scontrol(control); // we need this to test the equality. sigh.
204 CPPUNIT_ASSERT_EQUAL(scontrol,s);
205 }
206 {
207 const char *alphazero="abcdefghijk\0mnopqrstuvwxyz";
208 SBuf s(alphazero,26);
209 std::string str(alphazero,26);
210 CPPUNIT_ASSERT_EQUAL(0,memcmp(str.data(),s.rawContent(),26));
211 }
212 }
213
214 void
215 testSBuf::testAppendf()
216 {
217 SBuf s1,s2;
218 s1.appendf("%s:%d:%03.2f",fox,1234,1234.56);
219 s2.assign("The quick brown fox jumped over the lazy dog:1234:1234.56");
220 CPPUNIT_ASSERT_EQUAL(s2,s1);
221 }
222
223 void
224 testSBuf::testDumpStats()
225 {
226 SBuf::GetStats().dump(std::cout);
227 MemBlob::GetStats().dump(std::cout);
228 std::cout << "sizeof(SBuf): " << sizeof(SBuf) << std::endl;
229 std::cout << "sizeof(MemBlob): " << sizeof(MemBlob) << std::endl;
230 }
231
232 void
233 testSBuf::testSubscriptOp()
234 {
235 SBuf chg(literal);
236 CPPUNIT_ASSERT_EQUAL(chg[5],'u');
237 chg.setAt(5,'e');
238 CPPUNIT_ASSERT_EQUAL(literal[5],'u');
239 CPPUNIT_ASSERT_EQUAL(chg[5],'e');
240 }
241
242 // note: can't use cppunit's CPPUNIT_TEST_EXCEPTION because TextException asserts, and
243 // so the test can't be properly completed.
244 void
245 testSBuf::testSubscriptOpFail()
246 {
247 char c;
248 c=literal.at(literal.length()); //out of bounds by 1
249 //notreached
250 std::cout << c << std::endl;
251 }
252
253 static int sign(int v)
254 {
255 if (v < 0)
256 return -1;
257 if (v>0)
258 return 1;
259 return 0;
260 }
261
262 static void
263 testComparisonStdFull(const char *left, const char *right)
264 {
265 if (sign(strcmp(left, right)) != sign(SBuf(left).cmp(SBuf(right))))
266 std::cerr << std::endl << " cmp(SBuf) npos " << left << " ?= " << right << std::endl;
267 CPPUNIT_ASSERT_EQUAL(sign(strcmp(left, right)), sign(SBuf(left).cmp(SBuf(right))));
268
269 if (sign(strcmp(left, right)) != sign(SBuf(left).cmp(right)))
270 std::cerr << std::endl << " cmp(char*) npos " << left << " ?= " << right << std::endl;
271 CPPUNIT_ASSERT_EQUAL(sign(strcmp(left, right)), sign(SBuf(left).cmp(right)));
272
273 if (sign(strcasecmp(left, right)) != sign(SBuf(left).caseCmp(SBuf(right))))
274 std::cerr << std::endl << " caseCmp(SBuf) npos " << left << " ?= " << right << std::endl;
275 CPPUNIT_ASSERT_EQUAL(sign(strcasecmp(left, right)), sign(SBuf(left).caseCmp(SBuf(right))));
276
277 if (sign(strcasecmp(left, right)) != sign(SBuf(left).caseCmp(right)))
278 std::cerr << std::endl << " caseCmp(char*) npos " << left << " ?= " << right << std::endl;
279 CPPUNIT_ASSERT_EQUAL(sign(strcasecmp(left, right)), sign(SBuf(left).caseCmp(right)));
280 }
281
282 static void
283 testComparisonStdN(const char *left, const char *right, const size_t n)
284 {
285 if (sign(strncmp(left, right, n)) != sign(SBuf(left).cmp(SBuf(right), n)))
286 std::cerr << std::endl << " cmp(SBuf) " << n << ' ' << left << " ?= " << right << std::endl;
287 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left, right, n)), sign(SBuf(left).cmp(SBuf(right), n)));
288
289 if (sign(strncmp(left, right, n)) != sign(SBuf(left).cmp(right, n)))
290 std::cerr << std::endl << " cmp(char*) " << n << ' ' << SBuf(left) << " ?= " << right << std::endl;
291 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left, right, n)), sign(SBuf(left).cmp(right, n)));
292
293 if (sign(strncasecmp(left, right, n)) != sign(SBuf(left).caseCmp(SBuf(right), n)))
294 std::cerr << std::endl << " caseCmp(SBuf) " << n << ' ' << left << " ?= " << right << std::endl;
295 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left, right, n)), sign(SBuf(left).caseCmp(SBuf(right), n)));
296
297 if (sign(strncasecmp(left, right, n)) != sign(SBuf(left).caseCmp(right, n)))
298 std::cerr << std::endl << " caseCmp(char*) " << n << ' ' << SBuf(left) << " ?= " << right << std::endl;
299 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left, right, n)), sign(SBuf(left).caseCmp(right, n)));
300 }
301
302 static void
303 testComparisonStdOneWay(const char *left, const char *right)
304 {
305 testComparisonStdFull(left, right);
306 const size_t maxN = 2 + min(strlen(left), strlen(right));
307 for (size_t n = 0; n <= maxN; ++n) {
308 testComparisonStdN(left, right, n);
309 }
310 }
311
312 static void
313 testComparisonStd(const char *s1, const char *s2)
314 {
315 testComparisonStdOneWay(s1, s2);
316 testComparisonStdOneWay(s2, s1);
317 }
318
319 void
320 testSBuf::testComparisons()
321 {
322 //same length
323 SBuf s1("foo"),s2("foe");
324 CPPUNIT_ASSERT(s1.cmp(s2)>0);
325 CPPUNIT_ASSERT(s1.caseCmp(s2)>0);
326 CPPUNIT_ASSERT(s2.cmp(s1)<0);
327 CPPUNIT_ASSERT_EQUAL(0,s1.cmp(s2,2));
328 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2,2));
329 CPPUNIT_ASSERT(s1 > s2);
330 CPPUNIT_ASSERT(s2 < s1);
331 CPPUNIT_ASSERT_EQUAL(sign(s1.cmp(s2)),sign(strcmp(s1.c_str(),s2.c_str())));
332 //different lengths
333 s1.assign("foo");
334 s2.assign("foof");
335 CPPUNIT_ASSERT(s1.cmp(s2)<0);
336 CPPUNIT_ASSERT_EQUAL(sign(s1.cmp(s2)),sign(strcmp(s1.c_str(),s2.c_str())));
337 CPPUNIT_ASSERT(s1 < s2);
338 // specifying the max-length and overhanging size
339 CPPUNIT_ASSERT_EQUAL(1,SBuf("foolong").caseCmp(SBuf("foo"), 5));
340 // case-insensive comaprison
341 s1 = "foo";
342 s2 = "fOo";
343 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2));
344 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2,2));
345 // \0-clenliness test
346 s1.assign("f\0oo",4);
347 s2.assign("f\0Oo",4);
348 CPPUNIT_ASSERT(s1.cmp(s2) > 0);
349 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2));
350 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2,3));
351 CPPUNIT_ASSERT_EQUAL(0,s1.caseCmp(s2,2));
352 CPPUNIT_ASSERT_EQUAL(0,s1.cmp(s2,2));
353
354 testComparisonStd("foo", "fooz");
355 testComparisonStd("foo", "foo");
356 testComparisonStd("foo", "f");
357 testComparisonStd("foo", "bar");
358
359 testComparisonStd("foo", "FOOZ");
360 testComparisonStd("foo", "FOO");
361 testComparisonStd("foo", "F");
362
363 testComparisonStdOneWay("", "");
364
365 // rare case C-string input matching SBuf with N>strlen(s)
366 {
367 char *right = xstrdup("foo34567890123456789012345678");
368 SBuf left("fooZYXWVUTSRQPONMLKJIHGFEDCBA");
369 // is 3 bytes in length. NEVER more.
370 right[3] = '\0';
371 left.setAt(3, '\0');
372
373 // pick another spot to truncate at if something goes horribly wrong.
374 right[14] = '\0';
375 left.setAt(14, '\0');
376
377 const SBuf::size_type maxN = 20 + min(left.length(), static_cast<SBuf::size_type>(strlen(right)));
378 for (SBuf::size_type n = 0; n <= maxN; ++n) {
379 if (sign(strncmp(left.rawContent(), right, n)) != sign(left.cmp(right, n)) )
380 std::cerr << std::endl << " cmp(char*) " << n << ' ' << left << " ?= " << right;
381 CPPUNIT_ASSERT_EQUAL(sign(strncmp(left.rawContent(), right, n)), sign(left.cmp(right, n)));
382 if (sign(strncasecmp(left.rawContent(), right, n)) != sign(left.caseCmp(right, n)))
383 std::cerr << std::endl << " caseCmp(char*) " << n << ' ' << left << " ?= " << right;
384 CPPUNIT_ASSERT_EQUAL(sign(strncasecmp(left.rawContent(), right, n)), sign(left.caseCmp(right, n)));
385 }
386 xfree(right);
387 }
388 }
389
390 void
391 testSBuf::testConsume()
392 {
393 SBuf s1(literal),s2,s3;
394 s2=s1.consume(4);
395 s3.assign("The ");
396 CPPUNIT_ASSERT_EQUAL(s2,s3);
397 s3.assign("quick brown fox jumped over the lazy dog");
398 CPPUNIT_ASSERT_EQUAL(s1,s3);
399 s1.consume(40);
400 CPPUNIT_ASSERT_EQUAL(s1,SBuf());
401 }
402
403 void
404 testSBuf::testRawContent()
405 {
406 SBuf s1(literal);
407 SBuf s2(s1);
408 s2.append("foo");
409 const char *foo;
410 foo = s1.rawContent();
411 CPPUNIT_ASSERT_EQUAL(0,strncmp(fox,foo,s1.length()));
412 foo = s1.c_str();
413 CPPUNIT_ASSERT(!strcmp(fox,foo));
414 }
415
416 void
417 testSBuf::testRawSpace()
418 {
419 SBuf s1(literal);
420 SBuf s2(fox1);
421 SBuf::size_type sz=s2.length();
422 char *rb=s2.rawSpace(strlen(fox2)+1);
423 strcpy(rb,fox2);
424 s2.forceSize(sz+strlen(fox2));
425 CPPUNIT_ASSERT_EQUAL(s1,s2);
426 }
427
428 void
429 testSBuf::testChop()
430 {
431 SBuf s1(literal),s2;
432 s1.chop(4,5);
433 s2.assign("quick");
434 CPPUNIT_ASSERT_EQUAL(s1,s2);
435 s1=literal;
436 s2.clear();
437 s1.chop(5,0);
438 CPPUNIT_ASSERT_EQUAL(s1,s2);
439 const char *alphabet="abcdefghijklmnopqrstuvwxyz";
440 SBuf a(alphabet);
441 std::string s(alphabet); // TODO
442 { //regular chopping
443 SBuf b(a);
444 b.chop(3,3);
445 SBuf ref("def");
446 CPPUNIT_ASSERT_EQUAL(ref,b);
447 }
448 { // chop at end
449 SBuf b(a);
450 b.chop(b.length()-3);
451 SBuf ref("xyz");
452 CPPUNIT_ASSERT_EQUAL(ref,b);
453 }
454 { // chop at beginning
455 SBuf b(a);
456 b.chop(0,3);
457 SBuf ref("abc");
458 CPPUNIT_ASSERT_EQUAL(ref,b);
459 }
460 { // chop to zero length
461 SBuf b(a);
462 b.chop(5,0);
463 SBuf ref("");
464 CPPUNIT_ASSERT_EQUAL(ref,b);
465 }
466 { // chop beyond end (at npos)
467 SBuf b(a);
468 b.chop(SBuf::npos,4);
469 SBuf ref("");
470 CPPUNIT_ASSERT_EQUAL(ref,b);
471 }
472 { // chop beyond end
473 SBuf b(a);
474 b.chop(b.length()+2,4);
475 SBuf ref("");
476 CPPUNIT_ASSERT_EQUAL(ref,b);
477 }
478 { // null-chop
479 SBuf b(a);
480 b.chop(0,b.length());
481 SBuf ref(a);
482 CPPUNIT_ASSERT_EQUAL(ref,b);
483 }
484 { // overflow chopped area
485 SBuf b(a);
486 b.chop(b.length()-3,b.length());
487 SBuf ref("xyz");
488 CPPUNIT_ASSERT_EQUAL(ref,b);
489 }
490 }
491
492 void
493 testSBuf::testChomp()
494 {
495 SBuf s1("complete string");
496 SBuf s2(s1);
497 s2.trim(SBuf(" ,"));
498 CPPUNIT_ASSERT_EQUAL(s1,s2);
499 s2.assign(" complete string ,");
500 s2.trim(SBuf(" ,"));
501 CPPUNIT_ASSERT_EQUAL(s1,s2);
502 s1.assign(", complete string ,");
503 s2=s1;
504 s2.trim(SBuf(" "));
505 CPPUNIT_ASSERT_EQUAL(s1,s2);
506 }
507
508 // inspired by SBufFindTest; to be expanded.
509 class SBufSubstrAutoTest
510 {
511 SBuf fullString, sb;
512 std::string fullReference, str;
513 public:
514 void performEqualityTest() {
515 SBuf ref(str);
516 CPPUNIT_ASSERT_EQUAL(ref,sb);
517 }
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();
524 }
525 }
526 }
527 };
528
529 void
530 testSBuf::testSubstr()
531 {
532 SBuf s1(literal),s2,s3;
533 s2=s1.substr(4,5);
534 s3.assign("quick");
535 CPPUNIT_ASSERT_EQUAL(s2,s3);
536 s1.chop(4,5);
537 CPPUNIT_ASSERT_EQUAL(s1,s2);
538 SBufSubstrAutoTest sat; // work done in the constructor
539 }
540
541 void
542 testSBuf::testFindChar()
543 {
544 const char *alphabet="abcdefghijklmnopqrstuvwxyz";
545 SBuf s1(alphabet);
546 SBuf::size_type idx;
547 SBuf::size_type nposResult=SBuf::npos;
548
549 // FORWARD SEARCH
550 // needle in haystack
551 idx=s1.find('d');
552 CPPUNIT_ASSERT_EQUAL(3U,idx);
553 CPPUNIT_ASSERT_EQUAL('d',s1[idx]);
554
555 // needle not present in haystack
556 idx=s1.find(' '); //fails
557 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
558
559 // search in portion
560 idx=s1.find('e',3U);
561 CPPUNIT_ASSERT_EQUAL(4U,idx);
562
563 // char not in searched portion
564 idx=s1.find('e',5U);
565 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
566
567 // invalid start position
568 idx=s1.find('d',SBuf::npos);
569 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
570
571 // search outside of haystack
572 idx=s1.find('d',s1.length()+1);
573 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
574
575 // REVERSE SEARCH
576 // needle in haystack
577 idx=s1.rfind('d');
578 CPPUNIT_ASSERT_EQUAL(3U, idx);
579 CPPUNIT_ASSERT_EQUAL('d', s1[idx]);
580
581 // needle not present in haystack
582 idx=s1.rfind(' '); //fails
583 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
584
585 // search in portion
586 idx=s1.rfind('e',5);
587 CPPUNIT_ASSERT_EQUAL(4U,idx);
588
589 // char not in searched portion
590 idx=s1.rfind('e',3);
591 CPPUNIT_ASSERT_EQUAL(nposResult,idx);
592
593 // overlong haystack specification
594 idx=s1.rfind('d',s1.length()+1);
595 CPPUNIT_ASSERT_EQUAL(3U,idx);
596 }
597
598 void
599 testSBuf::testFindSBuf()
600 {
601 const char *alphabet="abcdefghijklmnopqrstuvwxyz";
602 SBuf haystack(alphabet);
603 SBuf::size_type idx;
604 SBuf::size_type nposResult=SBuf::npos;
605
606 // FORWARD search
607 // needle in haystack
608 idx = haystack.find(SBuf("def"));
609 CPPUNIT_ASSERT_EQUAL(3U,idx);
610
611 idx = haystack.find(SBuf("xyz"));
612 CPPUNIT_ASSERT_EQUAL(23U,idx);
613
614 // needle not in haystack, no initial char match
615 idx = haystack.find(SBuf(" eq"));
616 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
617
618 // needle not in haystack, initial sequence match
619 idx = haystack.find(SBuf("deg"));
620 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
621
622 // needle past end of haystack
623 idx = haystack.find(SBuf("xyz1"));
624 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
625
626 // search in portion: needle not in searched part
627 idx = haystack.find(SBuf("def"),7);
628 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
629
630 // search in portion: overhang
631 idx = haystack.find(SBuf("def"),4);
632 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
633
634 // invalid start position
635 idx = haystack.find(SBuf("def"),SBuf::npos);
636 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
637
638 // needle bigger than haystack
639 idx = SBuf("def").find(haystack);
640 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
641
642 // search in a double-matching haystack
643 {
644 SBuf h2=haystack;
645 h2.append(haystack);
646
647 idx = h2.find(SBuf("def"));
648 CPPUNIT_ASSERT_EQUAL(3U,idx);
649
650 idx = h2.find(SBuf("xyzab"));
651 CPPUNIT_ASSERT_EQUAL(23U,idx);
652 }
653
654 // REVERSE search
655 // needle in haystack
656 idx = haystack.rfind(SBuf("def"));
657 CPPUNIT_ASSERT_EQUAL(3U,idx);
658
659 idx = haystack.rfind(SBuf("xyz"));
660 CPPUNIT_ASSERT_EQUAL(23U,idx);
661
662 // needle not in haystack, no initial char match
663 idx = haystack.rfind(SBuf(" eq"));
664 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
665
666 // needle not in haystack, initial sequence match
667 idx = haystack.rfind(SBuf("deg"));
668 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
669
670 // needle past end of haystack
671 idx = haystack.rfind(SBuf("xyz1"));
672 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
673
674 // search in portion: needle in searched part
675 idx = haystack.rfind(SBuf("def"),7);
676 CPPUNIT_ASSERT_EQUAL(3U, idx);
677
678 // search in portion: needle not in searched part
679 idx = haystack.rfind(SBuf("mno"),3);
680 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
681
682 // search in portion: overhang
683 idx = haystack.rfind(SBuf("def"),4);
684 CPPUNIT_ASSERT_EQUAL(3U, idx);
685
686 // npos start position
687 idx = haystack.rfind(SBuf("def"),SBuf::npos);
688 CPPUNIT_ASSERT_EQUAL(3U, idx);
689
690 // needle bigger than haystack
691 idx = SBuf("def").rfind(haystack);
692 CPPUNIT_ASSERT_EQUAL(nposResult, idx);
693
694 // search in a double-matching haystack
695 {
696 SBuf h2=haystack;
697 h2.append(haystack);
698
699 idx = h2.rfind(SBuf("def"));
700 CPPUNIT_ASSERT_EQUAL(29U,idx);
701
702 idx = h2.find(SBuf("xyzab"));
703 CPPUNIT_ASSERT_EQUAL(23U,idx);
704 }
705 }
706
707 void
708 testSBuf::testRFindChar()
709 {
710 SBuf s1(literal);
711 SBuf::size_type idx;
712 idx=s1.rfind(' ');
713 CPPUNIT_ASSERT_EQUAL(40U,idx);
714 CPPUNIT_ASSERT_EQUAL(' ',s1[idx]);
715 }
716
717 void
718 testSBuf::testRFindSBuf()
719 {
720 SBuf haystack(literal),afox("fox");
721 SBuf goobar("goobar");
722 SBuf::size_type idx;
723
724 // corner case: search for a zero-length SBuf
725 idx=haystack.rfind(SBuf(""));
726 CPPUNIT_ASSERT_EQUAL(haystack.length(),idx);
727
728 // corner case: search for a needle longer than the haystack
729 idx=afox.rfind(SBuf(" "));
730 CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
731
732 idx=haystack.rfind(SBuf("fox"));
733 CPPUNIT_ASSERT_EQUAL(16U,idx);
734
735 // needle not found, no match for first char
736 idx=goobar.rfind(SBuf("foo"));
737 CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
738
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);
742
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]);
747
748 idx=haystack.rfind(SBuf("The"));
749 CPPUNIT_ASSERT_EQUAL(0U,idx);
750
751 haystack.append("The");
752 idx=haystack.rfind(SBuf("The"));
753 CPPUNIT_ASSERT_EQUAL(44U,idx);
754
755 //partial match
756 haystack="The quick brown fox";
757 SBuf needle("foxy lady");
758 idx=haystack.rfind(needle);
759 CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
760 }
761
762 void
763 testSBuf::testSBufLength()
764 {
765 SBuf s(fox);
766 CPPUNIT_ASSERT_EQUAL(strlen(fox),(size_t)s.length());
767 }
768
769 void
770 testSBuf::testCopy()
771 {
772 char buf[40]; //shorter than literal()
773 SBuf s(fox1),s2;
774 CPPUNIT_ASSERT_EQUAL(s.length(),s.copy(buf,40));
775 CPPUNIT_ASSERT_EQUAL(0,strncmp(s.rawContent(),buf,s.length()));
776 s=literal;
777 CPPUNIT_ASSERT_EQUAL(40U,s.copy(buf,40));
778 s2.assign(buf,40);
779 s.chop(0,40);
780 CPPUNIT_ASSERT_EQUAL(s2,s);
781 }
782
783 void
784 testSBuf::testStringOps()
785 {
786 SBuf sng(ToLower(literal)),
787 ref("the quick brown fox jumped over the lazy dog");
788 CPPUNIT_ASSERT_EQUAL(ref,sng);
789 sng=literal;
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));
795 }
796
797 void
798 testSBuf::testGrow()
799 {
800 SBuf t;
801 t.assign("foo");
802 const char *ref=t.rawContent();
803 t.reserveCapacity(10240);
804 const char *match=t.rawContent();
805 CPPUNIT_ASSERT(match!=ref);
806 ref=match;
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);
810 }
811
812 void
813 testSBuf::testReserve()
814 {
815 SBufReservationRequirements requirements;
816 // use unusual numbers to ensure we dont hit a lucky boundary situation
817 requirements.minSpace = 10;
818 requirements.idealSpace = 82;
819 requirements.maxCapacity = 259;
820 requirements.allowShared = true;
821
822 // for each possible starting buffer length within the capacity
823 for (SBuf::size_type startLength = 0; startLength <= requirements.maxCapacity; ++startLength) {
824 std::cerr << ".";
825 SBuf b;
826 b.reserveCapacity(startLength);
827 CPPUNIT_ASSERT_EQUAL(b.length(), static_cast<unsigned int>(0));
828 CPPUNIT_ASSERT_EQUAL(b.spaceSize(), startLength);
829
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);
840 if (x > 0)
841 b.append('X');
842 }
843 }
844
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;
849 SBuf buffer;
850 buffer.reserve(requirements);
851 CPPUNIT_ASSERT(buffer.spaceSize() >= requirements.minSpace);
852 }
853 }
854
855 void
856 testSBuf::testStartsWith()
857 {
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
862
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));
870 }
871
872 void
873 testSBuf::testSBufStream()
874 {
875 SBuf b("const.string, int 10 and a float 10.5");
876 SBufStream ss;
877 ss << "const.string, int " << 10 << " and a float " << 10.5;
878 SBuf o=ss.buf();
879 CPPUNIT_ASSERT_EQUAL(b,o);
880 ss.clearBuf();
881 o=ss.buf();
882 CPPUNIT_ASSERT_EQUAL(SBuf(),o);
883 SBuf f1(fox1);
884 SBufStream ss2(f1);
885 ss2 << fox2;
886 CPPUNIT_ASSERT_EQUAL(ss2.buf(),literal);
887 CPPUNIT_ASSERT_EQUAL(f1,SBuf(fox1));
888 }
889
890 void
891 testSBuf::testFindFirstOf()
892 {
893 SBuf haystack(literal);
894 SBuf::size_type idx;
895
896 // not found
897 idx=haystack.findFirstOf(CharacterSet("t1","ADHRWYP"));
898 CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
899
900 // found at beginning
901 idx=haystack.findFirstOf(CharacterSet("t2","THANDF"));
902 CPPUNIT_ASSERT_EQUAL(0U,idx);
903
904 //found at end of haystack
905 idx=haystack.findFirstOf(CharacterSet("t3","QWERYVg"));
906 CPPUNIT_ASSERT_EQUAL(haystack.length()-1,idx);
907
908 //found in the middle of haystack
909 idx=haystack.findFirstOf(CharacterSet("t4","QWERqYV"));
910 CPPUNIT_ASSERT_EQUAL(4U,idx);
911 }
912
913 void
914 testSBuf::testFindFirstNotOf()
915 {
916 SBuf haystack(literal);
917 SBuf::size_type idx;
918
919 // all chars from the set
920 idx=haystack.findFirstNotOf(CharacterSet("t1",literal.c_str()));
921 CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
922
923 // found at beginning
924 idx=haystack.findFirstNotOf(CharacterSet("t2","a"));
925 CPPUNIT_ASSERT_EQUAL(0U,idx);
926
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);
930
931 //found in the middle of haystack
932 idx=haystack.findFirstNotOf(CharacterSet("t4","The"));
933 CPPUNIT_ASSERT_EQUAL(3U,idx);
934 }
935
936 void
937 testSBuf::testAutoFind()
938 {
939 SBufFindTest test;
940 test.run();
941 }
942
943 void
944 testSBuf::testStdStringOps()
945 {
946 const char *alphabet="abcdefghijklmnopqrstuvwxyz";
947 std::string astr(alphabet);
948 SBuf sb(alphabet);
949 CPPUNIT_ASSERT_EQUAL(astr,sb.toStdString());
950 }
951
952 void
953 testSBuf::testIterators()
954 {
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());
959 {
960 auto i = text.begin();
961 auto e = text.end();
962 CPPUNIT_ASSERT_EQUAL('f', *i);
963 CPPUNIT_ASSERT(i != e);
964 ++i;
965 CPPUNIT_ASSERT_EQUAL('o', *i);
966 CPPUNIT_ASSERT(i != e);
967 ++i;
968 CPPUNIT_ASSERT_EQUAL('o', *i);
969 CPPUNIT_ASSERT(i != e);
970 ++i;
971 CPPUNIT_ASSERT(i == e);
972 }
973 {
974 auto i = text.rbegin();
975 auto e = text.rend();
976 CPPUNIT_ASSERT_EQUAL('o', *i);
977 CPPUNIT_ASSERT(i != e);
978 ++i;
979 CPPUNIT_ASSERT_EQUAL('o', *i);
980 CPPUNIT_ASSERT(i != e);
981 ++i;
982 CPPUNIT_ASSERT_EQUAL('f', *i);
983 CPPUNIT_ASSERT(i != e);
984 ++i;
985 CPPUNIT_ASSERT(i == e);
986 }
987 }
988
989 void
990 testSBuf::testSBufHash()
991 {
992 // same SBuf must have same hash
993 auto hasher=std::hash<SBuf>();
994 CPPUNIT_ASSERT_EQUAL(hasher(literal),hasher(literal));
995
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)));
999
1000 //differen content should have different hash
1001 CPPUNIT_ASSERT(hasher(SBuf(fox)) != hasher(SBuf(fox1)));
1002
1003 {
1004 std::unordered_map<SBuf, int> um;
1005 um[SBuf("one")] = 1;
1006 um[SBuf("two")] = 2;
1007
1008 auto i = um.find(SBuf("one"));
1009 CPPUNIT_ASSERT(i != um.end());
1010 CPPUNIT_ASSERT(i->second == 1);
1011
1012 i = um.find(SBuf("eleventy"));
1013 CPPUNIT_ASSERT(i == um.end());
1014 }
1015 }
1016