xpcom/tests/TestStrings.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* vim:set ts=2 sw=2 et cindent: */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include "nsString.h"
     9 #include "nsStringBuffer.h"
    10 #include "nsReadableUtils.h"
    11 #include "nsCRTGlue.h"
    13 namespace TestStrings {
    15 void test_assign_helper(const nsACString& in, nsACString &_retval)
    16   {
    17     _retval = in;
    18   }
    20 bool test_assign()
    21   {
    22     nsCString result;
    23     test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
    24     bool r = strcmp(result.get(), "ab") == 0;
    25     if (!r)
    26       printf("[result=%s]\n", result.get());
    27     return r;
    28   }
    30 bool test_assign_c()
    31   {
    32     nsCString c; c.Assign('c');
    33     bool r = strcmp(c.get(), "c") == 0;
    34     if (!r)
    35       printf("[result=%s]\n", c.get());
    36     return r;
    37   }
    39 bool test1()
    40   {
    41     NS_NAMED_LITERAL_STRING(empty, "");
    42     const nsAString& aStr = empty;
    44     nsAutoString buf(aStr);
    46     int32_t n = buf.FindChar(',');
    48     n = buf.Length();
    50     buf.Cut(0, n + 1);
    51     n = buf.FindChar(',');
    53     if (n != kNotFound)
    54       printf("n=%d\n", n);
    56     return n == kNotFound;
    57   }
    59 bool test2()
    60   {
    61     nsCString data("hello world");
    62     const nsACString& aStr = data;
    64     nsCString temp(aStr);
    65     temp.Cut(0, 6);
    67     bool r = strcmp(temp.get(), "world") == 0;
    68     if (!r)
    69       printf("[temp=%s]\n", temp.get());
    70     return r;
    71   }
    73 bool test_find()
    74   {
    75     nsCString src("<!DOCTYPE blah blah blah>");
    77     int32_t i = src.Find("DOCTYPE", true, 2, 1);
    78     if (i == 2)
    79       return true;
    81     printf("i=%d\n", i);
    82     return false;
    83   }
    85 bool test_rfind()
    86   {
    87     const char text[] = "<!DOCTYPE blah blah blah>";
    88     const char term[] = "bLaH";
    89     nsCString src(text);
    90     int32_t i;
    92     i = src.RFind(term, true, 3, -1); 
    93     if (i != kNotFound)
    94       {
    95         printf("unexpected result searching from offset=3, i=%d\n", i);
    96         return false;
    97       }
    99     i = src.RFind(term, true, -1, -1);
   100     if (i != 20)
   101       {
   102         printf("unexpected result searching from offset=-1, i=%d\n", i);
   103         return false;
   104       }
   106     i = src.RFind(term, true, 13, -1);
   107     if (i != 10)
   108       {
   109         printf("unexpected result searching from offset=13, i=%d\n", i);
   110         return false;
   111       }
   113     i = src.RFind(term, true, 22, 3);
   114     if (i != 20)
   115       {
   116         printf("unexpected result searching from offset=22, i=%d\n", i);
   117         return false;
   118       }
   120     return true;
   121   }
   123 bool test_rfind_2()
   124   {
   125     const char text[] = "<!DOCTYPE blah blah blah>";
   126     nsCString src(text);
   127     int32_t i = src.RFind("TYPE", false, 5, -1); 
   128     if (i == 5)
   129       return true;
   131     printf("i=%d\n", i);
   132     return false;
   133   }
   135 bool test_rfind_3()
   136   {
   137     const char text[] = "urn:mozilla:locale:en-US:necko";
   138     nsAutoCString value(text);
   139     int32_t i = value.RFind(":");
   140     if (i == 24)
   141       return true;
   143     printf("i=%d\n", i);
   144     return false;
   145   }
   147 bool test_rfind_4()
   148   {
   149     nsCString value("a.msf");
   150     int32_t i = value.RFind(".msf");
   151     if (i != 1)
   152       {
   153         printf("i=%d\n", i);
   154         return false;
   155       }
   157     return true;
   158   }
   160 bool test_findinreadable()
   161   {
   162     const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
   163     nsAutoCString value(text);
   165     nsACString::const_iterator begin, end;
   166     value.BeginReading(begin);
   167     value.EndReading(end);
   168     nsACString::const_iterator delim_begin (begin),
   169                                delim_end   (end);
   171     // Search for last !/ at the end of the string
   172     if (!FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
   173         return false;
   174     char *r = ToNewCString(Substring(delim_begin, delim_end));
   175     // Should match the first "!/" but not the last
   176     if ((delim_end == end) || (strcmp(r, "!/")!=0))
   177       {
   178         printf("r = %s\n", r);
   179         nsMemory::Free(r);
   180         return false;
   181       }
   182     nsMemory::Free(r);
   184     delim_begin = begin;
   185     delim_end = end;
   187     // Search for first jar:
   188     if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   189         return false;
   191     r = ToNewCString(Substring(delim_begin, delim_end));
   192     // Should not match the first jar:, but the second one
   193     if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
   194       {
   195         printf("r = %s\n", r);
   196         nsMemory::Free(r);
   197         return false;
   198       }
   199     nsMemory::Free(r);
   201     // Search for jar: in a Substring
   202     delim_begin = begin; delim_begin++;
   203     delim_end = end;
   204     if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   205         return false;
   207     r = ToNewCString(Substring(delim_begin, delim_end));
   208     // Should not match the first jar:, but the second one
   209     if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
   210       {
   211         printf("r = %s\n", r);
   212         nsMemory::Free(r);
   213         return false;
   214       }
   215     nsMemory::Free(r);
   217     // Should not find a match
   218     if (FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end))
   219         return false;
   221     // When no match is found, range should be empty
   222     if (delim_begin != delim_end) 
   223         return false;
   225     // Should not find a match (search not beyond Substring)
   226     delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
   227     delim_end = end;
   228     if (FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   229         return false;
   231     // When no match is found, range should be empty
   232     if (delim_begin != delim_end) 
   233         return false;
   235     // Should not find a match (search not beyond Substring)
   236     delim_begin = begin;
   237     delim_end = end; for (int i=0;i<7;i++) delim_end--;
   238     if (FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end))
   239         return false;
   241     // When no match is found, range should be empty
   242     if (delim_begin != delim_end) 
   243         return false;
   245     return true;
   246   }
   248 bool test_rfindinreadable()
   249   {
   250     const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
   251     nsAutoCString value(text);
   253     nsACString::const_iterator begin, end;
   254     value.BeginReading(begin);
   255     value.EndReading(end);
   256     nsACString::const_iterator delim_begin (begin),
   257                                delim_end   (end);
   259     // Search for last !/ at the end of the string
   260     if (!RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
   261         return false;
   262     char *r = ToNewCString(Substring(delim_begin, delim_end));
   263     // Should match the last "!/"
   264     if ((delim_end != end) || (strcmp(r, "!/")!=0))
   265       {
   266         printf("r = %s\n", r);
   267         nsMemory::Free(r);
   268         return false;
   269       }
   270     nsMemory::Free(r);
   272     delim_begin = begin;
   273     delim_end = end;
   275     // Search for last jar: but not the first one...
   276     if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   277         return false;
   279     r = ToNewCString(Substring(delim_begin, delim_end));
   280     // Should not match the first jar:, but the second one
   281     if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
   282       {
   283         printf("r = %s\n", r);
   284         nsMemory::Free(r);
   285         return false;
   286       }
   287     nsMemory::Free(r);
   289     // Search for jar: in a Substring
   290     delim_begin = begin;
   291     delim_end = begin; for (int i=0;i<6;i++) delim_end++;
   292     if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
   293         printf("Search for jar: in a Substring\n");
   294         return false;
   295     }
   297     r = ToNewCString(Substring(delim_begin, delim_end));
   298     // Should not match the first jar:, but the second one
   299     if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
   300       {
   301         printf("r = %s\n", r);
   302         nsMemory::Free(r);
   303         return false;
   304       }
   305     nsMemory::Free(r);
   307     // Should not find a match
   308     delim_begin = begin;
   309     delim_end = end;
   310     if (RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)) {
   311         printf("Should not find a match\n");
   312         return false;
   313     }
   315     // When no match is found, range should be empty
   316     if (delim_begin != delim_end) {
   317         printf("1: When no match is found, range should be empty\n");
   318         return false;
   319     }
   321     // Should not find a match (search not before Substring)
   322     delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
   323     delim_end = end;
   324     if (RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
   325         printf("Should not find a match (search not before Substring)\n");
   326         return false;
   327     }
   329     // When no match is found, range should be empty
   330     if (delim_begin != delim_end) {
   331         printf("2: When no match is found, range should be empty\n");
   332         return false;
   333     }
   335     // Should not find a match (search not beyond Substring)
   336     delim_begin = begin;
   337     delim_end = end; for (int i=0;i<7;i++) delim_end--;
   338     if (RFindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)) {
   339         printf("Should not find a match (search not beyond Substring)\n");
   340         return false;
   341     }
   343     // When no match is found, range should be empty
   344     if (delim_begin != delim_end) {
   345         printf("3: When no match is found, range should be empty\n");
   346         return false;
   347     }
   349     return true;
   350   }
   352 bool test_distance()
   353   {
   354     const char text[] = "abc-xyz";
   355     nsCString s(text);
   356     nsCString::const_iterator begin, end;
   357     s.BeginReading(begin);
   358     s.EndReading(end);
   359     size_t d = Distance(begin, end);
   360     bool r = (d == sizeof(text)-1);
   361     if (!r)
   362       printf("d=%u\n", d);
   363     return r;
   364   }
   366 bool test_length()
   367   {
   368     const char text[] = "abc-xyz";
   369     nsCString s(text);
   370     size_t d = s.Length();
   371     bool r = (d == sizeof(text)-1);
   372     if (!r)
   373       printf("d=%u\n", d);
   374     return r;
   375   }
   377 bool test_trim()
   378   {
   379     const char text[] = " a\t    $   ";
   380     const char set[] = " \t$";
   382     nsCString s(text);
   383     s.Trim(set);
   384     bool r = strcmp(s.get(), "a") == 0;
   385     if (!r)
   386       printf("[s=%s]\n", s.get());
   387     return r;
   388   }
   390 bool test_replace_substr()
   391   {
   392     const char text[] = "abc-ppp-qqq-ppp-xyz";
   393     nsCString s(text);
   394     s.ReplaceSubstring("ppp", "www");
   395     bool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
   396     if (!r)
   397       {
   398         printf("[s=%s]\n", s.get());
   399         return false;
   400       }
   402     s.Assign("foobar");
   403     s.ReplaceSubstring("foo", "bar");
   404     s.ReplaceSubstring("bar", "");
   405     r = strcmp(s.get(), "") == 0;
   406     if (!r)
   407       {
   408         printf("[s=%s]\n", s.get());
   409         return false;
   410       }
   412     s.Assign("foofoofoo");
   413     s.ReplaceSubstring("foo", "foo");
   414     r = strcmp(s.get(), "foofoofoo") == 0;
   415     if (!r)
   416       {
   417         printf("[s=%s]\n", s.get());
   418         return false;
   419       }
   421     s.Assign("foofoofoo");
   422     s.ReplaceSubstring("of", "fo");
   423     r = strcmp(s.get(), "fofoofooo") == 0;
   424     if (!r)
   425       {
   426         printf("[s=%s]\n", s.get());
   427         return false;
   428       }
   430     return true;
   431   }
   433 bool test_replace_substr_2()
   434   {
   435     const char *oldName = nullptr;
   436     const char *newName = "user";
   437     nsString acctName; acctName.AssignLiteral("forums.foo.com");
   438     nsAutoString newAcctName, oldVal, newVal;
   439     oldVal.AssignWithConversion(oldName);
   440     newVal.AssignWithConversion(newName);
   441     newAcctName.Assign(acctName);
   443     // here, oldVal is empty.  we are testing that this function
   444     // does not hang.  see bug 235355.
   445     newAcctName.ReplaceSubstring(oldVal, newVal);
   447     // we expect that newAcctName will be unchanged.
   448     if (!newAcctName.Equals(acctName))
   449       return false;
   451     return true;
   452   }
   454 bool test_strip_ws()
   455   {
   456     const char text[] = " a    $   ";
   457     nsCString s(text);
   458     s.StripWhitespace();
   459     bool r = strcmp(s.get(), "a$") == 0;
   460     if (!r)
   461       printf("[s=%s]\n", s.get());
   462     return r;
   463   }
   465 bool test_equals_ic()
   466   {
   467     nsCString s;
   468     bool r = s.LowerCaseEqualsLiteral("view-source");
   469     if (r)
   470       printf("[r=%d]\n", r);
   471     return !r;
   472   }
   474 bool test_fixed_string()
   475   {
   476     char buf[256] = "hello world";
   478     nsFixedCString s(buf, sizeof(buf));
   480     if (s.Length() != strlen(buf))
   481       return false;
   483     if (strcmp(s.get(), buf) != 0)
   484       return false;
   486     s.Assign("foopy doopy doo");
   487     if (s.get() != buf)
   488       return false;
   490     return true;
   491   }
   493 bool test_concat()
   494   {
   495     nsCString bar("bar");
   496     const nsACString& barRef = bar;
   498     const nsPromiseFlatCString& result =
   499         PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
   500                            NS_LITERAL_CSTRING(",") +
   501                            barRef);
   502     if (strcmp(result.get(), "foo,bar") == 0)
   503       return true;
   505     printf("[result=%s]\n", result.get());
   506     return false;
   507   }
   509 bool test_concat_2()
   510   {
   511     nsCString fieldTextStr("xyz");
   512     nsCString text("text");
   513     const nsACString& aText = text;
   515     nsAutoCString result( fieldTextStr + aText );
   517     if (strcmp(result.get(), "xyztext") == 0)
   518       return true;
   520     printf("[result=%s]\n", result.get());
   521     return false;
   522   }
   524 bool test_concat_3()
   525   {
   526     nsCString result;
   527     nsCString ab("ab"), c("c");
   529     result = ab + result + c;
   530     if (strcmp(result.get(), "abc") == 0)
   531       return true;
   533     printf("[result=%s]\n", result.get());
   534     return false;
   535   }
   537 bool test_xpidl_string()
   538   {
   539     nsXPIDLCString a, b;
   540     a = b;
   541     if (a != b)
   542       return false;
   544     a.Adopt(0);
   545     if (a != b)
   546       return false;
   548     a.Append("foopy");
   549     a.Assign(b);
   550     if (a != b)
   551       return false;
   553     a.Insert("", 0);
   554     a.Assign(b);
   555     if (a != b)
   556       return false;
   558     const char text[] = "hello world";
   559     *getter_Copies(a) = NS_strdup(text);
   560     if (strcmp(a, text) != 0)
   561       return false;
   563     b = a;
   564     if (strcmp(a, b) != 0)
   565       return false;
   567     a.Adopt(0);
   568     nsACString::const_iterator begin, end;
   569     a.BeginReading(begin);
   570     a.EndReading(end);
   571     char *r = ToNewCString(Substring(begin, end));
   572     if (strcmp(r, "") != 0)
   573       return false;
   574     nsMemory::Free(r);
   576     a.Adopt(0);
   577     if (a != (const char*) 0)
   578       return false;
   580     /*
   581     int32_t index = a.FindCharInSet("xyz");
   582     if (index != kNotFound)
   583       return false;
   584     */
   586     return true;
   587   }
   589 bool test_empty_assign()
   590   {
   591     nsCString a;
   592     a.AssignLiteral("");
   594     a.AppendLiteral("");
   596     nsCString b;
   597     b.SetCapacity(0);
   598     return true;
   599   }
   601 bool test_set_length()
   602   {
   603     const char kText[] = "Default Plugin";
   604     nsCString buf;
   605     buf.SetCapacity(sizeof(kText)-1);
   606     buf.Assign(kText);
   607     buf.SetLength(sizeof(kText)-1);
   608     if (strcmp(buf.get(), kText) != 0)
   609       return false;
   610     return true;
   611   }
   613 bool test_substring()
   614   {
   615     nsCString super("hello world"), sub("hello");
   617     // this tests that |super| starts with |sub|,
   619     bool r = sub.Equals(StringHead(super, sub.Length()));
   620     if (!r)
   621       return false;
   623     // and verifies that |sub| does not start with |super|.
   625     r = super.Equals(StringHead(sub, super.Length()));
   626     if (r)
   627       return false;
   629     return true;
   630   }
   632 #define test_append(str, int, suffix) \
   633   str.Truncate(); \
   634   str.AppendInt(suffix = int ## suffix); \
   635   if (!str.EqualsLiteral(#int)) { \
   636     fputs("Error appending " #int "\n", stderr); \
   637     return false; \
   638   }
   640 #define test_appends(int, suffix) \
   641   test_append(str, int, suffix) \
   642   test_append(cstr, int, suffix)
   644 #define test_appendbase(str, prefix, int, suffix, base) \
   645   str.Truncate(); \
   646   str.AppendInt(suffix = prefix ## int ## suffix, base); \
   647   if (!str.EqualsLiteral(#int)) { \
   648     fputs("Error appending " #prefix #int "\n", stderr); \
   649     return false; \
   650   }
   652 #define test_appendbases(prefix, int, suffix, base) \
   653   test_appendbase(str, prefix, int, suffix, base) \
   654   test_appendbase(cstr, prefix, int, suffix, base)
   656 bool test_appendint()
   657   {
   658     nsString str;
   659     nsCString cstr;
   660     int32_t L;
   661     uint32_t UL;
   662     int64_t LL;
   663     uint64_t ULL;
   664     test_appends(2147483647, L)
   665     test_appends(-2147483648, L)
   666     test_appends(4294967295, UL)
   667     test_appends(9223372036854775807, LL)
   668     test_appends(-9223372036854775808, LL)
   669     test_appends(18446744073709551615, ULL)
   670     test_appendbases(0, 17777777777, L, 8)
   671     test_appendbases(0, 20000000000, L, 8)
   672     test_appendbases(0, 37777777777, UL, 8)
   673     test_appendbases(0, 777777777777777777777, LL, 8)
   674     test_appendbases(0, 1000000000000000000000, LL, 8)
   675     test_appendbases(0, 1777777777777777777777, ULL, 8)
   676     test_appendbases(0x, 7fffffff, L, 16)
   677     test_appendbases(0x, 80000000, L, 16)
   678     test_appendbases(0x, ffffffff, UL, 16)
   679     test_appendbases(0x, 7fffffffffffffff, LL, 16)
   680     test_appendbases(0x, 8000000000000000, LL, 16)
   681     test_appendbases(0x, ffffffffffffffff, ULL, 16)
   682     return true;
   683   }
   685 bool test_appendint64()
   686   {
   687     nsCString str;
   689     int64_t max = INT64_MAX;
   690     static const char max_expected[] = "9223372036854775807";
   691     int64_t min = INT64_MIN;
   692     static const char min_expected[] = "-9223372036854775808";
   693     static const char min_expected_oct[] = "1000000000000000000000";
   694     int64_t maxint_plus1 = 1LL << 32;
   695     static const char maxint_plus1_expected[] = "4294967296";
   696     static const char maxint_plus1_expected_x[] = "100000000";
   698     str.AppendInt(max);
   700     if (!str.Equals(max_expected)) {
   701       fprintf(stderr, "Error appending INT64_MAX: Got %s\n", str.get());
   702       return false;
   703     }
   705     str.Truncate();
   706     str.AppendInt(min);
   707     if (!str.Equals(min_expected)) {
   708       fprintf(stderr, "Error appending INT64_MIN: Got %s\n", str.get());
   709       return false;
   710     }
   711     str.Truncate();
   712     str.AppendInt(min, 8);
   713     if (!str.Equals(min_expected_oct)) {
   714       fprintf(stderr, "Error appending INT64_MIN (oct): Got %s\n", str.get());
   715       return false;
   716     }
   719     str.Truncate();
   720     str.AppendInt(maxint_plus1);
   721     if (!str.Equals(maxint_plus1_expected)) {
   722       fprintf(stderr, "Error appending UINT32_MAX + 1: Got %s\n", str.get());
   723       return false;
   724     }
   725     str.Truncate();
   726     str.AppendInt(maxint_plus1, 16);
   727     if (!str.Equals(maxint_plus1_expected_x)) {
   728       fprintf(stderr, "Error appending UINT32_MAX + 1 (hex): Got %s\n", str.get());
   729       return false;
   730     }
   733     return true;
   734   }
   736 bool test_appendfloat()
   737   {
   738     nsCString str;
   739     double bigdouble = 11223344556.66;
   740     static const char double_expected[] = "11223344556.66";
   741     static const char float_expected[] = "0.01";
   743     // AppendFloat is used to append doubles, therefore the precision must be
   744     // large enough (see bug 327719)
   745     str.AppendFloat( bigdouble );
   746     if (!str.Equals(double_expected)) {
   747       fprintf(stderr, "Error appending a big double: Got %s\n", str.get());
   748       return false;
   749     }
   751     str.Truncate();
   752     // AppendFloat is used to append floats (bug 327719 #27)
   753     str.AppendFloat( 0.1f * 0.1f );
   754     if (!str.Equals(float_expected)) {
   755       fprintf(stderr, "Error appending a float: Got %s\n", str.get());
   756       return false;
   757     }
   759     return true;
   760   }
   762 bool test_findcharinset()
   763   {
   764     nsCString buf("hello, how are you?");
   766     int32_t index = buf.FindCharInSet(",?", 5);
   767     if (index != 5)
   768       return false;
   770     index = buf.FindCharInSet("helo", 0);
   771     if (index != 0)
   772       return false;
   774     index = buf.FindCharInSet("z?", 6);
   775     if (index != (int32_t) buf.Length()-1)
   776       return false;
   778     return true;
   779   }
   781 bool test_rfindcharinset()
   782   {
   783     nsCString buf("hello, how are you?");
   785     int32_t index = buf.RFindCharInSet(",?", 5);
   786     if (index != 5)
   787       return false;
   789     index = buf.RFindCharInSet("helo", 0);
   790     if (index != 0)
   791       return false;
   793     index = buf.RFindCharInSet("z?", 6);
   794     if (index != kNotFound)
   795       return false;
   797     index = buf.RFindCharInSet("l", 5);
   798     if (index != 3)
   799       return false;
   801     buf.Assign("abcdefghijkabc");
   803     index = buf.RFindCharInSet("ab");
   804     if (index != 12)
   805       return false;
   807     index = buf.RFindCharInSet("ab", 11);
   808     if (index != 11)
   809       return false;
   811     index = buf.RFindCharInSet("ab", 10);
   812     if (index != 1)
   813       return false;
   815     index = buf.RFindCharInSet("ab", 0);
   816     if (index != 0)
   817       return false;
   819     index = buf.RFindCharInSet("cd", 1);
   820     if (index != kNotFound)
   821       return false;
   823     index = buf.RFindCharInSet("h");
   824     if (index != 7)
   825       return false;
   827     return true;
   828   }
   830 bool test_stringbuffer()
   831   {
   832     const char kData[] = "hello world";
   834     nsStringBuffer *buf;
   836     buf = nsStringBuffer::Alloc(sizeof(kData));
   837     if (!buf)
   838       return false;
   839     buf->Release();
   841     buf = nsStringBuffer::Alloc(sizeof(kData));
   842     if (!buf)
   843       return false;
   844     char *data = (char *) buf->Data();
   845     memcpy(data, kData, sizeof(kData));
   847     nsCString str;
   848     buf->ToString(sizeof(kData)-1, str);
   850     nsStringBuffer *buf2;
   851     buf2 = nsStringBuffer::FromString(str);
   853     bool rv = (buf == buf2);
   855     buf->Release();
   856     return rv;
   857   }
   859 bool test_voided()
   860   {
   861     const char kData[] = "hello world";
   863     nsXPIDLCString str;
   864     if (str)
   865       return false;
   866     if (!str.IsVoid())
   867       return false;
   868     if (!str.IsEmpty())
   869       return false;
   871     str.Assign(kData);
   872     if (strcmp(str, kData) != 0)
   873       return false;
   875     str.SetIsVoid(true);
   876     if (str)
   877       return false;
   878     if (!str.IsVoid())
   879       return false;
   880     if (!str.IsEmpty())
   881       return false;
   883     str.SetIsVoid(false);
   884     if (strcmp(str, "") != 0)
   885       return false;
   887     return true;
   888   }
   890 bool test_voided_autostr()
   891   {
   892     const char kData[] = "hello world";
   894     nsAutoCString str;
   895     if (str.IsVoid())
   896       return false;
   897     if (!str.IsEmpty())
   898       return false;
   900     str.Assign(kData);
   901     if (strcmp(str.get(), kData) != 0)
   902       return false;
   904     str.SetIsVoid(true);
   905     if (!str.IsVoid())
   906       return false;
   907     if (!str.IsEmpty())
   908       return false;
   910     str.Assign(kData);
   911     if (str.IsVoid())
   912       return false;
   913     if (str.IsEmpty())
   914       return false;
   915     if (strcmp(str.get(), kData) != 0)
   916       return false;
   918     return true;
   919   }
   921 bool test_voided_assignment()
   922   {
   923     nsCString a, b;
   924     b.SetIsVoid(true);
   925     a = b;
   926     return a.IsVoid() && a.get() == b.get();
   927   }
   929 bool test_empty_assignment()
   930   {
   931     nsCString a, b;
   932     a = b;
   933     return a.get() == b.get();
   934   }
   936 struct ToIntegerTest
   937 {
   938   const char *str;
   939   uint32_t radix;
   940   int32_t result;
   941   nsresult rv;
   942 };
   944 static const ToIntegerTest kToIntegerTests[] = {
   945   { "123", 10, 123, NS_OK },
   946   { "7b", 16, 123, NS_OK },
   947   { "90194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE },
   948   { nullptr, 0, 0, 0 }
   949 };
   951 bool test_string_tointeger()
   952 {
   953   int32_t i;
   954   nsresult rv;
   955   for (const ToIntegerTest* t = kToIntegerTests; t->str; ++t) {
   956     int32_t result = nsAutoCString(t->str).ToInteger(&rv, t->radix);
   957     if (rv != t->rv || result != t->result)
   958       return false;
   959     result = nsAutoCString(t->str).ToInteger(&i, t->radix);
   960     if ((nsresult)i != t->rv || result != t->result)
   961       return false;
   962   }
   963   return true;
   964 }
   966 static bool test_parse_string_helper(const char* str, char separator, int len,
   967                                        const char* s1, const char* s2)
   968 {
   969   nsCString data(str);
   970   nsTArray<nsCString> results;
   971   if (!ParseString(data, separator, results))
   972     return false;
   973   if (int(results.Length()) != len)
   974     return false;
   975   const char* strings[] = { s1, s2 };
   976   for (int i = 0; i < len; ++i) {
   977     if (!results[i].Equals(strings[i]))
   978       return false;
   979   }
   980   return true;
   981 }
   983 static bool test_parse_string_helper0(const char* str, char separator)
   984 {
   985   return test_parse_string_helper(str, separator, 0, nullptr, nullptr);
   986 }
   988 static bool test_parse_string_helper1(const char* str, char separator, const char* s1)
   989 {
   990   return test_parse_string_helper(str, separator, 1, s1, nullptr);
   991 }
   993 static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2)
   994 {
   995   return test_parse_string_helper(str, separator, 2, s1, s2);
   996 }
   998 static bool test_parse_string()
   999 {
  1000   return test_parse_string_helper1("foo, bar", '_', "foo, bar") &&
  1001          test_parse_string_helper2("foo, bar", ',', "foo", " bar") &&
  1002          test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") &&
  1003          test_parse_string_helper2("foo,bar", 'o', "f", ",bar") &&
  1004          test_parse_string_helper0("", '_') &&
  1005          test_parse_string_helper0("  ", ' ') &&
  1006          test_parse_string_helper1(" foo", ' ', "foo") &&
  1007          test_parse_string_helper1("  foo", ' ', "foo");
  1010 static bool test_strip_chars_helper(const char16_t* str, const char16_t* strip, const nsAString& result, uint32_t offset=0)
  1012   nsAutoString tmp(str);
  1013   nsAString& data = tmp;
  1014   data.StripChars(strip, offset);
  1015   return data.Equals(result);
  1018 static bool test_strip_chars()
  1020   return test_strip_chars_helper(MOZ_UTF16("foo \r \nbar"),
  1021                                  MOZ_UTF16(" \n\r"),
  1022                                  NS_LITERAL_STRING("foobar")) &&
  1023          test_strip_chars_helper(MOZ_UTF16("\r\nfoo\r\n"),
  1024                                  MOZ_UTF16(" \n\r"),
  1025                                  NS_LITERAL_STRING("foo")) &&
  1026          test_strip_chars_helper(MOZ_UTF16("foo"),
  1027                                  MOZ_UTF16(" \n\r"),
  1028                                  NS_LITERAL_STRING("foo")) &&
  1029          test_strip_chars_helper(MOZ_UTF16("foo"),
  1030                                  MOZ_UTF16("fo"),
  1031                                  NS_LITERAL_STRING("")) &&
  1032          test_strip_chars_helper(MOZ_UTF16("foo"),
  1033                                  MOZ_UTF16("foo"),
  1034                                  NS_LITERAL_STRING("")) &&
  1035          test_strip_chars_helper(MOZ_UTF16(" foo"),
  1036                                  MOZ_UTF16(" "),
  1037                                  NS_LITERAL_STRING(" foo"), 1);
  1040 static bool test_huge_capacity()
  1042   nsString a, b, c, d, e, f, g, h, i, j, k, l, m, n;
  1043   nsCString n1;
  1044   bool fail = false;
  1045 #undef ok
  1046 #define ok(x) { fail |= !(x); }
  1048   ok(a.SetCapacity(1));
  1049   ok(!a.SetCapacity(nsString::size_type(-1)/2));
  1050   ok(a.SetCapacity(0));  // free the allocated memory
  1052   ok(b.SetCapacity(1));
  1053   ok(!b.SetCapacity(nsString::size_type(-1)/2 - 1));
  1054   ok(b.SetCapacity(0));
  1056   ok(c.SetCapacity(1));
  1057   ok(!c.SetCapacity(nsString::size_type(-1)/2));
  1058   ok(c.SetCapacity(0));
  1060   ok(!d.SetCapacity(nsString::size_type(-1)/2 - 1));
  1061   ok(!d.SetCapacity(nsString::size_type(-1)/2));
  1062   ok(d.SetCapacity(0));
  1064   ok(!e.SetCapacity(nsString::size_type(-1)/4));
  1065   ok(!e.SetCapacity(nsString::size_type(-1)/4 + 1));
  1066   ok(e.SetCapacity(0));
  1068   ok(!f.SetCapacity(nsString::size_type(-1)/2));
  1069   ok(f.SetCapacity(0));
  1071   ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1000));
  1072   ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1001));
  1073   ok(g.SetCapacity(0));
  1075   ok(!h.SetCapacity(nsString::size_type(-1)/4+1));
  1076   ok(!h.SetCapacity(nsString::size_type(-1)/2));
  1077   ok(h.SetCapacity(0));
  1079   ok(i.SetCapacity(1));
  1080   ok(i.SetCapacity(nsString::size_type(-1)/4 - 1000));
  1081   ok(!i.SetCapacity(nsString::size_type(-1)/4 + 1));
  1082   ok(i.SetCapacity(0));
  1084   ok(j.SetCapacity(nsString::size_type(-1)/4 - 1000));
  1085   ok(!j.SetCapacity(nsString::size_type(-1)/4 + 1));
  1086   ok(j.SetCapacity(0));
  1088   ok(k.SetCapacity(nsString::size_type(-1)/8 - 1000));
  1089   ok(k.SetCapacity(nsString::size_type(-1)/4 - 1001));
  1090   ok(k.SetCapacity(nsString::size_type(-1)/4 - 998));
  1091   ok(!k.SetCapacity(nsString::size_type(-1)/4 + 1));
  1092   ok(k.SetCapacity(0));
  1094   ok(l.SetCapacity(nsString::size_type(-1)/8));
  1095   ok(l.SetCapacity(nsString::size_type(-1)/8 + 1));
  1096   ok(l.SetCapacity(nsString::size_type(-1)/8 + 2));
  1097   ok(l.SetCapacity(0));
  1099   ok(m.SetCapacity(nsString::size_type(-1)/8 + 1000));
  1100   ok(m.SetCapacity(nsString::size_type(-1)/8 + 1001));
  1101   ok(m.SetCapacity(0));
  1103   ok(n.SetCapacity(nsString::size_type(-1)/8+1));
  1104   ok(!n.SetCapacity(nsString::size_type(-1)/4));
  1105   ok(n.SetCapacity(0));
  1107   ok(n.SetCapacity(0));
  1108   ok(n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 2));
  1109   ok(n.SetCapacity(0));
  1110   ok(!n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 1));
  1111   ok(n.SetCapacity(0));
  1112   ok(n1.SetCapacity(0));
  1113   ok(n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 2));
  1114   ok(n1.SetCapacity(0));
  1115   ok(!n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 1));
  1116   ok(n1.SetCapacity(0));
  1118   // Ignore the result if the address space is less than 64-bit because
  1119   // some of the allocations above will exhaust the address space.  
  1120   if (sizeof(void*) >= 8) {
  1121     return !fail;
  1123   return true;
  1126 static bool test_tofloat_helper(const nsString& aStr, float aExpected, bool aSuccess)
  1128   int32_t result;
  1129   return aStr.ToFloat(&result) == aExpected &&
  1130          aSuccess ? result == NS_OK : result != NS_OK;
  1133 static bool test_tofloat()
  1135   return \
  1136     test_tofloat_helper(NS_LITERAL_STRING("42"), 42.f, true) &&
  1137     test_tofloat_helper(NS_LITERAL_STRING("42.0"), 42.f, true) &&
  1138     test_tofloat_helper(NS_LITERAL_STRING("-42"), -42.f, true) &&
  1139     test_tofloat_helper(NS_LITERAL_STRING("+42"), 42, true) &&
  1140     test_tofloat_helper(NS_LITERAL_STRING("13.37"), 13.37f, true) &&
  1141     test_tofloat_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789f, true) &&
  1142     test_tofloat_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456f, true) &&
  1143     test_tofloat_helper(NS_LITERAL_STRING("0"), 0.f, true) &&
  1144     test_tofloat_helper(NS_LITERAL_STRING("1.e5"), 100000, true) &&
  1145     test_tofloat_helper(NS_LITERAL_STRING(""), 0.f, false) &&
  1146     test_tofloat_helper(NS_LITERAL_STRING("42foo"), 42.f, false) &&
  1147     test_tofloat_helper(NS_LITERAL_STRING("foo"), 0.f, false) &&
  1148     true;
  1151 static bool test_todouble_helper(const nsString& aStr, double aExpected, bool aSuccess)
  1153   int32_t result;
  1154   return aStr.ToDouble(&result) == aExpected &&
  1155          aSuccess ? result == NS_OK : result != NS_OK;
  1158 static bool test_todouble()
  1160   return \
  1161     test_todouble_helper(NS_LITERAL_STRING("42"), 42, true) &&
  1162     test_todouble_helper(NS_LITERAL_STRING("42.0"), 42, true) &&
  1163     test_todouble_helper(NS_LITERAL_STRING("-42"), -42, true) &&
  1164     test_todouble_helper(NS_LITERAL_STRING("+42"), 42, true) &&
  1165     test_todouble_helper(NS_LITERAL_STRING("13.37"), 13.37, true) &&
  1166     test_todouble_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789, true) &&
  1167     test_todouble_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456, true) &&
  1168     test_todouble_helper(NS_LITERAL_STRING("123456789.98765432123456"), 123456789.98765432123456, true) &&
  1169     test_todouble_helper(NS_LITERAL_STRING("0"), 0, true) &&
  1170     test_todouble_helper(NS_LITERAL_STRING("1.e5"), 100000, true) &&
  1171     test_todouble_helper(NS_LITERAL_STRING(""), 0, false) &&
  1172     test_todouble_helper(NS_LITERAL_STRING("42foo"), 42, false) &&
  1173     test_todouble_helper(NS_LITERAL_STRING("foo"), 0, false) &&
  1174     true;
  1177 //----
  1179 typedef bool (*TestFunc)();
  1181 static const struct Test
  1183     const char* name;
  1184     TestFunc    func;
  1186 tests[] =
  1188     { "test_assign", test_assign },
  1189     { "test_assign_c", test_assign_c },
  1190     { "test1", test1 },
  1191     { "test2", test2 },
  1192     { "test_find", test_find },
  1193     { "test_rfind", test_rfind },
  1194     { "test_rfind_2", test_rfind_2 },
  1195     { "test_rfind_3", test_rfind_3 },
  1196     { "test_rfind_4", test_rfind_4 },
  1197     { "test_findinreadable", test_findinreadable },
  1198     { "test_rfindinreadable", test_rfindinreadable },
  1199     { "test_distance", test_distance },
  1200     { "test_length", test_length },
  1201     { "test_trim", test_trim },
  1202     { "test_replace_substr", test_replace_substr },
  1203     { "test_replace_substr_2", test_replace_substr_2 },
  1204     { "test_strip_ws", test_strip_ws },
  1205     { "test_equals_ic", test_equals_ic },
  1206     { "test_fixed_string", test_fixed_string },
  1207     { "test_concat", test_concat },
  1208     { "test_concat_2", test_concat_2 },
  1209     { "test_concat_3", test_concat_3 },
  1210     { "test_xpidl_string", test_xpidl_string },
  1211     { "test_empty_assign", test_empty_assign },
  1212     { "test_set_length", test_set_length },
  1213     { "test_substring", test_substring },
  1214     { "test_appendint", test_appendint },
  1215     { "test_appendint64", test_appendint64 },
  1216     { "test_appendfloat", test_appendfloat },
  1217     { "test_findcharinset", test_findcharinset },
  1218     { "test_rfindcharinset", test_rfindcharinset },
  1219     { "test_stringbuffer", test_stringbuffer },
  1220     { "test_voided", test_voided },
  1221     { "test_voided_autostr", test_voided_autostr },
  1222     { "test_voided_assignment", test_voided_assignment },
  1223     { "test_empty_assignment", test_empty_assignment },
  1224     { "test_string_tointeger", test_string_tointeger },
  1225     { "test_parse_string", test_parse_string },
  1226     { "test_strip_chars", test_strip_chars },
  1227     { "test_huge_capacity", test_huge_capacity },
  1228     { "test_tofloat", test_tofloat },
  1229     { "test_todouble", test_todouble },
  1230     { nullptr, nullptr }
  1231   };
  1235 using namespace TestStrings;
  1237 int main(int argc, char **argv)
  1239     int count = 1;
  1240     if (argc > 1)
  1241       count = atoi(argv[1]);
  1243     NS_LogInit();
  1245     while (count--)
  1247         for (const Test* t = tests; t->name != nullptr; ++t)
  1249             printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
  1253     return 0;

mercurial