stringutiltest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: stringutiltest.cpp 1895 2009-05-12 19:34:29Z afarris $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 //  Copyright 2008, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 #include <iostream>
00026 #include <string>
00027 #include "StringUtils.hpp"
00028 
00029 using namespace gpstk::StringUtils;
00030 using namespace std;
00031 
00038 // must be big enough to show hex digits in index
00039 char hexDumpMess[] =
00040 { 0x00, 0x10, 0x30, 0x33, 0x30, 0x31, 0x30, 0x35,
00041   0x3A, 0x65, 0x70, 0x68, 0x20, 0x3A, 0x30, 0x3A,
00042   0x35, 0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33,
00043   0x33, 0x34, 0x30, 0x32, 0x37, 0x37, 0x37, 0x37,
00044   0x37, 0x37, 0x37, 0x38, 0x42, 0x72, 0x69, 0x61,
00045   0x6e, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x68,
00046   0x6f, 0x73, 0x65, 0x72, 0x32, 0x35, 0x39, 0x32,
00047   0x35, 0x39, 0x32, 0x34, 0x20, 0x32, 0x20, 0x35,
00048   0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33, 0x38,
00049   0x38, 0x33, 0x36, 0x33, 0x34, 0x32, 0x35, 0x39,
00050   0x32, 0x35, 0x39, 0x33, 0x20, 0x32, 0x20, 0x38,
00051   0x35, 0x34, 0x31, 0x32, 0x20, 0x31, 0x20, 0x31,
00052   0x20, 0x30, 0x20, 0x31, 0x20, 0x31, 0x20, 0x34,
00053   0x20, 0x32, 0x20, 0x34, 0x20, 0x30, 0x20, 0x30,
00054   0x20, 0x33, 0x20, 0x31, 0x31, 0x39, 0x39, 0x20,
00055   0x32, 0x32, 0x63, 0x30, 0x61, 0x66, 0x30, 0x33,
00056   0x20, 0x31, 0x37, 0x37, 0x30, 0x32, 0x39, 0x65,
00057   0x34, 0x20, 0x61, 0x66, 0x34, 0x30, 0x30, 0x31,
00058   0x66, 0x20, 0x65, 0x36, 0x31, 0x62, 0x35, 0x38,
00059   0x61, 0x20, 0x35, 0x35, 0x66, 0x33, 0x37, 0x35,
00060   0x30, 0x20, 0x34, 0x63, 0x63, 0x61, 0x38, 0x38,
00061   0x37, 0x20, 0x31, 0x31, 0x35, 0x64, 0x37, 0x63,
00062   0x66, 0x63, 0x20, 0x33, 0x61, 0x64, 0x32, 0x30,
00063   0x34, 0x38, 0x61, 0x20, 0x63, 0x65, 0x61, 0x20,
00064   0x33, 0x36, 0x32, 0x63, 0x34, 0x38, 0x20, 0x31,
00065   0x31, 0x39, 0x39, 0x20, 0x32, 0x32, 0x63, 0x30,
00066   0x61, 0x66, 0x30, 0x33, 0x20, 0x31, 0x37, 0x37,
00067   0x30, 0x34, 0x61, 0x37, 0x34, 0x20, 0x33, 0x61,
00068   0x63, 0x34, 0x36, 0x36, 0x63, 0x31, 0x20, 0x62,
00069   0x64, 0x30, 0x63, 0x36, 0x64, 0x63, 0x20, 0x33,
00070   0x39, 0x31, 0x62, 0x33, 0x37, 0x38, 0x33, 0x20,
00071   0x33, 0x65, 0x30, 0x34, 0x30, 0x63, 0x66, 0x85 };
00072 
00074 int main()
00075 {
00076    try
00077    {
00078       int rc = 0;
00079       string result, ref;
00080       string a = "1234567890", b = "456";
00081       string aa = "---1234567890---";
00082       char c = '-';
00083       string d = "--";
00084       string hexDumpString(hexDumpMess, sizeof(hexDumpMess));
00085 
00086          // Someone may want to do this to a string stream and do some
00087          // comparisons and what-not... for now, I'm only testing to
00088          // make sure it doesn't crash...
00089       cout << "Standard hex dump:" << endl;
00090       hexDumpData(cout, hexDumpString);
00091       cout << endl << "Alternate hex dump:" << endl;
00092       HexDumpDataConfig hdcfg(true, true, true, 8, 2, 2, 1, 0, 0, 16, true,
00093                               '\'', 1);
00094       hexDumpData(cout, hexDumpString, 6, hdcfg);
00095       
00096       cout << endl << "Above using printable():" << endl;
00097       cout << printable(hexDumpString) << endl;
00098 
00099 
00100          //#############################
00101          // Justification tests
00102   
00103       cout << "Original: " << a << endl;
00104       ref = a;
00105       result = string(3,c) + a;
00106       a = rightJustify(a, a.length() + 3, c);
00107       if(a != result)
00108       {
00109          cout  << "rightJustify failed: " << a << endl 
00110                << "Should be: " << result << endl;
00111       }
00112   
00113       a = ref;
00114       result = a + string(3,c);
00115       a = leftJustify(a, a.length()+3, c);
00116       if(a != result)
00117       {
00118          cout << "leftJustify failed: " << a << endl 
00119               << "Should be: " << result << endl;
00120       }
00121   
00122       a = ref;
00123       result = string(3,c) + a + string(3,c);
00124       a = center(a, a.length()+6, c);
00125       if(a != result)
00126       {
00127          cout << "center failed: " << a << endl
00128               << "Should be: " << result << endl;
00129       }
00130 
00131       a = ref;
00132 
00133          //#############################
00134          // Strip Tests
00135 
00136          //######## Leading
00137       ref = aa;
00138       result = aa.substr(1,string::npos);
00139       aa = stripLeading(aa, c, 1);
00140       if(aa != result)
00141       {
00142          cout << "stripLeading(string, char, 1) failed: " << aa << endl
00143               << "Should be: " << result << endl;
00144       }
00145 
00146       aa = ref;
00147       result = aa.substr(2,string::npos);
00148       aa = stripLeading(aa, d, 1);
00149       if(aa != result)
00150       {
00151          cout << "stripLeading(string, char *, 1) failed: " << aa  << endl
00152               << "Should be: " << result << endl;
00153       }
00154 
00155       aa = ref;
00156       result = aa.substr(3,string::npos);
00157       aa = stripLeading(aa, c);
00158       if(aa != result)
00159       {
00160          cout << "stripLeading(string, char) failed: " << aa << endl
00161               << "Should be: " << result << endl;
00162       }
00163 
00164       aa = ref;
00165       result = aa.substr(2,string::npos);
00166       aa = stripLeading(aa, d);
00167       if(aa != result)
00168       {
00169          cout << "stripLeading(string, char *) failed: " << aa  << endl
00170               << "Should be: " << result << endl;
00171       }
00172 
00173          //######## Trailing
00174       aa = ref;
00175       result = aa.substr(0, aa.length()-1);
00176       aa = stripTrailing(aa, c, 1);
00177       if(aa != result)
00178       {
00179          cout << "stripTrailing(string, char, 1) failed: " << aa  << endl
00180               << "Should be: " << result << endl;
00181       }
00182 
00183       aa = ref;
00184       result = aa.substr(0, aa.length()-2);
00185       aa = stripTrailing(aa, d, 1);
00186       if(aa != result)
00187       {
00188          cout << "stripTrailing(string, char *, 1) failed: " << aa  << endl
00189               << "Should be: " << result << endl;
00190       }
00191 
00192       aa = ref;
00193       result = aa.substr(0, aa.length()-3);
00194       aa = stripTrailing(aa, c);
00195       if(aa != result)
00196       {
00197          cout << "stripTrailing(string, char) failed: " << aa  << endl
00198               << "Should be: " << result << endl;
00199       }
00200 
00201       aa = ref;
00202       result = aa.substr(0, aa.length()-2);
00203       aa = stripTrailing(aa, d);
00204       if(aa != result)
00205       {
00206          cout << "stripTrailing(string, char *) failed: " << aa  << endl
00207               << "Should be: " << result << endl;
00208       }
00209 
00210          //######## Both Leading & Trailing
00211       aa = ref;
00212       result = aa.substr(1,aa.length()-2);
00213       aa = strip(aa, c, 1);
00214       if(aa != result)
00215       {
00216          cout << "strip(string, char, 1) failed: " << aa  << endl
00217               << "Should be: " << result << endl;
00218       }
00219   
00220       aa = ref;
00221       result = aa.substr(2,aa.length()-4);
00222       aa = strip(aa, d, 1);
00223       if(aa != result)
00224       {
00225          cout << "strip(string, char *, 1) failed: " << aa  << endl
00226               << "Should be: " << result << endl;
00227       }
00228   
00229       aa = ref;
00230       result = aa.substr(3, aa.length()-6);
00231       aa = strip(aa,c);
00232       if(aa != result)
00233       {
00234          cout << "strip(string, char) failed: " << aa  << endl
00235               << "Should be: " << result << endl;
00236       }
00237   
00238       aa = ref;
00239       result = aa.substr(2, aa.length()-4);
00240       aa = strip(aa,d);
00241       if(aa != result)
00242       {
00243          cout << "strip(string, char *) failed: " << aa  << endl
00244               << "Should be: " << result << endl;
00245       }
00246 
00247          //################################ 
00248          //translation
00249       string xlat = "The Hoopy Ford Prefect Boffs the Eccentrica Gallumbits";
00250       string xlati1 = "sdfgDFG";
00251       string xlato1 = "!)#%#)%";
00252       string xlata1 = "The Hoopy )or) Pre#ect Bo##! the Eccentrica %allumbit!";
00253       string xlati2 = "sdfgDFG";
00254       string xlato2 = "!)#%";
00255       string xlata2 = "The Hoopy  or) Pre#ect Bo##! the Eccentrica  allumbit!";
00256       string xlati3 = "sdfgDFG";
00257       string xlato3 = "sdfgDFG";
00258       string xlata3 = "The Hoopy Ford Prefect Boffs the Eccentrica Gallumbits";
00259       aa = translate(xlat, xlati1, xlato1);
00260       if (aa != xlata1)
00261       {
00262          cout << "translate(const string&,const string&,const string&,char) "
00263               << "failed: " << aa << endl
00264               << "should be " << xlata1 << endl;
00265       }
00266       aa = translate(xlat, xlati2, xlato2);
00267       if (aa != xlata2)
00268       {
00269          cout << "translate(const string&,const string&,const string&,char) "
00270               << "failed: " << aa << endl
00271               << "should be " << xlata2 << endl;
00272       }
00273       aa = translate(xlat, xlati3, xlato3);
00274       if (aa != xlata3)
00275       {
00276          cout << "translate(const string&,const string&,const string&,char) "
00277               << "failed: " << aa << endl
00278               << "should be " << xlata3 << endl;
00279       }
00280 
00281          //################################ 
00282          //String to number
00283       int res = asInt(a);
00284       if (1234567890 != res)
00285       {
00286          cout << "asInt(string&) failed: " << res << endl
00287               << "should be 1234567890" << endl;
00288       }
00289 
00290       a = "12345.67890";
00291       double res2 = asDouble(a);
00292       if ( (double)(12345.67890) != res2)
00293       {
00294          cout << "asDouble(string&) failed: " << setprecision(15) << res2 << endl
00295               << "should be 12345.67890 " << endl;
00296       }
00297 
00298       float res25 = asFloat(a);
00299       if( (float)(12345.67890) != res25)
00300       {
00301          cout << "asFloat(string&) failed: " << setprecision(15) << res25 << endl
00302               << "should be 12345.67890 " << endl;
00303       }
00304   
00305       long double lda = 12345.67890L;
00306       long double res3 = asLongDouble(a);
00307       if (lda != res3)
00308       {
00309          cout << "asLongDouble(string&) failed: " << setprecision(21) << res3
00310               << endl << "should be 12345.67890 " << endl;
00311       }
00312   
00313          //#####################################33
00314          // Number as string
00315     
00316       res2 = 12345.6789; // double
00317       res3 = 12345.6789L; // long double
00318       res = 1234567890; // int
00319 
00320       aa = asString(res2);
00321       if(asDouble(aa) != res2)
00322       {
00323          cout << "asString(double) failed: " << aa << endl
00324               << "should be 12345.6789" << endl;
00325       }
00326   
00327       aa = asString(res3);
00328       if(asLongDouble(aa) != res3)
00329       {
00330          cout << "asString(long double) failed: " << aa << endl
00331               << "should be 12345.6789" << endl;
00332       }
00333 
00334       aa = asString(res);
00335       if(asInt(aa) != res)
00336       {
00337          cout << "asString(X) failed: " << aa << endl
00338               << "should be " << res << endl;
00339       }
00340   
00341          // ##############################3
00342          // Hex tests
00343 
00344       string q="0x100 bleh";
00345       string z="100 moo";
00346 
00347       result = x2d(q);
00348       if (result != string("256 bleh"))
00349       {
00350          cout << "x2d failed: " << result << endl
00351               << "Should be: '256 bleh'"<< endl;
00352       }
00353 
00354       result = d2x(z);
00355       if (result != string("64 moo"))
00356       {
00357          cout << "x2d failed: " << result << endl
00358               << "Should be: '64 moo'"<< endl;
00359       }
00360 
00361          //##############################3
00362          // String replacement
00363 
00364       a   = "010101000111010";
00365       aa  = "0-0-0-000---0-0";
00366       string aaa = "0abc0abc0abc000abcabcabc0abc0";
00367   
00368       ref = a;
00369       a = replaceAll(a, string("1"), string("-"));
00370       if(a != aa)
00371       {
00372          cout << "replaceAll() failed: " << a << endl
00373               << "should be            " << aa << endl;
00374       }
00375 
00376       a = ref;
00377       a = replaceAll(a, string("1"), string("abc"));
00378       if(a != aaa)
00379       {
00380          cout << "replaceAll() failed: " << a << endl
00381               << "should be            " << aaa << endl;
00382       }
00383 
00384       a = ref;
00385       aaa = "01230123012300012312312301230";
00386       a = replaceAll(a, string("1"), string("123"));
00387       if(a != aaa)
00388       {
00389          cout << "replaceAll() failed: " << a << endl
00390               << "should be            " << aaa << endl;
00391       }
00392 
00393          //##############################################
00394          // Word Tests
00395       a = "      The  Quick    Brown  Fox     Jumps  Over    The  Lazy    Dog.    ";
00396   
00397       aa = firstWord(a);
00398       if(aa != string("The"))
00399       {
00400          cout << "firstWord() failed: " << aa << endl
00401               << "should be 'The'" << endl;
00402       }
00403   
00404       res = numWords(a);
00405       if(res != 9)
00406       {
00407          cout << "numWords() failed: " << res << endl
00408               << "should be 9" << endl;
00409       }
00410   
00411       aa = word(a, 4);
00412       if(aa != string("Jumps"))
00413       {
00414          cout << "word() failed: " << aa << endl
00415               << "should be 'Jumps'" << endl;
00416       }
00417 
00418       removeWords(a, 3, 2);
00419       result = "      The  Quick    Brown  Over    The  Lazy    Dog.    ";
00420       if(a != result)
00421       {
00422          cout << "removeWords() failed: " << a << endl
00423               << "should be '" << result << "'" << endl;
00424       }
00425 
00426       aa = stripFirstWord(a);
00427       result = "Quick    Brown  Over    The  Lazy    Dog.    ";
00428       if ((aa != string("The")) ||
00429           (a != result))
00430       {
00431          cout << "stripFirstWord() failed: " << aa << endl
00432               << "should be 'The'" << endl
00433               << "and: " << a << endl
00434               << "should be '" << result << "'" << endl;
00435       }
00436 
00437       removeWords(a, 5);
00438       result = "Quick    Brown  Over    The  Lazy";
00439       if (a != result)
00440       {
00441          cout << "removeWords failed: " << a << endl
00442               << "should be '" << result << "'" << endl;
00443       }
00444 
00445       aa = words(a, 3);
00446       result = "The  Lazy";
00447       if (aa != result)
00448       {
00449          cout << "words failed: \'" << aa << "' ("  << a << ")" << endl
00450               << "should be '" << result << "'" << endl;
00451       }
00452 
00453          //####################
00454          // regular expressions
00455 
00456       rc = isLike(a, "Br.*Over.*");
00457       if (rc != 1)
00458       {
00459          cout << "isLike (Br.*Over.*) failed: " << rc << endl
00460               << "should be: 1" << endl;
00461       }
00462 
00463       rc = isLike(a, "Br.Over.*");
00464       if (rc != 0)
00465       {
00466          cout << "isLike (Br.Over.*) failed: " << rc << endl
00467               << "should be: 0" << endl;
00468       }
00469 
00470       aa = matches(a, "Br.*Over.*");
00471       result = "Brown  Over    The  Lazy";
00472       if (aa != result)
00473       {
00474          cout << "matches failed: " << aa << endl
00475               << "should be: '" << result << "'" << endl;
00476       }
00477 
00478       string prettyPrintThis("This is a very long sentence that will get cut up into lines with a maximum length of 40 characters, each line indented by a few spaces.");
00479       prettyPrint(prettyPrintThis, "\n", "    ", "      ", 40);
00480       cout << prettyPrintThis << endl;
00481 
00482       cout << "All other Tests Passed." << endl; 
00483 
00484       return 0;
00485    }
00486    catch (gpstk::Exception& e)
00487    {
00488       cout << e << endl;
00489    }
00490 }

Generated on Wed May 23 03:31:02 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1