michael@0: /* vim:set ts=2 sw=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsStringAPI.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsMemory.h" michael@0: michael@0: static const char kAsciiData[] = "Hello World"; michael@0: michael@0: static const char16_t kUnicodeData[] = michael@0: {'H','e','l','l','o',' ','W','o','r','l','d','\0'}; michael@0: michael@0: static bool test_basic_1() michael@0: { michael@0: nsCStringContainer s; michael@0: NS_CStringContainerInit(s); michael@0: michael@0: const char *ptr; michael@0: uint32_t len; michael@0: char *clone; michael@0: michael@0: NS_CStringGetData(s, &ptr); michael@0: if (ptr == nullptr || *ptr != '\0') michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: NS_CStringSetData(s, kAsciiData, UINT32_MAX); michael@0: len = NS_CStringGetData(s, &ptr); michael@0: if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: if (len != sizeof(kAsciiData)-1) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: clone = NS_CStringCloneData(s); michael@0: if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: NS_Free(clone); michael@0: michael@0: nsCStringContainer temp; michael@0: NS_CStringContainerInit(temp); michael@0: NS_CStringCopy(temp, s); michael@0: michael@0: len = NS_CStringGetData(temp, &ptr); michael@0: if (ptr == nullptr || strcmp(ptr, kAsciiData) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: if (len != sizeof(kAsciiData)-1) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: NS_CStringContainerFinish(temp); michael@0: michael@0: NS_CStringContainerFinish(s); michael@0: return true; michael@0: } michael@0: michael@0: static bool test_basic_2() michael@0: { michael@0: nsStringContainer s; michael@0: NS_StringContainerInit(s); michael@0: michael@0: const char16_t *ptr; michael@0: uint32_t len; michael@0: char16_t *clone; michael@0: michael@0: NS_StringGetData(s, &ptr); michael@0: if (ptr == nullptr || *ptr != '\0') michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: NS_StringSetData(s, kUnicodeData, UINT32_MAX); michael@0: len = NS_StringGetData(s, &ptr); michael@0: if (len != sizeof(kUnicodeData)/2 - 1) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: clone = NS_StringCloneData(s); michael@0: if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: NS_Free(clone); michael@0: michael@0: nsStringContainer temp; michael@0: NS_StringContainerInit(temp); michael@0: NS_StringCopy(temp, s); michael@0: michael@0: len = NS_StringGetData(temp, &ptr); michael@0: if (len != sizeof(kUnicodeData)/2 - 1) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: if (ptr == nullptr || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0) michael@0: { michael@0: NS_ERROR("unexpected result"); michael@0: return false; michael@0: } michael@0: michael@0: NS_StringContainerFinish(temp); michael@0: michael@0: NS_StringContainerFinish(s); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_convert() michael@0: { michael@0: nsStringContainer s; michael@0: NS_StringContainerInit(s); michael@0: NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1); michael@0: michael@0: nsCStringContainer temp; michael@0: NS_CStringContainerInit(temp); michael@0: michael@0: const char *data; michael@0: michael@0: NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp); michael@0: NS_CStringGetData(temp, &data); michael@0: if (strcmp(data, kAsciiData) != 0) michael@0: return false; michael@0: michael@0: NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp); michael@0: NS_CStringGetData(temp, &data); michael@0: if (strcmp(data, kAsciiData) != 0) michael@0: return false; michael@0: michael@0: NS_CStringContainerFinish(temp); michael@0: michael@0: NS_StringContainerFinish(s); michael@0: return true; michael@0: } michael@0: michael@0: static bool test_append() michael@0: { michael@0: nsCStringContainer s; michael@0: NS_CStringContainerInit(s); michael@0: michael@0: NS_CStringSetData(s, "foo"); michael@0: NS_CStringAppendData(s, "bar"); michael@0: michael@0: NS_CStringContainerFinish(s); michael@0: return true; michael@0: } michael@0: michael@0: // Replace all occurrences of |matchVal| with |newVal| michael@0: static void ReplaceSubstring( nsACString& str, michael@0: const nsACString& matchVal, michael@0: const nsACString& newVal ) michael@0: { michael@0: const char* sp, *mp, *np; michael@0: uint32_t sl, ml, nl; michael@0: michael@0: sl = NS_CStringGetData(str, &sp); michael@0: ml = NS_CStringGetData(matchVal, &mp); michael@0: nl = NS_CStringGetData(newVal, &np); michael@0: michael@0: for (const char* iter = sp; iter <= sp + sl - ml; ++iter) michael@0: { michael@0: if (memcmp(iter, mp, ml) == 0) michael@0: { michael@0: uint32_t offset = iter - sp; michael@0: michael@0: NS_CStringSetDataRange(str, offset, ml, np, nl); michael@0: michael@0: sl = NS_CStringGetData(str, &sp); michael@0: michael@0: iter = sp + offset + nl - 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static bool test_replace_driver(const char *strVal, michael@0: const char *matchVal, michael@0: const char *newVal, michael@0: const char *finalVal) michael@0: { michael@0: nsCStringContainer a; michael@0: NS_CStringContainerInit(a); michael@0: NS_CStringSetData(a, strVal); michael@0: michael@0: nsCStringContainer b; michael@0: NS_CStringContainerInit(b); michael@0: NS_CStringSetData(b, matchVal); michael@0: michael@0: nsCStringContainer c; michael@0: NS_CStringContainerInit(c); michael@0: NS_CStringSetData(c, newVal); michael@0: michael@0: ReplaceSubstring(a, b, c); michael@0: michael@0: const char *data; michael@0: NS_CStringGetData(a, &data); michael@0: if (strcmp(data, finalVal) != 0) michael@0: return false; michael@0: michael@0: NS_CStringContainerFinish(c); michael@0: NS_CStringContainerFinish(b); michael@0: NS_CStringContainerFinish(a); michael@0: return true; michael@0: } michael@0: michael@0: static bool test_replace() michael@0: { michael@0: bool rv; michael@0: michael@0: rv = test_replace_driver("hello world, hello again!", michael@0: "hello", michael@0: "goodbye", michael@0: "goodbye world, goodbye again!"); michael@0: if (!rv) michael@0: return rv; michael@0: michael@0: rv = test_replace_driver("foofoofoofoo!", michael@0: "foo", michael@0: "bar", michael@0: "barbarbarbar!"); michael@0: if (!rv) michael@0: return rv; michael@0: michael@0: rv = test_replace_driver("foo bar systems", michael@0: "xyz", michael@0: "crazy", michael@0: "foo bar systems"); michael@0: if (!rv) michael@0: return rv; michael@0: michael@0: rv = test_replace_driver("oh", michael@0: "xyz", michael@0: "crazy", michael@0: "oh"); michael@0: if (!rv) michael@0: return rv; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static const char* kWhitespace="\b\t\r\n "; michael@0: michael@0: static void michael@0: CompressWhitespace(nsACString &str) michael@0: { michael@0: const char *p; michael@0: int32_t i, len = (int32_t) NS_CStringGetData(str, &p); michael@0: michael@0: // trim leading whitespace michael@0: michael@0: for (i=0; i0) michael@0: { michael@0: NS_CStringCutData(str, 0, i); michael@0: len = (int32_t) NS_CStringGetData(str, &p); michael@0: } michael@0: michael@0: // trim trailing whitespace michael@0: michael@0: for (i=len-1; i>=0; --i) michael@0: { michael@0: if (!strchr(kWhitespace, (char) p[i])) michael@0: break; michael@0: } michael@0: michael@0: if (++i < len) michael@0: NS_CStringCutData(str, i, len - i); michael@0: } michael@0: michael@0: static bool test_compress_ws() michael@0: { michael@0: nsCStringContainer s; michael@0: NS_CStringContainerInit(s); michael@0: NS_CStringSetData(s, " \thello world\r \n"); michael@0: CompressWhitespace(s); michael@0: const char *d; michael@0: NS_CStringGetData(s, &d); michael@0: bool rv = !strcmp(d, "hello world"); michael@0: if (!rv) michael@0: printf("=> \"%s\"\n", d); michael@0: NS_CStringContainerFinish(s); michael@0: return rv; michael@0: } michael@0: michael@0: static bool test_depend() michael@0: { michael@0: static const char kData[] = "hello world"; michael@0: michael@0: nsCStringContainer s; michael@0: NS_ENSURE_SUCCESS( michael@0: NS_CStringContainerInit2(s, kData, sizeof(kData)-1, michael@0: NS_CSTRING_CONTAINER_INIT_DEPEND), michael@0: false); michael@0: michael@0: const char *sd; michael@0: NS_CStringGetData(s, &sd); michael@0: michael@0: bool rv = (sd == kData); michael@0: NS_CStringContainerFinish(s); michael@0: return rv; michael@0: } michael@0: michael@0: static bool test_depend_sub() michael@0: { michael@0: static const char kData[] = "hello world"; michael@0: michael@0: nsCStringContainer s; michael@0: NS_ENSURE_SUCCESS( michael@0: NS_CStringContainerInit2(s, kData, sizeof(kData)-1, michael@0: NS_CSTRING_CONTAINER_INIT_DEPEND | michael@0: NS_CSTRING_CONTAINER_INIT_SUBSTRING), michael@0: false); michael@0: michael@0: bool terminated; michael@0: const char *sd; michael@0: uint32_t len = NS_CStringGetData(s, &sd, &terminated); michael@0: michael@0: bool rv = (sd == kData && len == sizeof(kData)-1 && !terminated); michael@0: NS_CStringContainerFinish(s); michael@0: return rv; michael@0: } michael@0: michael@0: static bool test_adopt() michael@0: { michael@0: static const char kData[] = "hello world"; michael@0: michael@0: char *data = (char *) nsMemory::Clone(kData, sizeof(kData)); michael@0: if (!data) michael@0: return false; michael@0: michael@0: nsCStringContainer s; michael@0: NS_ENSURE_SUCCESS( michael@0: NS_CStringContainerInit2(s, data, UINT32_MAX, michael@0: NS_CSTRING_CONTAINER_INIT_ADOPT), michael@0: false); // leaks data on failure *shrug* michael@0: michael@0: const char *sd; michael@0: NS_CStringGetData(s, &sd); michael@0: michael@0: bool rv = (sd == data); michael@0: NS_CStringContainerFinish(s); michael@0: return rv; michael@0: } michael@0: michael@0: static bool test_adopt_sub() michael@0: { michael@0: static const char kData[] = "hello world"; michael@0: michael@0: char *data = (char *) nsMemory::Clone(kData, sizeof(kData)-1); michael@0: if (!data) michael@0: return false; michael@0: michael@0: nsCStringContainer s; michael@0: NS_ENSURE_SUCCESS( michael@0: NS_CStringContainerInit2(s, data, sizeof(kData)-1, michael@0: NS_CSTRING_CONTAINER_INIT_ADOPT | michael@0: NS_CSTRING_CONTAINER_INIT_SUBSTRING), michael@0: false); // leaks data on failure *shrug* michael@0: michael@0: bool terminated; michael@0: const char *sd; michael@0: uint32_t len = NS_CStringGetData(s, &sd, &terminated); michael@0: michael@0: bool rv = (sd == data && len == sizeof(kData)-1 && !terminated); michael@0: NS_CStringContainerFinish(s); michael@0: return rv; michael@0: } michael@0: michael@0: static bool test_mutation() michael@0: { michael@0: nsCStringContainer s; michael@0: NS_CStringContainerInit(s); michael@0: michael@0: const char kText[] = "Every good boy does fine."; michael@0: michael@0: char *buf; michael@0: uint32_t len = NS_CStringGetMutableData(s, sizeof(kText) - 1, &buf); michael@0: if (!buf || len != sizeof(kText) - 1) michael@0: return false; michael@0: memcpy(buf, kText, sizeof(kText)); michael@0: michael@0: const char *data; michael@0: NS_CStringGetData(s, &data); michael@0: if (strcmp(data, kText) != 0) michael@0: return false; michael@0: michael@0: uint32_t newLen = len + 1; michael@0: len = NS_CStringGetMutableData(s, newLen, &buf); michael@0: if (!buf || len != newLen) michael@0: return false; michael@0: michael@0: buf[len - 1] = '.'; michael@0: michael@0: NS_CStringGetData(s, &data); michael@0: if (strncmp(data, kText, len - 1) != 0 || data[len - 1] != '.') michael@0: return false; michael@0: michael@0: NS_CStringContainerFinish(s); michael@0: return true; michael@0: } michael@0: michael@0: static bool test_ascii() michael@0: { michael@0: nsCString testCString; michael@0: testCString.AppendASCII(kAsciiData); michael@0: if (!testCString.EqualsLiteral(kAsciiData)) michael@0: return false; michael@0: michael@0: testCString.AssignASCII(kAsciiData); michael@0: if (!testCString.LowerCaseEqualsLiteral("hello world")) michael@0: return false; michael@0: michael@0: nsString testString; michael@0: testString.AppendASCII(kAsciiData); michael@0: if (!testString.EqualsLiteral(kAsciiData)) michael@0: return false; michael@0: michael@0: testString.AssignASCII(kAsciiData); michael@0: if (!testString.LowerCaseEqualsLiteral("hello world")) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_chars() michael@0: { michael@0: nsCString testCString(kAsciiData); michael@0: if (testCString.First() != 'H') michael@0: return false; michael@0: if (testCString.Last() != 'd') michael@0: return false; michael@0: testCString.SetCharAt('u', 8); michael@0: if (!testCString.EqualsASCII("Hello Would")) michael@0: return false; michael@0: michael@0: nsString testString(kUnicodeData); michael@0: if (testString.First() != 'H') michael@0: return false; michael@0: if (testString.Last() != 'd') michael@0: return false; michael@0: testString.SetCharAt('u', 8); michael@0: if (!testString.EqualsASCII("Hello Would")) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_stripchars() michael@0: { michael@0: nsCString test(kAsciiData); michael@0: test.StripChars("ld"); michael@0: if (!test.Equals("Heo Wor")) michael@0: return false; michael@0: michael@0: test.Assign(kAsciiData); michael@0: test.StripWhitespace(); michael@0: if (!test.Equals("HelloWorld")) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_trim() michael@0: { michael@0: static const char kWS[] = "\n\t\r "; michael@0: static const char kTestString[] = " \n\tTesting...\n\r"; michael@0: michael@0: nsCString test1(kTestString); michael@0: nsCString test2(kTestString); michael@0: nsCString test3(kTestString); michael@0: michael@0: test1.Trim(kWS); michael@0: test2.Trim(kWS, true, false); michael@0: test3.Trim(kWS, false, true); michael@0: michael@0: if (!test1.Equals("Testing...")) michael@0: return false; michael@0: michael@0: if (!test2.Equals("Testing...\n\r")) michael@0: return false; michael@0: michael@0: if (!test3.Equals(" \n\tTesting...")) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_find() michael@0: { michael@0: nsString uni(kUnicodeData); michael@0: michael@0: static const char kHello[] = "Hello"; michael@0: static const char khello[] = "hello"; michael@0: static const char kBye[] = "Bye!"; michael@0: michael@0: int32_t found; michael@0: michael@0: found = uni.Find(kHello); michael@0: if (found != 0) michael@0: return false; michael@0: michael@0: found = uni.Find(khello, false); michael@0: if (found != -1) michael@0: return false; michael@0: michael@0: found = uni.Find(khello, true); michael@0: if (found != 0) michael@0: return false; michael@0: michael@0: found = uni.Find(kBye); michael@0: if (found != -1) michael@0: return false; michael@0: michael@0: found = uni.Find(NS_LITERAL_STRING("World")); michael@0: if (found != 6) michael@0: return false; michael@0: michael@0: found = uni.Find(uni); michael@0: if (found != 0) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_compressws() michael@0: { michael@0: nsString check(NS_LITERAL_STRING(" \tTesting \n\t1\n 2 3\n ")); michael@0: CompressWhitespace(check); michael@0: return check.Equals(NS_LITERAL_STRING("Testing 1 2 3")); michael@0: } michael@0: michael@0: static bool test_comparisons() michael@0: { michael@0: bool result; michael@0: michael@0: // nsString michael@0: michael@0: NS_NAMED_LITERAL_STRING(shortString1, "Foo"); michael@0: NS_NAMED_LITERAL_STRING(shortString2, "Bar"); michael@0: NS_NAMED_LITERAL_STRING(shortString3, "Bar"); michael@0: NS_NAMED_LITERAL_STRING(shortString4, "bar"); michael@0: NS_NAMED_LITERAL_STRING(longString, "FooBar"); michael@0: michael@0: // == michael@0: michael@0: result = (shortString1 == shortString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString2 == shortString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString3 == shortString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString1 == longString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longString == shortString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: // != michael@0: michael@0: result = (shortString1 != shortString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString2 != shortString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString3 != shortString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString1 != longString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longString != shortString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: // < michael@0: michael@0: result = (shortString1 < shortString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString2 < shortString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString1 < longString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longString < shortString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString2 < shortString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString3 < shortString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString4 < shortString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: // <= michael@0: michael@0: result = (shortString1 <= shortString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString2 <= shortString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString1 <= longString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longString <= shortString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString2 <= shortString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString3 <= shortString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString4 <= shortString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: // > michael@0: michael@0: result = (shortString1 > shortString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString2 > shortString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString1 > longString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longString > shortString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString2 > shortString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString3 > shortString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString4 > shortString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: // >= michael@0: michael@0: result = (shortString1 >= shortString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString2 >= shortString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString1 >= longString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longString >= shortString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString2 >= shortString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortString3 >= shortString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortString4 >= shortString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: // nsCString michael@0: michael@0: NS_NAMED_LITERAL_CSTRING(shortCString1, "Foo"); michael@0: NS_NAMED_LITERAL_CSTRING(shortCString2, "Bar"); michael@0: NS_NAMED_LITERAL_CSTRING(shortCString3, "Bar"); michael@0: NS_NAMED_LITERAL_CSTRING(shortCString4, "bar"); michael@0: NS_NAMED_LITERAL_CSTRING(longCString, "FooBar"); michael@0: michael@0: // == michael@0: michael@0: result = (shortCString1 == shortCString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString2 == shortCString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString3 == shortCString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString1 == longCString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longCString == shortCString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: // != michael@0: michael@0: result = (shortCString1 != shortCString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString2 != shortCString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString3 != shortCString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString1 != longCString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longCString != shortCString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: // < michael@0: michael@0: result = (shortCString1 < shortCString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString2 < shortCString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString1 < longCString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longCString < shortCString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString2 < shortCString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString3 < shortCString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString4 < shortCString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: // <= michael@0: michael@0: result = (shortCString1 <= shortCString2); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString2 <= shortCString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString1 <= longCString); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (longCString <= shortCString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString2 <= shortCString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString3 <= shortCString4); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString4 <= shortCString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: // > michael@0: michael@0: result = (shortCString1 > shortCString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString2 > shortCString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString1 > longCString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longCString > shortCString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString2 > shortCString3); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString3 > shortCString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString4 > shortCString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: // >= michael@0: michael@0: result = (shortCString1 >= shortCString2); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString2 >= shortCString1); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString1 >= longCString); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (longCString >= shortCString1); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString2 >= shortCString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: result = (shortCString3 >= shortCString4); michael@0: if (result) michael@0: return false; michael@0: michael@0: result = (shortCString4 >= shortCString3); michael@0: if (!result) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool test_parse_string_helper(const char* str, char separator, int len, michael@0: const char* s1, const char* s2) michael@0: { michael@0: nsCString data(str); michael@0: nsTArray results; michael@0: if (!ParseString(data, separator, results)) michael@0: return false; michael@0: if (int(results.Length()) != len) michael@0: return false; michael@0: const char* strings[] = { s1, s2 }; michael@0: for (int i = 0; i < len; ++i) { michael@0: if (!results[i].Equals(strings[i])) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static bool test_parse_string_helper0(const char* str, char separator) michael@0: { michael@0: return test_parse_string_helper(str, separator, 0, nullptr, nullptr); michael@0: } michael@0: michael@0: static bool test_parse_string_helper1(const char* str, char separator, const char* s1) michael@0: { michael@0: return test_parse_string_helper(str, separator, 1, s1, nullptr); michael@0: } michael@0: michael@0: static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2) michael@0: { michael@0: return test_parse_string_helper(str, separator, 2, s1, s2); michael@0: } michael@0: michael@0: static bool test_parse_string() michael@0: { michael@0: return test_parse_string_helper1("foo, bar", '_', "foo, bar") && michael@0: test_parse_string_helper2("foo, bar", ',', "foo", " bar") && michael@0: test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") && michael@0: test_parse_string_helper2("foo,bar", 'o', "f", ",bar") && michael@0: test_parse_string_helper0("", '_') && michael@0: test_parse_string_helper0(" ", ' ') && michael@0: test_parse_string_helper1(" foo", ' ', "foo") && michael@0: test_parse_string_helper1(" foo", ' ', "foo"); michael@0: } michael@0: michael@0: //---- michael@0: michael@0: typedef bool (*TestFunc)(); michael@0: michael@0: static const struct Test michael@0: { michael@0: const char* name; michael@0: TestFunc func; michael@0: } michael@0: tests[] = michael@0: { michael@0: { "test_basic_1", test_basic_1 }, michael@0: { "test_basic_2", test_basic_2 }, michael@0: { "test_convert", test_convert }, michael@0: { "test_append", test_append }, michael@0: { "test_replace", test_replace }, michael@0: { "test_compress_ws", test_compress_ws }, michael@0: { "test_depend", test_depend }, michael@0: { "test_depend_sub", test_depend_sub }, michael@0: { "test_adopt", test_adopt }, michael@0: { "test_adopt_sub", test_adopt_sub }, michael@0: { "test_mutation", test_mutation }, michael@0: { "test_ascii", test_ascii }, michael@0: { "test_chars", test_chars }, michael@0: { "test_stripchars", test_stripchars }, michael@0: { "test_trim", test_trim }, michael@0: { "test_find", test_find }, michael@0: { "test_compressws", test_compressws }, michael@0: { "test_comparisons", test_comparisons }, michael@0: { "test_parse_string", test_parse_string }, michael@0: { nullptr, nullptr } michael@0: }; michael@0: michael@0: //---- michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: int count = 1; michael@0: if (argc > 1) michael@0: count = atoi(argv[1]); michael@0: michael@0: while (count--) michael@0: { michael@0: for (const Test* t = tests; t->name != nullptr; ++t) michael@0: { michael@0: printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE"); michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: }