2 ************************************************************************
3 * Copyright (c) 1997-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_NORMALIZATION
12 #include "unicode/uchar.h"
13 #include "unicode/normlzr.h"
14 #include "unicode/uniset.h"
15 #include "unicode/putil.h"
22 #define CASE(id,test,exec) case id: \
26 logln((UnicodeString)""); \
31 void NormalizerConformanceTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
33 CASE(0, TestConformance
, exec
);
34 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
35 CASE(1, TestConformance32
, exec
);
37 // CASE(2, TestCase6);
38 default: name
= ""; break;
44 NormalizerConformanceTest::NormalizerConformanceTest() :
45 normalizer(UnicodeString(), UNORM_NFC
) {}
47 NormalizerConformanceTest::~NormalizerConformanceTest() {}
49 // more interesting conformance test cases, not in the unicode.org NormalizationTest.txt
50 static const char *moreCases
[]={
52 "0061 0332 0308;00E4 0332;0061 0332 0308;00E4 0332;0061 0332 0308; # Markus 0",
54 // Markus 2001oct26 - test edge case for iteration: U+0f73.cc==0 but decomposition.lead.cc==129
55 "0061 0301 0F73;00E1 0F71 0F72;0061 0F71 0F72 0301;00E1 0F71 0F72;0061 0F71 0F72 0301; # Markus 1"
58 void NormalizerConformanceTest::compare(const UnicodeString
& s1
, const UnicodeString
& s2
){
59 UErrorCode status
=U_ZERO_ERROR
;
60 // TODO: Re-enable this tests after UTC fixes UAX 21
61 if(s1
.indexOf((UChar32
)0x0345)>=0)return;
62 if(Normalizer::compare(s1
,s2
,U_FOLD_CASE_DEFAULT
,status
)!=0){
63 errln("Normalizer::compare() failed for s1: " + prettify(s1
) + " s2: " +prettify(s2
));
68 NormalizerConformanceTest::openNormalizationTestFile(const char *filename
) {
69 char unidataPath
[2000];
74 // look inside ICU_DATA first
75 folder
=pathToDataDirectory();
77 strcpy(unidataPath
, folder
);
78 strcat(unidataPath
, "unidata" U_FILE_SEP_STRING
);
79 strcat(unidataPath
, filename
);
80 input
=T_FileStream_open(unidataPath
, "rb");
86 // find icu/source/data/unidata relative to the test data
87 errorCode
=U_ZERO_ERROR
;
88 folder
=loadTestData(errorCode
);
89 if(U_SUCCESS(errorCode
)) {
90 strcpy(unidataPath
, folder
);
91 strcat(unidataPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".."
92 U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".."
93 U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"unidata" U_FILE_SEP_STRING
);
94 strcat(unidataPath
, filename
);
95 input
=T_FileStream_open(unidataPath
, "rb");
101 // look in icu/source/test/testdata/out/build
102 errorCode
=U_ZERO_ERROR
;
103 folder
=loadTestData(errorCode
);
104 if(U_SUCCESS(errorCode
)) {
105 strcpy(unidataPath
, folder
);
106 strcat(unidataPath
, U_FILE_SEP_STRING
);
107 strcat(unidataPath
, filename
);
108 input
=T_FileStream_open(unidataPath
, "rb");
114 // look in icu/source/test/testdata
115 errorCode
=U_ZERO_ERROR
;
116 folder
=loadTestData(errorCode
);
117 if(U_SUCCESS(errorCode
)) {
118 strcpy(unidataPath
, folder
);
119 strcat(unidataPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
);
120 strcat(unidataPath
, filename
);
121 input
=T_FileStream_open(unidataPath
, "rb");
127 // find icu/source/data/unidata relative to U_TOPSRCDIR
128 #if defined(U_TOPSRCDIR)
129 strcpy(unidataPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"unidata" U_FILE_SEP_STRING
);
130 strcat(unidataPath
, filename
);
131 input
=T_FileStream_open(unidataPath
, "rb");
136 strcpy(unidataPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"test" U_FILE_SEP_STRING
"testdata" U_FILE_SEP_STRING
);
137 strcat(unidataPath
, filename
);
138 input
=T_FileStream_open(unidataPath
, "rb");
144 dataerrln("Failed to open %s", filename
);
149 * Test the conformance of Normalizer to
150 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
152 void NormalizerConformanceTest::TestConformance() {
153 TestConformance(openNormalizationTestFile("NormalizationTest.txt"), 0);
156 void NormalizerConformanceTest::TestConformance32() {
157 TestConformance(openNormalizationTestFile("NormalizationTest-3.2.0.txt"), UNORM_UNICODE_3_2
);
160 void NormalizerConformanceTest::TestConformance(FileStream
*input
, int32_t options
) {
161 enum { BUF_SIZE
= 1024 };
162 char lineBuf
[BUF_SIZE
];
163 UnicodeString fields
[FIELD_COUNT
];
164 UErrorCode status
= U_ZERO_ERROR
;
165 int32_t passCount
= 0;
166 int32_t failCount
= 0;
173 // UnicodeSet for all code points that are not mentioned in NormalizationTest.txt
174 UnicodeSet
other(0, 0x10ffff);
176 int32_t count
, countMoreCases
= UPRV_LENGTHOF(moreCases
);
177 for (count
= 1;;++count
) {
178 if (!T_FileStream_eof(input
)) {
179 T_FileStream_readLine(input
, lineBuf
, (int32_t)sizeof(lineBuf
));
181 // once NormalizationTest.txt is finished, use moreCases[]
182 if(count
> countMoreCases
) {
184 } else if(count
== countMoreCases
) {
188 uprv_strcpy(lineBuf
, moreCases
[count
]);
190 if (lineBuf
[0] == 0 || lineBuf
[0] == '\n' || lineBuf
[0] == '\r') continue;
192 // Expect 5 columns of this format:
193 // 1E0C;1E0C;0044 0323;1E0C;0044 0323; # <comments>
195 // Parse out the comment.
196 if (lineBuf
[0] == '#') continue;
198 // Read separator lines starting with '@'
199 if (lineBuf
[0] == '@') {
204 // Parse out the fields
205 if (!hexsplit(lineBuf
, ';', fields
, FIELD_COUNT
)) {
206 errln((UnicodeString
)"Unable to parse line " + count
);
207 break; // Syntax error
210 // Remove a single code point from the "other" UnicodeSet
211 if(fields
[0].length()==fields
[0].moveIndex32(0, 1)) {
212 c
=fields
[0].char32At(0);
213 if(0xac20<=c
&& c
<=0xd73f && quick
) {
214 // not an exhaustive test run: skip most Hangul syllables
216 other
.remove(0xac20, 0xd73f);
223 if (checkConformance(fields
, lineBuf
, options
, status
)) {
227 if(status
== U_FILE_ACCESS_ERROR
) {
228 dataerrln("Something is wrong with the normalizer, skipping the rest of the test.");
232 if ((count
% 1000) == 0) {
233 logln("Line %d", count
);
237 T_FileStream_close(input
);
240 * Test that all characters that are not mentioned
241 * as single code points in column 1
242 * do not change under any normalization.
245 // remove U+ffff because that is the end-of-iteration sentinel value
246 other
.remove(0xffff);
248 for(c
=0; c
<=0x10ffff; quick
? c
+=113 : ++c
) {
249 if(0x30000<=c
&& c
<0xe0000) {
252 if(!other
.contains(c
)) {
256 fields
[0]=fields
[1]=fields
[2]=fields
[3]=fields
[4].setTo(c
);
257 sprintf(lineBuf
, "not mentioned code point U+%04lx", (long)c
);
259 if (checkConformance(fields
, lineBuf
, options
, status
)) {
263 if(status
== U_FILE_ACCESS_ERROR
) {
264 dataerrln("Something is wrong with the normalizer, skipping the rest of the test.: %s", u_errorName(status
));
268 if ((c
% 0x1000) == 0) {
269 logln("Code point U+%04lx", c
);
273 if (failCount
!= 0) {
274 dataerrln((UnicodeString
)"Total: " + failCount
+ " lines/code points failed, " +
275 passCount
+ " lines/code points passed");
277 logln((UnicodeString
)"Total: " + passCount
+ " lines/code points passed");
282 * Verify the conformance of the given line of the Unicode
283 * normalization (UTR 15) test suite file. For each line,
284 * there are five columns, corresponding to field[0]..field[4].
286 * The following invariants must be true for all conformant implementations
287 * c2 == NFC(c1) == NFC(c2) == NFC(c3)
288 * c3 == NFD(c1) == NFD(c2) == NFD(c3)
289 * c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
290 * c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
292 * @param field the 5 columns
293 * @param line the source line from the test suite file
294 * @return true if the test passes
296 UBool
NormalizerConformanceTest::checkConformance(const UnicodeString
* field
,
299 UErrorCode
&status
) {
300 UBool pass
= TRUE
, result
;
301 //UErrorCode status = U_ZERO_ERROR;
302 UnicodeString out
, fcd
;
305 for (int32_t i
=0; i
<FIELD_COUNT
; ++i
) {
308 Normalizer::normalize(field
[i
], UNORM_NFC
, options
, out
, status
);
309 if (U_FAILURE(status
)) {
310 dataerrln("Error running normalize UNORM_NFC: %s", u_errorName(status
));
312 pass
&= assertEqual("C", field
[i
], out
, field
[1], "c2!=C(c", fieldNum
);
313 iterativeNorm(field
[i
], UNORM_NFC
, options
, out
, +1);
314 pass
&= assertEqual("C(+1)", field
[i
], out
, field
[1], "c2!=C(c", fieldNum
);
315 iterativeNorm(field
[i
], UNORM_NFC
, options
, out
, -1);
316 pass
&= assertEqual("C(-1)", field
[i
], out
, field
[1], "c2!=C(c", fieldNum
);
319 Normalizer::normalize(field
[i
], UNORM_NFD
, options
, out
, status
);
320 if (U_FAILURE(status
)) {
321 dataerrln("Error running normalize UNORM_NFD: %s", u_errorName(status
));
323 pass
&= assertEqual("D", field
[i
], out
, field
[2], "c3!=D(c", fieldNum
);
324 iterativeNorm(field
[i
], UNORM_NFD
, options
, out
, +1);
325 pass
&= assertEqual("D(+1)", field
[i
], out
, field
[2], "c3!=D(c", fieldNum
);
326 iterativeNorm(field
[i
], UNORM_NFD
, options
, out
, -1);
327 pass
&= assertEqual("D(-1)", field
[i
], out
, field
[2], "c3!=D(c", fieldNum
);
330 Normalizer::normalize(field
[i
], UNORM_NFKC
, options
, out
, status
);
331 if (U_FAILURE(status
)) {
332 dataerrln("Error running normalize UNORM_NFKC: %s", u_errorName(status
));
334 pass
&= assertEqual("KC", field
[i
], out
, field
[3], "c4!=KC(c", fieldNum
);
335 iterativeNorm(field
[i
], UNORM_NFKC
, options
, out
, +1);
336 pass
&= assertEqual("KC(+1)", field
[i
], out
, field
[3], "c4!=KC(c", fieldNum
);
337 iterativeNorm(field
[i
], UNORM_NFKC
, options
, out
, -1);
338 pass
&= assertEqual("KC(-1)", field
[i
], out
, field
[3], "c4!=KC(c", fieldNum
);
341 Normalizer::normalize(field
[i
], UNORM_NFKD
, options
, out
, status
);
342 if (U_FAILURE(status
)) {
343 dataerrln("Error running normalize UNORM_NFKD: %s", u_errorName(status
));
345 pass
&= assertEqual("KD", field
[i
], out
, field
[4], "c5!=KD(c", fieldNum
);
346 iterativeNorm(field
[i
], UNORM_NFKD
, options
, out
, +1);
347 pass
&= assertEqual("KD(+1)", field
[i
], out
, field
[4], "c5!=KD(c", fieldNum
);
348 iterativeNorm(field
[i
], UNORM_NFKD
, options
, out
, -1);
349 pass
&= assertEqual("KD(-1)", field
[i
], out
, field
[4], "c5!=KD(c", fieldNum
);
352 compare(field
[1],field
[2]);
353 compare(field
[0],field
[1]);
355 if(UNORM_NO
== Normalizer::quickCheck(field
[1], UNORM_NFC
, options
, status
)) {
356 errln("Normalizer error: quickCheck(NFC(s), UNORM_NFC) is UNORM_NO");
359 if(UNORM_NO
== Normalizer::quickCheck(field
[2], UNORM_NFD
, options
, status
)) {
360 errln("Normalizer error: quickCheck(NFD(s), UNORM_NFD) is UNORM_NO");
363 if(UNORM_NO
== Normalizer::quickCheck(field
[3], UNORM_NFKC
, options
, status
)) {
364 errln("Normalizer error: quickCheck(NFKC(s), UNORM_NFKC) is UNORM_NO");
367 if(UNORM_NO
== Normalizer::quickCheck(field
[4], UNORM_NFKD
, options
, status
)) {
368 errln("Normalizer error: quickCheck(NFKD(s), UNORM_NFKD) is UNORM_NO");
372 // branch on options==0 for better code coverage
374 result
= Normalizer::isNormalized(field
[1], UNORM_NFC
, status
);
376 result
= Normalizer::isNormalized(field
[1], UNORM_NFC
, options
, status
);
379 dataerrln("Normalizer error: isNormalized(NFC(s), UNORM_NFC) is FALSE");
382 if(field
[0]!=field
[1] && Normalizer::isNormalized(field
[0], UNORM_NFC
, options
, status
)) {
383 errln("Normalizer error: isNormalized(s, UNORM_NFC) is TRUE");
386 if(!Normalizer::isNormalized(field
[3], UNORM_NFKC
, options
, status
)) {
387 dataerrln("Normalizer error: isNormalized(NFKC(s), UNORM_NFKC) is FALSE");
390 if(field
[0]!=field
[3] && Normalizer::isNormalized(field
[0], UNORM_NFKC
, options
, status
)) {
391 errln("Normalizer error: isNormalized(s, UNORM_NFKC) is TRUE");
395 // test FCD quick check and "makeFCD"
396 Normalizer::normalize(field
[0], UNORM_FCD
, options
, fcd
, status
);
397 if(UNORM_NO
== Normalizer::quickCheck(fcd
, UNORM_FCD
, options
, status
)) {
398 errln("Normalizer error: quickCheck(FCD(s), UNORM_FCD) is UNORM_NO");
401 if(UNORM_NO
== Normalizer::quickCheck(field
[2], UNORM_FCD
, options
, status
)) {
402 errln("Normalizer error: quickCheck(NFD(s), UNORM_FCD) is UNORM_NO");
405 if(UNORM_NO
== Normalizer::quickCheck(field
[4], UNORM_FCD
, options
, status
)) {
406 errln("Normalizer error: quickCheck(NFKD(s), UNORM_FCD) is UNORM_NO");
410 Normalizer::normalize(fcd
, UNORM_NFD
, options
, out
, status
);
411 if(out
!= field
[2]) {
412 dataerrln("Normalizer error: NFD(FCD(s))!=NFD(s)");
416 if (U_FAILURE(status
)) {
417 dataerrln("Normalizer::normalize returned error status: %s", u_errorName(status
));
421 if(field
[0]!=field
[2]) {
422 // two strings that are canonically equivalent must test
423 // equal under a canonical caseless match
424 // see UAX #21 Case Mappings and Jitterbug 2021 and
425 // Unicode Technical Committee meeting consensus 92-C31
429 rc
=Normalizer::compare(field
[0], field
[2], (options
<<UNORM_COMPARE_NORM_OPTIONS_SHIFT
)|U_COMPARE_IGNORE_CASE
, status
);
430 if(U_FAILURE(status
)) {
431 dataerrln("Normalizer::compare(case-insensitive) sets %s", u_errorName(status
));
434 errln("Normalizer::compare(original, NFD, case-insensitive) returned %d instead of 0 for equal", rc
);
440 dataerrln("FAIL: %s", line
);
446 * Do a normalization using the iterative API in the given direction.
447 * @param dir either +1 or -1
449 void NormalizerConformanceTest::iterativeNorm(const UnicodeString
& str
,
450 UNormalizationMode mode
, int32_t options
,
451 UnicodeString
& result
,
453 UErrorCode status
= U_ZERO_ERROR
;
454 normalizer
.setText(str
, status
);
455 normalizer
.setMode(mode
);
456 normalizer
.setOption(-1, 0); // reset all options
457 normalizer
.setOption(options
, 1); // set desired options
459 if (U_FAILURE(status
)) {
464 for (ch
= normalizer
.first(); ch
!= Normalizer::DONE
;
465 ch
= normalizer
.next()) {
469 for (ch
= normalizer
.last(); ch
!= Normalizer::DONE
;
470 ch
= normalizer
.previous()) {
471 result
.insert(0, ch
);
477 * @param op name of normalization form, e.g., "KC"
478 * @param s string being normalized
479 * @param got value received
480 * @param exp expected value
481 * @param msg description of this test
482 * @param return true if got == exp
484 UBool
NormalizerConformanceTest::assertEqual(const char *op
,
485 const UnicodeString
& s
,
486 const UnicodeString
& got
,
487 const UnicodeString
& exp
,
494 char *sChars
, *gotChars
, *expChars
;
495 UnicodeString
sPretty(prettify(s
));
496 UnicodeString
gotPretty(prettify(got
));
497 UnicodeString
expPretty(prettify(exp
));
499 sChars
= new char[sPretty
.length() + 1];
500 gotChars
= new char[gotPretty
.length() + 1];
501 expChars
= new char[expPretty
.length() + 1];
503 sPretty
.extract(0, sPretty
.length(), sChars
, sPretty
.length() + 1);
504 sChars
[sPretty
.length()] = 0;
505 gotPretty
.extract(0, gotPretty
.length(), gotChars
, gotPretty
.length() + 1);
506 gotChars
[gotPretty
.length()] = 0;
507 expPretty
.extract(0, expPretty
.length(), expChars
, expPretty
.length() + 1);
508 expChars
[expPretty
.length()] = 0;
510 errln(" %s%d)%s(%s)=%s, exp. %s", msg
, field
, op
, sChars
, gotChars
, expChars
);
519 * Split a string into pieces based on the given delimiter
520 * character. Then, parse the resultant fields from hex into
521 * characters. That is, "0040 0400;0C00;0899" -> new String[] {
522 * "\u0040\u0400", "\u0C00", "\u0899" }. The output is assumed to
523 * be of the proper length already, and exactly output.length
524 * fields are parsed. If there are too few an exception is
525 * thrown. If there are too many the extras are ignored.
527 * @return FALSE upon failure
529 UBool
NormalizerConformanceTest::hexsplit(const char *s
, char delimiter
,
530 UnicodeString output
[], int32_t outputLength
) {
535 for (i
=0; i
<outputLength
; ++i
) {
537 while(*t
== ' ' || *t
== '\t') {
541 // read a sequence of code points
544 c
= (UChar32
)uprv_strtoul(t
, &end
, 16);
546 if( (char *)t
== end
||
547 (uint32_t)c
> 0x10ffff ||
548 (*end
!= ' ' && *end
!= '\t' && *end
!= delimiter
)
550 errln(UnicodeString("Bad field ", "") + (i
+ 1) + " in " + UnicodeString(s
, ""));
556 t
= (const char *)end
;
559 while(*t
== ' ' || *t
== '\t') {
563 if(*t
== delimiter
) {
568 if((i
+ 1) == outputLength
) {
571 errln(UnicodeString("Missing field(s) in ", "") + s
+ " only " + (i
+ 1) + " out of " + outputLength
);
580 // Specific tests for debugging. These are generally failures taken from
581 // the conformance file, but culled out to make debugging easier.
583 void NormalizerConformanceTest::TestCase6(void) {
584 _testOneLine("0385;0385;00A8 0301;0020 0308 0301;0020 0308 0301;");
587 void NormalizerConformanceTest::_testOneLine(const char *line
) {
588 UErrorCode status
= U_ZERO_ERROR
;
589 UnicodeString fields
[FIELD_COUNT
];
590 if (!hexsplit(line
, ';', fields
, FIELD_COUNT
)) {
591 errln((UnicodeString
)"Unable to parse line " + line
);
593 checkConformance(fields
, line
, 0, status
);
597 #endif /* #if !UCONFIG_NO_NORMALIZATION */