xpcom/tests/external/TestMinStringAPI.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.

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 "nsStringAPI.h"
michael@0 9 #include "nsXPCOM.h"
michael@0 10 #include "nsMemory.h"
michael@0 11
michael@0 12 static const char kAsciiData[] = "Hello World";
michael@0 13
michael@0 14 static const char16_t kUnicodeData[] =
michael@0 15 {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
michael@0 16
michael@0 17 static bool test_basic_1()
michael@0 18 {
michael@0 19 nsCStringContainer s;
michael@0 20 NS_CStringContainerInit(s);
michael@0 21
michael@0 22 const char *ptr;
michael@0 23 uint32_t len;
michael@0 24 char *clone;
michael@0 25
michael@0 26 NS_CStringGetData(s, &ptr);
michael@0 27 if (ptr == nullptr || *ptr != '\0')
michael@0 28 {
michael@0 29 NS_ERROR("unexpected result");
michael@0 30 return false;
michael@0 31 }
michael@0 32
michael@0 33 NS_CStringSetData(s, kAsciiData, UINT32_MAX);
michael@0 34 len = NS_CStringGetData(s, &ptr);
michael@0 35 if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0)
michael@0 36 {
michael@0 37 NS_ERROR("unexpected result");
michael@0 38 return false;
michael@0 39 }
michael@0 40 if (len != sizeof(kAsciiData)-1)
michael@0 41 {
michael@0 42 NS_ERROR("unexpected result");
michael@0 43 return false;
michael@0 44 }
michael@0 45
michael@0 46 clone = NS_CStringCloneData(s);
michael@0 47 if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0)
michael@0 48 {
michael@0 49 NS_ERROR("unexpected result");
michael@0 50 return false;
michael@0 51 }
michael@0 52 NS_Free(clone);
michael@0 53
michael@0 54 nsCStringContainer temp;
michael@0 55 NS_CStringContainerInit(temp);
michael@0 56 NS_CStringCopy(temp, s);
michael@0 57
michael@0 58 len = NS_CStringGetData(temp, &ptr);
michael@0 59 if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0)
michael@0 60 {
michael@0 61 NS_ERROR("unexpected result");
michael@0 62 return false;
michael@0 63 }
michael@0 64 if (len != sizeof(kAsciiData)-1)
michael@0 65 {
michael@0 66 NS_ERROR("unexpected result");
michael@0 67 return false;
michael@0 68 }
michael@0 69
michael@0 70 NS_CStringContainerFinish(temp);
michael@0 71
michael@0 72 NS_CStringContainerFinish(s);
michael@0 73 return true;
michael@0 74 }
michael@0 75
michael@0 76 static bool test_basic_2()
michael@0 77 {
michael@0 78 nsStringContainer s;
michael@0 79 NS_StringContainerInit(s);
michael@0 80
michael@0 81 const char16_t *ptr;
michael@0 82 uint32_t len;
michael@0 83 char16_t *clone;
michael@0 84
michael@0 85 NS_StringGetData(s, &ptr);
michael@0 86 if (ptr == nullptr || *ptr != '\0')
michael@0 87 {
michael@0 88 NS_ERROR("unexpected result");
michael@0 89 return false;
michael@0 90 }
michael@0 91
michael@0 92 NS_StringSetData(s, kUnicodeData, UINT32_MAX);
michael@0 93 len = NS_StringGetData(s, &ptr);
michael@0 94 if (len != sizeof(kUnicodeData)/2 - 1)
michael@0 95 {
michael@0 96 NS_ERROR("unexpected result");
michael@0 97 return false;
michael@0 98 }
michael@0 99 if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
michael@0 100 {
michael@0 101 NS_ERROR("unexpected result");
michael@0 102 return false;
michael@0 103 }
michael@0 104
michael@0 105 clone = NS_StringCloneData(s);
michael@0 106 if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
michael@0 107 {
michael@0 108 NS_ERROR("unexpected result");
michael@0 109 return false;
michael@0 110 }
michael@0 111 NS_Free(clone);
michael@0 112
michael@0 113 nsStringContainer temp;
michael@0 114 NS_StringContainerInit(temp);
michael@0 115 NS_StringCopy(temp, s);
michael@0 116
michael@0 117 len = NS_StringGetData(temp, &ptr);
michael@0 118 if (len != sizeof(kUnicodeData)/2 - 1)
michael@0 119 {
michael@0 120 NS_ERROR("unexpected result");
michael@0 121 return false;
michael@0 122 }
michael@0 123 if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
michael@0 124 {
michael@0 125 NS_ERROR("unexpected result");
michael@0 126 return false;
michael@0 127 }
michael@0 128
michael@0 129 NS_StringContainerFinish(temp);
michael@0 130
michael@0 131 NS_StringContainerFinish(s);
michael@0 132
michael@0 133 return true;
michael@0 134 }
michael@0 135
michael@0 136 static bool test_convert()
michael@0 137 {
michael@0 138 nsStringContainer s;
michael@0 139 NS_StringContainerInit(s);
michael@0 140 NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1);
michael@0 141
michael@0 142 nsCStringContainer temp;
michael@0 143 NS_CStringContainerInit(temp);
michael@0 144
michael@0 145 const char *data;
michael@0 146
michael@0 147 NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp);
michael@0 148 NS_CStringGetData(temp, &data);
michael@0 149 if (strcmp(data, kAsciiData) != 0)
michael@0 150 return false;
michael@0 151
michael@0 152 NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp);
michael@0 153 NS_CStringGetData(temp, &data);
michael@0 154 if (strcmp(data, kAsciiData) != 0)
michael@0 155 return false;
michael@0 156
michael@0 157 NS_CStringContainerFinish(temp);
michael@0 158
michael@0 159 NS_StringContainerFinish(s);
michael@0 160 return true;
michael@0 161 }
michael@0 162
michael@0 163 static bool test_append()
michael@0 164 {
michael@0 165 nsCStringContainer s;
michael@0 166 NS_CStringContainerInit(s);
michael@0 167
michael@0 168 NS_CStringSetData(s, "foo");
michael@0 169 NS_CStringAppendData(s, "bar");
michael@0 170
michael@0 171 NS_CStringContainerFinish(s);
michael@0 172 return true;
michael@0 173 }
michael@0 174
michael@0 175 // Replace all occurrences of |matchVal| with |newVal|
michael@0 176 static void ReplaceSubstring( nsACString& str,
michael@0 177 const nsACString& matchVal,
michael@0 178 const nsACString& newVal )
michael@0 179 {
michael@0 180 const char* sp, *mp, *np;
michael@0 181 uint32_t sl, ml, nl;
michael@0 182
michael@0 183 sl = NS_CStringGetData(str, &sp);
michael@0 184 ml = NS_CStringGetData(matchVal, &mp);
michael@0 185 nl = NS_CStringGetData(newVal, &np);
michael@0 186
michael@0 187 for (const char* iter = sp; iter <= sp + sl - ml; ++iter)
michael@0 188 {
michael@0 189 if (memcmp(iter, mp, ml) == 0)
michael@0 190 {
michael@0 191 uint32_t offset = iter - sp;
michael@0 192
michael@0 193 NS_CStringSetDataRange(str, offset, ml, np, nl);
michael@0 194
michael@0 195 sl = NS_CStringGetData(str, &sp);
michael@0 196
michael@0 197 iter = sp + offset + nl - 1;
michael@0 198 }
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 static bool test_replace_driver(const char *strVal,
michael@0 203 const char *matchVal,
michael@0 204 const char *newVal,
michael@0 205 const char *finalVal)
michael@0 206 {
michael@0 207 nsCStringContainer a;
michael@0 208 NS_CStringContainerInit(a);
michael@0 209 NS_CStringSetData(a, strVal);
michael@0 210
michael@0 211 nsCStringContainer b;
michael@0 212 NS_CStringContainerInit(b);
michael@0 213 NS_CStringSetData(b, matchVal);
michael@0 214
michael@0 215 nsCStringContainer c;
michael@0 216 NS_CStringContainerInit(c);
michael@0 217 NS_CStringSetData(c, newVal);
michael@0 218
michael@0 219 ReplaceSubstring(a, b, c);
michael@0 220
michael@0 221 const char *data;
michael@0 222 NS_CStringGetData(a, &data);
michael@0 223 if (strcmp(data, finalVal) != 0)
michael@0 224 return false;
michael@0 225
michael@0 226 NS_CStringContainerFinish(c);
michael@0 227 NS_CStringContainerFinish(b);
michael@0 228 NS_CStringContainerFinish(a);
michael@0 229 return true;
michael@0 230 }
michael@0 231
michael@0 232 static bool test_replace()
michael@0 233 {
michael@0 234 bool rv;
michael@0 235
michael@0 236 rv = test_replace_driver("hello world, hello again!",
michael@0 237 "hello",
michael@0 238 "goodbye",
michael@0 239 "goodbye world, goodbye again!");
michael@0 240 if (!rv)
michael@0 241 return rv;
michael@0 242
michael@0 243 rv = test_replace_driver("foofoofoofoo!",
michael@0 244 "foo",
michael@0 245 "bar",
michael@0 246 "barbarbarbar!");
michael@0 247 if (!rv)
michael@0 248 return rv;
michael@0 249
michael@0 250 rv = test_replace_driver("foo bar systems",
michael@0 251 "xyz",
michael@0 252 "crazy",
michael@0 253 "foo bar systems");
michael@0 254 if (!rv)
michael@0 255 return rv;
michael@0 256
michael@0 257 rv = test_replace_driver("oh",
michael@0 258 "xyz",
michael@0 259 "crazy",
michael@0 260 "oh");
michael@0 261 if (!rv)
michael@0 262 return rv;
michael@0 263
michael@0 264 return true;
michael@0 265 }
michael@0 266
michael@0 267 static const char* kWhitespace="\b\t\r\n ";
michael@0 268
michael@0 269 static void
michael@0 270 CompressWhitespace(nsACString &str)
michael@0 271 {
michael@0 272 const char *p;
michael@0 273 int32_t i, len = (int32_t) NS_CStringGetData(str, &p);
michael@0 274
michael@0 275 // trim leading whitespace
michael@0 276
michael@0 277 for (i=0; i<len; ++i)
michael@0 278 {
michael@0 279 if (!strchr(kWhitespace, (char) p[i]))
michael@0 280 break;
michael@0 281 }
michael@0 282
michael@0 283 if (i>0)
michael@0 284 {
michael@0 285 NS_CStringCutData(str, 0, i);
michael@0 286 len = (int32_t) NS_CStringGetData(str, &p);
michael@0 287 }
michael@0 288
michael@0 289 // trim trailing whitespace
michael@0 290
michael@0 291 for (i=len-1; i>=0; --i)
michael@0 292 {
michael@0 293 if (!strchr(kWhitespace, (char) p[i]))
michael@0 294 break;
michael@0 295 }
michael@0 296
michael@0 297 if (++i < len)
michael@0 298 NS_CStringCutData(str, i, len - i);
michael@0 299 }
michael@0 300
michael@0 301 static bool test_compress_ws()
michael@0 302 {
michael@0 303 nsCStringContainer s;
michael@0 304 NS_CStringContainerInit(s);
michael@0 305 NS_CStringSetData(s, " \thello world\r \n");
michael@0 306 CompressWhitespace(s);
michael@0 307 const char *d;
michael@0 308 NS_CStringGetData(s, &d);
michael@0 309 bool rv = !strcmp(d, "hello world");
michael@0 310 if (!rv)
michael@0 311 printf("=> \"%s\"\n", d);
michael@0 312 NS_CStringContainerFinish(s);
michael@0 313 return rv;
michael@0 314 }
michael@0 315
michael@0 316 static bool test_depend()
michael@0 317 {
michael@0 318 static const char kData[] = "hello world";
michael@0 319
michael@0 320 nsCStringContainer s;
michael@0 321 NS_ENSURE_SUCCESS(
michael@0 322 NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
michael@0 323 NS_CSTRING_CONTAINER_INIT_DEPEND),
michael@0 324 false);
michael@0 325
michael@0 326 const char *sd;
michael@0 327 NS_CStringGetData(s, &sd);
michael@0 328
michael@0 329 bool rv = (sd == kData);
michael@0 330 NS_CStringContainerFinish(s);
michael@0 331 return rv;
michael@0 332 }
michael@0 333
michael@0 334 static bool test_depend_sub()
michael@0 335 {
michael@0 336 static const char kData[] = "hello world";
michael@0 337
michael@0 338 nsCStringContainer s;
michael@0 339 NS_ENSURE_SUCCESS(
michael@0 340 NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
michael@0 341 NS_CSTRING_CONTAINER_INIT_DEPEND |
michael@0 342 NS_CSTRING_CONTAINER_INIT_SUBSTRING),
michael@0 343 false);
michael@0 344
michael@0 345 bool terminated;
michael@0 346 const char *sd;
michael@0 347 uint32_t len = NS_CStringGetData(s, &sd, &terminated);
michael@0 348
michael@0 349 bool rv = (sd == kData && len == sizeof(kData)-1 && !terminated);
michael@0 350 NS_CStringContainerFinish(s);
michael@0 351 return rv;
michael@0 352 }
michael@0 353
michael@0 354 static bool test_adopt()
michael@0 355 {
michael@0 356 static const char kData[] = "hello world";
michael@0 357
michael@0 358 char *data = (char *) nsMemory::Clone(kData, sizeof(kData));
michael@0 359 if (!data)
michael@0 360 return false;
michael@0 361
michael@0 362 nsCStringContainer s;
michael@0 363 NS_ENSURE_SUCCESS(
michael@0 364 NS_CStringContainerInit2(s, data, UINT32_MAX,
michael@0 365 NS_CSTRING_CONTAINER_INIT_ADOPT),
michael@0 366 false); // leaks data on failure *shrug*
michael@0 367
michael@0 368 const char *sd;
michael@0 369 NS_CStringGetData(s, &sd);
michael@0 370
michael@0 371 bool rv = (sd == data);
michael@0 372 NS_CStringContainerFinish(s);
michael@0 373 return rv;
michael@0 374 }
michael@0 375
michael@0 376 static bool test_adopt_sub()
michael@0 377 {
michael@0 378 static const char kData[] = "hello world";
michael@0 379
michael@0 380 char *data = (char *) nsMemory::Clone(kData, sizeof(kData)-1);
michael@0 381 if (!data)
michael@0 382 return false;
michael@0 383
michael@0 384 nsCStringContainer s;
michael@0 385 NS_ENSURE_SUCCESS(
michael@0 386 NS_CStringContainerInit2(s, data, sizeof(kData)-1,
michael@0 387 NS_CSTRING_CONTAINER_INIT_ADOPT |
michael@0 388 NS_CSTRING_CONTAINER_INIT_SUBSTRING),
michael@0 389 false); // leaks data on failure *shrug*
michael@0 390
michael@0 391 bool terminated;
michael@0 392 const char *sd;
michael@0 393 uint32_t len = NS_CStringGetData(s, &sd, &terminated);
michael@0 394
michael@0 395 bool rv = (sd == data && len == sizeof(kData)-1 && !terminated);
michael@0 396 NS_CStringContainerFinish(s);
michael@0 397 return rv;
michael@0 398 }
michael@0 399
michael@0 400 static bool test_mutation()
michael@0 401 {
michael@0 402 nsCStringContainer s;
michael@0 403 NS_CStringContainerInit(s);
michael@0 404
michael@0 405 const char kText[] = "Every good boy does fine.";
michael@0 406
michael@0 407 char *buf;
michael@0 408 uint32_t len = NS_CStringGetMutableData(s, sizeof(kText) - 1, &buf);
michael@0 409 if (!buf || len != sizeof(kText) - 1)
michael@0 410 return false;
michael@0 411 memcpy(buf, kText, sizeof(kText));
michael@0 412
michael@0 413 const char *data;
michael@0 414 NS_CStringGetData(s, &data);
michael@0 415 if (strcmp(data, kText) != 0)
michael@0 416 return false;
michael@0 417
michael@0 418 uint32_t newLen = len + 1;
michael@0 419 len = NS_CStringGetMutableData(s, newLen, &buf);
michael@0 420 if (!buf || len != newLen)
michael@0 421 return false;
michael@0 422
michael@0 423 buf[len - 1] = '.';
michael@0 424
michael@0 425 NS_CStringGetData(s, &data);
michael@0 426 if (strncmp(data, kText, len - 1) != 0 || data[len - 1] != '.')
michael@0 427 return false;
michael@0 428
michael@0 429 NS_CStringContainerFinish(s);
michael@0 430 return true;
michael@0 431 }
michael@0 432
michael@0 433 static bool test_ascii()
michael@0 434 {
michael@0 435 nsCString testCString;
michael@0 436 testCString.AppendASCII(kAsciiData);
michael@0 437 if (!testCString.EqualsLiteral(kAsciiData))
michael@0 438 return false;
michael@0 439
michael@0 440 testCString.AssignASCII(kAsciiData);
michael@0 441 if (!testCString.LowerCaseEqualsLiteral("hello world"))
michael@0 442 return false;
michael@0 443
michael@0 444 nsString testString;
michael@0 445 testString.AppendASCII(kAsciiData);
michael@0 446 if (!testString.EqualsLiteral(kAsciiData))
michael@0 447 return false;
michael@0 448
michael@0 449 testString.AssignASCII(kAsciiData);
michael@0 450 if (!testString.LowerCaseEqualsLiteral("hello world"))
michael@0 451 return false;
michael@0 452
michael@0 453 return true;
michael@0 454 }
michael@0 455
michael@0 456 static bool test_chars()
michael@0 457 {
michael@0 458 nsCString testCString(kAsciiData);
michael@0 459 if (testCString.First() != 'H')
michael@0 460 return false;
michael@0 461 if (testCString.Last() != 'd')
michael@0 462 return false;
michael@0 463 testCString.SetCharAt('u', 8);
michael@0 464 if (!testCString.EqualsASCII("Hello Would"))
michael@0 465 return false;
michael@0 466
michael@0 467 nsString testString(kUnicodeData);
michael@0 468 if (testString.First() != 'H')
michael@0 469 return false;
michael@0 470 if (testString.Last() != 'd')
michael@0 471 return false;
michael@0 472 testString.SetCharAt('u', 8);
michael@0 473 if (!testString.EqualsASCII("Hello Would"))
michael@0 474 return false;
michael@0 475
michael@0 476 return true;
michael@0 477 }
michael@0 478
michael@0 479 static bool test_stripchars()
michael@0 480 {
michael@0 481 nsCString test(kAsciiData);
michael@0 482 test.StripChars("ld");
michael@0 483 if (!test.Equals("Heo Wor"))
michael@0 484 return false;
michael@0 485
michael@0 486 test.Assign(kAsciiData);
michael@0 487 test.StripWhitespace();
michael@0 488 if (!test.Equals("HelloWorld"))
michael@0 489 return false;
michael@0 490
michael@0 491 return true;
michael@0 492 }
michael@0 493
michael@0 494 static bool test_trim()
michael@0 495 {
michael@0 496 static const char kWS[] = "\n\t\r ";
michael@0 497 static const char kTestString[] = " \n\tTesting...\n\r";
michael@0 498
michael@0 499 nsCString test1(kTestString);
michael@0 500 nsCString test2(kTestString);
michael@0 501 nsCString test3(kTestString);
michael@0 502
michael@0 503 test1.Trim(kWS);
michael@0 504 test2.Trim(kWS, true, false);
michael@0 505 test3.Trim(kWS, false, true);
michael@0 506
michael@0 507 if (!test1.Equals("Testing..."))
michael@0 508 return false;
michael@0 509
michael@0 510 if (!test2.Equals("Testing...\n\r"))
michael@0 511 return false;
michael@0 512
michael@0 513 if (!test3.Equals(" \n\tTesting..."))
michael@0 514 return false;
michael@0 515
michael@0 516 return true;
michael@0 517 }
michael@0 518
michael@0 519 static bool test_find()
michael@0 520 {
michael@0 521 nsString uni(kUnicodeData);
michael@0 522
michael@0 523 static const char kHello[] = "Hello";
michael@0 524 static const char khello[] = "hello";
michael@0 525 static const char kBye[] = "Bye!";
michael@0 526
michael@0 527 int32_t found;
michael@0 528
michael@0 529 found = uni.Find(kHello);
michael@0 530 if (found != 0)
michael@0 531 return false;
michael@0 532
michael@0 533 found = uni.Find(khello, false);
michael@0 534 if (found != -1)
michael@0 535 return false;
michael@0 536
michael@0 537 found = uni.Find(khello, true);
michael@0 538 if (found != 0)
michael@0 539 return false;
michael@0 540
michael@0 541 found = uni.Find(kBye);
michael@0 542 if (found != -1)
michael@0 543 return false;
michael@0 544
michael@0 545 found = uni.Find(NS_LITERAL_STRING("World"));
michael@0 546 if (found != 6)
michael@0 547 return false;
michael@0 548
michael@0 549 found = uni.Find(uni);
michael@0 550 if (found != 0)
michael@0 551 return false;
michael@0 552
michael@0 553 return true;
michael@0 554 }
michael@0 555
michael@0 556 static bool test_compressws()
michael@0 557 {
michael@0 558 nsString check(NS_LITERAL_STRING(" \tTesting \n\t1\n 2 3\n "));
michael@0 559 CompressWhitespace(check);
michael@0 560 return check.Equals(NS_LITERAL_STRING("Testing 1 2 3"));
michael@0 561 }
michael@0 562
michael@0 563 static bool test_comparisons()
michael@0 564 {
michael@0 565 bool result;
michael@0 566
michael@0 567 // nsString
michael@0 568
michael@0 569 NS_NAMED_LITERAL_STRING(shortString1, "Foo");
michael@0 570 NS_NAMED_LITERAL_STRING(shortString2, "Bar");
michael@0 571 NS_NAMED_LITERAL_STRING(shortString3, "Bar");
michael@0 572 NS_NAMED_LITERAL_STRING(shortString4, "bar");
michael@0 573 NS_NAMED_LITERAL_STRING(longString, "FooBar");
michael@0 574
michael@0 575 // ==
michael@0 576
michael@0 577 result = (shortString1 == shortString2);
michael@0 578 if (result)
michael@0 579 return false;
michael@0 580
michael@0 581 result = (shortString2 == shortString3);
michael@0 582 if (!result)
michael@0 583 return false;
michael@0 584
michael@0 585 result = (shortString3 == shortString4);
michael@0 586 if (result)
michael@0 587 return false;
michael@0 588
michael@0 589 result = (shortString1 == longString);
michael@0 590 if (result)
michael@0 591 return false;
michael@0 592
michael@0 593 result = (longString == shortString1);
michael@0 594 if (result)
michael@0 595 return false;
michael@0 596
michael@0 597 // !=
michael@0 598
michael@0 599 result = (shortString1 != shortString2);
michael@0 600 if (!result)
michael@0 601 return false;
michael@0 602
michael@0 603 result = (shortString2 != shortString3);
michael@0 604 if (result)
michael@0 605 return false;
michael@0 606
michael@0 607 result = (shortString3 != shortString4);
michael@0 608 if (!result)
michael@0 609 return false;
michael@0 610
michael@0 611 result = (shortString1 != longString);
michael@0 612 if (!result)
michael@0 613 return false;
michael@0 614
michael@0 615 result = (longString != shortString1);
michael@0 616 if (!result)
michael@0 617 return false;
michael@0 618
michael@0 619 // <
michael@0 620
michael@0 621 result = (shortString1 < shortString2);
michael@0 622 if (result)
michael@0 623 return false;
michael@0 624
michael@0 625 result = (shortString2 < shortString1);
michael@0 626 if (!result)
michael@0 627 return false;
michael@0 628
michael@0 629 result = (shortString1 < longString);
michael@0 630 if (!result)
michael@0 631 return false;
michael@0 632
michael@0 633 result = (longString < shortString1);
michael@0 634 if (result)
michael@0 635 return false;
michael@0 636
michael@0 637 result = (shortString2 < shortString3);
michael@0 638 if (result)
michael@0 639 return false;
michael@0 640
michael@0 641 result = (shortString3 < shortString4);
michael@0 642 if (!result)
michael@0 643 return false;
michael@0 644
michael@0 645 result = (shortString4 < shortString3);
michael@0 646 if (result)
michael@0 647 return false;
michael@0 648
michael@0 649 // <=
michael@0 650
michael@0 651 result = (shortString1 <= shortString2);
michael@0 652 if (result)
michael@0 653 return false;
michael@0 654
michael@0 655 result = (shortString2 <= shortString1);
michael@0 656 if (!result)
michael@0 657 return false;
michael@0 658
michael@0 659 result = (shortString1 <= longString);
michael@0 660 if (!result)
michael@0 661 return false;
michael@0 662
michael@0 663 result = (longString <= shortString1);
michael@0 664 if (result)
michael@0 665 return false;
michael@0 666
michael@0 667 result = (shortString2 <= shortString3);
michael@0 668 if (!result)
michael@0 669 return false;
michael@0 670
michael@0 671 result = (shortString3 <= shortString4);
michael@0 672 if (!result)
michael@0 673 return false;
michael@0 674
michael@0 675 result = (shortString4 <= shortString3);
michael@0 676 if (result)
michael@0 677 return false;
michael@0 678
michael@0 679 // >
michael@0 680
michael@0 681 result = (shortString1 > shortString2);
michael@0 682 if (!result)
michael@0 683 return false;
michael@0 684
michael@0 685 result = (shortString2 > shortString1);
michael@0 686 if (result)
michael@0 687 return false;
michael@0 688
michael@0 689 result = (shortString1 > longString);
michael@0 690 if (result)
michael@0 691 return false;
michael@0 692
michael@0 693 result = (longString > shortString1);
michael@0 694 if (!result)
michael@0 695 return false;
michael@0 696
michael@0 697 result = (shortString2 > shortString3);
michael@0 698 if (result)
michael@0 699 return false;
michael@0 700
michael@0 701 result = (shortString3 > shortString4);
michael@0 702 if (result)
michael@0 703 return false;
michael@0 704
michael@0 705 result = (shortString4 > shortString3);
michael@0 706 if (!result)
michael@0 707 return false;
michael@0 708
michael@0 709 // >=
michael@0 710
michael@0 711 result = (shortString1 >= shortString2);
michael@0 712 if (!result)
michael@0 713 return false;
michael@0 714
michael@0 715 result = (shortString2 >= shortString1);
michael@0 716 if (result)
michael@0 717 return false;
michael@0 718
michael@0 719 result = (shortString1 >= longString);
michael@0 720 if (result)
michael@0 721 return false;
michael@0 722
michael@0 723 result = (longString >= shortString1);
michael@0 724 if (!result)
michael@0 725 return false;
michael@0 726
michael@0 727 result = (shortString2 >= shortString3);
michael@0 728 if (!result)
michael@0 729 return false;
michael@0 730
michael@0 731 result = (shortString3 >= shortString4);
michael@0 732 if (result)
michael@0 733 return false;
michael@0 734
michael@0 735 result = (shortString4 >= shortString3);
michael@0 736 if (!result)
michael@0 737 return false;
michael@0 738
michael@0 739 // nsCString
michael@0 740
michael@0 741 NS_NAMED_LITERAL_CSTRING(shortCString1, "Foo");
michael@0 742 NS_NAMED_LITERAL_CSTRING(shortCString2, "Bar");
michael@0 743 NS_NAMED_LITERAL_CSTRING(shortCString3, "Bar");
michael@0 744 NS_NAMED_LITERAL_CSTRING(shortCString4, "bar");
michael@0 745 NS_NAMED_LITERAL_CSTRING(longCString, "FooBar");
michael@0 746
michael@0 747 // ==
michael@0 748
michael@0 749 result = (shortCString1 == shortCString2);
michael@0 750 if (result)
michael@0 751 return false;
michael@0 752
michael@0 753 result = (shortCString2 == shortCString3);
michael@0 754 if (!result)
michael@0 755 return false;
michael@0 756
michael@0 757 result = (shortCString3 == shortCString4);
michael@0 758 if (result)
michael@0 759 return false;
michael@0 760
michael@0 761 result = (shortCString1 == longCString);
michael@0 762 if (result)
michael@0 763 return false;
michael@0 764
michael@0 765 result = (longCString == shortCString1);
michael@0 766 if (result)
michael@0 767 return false;
michael@0 768
michael@0 769 // !=
michael@0 770
michael@0 771 result = (shortCString1 != shortCString2);
michael@0 772 if (!result)
michael@0 773 return false;
michael@0 774
michael@0 775 result = (shortCString2 != shortCString3);
michael@0 776 if (result)
michael@0 777 return false;
michael@0 778
michael@0 779 result = (shortCString3 != shortCString4);
michael@0 780 if (!result)
michael@0 781 return false;
michael@0 782
michael@0 783 result = (shortCString1 != longCString);
michael@0 784 if (!result)
michael@0 785 return false;
michael@0 786
michael@0 787 result = (longCString != shortCString1);
michael@0 788 if (!result)
michael@0 789 return false;
michael@0 790
michael@0 791 // <
michael@0 792
michael@0 793 result = (shortCString1 < shortCString2);
michael@0 794 if (result)
michael@0 795 return false;
michael@0 796
michael@0 797 result = (shortCString2 < shortCString1);
michael@0 798 if (!result)
michael@0 799 return false;
michael@0 800
michael@0 801 result = (shortCString1 < longCString);
michael@0 802 if (!result)
michael@0 803 return false;
michael@0 804
michael@0 805 result = (longCString < shortCString1);
michael@0 806 if (result)
michael@0 807 return false;
michael@0 808
michael@0 809 result = (shortCString2 < shortCString3);
michael@0 810 if (result)
michael@0 811 return false;
michael@0 812
michael@0 813 result = (shortCString3 < shortCString4);
michael@0 814 if (!result)
michael@0 815 return false;
michael@0 816
michael@0 817 result = (shortCString4 < shortCString3);
michael@0 818 if (result)
michael@0 819 return false;
michael@0 820
michael@0 821 // <=
michael@0 822
michael@0 823 result = (shortCString1 <= shortCString2);
michael@0 824 if (result)
michael@0 825 return false;
michael@0 826
michael@0 827 result = (shortCString2 <= shortCString1);
michael@0 828 if (!result)
michael@0 829 return false;
michael@0 830
michael@0 831 result = (shortCString1 <= longCString);
michael@0 832 if (!result)
michael@0 833 return false;
michael@0 834
michael@0 835 result = (longCString <= shortCString1);
michael@0 836 if (result)
michael@0 837 return false;
michael@0 838
michael@0 839 result = (shortCString2 <= shortCString3);
michael@0 840 if (!result)
michael@0 841 return false;
michael@0 842
michael@0 843 result = (shortCString3 <= shortCString4);
michael@0 844 if (!result)
michael@0 845 return false;
michael@0 846
michael@0 847 result = (shortCString4 <= shortCString3);
michael@0 848 if (result)
michael@0 849 return false;
michael@0 850
michael@0 851 // >
michael@0 852
michael@0 853 result = (shortCString1 > shortCString2);
michael@0 854 if (!result)
michael@0 855 return false;
michael@0 856
michael@0 857 result = (shortCString2 > shortCString1);
michael@0 858 if (result)
michael@0 859 return false;
michael@0 860
michael@0 861 result = (shortCString1 > longCString);
michael@0 862 if (result)
michael@0 863 return false;
michael@0 864
michael@0 865 result = (longCString > shortCString1);
michael@0 866 if (!result)
michael@0 867 return false;
michael@0 868
michael@0 869 result = (shortCString2 > shortCString3);
michael@0 870 if (result)
michael@0 871 return false;
michael@0 872
michael@0 873 result = (shortCString3 > shortCString4);
michael@0 874 if (result)
michael@0 875 return false;
michael@0 876
michael@0 877 result = (shortCString4 > shortCString3);
michael@0 878 if (!result)
michael@0 879 return false;
michael@0 880
michael@0 881 // >=
michael@0 882
michael@0 883 result = (shortCString1 >= shortCString2);
michael@0 884 if (!result)
michael@0 885 return false;
michael@0 886
michael@0 887 result = (shortCString2 >= shortCString1);
michael@0 888 if (result)
michael@0 889 return false;
michael@0 890
michael@0 891 result = (shortCString1 >= longCString);
michael@0 892 if (result)
michael@0 893 return false;
michael@0 894
michael@0 895 result = (longCString >= shortCString1);
michael@0 896 if (!result)
michael@0 897 return false;
michael@0 898
michael@0 899 result = (shortCString2 >= shortCString3);
michael@0 900 if (!result)
michael@0 901 return false;
michael@0 902
michael@0 903 result = (shortCString3 >= shortCString4);
michael@0 904 if (result)
michael@0 905 return false;
michael@0 906
michael@0 907 result = (shortCString4 >= shortCString3);
michael@0 908 if (!result)
michael@0 909 return false;
michael@0 910
michael@0 911 return true;
michael@0 912 }
michael@0 913
michael@0 914 static bool test_parse_string_helper(const char* str, char separator, int len,
michael@0 915 const char* s1, const char* s2)
michael@0 916 {
michael@0 917 nsCString data(str);
michael@0 918 nsTArray<nsCString> results;
michael@0 919 if (!ParseString(data, separator, results))
michael@0 920 return false;
michael@0 921 if (int(results.Length()) != len)
michael@0 922 return false;
michael@0 923 const char* strings[] = { s1, s2 };
michael@0 924 for (int i = 0; i < len; ++i) {
michael@0 925 if (!results[i].Equals(strings[i]))
michael@0 926 return false;
michael@0 927 }
michael@0 928 return true;
michael@0 929 }
michael@0 930
michael@0 931 static bool test_parse_string_helper0(const char* str, char separator)
michael@0 932 {
michael@0 933 return test_parse_string_helper(str, separator, 0, nullptr, nullptr);
michael@0 934 }
michael@0 935
michael@0 936 static bool test_parse_string_helper1(const char* str, char separator, const char* s1)
michael@0 937 {
michael@0 938 return test_parse_string_helper(str, separator, 1, s1, nullptr);
michael@0 939 }
michael@0 940
michael@0 941 static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2)
michael@0 942 {
michael@0 943 return test_parse_string_helper(str, separator, 2, s1, s2);
michael@0 944 }
michael@0 945
michael@0 946 static bool test_parse_string()
michael@0 947 {
michael@0 948 return test_parse_string_helper1("foo, bar", '_', "foo, bar") &&
michael@0 949 test_parse_string_helper2("foo, bar", ',', "foo", " bar") &&
michael@0 950 test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") &&
michael@0 951 test_parse_string_helper2("foo,bar", 'o', "f", ",bar") &&
michael@0 952 test_parse_string_helper0("", '_') &&
michael@0 953 test_parse_string_helper0(" ", ' ') &&
michael@0 954 test_parse_string_helper1(" foo", ' ', "foo") &&
michael@0 955 test_parse_string_helper1(" foo", ' ', "foo");
michael@0 956 }
michael@0 957
michael@0 958 //----
michael@0 959
michael@0 960 typedef bool (*TestFunc)();
michael@0 961
michael@0 962 static const struct Test
michael@0 963 {
michael@0 964 const char* name;
michael@0 965 TestFunc func;
michael@0 966 }
michael@0 967 tests[] =
michael@0 968 {
michael@0 969 { "test_basic_1", test_basic_1 },
michael@0 970 { "test_basic_2", test_basic_2 },
michael@0 971 { "test_convert", test_convert },
michael@0 972 { "test_append", test_append },
michael@0 973 { "test_replace", test_replace },
michael@0 974 { "test_compress_ws", test_compress_ws },
michael@0 975 { "test_depend", test_depend },
michael@0 976 { "test_depend_sub", test_depend_sub },
michael@0 977 { "test_adopt", test_adopt },
michael@0 978 { "test_adopt_sub", test_adopt_sub },
michael@0 979 { "test_mutation", test_mutation },
michael@0 980 { "test_ascii", test_ascii },
michael@0 981 { "test_chars", test_chars },
michael@0 982 { "test_stripchars", test_stripchars },
michael@0 983 { "test_trim", test_trim },
michael@0 984 { "test_find", test_find },
michael@0 985 { "test_compressws", test_compressws },
michael@0 986 { "test_comparisons", test_comparisons },
michael@0 987 { "test_parse_string", test_parse_string },
michael@0 988 { nullptr, nullptr }
michael@0 989 };
michael@0 990
michael@0 991 //----
michael@0 992
michael@0 993 int main(int argc, char **argv)
michael@0 994 {
michael@0 995 int count = 1;
michael@0 996 if (argc > 1)
michael@0 997 count = atoi(argv[1]);
michael@0 998
michael@0 999 while (count--)
michael@0 1000 {
michael@0 1001 for (const Test* t = tests; t->name != nullptr; ++t)
michael@0 1002 {
michael@0 1003 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
michael@0 1004 }
michael@0 1005 }
michael@0 1006
michael@0 1007 return 0;
michael@0 1008 }

mercurial