2000-07-26 16:27:18 +00:00
|
|
|
/*
|
|
|
|
************************************************************************
|
2003-05-06 01:22:23 +00:00
|
|
|
* Copyright (c) 1997-2003, International Business Machines
|
2000-07-26 16:27:18 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
************************************************************************
|
|
|
|
*/
|
|
|
|
|
2001-11-06 23:09:34 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-05-06 01:22:23 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
|
2002-03-19 17:50:15 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-11-06 23:09:34 +00:00
|
|
|
#include "unicode/normlzr.h"
|
|
|
|
#include "unicode/uniset.h"
|
2003-02-26 01:45:06 +00:00
|
|
|
#include "unormimp.h"
|
2000-07-26 16:27:18 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "filestrm.h"
|
2001-11-06 23:09:34 +00:00
|
|
|
#include "normconf.h"
|
2002-07-25 18:41:14 +00:00
|
|
|
#include <stdio.h>
|
2000-07-26 16:27:18 +00:00
|
|
|
|
|
|
|
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
|
|
|
|
|
|
|
|
#define CASE(id,test) case id: \
|
|
|
|
name = #test; \
|
|
|
|
if (exec) { \
|
|
|
|
logln(#test "---"); \
|
|
|
|
logln((UnicodeString)""); \
|
|
|
|
test(); \
|
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void NormalizerConformanceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
|
2000-07-26 16:27:18 +00:00
|
|
|
switch (index) {
|
2003-02-26 01:45:06 +00:00
|
|
|
CASE(0, TestConformance);
|
|
|
|
CASE(1, TestConformance32);
|
|
|
|
// CASE(2, TestCase6);
|
2000-07-26 16:27:18 +00:00
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FIELD_COUNT 5
|
|
|
|
|
|
|
|
NormalizerConformanceTest::NormalizerConformanceTest() :
|
2001-11-06 23:09:34 +00:00
|
|
|
normalizer(UnicodeString(), UNORM_NFC) {}
|
2000-07-26 16:27:18 +00:00
|
|
|
|
|
|
|
NormalizerConformanceTest::~NormalizerConformanceTest() {}
|
|
|
|
|
2001-11-10 23:02:54 +00:00
|
|
|
// more interesting conformance test cases, not in the unicode.org NormalizationTest.txt
|
|
|
|
static const char *moreCases[]={
|
|
|
|
// Markus 2001aug30
|
|
|
|
"0061 0332 0308;00E4 0332;0061 0332 0308;00E4 0332;0061 0332 0308; # Markus 0",
|
|
|
|
|
|
|
|
// Markus 2001oct26 - test edge case for iteration: U+0f73.cc==0 but decomposition.lead.cc==129
|
|
|
|
"0061 0301 0F73;00E1 0F71 0F72;0061 0F71 0F72 0301;00E1 0F71 0F72;0061 0F71 0F72 0301; # Markus 1"
|
|
|
|
};
|
|
|
|
|
2002-08-01 00:52:36 +00:00
|
|
|
void NormalizerConformanceTest::compare(const UnicodeString& s1, const UnicodeString& s2){
|
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
|
|
|
// TODO: Re-enable this tests after UTC fixes UAX 21
|
|
|
|
if(s1.indexOf((UChar32)0x0345)>=0)return;
|
|
|
|
if(Normalizer::compare(s1,s2,U_FOLD_CASE_DEFAULT,status)!=0){
|
|
|
|
errln("Normalizer::compare() failed for s1: " + prettify(s1) + " s2: " +prettify(s2));
|
|
|
|
}
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
|
|
|
|
FileStream *
|
|
|
|
NormalizerConformanceTest::openNormalizationTestFile(const char *filename) {
|
|
|
|
char path[2000];
|
|
|
|
const char *folder;
|
|
|
|
FileStream *input;
|
|
|
|
UErrorCode errorCode;
|
|
|
|
|
|
|
|
// look inside ICU_DATA first
|
|
|
|
folder=u_getDataDirectory();
|
|
|
|
if(folder!=NULL) {
|
|
|
|
strcpy(path, folder);
|
|
|
|
strcat(path, "unidata" U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find icu/source/data/unidata relative to the test data
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
folder=loadTestData(errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
strcpy(path, folder);
|
|
|
|
strcat(path, U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".."
|
|
|
|
U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".."
|
|
|
|
U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look in icu/source/test/testdata/out/build
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
folder=loadTestData(errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
strcpy(path, folder);
|
|
|
|
strcat(path, U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look in icu/source/test/testdata
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
folder=loadTestData(errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
strcpy(path, folder);
|
|
|
|
strcat(path, U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find icu/source/data/unidata relative to U_TOPSRCDIR
|
|
|
|
#if defined(U_TOPSRCDIR)
|
|
|
|
strcpy(path, U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
2003-04-18 23:41:17 +00:00
|
|
|
|
|
|
|
strcpy(path, U_TOPSRCDIR U_FILE_SEP_STRING "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING);
|
|
|
|
strcat(path, filename);
|
|
|
|
input=T_FileStream_open(path, "rb");
|
|
|
|
if(input!=NULL) {
|
|
|
|
return input;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
errln("Failed to open %s", filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
/**
|
|
|
|
* Test the conformance of Normalizer to
|
2003-02-26 01:45:06 +00:00
|
|
|
* http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
|
2000-07-26 16:27:18 +00:00
|
|
|
*/
|
2003-02-26 01:45:06 +00:00
|
|
|
void NormalizerConformanceTest::TestConformance() {
|
|
|
|
TestConformance(openNormalizationTestFile("NormalizationTest.txt"), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalizerConformanceTest::TestConformance32() {
|
|
|
|
TestConformance(openNormalizationTestFile("NormalizationTest-3.2.0.txt"), UNORM_UNICODE_3_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t options) {
|
2000-07-26 16:27:18 +00:00
|
|
|
enum { BUF_SIZE = 1024 };
|
|
|
|
char lineBuf[BUF_SIZE];
|
|
|
|
UnicodeString fields[FIELD_COUNT];
|
|
|
|
int32_t passCount = 0;
|
|
|
|
int32_t failCount = 0;
|
2001-11-09 23:08:36 +00:00
|
|
|
UChar32 c;
|
2000-07-26 16:27:18 +00:00
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
if(input==NULL) {
|
2000-07-26 16:27:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-06 23:09:34 +00:00
|
|
|
// UnicodeSet for all code points that are not mentioned in NormalizationTest.txt
|
|
|
|
UnicodeSet other(0, 0x10ffff);
|
2000-12-03 03:58:52 +00:00
|
|
|
|
2001-11-10 23:02:54 +00:00
|
|
|
int32_t count, countMoreCases = sizeof(moreCases)/sizeof(moreCases[0]);
|
2001-11-08 00:27:19 +00:00
|
|
|
for (count = 1;;++count) {
|
2001-11-10 23:02:54 +00:00
|
|
|
if (!T_FileStream_eof(input)) {
|
|
|
|
T_FileStream_readLine(input, lineBuf, (int32_t)sizeof(lineBuf));
|
|
|
|
} else {
|
|
|
|
// once NormalizationTest.txt is finished, use moreCases[]
|
|
|
|
if(count > countMoreCases) {
|
|
|
|
count = 0;
|
|
|
|
} else if(count == countMoreCases) {
|
|
|
|
// all done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uprv_strcpy(lineBuf, moreCases[count]);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
2001-11-10 23:02:54 +00:00
|
|
|
if (lineBuf[0] == 0 || lineBuf[0] == '\n' || lineBuf[0] == '\r') continue;
|
2001-11-08 00:27:19 +00:00
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
// Expect 5 columns of this format:
|
|
|
|
// 1E0C;1E0C;0044 0323;1E0C;0044 0323; # <comments>
|
2001-11-08 00:27:19 +00:00
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
// Parse out the comment.
|
2001-08-17 22:50:39 +00:00
|
|
|
if (lineBuf[0] == '#') continue;
|
2001-11-08 00:27:19 +00:00
|
|
|
|
2001-08-17 22:50:39 +00:00
|
|
|
// Read separator lines starting with '@'
|
|
|
|
if (lineBuf[0] == '@') {
|
|
|
|
logln(lineBuf);
|
|
|
|
continue;
|
|
|
|
}
|
2001-11-08 00:27:19 +00:00
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
// Parse out the fields
|
2001-08-17 22:50:39 +00:00
|
|
|
if (!hexsplit(lineBuf, ';', fields, FIELD_COUNT)) {
|
2000-07-26 16:27:18 +00:00
|
|
|
errln((UnicodeString)"Unable to parse line " + count);
|
|
|
|
break; // Syntax error
|
|
|
|
}
|
2001-11-06 23:09:34 +00:00
|
|
|
|
|
|
|
// Remove a single code point from the "other" UnicodeSet
|
|
|
|
if(fields[0].length()==fields[0].moveIndex32(0, 1)) {
|
2001-11-09 23:08:36 +00:00
|
|
|
c=fields[0].char32At(0);
|
|
|
|
if(0xac20<=c && c<=0xd73f && quick) {
|
|
|
|
// not an exhaustive test run: skip most Hangul syllables
|
|
|
|
if(c==0xac20) {
|
|
|
|
other.remove(0xac20, 0xd73f);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
other.remove(c);
|
2001-11-06 23:09:34 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
if (checkConformance(fields, lineBuf, options)) {
|
2000-07-26 16:27:18 +00:00
|
|
|
++passCount;
|
|
|
|
} else {
|
|
|
|
++failCount;
|
|
|
|
}
|
2001-08-17 22:50:39 +00:00
|
|
|
if ((count % 1000) == 0) {
|
2003-02-19 21:52:01 +00:00
|
|
|
logln("Line %d", count);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_FileStream_close(input);
|
|
|
|
|
2001-11-06 23:09:34 +00:00
|
|
|
/*
|
|
|
|
* Test that all characters that are not mentioned
|
|
|
|
* as single code points in column 1
|
|
|
|
* do not change under any normalization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// remove U+ffff because that is the end-of-iteration sentinel value
|
|
|
|
other.remove(0xffff);
|
|
|
|
|
2001-11-09 23:08:36 +00:00
|
|
|
for(c=0; c<=0x10ffff; quick ? c+=113 : ++c) {
|
|
|
|
if(0x30000<=c && c<0xe0000) {
|
2001-11-06 23:09:34 +00:00
|
|
|
c=0xe0000;
|
|
|
|
}
|
|
|
|
if(!other.contains(c)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fields[0]=fields[1]=fields[2]=fields[3]=fields[4].setTo(c);
|
2001-11-14 02:51:50 +00:00
|
|
|
sprintf(lineBuf, "not mentioned code point U+%04lx", (long)c);
|
2001-11-06 23:09:34 +00:00
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
if (checkConformance(fields, lineBuf, options)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
++passCount;
|
|
|
|
} else {
|
|
|
|
++failCount;
|
|
|
|
}
|
2001-11-07 17:40:21 +00:00
|
|
|
if ((c % 0x1000) == 0) {
|
|
|
|
logln("Code point U+%04lx", c);
|
2001-11-06 23:09:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
if (failCount != 0) {
|
2001-11-10 23:02:54 +00:00
|
|
|
errln((UnicodeString)"Total: " + failCount + " lines/code points failed, " +
|
|
|
|
passCount + " lines/code points passed");
|
2000-07-26 16:27:18 +00:00
|
|
|
} else {
|
2001-11-10 23:02:54 +00:00
|
|
|
logln((UnicodeString)"Total: " + passCount + " lines/code points passed");
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify the conformance of the given line of the Unicode
|
|
|
|
* normalization (UTR 15) test suite file. For each line,
|
|
|
|
* there are five columns, corresponding to field[0]..field[4].
|
|
|
|
*
|
|
|
|
* The following invariants must be true for all conformant implementations
|
|
|
|
* c2 == NFC(c1) == NFC(c2) == NFC(c3)
|
|
|
|
* c3 == NFD(c1) == NFD(c2) == NFD(c3)
|
|
|
|
* c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
|
|
|
|
* c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
|
|
|
|
*
|
|
|
|
* @param field the 5 columns
|
|
|
|
* @param line the source line from the test suite file
|
|
|
|
* @return true if the test passes
|
|
|
|
*/
|
|
|
|
UBool NormalizerConformanceTest::checkConformance(const UnicodeString* field,
|
2003-02-26 01:45:06 +00:00
|
|
|
const char *line,
|
|
|
|
int32_t options) {
|
2003-05-10 23:10:09 +00:00
|
|
|
UBool pass = TRUE, result;
|
2000-07-26 16:27:18 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-11-06 23:09:34 +00:00
|
|
|
UnicodeString out, fcd;
|
2001-03-20 19:39:51 +00:00
|
|
|
int32_t fieldNum;
|
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
for (int32_t i=0; i<FIELD_COUNT; ++i) {
|
2001-03-20 19:39:51 +00:00
|
|
|
fieldNum = i+1;
|
2000-07-26 16:27:18 +00:00
|
|
|
if (i<3) {
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(field[i], UNORM_NFC, options, out, status);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("C", field[i], out, field[1], "c2!=C(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFC, options, out, +1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("C(+1)", field[i], out, field[1], "c2!=C(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFC, options, out, -1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("C(-1)", field[i], out, field[1], "c2!=C(c", fieldNum);
|
2000-07-26 16:27:18 +00:00
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(field[i], UNORM_NFD, options, out, status);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("D", field[i], out, field[2], "c3!=D(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFD, options, out, +1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("D(+1)", field[i], out, field[2], "c3!=D(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFD, options, out, -1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("D(-1)", field[i], out, field[2], "c3!=D(c", fieldNum);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(field[i], UNORM_NFKC, options, out, status);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KC", field[i], out, field[3], "c4!=KC(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFKC, options, out, +1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KC(+1)", field[i], out, field[3], "c4!=KC(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFKC, options, out, -1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KC(-1)", field[i], out, field[3], "c4!=KC(c", fieldNum);
|
2000-07-26 16:27:18 +00:00
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(field[i], UNORM_NFKD, options, out, status);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KD", field[i], out, field[4], "c5!=KD(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFKD, options, out, +1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KD(+1)", field[i], out, field[4], "c5!=KD(c", fieldNum);
|
2003-02-26 01:45:06 +00:00
|
|
|
iterativeNorm(field[i], UNORM_NFKD, options, out, -1);
|
2001-03-20 19:39:51 +00:00
|
|
|
pass &= assertEqual("KD(-1)", field[i], out, field[4], "c5!=KD(c", fieldNum);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
2002-08-01 00:52:36 +00:00
|
|
|
compare(field[1],field[2]);
|
|
|
|
compare(field[0],field[1]);
|
2001-11-06 23:09:34 +00:00
|
|
|
// test quick checks
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[1], UNORM_NFC, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFC(s), UNORM_NFC) is UNORM_NO");
|
2002-06-12 03:55:49 +00:00
|
|
|
pass = FALSE;
|
2001-11-06 23:09:34 +00:00
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[2], UNORM_NFD, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFD(s), UNORM_NFD) is UNORM_NO");
|
2002-06-12 03:55:49 +00:00
|
|
|
pass = FALSE;
|
2001-11-06 23:09:34 +00:00
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[3], UNORM_NFKC, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFKC(s), UNORM_NFKC) is UNORM_NO");
|
2002-06-12 03:55:49 +00:00
|
|
|
pass = FALSE;
|
2001-11-06 23:09:34 +00:00
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[4], UNORM_NFKD, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFKD(s), UNORM_NFKD) is UNORM_NO");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-10 23:10:09 +00:00
|
|
|
// branch on options==0 for better code coverage
|
|
|
|
if(options==0) {
|
|
|
|
result = Normalizer::isNormalized(field[1], UNORM_NFC, status);
|
|
|
|
} else {
|
|
|
|
result = Normalizer::isNormalized(field[1], UNORM_NFC, options, status);
|
|
|
|
}
|
|
|
|
if(!result) {
|
2002-06-12 03:55:49 +00:00
|
|
|
errln("Normalizer error: isNormalized(NFC(s), UNORM_NFC) is FALSE");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(field[0]!=field[1] && Normalizer::isNormalized(field[0], UNORM_NFC, options, status)) {
|
2002-06-12 03:55:49 +00:00
|
|
|
errln("Normalizer error: isNormalized(s, UNORM_NFC) is TRUE");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(!Normalizer::isNormalized(field[3], UNORM_NFKC, options, status)) {
|
2002-06-12 03:55:49 +00:00
|
|
|
errln("Normalizer error: isNormalized(NFKC(s), UNORM_NFKC) is FALSE");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(field[0]!=field[3] && Normalizer::isNormalized(field[0], UNORM_NFKC, options, status)) {
|
2002-06-12 03:55:49 +00:00
|
|
|
errln("Normalizer error: isNormalized(s, UNORM_NFKC) is TRUE");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2001-11-06 23:09:34 +00:00
|
|
|
// test FCD quick check and "makeFCD"
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(field[0], UNORM_FCD, options, fcd, status);
|
|
|
|
if(UNORM_NO == Normalizer::quickCheck(fcd, UNORM_FCD, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(FCD(s), UNORM_FCD) is UNORM_NO");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[2], UNORM_FCD, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFD(s), UNORM_FCD) is UNORM_NO");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
2003-02-26 01:45:06 +00:00
|
|
|
if(UNORM_NO == Normalizer::quickCheck(field[4], UNORM_FCD, options, status)) {
|
2001-11-06 23:09:34 +00:00
|
|
|
errln("Normalizer error: quickCheck(NFKD(s), UNORM_FCD) is UNORM_NO");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2003-02-26 01:45:06 +00:00
|
|
|
Normalizer::normalize(fcd, UNORM_NFD, options, out, status);
|
2001-11-06 23:09:34 +00:00
|
|
|
if(out != field[2]) {
|
|
|
|
errln("Normalizer error: NFD(FCD(s))!=NFD(s)");
|
|
|
|
pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("Normalizer::normalize returned error status");
|
2003-02-19 21:52:01 +00:00
|
|
|
pass = FALSE;
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
2003-02-19 21:52:01 +00:00
|
|
|
|
|
|
|
if(field[0]!=field[2]) {
|
|
|
|
// two strings that are canonically equivalent must test
|
|
|
|
// equal under a canonical caseless match
|
|
|
|
// see UAX #21 Case Mappings and Jitterbug 2021 and
|
|
|
|
// Unicode Technical Committee meeting consensus 92-C31
|
|
|
|
int32_t rc;
|
|
|
|
|
|
|
|
status=U_ZERO_ERROR;
|
2003-02-26 01:45:06 +00:00
|
|
|
rc=Normalizer::compare(field[0], field[2], (options<<UNORM_COMPARE_NORM_OPTIONS_SHIFT)|U_COMPARE_IGNORE_CASE, status);
|
2003-02-19 21:52:01 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2003-02-26 01:45:06 +00:00
|
|
|
errln("Normalizer::compare(case-insensitive) sets %s", u_errorName(status));
|
2003-02-19 21:52:01 +00:00
|
|
|
pass=FALSE;
|
|
|
|
} else if(rc!=0) {
|
2003-02-26 01:45:06 +00:00
|
|
|
errln("Normalizer::compare(original, NFD, case-insensitive) returned %d instead of 0 for equal", rc);
|
2003-02-19 21:52:01 +00:00
|
|
|
pass=FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-26 16:27:18 +00:00
|
|
|
if (!pass) {
|
2003-02-19 21:52:01 +00:00
|
|
|
errln("FAIL: %s", line);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a normalization using the iterative API in the given direction.
|
|
|
|
* @param dir either +1 or -1
|
|
|
|
*/
|
|
|
|
void NormalizerConformanceTest::iterativeNorm(const UnicodeString& str,
|
2003-02-26 01:45:06 +00:00
|
|
|
UNormalizationMode mode, int32_t options,
|
2000-07-26 16:27:18 +00:00
|
|
|
UnicodeString& result,
|
|
|
|
int8_t dir) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
normalizer.setText(str, status);
|
|
|
|
normalizer.setMode(mode);
|
2003-02-26 01:45:06 +00:00
|
|
|
normalizer.setOption(-1, 0); // reset all options
|
|
|
|
normalizer.setOption(options, 1); // set desired options
|
2000-07-26 16:27:18 +00:00
|
|
|
result.truncate(0);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UChar32 ch;
|
|
|
|
if (dir > 0) {
|
|
|
|
for (ch = normalizer.first(); ch != Normalizer::DONE;
|
|
|
|
ch = normalizer.next()) {
|
2001-08-17 22:50:39 +00:00
|
|
|
result.append(ch);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (ch = normalizer.last(); ch != Normalizer::DONE;
|
|
|
|
ch = normalizer.previous()) {
|
2001-08-17 22:50:39 +00:00
|
|
|
result.insert(0, ch);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param op name of normalization form, e.g., "KC"
|
|
|
|
* @param s string being normalized
|
|
|
|
* @param got value received
|
|
|
|
* @param exp expected value
|
|
|
|
* @param msg description of this test
|
|
|
|
* @param return true if got == exp
|
|
|
|
*/
|
2001-03-20 19:39:51 +00:00
|
|
|
UBool NormalizerConformanceTest::assertEqual(const char *op,
|
2000-07-26 16:27:18 +00:00
|
|
|
const UnicodeString& s,
|
|
|
|
const UnicodeString& got,
|
|
|
|
const UnicodeString& exp,
|
2001-03-20 19:39:51 +00:00
|
|
|
const char *msg,
|
|
|
|
int32_t field)
|
|
|
|
{
|
|
|
|
if (exp == got)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
char *sChars, *gotChars, *expChars;
|
|
|
|
UnicodeString sPretty(prettify(s));
|
|
|
|
UnicodeString gotPretty(prettify(got));
|
|
|
|
UnicodeString expPretty(prettify(exp));
|
|
|
|
|
|
|
|
sChars = new char[sPretty.length() + 1];
|
|
|
|
gotChars = new char[gotPretty.length() + 1];
|
|
|
|
expChars = new char[expPretty.length() + 1];
|
|
|
|
|
|
|
|
sPretty.extract(0, sPretty.length(), sChars, sPretty.length() + 1);
|
|
|
|
sChars[sPretty.length()] = 0;
|
|
|
|
gotPretty.extract(0, gotPretty.length(), gotChars, gotPretty.length() + 1);
|
|
|
|
gotChars[gotPretty.length()] = 0;
|
|
|
|
expPretty.extract(0, expPretty.length(), expChars, expPretty.length() + 1);
|
|
|
|
expChars[expPretty.length()] = 0;
|
|
|
|
|
|
|
|
errln(" %s%d)%s(%s)=%s, exp. %s", msg, field, op, sChars, gotChars, expChars);
|
|
|
|
|
|
|
|
delete []sChars;
|
|
|
|
delete []gotChars;
|
|
|
|
delete []expChars;
|
2000-07-26 16:27:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a string into pieces based on the given delimiter
|
|
|
|
* character. Then, parse the resultant fields from hex into
|
|
|
|
* characters. That is, "0040 0400;0C00;0899" -> new String[] {
|
|
|
|
* "\u0040\u0400", "\u0C00", "\u0899" }. The output is assumed to
|
|
|
|
* be of the proper length already, and exactly output.length
|
|
|
|
* fields are parsed. If there are too few an exception is
|
|
|
|
* thrown. If there are too many the extras are ignored.
|
|
|
|
*
|
|
|
|
* @return FALSE upon failure
|
|
|
|
*/
|
2001-08-17 22:50:39 +00:00
|
|
|
UBool NormalizerConformanceTest::hexsplit(const char *s, char delimiter,
|
|
|
|
UnicodeString output[], int32_t outputLength) {
|
|
|
|
const char *t = s;
|
|
|
|
char *end = NULL;
|
|
|
|
UChar32 c;
|
2000-07-26 16:27:18 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<outputLength; ++i) {
|
2001-08-17 22:50:39 +00:00
|
|
|
// skip whitespace
|
|
|
|
while(*t == ' ' || *t == '\t') {
|
|
|
|
++t;
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
2001-08-17 22:50:39 +00:00
|
|
|
|
|
|
|
// read a sequence of code points
|
|
|
|
output[i].remove();
|
|
|
|
for(;;) {
|
|
|
|
c = (UChar32)uprv_strtoul(t, &end, 16);
|
|
|
|
|
|
|
|
if( (char *)t == end ||
|
|
|
|
(uint32_t)c > 0x10ffff ||
|
|
|
|
(*end != ' ' && *end != '\t' && *end != delimiter)
|
|
|
|
) {
|
|
|
|
errln(UnicodeString("Bad field ", "") + (i + 1) + " in " + UnicodeString(s, ""));
|
2000-07-26 16:27:18 +00:00
|
|
|
return FALSE;
|
2001-08-17 22:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output[i].append(c);
|
|
|
|
|
|
|
|
t = (const char *)end;
|
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
while(*t == ' ' || *t == '\t') {
|
|
|
|
++t;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*t == delimiter) {
|
|
|
|
++t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(*t == 0) {
|
|
|
|
if((i + 1) == outputLength) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
errln(UnicodeString("Missing field(s) in ", "") + s + " only " + (i + 1) + " out of " + outputLength);
|
|
|
|
return FALSE;
|
2001-04-24 17:34:24 +00:00
|
|
|
}
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specific tests for debugging. These are generally failures taken from
|
|
|
|
// the conformance file, but culled out to make debugging easier.
|
|
|
|
|
|
|
|
void NormalizerConformanceTest::TestCase6(void) {
|
|
|
|
_testOneLine("0385;0385;00A8 0301;0020 0308 0301;0020 0308 0301;");
|
|
|
|
}
|
|
|
|
|
2001-08-17 22:50:39 +00:00
|
|
|
void NormalizerConformanceTest::_testOneLine(const char *line) {
|
2000-07-26 16:27:18 +00:00
|
|
|
UnicodeString fields[FIELD_COUNT];
|
2001-08-17 22:50:39 +00:00
|
|
|
if (!hexsplit(line, ';', fields, FIELD_COUNT)) {
|
2000-07-26 16:27:18 +00:00
|
|
|
errln((UnicodeString)"Unable to parse line " + line);
|
|
|
|
} else {
|
2003-02-26 01:45:06 +00:00
|
|
|
checkConformance(fields, line, 0);
|
2000-07-26 16:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-06 01:22:23 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_NORMALIZATION */
|