/[resiprocate]/main/rutil/test/testData.cxx
ViewVC logotype

Contents of /main/rutil/test/testData.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10778 - (show annotations) (download)
Tue Dec 24 01:26:08 2013 UTC (5 years, 10 months ago) by Dpocock
File MIME type: text/plain
File size: 44241 byte(s)
rutil: Data: add fromHex method
1 #include "rutil/Data.hxx"
2 #include "rutil/DataStream.hxx"
3 #include "rutil/Log.hxx"
4 #include "assert.h"
5 #include <iostream>
6 #include <limits>
7 #include <math.h>
8
9 using namespace resip;
10 using namespace std;
11
12 #define DOUBLE_EQUALITY(a, b) essentiallyEqual(a, b, std::numeric_limits<double>::epsilon())
13
14 // for friends
15 class TestData
16 {
17 public:
18
19 bool approximatelyEqual(double a, double b, float epsilon)
20 {
21 return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
22 }
23
24 bool essentiallyEqual(double a, double b, double epsilon)
25 {
26 return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
27 }
28
29 int main()
30 {
31 Log::initialize(Log::Cout, Log::Debug, Data::Empty);
32
33 {
34 const char* txt = "buffer";
35 Data d(txt);
36 // copies
37 assert(txt != d.data());
38
39 const char* b = d.data();
40 d.c_str();
41 // not reallocated
42 assert(b == d.data());
43 }
44
45 {
46 const char* txt = "buffer";
47 Data d(Data::Share, txt, strlen(txt));
48 // shared
49 assert(txt == d.data());
50
51 d.c_str();
52 // reallocated
53 assert(txt != d.data());
54 }
55
56 {
57 const char* txt = "buffer";
58 Data d(Data::Borrow, txt, strlen(txt));
59 // shared
60 assert(txt == d.data());
61 const char* b = d.data();
62
63 d.c_str();
64 // reallocated
65 assert(b != d.data());
66 }
67
68 {
69 const int s = 12;
70 char* txt = new char[s];
71 Data d(Data::Take, txt, s);
72 // shared
73 assert(txt == d.data());
74
75 d.c_str();
76 // reallocated
77 assert(txt != d.data());
78 }
79
80 {
81 {
82 int length = 16;
83 char* buffer = new char [length];
84
85 for (int i=0; i<16; ++i)
86 {
87 buffer[i] = ' ';
88 }
89
90 Data target(Data::Take, buffer, length);
91 std::cerr << target.c_str() << endl;
92 }
93
94 {
95 Data input("abcdefghij");
96 std::cerr << "T0: " << input << std::endl;
97 input.replace("a", "b");
98 std::cerr << "T1: " << input << std::endl;
99 assert(input == "bbcdefghij");
100 input.replace("bb", "");
101 std::cerr << "T2: " << input << std::endl;
102 assert(input == "cdefghij");
103 }
104 {
105 Data input("");
106 std::cerr << "T0: " << input << std::endl;
107 input.replace("a", "b");
108 std::cerr << "T1: " << input << std::endl;
109 assert(input == "");
110 input.replace("bb", "");
111 std::cerr << "T2: " << input << std::endl;
112 assert(input == "");
113 }
114
115 {
116 Data from;
117 Data to;
118 Data example;
119
120 // asserts
121 //example.replace(from, to);
122 }
123
124 {
125 Data from("a");
126 Data to("b");
127 Data example;
128
129 example.replace(from, to);
130 assert(example.empty());
131 }
132
133 {
134 Data from("a");
135 Data to("b");
136 Data example("c");
137
138 example.replace(from, to);
139 assert(example == "c");
140 }
141
142 {
143 Data from("a");
144 Data to("b");
145 Data example("a");
146
147 example.replace(from, to);
148 assert(example == "b");
149 }
150
151 {
152 Data from("a");
153 Data to("b");
154 Data example("aaaa");
155
156 example.replace(from, to);
157 assert(example == "bbbb");
158 }
159
160 {
161 Data from("a");
162 Data to("b");
163 Data example("abracadabra");
164
165 example.replace(from, to);
166 assert(example == "bbrbcbdbbrb");
167 }
168
169 {
170 Data from("aa");
171 Data to("b");
172 Data example("aa");
173
174 example.replace(from, to);
175 assert(example == "b");
176 }
177
178 {
179 Data from("aa");
180 Data to("b");
181 Data example("aaaaa");
182
183 example.replace(from, to);
184 assert(example == "bba");
185 }
186
187 {
188 Data from("a");
189 Data to("bb");
190 Data example("a");
191
192 example.replace(from, to);
193 assert(example == "bb");
194 }
195
196 {
197 Data from("a");
198 Data to("bb");
199 Data example("abracadabra");
200
201 example.replace(from, to);
202 assert(example == "bbbrbbcbbdbbbrbb");
203 }
204
205 {
206 Data from("a");
207 Data to("bb");
208 const char* buffer = "abracadabra";
209 Data example(Data::Share, buffer, strlen(buffer));
210
211 example.replace(from, to);
212 assert(example == "bbbrbbcbbdbbbrbb");
213 }
214
215 {
216 Data from("a");
217 Data to("aa");
218 Data example("a");
219
220 example.replace(from, to);
221 assert(example == "aa");
222 }
223
224 {
225 Data from("a");
226 Data to("aa");
227 Data example("abracadabra");
228
229 example.replace(from, to);
230 assert(example == "aabraacaadaabraa");
231 }
232
233 {
234 Data from("abracadabra");
235 Data to("a");
236 Data example("abracadabra");
237
238 example.replace(from, to);
239 assert(example == "a");
240 }
241
242 {
243 Data from("abracadabra");
244 Data to("");
245 Data example("abracadabra");
246
247 example.replace(from, to);
248 assert(example == "");
249 }
250
251 {
252 Data from("abracadabra");
253 Data to("");
254 Data example("abracadabraabracadabraabracadabra");
255
256 example.replace(from, to);
257 assert(example == "");
258 }
259 }
260
261 {
262 Data input("abc123abca");
263 std::cerr << "T0: " << input << std::endl;
264 input.replace("abc", "ABCD");
265 std::cerr << "T1: " << input << std::endl;
266 assert(input == "ABCD123ABCDa");
267 }
268
269
270 {
271 const char* s = "a";
272 const char* ss = "bb";
273 const char* sss = "ccc";
274
275 Data one;
276 Data two;
277 Data three;
278
279 for (int i = 0; i < 100; ++i)
280 {
281 one.append(s, strlen(s));
282 two.append(ss, strlen(ss));
283 three.append(sss, strlen(sss));
284 }
285
286 assert(one.size() == 100);
287 assert(two.size() == 200);
288 assert(three.size() == 300);
289 }
290
291 {
292 Data httpString("safe");
293
294 Data enc;
295 enc = httpString.urlEncoded();
296
297 cerr << "res: " << enc << endl;
298 }
299
300 {
301 Data httpString("-_.~!$'()*,;=:@/?");
302 httpString += "0123456789";
303
304 Data result;
305 {
306 DataStream str(result);
307 httpString.urlEncode(str);
308 }
309 assert(result == httpString.urlEncoded());
310 cerr << ">> " << httpString.urlEncoded() << endl;
311 cerr << "<< " << httpString.urlEncoded().urlDecoded() << endl;
312 cerr << ".. " << httpString << endl;
313
314 assert(httpString == httpString.urlEncoded().urlDecoded());
315 assert(result == httpString);
316 }
317
318 {
319 Data httpString("http::/foo.com/in word?arg1=\"quote%\"&arg2=\"%%%%%\"");
320
321 Data result;
322 {
323 DataStream str(result);
324 httpString.urlEncode(str);
325 }
326
327 cerr << result << endl;
328 assert(result == "http::/foo.com/in+word?arg1=%22quote%25%22%26arg2=%22%25%25%25%25%25%22");
329 assert(result == httpString.urlEncoded());
330
331 cerr << ">> " << httpString.urlEncoded() << endl;
332 cerr << "<< " << httpString.urlEncoded().urlDecoded() << endl;
333 cerr << ".. " << httpString << endl;
334
335 assert(httpString == httpString.urlEncoded().urlDecoded());
336 }
337
338 {
339 Data needsCharEncode("CharEncode % me");
340 cerr << "original " << needsCharEncode << endl;
341 cerr << "charEncoded " << needsCharEncode.charEncoded() << endl;
342 cerr << "charUnencoded " << needsCharEncode.charEncoded().charUnencoded() << endl;
343
344 assert(needsCharEncode.charEncoded().charUnencoded() == needsCharEncode);
345 }
346
347 {
348 Data needsCharEncode("CharEncode % me");
349 needsCharEncode += " \";/?:@&=+%$,/t-_.!~*'()";
350 needsCharEncode += char(0);
351 needsCharEncode += char(254);
352 needsCharEncode += char(17);
353
354 cerr << needsCharEncode.charEncoded() << endl;
355
356 assert(needsCharEncode.charEncoded().charUnencoded() == needsCharEncode);
357 }
358
359 {
360 Data needsNoCharEncode("dontcharEncodeme");
361
362 cerr << needsNoCharEncode.charEncoded() << endl;
363
364 assert(needsNoCharEncode.charEncoded().charUnencoded() == needsNoCharEncode);
365 }
366
367 {
368 Data charEncodeCase("%5b%5D%5B%5d");
369
370 cerr << charEncodeCase.charUnencoded() << endl;
371
372 Data shouldMatch("[][]");
373 assert(charEncodeCase.charUnencoded()==shouldMatch);
374 }
375
376 {
377 Data s1;
378 assert(s1.convertInt() == 0);
379
380 Data s2("12foo");
381 assert(s2.convertInt() == 12);
382
383 Data s3("12");
384 assert(s3.convertInt() == 12);
385
386 Data s4("foo");
387 assert(s4.convertInt() == 0);
388
389 Data s5(" ");
390 assert(s5.convertInt() == 0);
391
392 Data s6(" +");
393 assert(s6.convertInt() == 0);
394
395 Data s7(" +17");
396 assert(s7.convertInt() == 17);
397
398 Data s8(" -17");
399 assert(s8.convertInt() == -17);
400
401 Data s9(" --17");
402 assert(s9.convertInt() == 0);
403 }
404
405 {
406 Data s1;
407 assert(s1.convertUnsignedLong() == 0);
408
409 Data s2("12foo");
410 assert(s2.convertUnsignedLong() == 12);
411
412 Data s3("12");
413 assert(s3.convertUnsignedLong() == 12);
414
415 Data s4("foo");
416 assert(s4.convertUnsignedLong() == 0);
417
418 Data s5(" ");
419 assert(s5.convertUnsignedLong() == 0);
420
421 Data s6(" +");
422 assert(s6.convertUnsignedLong() == 0);
423
424 Data s7(" +17");
425 assert(s7.convertUnsignedLong() == 17);
426
427 Data s8(" -17");
428 assert(s8.convertUnsignedLong() == 0);
429 }
430
431 {
432 Data s1;
433 assert(s1.convertUInt64() == 0);
434
435 Data s2("12foo");
436 assert(s2.convertUInt64() == 12);
437
438 Data s3("12");
439 assert(s3.convertUInt64() == 12);
440
441 Data s4("foo");
442 assert(s4.convertUInt64() == 0);
443
444 Data s5(" ");
445 assert(s5.convertUInt64() == 0);
446
447 Data s6(" +");
448 assert(s6.convertUInt64() == 0);
449
450 Data s7(" +17");
451 assert(s7.convertUInt64() == 17);
452
453 Data s8(" -17");
454 assert(s8.convertUInt64() == 0);
455 }
456
457 {
458 Data s1;
459 assert(s1.convertSize() == 0);
460
461 Data s2("12foo");
462 assert(s2.convertSize() == 12);
463
464 Data s3("12");
465 assert(s3.convertSize() == 12);
466
467 Data s4("foo");
468 assert(s4.convertSize() == 0);
469
470 Data s5(" ");
471 assert(s5.convertSize() == 0);
472
473 Data s6(" +");
474 assert(s6.convertSize() == 0);
475
476 Data s7(" +17");
477 assert(s7.convertSize() == 17);
478
479 Data s8(" -17");
480 assert(s8.convertSize() == 0);
481 }
482
483 #ifndef RESIP_FIXED_POINT
484 {
485 Data s1;
486 assert(s1.convertDouble() == 0);
487
488 Data s2("12foo");
489 assert(s2.convertDouble() == 12);
490
491 Data s3("12");
492 assert(s3.convertDouble() == 12);
493
494 Data s4("foo");
495 assert(s4.convertDouble() == 0);
496
497 Data s5(" ");
498 assert(s5.convertDouble() == 0);
499
500 Data s6(" +");
501 assert(s6.convertDouble() == 0);
502
503 Data s7(" +17");
504 assert(s7.convertDouble() == 17);
505
506 Data s8(" -17");
507 assert(s8.convertDouble() == -17);
508
509 Data s9(" --17");
510 assert(s9.convertDouble() == 0);
511 }
512
513 {
514 Data s1(".");
515 assert(s1.convertDouble() == 0);
516
517 Data s2("12.12foo");
518 assert(DOUBLE_EQUALITY(s2.convertDouble(), 12.12L));
519
520 Data s3("12.12");
521 assert(DOUBLE_EQUALITY(s3.convertDouble(), 12.12L));
522
523 Data s4(".foo");
524 assert(s4.convertDouble() == 0);
525
526 Data s5(" .");
527 assert(s5.convertDouble() == 0);
528
529 Data s6(" +.");
530 assert(s6.convertDouble() == 0);
531
532 Data s6a(" -.");
533 assert(s6a.convertDouble() == 0);
534
535 Data s7(" +17.17");
536 assert(DOUBLE_EQUALITY(s7.convertDouble(), 17.17L));
537
538 Data s8(" -17.17");
539 assert(DOUBLE_EQUALITY(s8.convertDouble(), -17.17L));
540
541 Data s9(" -17.17foo");
542 assert(DOUBLE_EQUALITY(s9.convertDouble(), -17.17L));
543
544 Data s10(" --17.17");
545 assert(s10.convertDouble() == 0);
546
547 Data s11(" -0000.017");
548 assert(DOUBLE_EQUALITY(s11.convertDouble(), -0.017L));
549
550 Data s12(".017");
551 assert(DOUBLE_EQUALITY(s12.convertDouble(), 0.017L));
552
553 Data s13(" .017");
554 assert(DOUBLE_EQUALITY(s13.convertDouble(), 0.017L));
555
556 Data s14(" +.017");
557 assert(DOUBLE_EQUALITY(s14.convertDouble(), 0.017L));
558
559 Data s15(" -.017");
560 assert(DOUBLE_EQUALITY(s15.convertDouble(), -0.017L));
561
562 }
563 #endif
564 {
565 Data s;
566 s = "some text";
567 s += Data::Empty;
568 s += "";
569
570 assert(s == "some text");
571 }
572
573 {
574 Data d;
575 const char *q = "\0";
576 d += q;
577
578 for(const char *p =
579 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
580 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
581 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
582 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
583 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
584 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
585 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
586 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
587 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
588 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
589 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
590 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
591 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
592 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
593 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
594 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
595 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
596 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
597 *p;
598 ++p)
599 {
600 d += *p;
601 d += q;
602 d += "~";
603 }
604
605 }
606
607 {
608 Data d;
609 assert(d.empty());
610 assert(d.c_str()[0] == 0);
611 }
612
613 {
614 const int maxs = 1024;
615 Data a;
616 for (int i = 0; i < maxs; i++)
617 {
618 Data b(a.c_str());
619 Data c(b);
620
621 a += "a";
622 }
623 cerr << "test data size 0.." << maxs << endl;
624 }
625
626 // test comparison
627 {
628 {
629 Data c = "sadfsdf";
630 Data d;
631 d = c;
632 assert(c == d);
633 }
634
635 {
636 const char* f = "asdasd";
637 Data d = Data(f);
638 assert(d == f);
639 }
640
641 {
642 Data d;
643 const char* f = "asdasd";
644 d = f;
645 assert(d == f);
646 }
647
648 {
649 Data d("asdfasfdsadf");
650 const char* f = "asdasd";
651 d = f;
652 assert(d == f);
653 }
654
655 {
656 const char* f = "asdasd";
657 Data d = Data(f);
658 assert(!(d < f));
659 }
660
661 {
662 Data d;
663 const char* f = "asdasd";
664 d = f;
665 assert(!(d < f));
666 }
667
668 {
669 Data d("asdfasfdsadf");
670 const char* f = "asdasd";
671 d = f;
672 assert(!(d < f));
673 }
674
675 {
676 Data a("qwerty");
677 Data b("qwerty");
678 assert(!(a < b));
679 }
680
681 {
682 Data a("qwert");
683 Data b("qwerty");
684 assert(a < b);
685 }
686
687 {
688 Data a("qwert");
689 Data b("qwerty");
690 assert(a < b);
691 assert(!(b < a));
692 }
693
694 {
695 const char* f = "asdasda";
696 Data d("asdasd");
697 assert(d < f);
698 }
699
700 {
701 const char * c("asdfasfdsadf");
702 Data d(Data::Share, c, 4);
703 assert(d < c);
704 }
705
706 {
707 const char * c("asdfasfdsadf");
708 Data d(Data::Share, c, strlen(c));
709 assert(!(d < c));
710 }
711
712 {
713 const char * c("asdfasfdsadf");
714 Data d(Data::Share, c, strlen(c));
715 assert(!(d < c));
716 Data c1(d); // copy, null terminate
717 assert(!(d < c1));
718 assert(!(c1 < d));
719 }
720
721 {
722 const char* f = "asdasd";
723 Data d("fsadf");
724
725 assert(!(d <= f));
726 }
727
728 {
729 const Data f = "asdasd";
730 Data d("fsadf");
731
732 assert(!(d <= f));
733 }
734
735 {
736 const char* f = "asdasd";
737 Data d = Data(f);
738 assert(d <= f);
739 }
740
741 {
742 Data d;
743 const char* f = "asdasd";
744 d = f;
745 assert(d <= f);
746 }
747
748 {
749 Data d("asdfasfdsadf");
750 const char* f = "asdasd";
751 d = f;
752 assert(d <= f);
753 }
754
755 {
756 Data a("qwerty");
757 Data b("qwerty");
758 assert(a <= b);
759 }
760 {
761 Data a("qwert");
762 Data b("qwerty");
763 assert(a <= b);
764 }
765 {
766 Data a("qwert");
767 Data b("qwerty");
768 assert(a <= b);
769 assert(!(b <= a));
770 }
771
772 {
773 const char* f = "asdasda";
774 Data d("asdasd");
775 assert(d <= f);
776 }
777
778 {
779 const char * c("asdfasfdsadf");
780 Data d(Data::Share, c, 4);
781 assert(d <= c);
782 }
783
784 {
785 const char * c("asdfasfdsadf");
786 Data d(Data::Share, c, strlen(c));
787 assert(d <= c);
788 }
789
790 {
791 const char * c("asdfasfdsadf");
792 Data d(Data::Share, c, strlen(c));
793 assert(d <= c);
794 Data c1(d); // copy, null terminate
795 assert(d <= c1);
796 assert(c1 <= d);
797 }
798 }
799
800 // test assignment
801 {
802 {
803 Data d("sadfsa");
804 d = "gasdfg";
805
806 assert(d == "gasdfg");
807 }
808
809 {
810 Data d;
811 d = "gasdfg";
812
813 assert(d == "gasdfg");
814 }
815
816 {
817 Data d("sdfsdf");
818 Data e(d);
819 Data f("fsdgsdafg");
820 Data g(f);
821
822 e = g;
823 assert(e == g);
824 }
825 }
826
827 {
828 // test resizing
829 Data header(10, Data::Preallocate);
830 assert(header.empty());
831
832 header += 'c';
833 header += " char";
834 header += "acters";
835
836 assert(header.size() > 10);
837 cerr << header << endl;
838 assert(header == "c characters");
839 }
840
841 {
842 // test resizing
843 Data header(10, Data::Preallocate);
844 assert(header.empty());
845
846 header += 'c';
847 header += " char";
848 header += Data("acters");
849
850 assert(header.size() > 10);
851 cerr << header << endl;
852 assert(header == "c characters");
853 }
854
855 {
856 // test resizing
857 Data header(120, Data::Preallocate);
858 assert(header.empty());
859
860 header += 'c';
861 header += " char";
862 header += "acters";
863
864 assert(header == "c characters");
865 }
866
867 {
868 const char *txt = "here is some text";
869 Data notOwner(Data::Share, txt, strlen(txt));
870 assert(notOwner.mShareEnum == Data::Share);
871
872 notOwner += " more text";
873 assert(notOwner.mShareEnum == Data::Take);
874 assert(notOwner == "here is some text more text");
875 }
876
877 {
878 const char *txt = "here is some text";
879 Data notOwner(Data::Share, txt, strlen(txt));
880 assert(notOwner.mShareEnum == Data::Share);
881
882 notOwner += '!';
883 assert(notOwner.mShareEnum == Data::Take);
884 assert(notOwner == "here is some text!");
885 }
886
887 {
888 const char *txt = "here is some text";
889 Data notOwner(Data::Share, txt, strlen(txt));
890 assert(notOwner.mShareEnum == Data::Share);
891
892 notOwner += Data(" more text");
893 assert(notOwner.mShareEnum == Data::Take);
894 assert(notOwner == "here is some text more text");
895 }
896
897 {
898 Data v("some text");
899 assert(v.prefix("some"));
900 assert(v.prefix("some "));
901 assert(!v.prefix("ome "));
902 assert(!v.prefix("some more text"));
903
904 assert(v.prefix(Data::Empty));
905 assert(v.prefix("some text"));
906 assert(v.prefix(v));
907 assert(!v.prefix("some text "));
908 }
909
910 {
911 Data v("some text");
912 assert(v.postfix("text"));
913 assert(v.postfix(" text"));
914 assert(!v.postfix("tex"));
915 assert(!v.postfix("more some text"));
916
917 assert(v.postfix(Data::Empty));
918 assert(v.postfix("some text"));
919 assert(v.postfix(v));
920 assert(!v.postfix(" some text"));
921 }
922
923 {
924 Data transport("transport");
925 assert(isEqualNoCase(transport, "transport"));
926 }
927
928 {
929 Data d1("0123456789");
930 assert(d1.find("0") == 0);
931 assert(d1.find("1") == 1);
932 assert(d1.find("8") == 8);
933 assert(d1.find("9") == 9);
934 assert(d1.find("01") == 0);
935 assert(d1.find("12") == 1);
936 assert(d1.find("a") == Data::npos);
937 assert(d1.find("0123456789") == 0);
938 assert(d1.find("0123456789a") == Data::npos);
939
940 Data d2;
941 assert(d2.find("0") == Data::npos);
942 assert(d2.find("abc") == Data::npos);
943 assert(d2.find("") == Data::npos);
944 }
945 {
946 Data d1("abcdefghi");
947 assert(d1.find("def") == 3);
948 assert(d1.find("def", 3) == 3);
949 assert (d1.substr(d1.find("def"), 3) == "def");
950 cerr << "substr = " << d1.substr(5,4) << endl;
951 }
952 {
953 Data d1("http://123456/123");
954 assert(d1.find("/") == 5);
955 assert(d1.find("/", 7) == 13);
956 }
957 {
958 Data d1("0");
959 Data d2("0");
960 d1 ^= d2;
961 cerr << d1.hex() << endl;
962 assert(d1.size() == 1);
963 assert(d1[0] == 0);
964
965 d1 = "0";
966 d1 ^= Data();
967 cerr << d1.hex() << endl;
968 assert(d1.size() == 1);
969 assert(d1[0] == '0');
970
971 d1 = Data();
972 d1 ^= Data("0");
973 cerr << d1.hex() << endl;
974 assert(d1.size() == 1);
975 assert(d1[0] == '0');
976
977 d1 = Data();
978 d1 ^= Data();
979 cerr << d1.hex() << endl;
980 assert(d1.size() == 0);
981
982
983 d1 = "01234";
984 d1 ^= Data("01234");
985 cerr << d1.hex() << endl;
986 assert(d1.size() == 5);
987 assert(d1[0] == 0);
988 assert(d1[4] == 0);
989
990 d1 = "012";
991 d1 ^= Data("01234");
992 cerr << d1.hex() << endl;
993 assert(d1.size() == 5);
994 assert(d1[0] == 0);
995 assert(d1[1] == 0);
996 assert(d1[2] == 0);
997 assert(d1[3] == '3');
998 assert(d1[4] == '4');
999
1000 d1 ^= Data("01234");
1001 cerr << d1.hex() << endl;
1002 assert(d1[0] == '0');
1003 assert(d1[1] == '1');
1004 assert(d1[2] == '2');
1005 assert(d1[3] == 0);
1006 assert(d1[4] == 0);
1007
1008 d1 = Data(100, Data::Preallocate);
1009 d1 ^= Data("0");
1010 cerr << d1.hex() << endl;
1011
1012 {
1013 Data buffer;
1014 Data working;
1015
1016 {
1017 DataStream strm(buffer);
1018 strm << "user=phone";
1019 }
1020 working ^= buffer;
1021
1022 buffer.clear();
1023 {
1024 DataStream strm(buffer);
1025 strm << "maddr=192.168.1.1";
1026 }
1027 working ^= buffer;
1028
1029 Data result = working;
1030 working.clear();
1031
1032 buffer.clear();
1033 {
1034 DataStream strm(buffer);
1035 strm << "maddr=192.168.1.1";
1036 }
1037 working ^= buffer;
1038
1039 buffer.clear();
1040 {
1041 DataStream strm(buffer);
1042 strm << "user=phone";
1043 }
1044 working ^= buffer;
1045
1046 assert(result == working);
1047 }
1048 }
1049
1050 {
1051 Data d("f");
1052 Data raw = d.fromHex();
1053 assert(raw.size() == 1);
1054 assert((unsigned char)(raw[0]) == 0xf);
1055
1056 d = "Ff";
1057 raw = d.fromHex();
1058 assert(raw.size() == 1);
1059 assert((unsigned char)(raw[0]) == 0xff);
1060
1061 d = "Fff";
1062 raw = d.fromHex();
1063 assert(raw.size() == 2);
1064 assert((unsigned char)(raw[0]) == 0xf);
1065 assert((unsigned char)(raw[1]) == 0xff);
1066
1067 d = "d3b07384d113edec49eaa6238ad5ff00";
1068 raw = d.fromHex();
1069 assert(raw.size() == 16);
1070 assert((unsigned char)raw[0] == 0xd3);
1071 assert((unsigned char)raw[15] == 0x00);
1072 }
1073
1074 {
1075 Data d("012345");
1076 assert(d[0] == '0');
1077 assert(d[1] == '1');
1078 assert(d[2] == '2');
1079 assert(d[3] == '3');
1080 assert(d[4] == '4');
1081 assert(d[5] == '5');
1082 }
1083
1084 {
1085 Data *d = new Data("origin",6);
1086 {
1087 Data * t = d;
1088
1089 d = new Data(*d);
1090
1091 assert(d->size() == t->size());
1092 assert(d->mCapacity == t->mCapacity);
1093 // cout << d->size() << ":" << d->mCapacity << endl;
1094 delete t;
1095 }
1096 delete d;
1097 }
1098
1099 {
1100 char blah[] = "12345";
1101 Data d(blah, 3);
1102 assert(strlen(d.c_str()) == 3);
1103 }
1104
1105 {
1106 assert(Data(0) == "0");
1107 assert(Data(1) == "1");
1108 assert(Data(-1) == "-1");
1109 assert(Data(11) == "11");
1110 assert(Data(1234567) == "1234567");
1111 assert(Data(-1234567) == "-1234567");
1112 }
1113
1114 {
1115 assert(Data(UInt64(0)) == "0");
1116 assert(Data(UInt64(1)) == "1");
1117 assert(Data(UInt64(18446744073709551615ULL)) == "18446744073709551615"); // 2^64 - 1
1118 }
1119
1120 {
1121 cerr << "!! " << Data(true) << endl;
1122
1123 assert(Data(true) == "true");
1124 assert(Data(false) == "false");
1125 }
1126
1127 {
1128 assert(Data('c') == "c");
1129 }
1130
1131 #ifndef RESIP_FIXED_POINT
1132 {
1133 assert(Data(0.21344) == "0.2134");
1134 assert(Data(0.21347) == "0.2135");
1135 assert(Data(-0.21347) == "-0.2135");
1136 assert(Data(-0.21344) == "-0.2134");
1137 cerr << "!! " << Data(-123454.21344, Data::FiveDigitPrecision) << endl;
1138 assert(Data(-123454.21344, Data::FiveDigitPrecision) == "-123454.21344");
1139 assert(Data(-123454.21344, Data::SevenDigitPrecision) == "-123454.21344");
1140 }
1141 #endif
1142 {
1143 Data empt;
1144 Data empt1;
1145 assert(empt.size() == 0);
1146 assert(empt == empt);
1147 assert(empt == empt1);
1148 assert(empt1 == empt);
1149 assert(empt1 == "");
1150
1151 assert(!(empt != empt));
1152 assert(!(empt != empt1));
1153 assert(!(empt1 != empt));
1154 assert(!(empt1 != ""));
1155
1156 assert(empt1 == "");
1157 assert("sdf" != empt1);
1158 assert(Data("SAfdsaf") != empt1);
1159 empt = empt;
1160 empt = empt1;
1161 empt = "sdfasf";
1162 }
1163
1164 {
1165 Data d("qwerty");
1166 cerr << d << endl;
1167 assert(strcmp(d.data(), "qwerty") == 0);
1168
1169 Data e;
1170
1171 assert(e == "");
1172 }
1173
1174 {
1175 Data d("qwerty");
1176 assert(strcmp(d.c_str(), "qwerty") == 0);
1177
1178 Data e;
1179 assert(strcmp(e.c_str(), "") == 0);
1180 }
1181
1182 {
1183 Data d("123");
1184 assert(d.size() == 3);
1185 }
1186
1187 {
1188 Data d("one");
1189 Data c("two");
1190 d += c;
1191 assert(d == "onetwo");
1192
1193 Data empt;
1194 cerr << empt + d << endl;
1195 assert(empt + d == d);
1196 assert(empt + d == "onetwo");
1197 assert(empt + "three" == "three");
1198 }
1199
1200 {
1201 Data s;
1202 s = "c=";
1203 assert(s == "c=");
1204 s += "foo";
1205 assert(s == "c=foo");
1206 s += "\r\n";
1207 s += "bar";
1208 s += "\r\n";
1209 assert (s == "c=foo\r\nbar\r\n");
1210 }
1211
1212 {
1213 Data s;
1214 s += 'c';
1215 assert(s == "c");
1216 assert(s.size() == 1);
1217 }
1218
1219 {
1220 Data s;
1221 s = "c=";
1222 assert(s == "c=");
1223 s += 'f';
1224 assert(s == "c=f");
1225 assert(s.size() == 3);
1226 }
1227
1228 {
1229 Data s;
1230 s = "some text";
1231 s += Data::Empty;
1232
1233 assert(s == "some text");
1234 }
1235
1236 {
1237 Data a("one");
1238 Data b("two");
1239 Data c("three");
1240
1241 assert(a+b+c == "onetwothree");
1242 }
1243
1244 {
1245 Data d("one");
1246 cerr << "one + two = " << (d + "two") << endl;
1247 assert((d + "two") == "onetwo");
1248 }
1249
1250 {
1251 cerr << "test MD5" << endl;
1252 Data d;
1253 assert(d.md5() == "d41d8cd98f00b204e9800998ecf8427e");
1254
1255 Data d1("qwertyuiop");
1256 assert(d1.md5() == "6eea9b7ef19179a06954edd0f6c05ceb");
1257 }
1258
1259 {
1260 Data mixed("MiXed");
1261 mixed.lowercase();
1262 assert(mixed == "mixed");
1263 }
1264 {
1265 Data mixed("miXed");
1266 mixed.uppercase();
1267 assert(mixed == "MIXED");
1268 }
1269 {
1270 Data a("a");
1271 Data aa(a);
1272 assert(a.size() == aa.size());
1273 }
1274 {
1275 Data d("ssd");
1276 Data c;
1277 d = c;
1278 assert(d.empty());
1279 }
1280 {
1281 Data d;
1282 Data c;
1283 assert(!(d != c));
1284 d = c;
1285 assert(d.empty());
1286 }
1287 {
1288 char s[] = "userB@whistler.gloo.net:6062\r\nCo\031";
1289 char o[] = "S";
1290 Data d(Data::Share, s, strlen(s));
1291 Data c(Data::Share, o, strlen(o));
1292
1293 d = c;
1294 assert(c == "S");
1295 }
1296 {
1297 Data d((UInt64)235235);
1298 assert(d == "235235");
1299 }
1300
1301 if (1)
1302 {
1303 Data d3("MTIz"); Data e3("123" );
1304 //cerr << "base64 test " <<e3<< " = "<< d3.base64decode().c_str()<<endl;
1305 //cerr << "base64 test " <<d3<< " = "<< e3.base64encode().c_str()<<endl;
1306 assert( d3.base64decode() == e3 );
1307 assert( e3.base64encode() == d3 );
1308
1309 Data d1("MQ=="); Data e1("1" );
1310 //cerr << "base64 test "<<e1<<" = <"<<d1.base64decode()<<">"<<endl;
1311 //cerr << "base64 test hex "<<e1.hex()<<" = <"<<d1.base64decode().hex()<<">"<<endl;
1312 //cerr << "base64 test "<<d1<<" = <"<<e1.base64encode()<<">"<<endl;
1313 assert( e1 == d1.base64decode() );
1314 assert( e1.base64encode() == d1 );
1315
1316 Data d2("MTI=");
1317 assert( d2.base64decode() == Data("12" ) );
1318 assert( Data("12" ).base64encode() == d2 );
1319
1320 Data d4("MTIzNA==");
1321 assert( d4.base64decode() == Data("1234" ) );
1322 assert( Data("1234" ).base64encode() == d4 );
1323
1324 Data d5("MTIzNDU=");
1325 assert( d5.base64decode() == Data("12345" ) );
1326 assert( Data("12345" ).base64encode() == d5 );
1327
1328 Data d6("MTIzNDU2");
1329 assert( d6.base64decode() == Data("123456" ) );
1330 assert( Data("123456" ).base64encode() == d6 );
1331
1332 Data d7("MTIzNDU2Nw==");
1333 assert( d7.base64decode() == Data("1234567" ) );
1334 assert( Data("1234567" ).base64encode() == d7 );
1335 }
1336
1337 for(int i=0; i<4; ++i)
1338 {
1339 const char* buf1="5d7a9b7c02034b5b";
1340 const char* buf2=" 5d7a9b7c02034b5b";
1341 const char* buf3=" 5d7a9b7c02034b5b";
1342 const char* buf4=" 5d7a9b7c02034b5b";
1343 const char* ubuf1="5D7A9B7C02034B5B";
1344 const char* ubuf2=" 5D7A9B7C02034B5B";
1345 const char* ubuf3=" 5D7A9B7C02034B5B";
1346 const char* ubuf4=" 5D7A9B7C02034B5B";
1347
1348 Data d1(Data::Share, buf1, 16-i);
1349 Data d2(Data::Share, buf2+1, 16-i);
1350 Data d3(Data::Share, buf3+2, 16-i);
1351 Data d4(Data::Share, buf4+3, 16-i);
1352
1353 Data u1(Data::Share, ubuf1, 16-i);
1354 Data u2(Data::Share, ubuf2+1, 16-i);
1355 Data u3(Data::Share, ubuf3+2, 16-i);
1356 Data u4(Data::Share, ubuf4+3, 16-i);
1357
1358 // All of these point to the same data, but aligned differently.
1359 assert(d1.hash()==d2.hash());
1360 assert(d1.hash()==d3.hash());
1361 assert(d1.hash()==d4.hash());
1362
1363 assert(d1.hash()!=u1.hash());
1364 assert(d1.hash()!=u2.hash());
1365 assert(d1.hash()!=u3.hash());
1366 assert(d1.hash()!=u4.hash());
1367
1368 assert(d1.caseInsensitivehash()==d2.caseInsensitivehash());
1369 assert(d1.caseInsensitivehash()==d3.caseInsensitivehash());
1370 assert(d1.caseInsensitivehash()==d4.caseInsensitivehash());
1371
1372 assert(d1.caseInsensitivehash()==u1.caseInsensitivehash());
1373 assert(d1.caseInsensitivehash()==u2.caseInsensitivehash());
1374 assert(d1.caseInsensitivehash()==u3.caseInsensitivehash());
1375 assert(d1.caseInsensitivehash()==u4.caseInsensitivehash());
1376
1377 assert(d1.caseInsensitiveTokenHash()==d2.caseInsensitiveTokenHash());
1378 assert(d1.caseInsensitiveTokenHash()==d3.caseInsensitiveTokenHash());
1379 assert(d1.caseInsensitiveTokenHash()==d4.caseInsensitiveTokenHash());
1380
1381 assert(d1.caseInsensitiveTokenHash()==u1.caseInsensitiveTokenHash());
1382 assert(d1.caseInsensitiveTokenHash()==u2.caseInsensitiveTokenHash());
1383 assert(d1.caseInsensitiveTokenHash()==u3.caseInsensitiveTokenHash());
1384 assert(d1.caseInsensitiveTokenHash()==u4.caseInsensitiveTokenHash());
1385
1386 assert(d1.caseInsensitiveTokenCompare(d1));
1387 assert(d1.caseInsensitiveTokenCompare(d2));
1388 assert(d1.caseInsensitiveTokenCompare(d3));
1389 assert(d1.caseInsensitiveTokenCompare(d4));
1390 assert(d1.caseInsensitiveTokenCompare(u1));
1391 assert(d1.caseInsensitiveTokenCompare(u2));
1392 assert(d1.caseInsensitiveTokenCompare(u3));
1393 assert(d1.caseInsensitiveTokenCompare(u4));
1394
1395 assert(d2.caseInsensitiveTokenCompare(d1));
1396 assert(d2.caseInsensitiveTokenCompare(d2));
1397 assert(d2.caseInsensitiveTokenCompare(d3));
1398 assert(d2.caseInsensitiveTokenCompare(d4));
1399 assert(d2.caseInsensitiveTokenCompare(u1));
1400 assert(d2.caseInsensitiveTokenCompare(u2));
1401 assert(d2.caseInsensitiveTokenCompare(u3));
1402 assert(d2.caseInsensitiveTokenCompare(u4));
1403
1404 assert(d3.caseInsensitiveTokenCompare(d1));
1405 assert(d3.caseInsensitiveTokenCompare(d2));
1406 assert(d3.caseInsensitiveTokenCompare(d3));
1407 assert(d3.caseInsensitiveTokenCompare(d4));
1408 assert(d3.caseInsensitiveTokenCompare(u1));
1409 assert(d3.caseInsensitiveTokenCompare(u2));
1410 assert(d3.caseInsensitiveTokenCompare(u3));
1411 assert(d3.caseInsensitiveTokenCompare(u4));
1412
1413 assert(d4.caseInsensitiveTokenCompare(d1));
1414 assert(d4.caseInsensitiveTokenCompare(d2));
1415 assert(d4.caseInsensitiveTokenCompare(d3));
1416 assert(d4.caseInsensitiveTokenCompare(d4));
1417 assert(d4.caseInsensitiveTokenCompare(u1));
1418 assert(d4.caseInsensitiveTokenCompare(u2));
1419 assert(d4.caseInsensitiveTokenCompare(u3));
1420 assert(d4.caseInsensitiveTokenCompare(u4));
1421
1422 assert(u1.caseInsensitiveTokenCompare(d1));
1423 assert(u1.caseInsensitiveTokenCompare(d2));
1424 assert(u1.caseInsensitiveTokenCompare(d3));
1425 assert(u1.caseInsensitiveTokenCompare(d4));
1426 assert(u1.caseInsensitiveTokenCompare(u1));
1427 assert(u1.caseInsensitiveTokenCompare(u2));
1428 assert(u1.caseInsensitiveTokenCompare(u3));
1429 assert(u1.caseInsensitiveTokenCompare(u4));
1430
1431 assert(u2.caseInsensitiveTokenCompare(d1));
1432 assert(u2.caseInsensitiveTokenCompare(d2));
1433 assert(u2.caseInsensitiveTokenCompare(d3));
1434 assert(u2.caseInsensitiveTokenCompare(d4));
1435 assert(u2.caseInsensitiveTokenCompare(u1));
1436 assert(u2.caseInsensitiveTokenCompare(u2));
1437 assert(u2.caseInsensitiveTokenCompare(u3));
1438 assert(u2.caseInsensitiveTokenCompare(u4));
1439
1440 assert(u3.caseInsensitiveTokenCompare(d1));
1441 assert(u3.caseInsensitiveTokenCompare(d2));
1442 assert(u3.caseInsensitiveTokenCompare(d3));
1443 assert(u3.caseInsensitiveTokenCompare(d4));
1444 assert(u3.caseInsensitiveTokenCompare(u1));
1445 assert(u3.caseInsensitiveTokenCompare(u2));
1446 assert(u3.caseInsensitiveTokenCompare(u3));
1447 assert(u3.caseInsensitiveTokenCompare(u4));
1448
1449 assert(u4.caseInsensitiveTokenCompare(d1));
1450 assert(u4.caseInsensitiveTokenCompare(d2));
1451 assert(u4.caseInsensitiveTokenCompare(d3));
1452 assert(u4.caseInsensitiveTokenCompare(d4));
1453 assert(u4.caseInsensitiveTokenCompare(u1));
1454 assert(u4.caseInsensitiveTokenCompare(u2));
1455 assert(u4.caseInsensitiveTokenCompare(u3));
1456 assert(u4.caseInsensitiveTokenCompare(u4));
1457 }
1458 std::cerr << "All OK" << endl;
1459 return 0;
1460 }
1461 };
1462
1463 int
1464 main()
1465 {
1466 TestData td;
1467 int rVal = td.main();
1468 return rVal;
1469 }
1470 /* ====================================================================
1471 * The Vovida Software License, Version 1.0
1472 *
1473 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
1474 *
1475 * Redistribution and use in source and binary forms, with or without
1476 * modification, are permitted provided that the following conditions
1477 * are met:
1478 *
1479 * 1. Redistributions of source code must retain the above copyright
1480 * notice, this list of conditions and the following disclaimer.
1481 *
1482 * 2. Redistributions in binary form must reproduce the above copyright
1483 * notice, this list of conditions and the following disclaimer in
1484 * the documentation and/or other materials provided with the
1485 * distribution.
1486 *
1487 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
1488 * and "Vovida Open Communication Application Library (VOCAL)" must
1489 * not be used to endorse or promote products derived from this
1490 * software without prior written permission. For written
1491 * permission, please contact vocal@vovida.org.
1492 *
1493 * 4. Products derived from this software may not be called "VOCAL", nor
1494 * may "VOCAL" appear in their name, without prior written
1495 * permission of Vovida Networks, Inc.
1496 *
1497 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
1498 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1499 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
1500 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
1501 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
1502 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
1503 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1504 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1505 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1506 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1507 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
1508 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1509 * DAMAGE.
1510 *
1511 * ====================================================================
1512 *
1513 * This software consists of voluntary contributions made by Vovida
1514 * Networks, Inc. and many individuals on behalf of Vovida Networks,
1515 * Inc. For more information on Vovida Networks, Inc., please see
1516 * <http://www.vovida.org/>.
1517 *
1518 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27