xpcom/tests/TestStrings.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestStrings.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1254 @@
     1.4 +/* vim:set ts=2 sw=2 et cindent: */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include "nsString.h"
    1.12 +#include "nsStringBuffer.h"
    1.13 +#include "nsReadableUtils.h"
    1.14 +#include "nsCRTGlue.h"
    1.15 +
    1.16 +namespace TestStrings {
    1.17 +
    1.18 +void test_assign_helper(const nsACString& in, nsACString &_retval)
    1.19 +  {
    1.20 +    _retval = in;
    1.21 +  }
    1.22 +
    1.23 +bool test_assign()
    1.24 +  {
    1.25 +    nsCString result;
    1.26 +    test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
    1.27 +    bool r = strcmp(result.get(), "ab") == 0;
    1.28 +    if (!r)
    1.29 +      printf("[result=%s]\n", result.get());
    1.30 +    return r;
    1.31 +  }
    1.32 +
    1.33 +bool test_assign_c()
    1.34 +  {
    1.35 +    nsCString c; c.Assign('c');
    1.36 +    bool r = strcmp(c.get(), "c") == 0;
    1.37 +    if (!r)
    1.38 +      printf("[result=%s]\n", c.get());
    1.39 +    return r;
    1.40 +  }
    1.41 +
    1.42 +bool test1()
    1.43 +  {
    1.44 +    NS_NAMED_LITERAL_STRING(empty, "");
    1.45 +    const nsAString& aStr = empty;
    1.46 +
    1.47 +    nsAutoString buf(aStr);
    1.48 +
    1.49 +    int32_t n = buf.FindChar(',');
    1.50 +
    1.51 +    n = buf.Length();
    1.52 +
    1.53 +    buf.Cut(0, n + 1);
    1.54 +    n = buf.FindChar(',');
    1.55 +
    1.56 +    if (n != kNotFound)
    1.57 +      printf("n=%d\n", n);
    1.58 +
    1.59 +    return n == kNotFound;
    1.60 +  }
    1.61 +
    1.62 +bool test2()
    1.63 +  {
    1.64 +    nsCString data("hello world");
    1.65 +    const nsACString& aStr = data;
    1.66 +
    1.67 +    nsCString temp(aStr);
    1.68 +    temp.Cut(0, 6);
    1.69 +
    1.70 +    bool r = strcmp(temp.get(), "world") == 0;
    1.71 +    if (!r)
    1.72 +      printf("[temp=%s]\n", temp.get());
    1.73 +    return r;
    1.74 +  }
    1.75 +
    1.76 +bool test_find()
    1.77 +  {
    1.78 +    nsCString src("<!DOCTYPE blah blah blah>");
    1.79 +
    1.80 +    int32_t i = src.Find("DOCTYPE", true, 2, 1);
    1.81 +    if (i == 2)
    1.82 +      return true;
    1.83 +
    1.84 +    printf("i=%d\n", i);
    1.85 +    return false;
    1.86 +  }
    1.87 +
    1.88 +bool test_rfind()
    1.89 +  {
    1.90 +    const char text[] = "<!DOCTYPE blah blah blah>";
    1.91 +    const char term[] = "bLaH";
    1.92 +    nsCString src(text);
    1.93 +    int32_t i;
    1.94 +
    1.95 +    i = src.RFind(term, true, 3, -1); 
    1.96 +    if (i != kNotFound)
    1.97 +      {
    1.98 +        printf("unexpected result searching from offset=3, i=%d\n", i);
    1.99 +        return false;
   1.100 +      }
   1.101 +
   1.102 +    i = src.RFind(term, true, -1, -1);
   1.103 +    if (i != 20)
   1.104 +      {
   1.105 +        printf("unexpected result searching from offset=-1, i=%d\n", i);
   1.106 +        return false;
   1.107 +      }
   1.108 +
   1.109 +    i = src.RFind(term, true, 13, -1);
   1.110 +    if (i != 10)
   1.111 +      {
   1.112 +        printf("unexpected result searching from offset=13, i=%d\n", i);
   1.113 +        return false;
   1.114 +      }
   1.115 +
   1.116 +    i = src.RFind(term, true, 22, 3);
   1.117 +    if (i != 20)
   1.118 +      {
   1.119 +        printf("unexpected result searching from offset=22, i=%d\n", i);
   1.120 +        return false;
   1.121 +      }
   1.122 +
   1.123 +    return true;
   1.124 +  }
   1.125 +
   1.126 +bool test_rfind_2()
   1.127 +  {
   1.128 +    const char text[] = "<!DOCTYPE blah blah blah>";
   1.129 +    nsCString src(text);
   1.130 +    int32_t i = src.RFind("TYPE", false, 5, -1); 
   1.131 +    if (i == 5)
   1.132 +      return true;
   1.133 +
   1.134 +    printf("i=%d\n", i);
   1.135 +    return false;
   1.136 +  }
   1.137 +
   1.138 +bool test_rfind_3()
   1.139 +  {
   1.140 +    const char text[] = "urn:mozilla:locale:en-US:necko";
   1.141 +    nsAutoCString value(text);
   1.142 +    int32_t i = value.RFind(":");
   1.143 +    if (i == 24)
   1.144 +      return true;
   1.145 +
   1.146 +    printf("i=%d\n", i);
   1.147 +    return false;
   1.148 +  }
   1.149 +
   1.150 +bool test_rfind_4()
   1.151 +  {
   1.152 +    nsCString value("a.msf");
   1.153 +    int32_t i = value.RFind(".msf");
   1.154 +    if (i != 1)
   1.155 +      {
   1.156 +        printf("i=%d\n", i);
   1.157 +        return false;
   1.158 +      }
   1.159 +
   1.160 +    return true;
   1.161 +  }
   1.162 +
   1.163 +bool test_findinreadable()
   1.164 +  {
   1.165 +    const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
   1.166 +    nsAutoCString value(text);
   1.167 +
   1.168 +    nsACString::const_iterator begin, end;
   1.169 +    value.BeginReading(begin);
   1.170 +    value.EndReading(end);
   1.171 +    nsACString::const_iterator delim_begin (begin),
   1.172 +                               delim_end   (end);
   1.173 +
   1.174 +    // Search for last !/ at the end of the string
   1.175 +    if (!FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
   1.176 +        return false;
   1.177 +    char *r = ToNewCString(Substring(delim_begin, delim_end));
   1.178 +    // Should match the first "!/" but not the last
   1.179 +    if ((delim_end == end) || (strcmp(r, "!/")!=0))
   1.180 +      {
   1.181 +        printf("r = %s\n", r);
   1.182 +        nsMemory::Free(r);
   1.183 +        return false;
   1.184 +      }
   1.185 +    nsMemory::Free(r);
   1.186 +
   1.187 +    delim_begin = begin;
   1.188 +    delim_end = end;
   1.189 +
   1.190 +    // Search for first jar:
   1.191 +    if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   1.192 +        return false;
   1.193 +
   1.194 +    r = ToNewCString(Substring(delim_begin, delim_end));
   1.195 +    // Should not match the first jar:, but the second one
   1.196 +    if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
   1.197 +      {
   1.198 +        printf("r = %s\n", r);
   1.199 +        nsMemory::Free(r);
   1.200 +        return false;
   1.201 +      }
   1.202 +    nsMemory::Free(r);
   1.203 +
   1.204 +    // Search for jar: in a Substring
   1.205 +    delim_begin = begin; delim_begin++;
   1.206 +    delim_end = end;
   1.207 +    if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   1.208 +        return false;
   1.209 +
   1.210 +    r = ToNewCString(Substring(delim_begin, delim_end));
   1.211 +    // Should not match the first jar:, but the second one
   1.212 +    if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
   1.213 +      {
   1.214 +        printf("r = %s\n", r);
   1.215 +        nsMemory::Free(r);
   1.216 +        return false;
   1.217 +      }
   1.218 +    nsMemory::Free(r);
   1.219 +
   1.220 +    // Should not find a match
   1.221 +    if (FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end))
   1.222 +        return false;
   1.223 +
   1.224 +    // When no match is found, range should be empty
   1.225 +    if (delim_begin != delim_end) 
   1.226 +        return false;
   1.227 +
   1.228 +    // Should not find a match (search not beyond Substring)
   1.229 +    delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
   1.230 +    delim_end = end;
   1.231 +    if (FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   1.232 +        return false;
   1.233 +
   1.234 +    // When no match is found, range should be empty
   1.235 +    if (delim_begin != delim_end) 
   1.236 +        return false;
   1.237 +
   1.238 +    // Should not find a match (search not beyond Substring)
   1.239 +    delim_begin = begin;
   1.240 +    delim_end = end; for (int i=0;i<7;i++) delim_end--;
   1.241 +    if (FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end))
   1.242 +        return false;
   1.243 +
   1.244 +    // When no match is found, range should be empty
   1.245 +    if (delim_begin != delim_end) 
   1.246 +        return false;
   1.247 +
   1.248 +    return true;
   1.249 +  }
   1.250 +
   1.251 +bool test_rfindinreadable()
   1.252 +  {
   1.253 +    const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
   1.254 +    nsAutoCString value(text);
   1.255 +
   1.256 +    nsACString::const_iterator begin, end;
   1.257 +    value.BeginReading(begin);
   1.258 +    value.EndReading(end);
   1.259 +    nsACString::const_iterator delim_begin (begin),
   1.260 +                               delim_end   (end);
   1.261 +
   1.262 +    // Search for last !/ at the end of the string
   1.263 +    if (!RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
   1.264 +        return false;
   1.265 +    char *r = ToNewCString(Substring(delim_begin, delim_end));
   1.266 +    // Should match the last "!/"
   1.267 +    if ((delim_end != end) || (strcmp(r, "!/")!=0))
   1.268 +      {
   1.269 +        printf("r = %s\n", r);
   1.270 +        nsMemory::Free(r);
   1.271 +        return false;
   1.272 +      }
   1.273 +    nsMemory::Free(r);
   1.274 +
   1.275 +    delim_begin = begin;
   1.276 +    delim_end = end;
   1.277 +
   1.278 +    // Search for last jar: but not the first one...
   1.279 +    if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
   1.280 +        return false;
   1.281 +
   1.282 +    r = ToNewCString(Substring(delim_begin, delim_end));
   1.283 +    // Should not match the first jar:, but the second one
   1.284 +    if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
   1.285 +      {
   1.286 +        printf("r = %s\n", r);
   1.287 +        nsMemory::Free(r);
   1.288 +        return false;
   1.289 +      }
   1.290 +    nsMemory::Free(r);
   1.291 +
   1.292 +    // Search for jar: in a Substring
   1.293 +    delim_begin = begin;
   1.294 +    delim_end = begin; for (int i=0;i<6;i++) delim_end++;
   1.295 +    if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
   1.296 +        printf("Search for jar: in a Substring\n");
   1.297 +        return false;
   1.298 +    }
   1.299 +
   1.300 +    r = ToNewCString(Substring(delim_begin, delim_end));
   1.301 +    // Should not match the first jar:, but the second one
   1.302 +    if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
   1.303 +      {
   1.304 +        printf("r = %s\n", r);
   1.305 +        nsMemory::Free(r);
   1.306 +        return false;
   1.307 +      }
   1.308 +    nsMemory::Free(r);
   1.309 +
   1.310 +    // Should not find a match
   1.311 +    delim_begin = begin;
   1.312 +    delim_end = end;
   1.313 +    if (RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)) {
   1.314 +        printf("Should not find a match\n");
   1.315 +        return false;
   1.316 +    }
   1.317 +
   1.318 +    // When no match is found, range should be empty
   1.319 +    if (delim_begin != delim_end) {
   1.320 +        printf("1: When no match is found, range should be empty\n");
   1.321 +        return false;
   1.322 +    }
   1.323 +
   1.324 +    // Should not find a match (search not before Substring)
   1.325 +    delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
   1.326 +    delim_end = end;
   1.327 +    if (RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
   1.328 +        printf("Should not find a match (search not before Substring)\n");
   1.329 +        return false;
   1.330 +    }
   1.331 +
   1.332 +    // When no match is found, range should be empty
   1.333 +    if (delim_begin != delim_end) {
   1.334 +        printf("2: When no match is found, range should be empty\n");
   1.335 +        return false;
   1.336 +    }
   1.337 +
   1.338 +    // Should not find a match (search not beyond Substring)
   1.339 +    delim_begin = begin;
   1.340 +    delim_end = end; for (int i=0;i<7;i++) delim_end--;
   1.341 +    if (RFindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)) {
   1.342 +        printf("Should not find a match (search not beyond Substring)\n");
   1.343 +        return false;
   1.344 +    }
   1.345 +
   1.346 +    // When no match is found, range should be empty
   1.347 +    if (delim_begin != delim_end) {
   1.348 +        printf("3: When no match is found, range should be empty\n");
   1.349 +        return false;
   1.350 +    }
   1.351 +
   1.352 +    return true;
   1.353 +  }
   1.354 +
   1.355 +bool test_distance()
   1.356 +  {
   1.357 +    const char text[] = "abc-xyz";
   1.358 +    nsCString s(text);
   1.359 +    nsCString::const_iterator begin, end;
   1.360 +    s.BeginReading(begin);
   1.361 +    s.EndReading(end);
   1.362 +    size_t d = Distance(begin, end);
   1.363 +    bool r = (d == sizeof(text)-1);
   1.364 +    if (!r)
   1.365 +      printf("d=%u\n", d);
   1.366 +    return r;
   1.367 +  }
   1.368 +
   1.369 +bool test_length()
   1.370 +  {
   1.371 +    const char text[] = "abc-xyz";
   1.372 +    nsCString s(text);
   1.373 +    size_t d = s.Length();
   1.374 +    bool r = (d == sizeof(text)-1);
   1.375 +    if (!r)
   1.376 +      printf("d=%u\n", d);
   1.377 +    return r;
   1.378 +  }
   1.379 +
   1.380 +bool test_trim()
   1.381 +  {
   1.382 +    const char text[] = " a\t    $   ";
   1.383 +    const char set[] = " \t$";
   1.384 +
   1.385 +    nsCString s(text);
   1.386 +    s.Trim(set);
   1.387 +    bool r = strcmp(s.get(), "a") == 0;
   1.388 +    if (!r)
   1.389 +      printf("[s=%s]\n", s.get());
   1.390 +    return r;
   1.391 +  }
   1.392 +
   1.393 +bool test_replace_substr()
   1.394 +  {
   1.395 +    const char text[] = "abc-ppp-qqq-ppp-xyz";
   1.396 +    nsCString s(text);
   1.397 +    s.ReplaceSubstring("ppp", "www");
   1.398 +    bool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
   1.399 +    if (!r)
   1.400 +      {
   1.401 +        printf("[s=%s]\n", s.get());
   1.402 +        return false;
   1.403 +      }
   1.404 +
   1.405 +    s.Assign("foobar");
   1.406 +    s.ReplaceSubstring("foo", "bar");
   1.407 +    s.ReplaceSubstring("bar", "");
   1.408 +    r = strcmp(s.get(), "") == 0;
   1.409 +    if (!r)
   1.410 +      {
   1.411 +        printf("[s=%s]\n", s.get());
   1.412 +        return false;
   1.413 +      }
   1.414 +
   1.415 +    s.Assign("foofoofoo");
   1.416 +    s.ReplaceSubstring("foo", "foo");
   1.417 +    r = strcmp(s.get(), "foofoofoo") == 0;
   1.418 +    if (!r)
   1.419 +      {
   1.420 +        printf("[s=%s]\n", s.get());
   1.421 +        return false;
   1.422 +      }
   1.423 +
   1.424 +    s.Assign("foofoofoo");
   1.425 +    s.ReplaceSubstring("of", "fo");
   1.426 +    r = strcmp(s.get(), "fofoofooo") == 0;
   1.427 +    if (!r)
   1.428 +      {
   1.429 +        printf("[s=%s]\n", s.get());
   1.430 +        return false;
   1.431 +      }
   1.432 +
   1.433 +    return true;
   1.434 +  }
   1.435 +
   1.436 +bool test_replace_substr_2()
   1.437 +  {
   1.438 +    const char *oldName = nullptr;
   1.439 +    const char *newName = "user";
   1.440 +    nsString acctName; acctName.AssignLiteral("forums.foo.com");
   1.441 +    nsAutoString newAcctName, oldVal, newVal;
   1.442 +    oldVal.AssignWithConversion(oldName);
   1.443 +    newVal.AssignWithConversion(newName);
   1.444 +    newAcctName.Assign(acctName);
   1.445 +
   1.446 +    // here, oldVal is empty.  we are testing that this function
   1.447 +    // does not hang.  see bug 235355.
   1.448 +    newAcctName.ReplaceSubstring(oldVal, newVal);
   1.449 +
   1.450 +    // we expect that newAcctName will be unchanged.
   1.451 +    if (!newAcctName.Equals(acctName))
   1.452 +      return false;
   1.453 +
   1.454 +    return true;
   1.455 +  }
   1.456 +
   1.457 +bool test_strip_ws()
   1.458 +  {
   1.459 +    const char text[] = " a    $   ";
   1.460 +    nsCString s(text);
   1.461 +    s.StripWhitespace();
   1.462 +    bool r = strcmp(s.get(), "a$") == 0;
   1.463 +    if (!r)
   1.464 +      printf("[s=%s]\n", s.get());
   1.465 +    return r;
   1.466 +  }
   1.467 +
   1.468 +bool test_equals_ic()
   1.469 +  {
   1.470 +    nsCString s;
   1.471 +    bool r = s.LowerCaseEqualsLiteral("view-source");
   1.472 +    if (r)
   1.473 +      printf("[r=%d]\n", r);
   1.474 +    return !r;
   1.475 +  }
   1.476 +
   1.477 +bool test_fixed_string()
   1.478 +  {
   1.479 +    char buf[256] = "hello world";
   1.480 +
   1.481 +    nsFixedCString s(buf, sizeof(buf));
   1.482 +
   1.483 +    if (s.Length() != strlen(buf))
   1.484 +      return false;
   1.485 +
   1.486 +    if (strcmp(s.get(), buf) != 0)
   1.487 +      return false;
   1.488 +
   1.489 +    s.Assign("foopy doopy doo");
   1.490 +    if (s.get() != buf)
   1.491 +      return false;
   1.492 +    
   1.493 +    return true;
   1.494 +  }
   1.495 +
   1.496 +bool test_concat()
   1.497 +  {
   1.498 +    nsCString bar("bar");
   1.499 +    const nsACString& barRef = bar;
   1.500 +
   1.501 +    const nsPromiseFlatCString& result =
   1.502 +        PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
   1.503 +                           NS_LITERAL_CSTRING(",") +
   1.504 +                           barRef);
   1.505 +    if (strcmp(result.get(), "foo,bar") == 0)
   1.506 +      return true;
   1.507 +
   1.508 +    printf("[result=%s]\n", result.get());
   1.509 +    return false;
   1.510 +  }
   1.511 +
   1.512 +bool test_concat_2()
   1.513 +  {
   1.514 +    nsCString fieldTextStr("xyz");
   1.515 +    nsCString text("text");
   1.516 +    const nsACString& aText = text;
   1.517 +
   1.518 +    nsAutoCString result( fieldTextStr + aText );
   1.519 +
   1.520 +    if (strcmp(result.get(), "xyztext") == 0)
   1.521 +      return true;
   1.522 +    
   1.523 +    printf("[result=%s]\n", result.get());
   1.524 +    return false;
   1.525 +  }
   1.526 +
   1.527 +bool test_concat_3()
   1.528 +  {
   1.529 +    nsCString result;
   1.530 +    nsCString ab("ab"), c("c");
   1.531 +
   1.532 +    result = ab + result + c;
   1.533 +    if (strcmp(result.get(), "abc") == 0)
   1.534 +      return true;
   1.535 +
   1.536 +    printf("[result=%s]\n", result.get());
   1.537 +    return false;
   1.538 +  }
   1.539 +
   1.540 +bool test_xpidl_string()
   1.541 +  {
   1.542 +    nsXPIDLCString a, b;
   1.543 +    a = b;
   1.544 +    if (a != b)
   1.545 +      return false;
   1.546 +
   1.547 +    a.Adopt(0);
   1.548 +    if (a != b)
   1.549 +      return false;
   1.550 +
   1.551 +    a.Append("foopy");
   1.552 +    a.Assign(b);
   1.553 +    if (a != b)
   1.554 +      return false;
   1.555 +
   1.556 +    a.Insert("", 0);
   1.557 +    a.Assign(b);
   1.558 +    if (a != b)
   1.559 +      return false;
   1.560 +
   1.561 +    const char text[] = "hello world";
   1.562 +    *getter_Copies(a) = NS_strdup(text);
   1.563 +    if (strcmp(a, text) != 0)
   1.564 +      return false;
   1.565 +
   1.566 +    b = a;
   1.567 +    if (strcmp(a, b) != 0)
   1.568 +      return false;
   1.569 +
   1.570 +    a.Adopt(0);
   1.571 +    nsACString::const_iterator begin, end;
   1.572 +    a.BeginReading(begin);
   1.573 +    a.EndReading(end);
   1.574 +    char *r = ToNewCString(Substring(begin, end));
   1.575 +    if (strcmp(r, "") != 0)
   1.576 +      return false;
   1.577 +    nsMemory::Free(r);
   1.578 +
   1.579 +    a.Adopt(0);
   1.580 +    if (a != (const char*) 0)
   1.581 +      return false;
   1.582 +
   1.583 +    /*
   1.584 +    int32_t index = a.FindCharInSet("xyz");
   1.585 +    if (index != kNotFound)
   1.586 +      return false;
   1.587 +    */
   1.588 +
   1.589 +    return true;
   1.590 +  }
   1.591 +
   1.592 +bool test_empty_assign()
   1.593 +  {
   1.594 +    nsCString a;
   1.595 +    a.AssignLiteral("");
   1.596 +
   1.597 +    a.AppendLiteral("");
   1.598 +
   1.599 +    nsCString b;
   1.600 +    b.SetCapacity(0);
   1.601 +    return true;
   1.602 +  }
   1.603 +
   1.604 +bool test_set_length()
   1.605 +  {
   1.606 +    const char kText[] = "Default Plugin";
   1.607 +    nsCString buf;
   1.608 +    buf.SetCapacity(sizeof(kText)-1);
   1.609 +    buf.Assign(kText);
   1.610 +    buf.SetLength(sizeof(kText)-1);
   1.611 +    if (strcmp(buf.get(), kText) != 0)
   1.612 +      return false;
   1.613 +    return true;
   1.614 +  }
   1.615 +
   1.616 +bool test_substring()
   1.617 +  {
   1.618 +    nsCString super("hello world"), sub("hello");
   1.619 +
   1.620 +    // this tests that |super| starts with |sub|,
   1.621 +    
   1.622 +    bool r = sub.Equals(StringHead(super, sub.Length()));
   1.623 +    if (!r)
   1.624 +      return false;
   1.625 +
   1.626 +    // and verifies that |sub| does not start with |super|.
   1.627 +
   1.628 +    r = super.Equals(StringHead(sub, super.Length()));
   1.629 +    if (r)
   1.630 +      return false;
   1.631 +
   1.632 +    return true;
   1.633 +  }
   1.634 +
   1.635 +#define test_append(str, int, suffix) \
   1.636 +  str.Truncate(); \
   1.637 +  str.AppendInt(suffix = int ## suffix); \
   1.638 +  if (!str.EqualsLiteral(#int)) { \
   1.639 +    fputs("Error appending " #int "\n", stderr); \
   1.640 +    return false; \
   1.641 +  }
   1.642 +
   1.643 +#define test_appends(int, suffix) \
   1.644 +  test_append(str, int, suffix) \
   1.645 +  test_append(cstr, int, suffix)
   1.646 +
   1.647 +#define test_appendbase(str, prefix, int, suffix, base) \
   1.648 +  str.Truncate(); \
   1.649 +  str.AppendInt(suffix = prefix ## int ## suffix, base); \
   1.650 +  if (!str.EqualsLiteral(#int)) { \
   1.651 +    fputs("Error appending " #prefix #int "\n", stderr); \
   1.652 +    return false; \
   1.653 +  }
   1.654 +
   1.655 +#define test_appendbases(prefix, int, suffix, base) \
   1.656 +  test_appendbase(str, prefix, int, suffix, base) \
   1.657 +  test_appendbase(cstr, prefix, int, suffix, base)
   1.658 +
   1.659 +bool test_appendint()
   1.660 +  {
   1.661 +    nsString str;
   1.662 +    nsCString cstr;
   1.663 +    int32_t L;
   1.664 +    uint32_t UL;
   1.665 +    int64_t LL;
   1.666 +    uint64_t ULL;
   1.667 +    test_appends(2147483647, L)
   1.668 +    test_appends(-2147483648, L)
   1.669 +    test_appends(4294967295, UL)
   1.670 +    test_appends(9223372036854775807, LL)
   1.671 +    test_appends(-9223372036854775808, LL)
   1.672 +    test_appends(18446744073709551615, ULL)
   1.673 +    test_appendbases(0, 17777777777, L, 8)
   1.674 +    test_appendbases(0, 20000000000, L, 8)
   1.675 +    test_appendbases(0, 37777777777, UL, 8)
   1.676 +    test_appendbases(0, 777777777777777777777, LL, 8)
   1.677 +    test_appendbases(0, 1000000000000000000000, LL, 8)
   1.678 +    test_appendbases(0, 1777777777777777777777, ULL, 8)
   1.679 +    test_appendbases(0x, 7fffffff, L, 16)
   1.680 +    test_appendbases(0x, 80000000, L, 16)
   1.681 +    test_appendbases(0x, ffffffff, UL, 16)
   1.682 +    test_appendbases(0x, 7fffffffffffffff, LL, 16)
   1.683 +    test_appendbases(0x, 8000000000000000, LL, 16)
   1.684 +    test_appendbases(0x, ffffffffffffffff, ULL, 16)
   1.685 +    return true;
   1.686 +  }
   1.687 +
   1.688 +bool test_appendint64()
   1.689 +  {
   1.690 +    nsCString str;
   1.691 +
   1.692 +    int64_t max = INT64_MAX;
   1.693 +    static const char max_expected[] = "9223372036854775807";
   1.694 +    int64_t min = INT64_MIN;
   1.695 +    static const char min_expected[] = "-9223372036854775808";
   1.696 +    static const char min_expected_oct[] = "1000000000000000000000";
   1.697 +    int64_t maxint_plus1 = 1LL << 32;
   1.698 +    static const char maxint_plus1_expected[] = "4294967296";
   1.699 +    static const char maxint_plus1_expected_x[] = "100000000";
   1.700 +
   1.701 +    str.AppendInt(max);
   1.702 +
   1.703 +    if (!str.Equals(max_expected)) {
   1.704 +      fprintf(stderr, "Error appending INT64_MAX: Got %s\n", str.get());
   1.705 +      return false;
   1.706 +    }
   1.707 +
   1.708 +    str.Truncate();
   1.709 +    str.AppendInt(min);
   1.710 +    if (!str.Equals(min_expected)) {
   1.711 +      fprintf(stderr, "Error appending INT64_MIN: Got %s\n", str.get());
   1.712 +      return false;
   1.713 +    }
   1.714 +    str.Truncate();
   1.715 +    str.AppendInt(min, 8);
   1.716 +    if (!str.Equals(min_expected_oct)) {
   1.717 +      fprintf(stderr, "Error appending INT64_MIN (oct): Got %s\n", str.get());
   1.718 +      return false;
   1.719 +    }
   1.720 +
   1.721 +
   1.722 +    str.Truncate();
   1.723 +    str.AppendInt(maxint_plus1);
   1.724 +    if (!str.Equals(maxint_plus1_expected)) {
   1.725 +      fprintf(stderr, "Error appending UINT32_MAX + 1: Got %s\n", str.get());
   1.726 +      return false;
   1.727 +    }
   1.728 +    str.Truncate();
   1.729 +    str.AppendInt(maxint_plus1, 16);
   1.730 +    if (!str.Equals(maxint_plus1_expected_x)) {
   1.731 +      fprintf(stderr, "Error appending UINT32_MAX + 1 (hex): Got %s\n", str.get());
   1.732 +      return false;
   1.733 +    }
   1.734 +
   1.735 +
   1.736 +    return true;
   1.737 +  }
   1.738 +
   1.739 +bool test_appendfloat()
   1.740 +  {
   1.741 +    nsCString str;
   1.742 +    double bigdouble = 11223344556.66;
   1.743 +    static const char double_expected[] = "11223344556.66";
   1.744 +    static const char float_expected[] = "0.01";
   1.745 +
   1.746 +    // AppendFloat is used to append doubles, therefore the precision must be
   1.747 +    // large enough (see bug 327719)
   1.748 +    str.AppendFloat( bigdouble );
   1.749 +    if (!str.Equals(double_expected)) {
   1.750 +      fprintf(stderr, "Error appending a big double: Got %s\n", str.get());
   1.751 +      return false;
   1.752 +    }
   1.753 +    
   1.754 +    str.Truncate();
   1.755 +    // AppendFloat is used to append floats (bug 327719 #27)
   1.756 +    str.AppendFloat( 0.1f * 0.1f );
   1.757 +    if (!str.Equals(float_expected)) {
   1.758 +      fprintf(stderr, "Error appending a float: Got %s\n", str.get());
   1.759 +      return false;
   1.760 +    }
   1.761 +
   1.762 +    return true;
   1.763 +  }
   1.764 +
   1.765 +bool test_findcharinset()
   1.766 +  {
   1.767 +    nsCString buf("hello, how are you?");
   1.768 +
   1.769 +    int32_t index = buf.FindCharInSet(",?", 5);
   1.770 +    if (index != 5)
   1.771 +      return false;
   1.772 +
   1.773 +    index = buf.FindCharInSet("helo", 0);
   1.774 +    if (index != 0)
   1.775 +      return false;
   1.776 +
   1.777 +    index = buf.FindCharInSet("z?", 6);
   1.778 +    if (index != (int32_t) buf.Length()-1)
   1.779 +      return false;
   1.780 +
   1.781 +    return true;
   1.782 +  }
   1.783 +
   1.784 +bool test_rfindcharinset()
   1.785 +  {
   1.786 +    nsCString buf("hello, how are you?");
   1.787 +
   1.788 +    int32_t index = buf.RFindCharInSet(",?", 5);
   1.789 +    if (index != 5)
   1.790 +      return false;
   1.791 +
   1.792 +    index = buf.RFindCharInSet("helo", 0);
   1.793 +    if (index != 0)
   1.794 +      return false;
   1.795 +
   1.796 +    index = buf.RFindCharInSet("z?", 6);
   1.797 +    if (index != kNotFound)
   1.798 +      return false;
   1.799 +
   1.800 +    index = buf.RFindCharInSet("l", 5);
   1.801 +    if (index != 3)
   1.802 +      return false;
   1.803 +
   1.804 +    buf.Assign("abcdefghijkabc");
   1.805 +
   1.806 +    index = buf.RFindCharInSet("ab");
   1.807 +    if (index != 12)
   1.808 +      return false;
   1.809 +
   1.810 +    index = buf.RFindCharInSet("ab", 11);
   1.811 +    if (index != 11)
   1.812 +      return false;
   1.813 +
   1.814 +    index = buf.RFindCharInSet("ab", 10);
   1.815 +    if (index != 1)
   1.816 +      return false;
   1.817 +
   1.818 +    index = buf.RFindCharInSet("ab", 0);
   1.819 +    if (index != 0)
   1.820 +      return false;
   1.821 +
   1.822 +    index = buf.RFindCharInSet("cd", 1);
   1.823 +    if (index != kNotFound)
   1.824 +      return false;
   1.825 +
   1.826 +    index = buf.RFindCharInSet("h");
   1.827 +    if (index != 7)
   1.828 +      return false;
   1.829 +
   1.830 +    return true;
   1.831 +  }
   1.832 +
   1.833 +bool test_stringbuffer()
   1.834 +  {
   1.835 +    const char kData[] = "hello world";
   1.836 +
   1.837 +    nsStringBuffer *buf;
   1.838 +    
   1.839 +    buf = nsStringBuffer::Alloc(sizeof(kData));
   1.840 +    if (!buf)
   1.841 +      return false;
   1.842 +    buf->Release();
   1.843 + 
   1.844 +    buf = nsStringBuffer::Alloc(sizeof(kData));
   1.845 +    if (!buf)
   1.846 +      return false;
   1.847 +    char *data = (char *) buf->Data();
   1.848 +    memcpy(data, kData, sizeof(kData));
   1.849 +
   1.850 +    nsCString str;
   1.851 +    buf->ToString(sizeof(kData)-1, str);
   1.852 +
   1.853 +    nsStringBuffer *buf2;
   1.854 +    buf2 = nsStringBuffer::FromString(str);
   1.855 +
   1.856 +    bool rv = (buf == buf2);
   1.857 +
   1.858 +    buf->Release();
   1.859 +    return rv;
   1.860 +  }
   1.861 +
   1.862 +bool test_voided()
   1.863 +  {
   1.864 +    const char kData[] = "hello world";
   1.865 +
   1.866 +    nsXPIDLCString str;
   1.867 +    if (str)
   1.868 +      return false;
   1.869 +    if (!str.IsVoid())
   1.870 +      return false;
   1.871 +    if (!str.IsEmpty())
   1.872 +      return false;
   1.873 +
   1.874 +    str.Assign(kData);
   1.875 +    if (strcmp(str, kData) != 0)
   1.876 +      return false;
   1.877 +
   1.878 +    str.SetIsVoid(true);
   1.879 +    if (str)
   1.880 +      return false;
   1.881 +    if (!str.IsVoid())
   1.882 +      return false;
   1.883 +    if (!str.IsEmpty())
   1.884 +      return false;
   1.885 +
   1.886 +    str.SetIsVoid(false);
   1.887 +    if (strcmp(str, "") != 0)
   1.888 +      return false;
   1.889 +
   1.890 +    return true;
   1.891 +  }
   1.892 +
   1.893 +bool test_voided_autostr()
   1.894 +  {
   1.895 +    const char kData[] = "hello world";
   1.896 +
   1.897 +    nsAutoCString str;
   1.898 +    if (str.IsVoid())
   1.899 +      return false;
   1.900 +    if (!str.IsEmpty())
   1.901 +      return false;
   1.902 +
   1.903 +    str.Assign(kData);
   1.904 +    if (strcmp(str.get(), kData) != 0)
   1.905 +      return false;
   1.906 +
   1.907 +    str.SetIsVoid(true);
   1.908 +    if (!str.IsVoid())
   1.909 +      return false;
   1.910 +    if (!str.IsEmpty())
   1.911 +      return false;
   1.912 +
   1.913 +    str.Assign(kData);
   1.914 +    if (str.IsVoid())
   1.915 +      return false;
   1.916 +    if (str.IsEmpty())
   1.917 +      return false;
   1.918 +    if (strcmp(str.get(), kData) != 0)
   1.919 +      return false;
   1.920 +
   1.921 +    return true;
   1.922 +  }
   1.923 +
   1.924 +bool test_voided_assignment()
   1.925 +  {
   1.926 +    nsCString a, b;
   1.927 +    b.SetIsVoid(true);
   1.928 +    a = b;
   1.929 +    return a.IsVoid() && a.get() == b.get();
   1.930 +  }
   1.931 +
   1.932 +bool test_empty_assignment()
   1.933 +  {
   1.934 +    nsCString a, b;
   1.935 +    a = b;
   1.936 +    return a.get() == b.get();
   1.937 +  }
   1.938 +
   1.939 +struct ToIntegerTest
   1.940 +{
   1.941 +  const char *str;
   1.942 +  uint32_t radix;
   1.943 +  int32_t result;
   1.944 +  nsresult rv;
   1.945 +};
   1.946 +
   1.947 +static const ToIntegerTest kToIntegerTests[] = {
   1.948 +  { "123", 10, 123, NS_OK },
   1.949 +  { "7b", 16, 123, NS_OK },
   1.950 +  { "90194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE },
   1.951 +  { nullptr, 0, 0, 0 }
   1.952 +};
   1.953 +
   1.954 +bool test_string_tointeger()
   1.955 +{
   1.956 +  int32_t i;
   1.957 +  nsresult rv;
   1.958 +  for (const ToIntegerTest* t = kToIntegerTests; t->str; ++t) {
   1.959 +    int32_t result = nsAutoCString(t->str).ToInteger(&rv, t->radix);
   1.960 +    if (rv != t->rv || result != t->result)
   1.961 +      return false;
   1.962 +    result = nsAutoCString(t->str).ToInteger(&i, t->radix);
   1.963 +    if ((nsresult)i != t->rv || result != t->result)
   1.964 +      return false;
   1.965 +  }
   1.966 +  return true;
   1.967 +}
   1.968 +
   1.969 +static bool test_parse_string_helper(const char* str, char separator, int len,
   1.970 +                                       const char* s1, const char* s2)
   1.971 +{
   1.972 +  nsCString data(str);
   1.973 +  nsTArray<nsCString> results;
   1.974 +  if (!ParseString(data, separator, results))
   1.975 +    return false;
   1.976 +  if (int(results.Length()) != len)
   1.977 +    return false;
   1.978 +  const char* strings[] = { s1, s2 };
   1.979 +  for (int i = 0; i < len; ++i) {
   1.980 +    if (!results[i].Equals(strings[i]))
   1.981 +      return false;
   1.982 +  }
   1.983 +  return true;
   1.984 +}
   1.985 +
   1.986 +static bool test_parse_string_helper0(const char* str, char separator)
   1.987 +{
   1.988 +  return test_parse_string_helper(str, separator, 0, nullptr, nullptr);
   1.989 +}
   1.990 +
   1.991 +static bool test_parse_string_helper1(const char* str, char separator, const char* s1)
   1.992 +{
   1.993 +  return test_parse_string_helper(str, separator, 1, s1, nullptr);
   1.994 +}
   1.995 +
   1.996 +static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2)
   1.997 +{
   1.998 +  return test_parse_string_helper(str, separator, 2, s1, s2);
   1.999 +}
  1.1000 +
  1.1001 +static bool test_parse_string()
  1.1002 +{
  1.1003 +  return test_parse_string_helper1("foo, bar", '_', "foo, bar") &&
  1.1004 +         test_parse_string_helper2("foo, bar", ',', "foo", " bar") &&
  1.1005 +         test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") &&
  1.1006 +         test_parse_string_helper2("foo,bar", 'o', "f", ",bar") &&
  1.1007 +         test_parse_string_helper0("", '_') &&
  1.1008 +         test_parse_string_helper0("  ", ' ') &&
  1.1009 +         test_parse_string_helper1(" foo", ' ', "foo") &&
  1.1010 +         test_parse_string_helper1("  foo", ' ', "foo");
  1.1011 +}
  1.1012 +
  1.1013 +static bool test_strip_chars_helper(const char16_t* str, const char16_t* strip, const nsAString& result, uint32_t offset=0)
  1.1014 +{
  1.1015 +  nsAutoString tmp(str);
  1.1016 +  nsAString& data = tmp;
  1.1017 +  data.StripChars(strip, offset);
  1.1018 +  return data.Equals(result);
  1.1019 +}
  1.1020 +
  1.1021 +static bool test_strip_chars()
  1.1022 +{
  1.1023 +  return test_strip_chars_helper(MOZ_UTF16("foo \r \nbar"),
  1.1024 +                                 MOZ_UTF16(" \n\r"),
  1.1025 +                                 NS_LITERAL_STRING("foobar")) &&
  1.1026 +         test_strip_chars_helper(MOZ_UTF16("\r\nfoo\r\n"),
  1.1027 +                                 MOZ_UTF16(" \n\r"),
  1.1028 +                                 NS_LITERAL_STRING("foo")) &&
  1.1029 +         test_strip_chars_helper(MOZ_UTF16("foo"),
  1.1030 +                                 MOZ_UTF16(" \n\r"),
  1.1031 +                                 NS_LITERAL_STRING("foo")) &&
  1.1032 +         test_strip_chars_helper(MOZ_UTF16("foo"),
  1.1033 +                                 MOZ_UTF16("fo"),
  1.1034 +                                 NS_LITERAL_STRING("")) &&
  1.1035 +         test_strip_chars_helper(MOZ_UTF16("foo"),
  1.1036 +                                 MOZ_UTF16("foo"),
  1.1037 +                                 NS_LITERAL_STRING("")) &&
  1.1038 +         test_strip_chars_helper(MOZ_UTF16(" foo"),
  1.1039 +                                 MOZ_UTF16(" "),
  1.1040 +                                 NS_LITERAL_STRING(" foo"), 1);
  1.1041 +}
  1.1042 +
  1.1043 +static bool test_huge_capacity()
  1.1044 +{
  1.1045 +  nsString a, b, c, d, e, f, g, h, i, j, k, l, m, n;
  1.1046 +  nsCString n1;
  1.1047 +  bool fail = false;
  1.1048 +#undef ok
  1.1049 +#define ok(x) { fail |= !(x); }
  1.1050 +
  1.1051 +  ok(a.SetCapacity(1));
  1.1052 +  ok(!a.SetCapacity(nsString::size_type(-1)/2));
  1.1053 +  ok(a.SetCapacity(0));  // free the allocated memory
  1.1054 +
  1.1055 +  ok(b.SetCapacity(1));
  1.1056 +  ok(!b.SetCapacity(nsString::size_type(-1)/2 - 1));
  1.1057 +  ok(b.SetCapacity(0));
  1.1058 +
  1.1059 +  ok(c.SetCapacity(1));
  1.1060 +  ok(!c.SetCapacity(nsString::size_type(-1)/2));
  1.1061 +  ok(c.SetCapacity(0));
  1.1062 +
  1.1063 +  ok(!d.SetCapacity(nsString::size_type(-1)/2 - 1));
  1.1064 +  ok(!d.SetCapacity(nsString::size_type(-1)/2));
  1.1065 +  ok(d.SetCapacity(0));
  1.1066 +
  1.1067 +  ok(!e.SetCapacity(nsString::size_type(-1)/4));
  1.1068 +  ok(!e.SetCapacity(nsString::size_type(-1)/4 + 1));
  1.1069 +  ok(e.SetCapacity(0));
  1.1070 +
  1.1071 +  ok(!f.SetCapacity(nsString::size_type(-1)/2));
  1.1072 +  ok(f.SetCapacity(0));
  1.1073 +
  1.1074 +  ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1000));
  1.1075 +  ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1001));
  1.1076 +  ok(g.SetCapacity(0));
  1.1077 +
  1.1078 +  ok(!h.SetCapacity(nsString::size_type(-1)/4+1));
  1.1079 +  ok(!h.SetCapacity(nsString::size_type(-1)/2));
  1.1080 +  ok(h.SetCapacity(0));
  1.1081 +
  1.1082 +  ok(i.SetCapacity(1));
  1.1083 +  ok(i.SetCapacity(nsString::size_type(-1)/4 - 1000));
  1.1084 +  ok(!i.SetCapacity(nsString::size_type(-1)/4 + 1));
  1.1085 +  ok(i.SetCapacity(0));
  1.1086 +
  1.1087 +  ok(j.SetCapacity(nsString::size_type(-1)/4 - 1000));
  1.1088 +  ok(!j.SetCapacity(nsString::size_type(-1)/4 + 1));
  1.1089 +  ok(j.SetCapacity(0));
  1.1090 +
  1.1091 +  ok(k.SetCapacity(nsString::size_type(-1)/8 - 1000));
  1.1092 +  ok(k.SetCapacity(nsString::size_type(-1)/4 - 1001));
  1.1093 +  ok(k.SetCapacity(nsString::size_type(-1)/4 - 998));
  1.1094 +  ok(!k.SetCapacity(nsString::size_type(-1)/4 + 1));
  1.1095 +  ok(k.SetCapacity(0));
  1.1096 +
  1.1097 +  ok(l.SetCapacity(nsString::size_type(-1)/8));
  1.1098 +  ok(l.SetCapacity(nsString::size_type(-1)/8 + 1));
  1.1099 +  ok(l.SetCapacity(nsString::size_type(-1)/8 + 2));
  1.1100 +  ok(l.SetCapacity(0));
  1.1101 +
  1.1102 +  ok(m.SetCapacity(nsString::size_type(-1)/8 + 1000));
  1.1103 +  ok(m.SetCapacity(nsString::size_type(-1)/8 + 1001));
  1.1104 +  ok(m.SetCapacity(0));
  1.1105 +
  1.1106 +  ok(n.SetCapacity(nsString::size_type(-1)/8+1));
  1.1107 +  ok(!n.SetCapacity(nsString::size_type(-1)/4));
  1.1108 +  ok(n.SetCapacity(0));
  1.1109 +
  1.1110 +  ok(n.SetCapacity(0));
  1.1111 +  ok(n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 2));
  1.1112 +  ok(n.SetCapacity(0));
  1.1113 +  ok(!n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 1));
  1.1114 +  ok(n.SetCapacity(0));
  1.1115 +  ok(n1.SetCapacity(0));
  1.1116 +  ok(n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 2));
  1.1117 +  ok(n1.SetCapacity(0));
  1.1118 +  ok(!n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 1));
  1.1119 +  ok(n1.SetCapacity(0));
  1.1120 +
  1.1121 +  // Ignore the result if the address space is less than 64-bit because
  1.1122 +  // some of the allocations above will exhaust the address space.  
  1.1123 +  if (sizeof(void*) >= 8) {
  1.1124 +    return !fail;
  1.1125 +  }
  1.1126 +  return true;
  1.1127 +}
  1.1128 +
  1.1129 +static bool test_tofloat_helper(const nsString& aStr, float aExpected, bool aSuccess)
  1.1130 +{
  1.1131 +  int32_t result;
  1.1132 +  return aStr.ToFloat(&result) == aExpected &&
  1.1133 +         aSuccess ? result == NS_OK : result != NS_OK;
  1.1134 +}
  1.1135 +
  1.1136 +static bool test_tofloat()
  1.1137 +{
  1.1138 +  return \
  1.1139 +    test_tofloat_helper(NS_LITERAL_STRING("42"), 42.f, true) &&
  1.1140 +    test_tofloat_helper(NS_LITERAL_STRING("42.0"), 42.f, true) &&
  1.1141 +    test_tofloat_helper(NS_LITERAL_STRING("-42"), -42.f, true) &&
  1.1142 +    test_tofloat_helper(NS_LITERAL_STRING("+42"), 42, true) &&
  1.1143 +    test_tofloat_helper(NS_LITERAL_STRING("13.37"), 13.37f, true) &&
  1.1144 +    test_tofloat_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789f, true) &&
  1.1145 +    test_tofloat_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456f, true) &&
  1.1146 +    test_tofloat_helper(NS_LITERAL_STRING("0"), 0.f, true) &&
  1.1147 +    test_tofloat_helper(NS_LITERAL_STRING("1.e5"), 100000, true) &&
  1.1148 +    test_tofloat_helper(NS_LITERAL_STRING(""), 0.f, false) &&
  1.1149 +    test_tofloat_helper(NS_LITERAL_STRING("42foo"), 42.f, false) &&
  1.1150 +    test_tofloat_helper(NS_LITERAL_STRING("foo"), 0.f, false) &&
  1.1151 +    true;
  1.1152 +}
  1.1153 +
  1.1154 +static bool test_todouble_helper(const nsString& aStr, double aExpected, bool aSuccess)
  1.1155 +{
  1.1156 +  int32_t result;
  1.1157 +  return aStr.ToDouble(&result) == aExpected &&
  1.1158 +         aSuccess ? result == NS_OK : result != NS_OK;
  1.1159 +}
  1.1160 +
  1.1161 +static bool test_todouble()
  1.1162 +{
  1.1163 +  return \
  1.1164 +    test_todouble_helper(NS_LITERAL_STRING("42"), 42, true) &&
  1.1165 +    test_todouble_helper(NS_LITERAL_STRING("42.0"), 42, true) &&
  1.1166 +    test_todouble_helper(NS_LITERAL_STRING("-42"), -42, true) &&
  1.1167 +    test_todouble_helper(NS_LITERAL_STRING("+42"), 42, true) &&
  1.1168 +    test_todouble_helper(NS_LITERAL_STRING("13.37"), 13.37, true) &&
  1.1169 +    test_todouble_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789, true) &&
  1.1170 +    test_todouble_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456, true) &&
  1.1171 +    test_todouble_helper(NS_LITERAL_STRING("123456789.98765432123456"), 123456789.98765432123456, true) &&
  1.1172 +    test_todouble_helper(NS_LITERAL_STRING("0"), 0, true) &&
  1.1173 +    test_todouble_helper(NS_LITERAL_STRING("1.e5"), 100000, true) &&
  1.1174 +    test_todouble_helper(NS_LITERAL_STRING(""), 0, false) &&
  1.1175 +    test_todouble_helper(NS_LITERAL_STRING("42foo"), 42, false) &&
  1.1176 +    test_todouble_helper(NS_LITERAL_STRING("foo"), 0, false) &&
  1.1177 +    true;
  1.1178 +}
  1.1179 +
  1.1180 +//----
  1.1181 +
  1.1182 +typedef bool (*TestFunc)();
  1.1183 +
  1.1184 +static const struct Test
  1.1185 +  {
  1.1186 +    const char* name;
  1.1187 +    TestFunc    func;
  1.1188 +  }
  1.1189 +tests[] =
  1.1190 +  {
  1.1191 +    { "test_assign", test_assign },
  1.1192 +    { "test_assign_c", test_assign_c },
  1.1193 +    { "test1", test1 },
  1.1194 +    { "test2", test2 },
  1.1195 +    { "test_find", test_find },
  1.1196 +    { "test_rfind", test_rfind },
  1.1197 +    { "test_rfind_2", test_rfind_2 },
  1.1198 +    { "test_rfind_3", test_rfind_3 },
  1.1199 +    { "test_rfind_4", test_rfind_4 },
  1.1200 +    { "test_findinreadable", test_findinreadable },
  1.1201 +    { "test_rfindinreadable", test_rfindinreadable },
  1.1202 +    { "test_distance", test_distance },
  1.1203 +    { "test_length", test_length },
  1.1204 +    { "test_trim", test_trim },
  1.1205 +    { "test_replace_substr", test_replace_substr },
  1.1206 +    { "test_replace_substr_2", test_replace_substr_2 },
  1.1207 +    { "test_strip_ws", test_strip_ws },
  1.1208 +    { "test_equals_ic", test_equals_ic },
  1.1209 +    { "test_fixed_string", test_fixed_string },
  1.1210 +    { "test_concat", test_concat },
  1.1211 +    { "test_concat_2", test_concat_2 },
  1.1212 +    { "test_concat_3", test_concat_3 },
  1.1213 +    { "test_xpidl_string", test_xpidl_string },
  1.1214 +    { "test_empty_assign", test_empty_assign },
  1.1215 +    { "test_set_length", test_set_length },
  1.1216 +    { "test_substring", test_substring },
  1.1217 +    { "test_appendint", test_appendint },
  1.1218 +    { "test_appendint64", test_appendint64 },
  1.1219 +    { "test_appendfloat", test_appendfloat },
  1.1220 +    { "test_findcharinset", test_findcharinset },
  1.1221 +    { "test_rfindcharinset", test_rfindcharinset },
  1.1222 +    { "test_stringbuffer", test_stringbuffer },
  1.1223 +    { "test_voided", test_voided },
  1.1224 +    { "test_voided_autostr", test_voided_autostr },
  1.1225 +    { "test_voided_assignment", test_voided_assignment },
  1.1226 +    { "test_empty_assignment", test_empty_assignment },
  1.1227 +    { "test_string_tointeger", test_string_tointeger },
  1.1228 +    { "test_parse_string", test_parse_string },
  1.1229 +    { "test_strip_chars", test_strip_chars },
  1.1230 +    { "test_huge_capacity", test_huge_capacity },
  1.1231 +    { "test_tofloat", test_tofloat },
  1.1232 +    { "test_todouble", test_todouble },
  1.1233 +    { nullptr, nullptr }
  1.1234 +  };
  1.1235 +
  1.1236 +}
  1.1237 +
  1.1238 +using namespace TestStrings;
  1.1239 +
  1.1240 +int main(int argc, char **argv)
  1.1241 +  {
  1.1242 +    int count = 1;
  1.1243 +    if (argc > 1)
  1.1244 +      count = atoi(argv[1]);
  1.1245 +
  1.1246 +    NS_LogInit();
  1.1247 +
  1.1248 +    while (count--)
  1.1249 +      {
  1.1250 +        for (const Test* t = tests; t->name != nullptr; ++t)
  1.1251 +          {
  1.1252 +            printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
  1.1253 +          }
  1.1254 +      }
  1.1255 +    
  1.1256 +    return 0;
  1.1257 +  }

mercurial