-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpptest.cpp
executable file
·718 lines (584 loc) · 16.1 KB
/
cpptest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#if defined (BSTRLIB_CAN_USE_IOSTREAM)
#include <iostream>
/* #include <string> */
#endif
#include "bstrwrap.h"
#include "bstraux.h"
/* #include "bsafe.h" */
static struct tagbstring x = bsStatic ("test");
void cTest (void) {
static struct tagbstring a = bsStatic ("this is lower case");
static struct tagbstring vowels = bsStatic ("thi");
struct tagbstring t;
bstring b, u;
struct bstrList * sl;
int l;
printf ("\nC test\n");
b = bfromcstr ("");
bAssign (b, &a);
bUppercase (b);
printf ("- <%s>\n", b->data);
printf ("- caselessCmp (%s,%s) -> %d\n", b->data, a.data, bCaselessCmp (b, &a));
bTrunc (b, 7);
printf ("- <%s>\n", b->data);
b = bTail (b, 2);
printf ("- <%s>\n", b->data);
bFill (b, 'x', 0);
printf ("- <%s>\n", b->data);
bFill (b, 'x', 16);
printf ("- <%s>\n", b->data);
bSubs (b, 2, 4, &a, '?');
printf ("- <%s>\n", b->data);
bReplicate (b, 3);
printf ("- <%s>\n", b->data);
printf ("Vowel split test\n");
cstr2tbstr (t,"aeiou");
sl = bsplits (b, &t);
if (sl) {
int i;
for (i=0; i < sl->qty; i++) {
printf ("-\t<%s>\n", sl->entry[i]->data);
}
u = bjoin (sl, NULL);
if (u) printf ("Rejoined: <%s>\n", u->data);
bdestroy (u);
bstrListDestroy (sl);
} else {
printf ("!!!!! Unable to bsplit! [%d]\n", __LINE__);
}
printf ("bsplits(\"%s\", \"x!\") split test\n", b->data);
cstr2tbstr (t,"x!");
sl = bsplits (b, &t);
if (sl) {
int i;
bstring w;
struct tagbstring xx = bsStatic ("x");
for (i=0; i < sl->qty; i++) {
printf ("-\t<%s>\n", sl->entry[i]->data);
}
w = bjoin (sl, &xx);
if (w) printf ("Rejoined: <%s>\n", w->data);
bstrListDestroy (sl);
} else {
printf ("!!!!! Unable to bsplit! [%d]\n", __LINE__);
}
bReplicate (b, 0);
printf ("- bReplicate (*, 0) -> <%s>\n", b->data);
bCatCstr (b, "x");
bReplicate (b, 1);
printf ("- bReplicate (\"x\", 1) -> <%s>\n", b->data);
b->slen = 1;
bReplicate (b, 3);
printf ("- bReplicate (\"x\", 3) -> <%s>\n", b->data);
bReplicate (b, 0);
printf ("- bReplicate (*, 0) -> <%s>\n", b->data);
bAssign (b, &a);
b = bTail (b, 0);
printf ("- <%s>\n", b->data);
bSubs (b, 6, 4, &a, '?');
printf ("- <%s>\n", b->data);
bReverse (b);
printf ("- <%s>\n", b->data);
printf ("- binchr (<%s>, 0, <%s>) = %d\n", b->data, vowels.data, binchr (b, 0, &vowels));
l = b->slen;
binsert (b, 40, b, '*');
printf ("- binsert (b, 40, b, '*') -> <<%s>>\n", b->data);
bdelete (b, l, b->slen);
printf ("- bdelete (b, %d, *) -> <<%s>>\n", l, b->data);
binsert (b, 40, b, '*');
printf ("- binsert (b, 40, b, '*') -> <<%s>>\n", b->data);
bdelete (b, 40, l);
printf ("- bdelete (b, 40, %d) -> <<%s>>\n", l, b->data);
bdelete (b, l, b->slen);
printf ("- bdelete (b, %d, *) -> <<%s>>\n", l, b->data);
binsertch (b, 4, 5, '*');
printf ("- binsertch (b, 4, 5, '*') -> <<%s>>\n", b->data);
/* An illegal operations */
printf ("- bconcat (static<'%s'>, static<'%s'>) -> ", a.data, vowels.data);
l = bconcat (&a, &vowels);
printf ("%d\n", l);
printf ("- bconcat (static<'%s'>, static<'%s'>) -> ", vowels.data, a.data);
l = bconcat (&vowels, &a);
printf ("%d\n", l);
printf ("- bconcat (static<'%s'>, <'%s'>) -> ", vowels.data, b->data);
l = bconcat (&vowels, b);
printf ("%d\n", l);
bwriteprotect (*b);
printf ("- bconcat (protected<'%s'>, static<'%s'>) -> ", b->data, vowels.data);
l = bconcat (b, &vowels);
printf ("%d\n", l);
}
void cppTest (void) {
FILE * fp = NULL;
CBString q, r;
printf ("\nC++ test\n");
fp = fopen ("..\\license.txt", "r");
if (fp == NULL) fp = fopen ("license.txt", "r");
if (NULL != fp) {
while (q.gets ((bNgetc)fgetc, fp, '\n') >= 0) {
printf ("- %s", (const char *)q);
}
try {
int c;
fseek (fp, 0, SEEK_SET);
struct CBStream s ((bNread) fread, fp);
printf ("bsStream buffer size = %d\n", s.buffLengthGet ());
c = 0;
while (!s.eof ()) {
q = s.readLine ('\n');
printf ("* %s", (const char *) q);
c++;
if ((c % 10) == 9) {
r.format ("-- Line: %d ------------------------------------------------------------------\n", c + 1);
s.unread (r);
}
}
}
catch (struct CBStringException err) {
printf ("Exception thrown [%d]: %s\n", __LINE__, err.what());
}
fseek (fp, 0, SEEK_SET);
q.read ((bNread)fread, fp);
printf ("<%s>\n", (const char *)q);
fclose (fp);
#if defined(BSTRLIB_CAN_USE_STL)
{
CBStringList cv;
cv.split (q, '\n');
CBString c(cv);
for (size_t i=0; i < cv.size(); i++) {
printf ("+ <%s>\n", (const char *)cv.at (i));
}
printf ("join constructor: <%s>\n", (const char *)c);
}
#endif
}
printf ("x.mlen = %d\n", x.mlen);
printf ("x.slen = %d\n", x.slen);
printf ("x.data = %s\n", x.data);
q.format ("%s[%d]\n", __FILE__, __LINE__);
q = __FILE__;
q.formata ("[%d]", __LINE__);
printf ("q = <%s>\n", (const char *)q);
r = x;
printf ("q == q = %d\n", q == q);
printf ("q != q = %d\n", q != q);
printf ("q < q = %d\n", q < q);
printf ("q <= q = %d\n", q <= q);
printf ("q > q = %d\n", q > q);
printf ("q >= q = %d\n", q >= q);
printf ("q == r = %d\n", q == r);
printf ("q != r = %d\n", q != r);
printf ("q < r = %d\n", q < r);
printf ("q <= r = %d\n", q <= r);
printf ("q > r = %d\n", q > r);
printf ("q >= r = %d\n", q >= r);
printf ("q.find (r, 0) = %d\n", q.find (r, 0));
printf ("q.find (r, 1) = %d\n", q.find (r, 1));
printf ("q.find (r, 2) = %d\n", q.find (r, 2));
printf ("q.find (r, 3) = %d\n", q.find (r, 3));
printf ("q.find (r, 4) = %d\n", q.find (r, 4));
printf ("q.find (\"pp\", 0) = %d\n", q.find ("pp", 0));
printf ("q.find (\"pp\", 1) = %d\n", q.find ("pp", 1));
printf ("q.find (\"pp\", 2) = %d\n", q.find ("pp", 2));
printf ("q.reversefind (r, %d) = %d\n", q.length()-1, q.reversefind (r, q.length()-1));
printf ("q.reversefind (\"pp\", %d) = %d\n", q.length()-1, q.reversefind ("pp", q.length()-1));
printf ("\n\nq = <%s>\nr = <%s>\nx = <%s>\n", (const char *)q, (const char *)r, x.data);
for (int i=0; i < q.length(); i++) {
printf ("(%c)", (char) q[i]);
}
printf ("\n");
q.fill (20, 'y');
r.fill (20, 'x');
q += r;
q.insert (10, q);
puts ((const char *)q);
q += q;
puts ((const char *)q);
q = "abc";
q.repeat (((80 - 1) / q.length()) + 1);
q.trunc (80);
puts ((const char *)q);
q = " <-- lws rws--> ";
printf ("|%s|\n", (const char *)q);
r = q;
r.rtrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.ltrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.trim ();
printf ("|%s|\n", (const char *)r);
q = "a <-- lws rws--> ";
printf ("|%s|\n", (const char *)q);
r = q;
r.rtrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.ltrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.trim ();
printf ("|%s|\n", (const char *)r);
q = " <-- lws rws--> b";
printf ("|%s|\n", (const char *)q);
r = q;
r.rtrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.ltrim ();
printf ("|%s|\n", (const char *)r);
r = q;
r.trim ();
printf ("|%s|\n", (const char *)r);
#if defined(BSTRLIB_CAN_USE_STL)
{
CBStringList cv;
cv.split (q, CBString(" a"));
for (size_t i=0; i < cv.size(); i++) {
printf ("+ <%s>\n", (const char *)cv.at (i));
}
}
#endif
q.trunc(0);
q = q + 'c';
printf ("<%s>\n", (const char *) q);
q = q + 'a';
printf ("<%s>\n", (const char *) q);
q = q + 'r';
printf ("<%s>\n", (const char *) q);
q.formata ("%g", 3.4);
printf ("<%s>\n", (const char *) q);
q.formata ("%g", 1.5);
printf ("<%s>\n", (const char *) q);
q.trunc(0);
q.repeat (2);
printf ("<%s>\n", (const char *) q);
#ifdef BSTRLIB_THROWS_EXCEPTIONS
try {
q.trunc (-1);
}
catch (struct CBStringException err) {
printf ("Exception thrown: %s\n", err.what());
}
try {
putchar (q[-1]);
}
catch (struct CBStringException err) {
printf ("Exception thrown: %s\n", err.what());
}
try {
putchar (q[10000000]);
}
catch (struct CBStringException err) {
printf ("Exception thrown: %s\n", err.what());
}
#endif
q = "the";
q *= 4;
printf ("<%s>\n", (const char *) q);
q = 4 * CBString ("the");
printf ("<%s>\n", (const char *) q);
q = CBString ("the") * 4;
printf ("<%s>\n", (const char *) q);
}
void test3 (void) {
FILE * fp;
/* fp = fopen ("..\\adentrap.csv", "rb");
if (fp == NULL) fp = fopen ("adentrap.csv", "rb"); */
fp = fopen ("..\\license.txt", "rb");
if (fp == NULL) fp = fopen ("license.txt", "rb");
if (fp) {
struct bStream * s;
bstring r = bfromcstr ("");
struct tagbstring xx = bsStatic ("\r\n");
s = bsopen ((bNread) fread, fp);
while (0 == bsreadlns (r, s, &xx)) {
/* while (0 == bsreadln (r, s, '\n')) { */
printf ("<<%s>>\n", r->data);
}
bsclose (s);
fclose (fp);
}
}
void test4 (void) {
struct tagbstring f0 = bsStatic ("a"), f1 = bsStatic ("ab"), f2 = bsStatic ("b");
struct tagbstring r0 = bsStatic ("x"), r1 = bsStatic ("yz"), r2 = bsStatic ("");
struct tagbstring t;
bstring b;
printf ("\nReplaceAll test\n");
b = bfromcstr ("ababababababab");
bReplaceAll (b, &f0, &r0, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &r0, &f0, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &f1, &r1, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &r1, &f0, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &f0, &f1, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &f0, &r1, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &f2, &f1, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &r0, &f0, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &r2, &f0, 0);
printf ("<%s>\n", b->data);
bReplaceAll (b, &r1, &r2, 0);
printf ("<%s>\n", b->data);
blk2tbstr (t, b->data, 1);
bReplaceAll (b, &t, &r0, 0);
printf ("<%s>\n", b->data);
}
void test5 (void) {
struct tagbstring t0 = bsStatic ("Hello, world.");
bstring b;
char * nStr0;
b = bNetStr2Bstr (nStr0 = bStr2NetStr (&t0));
printf ("\nbstring <-> netstring conversion\n");
printf ("<%s>\n", t0.data);
printf ("%s\n", nStr0);
printf ("<%s>\n", b->data);
printf ("%s\n", bStr2NetStr (b));
nStr0 = bStr2NetStr (b = bNetStr2Bstr ("0:,"));
printf ("\nEmpty strings\n");
printf ("%s\n", nStr0);
printf ("<%s>\n", b->data);
printf ("<%s>\n", bNetStr2Bstr (nStr0)->data);
printf ("\nNULL parameter\n");
printf ("bNetStr2Bstr(NULL) = %p\n", (void *) bNetStr2Bstr (NULL));
printf ("bStr2NetStr(NULL) = %p\n", (void *) bStr2NetStr (NULL));
}
static int cb0 (void * parm, int ofs, int len) {
bstring b = bmidstr ((bstring) parm, ofs, len);
printf ("<%d:%d:%s>", ofs, len, b->data);
bdestroy (b);
return 0;
}
void test6 (void) {
struct tagbstring t0 = bsStatic ("abaabacabaaaabaacaabaaaaaa");
struct tagbstring t1 = bsStatic ("bc");
printf ("\nbsplit?cb test\n");
bsplitcb (&t0, 'b', 0, cb0, &t0);
printf ("\n");
bsplitscb (&t0, &t1, 0, cb0, &t0);
printf ("\n");
bsplitcb (&t0, 'd', 0, cb0, &t0);
printf ("\n");
}
void test7 (void) {
CBString dbl("-3.7e+10");
CBString flt("-3.7");
CBString i("-7");
CBString ui("7");
printf ("\nNumeric conversion test\n");
try {
CBString xx("animal");
printf ("%d", (int)xx);
}
catch (struct CBStringException err) {
printf ("Exception correctly thrown [%d]: %s\n", __LINE__, err.what());
}
printf ("<%g>\n", (double)dbl);
printf ("<%f>\n", (float)flt);
printf ("<%d>\n", (int)i);
printf ("<%u>\n", (unsigned int)ui);
}
#if 0
#if defined (BSTRLIB_CAN_USE_IOSTREAM)
#include <iostream>
/* #include <string> */
#endif
#endif
#if defined (BSTRLIB_CAN_USE_STL)
#include <string>
#endif
#if 0
using namespace std;
void test8 (void) {
#if defined (BSTRLIB_CAN_USE_IOSTREAM)
CBString b, c;
/* std::string b, c; */
/* std::cin >> b >> c;
std::cout << "<" << b << ">" << c << std::endl; */
/* std:: */ cout << "Enter some text" << /* std:: */ endl;
getline (/* std:: */ cin, b);
getline (/* std:: */ cin, c);
/* std:: */ cout << b << /* std:: */ endl << c;
#endif
return;
}
#endif
#include <time.h>
#define TESTSTRING1 ("<sometag name=\"John Doe\" position=\"Executive VP Marketing\"/>")
void test9 (void) {
int i, c;
clock_t t0, t1;
#if defined (BSTRLIB_CAN_USE_STL)
t0 = clock();
for (c=i=0; i < 1000000; i++) {
std::string b;
c += b.length();
}
t1 = clock() - t0;
printf ("std::string empty constructor performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
for (c=i=0; i < 1000000; i++) {
std::string b (TESTSTRING1);
c += b.length();
}
t1 = clock() - t0;
printf ("std::string non-empty constructor performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
std::string b;
for (c=i=0; i < 1000000; i++) {
b = TESTSTRING1;
c += b.length();
}
}
t1 = clock() - t0;
printf ("std::string char * assignment performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
std::string b (TESTSTRING1);
for (c=i=0; i < 1000000; i++) {
c += b[0];
c += b[11];
c += b.c_str()[1];
}
}
t1 = clock() - t0;
printf ("std::string char extraction performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
std::string a (TESTSTRING1);
std::string b;
for (c=i=0; i < 10000; i++) {
b += a;
b += "!!";
}
}
t1 = clock() - t0;
printf ("std::string concatenation performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
std::string b ("Dot. 123. Some more data.");
for (c=i=0; i < 1000000; i++) {
c = b.find ('.');
c = b.find ("123");
/* c = b.find_first_of ("sm"); */
}
}
t1 = clock() - t0;
printf ("std::string scan performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
#endif
t0 = clock();
for (c=i=0; i < 1000000; i++) {
CBString b;
c += b.length();
}
t1 = clock() - t0;
printf ("CBString empty constructor performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
for (c=i=0; i < 1000000; i++) {
CBString b (TESTSTRING1);
c += b.length();
}
t1 = clock() - t0;
printf ("CBString non-empty constructor performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
CBString b;
for (c=i=0; i < 1000000; i++) {
b = TESTSTRING1;
c += b.length();
}
}
t1 = clock() - t0;
printf ("CBString char * assignment performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
CBString b (TESTSTRING1);
for (c=i=0; i < 1000000; i++) {
c += b[0];
c += b[11];
c += ((const char *)b)[1];
}
}
t1 = clock() - t0;
printf ("CBString char extraction performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
CBString a (TESTSTRING1);
CBString b;
for (c=i=0; i < 10000; i++) {
b += a;
b += "!!";
}
}
t1 = clock() - t0;
printf ("CBString concatenation performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
t0 = clock();
{
CBString b ("Dot. 123. Some more data.");
for (c=i=0; i < 1000000; i++) {
c = b.find ('.');
c = b.find ("123");
/* c = b.find ("sm"); */
}
}
t1 = clock() - t0;
printf ("CBString scan performance: %f\n", (float)t1 / CLOCKS_PER_SEC );
return;
}
void test10 (void) {
time_t tloc;
struct tm tod;
bstring b;
time (&tloc);
tod = *localtime (&tloc);
b = bStrfTime ("%A %B %d, %Y", &tod);
printf ("<%s>\n", b->data);
bSecureDestroy (b);
b = bAscTime (&tod);
printf ("%s", b->data);
bSecureDestroy (b);
b = bCTime (&tloc);
printf ("%s", b->data);
bSecureDestroy (b);
}
int xfgetc (void * ctx) {
return fgetc ((FILE *) ctx);
}
void test11 (void) {
bstring inp;
printf ("Secure input:\n");
inp = bSecureInput (24, '\n', xfgetc, stdin);
printf ("%s", bdatae (inp, "Failed to read secure input\n"));
bSecureDestroy (inp);
}
int main () {
cppTest ();
cTest ();
test3 ();
test4 ();
test5 ();
test6 ();
test7 ();
#if 0
test8 ();
test9 ();
#endif
test10 ();
#if 0
test11 ();
#endif
return 0;
}