Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

SubStrTest.C

Go to the documentation of this file.
00001 
00046 #include <stdio.h>
00047 
00048 #include "SubString.h"
00049 
00050 
00051 bool SubStringPassByValue( SubString subStr, size_t previousRefCnt )
00052 {
00053   bool result = (subStr.getRefCnt() == previousRefCnt + 1);
00054   if (result == false) {
00055     printf("SubStringPassByValue: argument subStr.getRefCnt() = %d\n",
00056            subStr.getRefCnt() );
00057     printf("SubStringPassByValue: previousRefCnt = %d\n", previousRefCnt );
00058   }
00059   return result;
00060 }
00061 
00062 
00063 bool SubStringPassByReference( SubString &subStr, size_t previousRefCnt )
00064 {
00065   bool result = (subStr.getRefCnt() == previousRefCnt);
00066   return result;
00067 }
00068 
00069 
00074 bool subStrRefCnt()
00075 {
00076   bool result = true;
00077   printf("Test SubString reference count\n");
00078 
00079   String a("abcdefghijk");
00080 
00081   //
00082   // Test reference count
00083   //
00084   SubString subStrA(a, 5, 6);
00085   if (subStrA.getRefCnt() != 1) {
00086     printf("1. reference count is %d, should be 1\n",
00087            subStrA.getRefCnt());
00088     result = false;
00089   }
00090 
00091   size_t previousRefCnt = subStrA.getRefCnt();
00092   if (! SubStringPassByReference( subStrA, previousRefCnt) ) {
00093     printf("2. Pass by reference failed\n");
00094     result = false;
00095   }
00096 
00097   previousRefCnt = subStrA.getRefCnt();
00098   if (! SubStringPassByValue( subStrA, previousRefCnt) ) {
00099     printf("3. Pass by reference value\n");
00100     result = false;
00101   }
00102 
00103   if (a.getRefCnt() != 1) {
00104     printf("4. refCnt of the String referenced by the SubString = %d, should be 1\n", a.getRefCnt() );
00105     result = false;
00106   }
00107 
00108   return result;
00109 } // subStrRefCnt
00110 
00111 
00112 
00145 bool subStrSection()
00146 {
00147   printf("Test SubString section operations\n");
00148   bool rslt = true;
00149   
00150   String a("abcdefghij");
00151   String d = a;
00152 
00153   // a(0,5) means start at index 0, length of the section is 5
00154   String subA = a(0,5);
00155   if (subA.strlen() != 5) {
00156     printf("1. SubString length = %d, should be 5\n", subA.strlen() );
00157     rslt = false;
00158   }
00159 
00160   // make sure that the reference count for the original string
00161   // is not affected.  Note that a right hand size SubString 
00162   // operation does not affect the String on which the SubString
00163   // is defined.
00164   if (d.getRefCnt() != 2) {
00165     printf("2. d.getRefCnt() = %d, should be 2\n", d.getRefCnt() );
00166   }
00167   
00168   String b = subA;
00169   if (b != "abcde") {
00170     printf("3. SubString section is incorrect. ");
00171     const char *Cstr = b;
00172     printf("SubStr = %s, should be \"abcde\"\n", Cstr );
00173     rslt = false;
00174   }
00175 
00176   String c = a(5,5);
00177   if (c != "fghij") {
00178     printf("4. SubString section is incorrect. ");
00179     const char *Cstr = b;
00180     printf("SubStr = %s, should be \"abcde\"\n", Cstr );
00181     rslt = false;
00182   }
00183 
00184   if (a.getRefCnt() != 2) {
00185     printf("5. a.getRefCnt() = %d, should be 2\n", a.getRefCnt() );
00186     rslt = false;
00187   }
00188 
00189   bool exceptionTest1 = false;
00190   // Test exceptions on an out of bound reference
00191   try {
00192     // use a length (8) that places the section beyond the end
00193     // of the String.
00194     String x = a(5, 8);
00195   }
00196   catch (std::out_of_range e) {
00197     exceptionTest1 = true;
00198   }
00199 
00200   if (!exceptionTest1) {
00201     printf("6. There should have been a SubString construction exception\n");
00202     rslt = false;
00203   }
00204 
00205   bool exceptionTest2 = false;
00206   try {
00207     // use a start that is beyond the end of the String
00208     String x = a(10, 1);
00209   }
00210   catch (std::out_of_range e) {
00211     exceptionTest2 = true;
00212   }
00213 
00214   if (!exceptionTest2) {
00215     printf("7. There should have been a SubString construction exception\n");
00216     rslt = false;
00217   }
00218 
00219   String empty;
00220   if (empty.strlen() != 0) {
00221     printf("8. length of an empty string is wrong\n");
00222     rslt = false;
00223   }
00224 
00225   bool exceptionTest3 = false;
00226   String empty2;
00227   try {
00228     empty2 = empty(0,0);
00229   }
00230   catch(std::out_of_range e) {
00231     exceptionTest3 = true;
00232   }
00233 
00234   if (!exceptionTest3) {
00235     printf("9. There should have been a SubString construction exception\n");
00236     rslt = false;
00237   }
00238 
00239   if (empty2.strlen() != 0) {
00240     printf("10. length of empty2 is wrong\n");
00241     rslt = false;
00242   }
00243 
00244   if (empty2.getRefCnt() != 1) {
00245     printf("11. reference count of empty2 is wrong\n");
00246     rslt = false;
00247   }
00248 
00249   String e = "12345678";
00250   String f = e;
00251   e(4,0) = "abcd";
00252   if (e != "1234abcd5678") {
00253     printf("12. insert, via a zero length section, failed\n");
00254     size_t len = e.strlen();
00255     printf("12. e.strlen() = %d\n", len );
00256     if (len > 0) {
00257       printf("12. e = %s\n", (const char *)e );
00258     }
00259   }
00260 
00261   if (e.strlen() != 12) {
00262     printf("13. e.strlen() = %d, should be 12\n", e.strlen() );
00263   }
00264 
00265   if (f != "12345678") {
00266     printf("14. improperly altered string in 'f'\n");
00267   }
00268 
00269   if (f.getRefCnt() != 1 && e.getRefCnt() != 1) {
00270     printf("15. reference counts are wrong\n");
00271   }
00272   
00273   return rslt;
00274 } // subStrSection
00275 
00276 
00277 
00281 bool subStrAssign()
00282 {
00283   bool rslt = true;
00284 
00285   printf("Test assignment to and from a SubString object\n");
00286   const char *CStr1 = "abcde1234jklm";
00287   const char *CStr2 = "abcdefghijklm";
00288   String a = CStr1;
00289 
00290   if (a != CStr1) {
00291     printf("1. Contents of String is incorrect\n");
00292     rslt = false;
00293   }
00294 
00295   String b = a;
00296   if (b.getRefCnt() != 2) {
00297     printf("2. Reference count is incorrect\n");
00298     rslt = false;
00299   }
00300 
00301   a(5, 4) = "fghi";
00302   if (a != CStr2) {
00303     printf("3. Contents of String is incorrect\n");
00304     rslt = false;
00305   }
00306 
00307   if (b.getRefCnt() != 1) {
00308     printf("4. Reference count is incorrect\n");
00309     rslt = false;
00310   }
00311   
00312   String c = a(0, 5);
00313   if (c != "abcde") {
00314     printf("5. Contents of String is incorrect\n");
00315     rslt = false;
00316   }
00317 
00318   // assign a string to a substring section
00319   a(5, 5) = c;
00320   if (a(0,10) != "abcdeabcde") {
00321     printf("6. Contents of String is incorrect\n");
00322     rslt = false;
00323   }
00324 
00325   // Now check assignment to regions that are not the same
00326   // length as the string being assigned.
00327 
00328   String target = "the lazy golden";
00329   String t2 = target;
00330 
00331   // assign a longer C-string to a shorter assignment region
00332   t2(0, 3) = "morgie is a";
00333   if (t2 != "morgie is a lazy golden") {
00334     printf("7. C-string assignment to SubString is wrong\n");
00335     rslt = false;
00336   }
00337 
00338   if (target.getRefCnt() != 1) {
00339     printf("8. Reference count is wrong\n");
00340     rslt = false;
00341   }
00342 
00343   if (target != "the lazy golden") {
00344     printf("9. The contents of target is wrong\n");
00345     rslt = false;
00346   }
00347 
00348   t2(17, 6) = "spaniel";
00349   // assign a shorter C-string to a longer assignment region
00350   t2(0, 6) = "max";
00351   if (t2 != "max is a lazy spaniel") {
00352     const char *Cstr = t2;
00353     printf("10. The contents of t2 is incorrect.  t2 = %s\n", Cstr );
00354     rslt = false;
00355   }
00356 
00357   // Test assignments of String to SubString
00358 
00359   // Longer assigned to shorter
00360   target = "max is a dog";
00361   String replaceAnimal = "people";
00362   String replaceName = "Maggie";
00363   target(9,3) = replaceAnimal;
00364   target(0,3) = replaceName;
00365   if (target != "Maggie is a people") {
00366     printf("11. String to SubString assignment failed\n");
00367     rslt = false;
00368   }
00369 
00370   // shorter assignments to longer SubString region
00371   replaceName = "Ian";
00372   target(0, 6) = replaceName;
00373 
00374   if (target != "Ian is a people") {
00375     printf("11. String to SubString assignment failed\n");
00376     rslt = false;
00377   }
00378 
00379   if (target.strlen() != strlen("Ian is a people")) {
00380     printf("12. length of the string is wrong\n");
00381     rslt = false;
00382   }
00383 
00384   // Test the explicit SubString constructor with assignment.
00385   // Also make sure that chained assignment works in this case.
00386 
00387   const char *expected_result = "the lazy golden";
00388   String u;
00389   String s = "the lazy dog";
00390   SubString t(s, 9, 3);
00391 
00392   u = t = "golden";
00393   
00394   if (s != expected_result) {
00395     printf("13. the contents of s is \"%s\", should be \"%s\"\n",
00396            (const char *)s, expected_result);
00397     rslt = false;
00398   }
00399 
00400   if (u != expected_result) {
00401     printf("14. the contents of u is \"%s\", should be \"%s\"\n",
00402            (const char *)u, expected_result);
00403     rslt = false;
00404   }
00405 
00406   // The result of the assignment to the SubString should be the
00407   // String, "s" passed to the constructor.  This is assigned to
00408   // the String u, so the reference count should be 2.
00409   if (u.getRefCnt() != 2) {
00410     printf("15. reference count is %d, should be 2\n", u.getRefCnt() );
00411     rslt = false;
00412   }
00413 
00414   return rslt;
00415 } // subStrAssign
00416 
00417 
00421 bool subStrRelations()
00422 {
00423   bool rslt = true;
00424   String a = "abcdeabcde";
00425   printf("Test SubString relational operators\n");
00426 
00427   //
00428   //  == operator
00429   //
00430 
00431   // SubString != SubString
00432   bool t1 = false;
00433   if (a(0,5) == a(5, 5)) {
00434     t1 = true;
00435   }
00436   if (!t1) {
00437     printf("1. SubString == SubString failed\n");
00438     rslt = false;
00439   }
00440 
00441   // SubString == Cstr 
00442   bool t2 = false;
00443   if (a(0,5) == "abcde") {
00444     t2 = true;
00445   }
00446   if (!t2) {
00447     printf("2. SubString == Cstr failed\n");
00448     rslt = false;
00449   }
00450 
00451   // SubString == String
00452   bool t3 = false;
00453   String b = "abcde";
00454   if (a(0,5) == b) {
00455     t3 = true;
00456   }
00457   if (!t3) {
00458     printf("3. SubString == String failed\n");
00459     rslt = false;
00460   }
00461 
00462   t1 = true;
00463   if (a(1,4) == "bcd") {
00464     t1 = false;
00465   }
00466   if (!t1) {
00467     printf("4. SubString == CStr of unequal length should have been false\n");
00468     rslt = false;
00469   }
00470 
00471 
00472   //
00473   // != operator
00474   //
00475   t1 = false; 
00476   t2 = false; 
00477   t3 = false;
00478   //          012345678901234
00479   String c = "abcdexyzzyabcde";
00480   // SubString != SubString
00481   if (a(0,5) != c(5,5)) {
00482     t1 = true;
00483   }
00484   if (!t1) {
00485     printf("5. SubString != SubString failed\n");
00486     rslt = false;
00487   }
00488 
00489   // SubString != Cstr
00490   if (a(0,5) != "xyzzy") {
00491     t2 = true;
00492   }
00493   if (!t2) {
00494     printf("6. SubString != SubString failed\n");
00495     rslt = false;
00496   }
00497   // SubString != String
00498   b = "zyzzy";
00499   if (a(0,5) != b) {
00500     t3 = true;
00501   }
00502   if (!t3) {
00503     printf("7. SubString != String failed\n");
00504     rslt = false;
00505   }
00506 
00507   bool t4 = false;
00508   if (a(0, 5) != "abcd") {
00509     t4 = true;
00510   }
00511   if (!t4) {
00512     printf("8. SubString != CStr of unequal length failed\n");    
00513     rslt = false;
00514   }
00515 
00516   t4 = true;
00517   if (a(1, 4) != "bcde") {
00518     t4 = false;
00519   }
00520   if (!t4) {
00521     printf("9. SubString != CStr are equal - test should be false\n");
00522     rslt = false;
00523   }
00524 
00525   //
00526   // < operator
00527   //
00528   t1 = false;
00529   t2 = false;
00530   t3 = false;
00531   t4 = false;
00532 
00533   // SubString < SubString
00534   if (a(1,5) < c(5,5)) {
00535     t1 = true;
00536   }
00537   if (!t1) {
00538     printf("10. SubString < SubString failed\n");
00539     rslt = false;
00540   }
00541 
00542   // SubString < SubString: overlapping SubString sections on the same String
00543   t1 = false;
00544   if (c(1,5) < c(5,5)) {
00545     t1 = true;
00546   }
00547   if (!t1) {
00548     printf("11. SubString < SubString failed for overlapping sections\n");
00549     rslt = false;
00550   }
00551 
00552   // SubString < Cstr
00553   t1 = false;
00554   if (c(1,5) < "pdsrsmzf") {
00555     t1 = true;
00556   }
00557   if (!t1) {
00558     printf("12. SubString < Cstr \n");
00559     rslt = false;
00560   }
00561 
00562   // SubString < String
00563   // note that b is still "xyzzy"
00564   t1 = false;
00565   if (c(0,8) < b) {
00566     t1 = true;
00567   }
00568   if (!t1) {
00569     printf("13. SubString < String \n");
00570     rslt = false;
00571   }
00572 
00573   //
00574   // > operator
00575   //
00576 
00577   // SubString > SubString
00578   //   0123456789
00579   String d = "    abc   ";
00580   t1 = false;
00581   if (c(10, 5) > d(2, 5)) {
00582     t1 = true;
00583   }
00584   if (!t1) {
00585     printf("14. SubString > SubString\n");
00586     rslt = false;
00587   }
00588   
00589   // SubString > Cstr (note, "xyz" will be "extended" with null chars)
00590   t1 = false;
00591   if (c(5, 5) > "xyz") {
00592     t1 = true;
00593   }
00594   if (!t1) {
00595     printf("15. SubString > Cstr\n");
00596     rslt = false;
00597   }
00598 
00599   // SubString > String
00600   b = "lesser of two evils";
00601   t1 = false;
00602   if (c(5, 5) > b) {
00603     t1 = true;
00604   }
00605   if (!t1) {
00606     printf("16. SubString > String\n");
00607     String ts = c(0, 5);
00608     const char *t1 = ts;
00609     const char *t2 = b;
00610     printf("c(0, 5) = %s, b = %s\n", t1, t2);
00611     rslt = false;
00612   }
00613 
00614   //
00615   // >= operator
00616   //
00617   t1 = false;
00618   if (c(0,5) >= c(10, 5)) {
00619     t1 = true;
00620   }
00621   if (!t1) {
00622     printf("17. SubString >= SubString\n");
00623     rslt = false;
00624   }
00625 
00626   t1 = false;
00627   if (c(0,5) >= "abcde") {
00628     t1 = true;
00629   }
00630   if (!t1) {
00631     printf("18. SubString >= CStr\n");
00632     rslt = false;
00633   }
00634 
00635   t1 = false;
00636   if (c(5,5) >= "abcde") {
00637     t1 = true;
00638   }
00639   if (!t1) {
00640     printf("19. SubString >= CStr\n");
00641     rslt = false;
00642   }
00643 
00644 
00645   t1 = false;
00646   b = "abcde";
00647   if (c(0,5) >= b) {
00648     t1 = true;
00649   }
00650   if (!t1) {
00651     printf("20. SubString >= String\n");
00652     rslt = false;
00653   }
00654 
00655   //
00656   // <= operator
00657   //
00658   t1 = false;
00659   if (c(0,5) <= c(5, 5)) {
00660     t1 = true;
00661   }
00662   if (!t1) {
00663     printf("21. SubString <= SubString\n");
00664     rslt = false;
00665   }
00666 
00667   t1 = false;
00668   if (c(0,5) <= "xyzzy") {
00669     t1 = true;
00670   }
00671   if (!t1) {
00672     printf("22. SubString <= CStr\n");
00673     rslt = false;
00674   }
00675 
00676   t1 = false;
00677   b = "xyzzy";
00678   if (c(0,5) <= b) {
00679     t1 = true;
00680   }
00681   if (!t1) {
00682     printf("23. SubString >= String\n");
00683     rslt = false;
00684   }
00685 
00686   t1 = false;
00687   b = "abcde";
00688   if (c(0,5) <= b) {
00689     t1 = true;
00690   }
00691   if (!t1) {
00692     printf("24. SubString >= String\n");
00693     rslt = false;
00694   }
00695 
00696   return rslt;
00697 } // subStrRelations
00698 
00699 
00700 
00705 bool globalSubStrRelations()
00706 {
00707   printf("Test global SubString relational operators\n");
00708   bool rslt = true;
00709 
00710   //          012345678901234
00711   String a = "abcdexyzzyabcde";
00712 
00713   // Cstr == SubString
00714   bool t1 = false;
00715   if ("abcde" == a(0, 5)) {
00716     t1 = true;
00717   }
00718   if (!t1) {
00719     printf("1. Cstr == SubString\n");
00720     rslt = false;
00721   }
00722 
00723   // Cstr != SubString
00724   t1 = false;
00725   if ("abcde" != a(5, 5)) {
00726     t1 = true;
00727   }
00728   if (!t1) {
00729     printf("2. Cstr != SubString\n");
00730     rslt = false;
00731   }
00732 
00733   // Cstr < SubString
00734   t1 = false;
00735   if ("abcde" < a(5, 5)) {
00736     t1 = true;
00737   }
00738   if (!t1) {
00739     printf("3. Cstr < SubString\n");
00740     rslt = false;
00741   }
00742 
00743   // Cstr > SubString
00744   t1 = false;
00745   if ("xyzzy" > a(0, 5)) {
00746     t1 = true;
00747   }
00748   if (!t1) {
00749     printf("4. Cstr > SubString\n");
00750     rslt = false;
00751   }
00752 
00753   // Cstr >= SubString
00754   t1 = false;
00755   if ("abcde" >= a(0, 5)) {
00756     t1 = true;
00757   }
00758   if (!t1) {
00759     printf("5. Cstr >= SubString\n");
00760     rslt = false;
00761   }
00762 
00763   // Cstr >= SubString
00764   t1 = false;
00765   if ("xyzzy" >= a(0, 5)) {
00766     t1 = true;
00767   }
00768   if (!t1) {
00769     printf("6. Cstr >= SubString\n");
00770     rslt = false;
00771   }
00772 
00773   // Cstr <= SubString
00774   t1 = false;
00775   if ("abcde" <= a(0, 5)) {
00776     t1 = true;
00777   }
00778   if (!t1) {
00779     printf("7. Cstr <= SubString\n");
00780     rslt = false;
00781   }
00782 
00783   // Cstr <= SubString
00784   t1 = false;
00785   if ("abcde" <= a(5, 5)) {
00786     t1 = true;
00787   }
00788   if (!t1) {
00789     printf("8. Cstr <= SubString\n");
00790     rslt = false;
00791   }
00792 
00793   return rslt;
00794 }  // globalSubStrRelations
00795 
00796 
00797 int
00798 main()
00799 {
00800   bool passed = true;
00801   printf("SubString tests \n");
00802 
00803   if (!subStrRefCnt()) {
00804     passed = false;
00805   }
00806 
00807   if (!subStrSection()) {
00808     passed = false;
00809   }
00810 
00811   if (!subStrAssign()) {
00812     passed = false;
00813   }
00814 
00815   if (!subStrRelations()) {
00816     passed = false;
00817   }
00818 
00819   if (! globalSubStrRelations()) {
00820     passed = false;
00821   }
00822 
00823   printf("Tests ");
00824   if (passed)
00825     printf("passed\n");
00826   else
00827     printf("failed\n");
00828   return 0;
00829 }

Generated on Mon Sep 22 20:22:58 2003 by doxygen 1.3.3