From 0cbeb463a632ee79173872cfcc2edfe46d664361 Mon Sep 17 00:00:00 2001 From: Wes Kocher Date: Fri, 12 Dec 2014 16:34:21 -0800 Subject: [PATCH] Backed out changeset d468b88bdb23 (bug 1110956) --HG-- rename : xpcom/glue/tests/gtest/TestStrings.cpp => xpcom/tests/TestStrings.cpp --- xpcom/glue/tests/gtest/TestStrings.cpp | 981 ----------------- xpcom/glue/tests/gtest/moz.build | 1 - xpcom/tests/TestStrings.cpp | 1405 ++++++++++++++++++++++++ xpcom/tests/moz.build | 1 + 4 files changed, 1406 insertions(+), 982 deletions(-) delete mode 100644 xpcom/glue/tests/gtest/TestStrings.cpp create mode 100644 xpcom/tests/TestStrings.cpp diff --git a/xpcom/glue/tests/gtest/TestStrings.cpp b/xpcom/glue/tests/gtest/TestStrings.cpp deleted file mode 100644 index fa904e8ce07..00000000000 --- a/xpcom/glue/tests/gtest/TestStrings.cpp +++ /dev/null @@ -1,981 +0,0 @@ -/* vim:set ts=2 sw=2 et cindent: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include -#include "nsString.h" -#include "nsStringBuffer.h" -#include "nsReadableUtils.h" -#include "nsCRTGlue.h" -#include "gtest/gtest.h" - -namespace TestStrings { - -void test_assign_helper(const nsACString& in, nsACString &_retval) -{ - _retval = in; -} - -TEST(Strings, assign) -{ - nsCString result; - test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result); - ASSERT_STREQ(result.get(), "ab"); -} - -TEST(Strings, assign_c) -{ - nsCString c; c.Assign('c'); - ASSERT_STREQ(c.get(), "c"); -} - -TEST(Strings, test1) -{ - NS_NAMED_LITERAL_STRING(empty, ""); - const nsAString& aStr = empty; - - nsAutoString buf(aStr); - - int32_t n = buf.FindChar(','); - - n = buf.Length(); - - buf.Cut(0, n + 1); - n = buf.FindChar(','); - - ASSERT_EQ(n, kNotFound); -} - -TEST(Strings, test2) -{ - nsCString data("hello world"); - const nsACString& aStr = data; - - nsCString temp(aStr); - temp.Cut(0, 6); - - ASSERT_STREQ(temp.get(), "world"); -} - -TEST(Strings, find) -{ - nsCString src(""); - - int32_t i = src.Find("DOCTYPE", true, 2, 1); - ASSERT_EQ(i, 2); -} - -TEST(Strings, rfind) -{ - const char text[] = ""; - const char term[] = "bLaH"; - nsCString src(text); - int32_t i; - - i = src.RFind(term, true, 3, -1); - ASSERT_EQ(i, kNotFound); - - i = src.RFind(term, true, -1, -1); - ASSERT_EQ(i, 20); - - i = src.RFind(term, true, 13, -1); - ASSERT_EQ(i, 10); - - i = src.RFind(term, true, 22, 3); - ASSERT_EQ(i, 20); -} - -TEST(Strings, rfind_2) -{ - const char text[] = ""; - nsCString src(text); - int32_t i = src.RFind("TYPE", false, 5, -1); - ASSERT_EQ(i, 5); -} - -TEST(Strings, rfind_3) -{ - const char text[] = "urn:mozilla:locale:en-US:necko"; - nsAutoCString value(text); - int32_t i = value.RFind(":"); - ASSERT_EQ(i, 24); -} - -TEST(Strings, rfind_4) -{ - nsCString value("a.msf"); - int32_t i = value.RFind(".msf"); - ASSERT_EQ(i, 1); -} - -TEST(Strings, findinreadable) -{ - const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/"; - nsAutoCString value(text); - - nsACString::const_iterator begin, end; - value.BeginReading(begin); - value.EndReading(end); - nsACString::const_iterator delim_begin (begin), - delim_end (end); - - // Search for last !/ at the end of the string - ASSERT_TRUE(FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end)); - char *r = ToNewCString(Substring(delim_begin, delim_end)); - // Should match the first "!/" but not the last - ASSERT_NE(delim_end, end); - ASSERT_STREQ(r, "!/"); - nsMemory::Free(r); - - delim_begin = begin; - delim_end = end; - - // Search for first jar: - ASSERT_TRUE(FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - r = ToNewCString(Substring(delim_begin, delim_end)); - // Should not match the first jar:, but the second one - ASSERT_EQ(delim_begin, begin); - ASSERT_STREQ(r, "jar:"); - nsMemory::Free(r); - - // Search for jar: in a Substring - delim_begin = begin; delim_begin++; - delim_end = end; - ASSERT_TRUE(FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - r = ToNewCString(Substring(delim_begin, delim_end)); - // Should not match the first jar:, but the second one - ASSERT_NE(delim_begin, begin); - ASSERT_STREQ(r, "jar:"); - nsMemory::Free(r); - - // Should not find a match - ASSERT_FALSE(FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); - - // Should not find a match (search not beyond Substring) - delim_begin = begin; for (int i=0;i<6;i++) delim_begin++; - delim_end = end; - ASSERT_FALSE(FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); - - // Should not find a match (search not beyond Substring) - delim_begin = begin; - delim_end = end; for (int i=0;i<7;i++) delim_end--; - ASSERT_FALSE(FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); -} - -TEST(Strings, rfindinreadable) -{ - const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/"; - nsAutoCString value(text); - - nsACString::const_iterator begin, end; - value.BeginReading(begin); - value.EndReading(end); - nsACString::const_iterator delim_begin (begin), - delim_end (end); - - // Search for last !/ at the end of the string - ASSERT_TRUE(RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end)); - char *r = ToNewCString(Substring(delim_begin, delim_end)); - // Should match the last "!/" - ASSERT_EQ(delim_end, end); - ASSERT_STREQ(r, "!/"); - nsMemory::Free(r); - - delim_begin = begin; - delim_end = end; - - // Search for last jar: but not the first one... - ASSERT_TRUE(RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - r = ToNewCString(Substring(delim_begin, delim_end)); - // Should not match the first jar:, but the second one - ASSERT_NE(delim_begin, begin); - ASSERT_STREQ(r, "jar:"); - nsMemory::Free(r); - - // Search for jar: in a Substring - delim_begin = begin; - delim_end = begin; for (int i=0;i<6;i++) delim_end++; - ASSERT_TRUE(RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - r = ToNewCString(Substring(delim_begin, delim_end)); - // Should not match the first jar:, but the second one - ASSERT_EQ(delim_begin, begin); - ASSERT_STREQ(r, "jar:"); - nsMemory::Free(r); - - // Should not find a match - delim_begin = begin; - delim_end = end; - ASSERT_FALSE(RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); - - // Should not find a match (search not before Substring) - delim_begin = begin; for (int i=0;i<6;i++) delim_begin++; - delim_end = end; - ASSERT_FALSE(RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); - - // Should not find a match (search not beyond Substring) - delim_begin = begin; - delim_end = end; for (int i=0;i<7;i++) delim_end--; - ASSERT_FALSE(RFindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)); - - // When no match is found, range should be empty - ASSERT_EQ(delim_begin, delim_end); -} - -TEST(Strings, distance) -{ - const char text[] = "abc-xyz"; - nsCString s(text); - nsCString::const_iterator begin, end; - s.BeginReading(begin); - s.EndReading(end); - size_t d = Distance(begin, end); - ASSERT_EQ(d, sizeof(text) - 1); -} - -TEST(Strings, length) -{ - const char text[] = "abc-xyz"; - nsCString s(text); - size_t d = s.Length(); - ASSERT_EQ(d, sizeof(text) - 1); -} - -TEST(Strings, trim) -{ - const char text[] = " a\t $ "; - const char set[] = " \t$"; - - nsCString s(text); - s.Trim(set); - ASSERT_STREQ(s.get(), "a"); -} - -TEST(Strings, replace_substr) -{ - const char text[] = "abc-ppp-qqq-ppp-xyz"; - nsCString s(text); - s.ReplaceSubstring("ppp", "www"); - ASSERT_STREQ(s.get(), "abc-www-qqq-www-xyz"); - - s.Assign("foobar"); - s.ReplaceSubstring("foo", "bar"); - s.ReplaceSubstring("bar", ""); - ASSERT_STREQ(s.get(), ""); - - s.Assign("foofoofoo"); - s.ReplaceSubstring("foo", "foo"); - ASSERT_STREQ(s.get(), "foofoofoo"); - - s.Assign("foofoofoo"); - s.ReplaceSubstring("of", "fo"); - ASSERT_STREQ(s.get(), "fofoofooo"); -} - -TEST(Strings, replace_substr_2) -{ - const char *oldName = nullptr; - const char *newName = "user"; - nsString acctName; acctName.AssignLiteral("forums.foo.com"); - nsAutoString newAcctName, oldVal, newVal; - oldVal.AssignWithConversion(oldName); - newVal.AssignWithConversion(newName); - newAcctName.Assign(acctName); - - // here, oldVal is empty. we are testing that this function - // does not hang. see bug 235355. - newAcctName.ReplaceSubstring(oldVal, newVal); - - // we expect that newAcctName will be unchanged. - ASSERT_TRUE(newAcctName.Equals(acctName)); -} - -TEST(Strings, replace_substr_3) -{ - nsCString s; - s.Assign("abcabcabc"); - s.ReplaceSubstring("ca", "X"); - ASSERT_STREQ(s.get(), "abXbXbc"); - - s.Assign("abcabcabc"); - s.ReplaceSubstring("ca", "XYZ"); - ASSERT_STREQ(s.get(), "abXYZbXYZbc"); - - s.Assign("abcabcabc"); - s.ReplaceSubstring("ca", "XY"); - ASSERT_STREQ(s.get(), "abXYbXYbc"); - - s.Assign("abcabcabc"); - s.ReplaceSubstring("ca", "XYZ!"); - ASSERT_STREQ(s.get(), "abXYZ!bXYZ!bc"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "X"); - ASSERT_STREQ(s.get(), "aXaXaX"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "XYZ!"); - ASSERT_STREQ(s.get(), "aXYZ!aXYZ!aXYZ!"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "XY"); - ASSERT_STREQ(s.get(), "aXYaXYaXY"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "XYZABC"); - ASSERT_STREQ(s.get(), "aXYZABCaXYZABCaXYZABC"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "XYZ"); - ASSERT_STREQ(s.get(), "aXYZaXYZaXYZ"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("bcd", "XYZ!"); - ASSERT_STREQ(s.get(), "aXYZ!aXYZ!aXYZ!"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("ab", "X"); - ASSERT_STREQ(s.get(), "XcdXcdXcd"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("ab", "XYZABC"); - ASSERT_STREQ(s.get(), "XYZABCcdXYZABCcdXYZABCcd"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("ab", "XY"); - ASSERT_STREQ(s.get(), "XYcdXYcdXYcd"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("ab", "XYZ!"); - ASSERT_STREQ(s.get(), "XYZ!cdXYZ!cdXYZ!cd"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("notfound", "X"); - ASSERT_STREQ(s.get(), "abcdabcdabcd"); - - s.Assign("abcdabcdabcd"); - s.ReplaceSubstring("notfound", "longlongstring"); - ASSERT_STREQ(s.get(), "abcdabcdabcd"); -} - -TEST(Strings, strip_ws) -{ - const char text[] = " a $ "; - nsCString s(text); - s.StripWhitespace(); - ASSERT_STREQ(s.get(), "a$"); -} - -TEST(Strings, equals_ic) -{ - nsCString s; - ASSERT_FALSE(s.LowerCaseEqualsLiteral("view-source")); -} - -TEST(Strings, fixed_string) -{ - char buf[256] = "hello world"; - - nsFixedCString s(buf, sizeof(buf)); - - ASSERT_EQ(s.Length(), strlen(buf)); - - ASSERT_STREQ(s.get(), buf); - - s.Assign("foopy doopy doo"); - ASSERT_EQ(s.get(), buf); -} - -TEST(Strings, concat) -{ - nsCString bar("bar"); - const nsACString& barRef = bar; - - const nsPromiseFlatCString& result = - PromiseFlatCString(NS_LITERAL_CSTRING("foo") + - NS_LITERAL_CSTRING(",") + - barRef); - ASSERT_STREQ(result.get(), "foo,bar"); -} - -TEST(Strings, concat_2) -{ - nsCString fieldTextStr("xyz"); - nsCString text("text"); - const nsACString& aText = text; - - nsAutoCString result( fieldTextStr + aText ); - - ASSERT_STREQ(result.get(), "xyztext"); -} - -TEST(Strings, concat_3) -{ - nsCString result; - nsCString ab("ab"), c("c"); - - result = ab + result + c; - ASSERT_STREQ(result.get(), "abc"); -} - -TEST(Strings, xpidl_string) -{ - nsXPIDLCString a, b; - a = b; - ASSERT_TRUE(a == b); - - a.Adopt(0); - ASSERT_TRUE(a == b); - - a.Append("foopy"); - a.Assign(b); - ASSERT_TRUE(a == b); - - a.Insert("", 0); - a.Assign(b); - ASSERT_TRUE(a == b); - - const char text[] = "hello world"; - *getter_Copies(a) = NS_strdup(text); - ASSERT_STREQ(a, text); - - b = a; - ASSERT_STREQ(a, b); - - a.Adopt(0); - nsACString::const_iterator begin, end; - a.BeginReading(begin); - a.EndReading(end); - char *r = ToNewCString(Substring(begin, end)); - ASSERT_STREQ(r, ""); - nsMemory::Free(r); - - a.Adopt(0); - ASSERT_TRUE(a.IsVoid()); - - /* - int32_t index = a.FindCharInSet("xyz"); - ASSERT_EQ(index, kNotFound); - */ -} - -TEST(Strings, empty_assign) -{ - nsCString a; - a.AssignLiteral(""); - - a.AppendLiteral(""); - - nsCString b; - b.SetCapacity(0); -} - -TEST(Strings, set_length) -{ - const char kText[] = "Default Plugin"; - nsCString buf; - buf.SetCapacity(sizeof(kText)-1); - buf.Assign(kText); - buf.SetLength(sizeof(kText)-1); - ASSERT_STREQ(buf.get(), kText); -} - -TEST(Strings, substring) -{ - nsCString super("hello world"), sub("hello"); - - // this tests that |super| starts with |sub|, - - ASSERT_TRUE(sub.Equals(StringHead(super, sub.Length()))); - - // and verifies that |sub| does not start with |super|. - - ASSERT_FALSE(super.Equals(StringHead(sub, super.Length()))); -} - -#define test_append_expect(str, int, suffix, expect) \ - str.Truncate(); \ - str.AppendInt(suffix = int); \ - ASSERT_TRUE(str.EqualsLiteral(expect)); - -#define test_appends_expect(int, suffix, expect) \ - test_append_expect(str, int, suffix, expect) \ - test_append_expect(cstr, int, suffix, expect) - -#define test_appendbase(str, prefix, int, suffix, base) \ - str.Truncate(); \ - str.AppendInt(suffix = prefix ## int ## suffix, base); \ - ASSERT_TRUE(str.EqualsLiteral(#int)); - -#define test_appendbases(prefix, int, suffix, base) \ - test_appendbase(str, prefix, int, suffix, base) \ - test_appendbase(cstr, prefix, int, suffix, base) - -TEST(Strings, appendint) -{ - nsString str; - nsCString cstr; - int32_t L; - uint32_t UL; - int64_t LL; - uint64_t ULL; - test_appends_expect(INT32_MAX, L, "2147483647") - test_appends_expect(INT32_MIN, L, "-2147483648") - test_appends_expect(UINT32_MAX, UL, "4294967295") - test_appends_expect(INT64_MAX, LL, "9223372036854775807") - test_appends_expect(INT64_MIN, LL, "-9223372036854775808") - test_appends_expect(UINT64_MAX, ULL, "18446744073709551615") - test_appendbases(0, 17777777777, L, 8) - test_appendbases(0, 20000000000, L, 8) - test_appendbases(0, 37777777777, UL, 8) - test_appendbases(0, 777777777777777777777, LL, 8) - test_appendbases(0, 1000000000000000000000, LL, 8) - test_appendbases(0, 1777777777777777777777, ULL, 8) - test_appendbases(0x, 7fffffff, L, 16) - test_appendbases(0x, 80000000, L, 16) - test_appendbases(0x, ffffffff, UL, 16) - test_appendbases(0x, 7fffffffffffffff, LL, 16) - test_appendbases(0x, 8000000000000000, LL, 16) - test_appendbases(0x, ffffffffffffffff, ULL, 16) -} - -TEST(Strings, appendint64) -{ - nsCString str; - - int64_t max = INT64_MAX; - static const char max_expected[] = "9223372036854775807"; - int64_t min = INT64_MIN; - static const char min_expected[] = "-9223372036854775808"; - static const char min_expected_oct[] = "1000000000000000000000"; - int64_t maxint_plus1 = 1LL << 32; - static const char maxint_plus1_expected[] = "4294967296"; - static const char maxint_plus1_expected_x[] = "100000000"; - - str.AppendInt(max); - - ASSERT_TRUE(str.Equals(max_expected)); - - str.Truncate(); - str.AppendInt(min); - ASSERT_TRUE(str.Equals(min_expected)); - str.Truncate(); - str.AppendInt(min, 8); - ASSERT_TRUE(str.Equals(min_expected_oct)); - - - str.Truncate(); - str.AppendInt(maxint_plus1); - ASSERT_TRUE(str.Equals(maxint_plus1_expected)); - str.Truncate(); - str.AppendInt(maxint_plus1, 16); - ASSERT_TRUE(str.Equals(maxint_plus1_expected_x)); -} - -TEST(Strings, appendfloat) -{ - nsCString str; - double bigdouble = 11223344556.66; - static const char double_expected[] = "11223344556.66"; - static const char float_expected[] = "0.01"; - - // AppendFloat is used to append doubles, therefore the precision must be - // large enough (see bug 327719) - str.AppendFloat( bigdouble ); - ASSERT_TRUE(str.Equals(double_expected)); - - str.Truncate(); - // AppendFloat is used to append floats (bug 327719 #27) - str.AppendFloat( 0.1f * 0.1f ); - ASSERT_TRUE(str.Equals(float_expected)); -} - -TEST(Strings, findcharinset) -{ - nsCString buf("hello, how are you?"); - - int32_t index = buf.FindCharInSet(",?", 5); - ASSERT_EQ(index, 5); - - index = buf.FindCharInSet("helo", 0); - ASSERT_EQ(index, 0); - - index = buf.FindCharInSet("z?", 6); - ASSERT_EQ(index, (int32_t) buf.Length() - 1); -} - -TEST(Strings, rfindcharinset) -{ - nsCString buf("hello, how are you?"); - - int32_t index = buf.RFindCharInSet(",?", 5); - ASSERT_EQ(index, 5); - - index = buf.RFindCharInSet("helo", 0); - ASSERT_EQ(index, 0); - - index = buf.RFindCharInSet("z?", 6); - ASSERT_EQ(index, kNotFound); - - index = buf.RFindCharInSet("l", 5); - ASSERT_EQ(index, 3); - - buf.Assign("abcdefghijkabc"); - - index = buf.RFindCharInSet("ab"); - ASSERT_EQ(index, 12); - - index = buf.RFindCharInSet("ab", 11); - ASSERT_EQ(index, 11); - - index = buf.RFindCharInSet("ab", 10); - ASSERT_EQ(index, 1); - - index = buf.RFindCharInSet("ab", 0); - ASSERT_EQ(index, 0); - - index = buf.RFindCharInSet("cd", 1); - ASSERT_EQ(index, kNotFound); - - index = buf.RFindCharInSet("h"); - ASSERT_EQ(index, 7); -} - -TEST(Strings, stringbuffer) -{ - const char kData[] = "hello world"; - - nsRefPtr buf; - - buf = nsStringBuffer::Alloc(sizeof(kData)); - ASSERT_TRUE(!!buf); - buf->Release(); - - buf = nsStringBuffer::Alloc(sizeof(kData)); - ASSERT_TRUE(!!buf); - char *data = (char *) buf->Data(); - memcpy(data, kData, sizeof(kData)); - - nsCString str; - buf->ToString(sizeof(kData)-1, str); - - nsStringBuffer *buf2; - buf2 = nsStringBuffer::FromString(str); - - ASSERT_EQ(buf, buf2); - - buf->Release(); -} - -TEST(Strings, voided) -{ - const char kData[] = "hello world"; - - nsXPIDLCString str; - ASSERT_FALSE(str); - ASSERT_TRUE(str.IsVoid()); - ASSERT_TRUE(str.IsEmpty()); - - str.Assign(kData); - ASSERT_STREQ(str, kData); - - str.SetIsVoid(true); - ASSERT_FALSE(str); - ASSERT_TRUE(str.IsVoid()); - ASSERT_TRUE(str.IsEmpty()); - - str.SetIsVoid(false); - ASSERT_STREQ(str, ""); -} - -TEST(Strings, voided_autostr) -{ - const char kData[] = "hello world"; - - nsAutoCString str; - ASSERT_FALSE(str.IsVoid()); - ASSERT_TRUE(str.IsEmpty()); - - str.Assign(kData); - ASSERT_STREQ(str.get(), kData); - - str.SetIsVoid(true); - ASSERT_TRUE(str.IsVoid()); - ASSERT_TRUE(str.IsEmpty()); - - str.Assign(kData); - ASSERT_FALSE(str.IsVoid()); - ASSERT_FALSE(str.IsEmpty()); - ASSERT_STREQ(str.get(), kData); -} - -TEST(Strings, voided_assignment) -{ - nsCString a, b; - b.SetIsVoid(true); - a = b; - ASSERT_TRUE(a.IsVoid()); - ASSERT_EQ(a.get(), b.get()); -} - -TEST(Strings, empty_assignment) -{ - nsCString a, b; - a = b; - ASSERT_EQ(a.get(), b.get()); -} - -struct ToIntegerTest -{ - const char *str; - uint32_t radix; - int32_t result; - nsresult rv; -}; - -static const ToIntegerTest kToIntegerTests[] = { - { "123", 10, 123, NS_OK }, - { "7b", 16, 123, NS_OK }, - { "90194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE }, - { nullptr, 0, 0, NS_OK } -}; - -TEST(Strings, string_tointeger) -{ - nsresult rv; - for (const ToIntegerTest* t = kToIntegerTests; t->str; ++t) { - int32_t result = nsAutoCString(t->str).ToInteger(&rv, t->radix); - ASSERT_EQ(rv, t->rv); - ASSERT_EQ(result, t->result); - result = nsAutoCString(t->str).ToInteger(&rv, t->radix); - ASSERT_EQ(rv, t->rv); - ASSERT_EQ(result, t->result); - } -} - -static void test_parse_string_helper(const char* str, char separator, int len, - const char* s1, const char* s2) -{ - nsCString data(str); - nsTArray results; - ASSERT_TRUE(ParseString(data, separator, results)); - ASSERT_EQ(int(results.Length()), len); - const char* strings[] = { s1, s2 }; - for (int i = 0; i < len; ++i) { - ASSERT_TRUE(results[i].Equals(strings[i])); - } -} - -static void test_parse_string_helper0(const char* str, char separator) -{ - test_parse_string_helper(str, separator, 0, nullptr, nullptr); -} - -static void test_parse_string_helper1(const char* str, char separator, const char* s1) -{ - test_parse_string_helper(str, separator, 1, s1, nullptr); -} - -static void test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2) -{ - test_parse_string_helper(str, separator, 2, s1, s2); -} - -TEST(String, parse_string) -{ - test_parse_string_helper1("foo, bar", '_', "foo, bar"); - test_parse_string_helper2("foo, bar", ',', "foo", " bar"); - test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar"); - test_parse_string_helper2("foo,bar", 'o', "f", ",bar"); - test_parse_string_helper0("", '_'); - test_parse_string_helper0(" ", ' '); - test_parse_string_helper1(" foo", ' ', "foo"); - test_parse_string_helper1(" foo", ' ', "foo"); -} - -static void test_strip_chars_helper(const char16_t* str, const char16_t* strip, const nsAString& result, uint32_t offset=0) -{ - nsAutoString tmp(str); - nsAString& data = tmp; - data.StripChars(strip, offset); - ASSERT_TRUE(data.Equals(result)); -} - -TEST(String, strip_chars) -{ - test_strip_chars_helper(MOZ_UTF16("foo \r \nbar"), - MOZ_UTF16(" \n\r"), - NS_LITERAL_STRING("foobar")); - test_strip_chars_helper(MOZ_UTF16("\r\nfoo\r\n"), - MOZ_UTF16(" \n\r"), - NS_LITERAL_STRING("foo")); - test_strip_chars_helper(MOZ_UTF16("foo"), - MOZ_UTF16(" \n\r"), - NS_LITERAL_STRING("foo")); - test_strip_chars_helper(MOZ_UTF16("foo"), - MOZ_UTF16("fo"), - NS_LITERAL_STRING("")); - test_strip_chars_helper(MOZ_UTF16("foo"), - MOZ_UTF16("foo"), - NS_LITERAL_STRING("")); - test_strip_chars_helper(MOZ_UTF16(" foo"), - MOZ_UTF16(" "), - NS_LITERAL_STRING(" foo"), 1); -} - -TEST(Strings, huge_capacity) -{ - nsString a, b, c, d, e, f, g, h, i, j, k, l, m, n; - nsCString n1; - - // Ignore the result if the address space is less than 64-bit because - // some of the allocations above will exhaust the address space. - if (sizeof(void*) >= 8) { - ASSERT_TRUE(a.SetCapacity(1, fallible_t())); - ASSERT_FALSE(a.SetCapacity(nsString::size_type(-1)/2, fallible_t())); - ASSERT_TRUE(a.SetCapacity(0, fallible_t())); // free the allocated memory - - ASSERT_TRUE(b.SetCapacity(1, fallible_t())); - ASSERT_FALSE(b.SetCapacity(nsString::size_type(-1)/2 - 1, fallible_t())); - ASSERT_TRUE(b.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(c.SetCapacity(1, fallible_t())); - ASSERT_FALSE(c.SetCapacity(nsString::size_type(-1)/2, fallible_t())); - ASSERT_TRUE(c.SetCapacity(0, fallible_t())); - - ASSERT_FALSE(d.SetCapacity(nsString::size_type(-1)/2 - 1, fallible_t())); - ASSERT_FALSE(d.SetCapacity(nsString::size_type(-1)/2, fallible_t())); - ASSERT_TRUE(d.SetCapacity(0, fallible_t())); - - ASSERT_FALSE(e.SetCapacity(nsString::size_type(-1)/4, fallible_t())); - ASSERT_FALSE(e.SetCapacity(nsString::size_type(-1)/4 + 1, fallible_t())); - ASSERT_TRUE(e.SetCapacity(0, fallible_t())); - - ASSERT_FALSE(f.SetCapacity(nsString::size_type(-1)/2, fallible_t())); - ASSERT_TRUE(f.SetCapacity(0, fallible_t())); - - ASSERT_FALSE(g.SetCapacity(nsString::size_type(-1)/4 + 1000, fallible_t())); - ASSERT_FALSE(g.SetCapacity(nsString::size_type(-1)/4 + 1001, fallible_t())); - ASSERT_TRUE(g.SetCapacity(0, fallible_t())); - - ASSERT_FALSE(h.SetCapacity(nsString::size_type(-1)/4+1, fallible_t())); - ASSERT_FALSE(h.SetCapacity(nsString::size_type(-1)/2, fallible_t())); - ASSERT_TRUE(h.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(i.SetCapacity(1, fallible_t())); - ASSERT_TRUE(i.SetCapacity(nsString::size_type(-1)/4 - 1000, fallible_t())); - ASSERT_FALSE(i.SetCapacity(nsString::size_type(-1)/4 + 1, fallible_t())); - ASSERT_TRUE(i.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(j.SetCapacity(nsString::size_type(-1)/4 - 1000, fallible_t())); - ASSERT_FALSE(j.SetCapacity(nsString::size_type(-1)/4 + 1, fallible_t())); - ASSERT_TRUE(j.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(k.SetCapacity(nsString::size_type(-1)/8 - 1000, fallible_t())); - ASSERT_TRUE(k.SetCapacity(nsString::size_type(-1)/4 - 1001, fallible_t())); - ASSERT_TRUE(k.SetCapacity(nsString::size_type(-1)/4 - 998, fallible_t())); - ASSERT_FALSE(k.SetCapacity(nsString::size_type(-1)/4 + 1, fallible_t())); - ASSERT_TRUE(k.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(l.SetCapacity(nsString::size_type(-1)/8, fallible_t())); - ASSERT_TRUE(l.SetCapacity(nsString::size_type(-1)/8 + 1, fallible_t())); - ASSERT_TRUE(l.SetCapacity(nsString::size_type(-1)/8 + 2, fallible_t())); - ASSERT_TRUE(l.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(m.SetCapacity(nsString::size_type(-1)/8 + 1000, fallible_t())); - ASSERT_TRUE(m.SetCapacity(nsString::size_type(-1)/8 + 1001, fallible_t())); - ASSERT_TRUE(m.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(n.SetCapacity(nsString::size_type(-1)/8+1, fallible_t())); - ASSERT_FALSE(n.SetCapacity(nsString::size_type(-1)/4, fallible_t())); - ASSERT_TRUE(n.SetCapacity(0, fallible_t())); - - ASSERT_TRUE(n.SetCapacity(0, fallible_t())); - ASSERT_TRUE(n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 2, fallible_t())); - ASSERT_TRUE(n.SetCapacity(0, fallible_t())); - ASSERT_FALSE(n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 1, fallible_t())); - ASSERT_TRUE(n.SetCapacity(0, fallible_t())); - ASSERT_TRUE(n1.SetCapacity(0, fallible_t())); - ASSERT_TRUE(n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 2, fallible_t())); - ASSERT_TRUE(n1.SetCapacity(0, fallible_t())); - ASSERT_FALSE(n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 1, fallible_t())); - ASSERT_TRUE(n1.SetCapacity(0, fallible_t())); - } -} - -static void test_tofloat_helper(const nsString& aStr, float aExpected, bool aSuccess) -{ - nsresult result; - ASSERT_EQ(aStr.ToFloat(&result), aExpected); - if (aSuccess) { - ASSERT_EQ(result, NS_OK); - } else { - ASSERT_NE(result, NS_OK); - } -} - -TEST(Strings, tofloat) -{ - test_tofloat_helper(NS_LITERAL_STRING("42"), 42.f, true); - test_tofloat_helper(NS_LITERAL_STRING("42.0"), 42.f, true); - test_tofloat_helper(NS_LITERAL_STRING("-42"), -42.f, true); - test_tofloat_helper(NS_LITERAL_STRING("+42"), 42, true); - test_tofloat_helper(NS_LITERAL_STRING("13.37"), 13.37f, true); - test_tofloat_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789f, true); - test_tofloat_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456f, true); - test_tofloat_helper(NS_LITERAL_STRING("0"), 0.f, true); - test_tofloat_helper(NS_LITERAL_STRING("1.e5"), 100000, true); - test_tofloat_helper(NS_LITERAL_STRING(""), 0.f, false); - test_tofloat_helper(NS_LITERAL_STRING("42foo"), 42.f, false); - test_tofloat_helper(NS_LITERAL_STRING("foo"), 0.f, false); -} - -static void test_todouble_helper(const nsString& aStr, double aExpected, bool aSuccess) -{ - nsresult result; - ASSERT_EQ(aStr.ToDouble(&result), aExpected); - if (aSuccess) { - ASSERT_EQ(result, NS_OK); - } else { - ASSERT_NE(result, NS_OK); - } -} - -TEST(Strings, todouble) -{ - test_todouble_helper(NS_LITERAL_STRING("42"), 42, true); - test_todouble_helper(NS_LITERAL_STRING("42.0"), 42, true); - test_todouble_helper(NS_LITERAL_STRING("-42"), -42, true); - test_todouble_helper(NS_LITERAL_STRING("+42"), 42, true); - test_todouble_helper(NS_LITERAL_STRING("13.37"), 13.37, true); - test_todouble_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789, true); - test_todouble_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456, true); - test_todouble_helper(NS_LITERAL_STRING("123456789.98765432123456"), 123456789.98765432123456, true); - test_todouble_helper(NS_LITERAL_STRING("0"), 0, true); - test_todouble_helper(NS_LITERAL_STRING("1.e5"), 100000, true); - test_todouble_helper(NS_LITERAL_STRING(""), 0, false); - test_todouble_helper(NS_LITERAL_STRING("42foo"), 42, false); - test_todouble_helper(NS_LITERAL_STRING("foo"), 0, false); -} - -} diff --git a/xpcom/glue/tests/gtest/moz.build b/xpcom/glue/tests/gtest/moz.build index 81547e3d302..053d054009e 100644 --- a/xpcom/glue/tests/gtest/moz.build +++ b/xpcom/glue/tests/gtest/moz.build @@ -7,7 +7,6 @@ UNIFIED_SOURCES += [ 'TestFileUtils.cpp', 'TestGCPostBarriers.cpp', - 'TestStrings.cpp', ] LOCAL_INCLUDES = [ diff --git a/xpcom/tests/TestStrings.cpp b/xpcom/tests/TestStrings.cpp new file mode 100644 index 00000000000..65865352e9e --- /dev/null +++ b/xpcom/tests/TestStrings.cpp @@ -0,0 +1,1405 @@ +/* vim:set ts=2 sw=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include +#include +#include "nsString.h" +#include "nsStringBuffer.h" +#include "nsReadableUtils.h" +#include "nsCRTGlue.h" + +namespace TestStrings { + +void test_assign_helper(const nsACString& in, nsACString &_retval) + { + _retval = in; + } + +bool test_assign() + { + nsCString result; + test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result); + bool r = strcmp(result.get(), "ab") == 0; + if (!r) + printf("[result=%s]\n", result.get()); + return r; + } + +bool test_assign_c() + { + nsCString c; c.Assign('c'); + bool r = strcmp(c.get(), "c") == 0; + if (!r) + printf("[result=%s]\n", c.get()); + return r; + } + +bool test1() + { + NS_NAMED_LITERAL_STRING(empty, ""); + const nsAString& aStr = empty; + + nsAutoString buf(aStr); + + int32_t n = buf.FindChar(','); + + n = buf.Length(); + + buf.Cut(0, n + 1); + n = buf.FindChar(','); + + if (n != kNotFound) + printf("n=%d\n", n); + + return n == kNotFound; + } + +bool test2() + { + nsCString data("hello world"); + const nsACString& aStr = data; + + nsCString temp(aStr); + temp.Cut(0, 6); + + bool r = strcmp(temp.get(), "world") == 0; + if (!r) + printf("[temp=%s]\n", temp.get()); + return r; + } + +bool test_find() + { + nsCString src(""); + + int32_t i = src.Find("DOCTYPE", true, 2, 1); + if (i == 2) + return true; + + printf("i=%d\n", i); + return false; + } + +bool test_rfind() + { + const char text[] = ""; + const char term[] = "bLaH"; + nsCString src(text); + int32_t i; + + i = src.RFind(term, true, 3, -1); + if (i != kNotFound) + { + printf("unexpected result searching from offset=3, i=%d\n", i); + return false; + } + + i = src.RFind(term, true, -1, -1); + if (i != 20) + { + printf("unexpected result searching from offset=-1, i=%d\n", i); + return false; + } + + i = src.RFind(term, true, 13, -1); + if (i != 10) + { + printf("unexpected result searching from offset=13, i=%d\n", i); + return false; + } + + i = src.RFind(term, true, 22, 3); + if (i != 20) + { + printf("unexpected result searching from offset=22, i=%d\n", i); + return false; + } + + return true; + } + +bool test_rfind_2() + { + const char text[] = ""; + nsCString src(text); + int32_t i = src.RFind("TYPE", false, 5, -1); + if (i == 5) + return true; + + printf("i=%d\n", i); + return false; + } + +bool test_rfind_3() + { + const char text[] = "urn:mozilla:locale:en-US:necko"; + nsAutoCString value(text); + int32_t i = value.RFind(":"); + if (i == 24) + return true; + + printf("i=%d\n", i); + return false; + } + +bool test_rfind_4() + { + nsCString value("a.msf"); + int32_t i = value.RFind(".msf"); + if (i != 1) + { + printf("i=%d\n", i); + return false; + } + + return true; + } + +bool test_findinreadable() + { + const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/"; + nsAutoCString value(text); + + nsACString::const_iterator begin, end; + value.BeginReading(begin); + value.EndReading(end); + nsACString::const_iterator delim_begin (begin), + delim_end (end); + + // Search for last !/ at the end of the string + if (!FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end)) + return false; + char *r = ToNewCString(Substring(delim_begin, delim_end)); + // Should match the first "!/" but not the last + if ((delim_end == end) || (strcmp(r, "!/")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + delim_begin = begin; + delim_end = end; + + // Search for first jar: + if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) + return false; + + r = ToNewCString(Substring(delim_begin, delim_end)); + // Should not match the first jar:, but the second one + if ((delim_begin != begin) || (strcmp(r, "jar:")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + // Search for jar: in a Substring + delim_begin = begin; delim_begin++; + delim_end = end; + if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) + return false; + + r = ToNewCString(Substring(delim_begin, delim_end)); + // Should not match the first jar:, but the second one + if ((delim_begin == begin) || (strcmp(r, "jar:")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + // Should not find a match + if (FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)) + return false; + + // When no match is found, range should be empty + if (delim_begin != delim_end) + return false; + + // Should not find a match (search not beyond Substring) + delim_begin = begin; for (int i=0;i<6;i++) delim_begin++; + delim_end = end; + if (FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) + return false; + + // When no match is found, range should be empty + if (delim_begin != delim_end) + return false; + + // Should not find a match (search not beyond Substring) + delim_begin = begin; + delim_end = end; for (int i=0;i<7;i++) delim_end--; + if (FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)) + return false; + + // When no match is found, range should be empty + if (delim_begin != delim_end) + return false; + + return true; + } + +bool test_rfindinreadable() + { + const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/"; + nsAutoCString value(text); + + nsACString::const_iterator begin, end; + value.BeginReading(begin); + value.EndReading(end); + nsACString::const_iterator delim_begin (begin), + delim_end (end); + + // Search for last !/ at the end of the string + if (!RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end)) + return false; + char *r = ToNewCString(Substring(delim_begin, delim_end)); + // Should match the last "!/" + if ((delim_end != end) || (strcmp(r, "!/")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + delim_begin = begin; + delim_end = end; + + // Search for last jar: but not the first one... + if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) + return false; + + r = ToNewCString(Substring(delim_begin, delim_end)); + // Should not match the first jar:, but the second one + if ((delim_begin == begin) || (strcmp(r, "jar:")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + // Search for jar: in a Substring + delim_begin = begin; + delim_end = begin; for (int i=0;i<6;i++) delim_end++; + if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) { + printf("Search for jar: in a Substring\n"); + return false; + } + + r = ToNewCString(Substring(delim_begin, delim_end)); + // Should not match the first jar:, but the second one + if ((delim_begin != begin) || (strcmp(r, "jar:")!=0)) + { + printf("r = %s\n", r); + nsMemory::Free(r); + return false; + } + nsMemory::Free(r); + + // Should not find a match + delim_begin = begin; + delim_end = end; + if (RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)) { + printf("Should not find a match\n"); + return false; + } + + // When no match is found, range should be empty + if (delim_begin != delim_end) { + printf("1: When no match is found, range should be empty\n"); + return false; + } + + // Should not find a match (search not before Substring) + delim_begin = begin; for (int i=0;i<6;i++) delim_begin++; + delim_end = end; + if (RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) { + printf("Should not find a match (search not before Substring)\n"); + return false; + } + + // When no match is found, range should be empty + if (delim_begin != delim_end) { + printf("2: When no match is found, range should be empty\n"); + return false; + } + + // Should not find a match (search not beyond Substring) + delim_begin = begin; + delim_end = end; for (int i=0;i<7;i++) delim_end--; + if (RFindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)) { + printf("Should not find a match (search not beyond Substring)\n"); + return false; + } + + // When no match is found, range should be empty + if (delim_begin != delim_end) { + printf("3: When no match is found, range should be empty\n"); + return false; + } + + return true; + } + +bool test_distance() + { + const char text[] = "abc-xyz"; + nsCString s(text); + nsCString::const_iterator begin, end; + s.BeginReading(begin); + s.EndReading(end); + size_t d = Distance(begin, end); + bool r = (d == sizeof(text)-1); + if (!r) + printf("d=%u\n", d); + return r; + } + +bool test_length() + { + const char text[] = "abc-xyz"; + nsCString s(text); + size_t d = s.Length(); + bool r = (d == sizeof(text)-1); + if (!r) + printf("d=%u\n", d); + return r; + } + +bool test_trim() + { + const char text[] = " a\t $ "; + const char set[] = " \t$"; + + nsCString s(text); + s.Trim(set); + bool r = strcmp(s.get(), "a") == 0; + if (!r) + printf("[s=%s]\n", s.get()); + return r; + } + +bool test_replace_substr() + { + const char text[] = "abc-ppp-qqq-ppp-xyz"; + nsCString s(text); + s.ReplaceSubstring("ppp", "www"); + bool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("foobar"); + s.ReplaceSubstring("foo", "bar"); + s.ReplaceSubstring("bar", ""); + r = strcmp(s.get(), "") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("foofoofoo"); + s.ReplaceSubstring("foo", "foo"); + r = strcmp(s.get(), "foofoofoo") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("foofoofoo"); + s.ReplaceSubstring("of", "fo"); + r = strcmp(s.get(), "fofoofooo") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + return true; + } + +bool test_replace_substr_2() + { + const char *oldName = nullptr; + const char *newName = "user"; + nsString acctName; acctName.AssignLiteral("forums.foo.com"); + nsAutoString newAcctName, oldVal, newVal; + oldVal.AssignWithConversion(oldName); + newVal.AssignWithConversion(newName); + newAcctName.Assign(acctName); + + // here, oldVal is empty. we are testing that this function + // does not hang. see bug 235355. + newAcctName.ReplaceSubstring(oldVal, newVal); + + // we expect that newAcctName will be unchanged. + if (!newAcctName.Equals(acctName)) + return false; + + return true; + } + +bool test_replace_substr_3() + { + nsCString s; + s.Assign("abcabcabc"); + s.ReplaceSubstring("ca", "X"); + bool r = strcmp(s.get(), "abXbXbc") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcabcabc"); + s.ReplaceSubstring("ca", "XYZ"); + r = strcmp(s.get(), "abXYZbXYZbc") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcabcabc"); + s.ReplaceSubstring("ca", "XY"); + r = strcmp(s.get(), "abXYbXYbc") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcabcabc"); + s.ReplaceSubstring("ca", "XYZ!"); + r = strcmp(s.get(), "abXYZ!bXYZ!bc") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "X"); + r = strcmp(s.get(), "aXaXaX") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "XYZ!"); + r = strcmp(s.get(), "aXYZ!aXYZ!aXYZ!") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "XY"); + r = strcmp(s.get(), "aXYaXYaXY") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "XYZABC"); + r = strcmp(s.get(), "aXYZABCaXYZABCaXYZABC") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "XYZ"); + r = strcmp(s.get(), "aXYZaXYZaXYZ") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("bcd", "XYZ!"); + r = strcmp(s.get(), "aXYZ!aXYZ!aXYZ!") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("ab", "X"); + r = strcmp(s.get(), "XcdXcdXcd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("ab", "XYZABC"); + r = strcmp(s.get(), "XYZABCcdXYZABCcdXYZABCcd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("ab", "XY"); + r = strcmp(s.get(), "XYcdXYcdXYcd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("ab", "XYZ!"); + r = strcmp(s.get(), "XYZ!cdXYZ!cdXYZ!cd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("notfound", "X"); + r = strcmp(s.get(), "abcdabcdabcd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + s.Assign("abcdabcdabcd"); + s.ReplaceSubstring("notfound", "longlongstring"); + r = strcmp(s.get(), "abcdabcdabcd") == 0; + if (!r) + { + printf("[s=%s]\n", s.get()); + return false; + } + + return true; + } + +bool test_strip_ws() + { + const char text[] = " a $ "; + nsCString s(text); + s.StripWhitespace(); + bool r = strcmp(s.get(), "a$") == 0; + if (!r) + printf("[s=%s]\n", s.get()); + return r; + } + +bool test_equals_ic() + { + nsCString s; + bool r = s.LowerCaseEqualsLiteral("view-source"); + if (r) + printf("[r=%d]\n", r); + return !r; + } + +bool test_fixed_string() + { + char buf[256] = "hello world"; + + nsFixedCString s(buf, sizeof(buf)); + + if (s.Length() != strlen(buf)) + return false; + + if (strcmp(s.get(), buf) != 0) + return false; + + s.Assign("foopy doopy doo"); + if (s.get() != buf) + return false; + + return true; + } + +bool test_concat() + { + nsCString bar("bar"); + const nsACString& barRef = bar; + + const nsPromiseFlatCString& result = + PromiseFlatCString(NS_LITERAL_CSTRING("foo") + + NS_LITERAL_CSTRING(",") + + barRef); + if (strcmp(result.get(), "foo,bar") == 0) + return true; + + printf("[result=%s]\n", result.get()); + return false; + } + +bool test_concat_2() + { + nsCString fieldTextStr("xyz"); + nsCString text("text"); + const nsACString& aText = text; + + nsAutoCString result( fieldTextStr + aText ); + + if (strcmp(result.get(), "xyztext") == 0) + return true; + + printf("[result=%s]\n", result.get()); + return false; + } + +bool test_concat_3() + { + nsCString result; + nsCString ab("ab"), c("c"); + + result = ab + result + c; + if (strcmp(result.get(), "abc") == 0) + return true; + + printf("[result=%s]\n", result.get()); + return false; + } + +bool test_xpidl_string() + { + nsXPIDLCString a, b; + a = b; + if (a != b) + return false; + + a.Adopt(0); + if (a != b) + return false; + + a.Append("foopy"); + a.Assign(b); + if (a != b) + return false; + + a.Insert("", 0); + a.Assign(b); + if (a != b) + return false; + + const char text[] = "hello world"; + *getter_Copies(a) = NS_strdup(text); + if (strcmp(a, text) != 0) + return false; + + b = a; + if (strcmp(a, b) != 0) + return false; + + a.Adopt(0); + nsACString::const_iterator begin, end; + a.BeginReading(begin); + a.EndReading(end); + char *r = ToNewCString(Substring(begin, end)); + if (strcmp(r, "") != 0) + return false; + nsMemory::Free(r); + + a.Adopt(0); + if (a != (const char*) 0) + return false; + + /* + int32_t index = a.FindCharInSet("xyz"); + if (index != kNotFound) + return false; + */ + + return true; + } + +bool test_empty_assign() + { + nsCString a; + a.AssignLiteral(""); + + a.AppendLiteral(""); + + nsCString b; + b.SetCapacity(0); + return true; + } + +bool test_set_length() + { + const char kText[] = "Default Plugin"; + nsCString buf; + buf.SetCapacity(sizeof(kText)-1); + buf.Assign(kText); + buf.SetLength(sizeof(kText)-1); + if (strcmp(buf.get(), kText) != 0) + return false; + return true; + } + +bool test_substring() + { + nsCString super("hello world"), sub("hello"); + + // this tests that |super| starts with |sub|, + + bool r = sub.Equals(StringHead(super, sub.Length())); + if (!r) + return false; + + // and verifies that |sub| does not start with |super|. + + r = super.Equals(StringHead(sub, super.Length())); + if (r) + return false; + + return true; + } + +#define test_append(str, int, suffix) \ + str.Truncate(); \ + str.AppendInt(suffix = int ## suffix); \ + if (!str.EqualsLiteral(#int)) { \ + fputs("Error appending " #int "\n", stderr); \ + return false; \ + } + +#define test_appends(int, suffix) \ + test_append(str, int, suffix) \ + test_append(cstr, int, suffix) + +#define test_appendbase(str, prefix, int, suffix, base) \ + str.Truncate(); \ + str.AppendInt(suffix = prefix ## int ## suffix, base); \ + if (!str.EqualsLiteral(#int)) { \ + fputs("Error appending " #prefix #int "\n", stderr); \ + return false; \ + } + +#define test_appendbases(prefix, int, suffix, base) \ + test_appendbase(str, prefix, int, suffix, base) \ + test_appendbase(cstr, prefix, int, suffix, base) + +bool test_appendint() + { + nsString str; + nsCString cstr; + int32_t L; + uint32_t UL; + int64_t LL; + uint64_t ULL; + test_appends(2147483647, L) + test_appends(-2147483648, L) + test_appends(4294967295, UL) + test_appends(9223372036854775807, LL) + test_appends(-9223372036854775808, LL) + test_appends(18446744073709551615, ULL) + test_appendbases(0, 17777777777, L, 8) + test_appendbases(0, 20000000000, L, 8) + test_appendbases(0, 37777777777, UL, 8) + test_appendbases(0, 777777777777777777777, LL, 8) + test_appendbases(0, 1000000000000000000000, LL, 8) + test_appendbases(0, 1777777777777777777777, ULL, 8) + test_appendbases(0x, 7fffffff, L, 16) + test_appendbases(0x, 80000000, L, 16) + test_appendbases(0x, ffffffff, UL, 16) + test_appendbases(0x, 7fffffffffffffff, LL, 16) + test_appendbases(0x, 8000000000000000, LL, 16) + test_appendbases(0x, ffffffffffffffff, ULL, 16) + return true; + } + +bool test_appendint64() + { + nsCString str; + + int64_t max = INT64_MAX; + static const char max_expected[] = "9223372036854775807"; + int64_t min = INT64_MIN; + static const char min_expected[] = "-9223372036854775808"; + static const char min_expected_oct[] = "1000000000000000000000"; + int64_t maxint_plus1 = 1LL << 32; + static const char maxint_plus1_expected[] = "4294967296"; + static const char maxint_plus1_expected_x[] = "100000000"; + + str.AppendInt(max); + + if (!str.Equals(max_expected)) { + fprintf(stderr, "Error appending INT64_MAX: Got %s\n", str.get()); + return false; + } + + str.Truncate(); + str.AppendInt(min); + if (!str.Equals(min_expected)) { + fprintf(stderr, "Error appending INT64_MIN: Got %s\n", str.get()); + return false; + } + str.Truncate(); + str.AppendInt(min, 8); + if (!str.Equals(min_expected_oct)) { + fprintf(stderr, "Error appending INT64_MIN (oct): Got %s\n", str.get()); + return false; + } + + + str.Truncate(); + str.AppendInt(maxint_plus1); + if (!str.Equals(maxint_plus1_expected)) { + fprintf(stderr, "Error appending UINT32_MAX + 1: Got %s\n", str.get()); + return false; + } + str.Truncate(); + str.AppendInt(maxint_plus1, 16); + if (!str.Equals(maxint_plus1_expected_x)) { + fprintf(stderr, "Error appending UINT32_MAX + 1 (hex): Got %s\n", str.get()); + return false; + } + + + return true; + } + +bool test_appendfloat() + { + nsCString str; + double bigdouble = 11223344556.66; + static const char double_expected[] = "11223344556.66"; + static const char float_expected[] = "0.01"; + + // AppendFloat is used to append doubles, therefore the precision must be + // large enough (see bug 327719) + str.AppendFloat( bigdouble ); + if (!str.Equals(double_expected)) { + fprintf(stderr, "Error appending a big double: Got %s\n", str.get()); + return false; + } + + str.Truncate(); + // AppendFloat is used to append floats (bug 327719 #27) + str.AppendFloat( 0.1f * 0.1f ); + if (!str.Equals(float_expected)) { + fprintf(stderr, "Error appending a float: Got %s\n", str.get()); + return false; + } + + return true; + } + +bool test_findcharinset() + { + nsCString buf("hello, how are you?"); + + int32_t index = buf.FindCharInSet(",?", 5); + if (index != 5) + return false; + + index = buf.FindCharInSet("helo", 0); + if (index != 0) + return false; + + index = buf.FindCharInSet("z?", 6); + if (index != (int32_t) buf.Length()-1) + return false; + + return true; + } + +bool test_rfindcharinset() + { + nsCString buf("hello, how are you?"); + + int32_t index = buf.RFindCharInSet(",?", 5); + if (index != 5) + return false; + + index = buf.RFindCharInSet("helo", 0); + if (index != 0) + return false; + + index = buf.RFindCharInSet("z?", 6); + if (index != kNotFound) + return false; + + index = buf.RFindCharInSet("l", 5); + if (index != 3) + return false; + + buf.Assign("abcdefghijkabc"); + + index = buf.RFindCharInSet("ab"); + if (index != 12) + return false; + + index = buf.RFindCharInSet("ab", 11); + if (index != 11) + return false; + + index = buf.RFindCharInSet("ab", 10); + if (index != 1) + return false; + + index = buf.RFindCharInSet("ab", 0); + if (index != 0) + return false; + + index = buf.RFindCharInSet("cd", 1); + if (index != kNotFound) + return false; + + index = buf.RFindCharInSet("h"); + if (index != 7) + return false; + + return true; + } + +bool test_stringbuffer() + { + const char kData[] = "hello world"; + + nsStringBuffer *buf; + + buf = nsStringBuffer::Alloc(sizeof(kData)); + if (!buf) + return false; + buf->Release(); + + buf = nsStringBuffer::Alloc(sizeof(kData)); + if (!buf) + return false; + char *data = (char *) buf->Data(); + memcpy(data, kData, sizeof(kData)); + + nsCString str; + buf->ToString(sizeof(kData)-1, str); + + nsStringBuffer *buf2; + buf2 = nsStringBuffer::FromString(str); + + bool rv = (buf == buf2); + + buf->Release(); + return rv; + } + +bool test_voided() + { + const char kData[] = "hello world"; + + nsXPIDLCString str; + if (str) + return false; + if (!str.IsVoid()) + return false; + if (!str.IsEmpty()) + return false; + + str.Assign(kData); + if (strcmp(str, kData) != 0) + return false; + + str.SetIsVoid(true); + if (str) + return false; + if (!str.IsVoid()) + return false; + if (!str.IsEmpty()) + return false; + + str.SetIsVoid(false); + if (strcmp(str, "") != 0) + return false; + + return true; + } + +bool test_voided_autostr() + { + const char kData[] = "hello world"; + + nsAutoCString str; + if (str.IsVoid()) + return false; + if (!str.IsEmpty()) + return false; + + str.Assign(kData); + if (strcmp(str.get(), kData) != 0) + return false; + + str.SetIsVoid(true); + if (!str.IsVoid()) + return false; + if (!str.IsEmpty()) + return false; + + str.Assign(kData); + if (str.IsVoid()) + return false; + if (str.IsEmpty()) + return false; + if (strcmp(str.get(), kData) != 0) + return false; + + return true; + } + +bool test_voided_assignment() + { + nsCString a, b; + b.SetIsVoid(true); + a = b; + return a.IsVoid() && a.get() == b.get(); + } + +bool test_empty_assignment() + { + nsCString a, b; + a = b; + return a.get() == b.get(); + } + +struct ToIntegerTest +{ + const char *str; + uint32_t radix; + int32_t result; + nsresult rv; +}; + +static const ToIntegerTest kToIntegerTests[] = { + { "123", 10, 123, NS_OK }, + { "7b", 16, 123, NS_OK }, + { "90194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE }, + { nullptr, 0, 0, 0 } +}; + +bool test_string_tointeger() +{ + int32_t i; + nsresult rv; + for (const ToIntegerTest* t = kToIntegerTests; t->str; ++t) { + int32_t result = nsAutoCString(t->str).ToInteger(&rv, t->radix); + if (rv != t->rv || result != t->result) + return false; + result = nsAutoCString(t->str).ToInteger(&i, t->radix); + if ((nsresult)i != t->rv || result != t->result) + return false; + } + return true; +} + +static bool test_parse_string_helper(const char* str, char separator, int len, + const char* s1, const char* s2) +{ + nsCString data(str); + nsTArray results; + if (!ParseString(data, separator, results)) + return false; + if (int(results.Length()) != len) + return false; + const char* strings[] = { s1, s2 }; + for (int i = 0; i < len; ++i) { + if (!results[i].Equals(strings[i])) + return false; + } + return true; +} + +static bool test_parse_string_helper0(const char* str, char separator) +{ + return test_parse_string_helper(str, separator, 0, nullptr, nullptr); +} + +static bool test_parse_string_helper1(const char* str, char separator, const char* s1) +{ + return test_parse_string_helper(str, separator, 1, s1, nullptr); +} + +static bool test_parse_string_helper2(const char* str, char separator, const char* s1, const char* s2) +{ + return test_parse_string_helper(str, separator, 2, s1, s2); +} + +static bool test_parse_string() +{ + return test_parse_string_helper1("foo, bar", '_', "foo, bar") && + test_parse_string_helper2("foo, bar", ',', "foo", " bar") && + test_parse_string_helper2("foo, bar ", ' ', "foo,", "bar") && + test_parse_string_helper2("foo,bar", 'o', "f", ",bar") && + test_parse_string_helper0("", '_') && + test_parse_string_helper0(" ", ' ') && + test_parse_string_helper1(" foo", ' ', "foo") && + test_parse_string_helper1(" foo", ' ', "foo"); +} + +static bool test_strip_chars_helper(const char16_t* str, const char16_t* strip, const nsAString& result, uint32_t offset=0) +{ + nsAutoString tmp(str); + nsAString& data = tmp; + data.StripChars(strip, offset); + return data.Equals(result); +} + +static bool test_strip_chars() +{ + return test_strip_chars_helper(MOZ_UTF16("foo \r \nbar"), + MOZ_UTF16(" \n\r"), + NS_LITERAL_STRING("foobar")) && + test_strip_chars_helper(MOZ_UTF16("\r\nfoo\r\n"), + MOZ_UTF16(" \n\r"), + NS_LITERAL_STRING("foo")) && + test_strip_chars_helper(MOZ_UTF16("foo"), + MOZ_UTF16(" \n\r"), + NS_LITERAL_STRING("foo")) && + test_strip_chars_helper(MOZ_UTF16("foo"), + MOZ_UTF16("fo"), + NS_LITERAL_STRING("")) && + test_strip_chars_helper(MOZ_UTF16("foo"), + MOZ_UTF16("foo"), + NS_LITERAL_STRING("")) && + test_strip_chars_helper(MOZ_UTF16(" foo"), + MOZ_UTF16(" "), + NS_LITERAL_STRING(" foo"), 1); +} + +static bool test_huge_capacity() +{ + nsString a, b, c, d, e, f, g, h, i, j, k, l, m, n; + nsCString n1; + bool fail = false; +#undef ok +#define ok(x) { fail |= !(x); } + + ok(a.SetCapacity(1)); + ok(!a.SetCapacity(nsString::size_type(-1)/2)); + ok(a.SetCapacity(0)); // free the allocated memory + + ok(b.SetCapacity(1)); + ok(!b.SetCapacity(nsString::size_type(-1)/2 - 1)); + ok(b.SetCapacity(0)); + + ok(c.SetCapacity(1)); + ok(!c.SetCapacity(nsString::size_type(-1)/2)); + ok(c.SetCapacity(0)); + + ok(!d.SetCapacity(nsString::size_type(-1)/2 - 1)); + ok(!d.SetCapacity(nsString::size_type(-1)/2)); + ok(d.SetCapacity(0)); + + ok(!e.SetCapacity(nsString::size_type(-1)/4)); + ok(!e.SetCapacity(nsString::size_type(-1)/4 + 1)); + ok(e.SetCapacity(0)); + + ok(!f.SetCapacity(nsString::size_type(-1)/2)); + ok(f.SetCapacity(0)); + + ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1000)); + ok(!g.SetCapacity(nsString::size_type(-1)/4 + 1001)); + ok(g.SetCapacity(0)); + + ok(!h.SetCapacity(nsString::size_type(-1)/4+1)); + ok(!h.SetCapacity(nsString::size_type(-1)/2)); + ok(h.SetCapacity(0)); + + ok(i.SetCapacity(1)); + ok(i.SetCapacity(nsString::size_type(-1)/4 - 1000)); + ok(!i.SetCapacity(nsString::size_type(-1)/4 + 1)); + ok(i.SetCapacity(0)); + + ok(j.SetCapacity(nsString::size_type(-1)/4 - 1000)); + ok(!j.SetCapacity(nsString::size_type(-1)/4 + 1)); + ok(j.SetCapacity(0)); + + ok(k.SetCapacity(nsString::size_type(-1)/8 - 1000)); + ok(k.SetCapacity(nsString::size_type(-1)/4 - 1001)); + ok(k.SetCapacity(nsString::size_type(-1)/4 - 998)); + ok(!k.SetCapacity(nsString::size_type(-1)/4 + 1)); + ok(k.SetCapacity(0)); + + ok(l.SetCapacity(nsString::size_type(-1)/8)); + ok(l.SetCapacity(nsString::size_type(-1)/8 + 1)); + ok(l.SetCapacity(nsString::size_type(-1)/8 + 2)); + ok(l.SetCapacity(0)); + + ok(m.SetCapacity(nsString::size_type(-1)/8 + 1000)); + ok(m.SetCapacity(nsString::size_type(-1)/8 + 1001)); + ok(m.SetCapacity(0)); + + ok(n.SetCapacity(nsString::size_type(-1)/8+1)); + ok(!n.SetCapacity(nsString::size_type(-1)/4)); + ok(n.SetCapacity(0)); + + ok(n.SetCapacity(0)); + ok(n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 2)); + ok(n.SetCapacity(0)); + ok(!n.SetCapacity((nsString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 2 - 1)); + ok(n.SetCapacity(0)); + ok(n1.SetCapacity(0)); + ok(n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 2)); + ok(n1.SetCapacity(0)); + ok(!n1.SetCapacity((nsCString::size_type(-1)/2 - sizeof(nsStringBuffer)) / 1 - 1)); + ok(n1.SetCapacity(0)); + + // Ignore the result if the address space is less than 64-bit because + // some of the allocations above will exhaust the address space. + if (sizeof(void*) >= 8) { + return !fail; + } + return true; +} + +static bool test_tofloat_helper(const nsString& aStr, float aExpected, bool aSuccess) +{ + int32_t result; + return aStr.ToFloat(&result) == aExpected && + aSuccess ? result == NS_OK : result != NS_OK; +} + +static bool test_tofloat() +{ + return \ + test_tofloat_helper(NS_LITERAL_STRING("42"), 42.f, true) && + test_tofloat_helper(NS_LITERAL_STRING("42.0"), 42.f, true) && + test_tofloat_helper(NS_LITERAL_STRING("-42"), -42.f, true) && + test_tofloat_helper(NS_LITERAL_STRING("+42"), 42, true) && + test_tofloat_helper(NS_LITERAL_STRING("13.37"), 13.37f, true) && + test_tofloat_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789f, true) && + test_tofloat_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456f, true) && + test_tofloat_helper(NS_LITERAL_STRING("0"), 0.f, true) && + test_tofloat_helper(NS_LITERAL_STRING("1.e5"), 100000, true) && + test_tofloat_helper(NS_LITERAL_STRING(""), 0.f, false) && + test_tofloat_helper(NS_LITERAL_STRING("42foo"), 42.f, false) && + test_tofloat_helper(NS_LITERAL_STRING("foo"), 0.f, false) && + true; +} + +static bool test_todouble_helper(const nsString& aStr, double aExpected, bool aSuccess) +{ + int32_t result; + return aStr.ToDouble(&result) == aExpected && + aSuccess ? result == NS_OK : result != NS_OK; +} + +static bool test_todouble() +{ + return \ + test_todouble_helper(NS_LITERAL_STRING("42"), 42, true) && + test_todouble_helper(NS_LITERAL_STRING("42.0"), 42, true) && + test_todouble_helper(NS_LITERAL_STRING("-42"), -42, true) && + test_todouble_helper(NS_LITERAL_STRING("+42"), 42, true) && + test_todouble_helper(NS_LITERAL_STRING("13.37"), 13.37, true) && + test_todouble_helper(NS_LITERAL_STRING("1.23456789"), 1.23456789, true) && + test_todouble_helper(NS_LITERAL_STRING("1.98765432123456"), 1.98765432123456, true) && + test_todouble_helper(NS_LITERAL_STRING("123456789.98765432123456"), 123456789.98765432123456, true) && + test_todouble_helper(NS_LITERAL_STRING("0"), 0, true) && + test_todouble_helper(NS_LITERAL_STRING("1.e5"), 100000, true) && + test_todouble_helper(NS_LITERAL_STRING(""), 0, false) && + test_todouble_helper(NS_LITERAL_STRING("42foo"), 42, false) && + test_todouble_helper(NS_LITERAL_STRING("foo"), 0, false) && + true; +} + +//---- + +typedef bool (*TestFunc)(); + +static const struct Test + { + const char* name; + TestFunc func; + } +tests[] = + { + { "test_assign", test_assign }, + { "test_assign_c", test_assign_c }, + { "test1", test1 }, + { "test2", test2 }, + { "test_find", test_find }, + { "test_rfind", test_rfind }, + { "test_rfind_2", test_rfind_2 }, + { "test_rfind_3", test_rfind_3 }, + { "test_rfind_4", test_rfind_4 }, + { "test_findinreadable", test_findinreadable }, + { "test_rfindinreadable", test_rfindinreadable }, + { "test_distance", test_distance }, + { "test_length", test_length }, + { "test_trim", test_trim }, + { "test_replace_substr", test_replace_substr }, + { "test_replace_substr_2", test_replace_substr_2 }, + { "test_replace_substr_3", test_replace_substr_3 }, + { "test_strip_ws", test_strip_ws }, + { "test_equals_ic", test_equals_ic }, + { "test_fixed_string", test_fixed_string }, + { "test_concat", test_concat }, + { "test_concat_2", test_concat_2 }, + { "test_concat_3", test_concat_3 }, + { "test_xpidl_string", test_xpidl_string }, + { "test_empty_assign", test_empty_assign }, + { "test_set_length", test_set_length }, + { "test_substring", test_substring }, + { "test_appendint", test_appendint }, + { "test_appendint64", test_appendint64 }, + { "test_appendfloat", test_appendfloat }, + { "test_findcharinset", test_findcharinset }, + { "test_rfindcharinset", test_rfindcharinset }, + { "test_stringbuffer", test_stringbuffer }, + { "test_voided", test_voided }, + { "test_voided_autostr", test_voided_autostr }, + { "test_voided_assignment", test_voided_assignment }, + { "test_empty_assignment", test_empty_assignment }, + { "test_string_tointeger", test_string_tointeger }, + { "test_parse_string", test_parse_string }, + { "test_strip_chars", test_strip_chars }, + { "test_huge_capacity", test_huge_capacity }, + { "test_tofloat", test_tofloat }, + { "test_todouble", test_todouble }, + { nullptr, nullptr } + }; + +} + +using namespace TestStrings; + +int main(int argc, char **argv) + { + int count = 1; + if (argc > 1) + count = atoi(argv[1]); + + NS_LogInit(); + + while (count--) + { + for (const Test* t = tests; t->name != nullptr; ++t) + { + printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--"); + } + } + + return 0; + } diff --git a/xpcom/tests/moz.build b/xpcom/tests/moz.build index 56a2a7a0b29..4b218bdeaa7 100644 --- a/xpcom/tests/moz.build +++ b/xpcom/tests/moz.build @@ -87,6 +87,7 @@ if CONFIG['MOZ_MEMORY']: # 'TestPipes', # 'TestPriorityQueue', # 'TestStorageStream', +# 'TestStrings', # 'TestSynchronization', # 'TestTArray', # 'TestThreadPool',