String routines to complement <string> && <cstring>:
string_tool_test.cpp
Go to the documentation of this file.
1 /* string_tool_test.cpp (C) 2012 adolfo@di-mare.com */
2 
3 /** \file string_tool_test.cpp
4  \brief Programa de prueba para las rutinas de \c string_tool.h.
5 
6  \author Adolfo Di Mare <adolfo@di-mare.com>
7  \date 2012
8 */
9 
10 #include "uUnit.h" // assertTrue()+assertFalse()
11 #include <iostream> // std::cout
12 
13 #include "string_tool.h"
14 #include "psz_string.h" // recuerda delete[] al usar (char*)
15 
16 /// Programa de prueba para las rutinas de \c string_tool.h.
17 int main( int argc , const char* argv[] ) {
18  // Pone el rótulo de la prueba
19  std::cout << "test->string_tool() [uUnit.h]" << std::endl;
20 
21  /// Simple macro to define && execute a test
22  #define exec_TEST(t) void t(); t();
23  if (true) {
39  }
40  else {
41  }
42  #undef exec_TEST
43 
44  return 0;
45 }
46 
47 /// \c test->trimQuote().
49 {{ // test::trimQuote()
50  std::string A = "\'A\'"; trimQuote(A);
51  assertTrue( "A"==A );
52 
53  std::string N = "\"\'"; trimQuote(N);
54  assertTrue( ""==N );
55 
56  std::string C = "\"C\""; trimQuote(C);
57  assertTrue( "C"==C );
58 
59  std::string ABC123XYZ = "ABC123XYZ";
60  trimQuote( ABC123XYZ );
61  assertTrue( "ABC123XYZ" == ABC123XYZ );
62 }}
63 }
64 
65 /// \c test->tolower().
66 void test_tolower() {
67 {{ // test::tolower()
68  std::string A = "A"; tolower(A);
69  assertTrue( "a"==A );
70 
71  std::string ABC123XYZ = "ABC123XYZ";
72  tolower( ABC123XYZ );
73  assertTrue( "abc123xyz" == ABC123XYZ );
74 }}
75  std::string UP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; tolower(UP);
76  assertTrue( "abcdefghijklmnopqrstuvwxyz" == UP );
77 }
78 
79 /// test->removeAccented() && test->removeAccented::string()
81  using namespace std;
82 {{ // test::removeAccented(string)
83  string Atilde = A_TILDE_64;
84  string Asimple = A_SIMPLE_64;
85 
86  string Astr = Atilde;
87  assertTrue( Asimple == removeAccented(Astr) );
88 }}
89 {{ // test::removeAccented(char*)
90  char Atilde[] = A_TILDE_64;
91  char Asimple[] = A_SIMPLE_64;
92 
93  char Astr[ sizeof(Atilde) ];
94  strcpy( Astr, Atilde );
95 
96  assertTrue( 0==strcmp( Asimple, removeAccented(Astr) ) );
97 }}
98 {{ // test::removeAccented(char)
99  char Atilde[] = A_TILDE_64;
100  char Asimple[] = A_SIMPLE_64;
101 
102  assertTrue( 'A' == removeAccented('Á') ); assertTrue( 'N' == removeAccented('Ñ') );
103  assertTrue( 'Y' == removeAccented('Ý') ); assertTrue( 'D' == removeAccented('Ð') );
104 
105  assertTrue( 's' == removeAccented('ß') ); assertTrue( 'P' == removeAccented('Þ') );
106  assertTrue( 'x' == removeAccented('×') ); assertTrue( '/' == removeAccented('÷') );
107  assertTrue( '0' == removeAccented('Ø') ); assertTrue( 'O' == removeAccented('ð') );
108 
109  assertTrue( Atilde != Asimple );
110 }}
111 {
112  // Copia del código que está arriba
113  const char *Atilde = A_TILDE_64;
114  const char *Asimple = A_SIMPLE_64;
115 
116  char Astr[ 255-192+1 ]; *Astr = 0;
117  strcpy( Astr, Atilde );
118 
119  assertTrue( 0==strcmp( Asimple, removeAccented(Astr) ) );
120  assertTrue( strlen(Asimple)==strlen(Astr) );
121 
122  #ifdef __GNUC__ // Esto es raro ...
123  assertTrue( (unsigned char)('À')==192 );
124  assertFalse( (char)('À')==192 );
125  assertFalse( (unsigned) ('À')==192 );
126  // http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
127  #endif
128 
129 // std::cout << 'À' << "\n" << Atilde << "\n" << Astr << "\n";
130 }
131 {
132  // verifica que la implementación de removeAccented(char) funciona
133  const char *Atilde = A_TILDE_64;
134  const char *Asimple = A_SIMPLE_64;
135 
136  char Astr[ 255-192+1 ];
137  strcpy( Astr, Atilde );
138 
139  char *p = Astr;
140  while ( (*p)!=0 ) {
141  *p = removeAccented(*p) ;
142  ++p;
143  // http://stackoverflow.com/questions/14094621/
144  }
145  assertTrue( 0==strcmp( Asimple, removeAccented(Astr) ) );
146  assertTrue( strlen(Asimple)==strlen(Astr) );
147  assertTrue( 'n' == removeAccented('ñ') );
148 }
149 }
150 
151 /// \c test->isalpha().
152 void test_isalpha() {
153 {{ // test::isalpha()
154  assertTrue( isalpha("A") );
155  assertTrue( isalpha("abcABCxyz") );
156  assertFalse( isalpha("123abc456") );
157 }}
158  assertTrue( isalpha("abcdefghijklmnopqrstuvwxyz") );
159  assertTrue( isalpha("ABCDEFGHIJKLMNOPQRSTUVWXYZ") );
160 }
161 
162 /// \c test->is_blank().
164 {{ // test::is_blank()
165  assertTrue( is_blank(" \t\r\n\v") );
166  assertFalse( is_blank(" ? ") );
167 }}
168 }
169 
170 /// \c test->digitCount().
172  using namespace std;
173 {{ // test::digitCount()
174  string sAE = "ABCDE";
175  string s15 = "12345";
176  assertTrue( digitCount("12345",0)==5 && s15[0]=='1' );
177  assertTrue( digitCount("12345",4)==1 && s15[4]=='5' );
178  assertTrue( digitCount("12345",5)==string::npos ); // fuera de rango
179  assertTrue( digitCount("ABCDE",0)==0 );
180 }}
181  assertTrue( "12345"[0]=='1' );
182  assertTrue( "12345"[4]=='5' );
183  assertTrue( digitCount("12345",0)==5 && "12345"[0]=='1' );
184  assertTrue( digitCount("12345",4)==1 && "12345"[4]=='5' );
185  assertTrue( digitCount("12345",5)==string::npos && "12345"[5]==0 );
186  assertTrue( digitCount("12345",6)==string::npos );
187  assertTrue( digitCount(string(),0)==string::npos );
188 
189  string s15 = "12345"; s15 += '.';
190  assertTrue( digitCount(s15,5)==0 && s15[5]=='.' );
191 
192  assertTrue( digitCount("--3",0)==0 );
193  assertTrue( digitCount( "-3",0)==0 );
194  assertTrue( digitCount( "3",0)==1 );
195  assertTrue( digitCount("++3",0)==0 );
196  assertTrue( digitCount( "+3",0)==0 );
197  assertTrue( digitCount( "3",0)==1 );
198 }
199 
200 /// \c test->alphaCount().
202  using namespace std;
203 {{ // test::alphaCount()
204  assertTrue( alphaCount("ABCDE",0)==5 && "ABCDE"[0]=='A' );
205  assertTrue( alphaCount("ABCDE",4)==1 && "ABCDE"[4]=='E' );
206  assertTrue( alphaCount("ABCDE",5)==string::npos ); // fuera de rango
207  assertTrue( alphaCount("12345",0)==0 );
208 }}
209 //
210  string s15 = "12345";
211  assertTrue( alphaCount("ABCDE",0)==5 && "ABCDE"[0]=='A' );
212  assertTrue( alphaCount("ABCDE",4)==1 && "ABCDE"[4]=='E' );
213 
214  string sAE = "ABCDE"; sAE += '.';
215  assertTrue( alphaCount(sAE,5)==0 && sAE[5]=='.' );
216  assertTrue( alphaCount(sAE,6)==string::npos );
217  assertTrue( alphaCount(string(),0)==string::npos );
218 }
219 
220 /// \c test->roundUp().
221 void test_roundUp() {
222  #undef roundUp
223  // no_roundUp(n,b) ( (n)%(b)==0 ? n : (b)*( (n)/(b) )+(b) )
224  // no_roundUp(n,b) ( (n)+(b) - (n)%(b) )
225 
226  /// Rounding up to the nearest multiple of a number.
227  /// - Not tested for negative numbers.
228  /// \see http://stackoverflow.com/questions/3407012/
229  #define roundUp(n,b) ( (b)==0 ? (n) : ( ((n)+(b)-1) - (((n)-1)%(b)) ) )
230 
231  // yes_roundUp(n,b) ( (b)==0 ? (n) : ( (n)%(b)==0 ? n : (n)+(b)-(n)%(b) ) )
232  // yes_roundUp(n,b) ( (b)==0 ? (n) : ( ((n + b - 1) / b) * b ) )
233 
234 if (true) // todavía no funciona sin (?:)
235 {{ // test::roundUp()
236  unsigned m;
237  { m = roundUp(17,8); } ++m;
238  assertTrue( 24 == roundUp(17,8) );
239  { m = roundUp(24,8); }
240  assertTrue( 24 == roundUp(24,8) );
241 
242  assertTrue( 24 == roundUp(24,4) );
243  assertTrue( 24 == roundUp(23,4) );
244  { m = roundUp(23,4); }
245  assertTrue( 24 == roundUp(21,4) );
246 
247  assertTrue( 20 == roundUp(20,4) );
248  assertTrue( 20 == roundUp(19,4) );
249  assertTrue( 20 == roundUp(18,4) );
250  assertTrue( 20 == roundUp(17,4) );
251 
252  assertTrue( 17 == roundUp(17,0) );
253  assertTrue( 20 == roundUp(20,0) );
254 }}
255  #undef roundUp
256 }
257 
258 /// \c test->issuffix().
260  using namespace std;
261 {{ // test::issuffix()
262  assertTrue( issuffix("la mano alzada","ALZADA" ) );
263  assertFalse( issuffix("ALZADA","la mano alzada" ) );
264  assertTrue( issuffix("alzada","ALZADA" ) );
265  assertTrue( issuffix(0,"") );
266  assertTrue( issuffix("",0) ); // NULL
267 }}
268  assertTrue( issuffix(0,0) );
269  assertTrue( issuffix("","") );
270  assertTrue( issuffix("alzada","" ) );
271 
272  assertTrue( issuffix("121","1" ) );
273  assertFalse( issuffix("1","121" ) );
274 
275  assertTrue( issuffix("12321","21" ) );
276  assertFalse( issuffix("21","12321" ) );
277  {
278  const char *filename ,*name_cfg;
279  filename = "yy-VACIO-bt0pp.cfg";
280  name_cfg = "Xvacio-bt0pp.cfg";
281  assertFalse( issuffix( filename , name_cfg ) );
282  filename = "yy-VACIO-bt0pp.cfg";
283  name_cfg = "vacio-bt0pp.cfg";
284  assertTrue( issuffix( filename , name_cfg ) );
285  }
286  {
287  const char *filename ,*name_cfg;
288  filename = "00-bt0pp.cfg";
289  name_cfg = "bt0pp.cfg";
290  assertTrue( issuffix( filename , name_cfg ) );
291  filename = "00-bt0pp.cfg xxx";
292  assertFalse( issuffix( filename , name_cfg ) );
293  }
294  assertFalse( issuffix("Xvacio-bt0pp.cfg","yy-VACIO-bt0pp.cfg" ) );
295  assertFalse( issuffix("Xvacio-bt0pp.cfg","zz-VACIO-bt0pp.cfg" ) );
296 
297  assertTrue( issuffix("yy-VACIO-bt0pp.cfg","vacio-bt0pp.cfg" ) );
298  assertTrue( issuffix("zz-VACIO-bt0pp.cfg","vacio-bt0pp.cfg" ) );
299 }
300 
301 /// \c test->str2uint().
303 {{ // test::str2uint()
304  assertTrue( 123 == str2uint("+123" ) );
305  assertTrue( 123 == str2uint("-123" ) );
306  assertTrue( 0 == str2uint("") );
307  assertTrue( 0 == str2uint("+-") );
308  assertTrue( 2012 == str2uint("2012.3.etc...") );
309 }}
310 }
311 
313 {{ // test::number_suffix()
314  assertTrue( 1==number_suffix("1") );
315  assertTrue( 1==number_suffix("grupo001") );
316  assertTrue( 123==number_suffix("g123") );
317  assertTrue( 123==number_suffix("-123") );
318  assertTrue( std::string::npos==number_suffix("") );
319  assertTrue( std::string::npos==number_suffix("132xx") );
320 }}
321  assertTrue( 0==number_suffix("000") );
322  assertTrue( 0==number_suffix("g0") );
323  assertTrue( 0==number_suffix("0") );
324  assertTrue( 0==number_suffix("-0") );
325  assertTrue( 1125==number_suffix("1125") );
326 }
327 
328 /// \c test->tostring().
330 {{ // test::tostring()
331  assertTrue( "001" == tostring(1, 3) );
332  assertTrue( "789" == tostring(123456789, 3) );
333  assertTrue( "" == tostring(123456789, 0) ); // 0==>""
334 }}
335  assertTrue( "1" == tostring(1, 1) );
336  assertTrue( "0" == tostring(0, 1) );
337  assertTrue( "000" == tostring(0, 3) );
338  assertTrue( "89" == tostring(123456789, 2) );
339  assertTrue( "" == tostring(123456789, 0) ); // 0==>""
340 }
341 
342 /// \c test->tostring_char().
344 {{ // test::tostring_char()
345  char touch[16]; assertTrue( !strcmp( "abc", "abc" ) );
346  tostring( 1, 3, touch ); assertTrue( !strcmp( "001", touch ) );
347  tostring(123456, 3, touch ); assertTrue( !strcmp( "456", touch ) );
348  tostring(123456, 0, touch ); assertTrue( !strcmp( "", touch ) );
349 }}
350  char touch[16];
351  tostring( 1, 1, touch ); assertTrue( !strcmp( "1", touch ) );
352  tostring( 0, 1, touch ); assertTrue( !strcmp( "0", touch ) );
353  tostring( 0, 3, touch ); assertTrue( !strcmp( "000", touch ) );
354  tostring(123456, 2, touch ); assertTrue( !strcmp( "56", touch ) );
355  tostring(123456, 0, touch ); assertTrue( !strcmp( "", touch ) ); // 0==>""
356 }
357 
358 /// \c test->is_email().
360 {{ // test::is_email()
361  assertFalse( is_email('@') );
362  const char* yo = "adolfo@di-mare.com";
363  std::string YO = yo;
364 
365  size_t prev = YO.find('@');
366  for ( size_t i=0; i<prev; ++i ) {
367  assertTrue( is_email( YO[i] ) );
368  }
369 
370  size_t post = prev+1;
371  for ( size_t i=post; i<YO.size(); ++i ) {
372  assertTrue( is_email( YO[i] ) );
373  }
374 }}
375 }
376 
377 /// \c test->tokens().
378 void test_tokens() {
379 {{ // test::tokens()
380  std::string ln;
381 
382  ln = " gsec =##-t|##-tpg (mejor peor); ;?-### ;etc." ;
383  // 1 2 34 5 6 7 8 910 11 12 13 14
384 #if 0
385  [gsec] [=] [##-t] [|] [##-tpg] [(] [mejor] [peor] [)] [;] [;] [?-###] [;] [etc.]
386  1 2 3 4 5 6 7 8 9 10 11 12 13 14
387 #endif
388 
389  std::list< std::string > LLL;
390  std::list< std::string >::const_iterator it;
391  tokens( ln, LLL , " =();|" );
392 
393  assertTrue( 14 == LLL.size() ); assertTrue( 12 != LLL.size() );
394  it = LLL.begin(); assertTrue( *it == "gsec" );
395  ++it; assertTrue( *it == "=" );
396  ++it; assertTrue( *it == "##-t" );
397 
398  it = LLL.begin(); for (int i=1; i<6; ++i,it++) { } assertTrue( *it == "(" );
399  it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == ";" );
400  it = LLL.begin(); for (int i=1; i<12; ++i,it++) { } assertTrue( *it == "?-###" );
401  { // puros blancos
402  std::list< std::string > LLL;
403  std::list< std::string >::const_iterator it;
404  std::string ln;
405  ln = " " ; tokens( ln, LLL , " ;" ); // puros blancos...
406  assertTrue( LLL.empty() );
407  ln = " ; etc "; tokens( ln, LLL , " ;" ); // puros blancos...
408  assertTrue( LLL.size()==2 && LLL.front()==";" );
409  }
410 }}
411 {
412  std::string ln;
413  ln = "gsec=##-t|##-tpg(mejor peor); ;?-###;etc." ;
414  // 12 34 56 7 8 910 11 1213 14
415 #if 0
416  [gsec] [=] [##-t] [|] [##-tpg] [(] [mejor] [peor] [)] [;] [;] [?-###] [;] [etc.]
417  1 2 3 4 5 6 7 8 9 10 11 12 13 14
418 #endif
419  std::list< std::string > LLL;
420  std::list< std::string >::const_iterator it;
421  tokens( ln, LLL , " =();|" );
422  #if 0
423  assertFalse( " *** Por que NO detecta '|' ni ')' ??? *** " );
424  for ( it=LLL.begin(); it!=LLL.end(); ++it) {
425  std::cout<< "[" << *it << "] ";
426  }
427  std::cout << std::endl;
428  #endif
429  assertTrue( 14 == LLL.size() ); assertTrue( 12 != LLL.size() );
430  it = LLL.begin(); for (int i=1; i<6; ++i,it++) { } assertTrue( *it == "(" );
431  it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == ";" );
432  it = LLL.begin(); for (int i=1; i<12; ++i,it++) { } assertTrue( *it == "?-###" );
433 }
434 
435 { // Más pruebas para tokens()
436  std::string ln;
437  // 0123 45 67 89x 11x 13x 15x 17x 19
438  ln = "2012.1.ci1101.g04.adolfo..dimare.examen.ex2.1-enunciado.htm" ;
439  std::list< std::string > LLL;
440  std::list< std::string >::const_iterator it;
441  tokens( ln, LLL , " ." );
442  #if 0
443  unsigned i=0;
444  for ( it=LLL.begin(); it!=LLL.end(); ++it,++i) {
445  std::cout<< i << "[" << *it << "] ";
446  }
447  std::cout << std::endl;
448  #endif
449  assertTrue( "htm" == LLL.back() );
450  it = LLL.begin(); for (int i=0; i<0; ++i,it++) { } assertTrue( *it == "2012" );
451  it = LLL.begin(); for (int i=0; i<1; ++i,it++) { } assertTrue( *it == "." );
452  it = LLL.begin(); for (int i=0; i<2; ++i,it++) { } assertTrue( *it == "1" );
453 
454  it = LLL.begin(); for (int i=0; i<8 ; ++i,it++) { } assertTrue( *it == "adolfo" );
455  it = LLL.begin(); for (int i=0; i<19 ; ++i,it++) { } assertTrue( *it == "htm" );
456 
457  it = LLL.begin(); for (int i=0; i<9 ; ++i,it++) { } assertTrue( *it == "." );
458  it = LLL.begin(); for (int i=0; i<10; ++i,it++) { } assertTrue( *it == "." );
459  assertTrue( "htm" == LLL.back() );
460  assertTrue( 20 == LLL.size() );
461 }
462 
463 { // Más pruebas para tokens()
464  std::string ln;
465  ln = " gsec =##-tarea|##-tpg (enunciado mejor peor) ;?-### ;?### ;etc.;" ;
466  std::list< std::string > LLL;
467  std::list< std::string >::const_iterator it;
468  tokens( ln, LLL , " =();|" );
469  assertTrue( ";" == LLL.back() );
470 }
471 
472 { // Más pruebas para tokens()
473 }
474 
475 { // Otras pruebas para tokens()
476  std::string ln;
477  std::list< std::string > LLL;
478  std::list< std::string >::const_iterator it;
479  const char* delimiters = " /";
480  ln = "SDF/Algor Design";
481  tokens( ln, LLL , delimiters );
482  it = LLL.begin();
483  assertTrue( *it == "SDF");
484  ++it; assertTrue( *it == "/");
485  ++it; assertTrue( *it == "Algor");
486  ++it; assertTrue( *it == "Design");
487  ++it; assertTrue( it == LLL.end() );
488 }
489 
490 { // Otras pruebas para tokens()
491  std::string ln;
492  std::list< std::string > LLL;
493  std::list< std::string >::const_iterator it;
494  { // 12345 6 7 8 9 0 1 2 3 4 5
495  ln = "()=;| ( ) = ; | ( ) = ; |";
496  LLL.clear(); tokens( ln, LLL , " =();|" );
497  assertTrue( LLL.size()==15 );
498  assertTrue( LLL.front()=="(" ); assertTrue( LLL.back()=="|" );
499  it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == "|" );
500 
501  ln = " "+ln;
502  LLL.clear(); tokens( ln, LLL , " =();|" );
503  assertTrue( LLL.size()==15 );
504  assertTrue( LLL.front()=="(" ); assertTrue( LLL.back()=="|" );
505  it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == "|" );
506 
507  ln = ln+" ";
508  LLL.clear(); tokens( ln, LLL , " =();|" );
509  assertTrue( LLL.size()==15 );
510  assertTrue( LLL.front()=="(" ); assertTrue( LLL.back()=="|" );
511  it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == "|" );
512  }
513  { // 123456789 012 3 45 67 89 01 23 45 67 89
514  ln = "(.).=.;.|..(..)..=..;..|..(..)..=..;..|";
515  LLL.clear(); tokens( ln, LLL , " =();|" );
516  assertTrue( LLL.size()==29 );
517  assertTrue( LLL.front()=="(" ); assertTrue( LLL.back()=="|" );
518  it = LLL.begin(); for (int i=1; i<15; ++i,it++) { } assertTrue( *it == "=" );
519 
520  ln = ".."+ln;
521  LLL.clear(); tokens( ln, LLL , " =();|" );
522  assertTrue( LLL.size()==30 );
523  assertTrue( LLL.front()==".." ); assertTrue( LLL.back()=="|" );
524  it = LLL.begin(); for (int i=1; i<16; ++i,it++) { } assertTrue( *it == "=" );
525 
526  ln = ln+".";
527  LLL.clear(); tokens( ln, LLL , " =();|" );
528  assertTrue( LLL.size()==31 );
529  assertTrue( LLL.front()==".." ); assertTrue( LLL.back()=="." );
530  it = LLL.begin(); for (int i=1; i<16; ++i,it++) { } assertTrue( *it == "=" );
531  }
532  { // ln.replace(".",":::");
533  ln = "(:::):::=:::;:::|::::::(::::::)::::::=::::::;::::::|::::::(::::::)::::::=::::::;::::::|";
534  LLL.clear(); tokens( ln, LLL , " =();|" );
535  assertTrue( LLL.size()==29 );
536  assertTrue( LLL.front()=="(" ); assertTrue( LLL.back()=="|" );
537  it = LLL.begin(); for (int i=1; i<15; ++i,it++) { } assertTrue( *it == "=" );
538 
539  ln = "---"+ln;
540  LLL.clear(); tokens( ln, LLL , " =();|" );
541  assertTrue( LLL.size()==30 );
542  assertTrue( LLL.front()=="---" ); assertTrue( LLL.back()=="|" );
543  it = LLL.begin(); for (int i=1; i<16; ++i,it++) { } assertTrue( *it == "=" );
544 
545  ln = ln+"---";
546  LLL.clear(); tokens( ln, LLL , " =();|" );
547  assertTrue( LLL.size()==31 );
548  assertTrue( LLL.front()=="---" ); assertTrue( LLL.back()=="---" );
549  it = LLL.begin(); for (int i=1; i<16; ++i,it++) { } assertTrue( *it == "=" );
550  }
551  {
552  ln = "";
553  LLL.clear(); tokens( ln, LLL , " =();|" );
554  assertTrue( LLL.size()==0 );
555  ln = ";";
556  LLL.clear(); tokens( ln, LLL , " =();|" );
557  assertTrue( LLL.size()==1 );
558  ln = " ; ";
559  LLL.clear(); tokens( ln, LLL , " =();|" );
560  assertTrue( LLL.size()==1 );
561  ln = "; =";
562  LLL.clear(); tokens( ln, LLL , " =();|" );
563  assertTrue( LLL.size()==2 );
564  assertTrue( LLL.front()==";" ); assertTrue( LLL.back()=="=" );
565  }
566 } // Otras pruebas para tokens()
567 } // test_tokens()
568 
569 // strins() from Software Artistry @ 1988
570 /* inserts "substr" into "num" at index "n" */
571 char *strins(const char *substr, char *num, int n)
572 {
573  memmove(&num[n+strlen(substr)],&num[n],strlen(&num[n])+1);
574  memcpy(&num[n],substr,strlen(substr));
575  return num;
576 } /* num ins */
577 
578 
579 // strdel() from Software Artistry @ 1988
580 char *strdel3_slow(char *str, int start, int len)
581 {
582  int slen;
583 
584  slen = strlen(str);
585  if (start >= slen) return str;
586  if (start+len > slen) len = slen-start;
587  memmove(&str[start],&str[start+len],strlen(&str[start+len])+1);
588  return str;
589 
590 } /* str del */
591 
592 // http://herongyang.com/Computer-History/C-Sample-Program-for-Array-and-Pointer.html
593 /* strdel_yang(str,n,l) function
594 * Deleting l characters at position n from str
595 * ptr: string pointer
596 * n: >=1, 1 means character one
597 * l: >=0, 0 means to the end
598 */
599 char* strdel_yang( char *str, int n, int l )
600 {
601  char *p;
602  char *b;
603  int m = 0;
604 
605  p = str;
606  while (*p++) m++; /* get the length, not including \0 */
607  if (m<n) return str;
608 
609  p = str;
610  while ((n--)>1) p++; /* get to the right position */
611  *p = '\0'; /* good for l=0 */
612 
613  if (l>0 && n+l<=m) {
614  b = p;
615  while ((l--)>0) p++; /* skip l characters */
616  while ((*b++ = *p++)); /* copy the rest, including \0 */
617  }
618  return str;
619 }
620 
621 /// test->strdel()
622 void test_strdel() {
623 {{ // test::strdel()
624  char str[] = "12.==.789";
625  strdel( str, 2,4 );
626  assertTrue( 0==strcmp( str, "12789" ) );
627 
628  strdel( str, strlen(str), 1 );
629  assertTrue( 0==strcmp( str, "12789" ) );
630 
631  strdel( str, size_t(911), 1 );
632  assertTrue( 0==strcmp( str, "12789" ) );
633 }}
634  const char base[] = "123456789.123456789.";
635  char bfast[ sizeof(base) ];
636  char bslow[ sizeof(base) ];
637  char byang[ sizeof(base) ];
638  assertTrue( 20==strlen(base) && 21<=sizeof(base) );
639 
640  for (size_t len=0; len<=strlen(base); ++len ) {
641  for (size_t from=0; from<=strlen(base); ++from ) {
642  strcpy(bfast, base); strcpy( bslow, base );
643  assertTrue( bfast == strdel( bfast, from, len ) );
644  assertTrue( bslow == strdel3_slow( bslow, from, len ) );
645  if ( 0==strcmp(bfast,bslow) ) { /* OK */ }
646  else {
647  std::cout << " ";
648  std::cout << "(len,from)==(" << len << ',' << from << ")\n";
649  assertTrue( 0==strcmp(bfast,bslow) );
650  }
651  strcpy(bfast, base); strcpy( byang, base );
652  assertTrue( bfast == strdel( bfast, from, len ) );
653  assertTrue( byang == strdel( byang, from, len ) );
654  if ( 0==strcmp(bfast,byang) ) { /* OK */ }
655  else {
656  std::cout << " ";
657  std::cout << "(len,from)==(" << len << ',' << from << ")\n";
658  std::cout << bfast << std::endl;
659  std::cout << byang << std::endl;
660  assertTrue( 0==strcmp(byang,bfast) );
661  }
662  }
663  }
664 }
665 
666 /// test->strdel2()
667 void test_strdel2() {
668 {{ // test::strdel2()
669  char str[] = "12.==.789";
670  strdel2( str+2,4 );
671  assertTrue( 0==strcmp( str, "12789" ) );
672 
673  strdel2( str+strlen(str), 1 );
674  assertTrue( 0==strcmp( str, "12789" ) );
675 
676  strdel2( str+size_t(911), 1 );
677  assertTrue( 0==strcmp( str, "12789" ) );
678 }}
679  const char base[] = "123456789.123456789.";
680  char bfast[ sizeof(base) ];
681  char bslow[ sizeof(base) ];
682  assertTrue( 20==strlen(base) && 21<=sizeof(base) );
683 
684  for (size_t len=0; len<=strlen(base); ++len ) {
685  for (size_t from=0; from<=strlen(base); ++from ) {
686  strcpy(bfast, base); strcpy(bslow, base);
687  strdel2( bfast +from, len );
688  strdel3_slow( bslow, from, len );
689  if ( 0==strcmp(bfast,bslow) ) { /* OK */ }
690  else {
691  std::cout << " ";
692  std::cout << "(len,from)==(" << len << ',' << from << ")\n";
693  assertTrue( 0==strcmp(bfast,bslow) );
694  }
695  }
696  }
697 }
698 
699 
700 // EOF: string_tool_test.cpp
test_strdel2
void test_strdel2()
test->strdel2()
Definition: string_tool_test.cpp:667
test_trimQuote
void test_trimQuote()
test->trimQuote().
Definition: string_tool_test.cpp:48
exec_TEST
#define exec_TEST(t)
strdel_yang
char * strdel_yang(char *str, int n, int l)
Definition: string_tool_test.cpp:599
str2uint
unsigned str2uint(const std::string &str)
Convierte 's' un número entero sin signo.
Definition: string_tool.cpp:274
issuffix
bool issuffix(const char *str, const char *suffix)
Verifica si suffix es un sufijo de str.
Definition: string_tool.cpp:172
alphaCount
unsigned alphaCount(const std::string &str, unsigned i)
Retorna la cantidad de letras consecutivas a partir de 'str[i]'.
Definition: string_tool.cpp:229
main
int main(int argc, const char *argv[])
Programa de prueba para las rutinas de string_tool.h.
Definition: string_tool_test.cpp:17
test_isalpha
void test_isalpha()
test->isalpha().
Definition: string_tool_test.cpp:152
test_str2uint
void test_str2uint()
test->str2uint().
Definition: string_tool_test.cpp:302
is_blank
bool is_blank(const std::string &word)
Retorna true si todos las caracteres en word son blancos.
Definition: string_tool.cpp:157
A_TILDE_64
#define A_TILDE_64
Definition: string_tool.h:53
test_issuffix
void test_issuffix()
test->issuffix().
Definition: string_tool_test.cpp:259
test_roundUp
void test_roundUp()
test->roundUp().
Definition: string_tool_test.cpp:221
string_tool.h
Rutinas de hileras que complementan <string> y <cstring>.
roundUp
#define roundUp(n, b)
test_removeAccented
void test_removeAccented()
test->removeAccented() && test->removeAccented::string()
Definition: string_tool_test.cpp:80
test_tostring
void test_tostring()
test->tostring().
Definition: string_tool_test.cpp:329
assertTrue
#define assertTrue(cond)
(cond ? () : cout << "cond" )
Definition: uUnit.h:93
number_suffix
unsigned number_suffix(const std::string &str)
Retorna el número que está al final de str.
Definition: string_tool.cpp:248
strdel2
char * strdel2(char *str, size_t len)
Deletes 'len' chars from the start of 'str'.
Definition: string_tool.cpp:407
psz_string.h
Empaque (char*) para usar hileras C de forma segura en C++.
tostring
std::string tostring(unsigned N, unsigned width)
Retorna la hilera que contiene los últimos 'width' dígitos de 'N'.
Definition: string_tool.cpp:304
test_is_email
void test_is_email()
test->is_email().
Definition: string_tool_test.cpp:359
tolower
void tolower(std::string &line)
Convierte a minúscula todas las letras de line.
Definition: string_tool.cpp:19
assertFalse
#define assertFalse(cond)
(!(cond) ? () : cout << "!" << (cond)" )
Definition: uUnit.h:95
test_is_blank
void test_is_blank()
test->is_blank().
Definition: string_tool_test.cpp:163
removeAccented
char removeAccented(char ch)
Retorna la letra sin acento que corresponde a ch.
Definition: string_tool.cpp:39
test_tostring_char
void test_tostring_char()
test->tostring_char().
Definition: string_tool_test.cpp:343
test_number_suffix
void test_number_suffix()
Definition: string_tool_test.cpp:312
uUnit.h
[u]Micro module for [Unit] program testing.
isalpha
bool isalpha(const std::string &line)
Regresa true si todos los caracteres de line son alfabéticos.
Definition: string_tool.cpp:140
test_tolower
void test_tolower()
test->tolower().
Definition: string_tool_test.cpp:66
test_alphaCount
void test_alphaCount()
test->alphaCount().
Definition: string_tool_test.cpp:201
A_SIMPLE_64
#define A_SIMPLE_64
Definition: string_tool.h:55
test_strdel
void test_strdel()
test->strdel()
Definition: string_tool_test.cpp:622
strdel3_slow
char * strdel3_slow(char *str, int start, int len)
Definition: string_tool_test.cpp:580
is_email
bool is_email(char ch)
Retorna true si ch es un caracter son letras que pueden aparecer en una dirección email.
Definition: string_tool.h:48
test_tokens
void test_tokens()
test->tokens().
Definition: string_tool_test.cpp:378
strins
char * strins(const char *substr, char *num, int n)
Definition: string_tool_test.cpp:571
test_digitCount
void test_digitCount()
test->digitCount().
Definition: string_tool_test.cpp:171
tokens
void tokens(const std::string &renglon, std::list< std::string > &L, const char *delimiters)
Separa las palabras de 'renglon' y las deja en la lista 'L'.
Definition: string_tool.cpp:348
strdel
char * strdel(char *str, size_t from, size_t len)
Deletes 'len' chars from 'str' starting at index 'from'.
Definition: string_tool.cpp:384
digitCount
unsigned digitCount(const std::string &str, unsigned i)
Retorna la cantidad de dígitos consecutivos a partir de str[i].
Definition: string_tool.cpp:209
trimQuote
void trimQuote(std::string &word)
Elimina comillas ['] ["] del principio o final de 'word'.
Definition: string_tool.cpp:121