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

test-string.cc

00001 /*
00002 *  Name:      test-string.cc
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Test for string
00005 *  Date:      $Date: 2003/04/14 00:18:36 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #include <cstdlib>
00027 #include <iostream>
00028 #include <mpcl/text/string.hh>
00029 #include <mpcl/test.h>
00030 
00031 
00032 using mpcl::text::Format;
00033 using mpcl::text::TString;
00034 
00035 
00036 //
00037 //  L O C A L   F U N C T I O N S
00038 //
00039 
00040 static int _TestStreams (void)
00041 {
00042 
00043   using std::cin;
00044   using std::cout;
00045   using std::endl;
00046 
00047   TString   yTest2;
00048   TString   yTest1 ("Hola Que Tal");
00049 
00050   TEST_INIT ("test for streams member functions");
00051 
00052   if ( !TEST_IN_SILENT_MODE )
00053   {
00054     cout << "[" << yTest1 << "]" << endl;
00055 
00056     yTest1 += " estas cara de pan";
00057     yTest1 += "";
00058     yTest2 += " estas cara de pan";
00059     cout << "[" << yTest1 << "]" << endl;
00060     cout << "[" << yTest2 << "]" << endl;
00061 
00062     cin >> yTest1;
00063     cin >> yTest2;
00064 
00065     cout << "[" << yTest1 << "]" << endl;
00066     cout << "[" << yTest2 << "]" << endl;
00067   }
00068 
00069   TEST_MEMORY_STATUS;
00070   TEST_RETURN_CODE;
00071 
00072 }  // _TestStreams()
00073 
00074 
00075 static int _TestStringConversionSpeed (void)
00076 {
00077 
00078   TEST_INIT ("benchmarks for conversion functions");
00079 
00080   TEST_START_WATCH;
00081   for (long int J = 0; ( J < 250000 ) ;++J)
00082   {
00083     TEST_NUMBERS (atoi (Format ("%li", J).c_str()), J);
00084   }
00085   TEST_STOP_WATCH;
00086   TEST_DISPLAY_WATCH ("'Format()' integer conversion");
00087 
00088   TEST_START_WATCH;
00089   for (long int J = 0; ( J < 250000 ) ;++J)
00090   {
00091     TEST_STRINGS (Format ("%.2f", 1.02).c_str(), "1.02");
00092   }
00093   TEST_STOP_WATCH;
00094   TEST_DISPLAY_WATCH ("'Format()' float conversion");
00095 
00096   TEST_MEMORY_STATUS;
00097   TEST_RETURN_CODE;
00098 
00099 }  // _TestStringConversionSpeed()
00100 
00101 
00102 static int _TestFormatSpeed (void)
00103 {
00104 
00105   TString   yTest1;
00106   char      acBuffer [1024];
00107   
00108   TEST_INIT ("benchmarks for formating support functions");
00109 
00110   TEST_START_WATCH;
00111   for (long int J = 0; ( J < 250000 ) ;++J)
00112   {
00113     yTest1  = '\'';
00114     yTest1 += "Hola";
00115     yTest1 += '\'';
00116     TEST_STRINGS ("'Hola'", yTest1.c_str());
00117   }
00118   TEST_STOP_WATCH;
00119   TEST_DISPLAY_WATCH ("C++ string handling");
00120 
00121   TEST_START_WATCH;
00122   for (long int J = 0; ( J < 250000 ) ;++J)
00123   {
00124     TEST_STRINGS ("'Hola'", Format ("'%s'", "Hola").c_str());
00125   }
00126   TEST_STOP_WATCH;
00127   TEST_DISPLAY_WATCH ("'Format()' string handling");
00128 
00129   TEST_START_WATCH;
00130   for (long int J = 0; ( J < 200000 ) ;++J)
00131   {
00132     sprintf (acBuffer, "<%.2f>", 123.45);
00133     TEST_STRINGS ("<123.45>", acBuffer);
00134   }
00135   TEST_STOP_WATCH;
00136   TEST_DISPLAY_WATCH ("'std::sprintf()' benchmark");
00137 
00138   TEST_START_WATCH;
00139   for (long int J = 0; ( J < 200000 ) ;++J)
00140   {
00141     TEST_STRINGS ("<123.45>", Format ("<%.2f>", 123.45).c_str());
00142   }
00143   TEST_STOP_WATCH;
00144   TEST_DISPLAY_WATCH ("'Format()' benchmark");
00145 
00146   TEST_MEMORY_STATUS;
00147   TEST_RETURN_CODE;
00148 
00149 }  // _TestFormatSpeed (void)
00150 
00151 
00153 int main (void)
00154 {
00155 
00156   TEST_INIT ("tests for class 'TString'");
00157 
00158   TString   yTest2;
00159   TString   yTest3;
00160   TString   yTest1 ("Hola Que Tal");
00161 
00162   yTest1 += " estas cara de pan";
00163   yTest1 += "";
00164   yTest2 += " estas cara de pan";
00165   TEST_STRINGS (yTest1.c_str(), "Hola Que Tal estas cara de pan");  
00166   TEST_STRINGS (yTest2.c_str(), " estas cara de pan");  
00167 
00168   yTest1.uppercase();
00169   yTest2.lowercase();
00170 
00171   TEST_STRINGS (yTest1.c_str(), "HOLA QUE TAL ESTAS CARA DE PAN");
00172   TEST_STRINGS (yTest2.c_str(), " estas cara de pan");
00173 
00174   yTest1.replaceAll ('A', 'a');
00175   yTest2.replaceAll ('a', 'b');
00176   yTest2.replaceAll ("z", "");
00177   TEST_STRINGS (yTest1.c_str(), "HOLa QUE TaL ESTaS CaRa DE PaN");
00178   TEST_STRINGS (yTest2.c_str(), " estbs cbrb de pbn");
00179 
00180   TEST_NUMBERS (yTest1.replaceAll ('T', 't'), 2);
00181   TEST_NUMBERS (yTest2.replaceAll ("a", ""), 0);
00182   TEST_NUMBERS (yTest2.replaceAll ("z", ""), 0);
00183   TEST_STRINGS (yTest1.c_str(), "HOLa QUE taL EStaS CaRa DE PaN");
00184   TEST_STRINGS (yTest2.c_str(), " estbs cbrb de pbn");
00185 
00186   TEST_NUMBERS (1, TString ("ABC").replaceAll ('A', 'B'));
00187   TEST_NUMBERS (0, TString ("ABC").replaceAll ('Z', 'B'));
00188   TEST_NUMBERS (1, TString ("ABC").replaceAll ('C', 'Z'));
00189 
00190   TEST_NUMBERS (1, TString ("ABC").replaceAll ("AB", "ZZZZ"));
00191   TEST_NUMBERS (1, TString ("ABC").replaceAll ("BC", "ZZZZ"));
00192   TEST_NUMBERS (3, TString ("AAA").replaceAll ("A",  "AA"));
00193 
00194   yTest1 = "ABC";  yTest1.replaceAll ("AB", "ZZZZ");
00195   yTest2 = "ABC";  yTest2.replaceAll ("BC", "ZZZZ");
00196   yTest3 = "AAA-"; yTest3.replaceAll ("A",  "AA");
00197   TEST_STRINGS (yTest1.c_str(), "ZZZZC");
00198   TEST_STRINGS (yTest2.c_str(), "AZZZZ");
00199   TEST_STRINGS (yTest3.c_str(), "AAAAAA-");
00200   
00201   //
00202   //  TString::occurrences()
00203   //  
00204   TEST_NUMBERS (2, yTest1.occurrences ("ZZ"));
00205   TEST_NUMBERS (1, yTest1.occurrences ("ZZZZC"));
00206   TEST_NUMBERS (0, yTest1.occurrences ("ZZZZC5"));
00207   TEST_NUMBERS (1, yTest2.occurrences ("AZ"));
00208   TEST_NUMBERS (6, yTest3.occurrences ("A"));
00209   TEST_NUMBERS (6, yTest3.occurrences ('A'));
00210   TEST_NUMBERS (1, yTest3.occurrences ("-"));
00211   TEST_NUMBERS (1, yTest3.occurrences ('-'));
00212 
00213   yTest1 = "ABC";  yTest1.replaceAll ("AB", "");
00214   yTest2 = "ABC";  yTest2.replaceAll ("BC", "");
00215   yTest3 = "AAA-"; yTest3.replaceAll ("A",  "");
00216   TEST_STRINGS (yTest1.c_str(), "C");
00217   TEST_STRINGS (yTest2.c_str(), "A");
00218   TEST_STRINGS (yTest3.c_str(), "-");
00219   
00220   //
00221   //  TString::insert()
00222   //  
00223   TEST_STRINGS (TString ("ABC").insert (3, "AB").c_str(), "ABCAB");
00224   TEST_STRINGS (TString ("ABC").insert (0, "BC").c_str(), "BCABC");
00225   TEST_STRINGS (TString ("AAA-").insert (1, "-").c_str(), "A-AA-");
00226   
00227   //
00228   //  TString::substr()
00229   //  
00230   TEST_STRINGS (TString ("ABC").substr (0, 3).c_str(),  "ABC");
00231   TEST_STRINGS (TString ("ABC").substr (0, 1).c_str(),  "A");
00232   TEST_STRINGS (TString ("AAA-").substr (3, 1).c_str(), "-");
00233   
00234   //
00235   //  TString::operator OP ()
00236   //
00237   TEST_NUMBERS (100220,  atoi (TString ("100220").c_str()));
00238   TEST_NUMBERS (-100220, atoi (TString ("-100220").c_str()));
00239   TEST_NUMBERS (false,   atoi (TString ("0000").c_str()));
00240   TEST_NUMBERS (true,    atoi (TString ("1").c_str()));
00241   TEST_NUMBERS (false,   atoi (TString ("Tango").c_str()));
00242   TEST_NUMBERS (false,   atoi (TString ("Cash").c_str()));
00243 
00244   //
00245   //  TString::leftFill() && TString::rigthFill()
00246   //
00247   TEST_NUMBERS (TString ("AAA").assign (3, 'B'), TString ("BBB"));
00248   TEST_NUMBERS (TString ("AAA").leftFill ('B', 2), TString ("AAA"));
00249   TEST_NUMBERS (TString ("AAA").leftFill ('B', 4), TString ("BAAA"));
00250   TEST_NUMBERS (TString ("AAA").leftFill ('B', 10), TString ("BBBBBBBAAA"));
00251   TEST_NUMBERS (TString ("AAA").rightFill ('B', 3), TString ("AAA"));
00252   TEST_NUMBERS (TString ("AAA").rightFill ('B', 2), TString ("AAA"));
00253   TEST_NUMBERS (TString ("AAA").rightFill ('B', 4), TString ("AAAB"));
00254   TEST_NUMBERS (TString ("AAA").rightFill ('B', 10), TString ("AAABBBBBBB"));
00255 
00256   //
00257   //  Format()
00258   //
00259   yTest1 = Format ("%s, Hola que tal %s como %% estas %s", "Buenos dias", "fali", "tio");
00260   TEST_NUMBERS (Format   ("%s", "Hola"), TString ("Hola"));
00261   TEST_NUMBERS (Format   ("%s,%s", "Hola", "Hola"), TString ("Hola,Hola"));
00262   TEST_NUMBERS (Format   ("%s%%%s", "Hola", "Hola"), TString ("Hola%Hola"));
00263   TEST_NUMBERS (Format   ("hola"), TString ("hola"));
00264   TEST_NUMBERS (yTest1,  TString ("Buenos dias, Hola que tal fali como % estas tio"));
00265   TEST_STRINGS ("75000", Format ("%ld", 75000).c_str());
00266 
00267   //
00268   //  TString::suppress()
00269   //
00270   TEST_STRINGS (TString ("Hola").suppress (' ').c_str(),                  "Hola");
00271   TEST_STRINGS (TString ("Hola ").suppress ('l').c_str(),                 "Hoa ");
00272   TEST_STRINGS (TString ("Hola capullo         ").suppress (' ').c_str(), "Holacapullo");
00273   TEST_STRINGS (TString ("Hola capullo").suppress ('l').c_str(),          "Hoa capuo");
00274   TEST_STRINGS (TString ("Hola").suppress ('H').c_str(),                  "ola");
00275 
00276   //
00277   //  TString::suppressLast()
00278   //
00279   TEST_STRINGS (TString ("Hola").suppressLast (' ').c_str(),                  "Hola");
00280   TEST_STRINGS (TString ("Hola ").suppressLast (' ').c_str(),                 "Hola");
00281   TEST_STRINGS (TString ("Hola capullo         ").suppressLast (' ').c_str(), "Hola capullo");
00282   TEST_STRINGS (TString (" Hola capullo        ").suppressLast (' ').c_str(), " Hola capullo");
00283   TEST_STRINGS (TString (" Hola ").suppressLast (' ').c_str(),                " Hola");
00284 
00285   //
00286   //  TString::suppressFirst()
00287   //
00288   TEST_STRINGS (TString ("Hola").suppressFirst (' ').c_str(),                  "Hola");
00289   TEST_STRINGS (TString (" Hola ").suppressFirst (' ').c_str(),                "Hola ");
00290   TEST_STRINGS (TString ("        Hola capullo").suppressFirst (' ').c_str(),  "Hola capullo");
00291   TEST_STRINGS (TString (" Hola capullo        ").suppressFirst (' ').c_str(), "Hola capullo        ");
00292   TEST_STRINGS (TString (" Hola ").suppressFirst (' ').c_str(),                "Hola ");
00293 
00294   //
00295   //  Function templates for TString
00296   //
00297   yTest1 = "Hello Dolly. How are you?";
00298   yTest2 = "Hello Dolly. How are you?";
00299   TEST_NUMBERS (( yTest1.uppercase() == "HELLO DOLLY. HOW ARE YOU?" ), true);
00300   TEST_NUMBERS (( yTest2.lowercase() == "hello dolly. how are you?" ), true);
00301 
00302   TEST_FUNCTION (_TestStreams());
00303   TEST_FUNCTION (_TestStringConversionSpeed());
00304   TEST_FUNCTION (_TestFormatSpeed());
00305   try
00306   {
00307     TString   yTest4 ((const char*) NULL);
00308 
00309     TEST_FAIL;
00310   }
00311   catch (const mpcl::TConstraintException&)
00312   {
00313     TEST_PASS;
00314   }
00315 
00316   TEST_MEMORY_STATUS;
00317   TEST_RETURN_CODE;
00318 
00319 }  // main()

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18