2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-02-23 09:38:10 -08:00
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2010-02-23 09:38:10 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringBuffer.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "UTFStrings.h"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "mozilla/HashFunctions.h"
|
2010-02-23 09:38:10 -08:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-02-23 09:38:10 -08:00
|
|
|
namespace TestUTF {
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-02-23 09:38:10 -08:00
|
|
|
test_valid()
|
|
|
|
{
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
|
2010-02-23 09:38:10 -08:00
|
|
|
nsDependentCString str8(ValidStrings[i].m8);
|
|
|
|
nsDependentString str16(ValidStrings[i].m16);
|
|
|
|
|
|
|
|
if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
nsCString tmp8("string ");
|
|
|
|
AppendUTF16toUTF8(str16, tmp8);
|
|
|
|
if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
nsString tmp16(NS_LITERAL_STRING("string "));
|
|
|
|
AppendUTF8toUTF16(str8, tmp16);
|
|
|
|
if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
if (CompareUTF8toUTF16(str8, str16) != 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-02-23 09:38:10 -08:00
|
|
|
test_invalid16()
|
|
|
|
{
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
|
2010-02-23 09:38:10 -08:00
|
|
|
nsDependentString str16(Invalid16Strings[i].m16);
|
|
|
|
nsDependentCString str8(Invalid16Strings[i].m8);
|
|
|
|
|
|
|
|
if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
nsCString tmp8("string ");
|
|
|
|
AppendUTF16toUTF8(str16, tmp8);
|
|
|
|
if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
if (CompareUTF8toUTF16(str8, str16) != 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-02-23 09:38:10 -08:00
|
|
|
test_invalid8()
|
|
|
|
{
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
|
2010-02-23 09:38:10 -08:00
|
|
|
nsDependentString str16(Invalid8Strings[i].m16);
|
|
|
|
nsDependentCString str8(Invalid8Strings[i].m8);
|
|
|
|
|
|
|
|
if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
nsString tmp16(NS_LITERAL_STRING("string "));
|
|
|
|
AppendUTF8toUTF16(str8, tmp16);
|
|
|
|
if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
if (CompareUTF8toUTF16(str8, str16) != 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-02-23 09:38:10 -08:00
|
|
|
test_malformed8()
|
|
|
|
{
|
2010-03-10 12:03:23 -08:00
|
|
|
// Don't run this test in debug builds as that intentionally asserts.
|
|
|
|
#ifndef DEBUG
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
|
2010-02-23 09:38:10 -08:00
|
|
|
nsDependentCString str8(Malformed8Strings[i]);
|
|
|
|
|
|
|
|
if (!NS_ConvertUTF8toUTF16(str8).IsEmpty())
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
nsString tmp16(NS_LITERAL_STRING("string"));
|
|
|
|
AppendUTF8toUTF16(str8, tmp16);
|
|
|
|
if (!tmp16.Equals(NS_LITERAL_STRING("string")))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
if (CompareUTF8toUTF16(str8, EmptyString()) == 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
2010-03-10 12:03:23 -08:00
|
|
|
#endif
|
2010-02-23 09:38:10 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-02-23 09:38:10 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-08 07:45:00 -08:00
|
|
|
test_hashas16()
|
|
|
|
{
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
|
2010-03-08 07:45:00 -08:00
|
|
|
nsDependentCString str8(ValidStrings[i].m8);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool err;
|
2012-03-12 15:53:18 -07:00
|
|
|
if (HashString(ValidStrings[i].m16) !=
|
|
|
|
HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
|
2010-03-08 07:45:00 -08:00
|
|
|
err)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
|
2010-03-08 07:45:00 -08:00
|
|
|
nsDependentCString str8(Invalid8Strings[i].m8);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool err;
|
2012-03-12 15:53:18 -07:00
|
|
|
if (HashString(Invalid8Strings[i].m16) !=
|
|
|
|
HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
|
2010-03-08 07:45:00 -08:00
|
|
|
err)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
|
|
|
|
2010-03-10 12:03:23 -08:00
|
|
|
// Don't run this test in debug builds as that intentionally asserts.
|
|
|
|
#ifndef DEBUG
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
|
2010-03-08 07:45:00 -08:00
|
|
|
nsDependentCString str8(Malformed8Strings[i]);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool err;
|
2012-03-12 15:53:18 -07:00
|
|
|
if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 ||
|
2010-03-08 07:45:00 -08:00
|
|
|
!err)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
2010-03-10 12:03:23 -08:00
|
|
|
#endif
|
2010-03-08 07:45:00 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
typedef bool (*TestFunc)();
|
2010-02-23 09:38:10 -08:00
|
|
|
|
|
|
|
static const struct Test
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
TestFunc func;
|
|
|
|
}
|
|
|
|
tests[] =
|
|
|
|
{
|
|
|
|
{ "test_valid", test_valid },
|
|
|
|
{ "test_invalid16", test_invalid16 },
|
|
|
|
{ "test_invalid8", test_invalid8 },
|
|
|
|
{ "test_malformed8", test_malformed8 },
|
2010-03-08 07:45:00 -08:00
|
|
|
{ "test_hashas16", test_hashas16 },
|
2012-07-30 07:20:58 -07:00
|
|
|
{ nullptr, nullptr }
|
2010-02-23 09:38:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace TestUTF;
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
count = atoi(argv[1]);
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
for (const Test* t = tests; t->name != nullptr; ++t)
|
2010-02-23 09:38:10 -08:00
|
|
|
{
|
|
|
|
printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|