-: 0:Source:test_CSV.cpp -: 0:Graph:test_CSV.gcno -: 0:Data:test_CSV.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:// test_CSV.cpp (C) 2008 adolfo@di-mare.com -: 2: -: 3:/** \file test_CSV.cpp -: 4: \brief Test ==> \c CSV. -: 5: -: 6: \author Adolfo Di Mare -: 7: \date 2008. -: 8: -: 9: \author A11788 Jose David Hine Gomez -: 10: \author A24444 Cristopher Rojas Salas -: 11: \author A56254 Carlos Jose Watson Morua -: 12: \author A61166 Adrian Ignacio Campos Oviedo -: 13: \author A61196 Tabare Capitan Jimenez -: 14: \author A71569 Sergio Esteban Castillo Segura -: 15: \author A73869 Pablo Ignacio Matamoros Sanchez -: 16: \author A74668 Erick Abdiel Orestes Chaves -: 17: \author A76944 Alexander Vega Sanou -: 18:*/ -: 19: -: 20:#include "BUnit.h" -: 21:#include "CSV_line.h" -: 22: -: 23:#define C_CSV // truco para que compile "CSV.c" -: 24: -: 25:/// Test ==> \c CSV. -: 26:class test_CSV : public TestCase { -: 27:public: function _ZN8test_CSVC1Ev called 1 returned 100% blocks executed 100% 2: 28: test_CSV() {} ///< Constructor. -: 29: bool run(); ///< Execute. -: 30:private: -: 31: void test_setData(); -: 32: void test_op_data(); -: 33: void test_trim(); -: 34: void test_trimCSV(); -: 35: void test_chop(); -: 36: void test_istringstream(); -: 37: void test_setQuotedCSV(); -: 38: void test_getNextCSV(); -: 39:// void test_rebuildDquote(); -: 40: #ifdef C_CSV -: 41: void test_trim_C(); -: 42: void test_chop_C(); -: 43: #endif -: 44:}; -: 45: -: 46:/// Efectúa las pruebas. function _ZN8test_CSV3runEv called 1 returned 100% blocks executed 100% 1: 47:bool test_CSV::run() { 1: 48: test_setData(); 1: 49: test_op_data(); 1: 50: test_trim(); 1: 51: test_trimCSV(); 1: 52: test_chop(); 1: 53: test_istringstream(); 1: 54: test_setQuotedCSV(); 1: 55: test_getNextCSV(); -: 56:// test_rebuildDquote(); -: 57: #ifdef C_CSV 1: 58: test_trim_C(); 1: 59: test_chop_C(); -: 60: #endif 1: 61: return wasSuccessful(); -: 62:} -: 63: -: 64:#include // cout -: 65:#include // basic_istringstream<> -: 66: -: 67:/// Cantidad de elementos del vector \c V[]. -: 68:/// - DIM() no se puede implementar con plantillas... -: 69:#define DIM(V) (sizeof(V) / sizeof(*V)) // *V == *(V+0) == V[0] -: 70: -: 71:/// Test ==> \c setData(). function _ZN8test_CSV12test_setDataEv called 1 returned 100% blocks executed 77% 1: 72:void test_CSV::test_setData() { -: 73: {{ // test::setData() function _ZN8CSV_lineD1Ev called 19 returned 100% blocks executed 100% 39: 74: CSV_line csv; // 012 3 ...4. .......5. -: 75: // // [,,a,b,"c,d","e f ""g"\r\n] 1: 76: csv.setData( ",,a,b,\"c,d\",\"e f \"\"g\"\r\n" ); 1: 77: assertTrue( csv[0] == "" ); // [] 1: 78: assertTrue( csv[1] == "" ); // [] 1: 79: assertTrue( csv[2] == "a" ); // [a] 1: 80: assertTrue( csv[3] == "b" ); // [b] 1: 81: assertTrue( csv[4] == "c,d" ); // [c,d] 1: 82: assertTrue( csv[5] == "e f \"g" ); // [e f "g] 1: 83: assertTrue( csv.size() == 6 ); -: 84: }} 1: 85: if (true) { 1: 86: char* t[13]; std::string str; -: 87: // 0 1 .2 ..3. ..4 ..5.. 6 ..7 ..8.. 9. ...10. ...11. .12 -: 88: // ",",""," ", "", "" , " " ,6, 7, 8 ,9"," \r, ",",11,","x\r\n" 1: 89: t[0] = "\",\""; t[ 6] = "6"; 1: 90: t[1] = "\"\""; t[ 7] = " 7"; 1: 91: t[2] = "\" \""; t[ 8] = " 8 "; 1: 92: t[3] = " \"\""; t[ 9] = "9\""; 1: 93: t[4] = " \"\" "; t[10] = "\" \r, \""; 1: 94: t[5] = " \" \" "; t[11] = "\",11,\""; 1: 95: t[12] = "x\r\n"; 13: 96: for (int i=0; i<12; ++i) { 12: 97: str += t[i]; 12: 98: str += ','; -: 99: } 1: 100: str += t[12]; -: 101: 1: 102: CSV_line csv; csv.setData( str.data() , str.length() ); 1: 103: assertTrue( csv.size() == 13 ); -: 104: 1: 105: assertTrue( csv[ 0] == "," ); 1: 106: assertTrue( csv[ 1] == "" ); 1: 107: assertTrue( csv[ 2] == " " ); 8: 108: for (int i=3; i<=9; ++i) { 7: 109: assertTrue_Msg( "t["+toString(i)+"] == ["+t[i]+"] ==> ["+csv[i]+']', t[i] == csv[i] ); -: 110: } 1: 111: assertTrue( csv[10] == " \r, " ); 1: 112: assertTrue( csv[11] == ",11," ); 1: 113: assertTrue( csv[12] != "x\r\n" ); assertTrue( csv[12] == "x" ); -: 114: } -: 115: { using namespace std; 1: 116: string s; s += '"'; s += '"'; // s contiene 2 comillas: "" 1: 117: assertTrue( s == string("\"\"") ); // así es cómo se hace 1: 118: assertTrue( "" == string("""") ); // """" ==> "" "" ==> "" (concatenación de nulas) 1: 119: assertTrue( string("\"\"") != """" ); // """" es la hilera nula -: 120: // std::cout << '[' << """" << ']' << std::endl; // graba [] -: 121: } 1: 122: if (false) { // código viejo usado para ver qué está pasando 1: 123: char* t[13]; std::string str; CSV_line csv; // left-over code 1: 124: std::cout << std::endl << '[' << str << ']' << std::endl; 1: 125: std::cout << "csv.size() == " << unsigned(csv.size()) << std::endl; 2: 126: for (size_t i=0; i [" << s << ']' << std::endl; -: 133: } 2: 134: for (int i=0; i<9; ++i) { 1: 135: assertTrue_Msg( "t["+toString(i)+"]["+t[i]+"] == ["+csv[i]+']', t[i] == csv[i] ); -: 136: } 1: 137: { int i=0; std::cout << __LINE__ << " csv[" << i << "] == [" << csv[i] << ']' << std::endl; } -: 138: } -: 139: { // Las comillas de Pipino Cuevas 1: 140: std::string res; 1: 141: setQuotedCSV(res,"2"); assertTrue(res == "2" ); 1: 142: setQuotedCSV(res,"4,5"); assertTrue(res == "\"4,5\"" ); -: 143: 1: 144: setQuotedCSV( res, "," ); assertTrue(res == "\",\""); // [","] 1: 145: setQuotedCSV( res, "2" ); assertTrue(res == "2"); // [2] 1: 146: setQuotedCSV( res, "" ); assertTrue(res == ""); // [] 1: 147: setQuotedCSV( res, "4,5" ); assertTrue(res == "\"4,5\""); // ["4,5"] -: 148: } -: 149: { // [," ... "" "3x,] 1: 150: CSV_line csv( ",\" ... \"\" \"3x,list" ); 1: 151: assertTrue( csv.size() == 3 ); 1: 152: assertTrue( csv[0] == "" ); 1: 153: assertTrue( csv[1] == "\" ... \"\" \"3x" ); 1: 154: assertTrue( csv[2] == "list" ); -: 155: } -: 156: { 1: 157: CSV_line csv( ",\".\r\n\"" ); 1: 158: assertTrue( csv.size() == 2 ); 1: 159: assertTrue( csv[0] == "" ); 1: 160: assertTrue( csv[1] == ".\r\n" ); -: 161: } -: 162: { // 0," ,,\r\n .. ",",,\r\n"",",123\r\n -: 163: // 0 ...........1. .........2. ..3\r\n 1: 164: CSV_line csv; char * t[4]; std::string str; 1: 165: t[0] = "0"; // [0] 1: 166: t[1] = "\" ,,\r\n .. \""; // [" ,,\r\n .. "]; 1: 167: t[2] = "\",,\r\n\"\",\""; // [",,\r\n"","] ==> [,,\r\n",] 1: 168: t[3] = "123\r\n"; // [123] 4: 169: for (int i=0; i<=2; ++i) { 3: 170: str += t[i]; 3: 171: str += ','; -: 172: } 1: 173: str += t[3]; -: 174: 1: 175: csv.setData( str.data() , str.length() ); 1: 176: assertTrue( csv.size() == 4 ); assertTrue( csv.size() != 5 ); 1: 177: assertTrue( csv[0] == "0" ); 1: 178: assertTrue( csv[1] == " ,,\r\n .. " ); 1: 179: assertTrue( csv[2] == ",,\r\n\"," ); // ==> [,,\r\n\",] 1: 180: assertTrue( csv[3] == "123" ); 1: 181: assertTrue( csv[3] != "123\r\n" ); assertTrue( csv[3] != "123\r" ); -: 182: } -: 183: { 1: 184: CSV_line csv( "123\r\n" ); // [123\r\n ] 1: 185: assertTrue( csv[0] == "123" ); 1: 186: assertTrue( csv[0] != "123\r\n" ); assertTrue( csv[0] != "123\r" ); -: 187: } -: 188: { 1: 189: CSV_line csv( ", \" 3rd\" \r\n" ); // [, " 3rd" \r\n] 1: 190: assertTrue( csv[0] == "" ); 1: 191: assertTrue( csv[1] == " \" 3rd\" " ); // [ " 3rd" ] 1: 192: std::string copy = csv[1]; 1: 193: trimCSV( copy ); 1: 194: assertTrue( copy == " 3rd" ); // [3rd] -: 195: } 1: 196: if (false) { // http://www.di-mare.com/adolfo/cursos/2008-1/p2-ea-3.htm 1: 197: const char * in[11]; const char * out[11]; 1: 198: in[ 0] = "\"zero\""; out[ 0] = "zero"; 1: 199: in[ 1] = ", \"if \"\" 1\" "; out[ 1] = " \"if \"\" 1\" "; 1: 200: in[ 2] = ", "; out[ 2] = " "; 1: 201: in[ 3] = ", \" 3xt\" \r\n"; out[ 3] = " \" 3xt\" "; 1: 202: in[ 4] = ",\"png\"\"\n\"\"\n"; out[ 4] = "png\"\n\""; 1: 203: in[ 5] = ",12345"; out[ 5] = "12345"; 1: 204: in[ 6] = ",\"\"\r\n"; out[ 6] = "\"\""; 1: 205: in[ 7] = ",12\r666\r\n"; out[ 7] = "12\r666"; 1: 206: in[ 8] = ",12\r\n"; out[ 8] = "12"; 1: 207: in[ 9] = ",\"con blanco\""; out[ 9] = "con blanco"; 1: 208: in[10] = ",\"con,coma\""; out[10] = "con,coma"; -: 209: 1: 210: std::string str; 2: 211: for (int i=0; i<=10; ++i) { 1: 212: str += in[i]; -: 213: } -: 214: -: 215: { 1: 216: std::vector VEC; 1: 217: VEC.clear(); // 1: 218: std::string csv; 1: 219: bool eol_CIN = false; // stop when the end of line is reached 1: 220: std::istringstream ist( str , std::ios::binary ); 1: 221: while ( /* ! eol_CIN && */ ! ist.fail() ) { // ! ist.eof() pitfall! 1: 222: eol_CIN = getNextCSV( csv, ist ); 1: 223: VEC.push_back( csv ); -: 224: } -: 225: // Using std::ios::binary ensures that no CR+LF chars are discarded 2: 226: for (int i=0; i<=10; ++i) { 1: 227: assertTrue_Msg( std::string()+in[i]+" ==> "+out[i] , VEC[i]==out[i] ); -: 228: } -: 229: } 1: 230: return; -: 231: } -: 232: { // A11788 1: 233: CSV_line csv( "\"\"\"n\"" ); // ["""n"] 1: 234: assertTrue( csv[0] == "\"n" ); // ["n] 1: 235: assertTrue( csv.size() == 1 ); -: 236: } -: 237: { // A24444-A71569 1: 238: const int N_COUT = 0; // Cantidad de valores de csv[] grabados en cout 1: 239: std::string s, str; -: 240: // 0 1 2 3 4 5 6 -: 241: // [,"\n","pa""pa","ce\nro", "el "" 1" , ,\n] 1: 242: str = ",\"\n\",\"pa\"\"pa\",\"ce\nro\", \"el \"\" 1\" , ,\n"; 1: 243: CSV_line csv; csv.setData( str.data() ); { 1: 244: assertTrue( csv[0] == "" ); 1: 245: assertTrue( csv[1] == "\n" ); 1: 246: assertTrue( csv[2] == "pa\"pa" ); 1: 247: assertTrue( csv[3] == "ce\nro" ); 1: 248: assertTrue( csv[4] == " \"el \"\" 1\" " ); 1: 249: assertTrue( csv[5] == " " ); 1: 250: assertTrue( csv[6] == "" ); 1: 251: assertTrue( csv.size() == 7 ); 2: 252: for ( int i=0; i CSV_line.operator[](). function _ZN8test_CSV12test_op_dataEv called 1 returned 100% blocks executed 88% 1: 348:void test_CSV::test_op_data() { -: 349: // A73869-A74668 -: 350: {{ // test::op_data() 1: 351: CSV_line csv ("a,b,c,d,e,f,g"); { 1: 352: assertTrue(csv[0] == "a"); 1: 353: assertTrue(csv[1] == "b"); 1: 354: assertTrue(csv[2] == "c"); 1: 355: assertTrue(csv[3] == "d"); 1: 356: assertTrue(csv[4] == "e"); 1: 357: assertTrue(csv[5] == "f"); 1: 358: assertTrue( csv.size() == 7 ); -: 359: } -: 360: }} -: 361: { // A73869-A74668 1: 362: CSV_line csv2 ( "poll, m, ts, 08" ); { 1: 363: assertTrue(csv2[0] == "poll"); 1: 364: assertTrue(csv2[1] == " m"); 1: 365: assertTrue(csv2[2] == " ts"); 1: 366: assertTrue(csv2[3] == " 08"); 1: 367: assertTrue(csv2.size() == 4 ); -: 368: } -: 369: 1: 370: CSV_line csv3 ("fsd,ah,que,pi,som,b,aaana"); { 1: 371: assertTrue(csv3[0] == "fsd"); 1: 372: assertTrue(csv3[1] == "ah"); 1: 373: assertTrue(csv3[2] == "que"); 1: 374: assertTrue(csv3[3] == "pi"); 1: 375: assertTrue(csv3[4] == "som"); 1: 376: assertTrue(csv3[5] == "b"); 1: 377: assertTrue( csv3.size() == 7 ); -: 378: } -: 379: } -: 380:} -: 381: -: 382:/// Test ==> \c setQuotedCSV(). function _ZN8test_CSV17test_setQuotedCSVEv called 1 returned 100% blocks executed 69% 1: 383:void test_CSV::test_setQuotedCSV() { -: 384: // A73869-A74668 -: 385: {{ // test::setQuotedCSV() 1: 386: std::string res; 1: 387: setQuotedCSV( res, "," ); assertTrue( res == "\",\"" ); // [","] 1: 388: setQuotedCSV( res, "2" ); assertTrue( res == "2" ); // [2] 1: 389: setQuotedCSV( res, "" ); assertTrue( res == "" ); // [] 1: 390: setQuotedCSV( res, "4,5" ); assertTrue( res == "\"4,5\"" ); // ["4,5"] 1: 391: setQuotedCSV( res, "K\"" ); assertTrue( res == "\"K\"\"\"" ); // ["K"""] 1: 392: setQuotedCSV( res, "\r\n" ); assertTrue( res == "\"\r\n\"" ); // ["\r\n"] -: 393: }} -: 394: { // A24444-A71569 1: 395: std::string dest, str; 1: 396: str = "valor1"; setQuotedCSV(dest, str); 1: 397: assertTrue( dest == "valor1" ); 1: 398: str = "valor 1"; setQuotedCSV(dest, str); 1: 399: assertTrue( dest == "\"valor 1\"" ); 1: 400: str = "valor,1"; setQuotedCSV(dest, str); 1: 401: assertTrue( dest == "\"valor,1\"" ); 1: 402: str = "valor\"1"; setQuotedCSV(dest, str); 1: 403: assertTrue( dest == "\"valor\"\"1\"" ); 1: 404: str = "valor,\"1\""; setQuotedCSV(dest, str); 1: 405: assertTrue( dest == "\"valor,\"\"1\"\"\"" ); -: 406: } -: 407: { // A56254 1: 408: std::string res; 1: 409: setQuotedCSV( res, "" ); assertTrue( res == "" ); 1: 410: setQuotedCSV( res, "2" ); assertTrue( res == "2" ); 1: 411: setQuotedCSV( res, "4,5" ); assertTrue( res == "\"4,5\"" ); 1: 412: setQuotedCSV( res, "& _ ()" ); assertTrue( res == "\"& _ ()\"" ); -: 413: } -: 414: { // A61166 1: 415: std::string res, str; 1: 416: res = str = "1 2 3 4 5 6"; { 1: 417: assertTrue ( res == "1 2 3 4 5 6" ); 1: 418: setQuotedCSV (res, str); 1: 419: assertTrue ( res != str ); 1: 420: assertTrue ( res == "\"1 2 3 4 5 6\"" ); -: 421: } 1: 422: res = str = "1,2,3,4,5,6"; { 1: 423: assertTrue ( res == "1,2,3,4,5,6" ); 1: 424: setQuotedCSV (res, str); 1: 425: assertTrue ( res != str ); 1: 426: assertTrue ( res == "\"1,2,3,4,5,6\"" ); -: 427: } 1: 428: res = str = "123\"456"; { 1: 429: assertTrue ( res == "123\"456" ); 1: 430: setQuotedCSV (res, str); 1: 431: assertTrue ( res != str ); 1: 432: assertTrue ( res == "\"123\"\"456\"" ); -: 433: } 1: 434: res = str = "123456"; { 1: 435: assertTrue ( res == "123456" ); 1: 436: setQuotedCSV (res, str); 1: 437: assertTrue ( res == str ); 1: 438: assertTrue ( res == "123456" ); -: 439: } 1: 440: res = str = "\""; { 1: 441: assertTrue ( res == "\"" ); 1: 442: setQuotedCSV (res, str); 1: 443: assertTrue ( res != str ); 1: 444: assertTrue ( res == "\"\"\"\"" ); -: 445: } -: 446: } -: 447:} -: 448: -: 449:/// Test ==> \c getNextCSV(). function _ZN8test_CSV15test_getNextCSVEv called 1 returned 100% blocks executed 59% 1: 450:void test_CSV::test_getNextCSV() { -: 451: { // A73869-A74668 1: 452: std::string prueba1 = "PSL,ITE,AMCY"; 1: 453: std::istringstream ist(prueba1); 1: 454: bool getn1 = getNextCSV(prueba1,ist); 1: 455: assertFalse (getn1); 1: 456: assertTrue (prueba1 == "PSL"); -: 457: 1: 458: if (false) { -: 459: { 1: 460: int error; //para inducir un error en CIN 1: 461: std::string res; 1: 462: std::cout << "Digite una letra para provocar un error"; 1: 463: std::cin >> error; 1: 464: bool fallo = getNextCSV(res,std::cin); 1: 465: assertTrue(!fallo); -: 466: } -: 467: { 1: 468: int error1; //para inducir un error en CIN 1: 469: std::string res1; 1: 470: std::cin >> error1; 1: 471: bool fallo1 = getNextCSV(res1,std::cin); 1: 472: assertTrue(!fallo1); -: 473: } -: 474: { 1: 475: int error2; //para inducir un error en CIN 1: 476: std::string res2; 1: 477: std::cin >> error2; 1: 478: bool fallo2 = getNextCSV(res2,std::cin); 1: 479: assertTrue(!fallo2); -: 480: } -: 481: { 1: 482: std::string prueba2 = "PSL\n"; 1: 483: std::istringstream ist2 (prueba2); 1: 484: bool getn1 = getNextCSV(prueba2,ist2); 1: 485: assertTrue (getn1); 1: 486: assertTrue (prueba2 == "PSL"); -: 487: } -: 488: } -: 489: } -: 490: { // A56254 1: 491: std::string csv; { 1: 492: std::string str1 = "a"; 1: 493: std::istringstream ist1(str1); 1: 494: getNextCSV( csv, ist1 ); assertTrue( csv == "a" ); -: 495: 1: 496: std::string str2 = "\"2,3\""; 1: 497: std::istringstream ist2(str2); 1: 498: getNextCSV( csv, ist2 ); assertTrue( csv == "2,3" ); -: 499: 1: 500: std::string str3 = "\n"; 1: 501: std::istringstream ist3(str3); 1: 502: getNextCSV( csv, ist3 ); assertTrue( csv == "" ); -: 503: } -: 504: } -: 505:} -: 506: -: 507:/// Test ==> \c trim(). function _ZN8test_CSV9test_trimEv called 1 returned 100% blocks executed 91% 1: 508:void test_CSV::test_trim() { -: 509: {{ // test::trim() 1: 510: std::string str; 1: 511: str = " a b "; trim(str); assertTrue( str == "a b" ); 1: 512: str = " a\nb "; trim(str); assertTrue( str == "a\nb" ); 1: 513: str = ""; trim(str); assertTrue( str == "" ); 1: 514: str = "\r\t\n "; trim(str); assertTrue( str == "" ); 1: 515: str = " a b "; trim(str); assertTrue( str == "a b" ); 1: 516: str = " ab " ; trim(str); assertTrue( str == "ab" ); -: 517: }} -: 518:} -: 519: -: 520:/// Test ==> \c trimCSV(). function _ZN8test_CSV12test_trimCSVEv called 1 returned 100% blocks executed 86% 1: 521:void test_CSV::test_trimCSV() { -: 522: {{ // test::trimCSV() 1: 523: CSV_line csv("\"zero\", \"if \"\" 1\" , , \" 3xt\" \r\f"); std::string s; 1: 524: s=csv[0]; assertTrue( s == "zero" ); trimCSV(s); assertTrue( s == "zero" ); 1: 525: s=csv[1]; assertTrue( s == " \"if \"\" 1\" " ); trimCSV(s); assertTrue( s == "if \" 1" ); 1: 526: s=csv[2]; assertTrue( s == " " ); trimCSV(s); assertTrue( s == "" ); 1: 527: s=csv[3]; assertTrue( s == " \" 3xt\" \r\f" ); trimCSV(s); assertTrue( s == " 3xt" ); -: 528: }} -: 529: {{ -: 530: // ["zero", "if "" 1" , , " 3xt" \r\n] 1: 531: CSV_line csv ("\"zero\", \"if \"\" 1\" , , \" 3xt\" \r\n"); 1: 532: std::vector cpy = csv; 1: 533: assertTrue( csv[0] == "zero" ); // [zero] 1: 534: assertTrue( csv[1] == " \"if \"\" 1\" " ); // [ "if "" 1" ] 1: 535: assertTrue( csv[2] == " " ); // [ ] 1: 536: assertTrue( csv[3] == " \" 3xt\" " ); // [ " 3xt" ] -: 537: 1: 538: trimCSV( cpy[0] ); assertTrue( cpy[0] == "zero" ); // [zero] 1: 539: trimCSV( cpy[1] ); assertTrue( cpy[1] == "if \" 1" ); // [if " 1] 1: 540: trimCSV( cpy[2] ); assertTrue( cpy[2] == "" ); // [] 1: 541: trimCSV( cpy[3] ); assertTrue( cpy[3] == " 3xt" ); // [ 3xt] -: 542: }} -: 543:} -: 544: -: 545:#ifdef C_CSV -: 546: -: 547:#include "CSV.c" -: 548: -: 549:/// Test ==> \c trim() [C]. function _ZN8test_CSV11test_trim_CEv called 1 returned 100% blocks executed 100% 1: 550:void test_CSV::test_trim_C() { -: 551: #define googa std::cout << "trim(" << str << ")==> ["; std::cout << trim(str) << ']' << std::endl; -: 552: {{ // test::trim_C() 1: 553: char str[20]; 1: 554: strcpy( str , " a b " ); assertTrue( strcmp( trim(str), "a b" ) == 0 ); 1: 555: strcpy( str , " a\nb " ); assertTrue( strcmp( trim(str), "a\nb") == 0 ); 1: 556: strcpy( str , "" ); assertTrue( strcmp( trim(str), "" ) == 0 ); 1: 557: strcpy( str , "\r\t\n " ); assertTrue( strcmp( trim(str), "" ) == 0 ); 1: 558: strcpy( str , " a b " ); assertTrue( strcmp( trim(str), "a b" ) == 0 ); 1: 559: strcpy( str , " ab " ); assertTrue( strcmp( trim(str), "ab" ) == 0 ); -: 560: -: 561: }} -: 562:} -: 563: -: 564:/// Test ==> \c chop() [C]. function _ZN8test_CSV11test_chop_CEv called 1 returned 100% blocks executed 100% 1: 565:void test_CSV::test_chop_C() { -: 566: {{ // test::chop_C() 1: 567: char str[20]; char ch; 1: 568: strcpy( str , "12345") ; assertTrue( strcmp(str ,"12345")==0 ); 1: 569: chop(str,'0'); assertTrue( strcmp(str ,"12345")==0 ); -: 570: 6: 571: for ( ch='5'; ch != '0'; --ch ) { 5: 572: assertTrue( str[strlen(str)-1] == ch ); 5: 573: chop(str,ch); -: 574: } -: 575: 1: 576: assertTrue( *str == 0 ); 1: 577: chop(str,'3'); assertTrue( *str == 0 ); -: 578: }} -: 579:} -: 580:#endif -: 581: -: 582:/// Test ==> \c chop(). function _ZN8test_CSV9test_chopEv called 1 returned 100% blocks executed 89% 1: 583:void test_CSV::test_chop() { -: 584: {{ // test::chop() 1: 585: std::string str; char ch; 1: 586: str = "12345"; assertTrue( str == "12345" ); 1: 587: chop(str,'0'); assertTrue( str == "12345" ); -: 588: 6: 589: for ( ch='5'; ch != '0'; --ch ) { 5: 590: assertTrue( str[str.size()-1] == ch ); 5: 591: chop(str,ch); -: 592: } -: 593: 1: 594: assertTrue( str == "" ); 1: 595: chop(str,'3'); assertTrue( str == "" ); -: 596: }} -: 597:} -: 598: -: 599:#include // basic_istringstream<> -: 600: -: 601:#ifdef English_dox -: 602:/// Shows the correct way to read \c str usign \c sstream. -: 603:/// - A \c basic_istringstream<> is used to read from \c str. -: 604:/// - The result gets stored into \c res. -: 605:/// - Reading is done using \c operator<<(). -: 606:#endif -: 607:#ifdef Spanish_dox -: 608:/// Muestra la forma correcta de leer la hilera \c str usando \c sstream. -: 609:/// - Se usa \c basic_istringstream<> para leer desde \c str. -: 610:/// - El resultado leído se deposita en \c res. -: 611:/// - La lectura se hace usando el operador \c operator<<(). -: 612:#endif function _Z11mem_istringRSsS_ called 1 returned 100% blocks executed 75% 1: 613:void mem_istring( std::string & res , std::string & str ) { -: 614: // typedef basic_istringstream istringstream; -: 615: { 1: 616: std::istringstream ist(str); // std::basic_istringstream ist(str); 1: 617: getNextCSV( res, ist ); -: 618: } -: 619: { 1: 620: res.clear(); 1: 621: std::basic_istringstream ist(str); // std::istringstream ist(str); 1: 622: char ch='%'; int i =0; 8: 623: while ( ! ist.eof() ) { // see http://www.horstmann.com/cpp/pitfalls.html 7: 624: ist >> ch; ++i; 7: 625: res += ch; 7: 626: ch = '!'; -: 627: } -: 628: // std::cout << '[' << res << ']' << '(' << i << ')' << std::endl; -: 629: } -: 630: { 1: 631: res.clear(); 1: 632: std::istringstream ist(str); // std::basic_istringstream ist(str); 1: 633: char ch='%'; int i =0; 27: 634: for (;;) { 14: 635: ist.get( ch ); ++i; 14: 636: if ( ist.eof() ) { // see http://www.horstmann.com/cpp/pitfalls.html 1: 637: break; -: 638: } 13: 639: res += ch; 13: 640: ch = '!'; -: 641: } -: 642: // std::cout << '[' << res << ']' << '(' << i << ')' << std::endl; -: 643: } -: 644: { 1: 645: res.clear(); 1: 646: std::istringstream ist(str); // std::basic_istringstream ist(str); 1: 647: char ch='%'; int i =0; 13: 648: for (;;) { 7: 649: ist >> ch; ++i; 7: 650: if ( ist.eof() ) { // see http://www.horstmann.com/cpp/pitfalls.html 1: 651: break; -: 652: } 6: 653: res += ch; 6: 654: ch = '!'; -: 655: } -: 656: // std::cout << '[' << res << ']' << '(' << i << ')' << std::endl; -: 657: } -: 658:} -: 659: -: 660:/// Test ==> \c istringstream(). function _ZN8test_CSV18test_istringstreamEv called 1 returned 100% blocks executed 68% 1: 661:void test_CSV::test_istringstream() { -: 662: { 1: 663: std::string res, str = " 1 2 3 4 5 6 "; 1: 664: mem_istring( res , str ); 1: 665: assertTrue( res != "1234566" ); 1: 666: assertTrue( res != " 1 2 3 4 5 6 " ); 1: 667: assertTrue( res == "123456" ); -: 668: } -: 669:} -: 670: -: 671: -: 672:#ifdef English_dox -: 673:/// Main program that executes the tests. -: 674:#endif -: 675:#ifdef Spanish_dox -: 676:/// Programa principal que ejecuta la prueba. -: 677:#endif function main called 1 returned 100% blocks executed 67% 1: 678:int main() { function _ZN8test_CSVD1Ev called 1 returned 100% blocks executed 75% function _ZN8test_CSVD0Ev called 0 returned 0% blocks executed 0% 3: 679: test_CSV tester; 1: 680: tester.run(); 1: 681: std::cout << tester.report(); 1: 682: return 0; 1: 683:} -: 684: -: 685:// EOF: test_CSV -: 686:/*EOF*/ -: 687:/*EOF*/ -: 688:/*EOF*/ -: 689:/*EOF*/ -: 690:/*EOF*/ -: 691:/*EOF*/ -: 692:/*EOF*/ -: 693:/*EOF*/ -: 694:/*EOF*/ -: 695:/*EOF*/ -: 696:/*EOF*/ -: 697:/*EOF*/ -: 698:/*EOF*/ -: 699:/*EOF*/ -: 700:/*EOF*/ -: 701:/*EOF*/ -: 702:/*EOF*/ function _Z41__static_initialization_and_destruction_0ii called 2 returned 100% blocks executed 100% 2: 703:/*EOF*/ function _GLOBAL__I__ZN8test_CSV3runEv called 1 returned 100% blocks executed 100% 2: 704:/*EOF*/ function _GLOBAL__D__ZN8test_CSV3runEv called 1 returned 100% blocks executed 100% 2: 705:/*EOF*/