llstring_tut.cpp

Go to the documentation of this file.
00001 
00034 #include <tut/tut.h>
00035 #include "lltut.h"
00036 #include "linden_common.h"
00037 #include "llstring.h"
00038 
00039 namespace tut
00040 {
00041         struct string_index
00042         {
00043         };
00044         typedef test_group<string_index> string_index_t;
00045         typedef string_index_t::object string_index_object_t;
00046         tut::string_index_t tut_string_index("string_test");
00047 
00048         template<> template<>
00049         void string_index_object_t::test<1>()
00050         {
00051                 LLString llstr1;
00052                 ensure("Empty LLString", (llstr1.size() == 0) && llstr1.empty());
00053 
00054                 LLString llstr2("Hello");
00055                 ensure("LLString = Hello", (!strcmp(llstr2.c_str(), "Hello")) && (llstr2.size() == 5) && !llstr2.empty());
00056 
00057                 LLString llstr3(llstr2);
00058                 ensure("LLString = LLString(LLString)", (!strcmp(llstr3.c_str(), "Hello")) && (llstr3.size() == 5) && !llstr3.empty());
00059 
00060                 std::string str("Hello World");
00061                 LLString llstr4(str, 6);
00062                 ensure("LLString = LLString(s, size_type pos, size_type n = npos)", (!strcmp(llstr4.c_str(), "World")) && (llstr4.size() == 5) && !llstr4.empty());
00063 
00064                 LLString llstr5(str, str.size());
00065                 ensure("LLString = LLString(s, size_type pos, size_type n = npos)", (llstr5.size() == 0) && llstr5.empty());
00066 
00067                 LLString llstr6(5, 'A');
00068                 ensure("LLString = LLString(count, c)", (!strcmp(llstr6.c_str(), "AAAAA")) && (llstr6.size() == 5) && !llstr6.empty());
00069 
00070                 LLString llstr7("Hello World", 5);
00071                 ensure("LLString(s, n)", (!strcmp(llstr7.c_str(), "Hello")) && (llstr7.size() == 5) && !llstr7.empty());
00072 
00073                 LLString llstr8("Hello World", 6, 5);
00074                 ensure("LLString(s, n, count)", (!strcmp(llstr8.c_str(), "World")) && (llstr8.size() == 5) && !llstr8.empty());
00075 
00076                 LLString llstr9("Hello World", sizeof("Hello World")-1, 5); // go past end
00077                 ensure("LLString(s, n, count) goes past end", (llstr9.size() == 0) && llstr9.empty());
00078         }
00079 
00080         template<> template<>
00081         void string_index_object_t::test<2>()
00082         {
00083                 LLString str("Len=5");
00084                 ensure("isValidIndex failed", LLString::isValidIndex(str, 0) == TRUE &&
00085                                                                           LLString::isValidIndex(str, 5) == TRUE &&
00086                                                                           LLString::isValidIndex(str, 6) == FALSE);
00087 
00088                 LLString str1;
00089                 ensure("isValidIndex failed fo rempty string", LLString::isValidIndex(str1, 0) == FALSE);
00090         }
00091 
00092         template<> template<>
00093         void string_index_object_t::test<3>()
00094         {
00095                 LLString str_val("               Testing the extra whitespaces   ");
00096                 LLString::trimHead(str_val);
00097                 ensure_equals("1: trimHead failed", str_val, "Testing the extra whitespaces   ");
00098 
00099                 LLString str_val1("\n\t\r\n  Testing the extra whitespaces   ");
00100                 LLString::trimHead(str_val1);
00101                 ensure_equals("2: trimHead failed", str_val1, "Testing the extra whitespaces   ");
00102         }
00103 
00104         template<> template<>
00105         void string_index_object_t::test<4>()
00106         {
00107                 LLString str_val("  Testing the   extra     whitespaces         ");
00108                 LLString::trimTail(str_val);
00109                 ensure_equals("1: trimTail failed", str_val, "  Testing the   extra     whitespaces");
00110 
00111                 LLString str_val1("\n  Testing the extra whitespaces  \n\t\r\n   ");
00112                 LLString::trimTail(str_val1);
00113                 ensure_equals("2: trimTail failed", str_val1, "\n  Testing the extra whitespaces");
00114         }
00115 
00116 
00117         template<> template<>
00118         void string_index_object_t::test<5>()
00119         {
00120                 LLString str_val("  \t \r Testing the   extra     \r\n whitespaces     \n \t    ");
00121                 LLString::trim(str_val);
00122                 ensure_equals("1: trim failed", str_val, "Testing the   extra     \r\n whitespaces");
00123         }
00124 
00125         template<> template<>
00126         void string_index_object_t::test<6>()
00127         {
00128                 LLString str("Second LindenLabs");
00129                 LLString::truncate(str, 6);
00130                 ensure_equals("1: truncate", str, "Second");
00131 
00132                 // further truncate more than the length
00133                 LLString::truncate(str, 0);
00134                 ensure_equals("2: truncate", str, "");
00135         }
00136 
00137         template<> template<>
00138         void string_index_object_t::test<7>()
00139         {
00140                 LLString str_val("SecondLife Source");
00141                 LLString::toUpper(str_val);
00142                 ensure_equals("toUpper failed", str_val, "SECONDLIFE SOURCE");
00143         }
00144 
00145         template<> template<>
00146         void string_index_object_t::test<8>()
00147         {
00148                 LLString str_val("SecondLife Source");
00149                 LLString::toLower(str_val);
00150                 ensure_equals("toLower failed", str_val, "secondlife source");
00151         }
00152 
00153         template<> template<>
00154         void string_index_object_t::test<9>()
00155         {
00156                 LLString str_val("Second");
00157                 ensure("1. isHead failed", LLString::isHead(str_val, "SecondLife Source") == TRUE);
00158                 ensure("2. isHead failed", LLString::isHead(str_val, " SecondLife Source") == FALSE);
00159                 LLString str_val2("");
00160                 ensure("3. isHead failed", LLString::isHead(str_val2, "") == FALSE);
00161         }
00162 
00163         template<> template<>
00164         void string_index_object_t::test<10>()
00165         {
00166                 LLString str_val("Hello.\n\n Lindenlabs. \n This is \na simple test.\n");
00167                 LLString orig_str_val(str_val);
00168                 LLString::addCRLF(str_val);
00169                 ensure_equals("addCRLF failed", str_val, "Hello.\r\n\r\n Lindenlabs. \r\n This is \r\na simple test.\r\n");
00170                 LLString::removeCRLF(str_val);
00171                 ensure_equals("removeCRLF failed", str_val, orig_str_val);
00172         }
00173 
00174         template<> template<>
00175         void string_index_object_t::test<11>()
00176         {
00177                 LLString str_val("Hello.\n\n\t \t Lindenlabs. \t\t");
00178                 LLString orig_str_val(str_val);
00179                 LLString::replaceTabsWithSpaces(str_val, 1);
00180                 ensure_equals("replaceTabsWithSpaces failed", str_val, "Hello.\n\n    Lindenlabs.   ");
00181                 LLString::replaceTabsWithSpaces(orig_str_val, 0);
00182                 ensure_equals("replaceTabsWithSpaces failed for 0", orig_str_val, "Hello.\n\n  Lindenlabs. ");
00183 
00184                 str_val = "\t\t\t\t";
00185                 LLString::replaceTabsWithSpaces(str_val, 0);
00186                 ensure_equals("replaceTabsWithSpaces failed for all tabs", str_val, "");
00187         }
00188 
00189         template<> template<>
00190         void string_index_object_t::test<12>()
00191         {
00192                 LLString str_val("Hello.\n\n\t\t\r\nLindenlabsX.");
00193                 LLString::replaceNonstandardASCII(str_val, 'X');
00194                 ensure_equals("replaceNonstandardASCII failed", str_val, "Hello.\n\nXXX\nLindenlabsX.");
00195         }
00196 
00197         template<> template<>
00198         void string_index_object_t::test<13>()
00199         {
00200                 LLString str_val("Hello.\n\t\r\nABCDEFGHIABABAB");
00201                 LLString::replaceChar(str_val, 'A', 'X');
00202                 ensure_equals("1: replaceChar failed", str_val, "Hello.\n\t\r\nXBCDEFGHIXBXBXB");
00203                 LLString str_val1("Hello.\n\t\r\nABCDEFGHIABABAB");
00204         }
00205 
00206         template<> template<>
00207         void string_index_object_t::test<14>()
00208         {
00209                 LLString str_val("Hello.\n\r\t");
00210                 ensure("containsNonprintable failed", LLString::containsNonprintable(str_val) == TRUE);
00211 
00212                 str_val = "ABC ";
00213                 ensure("containsNonprintable failed", LLString::containsNonprintable(str_val) == FALSE);
00214         }
00215 
00216         template<> template<>
00217         void string_index_object_t::test<15>()
00218         {
00219                 LLString str_val("Hello.\n\r\t Again!");
00220                 LLString::stripNonprintable(str_val);
00221                 ensure_equals("stripNonprintable failed", str_val, "Hello. Again!");
00222 
00223                 str_val = "\r\n\t\t";
00224                 LLString::stripNonprintable(str_val);
00225                 ensure_equals("stripNonprintable resulting in empty string failed", str_val, "");
00226         }
00227 
00228         template<> template<>
00229         void string_index_object_t::test<16>()
00230         {
00231                 BOOL value;
00232                 LLString str_val("1");
00233                 ensure("convertToBOOL 1 failed", LLString::convertToBOOL(str_val, value) && value);
00234                 str_val = "T";
00235                 ensure("convertToBOOL T failed", LLString::convertToBOOL(str_val, value) && value);
00236                 str_val = "t";
00237                 ensure("convertToBOOL t failed", LLString::convertToBOOL(str_val, value) && value);
00238                 str_val = "TRUE";
00239                 ensure("convertToBOOL TRUE failed", LLString::convertToBOOL(str_val, value) && value);
00240                 str_val = "True";
00241                 ensure("convertToBOOL True failed", LLString::convertToBOOL(str_val, value) && value);
00242                 str_val = "true";
00243                 ensure("convertToBOOL true failed", LLString::convertToBOOL(str_val, value) && value);
00244 
00245                 str_val = "0";
00246                 ensure("convertToBOOL 0 failed", LLString::convertToBOOL(str_val, value) && !value);
00247                 str_val = "F";
00248                 ensure("convertToBOOL F failed", LLString::convertToBOOL(str_val, value) && !value);
00249                 str_val = "f";
00250                 ensure("convertToBOOL f failed", LLString::convertToBOOL(str_val, value) && !value);
00251                 str_val = "FALSE";
00252                 ensure("convertToBOOL FASLE failed", LLString::convertToBOOL(str_val, value) && !value);
00253                 str_val = "False";
00254                 ensure("convertToBOOL False failed", LLString::convertToBOOL(str_val, value) && !value);
00255                 str_val = "false";
00256                 ensure("convertToBOOL false failed", LLString::convertToBOOL(str_val, value) && !value);
00257 
00258                 str_val = "Tblah";
00259                 ensure("convertToBOOL false failed", !LLString::convertToBOOL(str_val, value));
00260         }
00261 
00262         template<> template<>
00263         void string_index_object_t::test<17>()
00264         {
00265                 U8 value;
00266                 LLString str_val("255");
00267                 ensure("1: convertToU8 failed", LLString::convertToU8(str_val, value) && value == 255);
00268 
00269                 str_val = "0";
00270                 ensure("2: convertToU8 failed", LLString::convertToU8(str_val, value) && value == 0);
00271 
00272                 str_val = "-1";
00273                 ensure("3: convertToU8 failed", !LLString::convertToU8(str_val, value));
00274 
00275                 str_val = "256"; // bigger than MAX_U8
00276                 ensure("4: convertToU8 failed", !LLString::convertToU8(str_val, value));
00277         }
00278 
00279         template<> template<>
00280         void string_index_object_t::test<18>()
00281         {
00282                 S8 value;
00283                 LLString str_val("127");
00284                 ensure("1: convertToS8 failed", LLString::convertToS8(str_val, value) && value == 127);
00285 
00286                 str_val = "0";
00287                 ensure("2: convertToS8 failed", LLString::convertToS8(str_val, value) && value == 0);
00288 
00289                 str_val = "-128";
00290                 ensure("3: convertToS8 failed", LLString::convertToS8(str_val, value) && value == -128);
00291 
00292                 str_val = "128"; // bigger than MAX_S8
00293                 ensure("4: convertToS8 failed", !LLString::convertToS8(str_val, value));
00294 
00295                 str_val = "-129"; 
00296                 ensure("5: convertToS8 failed", !LLString::convertToS8(str_val, value));
00297         }
00298 
00299         template<> template<>
00300         void string_index_object_t::test<19>()
00301         {
00302                 S16 value;
00303                 LLString str_val("32767"); 
00304                 ensure("1: convertToS16 failed", LLString::convertToS16(str_val, value) && value == 32767);
00305 
00306                 str_val = "0";
00307                 ensure("2: convertToS16 failed", LLString::convertToS16(str_val, value) && value == 0);
00308 
00309                 str_val = "-32768";
00310                 ensure("3: convertToS16 failed", LLString::convertToS16(str_val, value) && value == -32768);
00311 
00312                 str_val = "32768"; 
00313                 ensure("4: convertToS16 failed", !LLString::convertToS16(str_val, value));
00314 
00315                 str_val = "-32769";
00316                 ensure("5: convertToS16 failed", !LLString::convertToS16(str_val, value));
00317         }
00318 
00319         template<> template<>
00320         void string_index_object_t::test<20>()
00321         {
00322                 U16 value;
00323                 LLString str_val("65535"); //0xFFFF
00324                 ensure("1: convertToU16 failed", LLString::convertToU16(str_val, value) && value == 65535);
00325 
00326                 str_val = "0";
00327                 ensure("2: convertToU16 failed", LLString::convertToU16(str_val, value) && value == 0);
00328 
00329                 str_val = "-1"; 
00330                 ensure("3: convertToU16 failed", !LLString::convertToU16(str_val, value));
00331 
00332                 str_val = "65536"; 
00333                 ensure("4: convertToU16 failed", !LLString::convertToU16(str_val, value));
00334         }
00335 
00336         template<> template<>
00337         void string_index_object_t::test<21>()
00338         {
00339                 U32 value;
00340                 LLString str_val("4294967295"); //0xFFFFFFFF
00341                 ensure("1: convertToU32 failed", LLString::convertToU32(str_val, value) && value == 4294967295);
00342 
00343                 str_val = "0";
00344                 ensure("2: convertToU32 failed", LLString::convertToU32(str_val, value) && value == 0);
00345 
00346                 str_val = "-1"; 
00347                 ensure("3: convertToU32 failed", LLString::convertToU32(str_val, value) && value == 4294967295);
00348 
00349                 str_val = "4294967296"; 
00350                 ensure("4: convertToU32 failed", !LLString::convertToU32(str_val, value));
00351         }
00352 
00353         template<> template<>
00354         void string_index_object_t::test<22>()
00355         {
00356                 S32 value;
00357                 LLString str_val("2147483647"); //0x7FFFFFFF
00358                 ensure("1: convertToS32 failed", LLString::convertToS32(str_val, value) && value == 2147483647);
00359 
00360                 str_val = "0";
00361                 ensure("2: convertToS32 failed", LLString::convertToS32(str_val, value) && value == 0);
00362 
00363                 str_val = "-2147483648"; 
00364                 ensure("3: convertToS32 failed", LLString::convertToS32(str_val, value)  && value == -2147483648);
00365 
00366                 str_val = "2147483648"; 
00367                 ensure("4: convertToS32 failed", !LLString::convertToS32(str_val, value));
00368 
00369                 str_val = "-2147483649"; 
00370                 ensure("5: convertToS32 failed", !LLString::convertToS32(str_val, value));
00371         }
00372 
00373         template<> template<>
00374         void string_index_object_t::test<23>()
00375         {
00376                 F32 value;
00377                 LLString str_val("2147483647"); //0x7FFFFFFF
00378                 ensure("1: convertToF32 failed", LLString::convertToF32(str_val, value) && value == 2147483647);
00379 
00380                 str_val = "0";
00381                 ensure("2: convertToF32 failed", LLString::convertToF32(str_val, value) && value == 0);
00382 
00383                 /* Need to find max/min F32 values
00384                 str_val = "-2147483648"; 
00385                 ensure("3: convertToF32 failed", LLString::convertToF32(str_val, value)  && value == -2147483648);
00386 
00387                 str_val = "2147483648"; 
00388                 ensure("4: convertToF32 failed", !LLString::convertToF32(str_val, value));
00389 
00390                 str_val = "-2147483649"; 
00391                 ensure("5: convertToF32 failed", !LLString::convertToF32(str_val, value));
00392                 */
00393         }
00394 
00395         template<> template<>
00396         void string_index_object_t::test<24>()
00397         {
00398                 F64 value;
00399                 LLString str_val("9223372036854775807"); //0x7FFFFFFFFFFFFFFF
00400                 ensure("1: convertToF64 failed", LLString::convertToF64(str_val, value) && value == 9223372036854775807);
00401 
00402                 str_val = "0";
00403                 ensure("2: convertToF64 failed", LLString::convertToF64(str_val, value) && value == 0);
00404 
00405                 /* Need to find max/min F64 values
00406                 str_val = "-2147483648"; 
00407                 ensure("3: convertToF32 failed", LLString::convertToF32(str_val, value)  && value == -2147483648);
00408 
00409                 str_val = "2147483648"; 
00410                 ensure("4: convertToF32 failed", !LLString::convertToF32(str_val, value));
00411 
00412                 str_val = "-2147483649"; 
00413                 ensure("5: convertToF32 failed", !LLString::convertToF32(str_val, value));
00414                 */
00415         }
00416 
00417         template<> template<>
00418         void string_index_object_t::test<25>()
00419         {
00420                 char* str1 = NULL;
00421                 char* str2 = NULL;
00422 
00423                 ensure("1: compareStrings failed", LLString::compareStrings(str1, str2) == 0);
00424                 str2 = "A";
00425                 ensure("2: compareStrings failed", LLString::compareStrings(str1, str2) > 0);
00426                 ensure("3: compareStrings failed", LLString::compareStrings(str2, str1) < 0);
00427                 
00428                 str1 = "A is smaller than B";
00429                 str2 = "B is greater than A";
00430                 ensure("4: compareStrings failed", LLString::compareStrings(str1, str2) < 0);
00431 
00432                 str2 = "A is smaller than B";
00433                 ensure("5: compareStrings failed", LLString::compareStrings(str1, str2) == 0);
00434         }
00435 
00436         template<> template<>
00437         void string_index_object_t::test<26>()
00438         {
00439                 char* str1 = NULL;
00440                 char* str2 = NULL;
00441 
00442                 ensure("1: compareInsensitive failed", LLString::compareInsensitive(str1, str2) == 0);
00443                 str2 = "A";
00444                 ensure("2: compareInsensitive failed", LLString::compareInsensitive(str1, str2) > 0);
00445                 ensure("3: compareInsensitive failed", LLString::compareInsensitive(str2, str1) < 0);
00446                 
00447                 str1 = "A is equal to a";
00448                 str2 = "a is EQUAL to A";
00449                 ensure("4: compareInsensitive failed", LLString::compareInsensitive(str1, str2) == 0);
00450         }
00451 
00452         template<> template<>
00453         void string_index_object_t::test<27>()
00454         {
00455                 LLString lhs_str("PROgraM12files");
00456                 LLString rhs_str("PROgram12Files");
00457                 ensure("compareDict 1 failed", LLString::compareDict(lhs_str, rhs_str) < 0);
00458                 ensure("precedesDict 1 failed", LLString::precedesDict(lhs_str, rhs_str) == TRUE);
00459                 
00460                 lhs_str = "PROgram12Files";
00461                 rhs_str = "PROgram12Files";
00462                 ensure("compareDict 2 failed", LLString::compareDict(lhs_str, rhs_str) == 0);
00463                 ensure("precedesDict 2 failed", LLString::precedesDict(lhs_str, rhs_str) == FALSE);
00464 
00465                 lhs_str = "PROgram12Files";
00466                 rhs_str = "PROgRAM12FILES";
00467                 ensure("compareDict 3 failed", LLString::compareDict(lhs_str, rhs_str) > 0);
00468                 ensure("precedesDict 3 failed", LLString::precedesDict(lhs_str, rhs_str) == FALSE);
00469         }
00470 
00471         template<> template<>
00472         void string_index_object_t::test<28>()
00473         {
00474                 char str1[] = "First String...";
00475                 char str2[100];
00476 
00477                 LLString::copy(str2, str1, 100);
00478                 ensure("LLString::copy with enough dest lenght failed", strcmp(str2, str1) == 0);
00479                 LLString::copy(str2, str1, sizeof("First"));
00480                 ensure("LLString::copy with less dest lenght failed", strcmp(str2, "First") == 0);
00481         }
00482 
00483         template<> template<>
00484         void string_index_object_t::test<29>()
00485         {
00486                 LLString str1 = "This is the sentence...";
00487                 LLString str2 = "This is the ";
00488                 LLString str3 = "first ";
00489                 LLString str4 = "This is the first sentence...";
00490                 LLString str5 = "This is the sentence...first ";
00491                 LLString dest;
00492 
00493                 dest = str1;
00494                 LLString::copyInto(dest, str3, str2.length());
00495                 ensure("LLString::copyInto insert failed", dest == str4);
00496 
00497                 dest = str1;
00498                 LLString::copyInto(dest, str3, dest.length());
00499                 ensure("LLString::copyInto append failed", dest == str5);
00500         }
00501 }
00502 

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