llstreamtools_tut.cpp

Go to the documentation of this file.
00001 
00034 #include <tut/tut.h>
00035 
00036 #include "linden_common.h"
00037 #include "llstreamtools.h"
00038 #include "lltut.h"
00039 
00040 
00041 namespace tut
00042 {
00043         struct streamtools_data
00044         {
00045         };
00046         typedef test_group<streamtools_data> streamtools_test;
00047         typedef streamtools_test::object streamtools_object;
00048         tut::streamtools_test streamtools_testcase("streamtools");
00049 
00050         //test cases for skip_whitespace()
00051         template<> template<>
00052         void streamtools_object::test<1>()
00053         {
00054                 char arr[255];
00055                 std::string str;
00056                 std::string expected_result;
00057                 std::string actual_result;
00058                 std::istringstream is;
00059 
00060                 is.str(str = "");
00061                 ensure("skip_whitespace: empty string", (false == skip_whitespace(is)));
00062 
00063                 is.clear();
00064                 is.str(str = " SecondLife is a 3D World");
00065                 skip_whitespace(is);
00066                 is.get(arr, 255, '\0');
00067                 expected_result = "SecondLife is a 3D World";
00068                 ensure_equals("skip_whitespace: space", arr, expected_result);
00069 
00070                 is.clear();
00071                 is.str(str = "\t          \tSecondLife is a 3D World");
00072                 skip_whitespace(is);
00073                 is.get(arr, 255, '\0');
00074                 expected_result = "SecondLife is a 3D World";
00075                 ensure_equals("skip_whitespace: space and tabs", arr, expected_result);
00076 
00077                 is.clear();
00078                 is.str(str = "\t          \tSecondLife is a 3D World       ");
00079                 skip_whitespace(is);
00080                 is.get(arr, 255, '\0');
00081                 expected_result = "SecondLife is a 3D World       ";
00082                 ensure_equals("skip_whitespace: space at end", arr, expected_result);
00083 
00084                 is.clear();
00085                 is.str(str = "\t \r\nSecondLife is a 3D World");
00086                 skip_whitespace(is);
00087                 is.get(arr, 255, '\0');
00088                 expected_result = "\r\nSecondLife is a 3D World";
00089                 ensure_equals("skip_whitespace: space at end", arr, expected_result);
00090         }
00091 
00092         //testcases for skip_emptyspaces()
00093         template<> template<>
00094         void streamtools_object::test<2>()
00095         {
00096                 char arr[255];
00097                 std::string str;
00098                 std::string expected_result;
00099                 std::string actual_result;
00100                 std::istringstream is;
00101                 bool ret;
00102 
00103                 is.clear();
00104                 is.str(str = "  \tSecondLife is a 3D World.\n");
00105                 skip_emptyspace(is);
00106                 is.get(arr, 255, '\0');
00107                 expected_result = "SecondLife is a 3D World.\n";
00108                 ensure_equals("skip_emptyspace: space and tabs", arr, expected_result);
00109 
00110                 is.clear();
00111                 is.str(str = "  \t\r\n    \r    SecondLife is a 3D World.\n");
00112                 skip_emptyspace(is);
00113                 is.get(arr, 255, '\0');
00114                 expected_result = "SecondLife is a 3D World.\n";
00115                 ensure_equals("skip_emptyspace: space, tabs, carriage return, newline", arr, expected_result);
00116 
00117                 is.clear();
00118                 is.str(str = "");
00119                 ret = skip_emptyspace(is);
00120                 is.get(arr, 255, '\0');
00121                 ensure("skip_emptyspace: empty string", ret == false);
00122 
00123                 is.clear();
00124                 is.str(str = "  \r\n  \t ");
00125                 ret = skip_emptyspace(is);
00126                 is.get(arr, 255, '\0');
00127                 ensure("skip_emptyspace: space newline empty", ret == false);
00128         }
00129 
00130         //testcases for skip_comments_and_emptyspace()
00131         template<> template<>
00132         void streamtools_object::test<3>()
00133         {
00134                 char arr[255];
00135                 std::string str;
00136                 std::string expected_result;
00137                 std::string actual_result;
00138                 std::istringstream is;
00139                 bool ret;
00140 
00141                 is.clear();
00142                 is.str(str = "  \t\r\n    \r    SecondLife is a 3D World.\n");
00143                 skip_comments_and_emptyspace(is);
00144                 is.get(arr, 255, '\0');
00145                 expected_result = "SecondLife is a 3D World.\n";
00146                 ensure_equals("skip_comments_and_emptyspace: space, tabs, carriage return, newline", arr, expected_result);
00147 
00148                 is.clear();
00149                 is.str(str = "#    \r\n    SecondLife is a 3D World.");
00150                 skip_comments_and_emptyspace(is);
00151                 is.get(arr, 255, '\0');
00152                 expected_result = "SecondLife is a 3D World.";
00153                 ensure_equals("skip_comments_and_emptyspace: skip comment - 1", arr, expected_result);
00154 
00155                 is.clear();
00156                 is.str(str = "#    \r\n  #  SecondLife is a 3D World. ##");
00157                 skip_comments_and_emptyspace(is);
00158                 is.get(arr, 255, '\0');
00159                 expected_result = "";
00160                 ensure_equals("skip_comments_and_emptyspace: skip comment - 2", arr, expected_result);
00161 
00162                 is.clear();
00163                 is.str(str = " \r\n  SecondLife is a 3D World. ##");
00164                 skip_comments_and_emptyspace(is);
00165                 is.get(arr, 255, '\0');
00166                 expected_result = "SecondLife is a 3D World. ##";
00167                 ensure_equals("skip_comments_and_emptyspace: skip comment - 3", arr, expected_result);
00168 
00169                 is.clear();
00170                 is.str(str = "");
00171                 ret = skip_comments_and_emptyspace(is);
00172                 is.get(arr, 255, '\0');
00173                 ensure("skip_comments_and_emptyspace: empty string", ret == false);
00174 
00175                 is.clear();
00176                 is.str(str = "  \r\n  \t # SecondLife is a 3D World");
00177                 ret = skip_comments_and_emptyspace(is);
00178                 is.get(arr, 255, '\0');
00179                 ensure("skip_comments_and_emptyspace: space newline comment empty", ret == false);
00180         }
00181         
00182         //testcases for skip_line()
00183         template<> template<>
00184         void streamtools_object::test<4>()
00185         {
00186                 char arr[255];
00187                 std::string str;
00188                 std::string expected_result;
00189                 std::string actual_result;
00190                 std::istringstream is;
00191                 bool ret;
00192 
00193                 is.clear();
00194                 is.str(str = "SecondLife is a 3D World.\n\n It provides an opportunity to the site \nuser to perform real life activities in virtual world.");
00195                 skip_line(is);
00196                 is.get(arr, 255, '\0');
00197                 expected_result = "\n It provides an opportunity to the site \nuser to perform real life activities in virtual world.";
00198                 ensure_equals("skip_line: 1 newline", arr, expected_result);
00199 
00200                 is.clear();
00201                 is.str(expected_result);
00202                 skip_line(is);
00203                 is.get(arr, 255, '\0');
00204                 expected_result = " It provides an opportunity to the site \nuser to perform real life activities in virtual world.";
00205                 ensure_equals("skip_line: 2 newline", arr, expected_result);
00206 
00207                 is.clear();
00208                 is.str(expected_result);
00209                 skip_line(is);
00210                 is.get(arr, 255, '\0');
00211                 expected_result = "user to perform real life activities in virtual world.";
00212                 ensure_equals("skip_line: 3 newline", arr, expected_result);
00213 
00214                 is.clear();
00215                 is.str(str = "");
00216                 ret = skip_line(is);
00217                 ensure("skip_line: empty string", ret == false);
00218         }
00219 
00220         
00221         // testcases for skip_to_next_word()
00222         template<> template<>
00223         void streamtools_object::test<5>()
00224         {
00225                 char arr[255];
00226                 std::string str;
00227                 std::string expected_result;
00228                 std::string actual_result;
00229                 std::istringstream is;
00230                 bool ret;
00231 
00232                 is.clear();
00233                 is.str(str = "SecondLife is a 3D_World.\n\n It-provides an opportunity to the site \nuser to perform real life activities in virtual world.");
00234                 skip_to_next_word(is); // get past SecondLife
00235                 is.get(arr, 255, '\0');
00236                 expected_result = "is a 3D_World.\n\n It-provides an opportunity to the site \nuser to perform real life activities in virtual world.";
00237                 ensure_equals("skip_to_next_word: 1", arr, expected_result);
00238 
00239                 is.clear();
00240                 is.str(expected_result);
00241                 skip_to_next_word(is); // get past is
00242                 skip_to_next_word(is); // get past a
00243                 skip_to_next_word(is); // get past 3D_World.\n\n 
00244                 is.get(arr, 255, '\0');
00245                 expected_result = "It-provides an opportunity to the site \nuser to perform real life activities in virtual world.";
00246                 ensure_equals("skip_to_next_word: get past .\n\n 2", arr, expected_result);
00247                 
00248                 is.clear();
00249                 is.str(expected_result);
00250                 skip_to_next_word(is); // get past It- 
00251                 expected_result = "provides an opportunity to the site \nuser to perform real life activities in virtual world.";
00252                 is.get(arr, 255, '\0');
00253                 ensure_equals("skip_to_next_word: get past -", arr, expected_result);
00254 
00255                 is.clear();
00256                 is.str(str = "");
00257                 ret = skip_to_next_word(is);
00258                 ensure("skip_line: empty string", ret == false);
00259 
00260                 is.clear();
00261                 is.str(str = "                   \r\n\r\n");
00262                 ret = skip_to_next_word(is);
00263                 ensure("skip_line: space new lines", ret == false);
00264         }
00265 
00266 
00267         //testcases for skip_to_end_of_next_keyword()
00268         template<> template<>
00269         void streamtools_object::test<6>()
00270         {
00271                 char arr[255];
00272                 std::string str;
00273                 std::string expected_result;
00274                 std::string actual_result;
00275                 std::istringstream is;
00276                 bool ret;
00277 
00278                 is.clear();
00279                 is.str(str = "FIRSTKEY followed by second delimiter\nSECONDKEY\t SecondValue followed by third delimiter   \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.");
00280                 ret = skip_to_end_of_next_keyword("FIRSTKEY", is); 
00281                 is.get(arr, 255, '\0');
00282                 expected_result = " followed by second delimiter\nSECONDKEY\t SecondValue followed by third delimiter   \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
00283                 ensure_equals("skip_to_end_of_next_keyword: 1", arr, expected_result);
00284 
00285                 is.clear();
00286                 is.str(expected_result);
00287                 ret = skip_to_end_of_next_keyword("SECONDKEY", is); 
00288                 is.get(arr, 255, '\0');
00289                 expected_result = "\t SecondValue followed by third delimiter   \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
00290                 ensure_equals("skip_to_end_of_next_keyword: 2", arr, expected_result);
00291 
00292                 is.clear();
00293                 is.str(expected_result);
00294                 ret = skip_to_end_of_next_keyword("SECONDKEY", is); 
00295                 is.get(arr, 255, '\0');
00296                 expected_result = "\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
00297                 ensure_equals("skip_to_end_of_next_keyword: 3", arr, expected_result);
00298 
00299                 is.clear();
00300                 is.str(expected_result);
00301                 ret = skip_to_end_of_next_keyword("FOURTHKEY", is); 
00302                 is.get(arr, 255, '\0');
00303                 expected_result = " FOURTHVALUEis a 3DWorld.";
00304                 ensure_equals("skip_to_end_of_next_keyword: 4", arr, expected_result);
00305 
00306                 is.clear();
00307                 is.str(str = "{should be skipped as newline/space/tab does not follow but this one should be picked\n { Does it?\n");
00308                 ret = skip_to_end_of_next_keyword("{", is); 
00309                 is.get(arr, 255, '\0');
00310                 expected_result = " Does it?\n";
00311                 ensure_equals("skip_to_end_of_next_keyword: multiple delim matches on same line", arr, expected_result);
00312 
00313                 is.clear();
00314                 is.str(str = "Delim { could not be found at start");
00315                 ret = skip_to_end_of_next_keyword("{", is); 
00316                 ensure("skip_to_end_of_next_keyword: delim should not be present", ret == false);
00317 
00318                 is.clear();
00319                 is.str(str = "Empty Delim");
00320                 ret = skip_to_end_of_next_keyword("", is); 
00321                 ensure("skip_to_end_of_next_keyword: empty delim should not be valid", ret == false);
00322 
00323                 is.clear();
00324                 is.str(str = "");
00325                 ret = skip_to_end_of_next_keyword("}", is); 
00326                 ensure("skip_to_end_of_next_keyword: empty string", ret == false);
00327         }
00328 
00329         //testcases for get_word(std::string& output_string, std::istream& input_stream)
00330         template<> template<>
00331         void streamtools_object::test<7>()
00332         {
00333                 std::string str;
00334                 std::string expected_result;
00335                 std::string actual_result;
00336                 std::istringstream is;
00337                 bool ret;
00338 
00339                 is.clear();
00340                 is.str(str = "  First Second \t \r  \n Third  Fourth-ShouldThisBePartOfFourth  Fifth\n");
00341                 actual_result = "";
00342                 ret = get_word(actual_result, is); 
00343                 expected_result = "First";
00344                 ensure_equals("get_word: 1", actual_result, expected_result);
00345 
00346                 actual_result = "";
00347                 ret = get_word(actual_result, is); 
00348                 expected_result = "Second";
00349                 ensure_equals("get_word: 2", actual_result, expected_result);
00350 
00351                 actual_result = "";
00352                 ret = get_word(actual_result, is); 
00353                 expected_result = "Third";
00354                 ensure_equals("get_word: 3", actual_result, expected_result);
00355 
00356                 // the current implementation of get_word seems inconsistent with
00357                 // skip_to_next_word. skip_to_next_word treats any character other
00358                 // than alhpa-numeric and '_' as a delimter, while get_word()
00359                 // treats only isspace() (i.e. space,  form-feed('\f'),  newline  ('\n'),  
00360                 // carriage  return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')
00361                 // as delimiters 
00362                 actual_result = "";
00363                 ret = get_word(actual_result, is); // will copy Fourth-ShouldThisBePartOfFourth
00364                 expected_result = "Fourth-ShouldThisBePartOfFourth"; // as per current impl.
00365                 ensure_equals("get_word: 4", actual_result, expected_result);
00366 
00367                 actual_result = "";
00368                 ret = get_word(actual_result, is); // will copy Fifth
00369                 expected_result = "Fifth"; // as per current impl.
00370                 ensure_equals("get_word: 5", actual_result, expected_result);
00371 
00372                 is.clear();
00373                 is.str(str = "  \t \r  \n ");
00374                 actual_result = "";
00375                 ret = get_word(actual_result, is); 
00376                 ensure("get_word: empty all spaces, newline tabs", ret == false);
00377 
00378                 is.clear();
00379                 is.str(str = "");
00380                 actual_result = "";
00381                 ret = get_word(actual_result, is); 
00382                 ensure("get_word: empty string", ret == false);
00383         }
00384 
00385         // testcase for get_word and skip_to_next_word compatibility
00386         template<> template<>
00387         void streamtools_object::test<8>()
00388         {
00389                 std::string str;
00390                 std::string expected_result;
00391                 std::string actual_result;
00392                 std::istringstream is;
00393                 bool ret;
00394 
00395                 is.clear();
00396                 is.str(str = "  First Second \t \r  \n Third  Fourth-ShouldThisBePartOfFourth  Fifth\n");
00397                 actual_result = "";
00398                 ret = get_word(actual_result, is); // First
00399                 actual_result = "";
00400                 ret = get_word(actual_result, is); // Second
00401                 actual_result = "";
00402                 ret = get_word(actual_result, is); // Third
00403 
00404                 // the current implementation of get_word seems inconsistent with
00405                 // skip_to_next_word. skip_to_next_word treats any character other
00406                 // than alhpa-numeric and '_' as a delimter, while get_word()
00407                 // treats only isspace() (i.e. space,  form-feed('\f'),  newline  ('\n'),  
00408                 // carriage  return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')
00409                 // as delimiters 
00410                 actual_result = "";
00411                 ret = get_word(actual_result, is); // will copy Fourth-ShouldThisBePartOfFourth
00412 
00413                 actual_result = "";
00414                 ret = get_word(actual_result, is); // will copy Fifth
00415 
00416                 is.clear();
00417                 is.str(str = "  First Second \t \r  \n Third  Fourth_ShouldThisBePartOfFourth Fifth\n");
00418                 ret = skip_to_next_word(is);  // should now point to First
00419                 ret = skip_to_next_word(is);  // should now point to Second
00420                 ret = skip_to_next_word(is);  // should now point to Third
00421                 ret = skip_to_next_word(is);  // should now point to Fourth
00422                 ret = skip_to_next_word(is);  // should now point to ShouldThisBePartOfFourth
00423                 expected_result = "";
00424                 // will copy ShouldThisBePartOfFourth, the fifth word, 
00425                 // while using get_word above five times result in getting "Fifth"
00426                 ret = get_word(expected_result, is); 
00427                 ensure_equals("get_word: skip_to_next_word compatibility", actual_result, expected_result);
00428         }
00429 
00430         //testcases for get_word(std::string& output_string, std::istream& input_stream, int n)
00431         template<> template<>
00432         void streamtools_object::test<9>()
00433         {
00434                 std::string str;
00435                 std::string expected_result;
00436                 std::string actual_result;
00437                 std::istringstream is;
00438                 bool ret;
00439 
00440                 is.clear();
00441                 is.str(str = "  First Second \t \r  \n Third  Fourth-ShouldThisBePartOfFourth  Fifth\n");
00442                 actual_result = "";
00443                 ret = get_word(actual_result, is, 255);
00444                 expected_result = "First";
00445                 ensure_equals("get_word: 1", actual_result, expected_result);
00446 
00447                 actual_result = "";
00448                 ret = get_word(actual_result, is, 4); 
00449                 expected_result = "Seco"; // should be cut short
00450                 ensure_equals("get_word: 2", actual_result, expected_result);
00451 
00452                 actual_result = "";
00453                 ret = get_word(actual_result, is, 255); 
00454                 expected_result = "nd"; // get remainder of Second
00455                 ensure_equals("get_word: 3", actual_result, expected_result);
00456 
00457                 actual_result = "";
00458                 ret = get_word(actual_result, is, 0); // 0 size string 
00459                 expected_result = ""; // get remainder of Second
00460                 ensure_equals("get_word: 0 sized output", actual_result, expected_result);
00461 
00462                 actual_result = "";
00463                 ret = get_word(actual_result, is, 255); 
00464                 expected_result = "Third"; 
00465                 ensure_equals("get_word: 4", actual_result, expected_result);
00466 
00467                 is.clear();
00468                 is.str(str = "  \t \r  \n ");
00469                 actual_result = "";
00470                 ret = get_word(actual_result, is, 255); 
00471                 ensure("get_word: empty all spaces, newline tabs", ret == false);
00472 
00473                 is.clear();
00474                 is.str(str = "");
00475                 actual_result = "";
00476                 ret = get_word(actual_result, is, 255); 
00477                 ensure("get_word: empty string", ret == false);
00478         }
00479         
00480         //test cases for get_line(std::string& output_string, std::istream& input_stream)
00481         template<> template<>
00482         void streamtools_object::test<10>()
00483         {
00484                 std::string str;
00485                 std::string expected_result;
00486                 std::string actual_result;
00487                 std::istringstream is;
00488                 bool ret;
00489 
00490                 is.clear();
00491                 is.str(str = "First Second \t \r\n Third  Fourth-ShouldThisBePartOfFourth  IsThisFifth\n");
00492                 actual_result = "";
00493                 ret = get_line(actual_result, is);
00494                 expected_result = "First Second \t \n";
00495                 ensure_equals("get_line: 1", actual_result, expected_result);
00496 
00497                 actual_result = "";
00498                 ret = get_line(actual_result, is);
00499                 expected_result = " Third  Fourth-ShouldThisBePartOfFourth  IsThisFifth\n";
00500                 ensure_equals("get_line: 2", actual_result, expected_result);
00501 
00502                 is.clear();
00503                 is.str(str = "\nFirst Line.\n\nSecond Line.\n");
00504                 actual_result = "";
00505                 ret = get_line(actual_result, is);
00506                 expected_result = "\n";
00507                 ensure_equals("get_line: First char as newline", actual_result, expected_result);
00508 
00509                 actual_result = "";
00510                 ret = get_line(actual_result, is);
00511                 expected_result = "First Line.\n";
00512                 ensure_equals("get_line: 3", actual_result, expected_result);
00513 
00514                 actual_result = "";
00515                 ret = get_line(actual_result, is);
00516                 expected_result = "\n";
00517                 ensure_equals("get_line: 4", actual_result, expected_result);
00518 
00519                 actual_result = "";
00520                 ret = get_line(actual_result, is);
00521                 expected_result = "Second Line.\n";
00522                 ensure_equals("get_line: 5", actual_result, expected_result);
00523         }       
00524 
00525         //test cases for get_line(std::string& output_string, std::istream& input_stream)
00526         template<> template<>
00527         void streamtools_object::test<11>()
00528         {
00529                 std::string str;
00530                 std::string expected_result;
00531                 std::string actual_result;
00532                 std::istringstream is;
00533                 bool ret;
00534 
00535                 is.clear();
00536                 is.str(str = "One Line only with no newline");
00537                 actual_result = "";
00538                 ret = get_line(actual_result, is);
00539                 expected_result = "One Line only with no newline";
00540                 ensure_equals("get_line: No newline", actual_result, expected_result);
00541                 ensure_equals("return value is good state of stream", ret, is.good());
00542         }
00543 
00544         //test cases for get_line(std::string& output_string, std::istream& input_stream)
00545         template<> template<>
00546         void streamtools_object::test<12>()
00547         {
00548                 skip_fail("get_line() incorrectly handles lone carriage return.");
00549                 std::string str;
00550                 std::string expected_result;
00551                 std::string actual_result;
00552                 std::istringstream is;
00553                 bool ret;
00554 
00555                 // need to be check if this test case is wrong or the implementation is wrong.
00556                 is.clear();
00557                 is.str(str = "Should not skip \r unless they are followed with newline .\r\n");
00558                 actual_result = "";
00559                 ret = get_line(actual_result, is);
00560                 expected_result = "Should not skip \r unless they are followed with newline .\n";
00561                 ensure_equals("get_line: carriage return skipped even though not followed by newline", actual_result, expected_result);
00562         }
00563 
00564         //test cases for get_line(std::string& output_string, std::istream& input_stream)
00565         template<> template<>
00566         void streamtools_object::test<13>()
00567         {
00568                 std::string str;
00569                 std::string expected_result;
00570                 std::string actual_result;
00571                 std::istringstream is;
00572                 bool ret;
00573 
00574                 is.clear();
00575                 is.str(str = "\n");
00576                 actual_result = "";
00577                 ret = get_line(actual_result, is);
00578                 expected_result = "\n";
00579                 ensure_equals("get_line: Just newline", actual_result, expected_result);
00580         }
00581 
00582 
00583         //testcases for get_line(std::string& output_string, std::istream& input_stream, int n)
00584         template<> template<>
00585         void streamtools_object::test<14>()
00586         {
00587                 std::string str;
00588                 std::string expected_result;
00589                 std::string actual_result;
00590                 std::istringstream is;
00591                 bool ret;
00592 
00593                 is.clear();
00594                 is.str(str = "First Line.\nSecond Line.\n");
00595                 actual_result = "";
00596                 ret = get_line(actual_result, is, 255);
00597                 expected_result = "First Line.\n";
00598                 ensure_equals("get_line: Basic Operation", actual_result, expected_result);
00599 
00600                 actual_result = "";
00601                 ret = get_line(actual_result, is, sizeof("Second")-1);
00602                 expected_result = "Second\n";
00603                 ensure_equals("get_line: Insufficient length 1", actual_result, expected_result);
00604 
00605                 actual_result = "";
00606                 ret = get_line(actual_result, is, 255);
00607                 expected_result = " Line.\n";
00608                 ensure_equals("get_line: Remainder after earlier insufficient length", actual_result, expected_result);
00609 
00610                 is.clear();
00611                 is.str(str = "One Line only with no newline with limited length");
00612                 actual_result = "";
00613                 ret = get_line(actual_result, is, sizeof("One Line only with no newline with limited length")-1);
00614                 expected_result = "One Line only with no newline with limited length\n";
00615                 ensure_equals("get_line: No newline with limited length", actual_result, expected_result);
00616 
00617                 is.clear();
00618                 is.str(str = "One Line only with no newline");
00619                 actual_result = "";
00620                 ret = get_line(actual_result, is, 255);
00621                 expected_result = "One Line only with no newline";
00622                 ensure_equals("get_line: No newline", actual_result, expected_result);
00623         }
00624 
00625         //testcases for get_line(std::string& output_string, std::istream& input_stream, int n)
00626         template<> template<>
00627         void streamtools_object::test<15>()
00628         {
00629                 std::string str;
00630                 std::string expected_result;
00631                 std::string actual_result;
00632                 std::istringstream is;
00633                 bool ret;
00634 
00635                 is.clear();
00636                 is.str(str = "One Line only with no newline");
00637                 actual_result = "";
00638                 ret = get_line(actual_result, is, 255);
00639                 expected_result = "One Line only with no newline";
00640                 ensure_equals("get_line: No newline", actual_result, expected_result);
00641                 ensure_equals("return value is good state of stream", ret, is.good());
00642         }
00643 
00644         //testcases for remove_last_char()
00645         template<> template<>
00646         void streamtools_object::test<16>()
00647         {
00648                 std::string str;
00649                 std::string expected_result;
00650                 bool ret;
00651 
00652                 str = "SecondLife is a 3D World";
00653 
00654                 ret = remove_last_char('d',str);
00655                 expected_result = "SecondLife is a 3D Worl";
00656                 ensure_equals("remove_last_char: should remove last char", str, expected_result);
00657 
00658                 str = "SecondLife is a 3D World";
00659                 ret = remove_last_char('W',str);
00660                 expected_result = "SecondLife is a 3D World";
00661                 ensure_equals("remove_last_char: should not remove as it is not last char", str, expected_result);
00662                 ensure("remove_last_char: should return false", ret == false);
00663 
00664                 str = "SecondLife is a 3D World\n";
00665                 ret = remove_last_char('\n',str);
00666                 expected_result = "SecondLife is a 3D World";
00667                 ensure_equals("remove_last_char: should remove last newline", str, expected_result);
00668                 ensure("remove_last_char: should remove newline and return true", ret == true);
00669         }
00670 
00671 
00672         //testcases for unescaped_string()
00673         template<> template<>
00674         void streamtools_object::test<17>()
00675         {
00676                 std::string str;
00677                 std::string expected_result;
00678 
00679                 str = "SecondLife is a 3D world \\n";
00680                 unescape_string(str);
00681                 expected_result = "SecondLife is a 3D world \n";
00682                 ensure_equals("unescape_string: newline", str, expected_result);
00683 
00684                 str = "SecondLife is a 3D world \\\\t \\n";
00685                 unescape_string(str);
00686                 expected_result = "SecondLife is a 3D world \\t \n";
00687                 ensure_equals("unescape_string: backslash and newline", str, expected_result);
00688 
00689                 str = "SecondLife is a 3D world \\ ";
00690                 unescape_string(str);
00691                 expected_result = "SecondLife is a 3D world \\ ";
00692                 ensure_equals("unescape_string: insufficient to unescape", str, expected_result);
00693 
00694                 str = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
00695                 unescape_string(str);
00696                 expected_result = "SecondLife is a 3D world \n \n \n \\\n";
00697                 ensure_equals("unescape_string: multipel newline and backslash", str, expected_result);
00698 
00699                 str = "SecondLife is a 3D world \\t";
00700                 unescape_string(str);
00701                 expected_result = "SecondLife is a 3D world \\t";
00702                 ensure_equals("unescape_string: leaves tab as is", str, expected_result);
00703 
00704                 str = "\\n";
00705                 unescape_string(str);
00706                 expected_result = "\n";
00707                 ensure_equals("unescape_string: only a newline", str, expected_result);
00708         }
00709 
00710         //testcases for escape_string()
00711         template<> template<>
00712         void streamtools_object::test<18>()
00713         {
00714                 std::string str;
00715                 std::string expected_result;
00716 
00717                 str = "SecondLife is a 3D world \n";
00718                 escape_string(str);
00719                 expected_result = "SecondLife is a 3D world \\n";
00720                 ensure_equals("escape_string: newline", str, expected_result);
00721 
00722                 str = "SecondLife is a 3D world \\t \n";
00723                 escape_string(str);
00724                 expected_result = "SecondLife is a 3D world \\\\t \\n";
00725                 ensure_equals("escape_string: backslash and newline", str, expected_result);
00726 
00727                 str = "SecondLife is a 3D world \n \n \n \\\n";
00728                 escape_string(str);
00729                 expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
00730                 ensure_equals("escape_string: multipel newline and backslash", str, expected_result);
00731 
00732                 str = "SecondLife is a 3D world \t";
00733                 escape_string(str);
00734                 expected_result = "SecondLife is a 3D world \t";
00735                 ensure_equals("unescape_string: leaves tab as is", str, expected_result);
00736 
00737                 str = "\n";
00738                 escape_string(str);
00739                 expected_result = "\\n";
00740                 ensure_equals("unescape_string: only a newline", str, expected_result);
00741 
00742                 // serialization/deserialization escape->unescape
00743                 str = "SecondLife is a 3D world \n \n \n \\\n";
00744                 escape_string(str);
00745                 unescape_string(str);
00746                 expected_result = "SecondLife is a 3D world \n \n \n \\\n";
00747                 ensure_equals("escape_string: should preserve with escape/unescape", str, expected_result);
00748 
00749                 // serialization/deserialization  unescape->escape
00750                 str = "SecondLife is a 3D world \\n \\n \\n \\\\";
00751                 unescape_string(str);
00752                 escape_string(str);
00753                 expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\";
00754                 ensure_equals("escape_string: should preserve with unescape/escape", str, expected_result);
00755         }
00756 
00757         // testcases for replace_newlines_with_whitespace()
00758         template<> template<>
00759         void streamtools_object::test<19>()
00760         {
00761                 std::string str;
00762                 std::string expected_result;
00763 
00764                 str = "SecondLife is a 3D \n\nworld\n";
00765                 replace_newlines_with_whitespace(str);
00766                 expected_result = "SecondLife is a 3D   world ";
00767                 ensure_equals("replace_newlines_with_whitespace: replace all newline", str, expected_result);
00768 
00769                 str = "\nSecondLife is a 3D world\n";
00770                 replace_newlines_with_whitespace(str);
00771                 expected_result = " SecondLife is a 3D world ";
00772                 ensure_equals("replace_newlines_with_whitespace: begin and newline", str, expected_result);
00773 
00774                 str = "SecondLife is a 3D world\r\t";
00775                 replace_newlines_with_whitespace(str);
00776                 expected_result = "SecondLife is a 3D world\r\t";
00777                 ensure_equals("replace_newlines_with_whitespace: should only replace newline", str, expected_result);
00778 
00779                 str = "";
00780                 replace_newlines_with_whitespace(str);
00781                 expected_result = "";
00782                 ensure_equals("replace_newlines_with_whitespace: empty string", str, expected_result);
00783         }
00784 
00785         //testcases for remove_double_quotes()
00786         template<> template<>
00787         void streamtools_object::test<20>()
00788         {
00789                 std::string str;
00790                 std::string expected_result;
00791 
00792                 str = "SecondLife is a \"\"3D world";
00793                 remove_double_quotes(str);
00794                 expected_result = "SecondLife is a 3D world";
00795                 ensure_equals("remove_double_quotes: replace empty doube quotes", str, expected_result);
00796 
00797                 str = "SecondLife is a \"3D world";
00798                 remove_double_quotes(str);
00799                 expected_result = "SecondLife is a 3D world";
00800                 ensure_equals("remove_double_quotes: keep as is it matching quote not found", str, expected_result);
00801         }
00802 
00803         // testcases for get_brace_count()
00804         template<> template<>
00805         void streamtools_object::test<21>()
00806         {
00807                 skip_fail("get_brace_count() has bugs.");
00808 
00809                 std::string str;
00810                 std::string expected_result;
00811                 int count;
00812 
00813                 str = "  {  ";
00814                 count = get_brace_count(str);
00815                 ensure("get_brace_count: 1 for {", count == 1);
00816 
00817                 str = "\t}\t\t   \n";
00818                 count = get_brace_count(str);
00819                 ensure("get_brace_count: 1 for {", count == -1);
00820 
00821                 str = "\t\t\t   \n";
00822                 count = get_brace_count(str);
00823                 ensure("get_brace_count: 0 for no braces", count == 0);
00824 
00825                 str = "{ Remaining line not empty\n";
00826                 count = get_brace_count(str);
00827                 ensure("get_brace_count: 0 for remainign line not empty", count == 0);
00828 
00829                 /* shouldn't this return 1? */
00830                 str = "{ /*Remaining line in comment*/\n";
00831                 count = get_brace_count(str);
00832                 ensure("get_brace_count: 1 for { with remaining line in comment", count == 1);
00833 
00834                 /* shouldn't this return -1? */
00835                 str = "  } //Remaining line in comment  \n";
00836                 count = get_brace_count(str);
00837                 ensure("get_brace_count: -1 for } with remaining line in comment", count == -1);
00838         }
00839 
00840         //testcases for get_keyword_and_value()
00841         template<> template<>
00842         void streamtools_object::test<22>()
00843         {
00844                 std::string s = "SecondLife is a 3D World";
00845                 std::string keyword;
00846                 std::string value;
00847                 get_keyword_and_value(keyword, value, s);
00848                 ensure("get_keyword_and_value: Unable to get Keyword and Value", ((keyword == "SecondLife") && (value == "is a 3D World")));
00849 
00850                 s = "SecondLife";
00851                 get_keyword_and_value(keyword, value, s);
00852                 ensure("get_keyword_and_value: value should be empty", ((keyword == "SecondLife") && (value == "")));
00853 
00854                 s = "SecondLife \t  is cool!     \n";
00855                 get_keyword_and_value(keyword, value, s);
00856                 ensure("get_keyword_and_value: remove space before value but not after", ((keyword == "SecondLife") && (value == "is cool!     ")));
00857         }
00858 
00859         //testcases for get_keyword_and_value()
00860         template<> template<>
00861         void streamtools_object::test<23>()
00862         {
00863                 skip_fail("get_keyword_and_value() has bugs.");
00864 
00865                 std::string s;
00866                 std::string keyword = "SOME PRIOR KEYWORD";
00867                 std::string value = "SOME PRIOR VALUE";
00868 
00869                 s = "SecondLife\n";
00870                 get_keyword_and_value(keyword, value, s);
00871                 ensure("get_keyword_and_value: terminated with newline. value should be empty", ((keyword == "SecondLife") && (value == "")));
00872         }
00873 
00874         //testcases for get_keyword_and_value()
00875         template<> template<>
00876         void streamtools_object::test<24>()
00877         {
00878                 skip_fail("get_keyword_and_value() has bugs.");
00879 
00880                 std::string s;
00881                 std::string keyword = "SOME PRIOR KEYWORD";
00882                 std::string value = "SOME PRIOR VALUE";
00883 
00884                 s = "";
00885                 get_keyword_and_value(keyword, value, s);
00886                 ensure("get_keyword_and_value: empty string. keyword value should empty", ((keyword == "") && (value == "")));
00887         }
00888 
00889         //testcase for fullread()
00890         template<> template<>
00891         void streamtools_object::test<25>()
00892         {
00893                 std::string str = "First Line.\nSecond Line\n";
00894                 std::istringstream is(str);
00895                 char buf[255] = {0};
00896                 
00897                 fullread(is, buf, 255);
00898                 ensure_memory_matches("fullread: read with newlines", (void*) buf,  str.size()-1, (void*) str.c_str(), str.size()-1);
00899 
00900                 is.clear();
00901                 is.str(str = "First Line.\nSecond Line\n");
00902                 memset(buf, 0, 255);
00903                 
00904                 char expected_string[] = "First Line.\nSecond";
00905                 int len = sizeof(expected_string)-1;
00906                 fullread(is, buf, len);
00907                 ensure_memory_matches("fullread: read with newlines", (void*) buf, len, (void*) &expected_string, len);
00908         }
00909 
00910 
00911 //   testcases for operator >>
00912 
00913         template<> template<>
00914         void streamtools_object::test<26>()
00915         {
00916                 char arr[255];
00917                 std::string str;
00918                 std::string toCheck = "SecondLife" ;
00919                 std::string expected_result;
00920                 std::istringstream stream("SecondLife is a 3D World");
00921                 stream >> toCheck.c_str();
00922                 stream.get(arr, 255, '\0');
00923                 expected_result = " is a 3D World"; 
00924                 ensure_equals("istream << operator", arr, expected_result);
00925 
00926                 stream.clear();
00927                 stream.str(str = "SecondLife is a 3D World");
00928                 toCheck = "is";
00929                 stream >> toCheck.c_str();
00930                 ensure("istream << operator should have failed", stream.good() == false);
00931         }
00932 }

Generated on Thu Jul 1 06:09:13 2010 for Second Life Viewer by  doxygen 1.4.7