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 "mozilla/ArrayUtils.h" michael@0: michael@0: #include michael@0: #include michael@0: #include "nsString.h" michael@0: #include "nsStringBuffer.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "UTFStrings.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "mozilla/HashFunctions.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: namespace TestUTF { michael@0: michael@0: bool michael@0: test_valid() michael@0: { michael@0: for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { michael@0: nsDependentCString str8(ValidStrings[i].m8); michael@0: nsDependentString str16(ValidStrings[i].m16); michael@0: michael@0: if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) michael@0: return false; michael@0: michael@0: if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) michael@0: return false; michael@0: michael@0: nsCString tmp8("string "); michael@0: AppendUTF16toUTF8(str16, tmp8); michael@0: if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) michael@0: return false; michael@0: michael@0: nsString tmp16(NS_LITERAL_STRING("string ")); michael@0: AppendUTF8toUTF16(str8, tmp16); michael@0: if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) michael@0: return false; michael@0: michael@0: if (CompareUTF8toUTF16(str8, str16) != 0) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: test_invalid16() michael@0: { michael@0: for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) { michael@0: nsDependentString str16(Invalid16Strings[i].m16); michael@0: nsDependentCString str8(Invalid16Strings[i].m8); michael@0: michael@0: if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) michael@0: return false; michael@0: michael@0: nsCString tmp8("string "); michael@0: AppendUTF16toUTF8(str16, tmp8); michael@0: if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) michael@0: return false; michael@0: michael@0: if (CompareUTF8toUTF16(str8, str16) != 0) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: test_invalid8() michael@0: { michael@0: for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { michael@0: nsDependentString str16(Invalid8Strings[i].m16); michael@0: nsDependentCString str8(Invalid8Strings[i].m8); michael@0: michael@0: if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) michael@0: return false; michael@0: michael@0: nsString tmp16(NS_LITERAL_STRING("string ")); michael@0: AppendUTF8toUTF16(str8, tmp16); michael@0: if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) michael@0: return false; michael@0: michael@0: if (CompareUTF8toUTF16(str8, str16) != 0) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: test_malformed8() michael@0: { michael@0: // Don't run this test in debug builds as that intentionally asserts. michael@0: #ifndef DEBUG michael@0: for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { michael@0: nsDependentCString str8(Malformed8Strings[i]); michael@0: michael@0: if (!NS_ConvertUTF8toUTF16(str8).IsEmpty()) michael@0: return false; michael@0: michael@0: nsString tmp16(NS_LITERAL_STRING("string")); michael@0: AppendUTF8toUTF16(str8, tmp16); michael@0: if (!tmp16.Equals(NS_LITERAL_STRING("string"))) michael@0: return false; michael@0: michael@0: if (CompareUTF8toUTF16(str8, EmptyString()) == 0) michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: test_hashas16() michael@0: { michael@0: for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { michael@0: nsDependentCString str8(ValidStrings[i].m8); michael@0: bool err; michael@0: if (HashString(ValidStrings[i].m16) != michael@0: HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || michael@0: err) michael@0: return false; michael@0: } michael@0: michael@0: for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { michael@0: nsDependentCString str8(Invalid8Strings[i].m8); michael@0: bool err; michael@0: if (HashString(Invalid8Strings[i].m16) != michael@0: HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || michael@0: err) michael@0: return false; michael@0: } michael@0: michael@0: // Don't run this test in debug builds as that intentionally asserts. michael@0: #ifndef DEBUG michael@0: for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { michael@0: nsDependentCString str8(Malformed8Strings[i]); michael@0: bool err; michael@0: if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 || michael@0: !err) michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: return true; 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_valid", test_valid }, michael@0: { "test_invalid16", test_invalid16 }, michael@0: { "test_invalid8", test_invalid8 }, michael@0: { "test_malformed8", test_malformed8 }, michael@0: { "test_hashas16", test_hashas16 }, michael@0: { nullptr, nullptr } michael@0: }; michael@0: michael@0: } michael@0: michael@0: using namespace TestUTF; 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: }