2 // This source file is part of the bstring string library. This code was
3 // written by Paul Hsieh in 2002-2006, and is covered by the BSD open source
4 // license. Refer to the accompanying documentation for details on usage and
11 // This file is the C++ unit test for Bstrlib
19 // Exceptions must be turned on in the compiler to successfully run
20 // this test. The compiler must also support STL.
22 #define dumpOutQty (32)
23 static bstring dumpOut
[dumpOutQty
];
24 static unsigned int rot
= 0;
26 char * dumpBstring (const bstring b
) {
27 rot
= (rot
+ 1) % (unsigned) dumpOutQty
;
28 if (dumpOut
[rot
] == NULL
) {
29 dumpOut
[rot
] = bfromcstr ("");
30 if (dumpOut
[rot
] == NULL
) return "FATAL INTERNAL ERROR";
32 dumpOut
[rot
]->slen
= 0;
34 bcatcstr (dumpOut
[rot
], "NULL");
37 sprintf (msg
, "%p", (void *)b
);
38 bcatcstr (dumpOut
[rot
], msg
);
41 sprintf (msg
, ":[err:slen=%d<0]", b
->slen
);
42 bcatcstr (dumpOut
[rot
], msg
);
44 if (b
->mlen
> 0 && b
->mlen
< b
->slen
) {
45 sprintf (msg
, ":[err:mlen=%d<slen=%d]", b
->mlen
, b
->slen
);
46 bcatcstr (dumpOut
[rot
], msg
);
49 bcatcstr (dumpOut
[rot
], "[p]");
50 } else if (b
->mlen
< 0) {
51 bcatcstr (dumpOut
[rot
], "[c]");
53 bcatcstr (dumpOut
[rot
], ":");
54 if (b
->data
== NULL
) {
55 bcatcstr (dumpOut
[rot
], "[err:data=NULL]");
57 bcatcstr (dumpOut
[rot
], "\"");
58 bcatcstr (dumpOut
[rot
], (const char *) b
->data
);
59 bcatcstr (dumpOut
[rot
], "\"");
64 return (char *) dumpOut
[rot
]->data
;
70 printf ("TEST: CBString constructor\n");
73 printf ("\tCBString c;\n");
75 ret
+= (0 != c0
.length());
76 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
78 printf ("\tCBString c(\"test\");\n");
80 ret
+= (c1
!= "test");
81 ret
+= '\0' != ((const char *)c1
)[c1
.length()];
83 printf ("\tCBString c(25, \"test\");\n");
84 CBString
c8 (25, "test");
85 ret
+= (c8
!= "test");
87 ret
+= '\0' != ((const char *)c8
)[c8
.length()];
89 printf ("\tCBString c('t');\n");
92 ret
+= '\0' != ((const char *)c2
)[c2
.length()];
94 printf ("\tCBString c('\\0');\n");
96 ret
+= (1 != c3
.length()) || ('\0' != c3
[0]);
97 ret
+= '\0' != ((const char *)c3
)[c3
.length()];
99 printf ("\tCBString c(bstr[\"test\"]);\n");
100 struct tagbstring t
= bsStatic ("test");
102 ret
+= (c4
!= t
.data
);
103 ret
+= '\0' != ((const char *)c4
)[c4
.length()];
105 printf ("\tCBString c(CBstr[\"test\"]);\n");
108 ret
+= '\0' != ((const char *)c5
)[c5
.length()];
110 printf ("\tCBString c('x',5);\n");
112 ret
+= (c6
!= "xxxxx");
113 ret
+= '\0' != ((const char *)c6
)[c6
.length()];
115 printf ("\tCBString c(\"123456\",4);\n");
116 CBString
c7 ((void *)"123456",4);
117 ret
+= (c7
!= "1234");
118 ret
+= '\0' != ((const char *)c7
)[c7
.length()];
121 catch (struct CBStringException err
) {
122 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
126 printf ("\t# failures: %d\n", ret
);
130 #define EXCEPTION_EXPECTED(line) \
134 printf ("\tException was expected\n"); \
136 catch (struct CBStringException) { }
141 printf ("TEST: CBString = operator\n");
145 struct tagbstring t
= bsStatic ("test");
147 ret
+= c0
.iswriteprotected();
149 ret
+= 1 != c0
.iswriteprotected();
150 EXCEPTION_EXPECTED (c0
= 'x');
151 EXCEPTION_EXPECTED (c0
= (unsigned char) 'x');
152 EXCEPTION_EXPECTED (c0
= "test");
153 EXCEPTION_EXPECTED (c0
= CBString ("test"));
154 EXCEPTION_EXPECTED (c0
= t
);
156 catch (struct CBStringException err
) {
157 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
163 struct tagbstring t
= bsStatic ("test");
165 printf ("\tc = 'x';\n");
168 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
169 printf ("\tc = (unsigned char)'x';\n");
170 c0
= (unsigned char) 'x';
172 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
173 printf ("\tc = \"test\";\n");
175 ret
+= (c0
!= "test");
176 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
177 printf ("\tc = CBStr[\"test\"];\n");
180 ret
+= '\0' != ((const char *)c1
)[c1
.length()];
181 printf ("\tc = tbstr[\"test\"];\n");
183 ret
+= (c0
!= "test");
184 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
187 catch (struct CBStringException err
) {
188 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
192 printf ("\t# failures: %d\n", ret
);
199 printf ("TEST: CBString += operator\n");
203 struct tagbstring t
= bsStatic ("test");
206 EXCEPTION_EXPECTED (c0
+= 'x');
207 EXCEPTION_EXPECTED (c0
+= (unsigned char) 'x');
208 EXCEPTION_EXPECTED (c0
+= "test");
209 EXCEPTION_EXPECTED (c0
+= CBString ("test"));
210 EXCEPTION_EXPECTED (c0
+= t
);
212 catch (struct CBStringException err
) {
213 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
219 struct tagbstring t
= bsStatic ("extra");
222 printf ("\tc += 'x';\n");
224 ret
+= (c0
!= "testx");
225 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
226 printf ("\tc += (unsigned char)'x';\n");
227 c0
+= (unsigned char) 'y';
228 ret
+= (c0
!= "testxy");
229 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
230 printf ("\tc += \"test\";\n");
232 ret
+= (c0
!= "testxytest");
233 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
234 printf ("\tc += CBStr[\"test\"];\n");
236 ret
+= (c0
!= "testxytesttestxytest");
237 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
238 printf ("\tc += tbstr[\"test\"];\n");
240 ret
+= (c0
!= "testxytesttestxytestextra");
241 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
244 catch (struct CBStringException err
) {
245 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
249 printf ("\t# failures: %d\n", ret
);
258 struct tagbstring t
= bsStatic ("extra");
260 printf ("TEST: CBString + operator\n");
263 printf ("\tc + 'x';\n");
265 ret
+= (c0
!= "testx");
266 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
267 printf ("\tc + (unsigned char)'x';\n");
268 c0
= c1
+ (unsigned char) 'y';
269 ret
+= (c0
!= "testy");
270 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
271 printf ("\tc + \"test\";\n");
272 c0
= c1
+ (const char *) "stuff";
273 ret
+= (c0
!= "teststuff");
274 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
275 printf ("\tc + (unsigned char *) \"test\";\n");
276 c0
= c1
+ (const unsigned char *) "stuff";
277 ret
+= (c0
!= "teststuff");
278 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
279 printf ("\tc + CBStr[\"test\"];\n");
280 c0
= c1
+ CBString ("other");
281 ret
+= (c0
!= "testother");
282 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
283 printf ("\tc + tbstr[\"test\"];\n");
285 ret
+= (c0
!= "testextra");
286 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
288 printf ("TEST: + CBString operator\n");
290 printf ("\t'x' + c;\n");
292 ret
+= (c0
!= "xtest");
293 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
294 printf ("\t(unsigned char)'y' + c;\n");
295 c0
= (unsigned char) 'y' + c1
;
296 ret
+= (c0
!= "ytest");
297 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
298 printf ("\t\"test\" + c;\n");
299 c0
= (const char *) "stuff" + c1
;
300 ret
+= (c0
!= "stufftest");
301 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
302 printf ("\t(unsigned char *) \"test\" + c;\n");
303 c0
= (const unsigned char *) "stuff" + c1
;
304 ret
+= (c0
!= "stufftest");
305 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
306 printf ("\ttbstr[\"extra\"] + c;\n");
308 ret
+= (c0
!= "extratest");
309 ret
+= '\0' != ((const char *)c0
)[c0
.length()];
312 catch (struct CBStringException err
) {
313 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
317 printf ("\t# failures: %d\n", ret
);
325 printf ("TEST: CBString == operator\n");
332 printf ("\tc == d;\n");
336 printf ("\tc == \"test\";\n");
337 ret
+= !(c0
== "test");
338 ret
+= (c2
== "test");
340 printf ("\tc == (unsigned char *) \"test\";\n");
341 ret
+= !(c0
== (unsigned char *) "test");
342 ret
+= (c2
== (unsigned char *) "test");
345 catch (struct CBStringException err
) {
346 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
350 printf ("\t# failures: %d\n", ret
);
358 printf ("TEST: CBString != operator\n");
365 printf ("\tc != d;\n");
369 printf ("\tc != \"test\";\n");
370 ret
+= (c0
!= "test");
371 ret
+= !(c2
!= "test");
373 printf ("\tc != (unsigned char *) \"test\";\n");
374 ret
+= (c0
!= (unsigned char *) "test");
375 ret
+= !(c2
!= (unsigned char *) "test");
378 catch (struct CBStringException err
) {
379 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
383 printf ("\t# failures: %d\n", ret
);
391 printf ("TEST: CBString <, <= operators\n");
398 printf ("\tc < d;\n");
404 printf ("\tc <= d;\n");
410 printf ("\tc < \"test\";\n");
411 ret
+= (c0
< "test");
412 ret
+= (c1
< "test");
413 ret
+= !(c2
< "test");
414 ret
+= (c0
< "other");
415 ret
+= (c1
< "other");
416 ret
+= (c2
< "other");
418 printf ("\tc <= \"test\";\n");
419 ret
+= !(c0
<= "test");
420 ret
+= !(c1
<= "test");
421 ret
+= !(c2
<= "test");
422 ret
+= (c0
<= "other");
423 ret
+= (c1
<= "other");
424 ret
+= !(c2
<= "other");
426 printf ("\tc < (unsigned char *) \"test\";\n");
427 ret
+= (c0
< (const char *) "test");
428 ret
+= (c1
< (const char *) "test");
429 ret
+= !(c2
< (const char *) "test");
430 ret
+= (c0
< (const char *) "other");
431 ret
+= (c1
< (const char *) "other");
432 ret
+= (c2
< (const char *) "other");
434 printf ("\tc <= (unsigned char *) \"test\";\n");
435 ret
+= !(c0
<= (const char *) "test");
436 ret
+= !(c1
<= (const char *) "test");
437 ret
+= !(c2
<= (const char *) "test");
438 ret
+= (c0
<= (const char *) "other");
439 ret
+= (c1
<= (const char *) "other");
440 ret
+= !(c2
<= (const char *) "other");
443 catch (struct CBStringException err
) {
444 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
448 printf ("\t# failures: %d\n", ret
);
456 printf ("TEST: CBString >, >= operators\n");
463 printf ("\tc >= d;\n");
469 printf ("\tc > d;\n");
475 printf ("\tc >= \"test\";\n");
476 ret
+= !(c0
>= "test");
477 ret
+= !(c1
>= "test");
478 ret
+= (c2
>= "test");
479 ret
+= !(c0
>= "other");
480 ret
+= !(c1
>= "other");
481 ret
+= !(c2
>= "other");
483 printf ("\tc > \"test\";\n");
484 ret
+= (c0
> "test");
485 ret
+= (c1
> "test");
486 ret
+= (c2
> "test");
487 ret
+= !(c0
> "other");
488 ret
+= !(c1
> "other");
489 ret
+= (c2
> "other");
491 printf ("\tc >= (unsigned char *) \"test\";\n");
492 ret
+= !(c0
>= (const char *) "test");
493 ret
+= !(c1
>= (const char *) "test");
494 ret
+= (c2
>= (const char *) "test");
495 ret
+= !(c0
>= (const char *) "other");
496 ret
+= !(c1
>= (const char *) "other");
497 ret
+= !(c2
>= (const char *) "other");
499 printf ("\tc > (unsigned char *) \"test\";\n");
500 ret
+= (c0
> (const char *) "test");
501 ret
+= (c1
> (const char *) "test");
502 ret
+= (c2
> (const char *) "test");
503 ret
+= !(c0
> (const char *) "other");
504 ret
+= !(c1
> (const char *) "other");
505 ret
+= (c2
> (const char *) "other");
508 catch (struct CBStringException err
) {
509 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
513 printf ("\t# failures: %d\n", ret
);
521 printf ("TEST: (const char *) CBString operator\n");
523 CBString
c0 ("test"), c1 ("other");
525 printf ("\t(const char *) CBString\n");
526 ret
+= 0 != memcmp ((const char *) c0
, "test", 5);
527 ret
+= 0 != memcmp ((const char *) c1
, "other", 6);
529 printf ("\t(const unsigned char *) CBString\n");
530 ret
+= 0 != memcmp ((const unsigned char *) c0
, "test", 5);
531 ret
+= 0 != memcmp ((const unsigned char *) c1
, "other", 6);
534 catch (struct CBStringException err
) {
535 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
539 printf ("\t# failures: %d\n", ret
);
547 printf ("TEST: (double), (float), (int) CBString operators\n");
548 CBString
c0 ("1.2e3"), c1("100"), c2("100.55");
549 printf ("\t(double) \"%s\"\n", (const char *) c0
);
550 ret
+= 1.2e3
!= (double) c0
;
551 printf ("\t(float) \"%s\"\n", (const char *) c0
);
552 ret
+= 1.2e3
!= (float) c0
;
553 printf ("\t(int) \"%s\"\n", (const char *) c1
);
554 ret
+= 100 != (float) c1
;
555 printf ("\t(int) \"%s\"\n", (const char *) c2
);
556 ret
+= 100 != (int) c2
;
557 printf ("\t(unsigned int) \"%s\"\n", (const char *) c2
);
558 ret
+= 100 != (unsigned int) c2
;
560 catch (struct CBStringException err
) {
561 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
566 CBString
c0 ("xxxxx");
567 printf ("\t(double) \"%s\"\n", (const char *) c0
);
568 ret
+= -1.2e3
!= (double) c0
;
570 catch (struct CBStringException err
) {
571 printf ("\tException (%s) correctly thrown\n", err
.what());
575 CBString
c0 ("xxxxx");
576 printf ("\t(float) \"%s\"\n", (const char *) c0
);
577 ret
+= -1.2e3
!= (float) c0
;
579 catch (struct CBStringException err
) {
580 printf ("\tException (%s) correctly thrown\n", err
.what());
584 CBString
c0 ("xxxxx");
585 printf ("\t(int) \"%s\"\n", (const char *) c0
);
586 ret
+= -100 != (int) c0
;
588 catch (struct CBStringException err
) {
589 printf ("\tException (%s) correctly thrown\n", err
.what());
593 CBString
c0 ("xxxxx");
594 printf ("\t(unsigned int) \"%s\"\n", (const char *) c0
);
595 ret
+= 1000 != (unsigned int) c0
;
597 catch (struct CBStringException err
) {
598 printf ("\tException (%s) correctly thrown\n", err
.what());
601 printf ("\t# failures: %d\n", ret
);
609 printf ("TEST: length() method\n");
610 CBString c0
, c1("Test");
612 printf ("\t\"%s\".length();\n", (const char *) c0
);
613 ret
+= 0 != c0
.length();
614 printf ("\t\"%s\".length();\n", (const char *) c1
);
615 ret
+= 4 != c1
.length();
618 catch (struct CBStringException err
) {
619 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
623 printf ("\t# failures: %d\n", ret
);
630 printf ("TEST: character() method, [] operator\n");
636 ret
+= (1 + c0
[0]) != 'u';
637 ret
+= ((unsigned char) c0
[0] + 1) != 'u';
638 ret
+= c0
.character(0) != 't';
639 EXCEPTION_EXPECTED (c0
[0] = 'x');
640 EXCEPTION_EXPECTED (c0
.character(0) = 'x');
642 catch (struct CBStringException err
) {
643 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
650 printf ("\t\"%s\".character ();\n", (const char *) c0
);
651 ret
+= 's' != c0
.character (2);
652 c0
.character (2) = 'x';
655 printf ("\t\"%s\"[];\n", (const char *) c0
);
660 catch (struct CBStringException err
) {
661 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
667 printf ("\t\"%s\".character ();\n", (const char *) c0
);
668 ret
+= '?' != c0
.character (0);
670 catch (struct CBStringException err
) {
671 printf ("\tException (%s) correctly thrown\n", err
.what());
676 printf ("\t\"%s\"[];\n", (const char *) c0
);
679 catch (struct CBStringException err
) {
680 printf ("\tException (%s) correctly thrown\n", err
.what());
683 printf ("\t# failures: %d\n", ret
);
690 #ifndef BSTRLIB_NOVSNP
691 printf ("TEST: format(), formata() methods\n");
697 EXCEPTION_EXPECTED (c0
.format ("%s(%d)", "extra", 4));
698 EXCEPTION_EXPECTED (c0
.formata ("%s(%d)", "extra", 4));
700 catch (struct CBStringException err
) {
701 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
706 CBString c0
, c1("Test"), c2
, c3
;
708 printf ("\tc.format (...);\n");
709 c0
.format ("%s(%d)", "extra", 4);
710 ret
+= c0
!= "extra(4)";
712 c2
= c0
+ c0
+ c0
+ c0
;
715 c3
.format ("x%s%s%s%s%s%s%s%s", (const char *) c0
, (const char *) c0
716 , (const char *) c0
, (const char *) c0
717 , (const char *) c0
, (const char *) c0
718 , (const char *) c0
, (const char *) c0
);
721 printf ("\t\"%s\".formata (...);\n", (const char *) c1
);
722 c1
.formata ("%s(%d)", "extra", 4);
723 ret
+= c1
!= "Testextra(4)";
725 c2
= c0
+ c0
+ c0
+ c0
;
729 c3
.formata ("%s%s%s%s%s%s%s%s", (const char *) c0
, (const char *) c0
730 , (const char *) c0
, (const char *) c0
731 , (const char *) c0
, (const char *) c0
732 , (const char *) c0
, (const char *) c0
);
735 catch (struct CBStringException err
) {
736 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
740 printf ("\t# failures: %d\n", ret
);
749 printf ("TEST: find() method\n");
750 CBString c0
, c1("Test");
752 printf ("\t\"%s\".find (CBString());\n", (const char *) c0
);
753 ret
+= -1 != c0
.find (CBString("x"));
754 ret
+= 1 != c1
.find (CBString("e"));
756 printf ("\t\"%s\".find (char *);\n", (const char *) c0
);
757 ret
+= -1 != c0
.find ("x");
758 ret
+= 1 != c1
.find ("e");
760 ret
+= 8 != CBString ("sssssssssap").find ("sap");
761 ret
+= 9 != CBString ("sssssssssap").find ("ap");
762 ret
+= 9 != CBString ("sssssssssap").find ("ap", 3);
763 ret
+= 9 != CBString ("sssssssssap").find ("a");
764 ret
+= 9 != CBString ("sssssssssap").find ("a", 3);
765 ret
+= -1 != CBString ("sssssssssap").find ("x");
766 ret
+= -1 != CBString ("sssssssssap").find ("x", 3);
767 ret
+= -1 != CBString ("sssssssssap").find ("ax");
768 ret
+= -1 != CBString ("sssssssssap").find ("ax", 3);
769 ret
+= -1 != CBString ("sssssssssap").find ("sax");
770 ret
+= -1 != CBString ("sssssssssap").find ("sax", 1);
771 ret
+= 8 != CBString ("sssssssssap").find ("sap", 3);
772 ret
+= 9 != CBString ("ssssssssssap").find ("sap", 3);
773 ret
+= 0 != CBString ("sssssssssap").find ("s");
774 ret
+= 3 != CBString ("sssssssssap").find ("s", 3);
775 ret
+= 9 != CBString ("sssssssssap").find ("a");
776 ret
+= 9 != CBString ("sssssssssap").find ("a", 5);
777 ret
+= 8 != CBString ("sasasasasap").find ("sap");
778 ret
+= 9 != CBString ("ssasasasasap").find ("sap");
780 printf ("\t\"%s\".find (char);\n", (const char *) c0
);
781 ret
+= -1 != c0
.find ('x');
782 ret
+= 1 != c1
.find ('e');
784 printf ("TEST: reversefind () method\n");
785 printf ("\t\"%s\".reversefind (CBString());\n", (const char *) c0
);
786 ret
+= -1 != c0
.reversefind (CBString("x"), c0
.length());
787 ret
+= 1 != c1
.reversefind (CBString("e"), c1
.length());
789 printf ("\t\"%s\".reversefind (char *);\n", (const char *) c0
);
790 ret
+= -1 != c0
.reversefind ("x", c0
.length());
791 ret
+= 1 != c1
.reversefind ("e", c1
.length());
793 printf ("\t\"%s\".reversefind (char);\n", (const char *) c0
);
794 ret
+= -1 != c0
.reversefind ('x', c0
.length());
795 ret
+= 1 != c1
.reversefind ('e', c1
.length());
798 catch (struct CBStringException err
) {
799 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
803 printf ("\t# failures: %d\n", ret
);
811 printf ("TEST: findchr(), reversefindchr() methods\n");
812 CBString c0
, c1("Test");
814 printf ("\t\"%s\".findchr (CBString(\"abcdef\"));\n", (const char *) c0
);
815 ret
+= -1 != c0
.findchr (CBString ("abcdef"));
816 printf ("\t\"%s\".findchr (CBString(\"abcdef\"));\n", (const char *) c1
);
817 ret
+= 1 != c1
.findchr (CBString ("abcdef"));
818 printf ("\t\"%s\".findchr (\"abcdef\");\n", (const char *) c0
);
819 ret
+= -1 != c0
.findchr ("abcdef");
820 printf ("\t\"%s\".findchr (\"abcdef\");\n", (const char *) c1
);
821 ret
+= 1 != c1
.findchr ("abcdef");
823 printf ("\t\"%s\".reversefindchr (CBString(\"abcdef\"));\n", (const char *) c0
);
824 ret
+= -1 != c0
.reversefindchr (CBString ("abcdef"), c0
.length());
825 printf ("\t\"%s\".reversefindchr (CBString(\"abcdef\"));\n", (const char *) c1
);
826 ret
+= 1 != c1
.reversefindchr (CBString ("abcdef"), c1
.length());
827 printf ("\t\"%s\".reversefindchr (\"abcdef\");\n", (const char *) c0
);
828 ret
+= -1 != c0
.reversefindchr ("abcdef", c0
.length());
829 printf ("\t\"%s\".reversefindchr (\"abcdef\");\n", (const char *) c1
);
830 ret
+= 1 != c1
.reversefindchr ("abcdef", c1
.length());
833 catch (struct CBStringException err
) {
834 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
838 printf ("\t# failures: %d\n", ret
);
846 printf ("TEST: nfindchr(), nreversefindchr() methods\n");
847 CBString c0
, c1("Test");
849 printf ("\t\"%s\".nfindchr (CBString(\"abcdef\"));\n", (const char *) c0
);
850 ret
+= -1 != c0
.nfindchr (CBString ("abcdef"));
851 printf ("\t\"%s\".nfindchr (CBString(\"abcdef\"));\n", (const char *) c1
);
852 ret
+= 0 != c1
.nfindchr (CBString ("abcdef"));
853 printf ("\t\"%s\".nfindchr (\"abcdef\");\n", (const char *) c0
);
854 ret
+= -1 != c0
.nfindchr ("abcdef");
855 printf ("\t\"%s\".nfindchr (\"abcdef\");\n", (const char *) c1
);
856 ret
+= 0 != c1
.nfindchr ("abcdef");
858 printf ("\t\"%s\".nreversefindchr (CBString(\"abcdef\"));\n", (const char *) c0
);
859 ret
+= -1 != c0
.nreversefindchr (CBString ("abcdef"), c0
.length());
860 printf ("\t\"%s\".nreversefindchr (CBString(\"abcdef\"));\n", (const char *) c1
);
861 ret
+= 3 != c1
.nreversefindchr (CBString ("abcdef"), c1
.length());
862 printf ("\t\"%s\".nreversefindchr (\"abcdef\");\n", (const char *) c0
);
863 ret
+= -1 != c0
.nreversefindchr ("abcdef", c0
.length());
864 printf ("\t\"%s\".nreversefindchr (\"abcdef\");\n", (const char *) c1
);
865 ret
+= 3 != c1
.nreversefindchr ("abcdef", c1
.length());
868 catch (struct CBStringException err
) {
869 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
873 printf ("\t# failures: %d\n", ret
);
880 printf ("TEST: midstr() method\n");
883 CBString c0
, c1("bogus"), c2
;
885 printf ("\t\"%s\".midstr (1,3)\n", (const char *) c0
);
886 ret
+= (c2
= c0
.midstr (1,3)) != "";
887 ret
+= '\0' != ((const char *)c2
)[c2
.length ()];
889 printf ("\t\"%s\".midstr (1,3)\n", (const char *) c1
);
890 ret
+= (c2
= c1
.midstr (1,3)) != "ogu";
891 ret
+= '\0' != ((const char *)c2
)[c2
.length ()];
894 catch (struct CBStringException err
) {
895 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
899 printf ("\t# failures: %d\n", ret
);
906 printf ("TEST: fill() method\n");
912 EXCEPTION_EXPECTED (c0
.fill (5, 'x'));
914 catch (struct CBStringException err
) {
915 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
920 CBString c0
, c1("Test-test");
922 printf ("\t\"%s\".fill(5,'x')\n", (const char *) c0
);
924 ret
+= c0
!= "xxxxx";
926 printf ("\t\"%s\".fill(5,'x')\n", (const char *) c1
);
928 ret
+= c1
!= "xxxxx";
930 catch (struct CBStringException err
) {
931 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
935 printf ("\t# failures: %d\n", ret
);
942 printf ("TEST: alloc() method\n");
948 EXCEPTION_EXPECTED (c0
.alloc (5));
950 catch (struct CBStringException err
) {
951 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
956 CBString c0
, c1("Test-test");
958 printf ("\t\"%s\".alloc(5)\n", (const char *) c0
);
962 printf ("\t\"%s\".alloc(5)\n", (const char *) c1
);
964 ret
+= c1
!= "Test-test";
966 catch (struct CBStringException err
) {
967 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
974 printf ("\t\"%s\".alloc(0)\n", (const char *) c0
);
976 ret
+= c0
!= "Error";
978 catch (struct CBStringException err
) {
979 printf ("\tException (%s) properly thrown\n", err
.what());
985 printf ("\t\"%s\".alloc(-1)\n", (const char *) c0
);
987 ret
+= c0
!= "Error";
989 catch (struct CBStringException err
) {
990 printf ("\tException (%s) properly thrown\n", err
.what());
993 printf ("\t# failures: %d\n", ret
);
1000 printf ("TEST: setsubstr() method\n");
1003 CBString
c0("Test-test");
1006 EXCEPTION_EXPECTED (c0
.setsubstr (4, "extra"));
1008 catch (struct CBStringException err
) {
1009 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1014 CBString c0
, c1("Test-test");
1016 printf ("\t\"%s\".setsubstr (4,\"extra\")\n", (const char *) c0
);
1017 c0
.setsubstr (4, "extra");
1018 ret
+= c0
!= " extra";
1019 printf ("\t\"%s\".setsubstr (4,\"extra\")\n", (const char *) c1
);
1020 c1
.setsubstr (4, "extra");
1021 ret
+= c1
!= "Testextra";
1024 catch (struct CBStringException err
) {
1025 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1032 printf ("\t\"%s\".setsubstr(-1,\"extra\")\n", (const char *) c0
);
1033 c0
.setsubstr (-1, "extra");
1036 catch (struct CBStringException err
) {
1037 printf ("\tException (%s) properly thrown\n", err
.what());
1040 printf ("\t# failures: %d\n", ret
);
1047 printf ("TEST: insert() method\n");
1050 CBString
c0("Test-test");
1053 EXCEPTION_EXPECTED (c0
.insert (4, "extra"));
1055 catch (struct CBStringException err
) {
1056 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1061 CBString c0
, c1("Test-test");
1063 printf ("\t\"%s\".insert (4,\"extra\")\n", (const char *) c0
);
1064 c0
.insert (4, "extra");
1065 ret
+= c0
!= " extra";
1066 printf ("\t\"%s\".insert (4,\"extra\")\n", (const char *) c1
);
1067 c1
.insert (4, "extra");
1068 ret
+= c1
!= "Testextra-test";
1071 catch (struct CBStringException err
) {
1072 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1079 printf ("\t\"%s\".insert(-1,\"extra\")\n", (const char *) c0
);
1080 c0
.insert (-1, "extra");
1083 catch (struct CBStringException err
) {
1084 printf ("\tException (%s) properly thrown\n", err
.what());
1087 printf ("\t# failures: %d\n", ret
);
1094 printf ("TEST: insertchrs() method\n");
1097 CBString
c0("Test-test");
1100 EXCEPTION_EXPECTED (c0
.insertchrs (4, 2, 'x'));
1102 catch (struct CBStringException err
) {
1103 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1108 CBString c0
, c1("Test-test");
1110 printf ("\t\"%s\".insertchrs (4,2,'x')\n", (const char *) c0
);
1111 c0
.insertchrs (4, 2, 'x');
1112 ret
+= c0
!= "xxxxxx";
1113 printf ("\t\"%s\".insertchrs (4,2,'x')\n", (const char *) c1
);
1114 c1
.insertchrs (4, 2, 'x');
1115 ret
+= c1
!= "Testxx-test";
1117 catch (struct CBStringException err
) {
1118 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1125 printf ("\t\"%s\".insertchrs (-1,2,'x')\n", (const char *) c0
);
1126 c0
.insertchrs (-1, 2, 'x');
1129 catch (struct CBStringException err
) {
1130 printf ("\tException (%s) properly thrown\n", err
.what());
1133 printf ("\t# failures: %d\n", ret
);
1140 printf ("TEST: replace() method\n");
1143 CBString
c0("Test-test");
1146 EXCEPTION_EXPECTED (c0
.replace (4, 2, "beef"));
1148 catch (struct CBStringException err
) {
1149 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1154 CBString c0
, c1("Test-test");
1156 printf ("\t\"%s\".replace (4,2,\"beef\")\n", (const char *) c0
);
1157 c0
.replace (4, 2, CBString ("beef"));
1158 ret
+= c0
!= " beef";
1160 c0
.replace (4, 2, "beef");
1161 ret
+= c0
!= " beef";
1163 printf ("\t\"%s\".replace (4,2,\"beef\")\n", (const char *) c1
);
1164 c1
.replace (4, 2, CBString ("beef"));
1165 ret
+= c1
!= "Testbeefest";
1167 c1
.replace (4, 2, "beef");
1168 ret
+= c1
!= "Testbeefest";
1170 catch (struct CBStringException err
) {
1171 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1175 printf ("\t# failures: %d\n", ret
);
1182 printf ("TEST: findreplace() method\n");
1185 CBString c0
, c1("Test-test");
1187 printf ("\t\"%s\".findreplace (\"est\",\"beef\")\n", (const char *) c0
);
1188 c0
.findreplace ("est", "beef");
1191 c0
.findreplace (CBString ("est"), CBString ("beef"));
1194 printf ("\t\"%s\".findreplace (\"est\",\"beef\")\n", (const char *) c1
);
1195 c1
.findreplace ("est", "beef");
1196 ret
+= c1
!= "Tbeef-tbeef";
1198 c1
.findreplace (CBString ("est"), CBString ("beef"));
1199 ret
+= c1
!= "Tbeef-tbeef";
1202 catch (struct CBStringException err
) {
1203 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1208 CBString c0
, c1("TeSt-tEsT");
1210 printf ("\t\"%s\".findreplacecaseless (\"est\",\"beef\")\n", (const char *) c0
);
1211 c0
.findreplacecaseless ("est", "beef");
1214 c0
.findreplacecaseless (CBString ("est"), CBString ("beef"));
1217 printf ("\t\"%s\".findreplacecaseless (\"est\",\"beef\")\n", (const char *) c1
);
1218 c1
.findreplacecaseless ("est", "beef");
1219 ret
+= c1
!= "Tbeef-tbeef";
1221 c1
.findreplacecaseless (CBString ("est"), CBString ("beef"));
1222 ret
+= c1
!= "Tbeef-tbeef";
1225 catch (struct CBStringException err
) {
1226 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1230 printf ("\t# failures: %d\n", ret
);
1237 printf ("TEST: remove() method\n");
1240 CBString c0
, c1("Test-test");
1242 printf ("\t\"%s\".remove (4,2)\n", (const char *) c0
);
1246 printf ("\t\"%s\".remove (4,2)\n", (const char *) c1
);
1248 ret
+= c1
!= "Testest";
1250 catch (struct CBStringException err
) {
1251 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1255 printf ("\t# failures: %d\n", ret
);
1262 printf ("TEST: trunc() method\n");
1265 CBString c0
, c1("Test-test");
1267 printf ("\t\"%s\".trunc (4)\n", (const char *) c0
);
1271 printf ("\t\"%s\".trunc (4)\n", (const char *) c1
);
1273 ret
+= c1
!= "Test";
1275 catch (struct CBStringException err
) {
1276 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1280 printf ("\t# failures: %d\n", ret
);
1287 printf ("TEST: repeat() method\n");
1290 CBString c0
, c1("Test");
1292 printf ("\t\"%s\".repeat (4)\n", (const char *) c0
);
1296 printf ("\t\"%s\".repeat (4)\n", (const char *) c1
);
1298 ret
+= c1
!= "TestTestTestTest";
1301 ret
+= c1
!= "TestTestTestTest";
1303 catch (struct CBStringException err
) {
1304 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1308 printf ("\t# failures: %d\n", ret
);
1315 printf ("TEST: ltrim(), rtrim() methods\n");
1318 CBString c0
, c1(" Test "), c2(" ");
1320 printf ("\t\"%s\".ltrim ()\n", (const char *) c0
);
1327 printf ("\t\"%s\".ltrim ()\n", (const char *) c1
);
1329 ret
+= c1
!= "Test ";
1332 ret
+= c1
!= " Test";
1334 printf ("\t\"%s\".ltrim ()\n", (const char *) c2
);
1341 catch (struct CBStringException err
) {
1342 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1346 printf ("\t# failures: %d\n", ret
);
1353 printf ("TEST: split(), join() mechanisms\n");
1356 CBString c0
, c1("a b c d e f");
1357 struct CBStringList s
;
1361 EXCEPTION_EXPECTED (c0
.join (s
));
1363 catch (struct CBStringException err
) {
1364 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1369 CBString c0
, c1("a b c d e f");
1370 struct CBStringList s
;
1372 printf ("\t\"%s\".split (' ')\n", (const char *) c1
);
1375 CBString
c2(s
), c3(s
, ',');
1377 printf ("\tc.join (<...>)\n");
1379 ret
+= c2
!= "abcdef";
1380 ret
+= c3
!= "a,b,c,d,e,f";
1382 ret
+= c0
!= "abcdef";
1384 ret
+= c0
!= "a,b,c,d,e,f";
1386 CBString strPepe
= "valor1@valor2@valor3@@@valor6";
1387 for (unsigned char c
= (unsigned char) '\0';;c
++) {
1391 sl
.split (strPepe
, c
);
1394 printf ("\tfailure[%d] split/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__
, (const char *) strPepe
, (const char *) x
);
1398 if (UCHAR_MAX
== c
) break;
1405 sl
.splitstr (strPepe
, CBString ("or"));
1406 x
.join (sl
, CBString ("or"));
1408 printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__
, (const char *) strPepe
, (const char *) x
);
1417 sl
.splitstr (strPepe
, CBString ("6"));
1418 x
.join (sl
, CBString ("6"));
1420 printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__
, (const char *) strPepe
, (const char *) x
);
1429 sl
.splitstr (strPepe
, CBString ("val"));
1430 x
.join (sl
, CBString ("val"));
1432 printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__
, (const char *) strPepe
, (const char *) x
);
1441 sl
.splitstr (strPepe
, CBString ("@@"));
1442 x
.join (sl
, CBString ("@@"));
1444 printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__
, (const char *) strPepe
, (const char *) x
);
1450 catch (struct CBStringException err
) {
1451 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1455 printf ("\t# failures: %d\n", ret
);
1462 printf ("TEST: caselessEqual(), caselessCmp() mechanisms\n");
1465 CBString
c0("Test"), c1("test"), c2("testy");
1467 printf ("\t\"%s\".caselessEqual (\"%s\")\n", (const char *) c0
, (const char *) c1
);
1468 ret
+= 1 != c0
.caselessEqual (c1
);
1469 ret
+= 1 != c1
.caselessEqual (c0
);
1470 printf ("\t\"%s\".caselessEqual (\"%s\")\n", (const char *) c0
, (const char *) c2
);
1471 ret
+= 0 != c0
.caselessEqual (c2
);
1472 ret
+= 0 != c2
.caselessEqual (c0
);
1474 printf ("\t\"%s\".caselessCmp (\"%s\")\n", (const char *) c0
, (const char *) c1
);
1475 ret
+= 0 != c0
.caselessCmp (c1
);
1476 ret
+= 0 != c1
.caselessCmp (c0
);
1477 printf ("\t\"%s\".caselessCmp (\"%s\")\n", (const char *) c0
, (const char *) c2
);
1478 ret
+= 0 == c0
.caselessCmp (c2
);
1479 ret
+= 0 == c2
.caselessCmp (c0
);
1481 catch (struct CBStringException err
) {
1482 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1486 printf ("\t# failures: %d\n", ret
);
1493 printf ("TEST: toupper(), tolower() mechanisms\n");
1496 CBString
c0("Test-test");
1499 EXCEPTION_EXPECTED (c0
.toupper());
1500 EXCEPTION_EXPECTED (c0
.tolower());
1502 catch (struct CBStringException err
) {
1503 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1511 printf ("\t\"%s\".toupper ()\n", (const char *) c0
);
1513 ret
+= c0
!= "TEST";
1516 printf ("\t\"%s\".tolower ()\n", (const char *) c0
);
1518 ret
+= c0
!= "test";
1520 catch (struct CBStringException err
) {
1521 printf ("Exception thrown [%d]: %s\n", __LINE__
, err
.what());
1525 printf ("\t# failures: %d\n", ret
);
1529 static size_t test31_aux (void *buff
, size_t elsize
, size_t nelem
, void *parm
) {
1541 printf ("TEST: CBStream test\n");
1543 CBStream
s((bNread
) test31_aux
, NULL
);
1544 s
<< CBString("Test");
1546 ret
+= ((c
= s
.read ()) != CBString ("Test"));
1549 printf ("\t\"%s\" through CBStream.read()\n", (const char *) c
);
1551 s
<< CBString("Test");
1554 ret
+= ((s
>> c
) != CBString ("Test"));
1557 printf ("\t\"%s\" through CBStream.>>\n", (const char *) c
);
1565 printf ("Direct case testing of CPP core functions\n");
1600 printf ("# test failures: %d\n", ret
);