2006-07-31 14:37:27 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-06-04 20:44:55 +03:00
|
|
|
#include "gtest/gtest.h"
|
2006-07-31 14:37:27 +04:00
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIUnicodeNormalizer.h"
|
2015-06-04 20:44:55 +03:00
|
|
|
#include "nsString.h"
|
2006-08-02 19:45:39 +04:00
|
|
|
#include "nsCharTraits.h"
|
2013-09-27 20:45:04 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2015-09-10 18:01:24 +03:00
|
|
|
#include "mozilla/Snprintf.h"
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
struct testcaseLine {
|
|
|
|
wchar_t* c1;
|
|
|
|
wchar_t* c2;
|
|
|
|
wchar_t* c3;
|
|
|
|
wchar_t* c4;
|
|
|
|
wchar_t* c5;
|
|
|
|
char* description;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG_smontagu
|
|
|
|
#define DEBUG_NAMED_TESTCASE(t, s) \
|
|
|
|
printf(t ": "); \
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < s.Length(); ++i) \
|
2006-07-31 14:37:27 +04:00
|
|
|
printf("%x ", s.CharAt(i)); \
|
|
|
|
printf("\n")
|
|
|
|
#else
|
|
|
|
#define DEBUG_NAMED_TESTCASE(t, s)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_TESTCASE(x) DEBUG_NAMED_TESTCASE(#x, x)
|
|
|
|
|
|
|
|
#define NORMALIZE_AND_COMPARE(base, comparison, form, description) \
|
|
|
|
normalized.Truncate();\
|
|
|
|
normalizer->NormalizeUnicode##form(comparison, normalized);\
|
|
|
|
DEBUG_NAMED_TESTCASE(#form "(" #comparison ")", normalized);\
|
|
|
|
if (!base.Equals(normalized)) {\
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = false;\
|
2006-07-31 14:37:27 +04:00
|
|
|
showError(description, #base " != " #form "(" #comparison ")\n");\
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID);
|
|
|
|
|
|
|
|
nsIUnicodeNormalizer *normalizer;
|
|
|
|
|
|
|
|
#include "NormalizationData.h"
|
|
|
|
|
2006-08-02 19:45:39 +04:00
|
|
|
void showError(const char* description, const char* errorText)
|
2006-07-31 14:37:27 +04:00
|
|
|
{
|
2015-06-04 20:44:55 +03:00
|
|
|
printf("%s failed: %s", description, errorText);
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool TestInvariants(testcaseLine* testLine)
|
2006-07-31 14:37:27 +04:00
|
|
|
{
|
|
|
|
nsAutoString c1, c2, c3, c4, c5, normalized;
|
2014-01-04 19:02:17 +04:00
|
|
|
c1 = nsDependentString((char16_t*)testLine->c1);
|
|
|
|
c2 = nsDependentString((char16_t*)testLine->c2);
|
|
|
|
c3 = nsDependentString((char16_t*)testLine->c3);
|
|
|
|
c4 = nsDependentString((char16_t*)testLine->c4);
|
|
|
|
c5 = nsDependentString((char16_t*)testLine->c5);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool rv = true;
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
1. The following invariants must be true for all conformant implementations
|
|
|
|
|
|
|
|
NFC
|
|
|
|
c2 == NFC(c1) == NFC(c2) == NFC(c3)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c2);
|
|
|
|
NORMALIZE_AND_COMPARE(c2, c1, NFC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c2, c2, NFC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c2, c3, NFC, testLine->description);
|
|
|
|
|
|
|
|
/*
|
|
|
|
c4 == NFC(c4) == NFC(c5)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c4);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c4, NFC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c5, NFC, testLine->description);
|
|
|
|
|
|
|
|
/*
|
|
|
|
NFD
|
|
|
|
c3 == NFD(c1) == NFD(c2) == NFD(c3)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c3);
|
|
|
|
NORMALIZE_AND_COMPARE(c3, c1, NFD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c3, c2, NFD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c3, c3, NFD, testLine->description);
|
|
|
|
/*
|
|
|
|
c5 == NFD(c4) == NFD(c5)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c5);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c4, NFD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c5, NFD, testLine->description);
|
|
|
|
|
|
|
|
/*
|
|
|
|
NFKC
|
|
|
|
c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c4);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c1, NFKC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c2, NFKC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c3, NFKC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c4, NFKC, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c4, c5, NFKC, testLine->description);
|
|
|
|
|
|
|
|
/*
|
|
|
|
NFKD
|
|
|
|
c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
|
|
|
|
*/
|
|
|
|
DEBUG_TESTCASE(c5);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c1, NFKD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c2, NFKD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c3, NFKD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c4, NFKD, testLine->description);
|
|
|
|
NORMALIZE_AND_COMPARE(c5, c5, NFKD, testLine->description);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t UTF32CodepointFromTestcase(testcaseLine* testLine)
|
2006-07-31 14:37:27 +04:00
|
|
|
{
|
|
|
|
if (!IS_SURROGATE(testLine->c1[0]))
|
|
|
|
return testLine->c1[0];
|
|
|
|
|
2006-12-21 10:03:23 +03:00
|
|
|
NS_ASSERTION(NS_IS_HIGH_SURROGATE(testLine->c1[0]) &&
|
|
|
|
NS_IS_LOW_SURROGATE(testLine->c1[1]),
|
2006-07-31 14:37:27 +04:00
|
|
|
"Test data neither in BMP nor legal surrogate pair");
|
|
|
|
return SURROGATE_TO_UCS4(testLine->c1[0], testLine->c1[1]);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool TestUnspecifiedCodepoint(uint32_t codepoint)
|
2006-07-31 14:37:27 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool rv = true;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t unicharArray[3];
|
2006-07-31 14:37:27 +04:00
|
|
|
nsAutoString X, normalized;
|
|
|
|
|
|
|
|
if (IS_IN_BMP(codepoint)) {
|
|
|
|
unicharArray[0] = codepoint;
|
|
|
|
unicharArray[1] = 0;
|
|
|
|
X = nsDependentString(unicharArray);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unicharArray[0] = H_SURROGATE(codepoint);
|
|
|
|
unicharArray[1] = L_SURROGATE(codepoint);
|
|
|
|
unicharArray[2] = 0;
|
|
|
|
X = nsDependentString(unicharArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
2. For every code point X assigned in this version of Unicode that is not specifically
|
|
|
|
listed in Part 1, the following invariants must be true for all conformant
|
|
|
|
implementations:
|
|
|
|
|
|
|
|
X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X)
|
|
|
|
*/
|
2015-09-10 18:01:24 +03:00
|
|
|
static const size_t len = 9;
|
|
|
|
char description[len];
|
|
|
|
|
2006-07-31 14:37:27 +04:00
|
|
|
DEBUG_TESTCASE(X);
|
2015-09-10 18:01:24 +03:00
|
|
|
snprintf(description, len, "U+%04X", codepoint);
|
2006-07-31 14:37:27 +04:00
|
|
|
NORMALIZE_AND_COMPARE(X, X, NFC, description);
|
|
|
|
NORMALIZE_AND_COMPARE(X, X, NFD, description);
|
|
|
|
NORMALIZE_AND_COMPARE(X, X, NFKC, description);
|
|
|
|
NORMALIZE_AND_COMPARE(X, X, NFKD, description);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestPart0()
|
|
|
|
{
|
|
|
|
printf("Test Part0: Specific cases\n");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t numFailed = 0;
|
|
|
|
uint32_t numPassed = 0;
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
while (Part0TestData[i].c1[0] != 0) {
|
|
|
|
if (TestInvariants(&Part0TestData[i++]))
|
|
|
|
++numPassed;
|
|
|
|
else
|
|
|
|
++numFailed;
|
|
|
|
}
|
|
|
|
printf(" %d cases passed, %d failed\n\n", numPassed, numFailed);
|
2015-06-04 20:44:55 +03:00
|
|
|
EXPECT_EQ(0u, numFailed);
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestPart1()
|
|
|
|
{
|
|
|
|
printf("Test Part1: Character by character test\n");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t numFailed = 0;
|
|
|
|
uint32_t numPassed = 0;
|
|
|
|
uint32_t codepoint;
|
|
|
|
uint32_t testDataCodepoint = UTF32CodepointFromTestcase(&Part1TestData[i]);
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
for (codepoint = 1; codepoint < 0x110000; ++codepoint) {
|
|
|
|
if (testDataCodepoint == codepoint) {
|
|
|
|
if (TestInvariants(&Part1TestData[i]))
|
|
|
|
++numPassed;
|
|
|
|
else
|
|
|
|
++numFailed;
|
|
|
|
testDataCodepoint = UTF32CodepointFromTestcase(&Part1TestData[++i]);
|
|
|
|
} else {
|
|
|
|
if (TestUnspecifiedCodepoint(codepoint))
|
|
|
|
++numPassed;
|
|
|
|
else
|
|
|
|
++numFailed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" %d cases passed, %d failed\n\n", numPassed, numFailed);
|
2015-06-04 20:44:55 +03:00
|
|
|
EXPECT_EQ(0u, numFailed);
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestPart2()
|
|
|
|
{
|
|
|
|
printf("Test Part2: Canonical Order Test\n");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t numFailed = 0;
|
|
|
|
uint32_t numPassed = 0;
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
while (Part2TestData[i].c1[0] != 0) {
|
|
|
|
if (TestInvariants(&Part2TestData[i++]))
|
|
|
|
++numPassed;
|
|
|
|
else
|
|
|
|
++numFailed;
|
|
|
|
}
|
|
|
|
printf(" %d cases passed, %d failed\n\n", numPassed, numFailed);
|
2015-06-04 20:44:55 +03:00
|
|
|
EXPECT_EQ(0u, numFailed);
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestPart3()
|
|
|
|
{
|
|
|
|
printf("Test Part3: PRI #29 Test\n");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t numFailed = 0;
|
|
|
|
uint32_t numPassed = 0;
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
while (Part3TestData[i].c1[0] != 0) {
|
|
|
|
if (TestInvariants(&Part3TestData[i++]))
|
|
|
|
++numPassed;
|
|
|
|
else
|
|
|
|
++numFailed;
|
|
|
|
}
|
|
|
|
printf(" %d cases passed, %d failed\n\n", numPassed, numFailed);
|
2015-06-04 20:44:55 +03:00
|
|
|
EXPECT_EQ(0u, numFailed);
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:44:55 +03:00
|
|
|
TEST(NormalizationTest, Main) {
|
2006-07-31 14:37:27 +04:00
|
|
|
if (sizeof(wchar_t) != 2) {
|
|
|
|
printf("This test can only be run where sizeof(wchar_t) == 2\n");
|
2015-06-04 20:44:55 +03:00
|
|
|
return;
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
if (strlen(versionText) == 0) {
|
|
|
|
printf("No testcases: to run the tests generate the header file using\n");
|
|
|
|
printf(" perl genNormalizationData.pl\n");
|
|
|
|
printf("in intl/unichar/tools and rebuild\n");
|
2015-06-04 20:44:55 +03:00
|
|
|
return;
|
2006-07-31 14:37:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("NormalizationTest: test nsIUnicodeNormalizer. UCD version: %s\n",
|
|
|
|
versionText);
|
|
|
|
|
2012-08-24 11:49:00 +04:00
|
|
|
normalizer = nullptr;
|
2006-07-31 14:37:27 +04:00
|
|
|
nsresult res;
|
|
|
|
res = CallGetService(kUnicodeNormalizerCID, &normalizer);
|
|
|
|
|
2015-06-04 20:44:55 +03:00
|
|
|
ASSERT_FALSE(NS_FAILED(res)) << "GetService failed";
|
|
|
|
ASSERT_NE(nullptr, normalizer);
|
2006-07-31 14:37:27 +04:00
|
|
|
|
|
|
|
TestPart0();
|
|
|
|
TestPart1();
|
|
|
|
TestPart2();
|
|
|
|
TestPart3();
|
|
|
|
|
|
|
|
NS_RELEASE(normalizer);
|
|
|
|
}
|