1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/external/TestMinStringAPI.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1008 @@ 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 "nsStringAPI.h" 1.12 +#include "nsXPCOM.h" 1.13 +#include "nsMemory.h" 1.14 + 1.15 +static const char kAsciiData[] = "Hello World"; 1.16 + 1.17 +static const char16_t kUnicodeData[] = 1.18 + {'H','e','l','l','o',' ','W','o','r','l','d','\0'}; 1.19 + 1.20 +static bool test_basic_1() 1.21 + { 1.22 + nsCStringContainer s; 1.23 + NS_CStringContainerInit(s); 1.24 + 1.25 + const char *ptr; 1.26 + uint32_t len; 1.27 + char *clone; 1.28 + 1.29 + NS_CStringGetData(s, &ptr); 1.30 + if (ptr == nullptr || *ptr != '\0') 1.31 + { 1.32 + NS_ERROR("unexpected result"); 1.33 + return false; 1.34 + } 1.35 + 1.36 + NS_CStringSetData(s, kAsciiData, UINT32_MAX); 1.37 + len = NS_CStringGetData(s, &ptr); 1.38 + if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) 1.39 + { 1.40 + NS_ERROR("unexpected result"); 1.41 + return false; 1.42 + } 1.43 + if (len != sizeof(kAsciiData)-1) 1.44 + { 1.45 + NS_ERROR("unexpected result"); 1.46 + return false; 1.47 + } 1.48 + 1.49 + clone = NS_CStringCloneData(s); 1.50 + if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) 1.51 + { 1.52 + NS_ERROR("unexpected result"); 1.53 + return false; 1.54 + } 1.55 + NS_Free(clone); 1.56 + 1.57 + nsCStringContainer temp; 1.58 + NS_CStringContainerInit(temp); 1.59 + NS_CStringCopy(temp, s); 1.60 + 1.61 + len = NS_CStringGetData(temp, &ptr); 1.62 + if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) 1.63 + { 1.64 + NS_ERROR("unexpected result"); 1.65 + return false; 1.66 + } 1.67 + if (len != sizeof(kAsciiData)-1) 1.68 + { 1.69 + NS_ERROR("unexpected result"); 1.70 + return false; 1.71 + } 1.72 + 1.73 + NS_CStringContainerFinish(temp); 1.74 + 1.75 + NS_CStringContainerFinish(s); 1.76 + return true; 1.77 + } 1.78 + 1.79 +static bool test_basic_2() 1.80 + { 1.81 + nsStringContainer s; 1.82 + NS_StringContainerInit(s); 1.83 + 1.84 + const char16_t *ptr; 1.85 + uint32_t len; 1.86 + char16_t *clone; 1.87 + 1.88 + NS_StringGetData(s, &ptr); 1.89 + if (ptr == nullptr || *ptr != '\0') 1.90 + { 1.91 + NS_ERROR("unexpected result"); 1.92 + return false; 1.93 + } 1.94 + 1.95 + NS_StringSetData(s, kUnicodeData, UINT32_MAX); 1.96 + len = NS_StringGetData(s, &ptr); 1.97 + if (len != sizeof(kUnicodeData)/2 - 1) 1.98 + { 1.99 + NS_ERROR("unexpected result"); 1.100 + return false; 1.101 + } 1.102 + if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) 1.103 + { 1.104 + NS_ERROR("unexpected result"); 1.105 + return false; 1.106 + } 1.107 + 1.108 + clone = NS_StringCloneData(s); 1.109 + if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) 1.110 + { 1.111 + NS_ERROR("unexpected result"); 1.112 + return false; 1.113 + } 1.114 + NS_Free(clone); 1.115 + 1.116 + nsStringContainer temp; 1.117 + NS_StringContainerInit(temp); 1.118 + NS_StringCopy(temp, s); 1.119 + 1.120 + len = NS_StringGetData(temp, &ptr); 1.121 + if (len != sizeof(kUnicodeData)/2 - 1) 1.122 + { 1.123 + NS_ERROR("unexpected result"); 1.124 + return false; 1.125 + } 1.126 + if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) 1.127 + { 1.128 + NS_ERROR("unexpected result"); 1.129 + return false; 1.130 + } 1.131 + 1.132 + NS_StringContainerFinish(temp); 1.133 + 1.134 + NS_StringContainerFinish(s); 1.135 + 1.136 + return true; 1.137 + } 1.138 + 1.139 +static bool test_convert() 1.140 + { 1.141 + nsStringContainer s; 1.142 + NS_StringContainerInit(s); 1.143 + NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1); 1.144 + 1.145 + nsCStringContainer temp; 1.146 + NS_CStringContainerInit(temp); 1.147 + 1.148 + const char *data; 1.149 + 1.150 + NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp); 1.151 + NS_CStringGetData(temp, &data); 1.152 + if (strcmp(data, kAsciiData) != 0) 1.153 + return false; 1.154 + 1.155 + NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp); 1.156 + NS_CStringGetData(temp, &data); 1.157 + if (strcmp(data, kAsciiData) != 0) 1.158 + return false; 1.159 + 1.160 + NS_CStringContainerFinish(temp); 1.161 + 1.162 + NS_StringContainerFinish(s); 1.163 + return true; 1.164 + } 1.165 + 1.166 +static bool test_append() 1.167 + { 1.168 + nsCStringContainer s; 1.169 + NS_CStringContainerInit(s); 1.170 + 1.171 + NS_CStringSetData(s, "foo"); 1.172 + NS_CStringAppendData(s, "bar"); 1.173 + 1.174 + NS_CStringContainerFinish(s); 1.175 + return true; 1.176 + } 1.177 + 1.178 +// Replace all occurrences of |matchVal| with |newVal| 1.179 +static void ReplaceSubstring( nsACString& str, 1.180 + const nsACString& matchVal, 1.181 + const nsACString& newVal ) 1.182 + { 1.183 + const char* sp, *mp, *np; 1.184 + uint32_t sl, ml, nl; 1.185 + 1.186 + sl = NS_CStringGetData(str, &sp); 1.187 + ml = NS_CStringGetData(matchVal, &mp); 1.188 + nl = NS_CStringGetData(newVal, &np); 1.189 + 1.190 + for (const char* iter = sp; iter <= sp + sl - ml; ++iter) 1.191 + { 1.192 + if (memcmp(iter, mp, ml) == 0) 1.193 + { 1.194 + uint32_t offset = iter - sp; 1.195 + 1.196 + NS_CStringSetDataRange(str, offset, ml, np, nl); 1.197 + 1.198 + sl = NS_CStringGetData(str, &sp); 1.199 + 1.200 + iter = sp + offset + nl - 1; 1.201 + } 1.202 + } 1.203 + } 1.204 + 1.205 +static bool test_replace_driver(const char *strVal, 1.206 + const char *matchVal, 1.207 + const char *newVal, 1.208 + const char *finalVal) 1.209 + { 1.210 + nsCStringContainer a; 1.211 + NS_CStringContainerInit(a); 1.212 + NS_CStringSetData(a, strVal); 1.213 + 1.214 + nsCStringContainer b; 1.215 + NS_CStringContainerInit(b); 1.216 + NS_CStringSetData(b, matchVal); 1.217 + 1.218 + nsCStringContainer c; 1.219 + NS_CStringContainerInit(c); 1.220 + NS_CStringSetData(c, newVal); 1.221 + 1.222 + ReplaceSubstring(a, b, c); 1.223 + 1.224 + const char *data; 1.225 + NS_CStringGetData(a, &data); 1.226 + if (strcmp(data, finalVal) != 0) 1.227 + return false; 1.228 + 1.229 + NS_CStringContainerFinish(c); 1.230 + NS_CStringContainerFinish(b); 1.231 + NS_CStringContainerFinish(a); 1.232 + return true; 1.233 + } 1.234 + 1.235 +static bool test_replace() 1.236 + { 1.237 + bool rv; 1.238 + 1.239 + rv = test_replace_driver("hello world, hello again!", 1.240 + "hello", 1.241 + "goodbye", 1.242 + "goodbye world, goodbye again!"); 1.243 + if (!rv) 1.244 + return rv; 1.245 + 1.246 + rv = test_replace_driver("foofoofoofoo!", 1.247 + "foo", 1.248 + "bar", 1.249 + "barbarbarbar!"); 1.250 + if (!rv) 1.251 + return rv; 1.252 + 1.253 + rv = test_replace_driver("foo bar systems", 1.254 + "xyz", 1.255 + "crazy", 1.256 + "foo bar systems"); 1.257 + if (!rv) 1.258 + return rv; 1.259 + 1.260 + rv = test_replace_driver("oh", 1.261 + "xyz", 1.262 + "crazy", 1.263 + "oh"); 1.264 + if (!rv) 1.265 + return rv; 1.266 + 1.267 + return true; 1.268 + } 1.269 + 1.270 +static const char* kWhitespace="\b\t\r\n "; 1.271 + 1.272 +static void 1.273 +CompressWhitespace(nsACString &str) 1.274 + { 1.275 + const char *p; 1.276 + int32_t i, len = (int32_t) NS_CStringGetData(str, &p); 1.277 + 1.278 + // trim leading whitespace 1.279 + 1.280 + for (i=0; i<len; ++i) 1.281 + { 1.282 + if (!strchr(kWhitespace, (char) p[i])) 1.283 + break; 1.284 + } 1.285 + 1.286 + if (i>0) 1.287 + { 1.288 + NS_CStringCutData(str, 0, i); 1.289 + len = (int32_t) NS_CStringGetData(str, &p); 1.290 + } 1.291 + 1.292 + // trim trailing whitespace 1.293 + 1.294 + for (i=len-1; i>=0; --i) 1.295 + { 1.296 + if (!strchr(kWhitespace, (char) p[i])) 1.297 + break; 1.298 + } 1.299 + 1.300 + if (++i < len) 1.301 + NS_CStringCutData(str, i, len - i); 1.302 + } 1.303 + 1.304 +static bool test_compress_ws() 1.305 + { 1.306 + nsCStringContainer s; 1.307 + NS_CStringContainerInit(s); 1.308 + NS_CStringSetData(s, " \thello world\r \n"); 1.309 + CompressWhitespace(s); 1.310 + const char *d; 1.311 + NS_CStringGetData(s, &d); 1.312 + bool rv = !strcmp(d, "hello world"); 1.313 + if (!rv) 1.314 + printf("=> \"%s\"\n", d); 1.315 + NS_CStringContainerFinish(s); 1.316 + return rv; 1.317 + } 1.318 + 1.319 +static bool test_depend() 1.320 + { 1.321 + static const char kData[] = "hello world"; 1.322 + 1.323 + nsCStringContainer s; 1.324 + NS_ENSURE_SUCCESS( 1.325 + NS_CStringContainerInit2(s, kData, sizeof(kData)-1, 1.326 + NS_CSTRING_CONTAINER_INIT_DEPEND), 1.327 + false); 1.328 + 1.329 + const char *sd; 1.330 + NS_CStringGetData(s, &sd); 1.331 + 1.332 + bool rv = (sd == kData); 1.333 + NS_CStringContainerFinish(s); 1.334 + return rv; 1.335 + } 1.336 + 1.337 +static bool test_depend_sub() 1.338 + { 1.339 + static const char kData[] = "hello world"; 1.340 + 1.341 + nsCStringContainer s; 1.342 + NS_ENSURE_SUCCESS( 1.343 + NS_CStringContainerInit2(s, kData, sizeof(kData)-1, 1.344 + NS_CSTRING_CONTAINER_INIT_DEPEND | 1.345 + NS_CSTRING_CONTAINER_INIT_SUBSTRING), 1.346 + false); 1.347 + 1.348 + bool terminated; 1.349 + const char *sd; 1.350 + uint32_t len = NS_CStringGetData(s, &sd, &terminated); 1.351 + 1.352 + bool rv = (sd == kData && len == sizeof(kData)-1 && !terminated); 1.353 + NS_CStringContainerFinish(s); 1.354 + return rv; 1.355 + } 1.356 + 1.357 +static bool test_adopt() 1.358 + { 1.359 + static const char kData[] = "hello world"; 1.360 + 1.361 + char *data = (char *) nsMemory::Clone(kData, sizeof(kData)); 1.362 + if (!data) 1.363 + return false; 1.364 + 1.365 + nsCStringContainer s; 1.366 + NS_ENSURE_SUCCESS( 1.367 + NS_CStringContainerInit2(s, data, UINT32_MAX, 1.368 + NS_CSTRING_CONTAINER_INIT_ADOPT), 1.369 + false); // leaks data on failure *shrug* 1.370 + 1.371 + const char *sd; 1.372 + NS_CStringGetData(s, &sd); 1.373 + 1.374 + bool rv = (sd == data); 1.375 + NS_CStringContainerFinish(s); 1.376 + return rv; 1.377 + } 1.378 + 1.379 +static bool test_adopt_sub() 1.380 + { 1.381 + static const char kData[] = "hello world"; 1.382 + 1.383 + char *data = (char *) nsMemory::Clone(kData, sizeof(kData)-1); 1.384 + if (!data) 1.385 + return false; 1.386 + 1.387 + nsCStringContainer s; 1.388 + NS_ENSURE_SUCCESS( 1.389 + NS_CStringContainerInit2(s, data, sizeof(kData)-1, 1.390 + NS_CSTRING_CONTAINER_INIT_ADOPT | 1.391 + NS_CSTRING_CONTAINER_INIT_SUBSTRING), 1.392 + false); // leaks data on failure *shrug* 1.393 + 1.394 + bool terminated; 1.395 + const char *sd; 1.396 + uint32_t len = NS_CStringGetData(s, &sd, &terminated); 1.397 + 1.398 + bool rv = (sd == data && len == sizeof(kData)-1 && !terminated); 1.399 + NS_CStringContainerFinish(s); 1.400 + return rv; 1.401 + } 1.402 + 1.403 +static bool test_mutation() 1.404 + { 1.405 + nsCStringContainer s; 1.406 + NS_CStringContainerInit(s); 1.407 + 1.408 + const char kText[] = "Every good boy does fine."; 1.409 + 1.410 + char *buf; 1.411 + uint32_t len = NS_CStringGetMutableData(s, sizeof(kText) - 1, &buf); 1.412 + if (!buf || len != sizeof(kText) - 1) 1.413 + return false; 1.414 + memcpy(buf, kText, sizeof(kText)); 1.415 + 1.416 + const char *data; 1.417 + NS_CStringGetData(s, &data); 1.418 + if (strcmp(data, kText) != 0) 1.419 + return false; 1.420 + 1.421 + uint32_t newLen = len + 1; 1.422 + len = NS_CStringGetMutableData(s, newLen, &buf); 1.423 + if (!buf || len != newLen) 1.424 + return false; 1.425 + 1.426 + buf[len - 1] = '.'; 1.427 + 1.428 + NS_CStringGetData(s, &data); 1.429 + if (strncmp(data, kText, len - 1) != 0 || data[len - 1] != '.') 1.430 + return false; 1.431 + 1.432 + NS_CStringContainerFinish(s); 1.433 + return true; 1.434 + } 1.435 + 1.436 +static bool test_ascii() 1.437 +{ 1.438 + nsCString testCString; 1.439 + testCString.AppendASCII(kAsciiData); 1.440 + if (!testCString.EqualsLiteral(kAsciiData)) 1.441 + return false; 1.442 + 1.443 + testCString.AssignASCII(kAsciiData); 1.444 + if (!testCString.LowerCaseEqualsLiteral("hello world")) 1.445 + return false; 1.446 + 1.447 + nsString testString; 1.448 + testString.AppendASCII(kAsciiData); 1.449 + if (!testString.EqualsLiteral(kAsciiData)) 1.450 + return false; 1.451 + 1.452 + testString.AssignASCII(kAsciiData); 1.453 + if (!testString.LowerCaseEqualsLiteral("hello world")) 1.454 + return false; 1.455 + 1.456 + return true; 1.457 +} 1.458 + 1.459 +static bool test_chars() 1.460 +{ 1.461 + nsCString testCString(kAsciiData); 1.462 + if (testCString.First() != 'H') 1.463 + return false; 1.464 + if (testCString.Last() != 'd') 1.465 + return false; 1.466 + testCString.SetCharAt('u', 8); 1.467 + if (!testCString.EqualsASCII("Hello Would")) 1.468 + return false; 1.469 + 1.470 + nsString testString(kUnicodeData); 1.471 + if (testString.First() != 'H') 1.472 + return false; 1.473 + if (testString.Last() != 'd') 1.474 + return false; 1.475 + testString.SetCharAt('u', 8); 1.476 + if (!testString.EqualsASCII("Hello Would")) 1.477 + return false; 1.478 + 1.479 + return true; 1.480 +} 1.481 + 1.482 +static bool test_stripchars() 1.483 +{ 1.484 + nsCString test(kAsciiData); 1.485 + test.StripChars("ld"); 1.486 + if (!test.Equals("Heo Wor")) 1.487 + return false; 1.488 + 1.489 + test.Assign(kAsciiData); 1.490 + test.StripWhitespace(); 1.491 + if (!test.Equals("HelloWorld")) 1.492 + return false; 1.493 + 1.494 + return true; 1.495 +} 1.496 + 1.497 +static bool test_trim() 1.498 +{ 1.499 + static const char kWS[] = "\n\t\r "; 1.500 + static const char kTestString[] = " \n\tTesting...\n\r"; 1.501 + 1.502 + nsCString test1(kTestString); 1.503 + nsCString test2(kTestString); 1.504 + nsCString test3(kTestString); 1.505 + 1.506 + test1.Trim(kWS); 1.507 + test2.Trim(kWS, true, false); 1.508 + test3.Trim(kWS, false, true); 1.509 + 1.510 + if (!test1.Equals("Testing...")) 1.511 + return false; 1.512 + 1.513 + if (!test2.Equals("Testing...\n\r")) 1.514 + return false; 1.515 + 1.516 + if (!test3.Equals(" \n\tTesting...")) 1.517 + return false; 1.518 + 1.519 + return true; 1.520 +} 1.521 + 1.522 +static bool test_find() 1.523 +{ 1.524 + nsString uni(kUnicodeData); 1.525 + 1.526 + static const char kHello[] = "Hello"; 1.527 + static const char khello[] = "hello"; 1.528 + static const char kBye[] = "Bye!"; 1.529 + 1.530 + int32_t found; 1.531 + 1.532 + found = uni.Find(kHello); 1.533 + if (found != 0) 1.534 + return false; 1.535 + 1.536 + found = uni.Find(khello, false); 1.537 + if (found != -1) 1.538 + return false; 1.539 + 1.540 + found = uni.Find(khello, true); 1.541 + if (found != 0) 1.542 + return false; 1.543 + 1.544 + found = uni.Find(kBye); 1.545 + if (found != -1) 1.546 + return false; 1.547 + 1.548 + found = uni.Find(NS_LITERAL_STRING("World")); 1.549 + if (found != 6) 1.550 + return false; 1.551 + 1.552 + found = uni.Find(uni); 1.553 + if (found != 0) 1.554 + return false; 1.555 + 1.556 + return true; 1.557 +} 1.558 + 1.559 +static bool test_compressws() 1.560 +{ 1.561 + nsString check(NS_LITERAL_STRING(" \tTesting \n\t1\n 2 3\n ")); 1.562 + CompressWhitespace(check); 1.563 + return check.Equals(NS_LITERAL_STRING("Testing 1 2 3")); 1.564 +} 1.565 + 1.566 +static bool test_comparisons() 1.567 +{ 1.568 + bool result; 1.569 + 1.570 + // nsString 1.571 + 1.572 + NS_NAMED_LITERAL_STRING(shortString1, "Foo"); 1.573 + NS_NAMED_LITERAL_STRING(shortString2, "Bar"); 1.574 + NS_NAMED_LITERAL_STRING(shortString3, "Bar"); 1.575 + NS_NAMED_LITERAL_STRING(shortString4, "bar"); 1.576 + NS_NAMED_LITERAL_STRING(longString, "FooBar"); 1.577 + 1.578 + // == 1.579 + 1.580 + result = (shortString1 == shortString2); 1.581 + if (result) 1.582 + return false; 1.583 + 1.584 + result = (shortString2 == shortString3); 1.585 + if (!result) 1.586 + return false; 1.587 + 1.588 + result = (shortString3 == shortString4); 1.589 + if (result) 1.590 + return false; 1.591 + 1.592 + result = (shortString1 == longString); 1.593 + if (result) 1.594 + return false; 1.595 + 1.596 + result = (longString == shortString1); 1.597 + if (result) 1.598 + return false; 1.599 + 1.600 + // != 1.601 + 1.602 + result = (shortString1 != shortString2); 1.603 + if (!result) 1.604 + return false; 1.605 + 1.606 + result = (shortString2 != shortString3); 1.607 + if (result) 1.608 + return false; 1.609 + 1.610 + result = (shortString3 != shortString4); 1.611 + if (!result) 1.612 + return false; 1.613 + 1.614 + result = (shortString1 != longString); 1.615 + if (!result) 1.616 + return false; 1.617 + 1.618 + result = (longString != shortString1); 1.619 + if (!result) 1.620 + return false; 1.621 + 1.622 + // < 1.623 + 1.624 + result = (shortString1 < shortString2); 1.625 + if (result) 1.626 + return false; 1.627 + 1.628 + result = (shortString2 < shortString1); 1.629 + if (!result) 1.630 + return false; 1.631 + 1.632 + result = (shortString1 < longString); 1.633 + if (!result) 1.634 + return false; 1.635 + 1.636 + result = (longString < shortString1); 1.637 + if (result) 1.638 + return false; 1.639 + 1.640 + result = (shortString2 < shortString3); 1.641 + if (result) 1.642 + return false; 1.643 + 1.644 + result = (shortString3 < shortString4); 1.645 + if (!result) 1.646 + return false; 1.647 + 1.648 + result = (shortString4 < shortString3); 1.649 + if (result) 1.650 + return false; 1.651 + 1.652 + // <= 1.653 + 1.654 + result = (shortString1 <= shortString2); 1.655 + if (result) 1.656 + return false; 1.657 + 1.658 + result = (shortString2 <= shortString1); 1.659 + if (!result) 1.660 + return false; 1.661 + 1.662 + result = (shortString1 <= longString); 1.663 + if (!result) 1.664 + return false; 1.665 + 1.666 + result = (longString <= shortString1); 1.667 + if (result) 1.668 + return false; 1.669 + 1.670 + result = (shortString2 <= shortString3); 1.671 + if (!result) 1.672 + return false; 1.673 + 1.674 + result = (shortString3 <= shortString4); 1.675 + if (!result) 1.676 + return false; 1.677 + 1.678 + result = (shortString4 <= shortString3); 1.679 + if (result) 1.680 + return false; 1.681 + 1.682 + // > 1.683 + 1.684 + result = (shortString1 > shortString2); 1.685 + if (!result) 1.686 + return false; 1.687 + 1.688 + result = (shortString2 > shortString1); 1.689 + if (result) 1.690 + return false; 1.691 + 1.692 + result = (shortString1 > longString); 1.693 + if (result) 1.694 + return false; 1.695 + 1.696 + result = (longString > shortString1); 1.697 + if (!result) 1.698 + return false; 1.699 + 1.700 + result = (shortString2 > shortString3); 1.701 + if (result) 1.702 + return false; 1.703 + 1.704 + result = (shortString3 > shortString4); 1.705 + if (result) 1.706 + return false; 1.707 + 1.708 + result = (shortString4 > shortString3); 1.709 + if (!result) 1.710 + return false; 1.711 + 1.712 + // >= 1.713 + 1.714 + result = (shortString1 >= shortString2); 1.715 + if (!result) 1.716 + return false; 1.717 + 1.718 + result = (shortString2 >= shortString1); 1.719 + if (result) 1.720 + return false; 1.721 + 1.722 + result = (shortString1 >= longString); 1.723 + if (result) 1.724 + return false; 1.725 + 1.726 + result = (longString >= shortString1); 1.727 + if (!result) 1.728 + return false; 1.729 + 1.730 + result = (shortString2 >= shortString3); 1.731 + if (!result) 1.732 + return false; 1.733 + 1.734 + result = (shortString3 >= shortString4); 1.735 + if (result) 1.736 + return false; 1.737 + 1.738 + result = (shortString4 >= shortString3); 1.739 + if (!result) 1.740 + return false; 1.741 + 1.742 + // nsCString 1.743 + 1.744 + NS_NAMED_LITERAL_CSTRING(shortCString1, "Foo"); 1.745 + NS_NAMED_LITERAL_CSTRING(shortCString2, "Bar"); 1.746 + NS_NAMED_LITERAL_CSTRING(shortCString3, "Bar"); 1.747 + NS_NAMED_LITERAL_CSTRING(shortCString4, "bar"); 1.748 + NS_NAMED_LITERAL_CSTRING(longCString, "FooBar"); 1.749 + 1.750 + // == 1.751 + 1.752 + result = (shortCString1 == shortCString2); 1.753 + if (result) 1.754 + return false; 1.755 + 1.756 + result = (shortCString2 == shortCString3); 1.757 + if (!result) 1.758 + return false; 1.759 + 1.760 + result = (shortCString3 == shortCString4); 1.761 + if (result) 1.762 + return false; 1.763 + 1.764 + result = (shortCString1 == longCString); 1.765 + if (result) 1.766 + return false; 1.767 + 1.768 + result = (longCString == shortCString1); 1.769 + if (result) 1.770 + return false; 1.771 + 1.772 + // != 1.773 + 1.774 + result = (shortCString1 != shortCString2); 1.775 + if (!result) 1.776 + return false; 1.777 + 1.778 + result = (shortCString2 != shortCString3); 1.779 + if (result) 1.780 + return false; 1.781 + 1.782 + result = (shortCString3 != shortCString4); 1.783 + if (!result) 1.784 + return false; 1.785 + 1.786 + result = (shortCString1 != longCString); 1.787 + if (!result) 1.788 + return false; 1.789 + 1.790 + result = (longCString != shortCString1); 1.791 + if (!result) 1.792 + return false; 1.793 + 1.794 + // < 1.795 + 1.796 + result = (shortCString1 < shortCString2); 1.797 + if (result) 1.798 + return false; 1.799 + 1.800 + result = (shortCString2 < shortCString1); 1.801 + if (!result) 1.802 + return false; 1.803 + 1.804 + result = (shortCString1 < longCString); 1.805 + if (!result) 1.806 + return false; 1.807 + 1.808 + result = (longCString < shortCString1); 1.809 + if (result) 1.810 + return false; 1.811 + 1.812 + result = (shortCString2 < shortCString3); 1.813 + if (result) 1.814 + return false; 1.815 + 1.816 + result = (shortCString3 < shortCString4); 1.817 + if (!result) 1.818 + return false; 1.819 + 1.820 + result = (shortCString4 < shortCString3); 1.821 + if (result) 1.822 + return false; 1.823 + 1.824 + // <= 1.825 + 1.826 + result = (shortCString1 <= shortCString2); 1.827 + if (result) 1.828 + return false; 1.829 + 1.830 + result = (shortCString2 <= shortCString1); 1.831 + if (!result) 1.832 + return false; 1.833 + 1.834 + result = (shortCString1 <= longCString); 1.835 + if (!result) 1.836 + return false; 1.837 + 1.838 + result = (longCString <= shortCString1); 1.839 + if (result) 1.840 + return false; 1.841 + 1.842 + result = (shortCString2 <= shortCString3); 1.843 + if (!result) 1.844 + return false; 1.845 + 1.846 + result = (shortCString3 <= shortCString4); 1.847 + if (!result) 1.848 + return false; 1.849 + 1.850 + result = (shortCString4 <= shortCString3); 1.851 + if (result) 1.852 + return false; 1.853 + 1.854 + // > 1.855 + 1.856 + result = (shortCString1 > shortCString2); 1.857 + if (!result) 1.858 + return false; 1.859 + 1.860 + result = (shortCString2 > shortCString1); 1.861 + if (result) 1.862 + return false; 1.863 + 1.864 + result = (shortCString1 > longCString); 1.865 + if (result) 1.866 + return false; 1.867 + 1.868 + result = (longCString > shortCString1); 1.869 + if (!result) 1.870 + return false; 1.871 + 1.872 + result = (shortCString2 > shortCString3); 1.873 + if (result) 1.874 + return false; 1.875 + 1.876 + result = (shortCString3 > shortCString4); 1.877 + if (result) 1.878 + return false; 1.879 + 1.880 + result = (shortCString4 > shortCString3); 1.881 + if (!result) 1.882 + return false; 1.883 + 1.884 + // >= 1.885 + 1.886 + result = (shortCString1 >= shortCString2); 1.887 + if (!result) 1.888 + return false; 1.889 + 1.890 + result = (shortCString2 >= shortCString1); 1.891 + if (result) 1.892 + return false; 1.893 + 1.894 + result = (shortCString1 >= longCString); 1.895 + if (result) 1.896 + return false; 1.897 + 1.898 + result = (longCString >= shortCString1); 1.899 + if (!result) 1.900 + return false; 1.901 + 1.902 + result = (shortCString2 >= shortCString3); 1.903 + if (!result) 1.904 + return false; 1.905 + 1.906 + result = (shortCString3 >= shortCString4); 1.907 + if (result) 1.908 + return false; 1.909 + 1.910 + result = (shortCString4 >= shortCString3); 1.911 + if (!result) 1.912 + return false; 1.913 + 1.914 + return true; 1.915 +} 1.916 + 1.917 +static bool test_parse_string_helper(const char* str, char separator, int len, 1.918 + const char* s1, const char* s2) 1.919 +{ 1.920 + nsCString data(str); 1.921 + nsTArray<nsCString> results; 1.922 + if (!ParseString(data, separator, results)) 1.923 + return false; 1.924 + if (int(results.Length()) != len) 1.925 + return false; 1.926 + const char* strings[] = { s1, s2 }; 1.927 + for (int i = 0; i < len; ++i) { 1.928 + if (!results[i].Equals(strings[i])) 1.929 + return false; 1.930 + } 1.931 + return true; 1.932 +} 1.933 + 1.934 +static bool test_parse_string_helper0(const char* str, char separator) 1.935 +{ 1.936 + return test_parse_string_helper(str, separator, 0, nullptr, nullptr); 1.937 +} 1.938 + 1.939 +static bool test_parse_string_helper1(const char* str, char separator, const char* s1) 1.940 +{ 1.941 + return test_parse_string_helper(str, separator, 1, s1, nullptr); 1.942 +} 1.943 + 1.944 +static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2) 1.945 +{ 1.946 + return test_parse_string_helper(str, separator, 2, s1, s2); 1.947 +} 1.948 + 1.949 +static bool test_parse_string() 1.950 +{ 1.951 + return test_parse_string_helper1("foo, bar", '_', "foo, bar") && 1.952 + test_parse_string_helper2("foo, bar", ',', "foo", " bar") && 1.953 + test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") && 1.954 + test_parse_string_helper2("foo,bar", 'o', "f", ",bar") && 1.955 + test_parse_string_helper0("", '_') && 1.956 + test_parse_string_helper0(" ", ' ') && 1.957 + test_parse_string_helper1(" foo", ' ', "foo") && 1.958 + test_parse_string_helper1(" foo", ' ', "foo"); 1.959 +} 1.960 + 1.961 +//---- 1.962 + 1.963 +typedef bool (*TestFunc)(); 1.964 + 1.965 +static const struct Test 1.966 + { 1.967 + const char* name; 1.968 + TestFunc func; 1.969 + } 1.970 +tests[] = 1.971 + { 1.972 + { "test_basic_1", test_basic_1 }, 1.973 + { "test_basic_2", test_basic_2 }, 1.974 + { "test_convert", test_convert }, 1.975 + { "test_append", test_append }, 1.976 + { "test_replace", test_replace }, 1.977 + { "test_compress_ws", test_compress_ws }, 1.978 + { "test_depend", test_depend }, 1.979 + { "test_depend_sub", test_depend_sub }, 1.980 + { "test_adopt", test_adopt }, 1.981 + { "test_adopt_sub", test_adopt_sub }, 1.982 + { "test_mutation", test_mutation }, 1.983 + { "test_ascii", test_ascii }, 1.984 + { "test_chars", test_chars }, 1.985 + { "test_stripchars", test_stripchars }, 1.986 + { "test_trim", test_trim }, 1.987 + { "test_find", test_find }, 1.988 + { "test_compressws", test_compressws }, 1.989 + { "test_comparisons", test_comparisons }, 1.990 + { "test_parse_string", test_parse_string }, 1.991 + { nullptr, nullptr } 1.992 + }; 1.993 + 1.994 +//---- 1.995 + 1.996 +int main(int argc, char **argv) 1.997 + { 1.998 + int count = 1; 1.999 + if (argc > 1) 1.1000 + count = atoi(argv[1]); 1.1001 + 1.1002 + while (count--) 1.1003 + { 1.1004 + for (const Test* t = tests; t->name != nullptr; ++t) 1.1005 + { 1.1006 + printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE"); 1.1007 + } 1.1008 + } 1.1009 + 1.1010 + return 0; 1.1011 + }