Back to index

lightning-sunbird  0.9+nobinonly
TestStrings.cpp
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 et cindent: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by IBM Corporation are Copyright (C) 2003
00019  * IBM Corporation.  All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 #include "nsString.h"
00040 #include "nsStringBuffer.h"
00041 #include "nsReadableUtils.h"
00042 #include "nsCRT.h"
00043 
00044 void test_assign_helper(const nsACString& in, nsACString &_retval)
00045   {
00046     _retval = in;
00047   }
00048 
00049 PRBool test_assign()
00050   {
00051     nsCString result;
00052     test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
00053     PRBool r = strcmp(result.get(), "ab") == 0;
00054     if (!r)
00055       printf("[result=%s]\n", result.get());
00056     return r;
00057   }
00058 
00059 PRBool test_assign_c()
00060   {
00061     nsCString c; c.Assign('c');
00062     PRBool r = strcmp(c.get(), "c") == 0;
00063     if (!r)
00064       printf("[result=%s]\n", c.get());
00065     return r;
00066   }
00067 
00068 PRBool test1()
00069   {
00070     NS_NAMED_LITERAL_STRING(empty, "");
00071     const nsAString& aStr = empty;
00072 
00073     nsAutoString buf(aStr);
00074 
00075     PRInt32 n = buf.FindChar(',');
00076 
00077     n = buf.Length();
00078 
00079     buf.Cut(0, n + 1);
00080     n = buf.FindChar(',');
00081 
00082     if (n != kNotFound)
00083       printf("n=%d\n", n);
00084 
00085     return n == kNotFound;
00086   }
00087 
00088 PRBool test2()
00089   {
00090     nsCString data("hello world");
00091     const nsACString& aStr = data;
00092 
00093     nsCString temp(aStr);
00094     temp.Cut(0, 6);
00095 
00096     PRBool r = strcmp(temp.get(), "world") == 0;
00097     if (!r)
00098       printf("[temp=%s]\n", temp.get());
00099     return r;
00100   }
00101 
00102 PRBool test_find()
00103   {
00104     nsCString src("<!DOCTYPE blah blah blah>");
00105 
00106     PRInt32 i = src.Find("DOCTYPE", PR_TRUE, 2, 1);
00107     if (i == 2)
00108       return PR_TRUE;
00109 
00110     printf("i=%d\n", i);
00111     return PR_FALSE;
00112   }
00113 
00114 PRBool test_rfind()
00115   {
00116     const char text[] = "<!DOCTYPE blah blah blah>";
00117     const char term[] = "bLaH";
00118     nsCString src(text);
00119     PRInt32 i;
00120 
00121     i = src.RFind(term, PR_TRUE, 3, -1); 
00122     if (i != kNotFound)
00123       {
00124         printf("unexpected result searching from offset=3, i=%d\n", i);
00125         return PR_FALSE;
00126       }
00127 
00128     i = src.RFind(term, PR_TRUE, -1, -1);
00129     if (i != 20)
00130       {
00131         printf("unexpected result searching from offset=-1, i=%d\n", i);
00132         return PR_FALSE;
00133       }
00134 
00135     i = src.RFind(term, PR_TRUE, 13, -1);
00136     if (i != 10)
00137       {
00138         printf("unexpected result searching from offset=13, i=%d\n", i);
00139         return PR_FALSE;
00140       }
00141 
00142     i = src.RFind(term, PR_TRUE, 22, 3);
00143     if (i != 20)
00144       {
00145         printf("unexpected result searching from offset=22, i=%d\n", i);
00146         return PR_FALSE;
00147       }
00148 
00149     return PR_TRUE;
00150   }
00151 
00152 PRBool test_rfind_2()
00153   {
00154     const char text[] = "<!DOCTYPE blah blah blah>";
00155     nsCString src(text);
00156     PRInt32 i = src.RFind("TYPE", PR_FALSE, 5, -1); 
00157     if (i == 5)
00158       return PR_TRUE;
00159 
00160     printf("i=%d\n", i);
00161     return PR_FALSE;
00162   }
00163 
00164 PRBool test_rfind_3()
00165   {
00166     const char text[] = "urn:mozilla:locale:en-US:necko";
00167     nsCAutoString value(text);
00168     PRInt32 i = value.RFind(":");
00169     if (i == 24)
00170       return PR_TRUE;
00171 
00172     printf("i=%d\n", i);
00173     return PR_FALSE;
00174   }
00175 
00176 PRBool test_rfind_4()
00177   {
00178     nsCString value("a.msf");
00179     PRInt32 i = value.RFind(".msf");
00180     if (i != 1)
00181       {
00182         printf("i=%d\n", i);
00183         return PR_FALSE;
00184       }
00185 
00186     return PR_TRUE;
00187   }
00188 
00189 PRBool test_distance()
00190   {
00191     const char text[] = "abc-xyz";
00192     nsCString s(text);
00193     nsCString::const_iterator begin, end;
00194     s.BeginReading(begin);
00195     s.EndReading(end);
00196     size_t d = Distance(begin, end);
00197     PRBool r = (d == sizeof(text)-1);
00198     if (!r)
00199       printf("d=%u\n", d);
00200     return r;
00201   }
00202 
00203 PRBool test_length()
00204   {
00205     const char text[] = "abc-xyz";
00206     nsCString s(text);
00207     size_t d = s.Length();
00208     PRBool r = (d == sizeof(text)-1);
00209     if (!r)
00210       printf("d=%u\n", d);
00211     return r;
00212   }
00213 
00214 PRBool test_trim()
00215   {
00216     const char text[] = " a\t    $   ";
00217     const char set[] = " \t$";
00218 
00219     nsCString s(text);
00220     s.Trim(set);
00221     PRBool r = strcmp(s.get(), "a") == 0;
00222     if (!r)
00223       printf("[s=%s]\n", s.get());
00224     return r;
00225   }
00226 
00227 PRBool test_replace_substr()
00228   {
00229     const char text[] = "abc-ppp-qqq-ppp-xyz";
00230     nsCString s(text);
00231     s.ReplaceSubstring("ppp", "www");
00232     PRBool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
00233     if (!r)
00234       {
00235         printf("[s=%s]\n", s.get());
00236         return PR_FALSE;
00237       }
00238 
00239     s.Assign("foobar");
00240     s.ReplaceSubstring("foo", "bar");
00241     s.ReplaceSubstring("bar", "");
00242     r = strcmp(s.get(), "") == 0;
00243     if (!r)
00244       {
00245         printf("[s=%s]\n", s.get());
00246         return PR_FALSE;
00247       }
00248 
00249     s.Assign("foofoofoo");
00250     s.ReplaceSubstring("foo", "foo");
00251     r = strcmp(s.get(), "foofoofoo") == 0;
00252     if (!r)
00253       {
00254         printf("[s=%s]\n", s.get());
00255         return PR_FALSE;
00256       }
00257 
00258     s.Assign("foofoofoo");
00259     s.ReplaceSubstring("of", "fo");
00260     r = strcmp(s.get(), "fofoofooo") == 0;
00261     if (!r)
00262       {
00263         printf("[s=%s]\n", s.get());
00264         return PR_FALSE;
00265       }
00266 
00267     return PR_TRUE;
00268   }
00269 
00270 PRBool test_replace_substr_2()
00271   {
00272     const char *oldName = nsnull;
00273     const char *newName = "user";
00274     nsString acctName; acctName.AssignLiteral("forums.foo.com");
00275     nsAutoString newAcctName, oldVal, newVal;
00276     oldVal.AssignWithConversion(oldName);
00277     newVal.AssignWithConversion(newName);
00278     newAcctName.Assign(acctName);
00279 
00280     // here, oldVal is empty.  we are testing that this function
00281     // does not hang.  see bug 235355.
00282     newAcctName.ReplaceSubstring(oldVal, newVal);
00283 
00284     // we expect that newAcctName will be unchanged.
00285     if (!newAcctName.Equals(acctName))
00286       return PR_FALSE;
00287 
00288     return PR_TRUE;
00289   }
00290 
00291 PRBool test_strip_ws()
00292   {
00293     const char text[] = " a    $   ";
00294     nsCString s(text);
00295     s.StripWhitespace();
00296     PRBool r = strcmp(s.get(), "a$") == 0;
00297     if (!r)
00298       printf("[s=%s]\n", s.get());
00299     return r;
00300   }
00301 
00302 PRBool test_equals_ic()
00303   {
00304     nsCString s;
00305     PRBool r = s.LowerCaseEqualsLiteral("view-source");
00306     if (r)
00307       printf("[r=%d]\n", r);
00308     return !r;
00309   }
00310 
00311 PRBool test_fixed_string()
00312   {
00313     char buf[256] = "hello world";
00314 
00315     nsFixedCString s(buf, sizeof(buf));
00316 
00317     if (s.Length() != strlen(buf))
00318       return PR_FALSE;
00319 
00320     if (strcmp(s.get(), buf) != 0)
00321       return PR_FALSE;
00322 
00323     s.Assign("foopy doopy doo");
00324     if (s.get() != buf)
00325       return PR_FALSE;
00326     
00327     return PR_TRUE;
00328   }
00329 
00330 PRBool test_concat()
00331   {
00332     nsCString bar("bar");
00333     const nsACString& barRef = bar;
00334 
00335     const nsPromiseFlatCString& result =
00336         PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
00337                            NS_LITERAL_CSTRING(",") +
00338                            barRef);
00339     if (strcmp(result.get(), "foo,bar") == 0)
00340       return PR_TRUE;
00341 
00342     printf("[result=%s]\n", result.get());
00343     return PR_FALSE;
00344   }
00345 
00346 PRBool test_concat_2()
00347   {
00348     nsCString fieldTextStr("xyz");
00349     nsCString text("text");
00350     const nsACString& aText = text;
00351 
00352     nsCAutoString result( fieldTextStr + aText );
00353 
00354     if (strcmp(result.get(), "xyztext") == 0)
00355       return PR_TRUE;
00356     
00357     printf("[result=%s]\n", result.get());
00358     return PR_FALSE;
00359   }
00360 
00361 #if 0
00362 PRBool test_concat_3()
00363   {
00364     nsCString a("a"), b("b");
00365 
00366     // THIS DOES NOT COMPILE
00367     const nsACString& r = a + b;
00368 
00369     return PR_TRUE;
00370   }
00371 #endif
00372 
00373 PRBool test_xpidl_string()
00374   {
00375     nsXPIDLCString a, b;
00376     a = b;
00377     if (a != b)
00378       return PR_FALSE;
00379 
00380     a.Adopt(0);
00381     if (a != b)
00382       return PR_FALSE;
00383 
00384     a.Append("foopy");
00385     a.Assign(b);
00386     if (a != b)
00387       return PR_FALSE;
00388 
00389     a.Insert("", 0);
00390     a.Assign(b);
00391     if (a != b)
00392       return PR_FALSE;
00393 
00394     const char text[] = "hello world";
00395     *getter_Copies(a) = nsCRT::strdup(text);
00396     if (strcmp(a, text) != 0)
00397       return PR_FALSE;
00398 
00399     b = a;
00400     if (strcmp(a, b) != 0)
00401       return PR_FALSE;
00402 
00403     a.Adopt(0);
00404     nsACString::const_iterator begin, end;
00405     a.BeginReading(begin);
00406     a.EndReading(end);
00407     char *r = ToNewCString(Substring(begin, end));
00408     if (strcmp(r, "") != 0)
00409       return PR_FALSE;
00410     nsMemory::Free(r);
00411 
00412     a.Adopt(0);
00413     if (a != (const char*) 0)
00414       return PR_FALSE;
00415 
00416     /*
00417     PRInt32 index = a.FindCharInSet("xyz");
00418     if (index != kNotFound)
00419       return PR_FALSE;
00420     */
00421 
00422     return PR_TRUE;
00423   }
00424 
00425 PRBool test_empty_assign()
00426   {
00427     nsCString a;
00428     a.AssignLiteral("");
00429 
00430     a.AppendLiteral("");
00431 
00432     nsCString b;
00433     b.SetCapacity(0);
00434     return PR_TRUE;
00435   }
00436 
00437 PRBool test_set_length()
00438   {
00439     const char kText[] = "Default Plugin";
00440     nsCString buf;
00441     buf.SetCapacity(sizeof(kText)-1);
00442     buf.Assign(kText);
00443     buf.SetLength(sizeof(kText)-1);
00444     if (strcmp(buf.get(), kText) != 0)
00445       return PR_FALSE;
00446     return PR_TRUE;
00447   }
00448 
00449 PRBool test_substring()
00450   {
00451     nsCString super("hello world"), sub("hello");
00452 
00453     // this tests that |super| starts with |sub|,
00454     
00455     PRBool r = sub.Equals(StringHead(super, sub.Length()));
00456     if (!r)
00457       return PR_FALSE;
00458 
00459     // and verifies that |sub| does not start with |super|.
00460 
00461     r = super.Equals(StringHead(sub, super.Length()));
00462     if (r)
00463       return PR_FALSE;
00464 
00465     return PR_TRUE;
00466   }
00467 
00468 PRBool test_appendint64()
00469   {
00470     nsCString str;
00471 
00472     PRInt64 max = LL_MaxInt();
00473     static const char max_expected[] = "9223372036854775807";
00474     PRInt64 min = LL_MinInt();
00475     static const char min_expected[] = "-9223372036854775808";
00476     static const char min_expected_oct[] = "1000000000000000000000";
00477     PRInt64 maxint_plus1 = LL_INIT(1, 0);
00478     static const char maxint_plus1_expected[] = "4294967296";
00479     static const char maxint_plus1_expected_x[] = "100000000";
00480 
00481     str.AppendInt(max);
00482 
00483     if (!str.Equals(max_expected)) {
00484       fprintf(stderr, "Error appending LL_MaxInt(): Got %s\n", str.get());
00485       return PR_FALSE;
00486     }
00487 
00488     str.Truncate();
00489     str.AppendInt(min);
00490     if (!str.Equals(min_expected)) {
00491       fprintf(stderr, "Error appending LL_MinInt(): Got %s\n", str.get());
00492       return PR_FALSE;
00493     }
00494     str.Truncate();
00495     str.AppendInt(min, 8);
00496     if (!str.Equals(min_expected_oct)) {
00497       fprintf(stderr, "Error appending LL_MinInt() (oct): Got %s\n", str.get());
00498       return PR_FALSE;
00499     }
00500 
00501 
00502     str.Truncate();
00503     str.AppendInt(maxint_plus1);
00504     if (!str.Equals(maxint_plus1_expected)) {
00505       fprintf(stderr, "Error appending PR_UINT32_MAX + 1: Got %s\n", str.get());
00506       return PR_FALSE;
00507     }
00508     str.Truncate();
00509     str.AppendInt(maxint_plus1, 16);
00510     if (!str.Equals(maxint_plus1_expected_x)) {
00511       fprintf(stderr, "Error appending PR_UINT32_MAX + 1 (hex): Got %s\n", str.get());
00512       return PR_FALSE;
00513     }
00514 
00515 
00516     return PR_TRUE;
00517   }
00518 
00519 PRBool test_findcharinset()
00520   {
00521     nsCString buf("hello, how are you?");
00522 
00523     PRInt32 index = buf.FindCharInSet(",?", 5);
00524     if (index != 5)
00525       return PR_FALSE;
00526 
00527     index = buf.FindCharInSet("helo", 0);
00528     if (index != 0)
00529       return PR_FALSE;
00530 
00531     index = buf.FindCharInSet("z?", 6);
00532     if (index != (PRInt32) buf.Length()-1)
00533       return PR_FALSE;
00534 
00535     return PR_TRUE;
00536   }
00537 
00538 PRBool test_rfindcharinset()
00539   {
00540     nsCString buf("hello, how are you?");
00541 
00542     PRInt32 index = buf.RFindCharInSet(",?", 5);
00543     if (index != 5)
00544       return PR_FALSE;
00545 
00546     index = buf.RFindCharInSet("helo", 0);
00547     if (index != 0)
00548       return PR_FALSE;
00549 
00550     index = buf.RFindCharInSet("z?", 6);
00551     if (index != kNotFound)
00552       return PR_FALSE;
00553 
00554     index = buf.RFindCharInSet("l", 5);
00555     if (index != 3)
00556       return PR_FALSE;
00557 
00558     buf.Assign("abcdefghijkabc");
00559 
00560     index = buf.RFindCharInSet("ab");
00561     if (index != 12)
00562       return PR_FALSE;
00563 
00564     index = buf.RFindCharInSet("ab", 11);
00565     if (index != 11)
00566       return PR_FALSE;
00567 
00568     index = buf.RFindCharInSet("ab", 10);
00569     if (index != 1)
00570       return PR_FALSE;
00571 
00572     index = buf.RFindCharInSet("ab", 0);
00573     if (index != 0)
00574       return PR_FALSE;
00575 
00576     index = buf.RFindCharInSet("cd", 1);
00577     if (index != kNotFound)
00578       return PR_FALSE;
00579 
00580     index = buf.RFindCharInSet("h");
00581     if (index != 7)
00582       return PR_FALSE;
00583 
00584     return PR_TRUE;
00585   }
00586 
00587 PRBool test_stringbuffer()
00588   {
00589     const char kData[] = "hello world";
00590 
00591     nsStringBuffer *buf;
00592     
00593     buf = nsStringBuffer::Alloc(sizeof(kData));
00594     if (!buf)
00595       return PR_FALSE;
00596     buf->Release();
00597  
00598     buf = nsStringBuffer::Alloc(sizeof(kData));
00599     if (!buf)
00600       return PR_FALSE;
00601     char *data = (char *) buf->Data();
00602     memcpy(data, kData, sizeof(kData));
00603 
00604     nsCString str;
00605     buf->ToString(sizeof(kData)-1, str);
00606 
00607     nsStringBuffer *buf2;
00608     buf2 = nsStringBuffer::FromString(str);
00609 
00610     PRBool rv = (buf == buf2);
00611 
00612     buf->Release();
00613     return rv;
00614   }
00615 
00616 //----
00617 
00618 typedef PRBool (*TestFunc)();
00619 
00620 static const struct Test
00621   {
00622     const char* name;
00623     TestFunc    func;
00624   }
00625 tests[] =
00626   {
00627     { "test_assign", test_assign },
00628     { "test_assign_c", test_assign_c },
00629     { "test1", test1 },
00630     { "test2", test2 },
00631     { "test_find", test_find },
00632     { "test_rfind", test_rfind },
00633     { "test_rfind_2", test_rfind_2 },
00634     { "test_rfind_3", test_rfind_3 },
00635     { "test_rfind_4", test_rfind_4 },
00636     { "test_distance", test_distance },
00637     { "test_length", test_length },
00638     { "test_trim", test_trim },
00639     { "test_replace_substr", test_replace_substr },
00640     { "test_replace_substr_2", test_replace_substr_2 },
00641     { "test_strip_ws", test_strip_ws },
00642     { "test_equals_ic", test_equals_ic },
00643     { "test_fixed_string", test_fixed_string },
00644     { "test_concat", test_concat },
00645     { "test_concat_2", test_concat_2 },
00646     { "test_xpidl_string", test_xpidl_string },
00647     { "test_empty_assign", test_empty_assign },
00648     { "test_set_length", test_set_length },
00649     { "test_substring", test_substring },
00650     { "test_appendint64", test_appendint64 },
00651     { "test_findcharinset", test_findcharinset },
00652     { "test_rfindcharinset", test_rfindcharinset },
00653     { "test_stringbuffer", test_stringbuffer },
00654     { nsnull, nsnull }
00655   };
00656 
00657 //----
00658 
00659 int main(int argc, char **argv)
00660   {
00661     int count = 1;
00662     if (argc > 1)
00663       count = atoi(argv[1]);
00664 
00665     while (count--)
00666       {
00667         for (const Test* t = tests; t->name != nsnull; ++t)
00668           {
00669             printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
00670           }
00671       }
00672     
00673     return 0;
00674   }