xpcom/tests/TestStrings.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial