• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2016, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 
9 #include "unicode/utypes.h"
10 
11 #if !UCONFIG_NO_NORMALIZATION
12 
13 #include "unicode/uchar.h"
14 #include "unicode/errorcode.h"
15 #include "unicode/normlzr.h"
16 #include "unicode/stringoptions.h"
17 #include "unicode/stringpiece.h"
18 #include "unicode/uniset.h"
19 #include "unicode/usetiter.h"
20 #include "unicode/schriter.h"
21 #include "unicode/utf16.h"
22 #include "cmemory.h"
23 #include "cstring.h"
24 #include "normalizer2impl.h"
25 #include "testutil.h"
26 #include "tstnorm.h"
27 
28 #define ARRAY_LENGTH(array) UPRV_LENGTHOF(array)
29 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)30 void BasicNormalizerTest::runIndexedTest(int32_t index, UBool exec,
31                                          const char* &name, char* /*par*/) {
32     if(exec) {
33         logln("TestSuite BasicNormalizerTest: ");
34     }
35     TESTCASE_AUTO_BEGIN;
36     TESTCASE_AUTO(TestDecomp);
37     TESTCASE_AUTO(TestCompatDecomp);
38     TESTCASE_AUTO(TestCanonCompose);
39     TESTCASE_AUTO(TestCompatCompose);
40     TESTCASE_AUTO(TestPrevious);
41     TESTCASE_AUTO(TestHangulDecomp);
42     TESTCASE_AUTO(TestHangulCompose);
43     TESTCASE_AUTO(TestTibetan);
44     TESTCASE_AUTO(TestCompositionExclusion);
45     TESTCASE_AUTO(TestZeroIndex);
46     TESTCASE_AUTO(TestVerisign);
47     TESTCASE_AUTO(TestPreviousNext);
48     TESTCASE_AUTO(TestNormalizerAPI);
49     TESTCASE_AUTO(TestConcatenate);
50     TESTCASE_AUTO(FindFoldFCDExceptions);
51     TESTCASE_AUTO(TestCompare);
52     TESTCASE_AUTO(TestSkippable);
53 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
54     TESTCASE_AUTO(TestCustomComp);
55     TESTCASE_AUTO(TestCustomFCC);
56 #endif
57     TESTCASE_AUTO(TestFilteredNormalizer2Coverage);
58     TESTCASE_AUTO(TestComposeUTF8WithEdits);
59     TESTCASE_AUTO(TestDecomposeUTF8WithEdits);
60     TESTCASE_AUTO(TestLowMappingToEmpty_D);
61     TESTCASE_AUTO(TestLowMappingToEmpty_FCD);
62     TESTCASE_AUTO(TestNormalizeIllFormedText);
63     TESTCASE_AUTO(TestComposeJamoTBase);
64     TESTCASE_AUTO(TestComposeBoundaryAfter);
65     TESTCASE_AUTO(TestNFKC_SCF);
66     TESTCASE_AUTO_END;
67 }
68 
69 /**
70  * Convert Java-style strings with \u Unicode escapes into UnicodeString objects
71  */
str(const char * input)72 static UnicodeString str(const char *input)
73 {
74     UnicodeString str(input, ""); // Invariant conversion
75     return str.unescape();
76 }
77 
78 
BasicNormalizerTest()79 BasicNormalizerTest::BasicNormalizerTest()
80 {
81   // canonTest
82   // Input                    Decomposed                    Composed
83 
84     canonTests[0][0] = str("cat");  canonTests[0][1] = str("cat"); canonTests[0][2] =  str("cat");
85 
86     canonTests[1][0] = str("\\u00e0ardvark");    canonTests[1][1] = str("a\\u0300ardvark");  canonTests[1][2] = str("\\u00e0ardvark");
87 
88     canonTests[2][0] = str("\\u1e0a"); canonTests[2][1] = str("D\\u0307"); canonTests[2][2] = str("\\u1e0a");                 // D-dot_above
89 
90     canonTests[3][0] = str("D\\u0307");  canonTests[3][1] = str("D\\u0307"); canonTests[3][2] = str("\\u1e0a");            // D dot_above
91 
92     canonTests[4][0] = str("\\u1e0c\\u0307"); canonTests[4][1] = str("D\\u0323\\u0307");  canonTests[4][2] = str("\\u1e0c\\u0307");         // D-dot_below dot_above
93 
94     canonTests[5][0] = str("\\u1e0a\\u0323"); canonTests[5][1] = str("D\\u0323\\u0307");  canonTests[5][2] = str("\\u1e0c\\u0307");        // D-dot_above dot_below
95 
96     canonTests[6][0] = str("D\\u0307\\u0323"); canonTests[6][1] = str("D\\u0323\\u0307");  canonTests[6][2] = str("\\u1e0c\\u0307");         // D dot_below dot_above
97 
98     canonTests[7][0] = str("\\u1e10\\u0307\\u0323");  canonTests[7][1] = str("D\\u0327\\u0323\\u0307"); canonTests[7][2] = str("\\u1e10\\u0323\\u0307");     // D dot_below cedilla dot_above
99 
100     canonTests[8][0] = str("D\\u0307\\u0328\\u0323"); canonTests[8][1] = str("D\\u0328\\u0323\\u0307"); canonTests[8][2] = str("\\u1e0c\\u0328\\u0307");     // D dot_above ogonek dot_below
101 
102     canonTests[9][0] = str("\\u1E14"); canonTests[9][1] = str("E\\u0304\\u0300"); canonTests[9][2] = str("\\u1E14");         // E-macron-grave
103 
104     canonTests[10][0] = str("\\u0112\\u0300"); canonTests[10][1] = str("E\\u0304\\u0300");  canonTests[10][2] = str("\\u1E14");            // E-macron + grave
105 
106     canonTests[11][0] = str("\\u00c8\\u0304"); canonTests[11][1] = str("E\\u0300\\u0304");  canonTests[11][2] = str("\\u00c8\\u0304");         // E-grave + macron
107 
108     canonTests[12][0] = str("\\u212b"); canonTests[12][1] = str("A\\u030a"); canonTests[12][2] = str("\\u00c5");             // angstrom_sign
109 
110     canonTests[13][0] = str("\\u00c5");      canonTests[13][1] = str("A\\u030a");  canonTests[13][2] = str("\\u00c5");            // A-ring
111 
112     canonTests[14][0] = str("\\u00C4ffin");  canonTests[14][1] = str("A\\u0308ffin");  canonTests[14][2] = str("\\u00C4ffin");
113 
114     canonTests[15][0] = str("\\u00C4\\uFB03n"); canonTests[15][1] = str("A\\u0308\\uFB03n"); canonTests[15][2] = str("\\u00C4\\uFB03n");
115 
116     canonTests[16][0] = str("Henry IV"); canonTests[16][1] = str("Henry IV"); canonTests[16][2] = str("Henry IV");
117 
118     canonTests[17][0] = str("Henry \\u2163");  canonTests[17][1] = str("Henry \\u2163");  canonTests[17][2] = str("Henry \\u2163");
119 
120     canonTests[18][0] = str("\\u30AC");  canonTests[18][1] = str("\\u30AB\\u3099");  canonTests[18][2] = str("\\u30AC");              // ga (Katakana)
121 
122     canonTests[19][0] = str("\\u30AB\\u3099"); canonTests[19][1] = str("\\u30AB\\u3099");  canonTests[19][2] = str("\\u30AC");            // ka + ten
123 
124     canonTests[20][0] = str("\\uFF76\\uFF9E"); canonTests[20][1] = str("\\uFF76\\uFF9E");  canonTests[20][2] = str("\\uFF76\\uFF9E");       // hw_ka + hw_ten
125 
126     canonTests[21][0] = str("\\u30AB\\uFF9E"); canonTests[21][1] = str("\\u30AB\\uFF9E");  canonTests[21][2] = str("\\u30AB\\uFF9E");         // ka + hw_ten
127 
128     canonTests[22][0] = str("\\uFF76\\u3099"); canonTests[22][1] = str("\\uFF76\\u3099");  canonTests[22][2] = str("\\uFF76\\u3099");         // hw_ka + ten
129 
130     canonTests[23][0] = str("A\\u0300\\u0316"); canonTests[23][1] = str("A\\u0316\\u0300");  canonTests[23][2] = str("\\u00C0\\u0316");
131 
132     /* compatTest */
133   // Input                        Decomposed                        Composed
134   compatTests[0][0] = str("cat"); compatTests[0][1] = str("cat"); compatTests[0][2] = str("cat") ;
135 
136   compatTests[1][0] = str("\\uFB4f");  compatTests[1][1] = str("\\u05D0\\u05DC"); compatTests[1][2] = str("\\u05D0\\u05DC");  // Alef-Lamed vs. Alef, Lamed
137 
138   compatTests[2][0] = str("\\u00C4ffin"); compatTests[2][1] = str("A\\u0308ffin"); compatTests[2][2] = str("\\u00C4ffin") ;
139 
140   compatTests[3][0] = str("\\u00C4\\uFB03n"); compatTests[3][1] = str("A\\u0308ffin"); compatTests[3][2] = str("\\u00C4ffin") ; // ffi ligature -> f + f + i
141 
142   compatTests[4][0] = str("Henry IV"); compatTests[4][1] = str("Henry IV"); compatTests[4][2] = str("Henry IV") ;
143 
144   compatTests[5][0] = str("Henry \\u2163"); compatTests[5][1] = str("Henry IV");  compatTests[5][2] = str("Henry IV") ;
145 
146   compatTests[6][0] = str("\\u30AC"); compatTests[6][1] = str("\\u30AB\\u3099"); compatTests[6][2] = str("\\u30AC") ; // ga (Katakana)
147 
148   compatTests[7][0] = str("\\u30AB\\u3099"); compatTests[7][1] = str("\\u30AB\\u3099"); compatTests[7][2] = str("\\u30AC") ; // ka + ten
149 
150   compatTests[8][0] = str("\\uFF76\\u3099"); compatTests[8][1] = str("\\u30AB\\u3099"); compatTests[8][2] = str("\\u30AC") ; // hw_ka + ten
151 
152   /* These two are broken in Unicode 2.1.2 but fixed in 2.1.5 and later */
153   compatTests[9][0] = str("\\uFF76\\uFF9E"); compatTests[9][1] = str("\\u30AB\\u3099"); compatTests[9][2] = str("\\u30AC") ; // hw_ka + hw_ten
154 
155   compatTests[10][0] = str("\\u30AB\\uFF9E"); compatTests[10][1] = str("\\u30AB\\u3099"); compatTests[10][2] = str("\\u30AC") ; // ka + hw_ten
156 
157   /* Hangul Canonical */
158   // Input                        Decomposed                        Composed
159   hangulCanon[0][0] = str("\\ud4db"); hangulCanon[0][1] = str("\\u1111\\u1171\\u11b6"); hangulCanon[0][2] = str("\\ud4db") ;
160 
161   hangulCanon[1][0] = str("\\u1111\\u1171\\u11b6"), hangulCanon[1][1] = str("\\u1111\\u1171\\u11b6"),   hangulCanon[1][2] = str("\\ud4db");
162 }
163 
~BasicNormalizerTest()164 BasicNormalizerTest::~BasicNormalizerTest()
165 {
166 }
167 
TestPrevious()168 void BasicNormalizerTest::TestPrevious()
169 {
170   Normalizer* norm = new Normalizer("", UNORM_NFD);
171 
172   logln("testing decomp...");
173   uint32_t i;
174   for (i = 0; i < ARRAY_LENGTH(canonTests); i++) {
175     backAndForth(norm, canonTests[i][0]);
176   }
177 
178   logln("testing compose...");
179   norm->setMode(UNORM_NFC);
180   for (i = 0; i < ARRAY_LENGTH(canonTests); i++) {
181     backAndForth(norm, canonTests[i][0]);
182   }
183 
184   delete norm;
185 }
186 
TestDecomp()187 void BasicNormalizerTest::TestDecomp()
188 {
189   Normalizer* norm = new Normalizer("", UNORM_NFD);
190   iterateTest(norm, canonTests, ARRAY_LENGTH(canonTests), 1);
191   staticTest(UNORM_NFD, 0, canonTests, ARRAY_LENGTH(canonTests), 1);
192   delete norm;
193 }
194 
TestCompatDecomp()195 void BasicNormalizerTest::TestCompatDecomp()
196 {
197   Normalizer* norm = new Normalizer("", UNORM_NFKD);
198   iterateTest(norm, compatTests, ARRAY_LENGTH(compatTests), 1);
199 
200   staticTest(UNORM_NFKD, 0,
201          compatTests, ARRAY_LENGTH(compatTests), 1);
202   delete norm;
203 }
204 
TestCanonCompose()205 void BasicNormalizerTest::TestCanonCompose()
206 {
207   Normalizer* norm = new Normalizer("", UNORM_NFC);
208   iterateTest(norm, canonTests, ARRAY_LENGTH(canonTests), 2);
209 
210   staticTest(UNORM_NFC, 0, canonTests,
211          ARRAY_LENGTH(canonTests), 2);
212   delete norm;
213 }
214 
TestCompatCompose()215 void BasicNormalizerTest::TestCompatCompose()
216 {
217   Normalizer* norm = new Normalizer("", UNORM_NFKC);
218   iterateTest(norm, compatTests, ARRAY_LENGTH(compatTests), 2);
219 
220   staticTest(UNORM_NFKC, 0,
221          compatTests, ARRAY_LENGTH(compatTests), 2);
222   delete norm;
223 }
224 
225 
226 //-------------------------------------------------------------------------------
227 
TestHangulCompose()228 void BasicNormalizerTest::TestHangulCompose()
229 {
230   // Make sure that the static composition methods work
231   logln("Canonical composition...");
232   staticTest(UNORM_NFC, 0,                    hangulCanon,  ARRAY_LENGTH(hangulCanon),  2);
233   logln("Compatibility composition...");
234 
235   // Now try iterative composition....
236   logln("Static composition...");
237   Normalizer* norm = new Normalizer("", UNORM_NFC);
238   iterateTest(norm, hangulCanon, ARRAY_LENGTH(hangulCanon), 2);
239   norm->setMode(UNORM_NFKC);
240 
241   // And finally, make sure you can do it in reverse too
242   logln("Reverse iteration...");
243   norm->setMode(UNORM_NFC);
244   for (uint32_t i = 0; i < ARRAY_LENGTH(hangulCanon); i++) {
245     backAndForth(norm, hangulCanon[i][0]);
246   }
247   delete norm;
248 }
249 
TestHangulDecomp()250 void BasicNormalizerTest::TestHangulDecomp()
251 {
252   // Make sure that the static decomposition methods work
253   logln("Canonical decomposition...");
254   staticTest(UNORM_NFD, 0,                     hangulCanon,  ARRAY_LENGTH(hangulCanon),  1);
255   logln("Compatibility decomposition...");
256 
257   // Now the iterative decomposition methods...
258   logln("Iterative decomposition...");
259   Normalizer* norm = new Normalizer("", UNORM_NFD);
260   iterateTest(norm, hangulCanon, ARRAY_LENGTH(hangulCanon), 1);
261   norm->setMode(UNORM_NFKD);
262 
263   // And finally, make sure you can do it in reverse too
264   logln("Reverse iteration...");
265   norm->setMode(UNORM_NFD);
266   for (uint32_t i = 0; i < ARRAY_LENGTH(hangulCanon); i++) {
267     backAndForth(norm, hangulCanon[i][0]);
268   }
269   delete norm;
270 }
271 
272 /**
273  * The Tibetan vowel sign AA, 0f71, was messed up prior to Unicode version 2.1.9.
274  */
TestTibetan()275 void BasicNormalizerTest::TestTibetan() {
276     UnicodeString decomp[1][3];
277     decomp[0][0] = str("\\u0f77");
278     decomp[0][1] = str("\\u0f77");
279     decomp[0][2] = str("\\u0fb2\\u0f71\\u0f80");
280 
281     UnicodeString compose[1][3];
282     compose[0][0] = str("\\u0fb2\\u0f71\\u0f80");
283     compose[0][1] = str("\\u0fb2\\u0f71\\u0f80");
284     compose[0][2] = str("\\u0fb2\\u0f71\\u0f80");
285 
286     staticTest(UNORM_NFD,         0, decomp, ARRAY_LENGTH(decomp), 1);
287     staticTest(UNORM_NFKD,  0, decomp, ARRAY_LENGTH(decomp), 2);
288     staticTest(UNORM_NFC,        0, compose, ARRAY_LENGTH(compose), 1);
289     staticTest(UNORM_NFKC, 0, compose, ARRAY_LENGTH(compose), 2);
290 }
291 
292 /**
293  * Make sure characters in the CompositionExclusion.txt list do not get
294  * composed to.
295  */
TestCompositionExclusion()296 void BasicNormalizerTest::TestCompositionExclusion() {
297     // This list is generated from CompositionExclusion.txt.
298     // Update whenever the normalizer tables are updated.  Note
299     // that we test all characters listed, even those that can be
300     // derived from the Unicode DB and are therefore commented
301     // out.
302     // ### TODO read composition exclusion from source/data/unidata file
303     // and test against that
304     UnicodeString EXCLUDED = str(
305         "\\u0340\\u0341\\u0343\\u0344\\u0374\\u037E\\u0387\\u0958"
306         "\\u0959\\u095A\\u095B\\u095C\\u095D\\u095E\\u095F\\u09DC"
307         "\\u09DD\\u09DF\\u0A33\\u0A36\\u0A59\\u0A5A\\u0A5B\\u0A5E"
308         "\\u0B5C\\u0B5D\\u0F43\\u0F4D\\u0F52\\u0F57\\u0F5C\\u0F69"
309         "\\u0F73\\u0F75\\u0F76\\u0F78\\u0F81\\u0F93\\u0F9D\\u0FA2"
310         "\\u0FA7\\u0FAC\\u0FB9\\u1F71\\u1F73\\u1F75\\u1F77\\u1F79"
311         "\\u1F7B\\u1F7D\\u1FBB\\u1FBE\\u1FC9\\u1FCB\\u1FD3\\u1FDB"
312         "\\u1FE3\\u1FEB\\u1FEE\\u1FEF\\u1FF9\\u1FFB\\u1FFD\\u2000"
313         "\\u2001\\u2126\\u212A\\u212B\\u2329\\u232A\\uF900\\uFA10"
314         "\\uFA12\\uFA15\\uFA20\\uFA22\\uFA25\\uFA26\\uFA2A\\uFB1F"
315         "\\uFB2A\\uFB2B\\uFB2C\\uFB2D\\uFB2E\\uFB2F\\uFB30\\uFB31"
316         "\\uFB32\\uFB33\\uFB34\\uFB35\\uFB36\\uFB38\\uFB39\\uFB3A"
317         "\\uFB3B\\uFB3C\\uFB3E\\uFB40\\uFB41\\uFB43\\uFB44\\uFB46"
318         "\\uFB47\\uFB48\\uFB49\\uFB4A\\uFB4B\\uFB4C\\uFB4D\\uFB4E"
319         );
320     UErrorCode status = U_ZERO_ERROR;
321     for (int32_t i=0; i<EXCLUDED.length(); ++i) {
322         UnicodeString a(EXCLUDED.charAt(i));
323         UnicodeString b;
324         UnicodeString c;
325         Normalizer::normalize(a, UNORM_NFKD, 0, b, status);
326         Normalizer::normalize(b, UNORM_NFC, 0, c, status);
327         if (c == a) {
328             errln("FAIL: " + hex(a) + " x DECOMP_COMPAT => " +
329                   hex(b) + " x COMPOSE => " +
330                   hex(c));
331         } else if (verbose) {
332             logln("Ok: " + hex(a) + " x DECOMP_COMPAT => " +
333                   hex(b) + " x COMPOSE => " +
334                   hex(c));
335         }
336     }
337 }
338 
339 /**
340  * Test for a problem that showed up just before ICU 1.6 release
341  * having to do with combining characters with an index of zero.
342  * Such characters do not participate in any canonical
343  * decompositions.  However, having an index of zero means that
344  * they all share one typeMask[] entry, that is, they all have to
345  * map to the same canonical class, which is not the case, in
346  * reality.
347  */
TestZeroIndex()348 void BasicNormalizerTest::TestZeroIndex() {
349     const char* DATA[] = {
350         // Expect col1 x COMPOSE_COMPAT => col2
351         // Expect col2 x DECOMP => col3
352         "A\\u0316\\u0300", "\\u00C0\\u0316", "A\\u0316\\u0300",
353         "A\\u0300\\u0316", "\\u00C0\\u0316", "A\\u0316\\u0300",
354         "A\\u0327\\u0300", "\\u00C0\\u0327", "A\\u0327\\u0300",
355         "c\\u0321\\u0327", "c\\u0321\\u0327", "c\\u0321\\u0327",
356         "c\\u0327\\u0321", "\\u00E7\\u0321", "c\\u0327\\u0321",
357     };
358     int32_t DATA_length = UPRV_LENGTHOF(DATA);
359 
360     for (int32_t i=0; i<DATA_length; i+=3) {
361         UErrorCode status = U_ZERO_ERROR;
362         UnicodeString a(DATA[i], "");
363         a = a.unescape();
364         UnicodeString b;
365         Normalizer::normalize(a, UNORM_NFKC, 0, b, status);
366         if (U_FAILURE(status)) {
367             dataerrln("Error calling normalize UNORM_NFKC: %s", u_errorName(status));
368         } else {
369             UnicodeString exp(DATA[i+1], "");
370             exp = exp.unescape();
371             if (b == exp) {
372                 logln(UnicodeString("Ok: ") + hex(a) + " x COMPOSE_COMPAT => " + hex(b));
373             } else {
374                 errln(UnicodeString("FAIL: ") + hex(a) + " x COMPOSE_COMPAT => " + hex(b) +
375                       ", expect " + hex(exp));
376             }
377         }
378         Normalizer::normalize(b, UNORM_NFD, 0, a, status);
379         if (U_FAILURE(status)) {
380             dataerrln("Error calling normalize UNORM_NFD: %s", u_errorName(status));
381         } else {
382             UnicodeString exp = UnicodeString(DATA[i+2], "").unescape();
383             if (a == exp) {
384                 logln(UnicodeString("Ok: ") + hex(b) + " x DECOMP => " + hex(a));
385             } else {
386                 errln(UnicodeString("FAIL: ") + hex(b) + " x DECOMP => " + hex(a) +
387                       ", expect " + hex(exp));
388             }
389         }
390     }
391 }
392 
393 /**
394  * Run a few specific cases that are failing for Verisign.
395  */
TestVerisign()396 void BasicNormalizerTest::TestVerisign() {
397     /*
398       > Their input:
399       > 05B8 05B9 05B1 0591 05C3 05B0 05AC 059F
400       > Their output (supposedly from ICU):
401       > 05B8 05B1 05B9 0591 05C3 05B0 05AC 059F
402       > My output from charlint:
403       > 05B1 05B8 05B9 0591 05C3 05B0 05AC 059F
404 
405       05B8 05B9 05B1 0591 05C3 05B0 05AC 059F => 05B1 05B8 05B9 0591 05C3 05B0
406       05AC 059F
407 
408       U+05B8  18  E HEBREW POINT QAMATS
409       U+05B9  19  F HEBREW POINT HOLAM
410       U+05B1  11 HEBREW POINT HATAF SEGOL
411       U+0591 220 HEBREW ACCENT ETNAHTA
412       U+05C3   0 HEBREW PUNCTUATION SOF PASUQ
413       U+05B0  10 HEBREW POINT SHEVA
414       U+05AC 230 HEBREW ACCENT ILUY
415       U+059F 230 HEBREW ACCENT QARNEY PARA
416 
417       U+05B1  11 HEBREW POINT HATAF SEGOL
418       U+05B8  18 HEBREW POINT QAMATS
419       U+05B9  19 HEBREW POINT HOLAM
420       U+0591 220 HEBREW ACCENT ETNAHTA
421       U+05C3   0 HEBREW PUNCTUATION SOF PASUQ
422       U+05B0  10 HEBREW POINT SHEVA
423       U+05AC 230 HEBREW ACCENT ILUY
424       U+059F 230 HEBREW ACCENT QARNEY PARA
425 
426       Wrong result:
427       U+05B8  18 HEBREW POINT QAMATS
428       U+05B1  11 HEBREW POINT HATAF SEGOL
429       U+05B9  19 HEBREW POINT HOLAM
430       U+0591 220 HEBREW ACCENT ETNAHTA
431       U+05C3   0 HEBREW PUNCTUATION SOF PASUQ
432       U+05B0  10 HEBREW POINT SHEVA
433       U+05AC 230 HEBREW ACCENT ILUY
434       U+059F 230 HEBREW ACCENT QARNEY PARA
435 
436 
437       > Their input:
438       >0592 05B7 05BC 05A5 05B0 05C0 05C4 05AD
439       >Their output (supposedly from ICU):
440       >0592 05B0 05B7 05BC 05A5 05C0 05AD 05C4
441       >My output from charlint:
442       >05B0 05B7 05BC 05A5 0592 05C0 05AD 05C4
443 
444       0592 05B7 05BC 05A5 05B0 05C0 05C4 05AD => 05B0 05B7 05BC 05A5 0592 05C0
445       05AD 05C4
446 
447       U+0592 230 HEBREW ACCENT SEGOL
448       U+05B7  17 HEBREW POINT PATAH
449       U+05BC  21 HEBREW POINT DAGESH OR MAPIQ
450       U+05A5 220 HEBREW ACCENT MERKHA
451       U+05B0  10 HEBREW POINT SHEVA
452       U+05C0   0 HEBREW PUNCTUATION PASEQ
453       U+05C4 230 HEBREW MARK UPPER DOT
454       U+05AD 222 HEBREW ACCENT DEHI
455 
456       U+05B0  10 HEBREW POINT SHEVA
457       U+05B7  17 HEBREW POINT PATAH
458       U+05BC  21 HEBREW POINT DAGESH OR MAPIQ
459       U+05A5 220 HEBREW ACCENT MERKHA
460       U+0592 230 HEBREW ACCENT SEGOL
461       U+05C0   0 HEBREW PUNCTUATION PASEQ
462       U+05AD 222 HEBREW ACCENT DEHI
463       U+05C4 230 HEBREW MARK UPPER DOT
464 
465       Wrong result:
466       U+0592 230 HEBREW ACCENT SEGOL
467       U+05B0  10 HEBREW POINT SHEVA
468       U+05B7  17 HEBREW POINT PATAH
469       U+05BC  21 HEBREW POINT DAGESH OR MAPIQ
470       U+05A5 220 HEBREW ACCENT MERKHA
471       U+05C0   0 HEBREW PUNCTUATION PASEQ
472       U+05AD 222 HEBREW ACCENT DEHI
473       U+05C4 230 HEBREW MARK UPPER DOT
474     */
475     UnicodeString data[2][3];
476     data[0][0] = str("\\u05B8\\u05B9\\u05B1\\u0591\\u05C3\\u05B0\\u05AC\\u059F");
477     data[0][1] = str("\\u05B1\\u05B8\\u05B9\\u0591\\u05C3\\u05B0\\u05AC\\u059F");
478     data[0][2] = str("");
479     data[1][0] = str("\\u0592\\u05B7\\u05BC\\u05A5\\u05B0\\u05C0\\u05C4\\u05AD");
480     data[1][1] = str("\\u05B0\\u05B7\\u05BC\\u05A5\\u0592\\u05C0\\u05AD\\u05C4");
481     data[1][2] = str("");
482 
483     staticTest(UNORM_NFD, 0, data, ARRAY_LENGTH(data), 1);
484     staticTest(UNORM_NFC, 0, data, ARRAY_LENGTH(data), 1);
485 }
486 
487 //------------------------------------------------------------------------
488 // Internal utilities
489 //
490 
hex(char16_t ch)491 UnicodeString BasicNormalizerTest::hex(char16_t ch) {
492     UnicodeString result;
493     return appendHex(ch, 4, result);
494 }
495 
hex(const UnicodeString & s)496 UnicodeString BasicNormalizerTest::hex(const UnicodeString& s) {
497     UnicodeString result;
498     for (int i = 0; i < s.length(); ++i) {
499         if (i != 0) result += static_cast<char16_t>(0x2c)/*,*/;
500         appendHex(s[i], 4, result);
501     }
502     return result;
503 }
504 
505 
insert(UnicodeString & dest,int pos,UChar32 ch)506 inline static void insert(UnicodeString& dest, int pos, UChar32 ch)
507 {
508     dest.replace(pos, 0, ch);
509 }
510 
backAndForth(Normalizer * iter,const UnicodeString & input)511 void BasicNormalizerTest::backAndForth(Normalizer* iter, const UnicodeString& input)
512 {
513     UChar32 ch;
514     UErrorCode status = U_ZERO_ERROR;
515     iter->setText(input, status);
516 
517     // Run through the iterator forwards and stick it into a StringBuffer
518     UnicodeString forward;
519     for (ch = iter->first(); ch != iter->DONE; ch = iter->next()) {
520         forward += ch;
521     }
522 
523     // Now do it backwards
524     UnicodeString reverse;
525     for (ch = iter->last(); ch != iter->DONE; ch = iter->previous()) {
526         insert(reverse, 0, ch);
527     }
528 
529     if (forward != reverse) {
530         errln("Forward/reverse mismatch for input " + hex(input)
531               + ", forward: " + hex(forward) + ", backward: " + hex(reverse));
532     }
533 }
534 
staticTest(UNormalizationMode mode,int options,UnicodeString tests[][3],int length,int outCol)535 void BasicNormalizerTest::staticTest(UNormalizationMode mode, int options,
536                      UnicodeString tests[][3], int length,
537                      int outCol)
538 {
539     UErrorCode status = U_ZERO_ERROR;
540     for (int i = 0; i < length; i++)
541     {
542         UnicodeString& input = tests[i][0];
543         UnicodeString& expect = tests[i][outCol];
544 
545         logln("Normalizing '" + input + "' (" + hex(input) + ")" );
546 
547         UnicodeString output;
548         Normalizer::normalize(input, mode, options, output, status);
549 
550         if (output != expect) {
551             dataerrln(UnicodeString("ERROR: case ") + i + " normalized " + hex(input) + "\n"
552                 + "                expected " + hex(expect) + "\n"
553                 + "              static got " + hex(output) );
554         }
555     }
556 }
557 
iterateTest(Normalizer * iter,UnicodeString tests[][3],int length,int outCol)558 void BasicNormalizerTest::iterateTest(Normalizer* iter,
559                                       UnicodeString tests[][3], int length,
560                                       int outCol)
561 {
562     UErrorCode status = U_ZERO_ERROR;
563     for (int i = 0; i < length; i++)
564     {
565         UnicodeString& input = tests[i][0];
566         UnicodeString& expect = tests[i][outCol];
567 
568         logln("Normalizing '" + input + "' (" + hex(input) + ")" );
569 
570         iter->setText(input, status);
571         assertEqual(input, expect, iter, UnicodeString("ERROR: case ") + i + " ");
572     }
573 }
574 
assertEqual(const UnicodeString & input,const UnicodeString & expected,Normalizer * iter,const UnicodeString & errPrefix)575 void BasicNormalizerTest::assertEqual(const UnicodeString&    input,
576                       const UnicodeString&    expected,
577                       Normalizer*        iter,
578                       const UnicodeString&    errPrefix)
579 {
580     UnicodeString result;
581 
582     for (UChar32 ch = iter->first(); ch != iter->DONE; ch = iter->next()) {
583         result += ch;
584     }
585     if (result != expected) {
586         dataerrln(errPrefix + "normalized " + hex(input) + "\n"
587             + "                expected " + hex(expected) + "\n"
588             + "             iterate got " + hex(result) );
589     }
590 }
591 
592 // helper class for TestPreviousNext()
593 // simple UTF-32 character iterator
594 class UChar32Iterator {
595 public:
UChar32Iterator(const UChar32 * text,int32_t len,int32_t index)596     UChar32Iterator(const UChar32 *text, int32_t len, int32_t index) :
597         s(text), length(len), i(index) {}
598 
current()599     UChar32 current() {
600         if(i<length) {
601             return s[i];
602         } else {
603             return 0xffff;
604         }
605     }
606 
next()607     UChar32 next() {
608         if(i<length) {
609             return s[i++];
610         } else {
611             return 0xffff;
612         }
613     }
614 
previous()615     UChar32 previous() {
616         if(i>0) {
617             return s[--i];
618         } else {
619             return 0xffff;
620         }
621     }
622 
getIndex()623     int32_t getIndex() {
624         return i;
625     }
626 private:
627     const UChar32 *s;
628     int32_t length, i;
629 };
630 
631 void
TestPreviousNext(const char16_t * src,int32_t srcLength,const UChar32 * expect,int32_t expectLength,const int32_t * expectIndex,int32_t srcMiddle,int32_t expectMiddle,const char * moves,UNormalizationMode mode,const char * name)632 BasicNormalizerTest::TestPreviousNext(const char16_t *src, int32_t srcLength,
633                                       const UChar32 *expect, int32_t expectLength,
634                                       const int32_t *expectIndex, // its length=expectLength+1
635                                       int32_t srcMiddle, int32_t expectMiddle,
636                                       const char *moves,
637                                       UNormalizationMode mode,
638                                       const char *name) {
639     // Sanity check non-iterative normalization.
640     {
641         IcuTestErrorCode errorCode(*this, "TestPreviousNext");
642         UnicodeString result;
643         Normalizer::normalize(UnicodeString(src, srcLength), mode, 0, result, errorCode);
644         if (errorCode.isFailure()) {
645             dataerrln("error: non-iterative normalization of %s failed: %s",
646                       name, errorCode.errorName());
647             errorCode.reset();
648             return;
649         }
650         // UnicodeString::fromUTF32(expect, expectLength)
651         // would turn unpaired surrogates into U+FFFD.
652         for (int32_t i = 0, j = 0; i < result.length(); ++j) {
653             UChar32 c = result.char32At(i);
654             if (c != expect[j]) {
655                 errln("error: non-iterative normalization of %s did not yield the expected result",
656                       name);
657             }
658             i += U16_LENGTH(c);
659         }
660     }
661 
662     // iterators
663     Normalizer iter(src, srcLength, mode);
664 
665     // test getStaticClassID and getDynamicClassID
666     if(iter.getDynamicClassID() != Normalizer::getStaticClassID()) {
667         errln("getStaticClassID != getDynamicClassID for Normalizer.");
668     }
669 
670     UChar32Iterator iter32(expect, expectLength, expectMiddle);
671 
672     UChar32 c1, c2;
673     char m;
674 
675     // initially set the indexes into the middle of the strings
676     iter.setIndexOnly(srcMiddle);
677 
678     // move around and compare the iteration code points with
679     // the expected ones
680     const char *move=moves;
681     while((m=*move++)!=0) {
682         if(m=='-') {
683             c1=iter.previous();
684             c2=iter32.previous();
685         } else if(m=='0') {
686             c1=iter.current();
687             c2=iter32.current();
688         } else /* m=='+' */ {
689             c1=iter.next();
690             c2=iter32.next();
691         }
692 
693         // compare results
694         if(c1!=c2) {
695             // copy the moves until the current (m) move, and terminate
696             char history[64];
697             uprv_strcpy(history, moves);
698             history[move-moves]=0;
699             dataerrln("error: mismatch in Normalizer iteration (%s) at %s: "
700                   "got c1=U+%04lx != expected c2=U+%04lx",
701                   name, history, c1, c2);
702             break;
703         }
704 
705         // compare indexes
706         if(iter.getIndex()!=expectIndex[iter32.getIndex()]) {
707             // copy the moves until the current (m) move, and terminate
708             char history[64];
709             uprv_strcpy(history, moves);
710             history[move-moves]=0;
711             errln("error: index mismatch in Normalizer iteration (%s) at %s: "
712                   "Normalizer index %ld expected %ld\n",
713                   name, history, iter.getIndex(), expectIndex[iter32.getIndex()]);
714             break;
715         }
716     }
717 }
718 
719 void
TestPreviousNext()720 BasicNormalizerTest::TestPreviousNext() {
721     // src and expect strings
722     static const char16_t src[]={
723         U16_LEAD(0x2f999), U16_TRAIL(0x2f999),
724         U16_LEAD(0x1d15f), U16_TRAIL(0x1d15f),
725         0xc4,
726         0x1ed0
727     };
728     static const UChar32 expect[]={
729         0x831d,
730         0x1d158, 0x1d165,
731         0x41, 0x308,
732         0x4f, 0x302, 0x301
733     };
734 
735     // expected src indexes corresponding to expect indexes
736     static const int32_t expectIndex[]={
737         0,
738         2, 2,
739         4, 4,
740         5, 5, 5,
741         6 // behind last character
742     };
743 
744     // src and expect strings for regression test for j2911
745     static const char16_t src_j2911[]={
746         U16_LEAD(0x2f999), U16_TRAIL(0x2f999),
747         0xdd00, 0xd900, // unpaired surrogates - regression test for j2911
748         0xc4,
749         0x4f, 0x302, 0x301
750     };
751     static const UChar32 expect_j2911[]={
752         0x831d,
753         0xdd00, 0xd900, // unpaired surrogates - regression test for j2911
754         0xc4,
755         0x1ed0
756     };
757 
758     // expected src indexes corresponding to expect indexes
759     static const int32_t expectIndex_j2911[]={
760         0,
761         2, 3,
762         4,
763         5,
764         8 // behind last character
765     };
766 
767     // initial indexes into the src and expect strings
768     // for both sets of test data
769     enum {
770         SRC_MIDDLE=4,
771         EXPECT_MIDDLE=3,
772         SRC_MIDDLE_2=2,
773         EXPECT_MIDDLE_2=1
774     };
775 
776     // movement vector
777     // - for previous(), 0 for current(), + for next()
778     // for both sets of test data
779     static const char *const moves="0+0+0--0-0-+++0--+++++++0--------";
780 
781     TestPreviousNext(src, UPRV_LENGTHOF(src),
782                      expect, UPRV_LENGTHOF(expect),
783                      expectIndex,
784                      SRC_MIDDLE, EXPECT_MIDDLE,
785                      moves, UNORM_NFD, "basic");
786 
787     TestPreviousNext(src_j2911, UPRV_LENGTHOF(src_j2911),
788                      expect_j2911, UPRV_LENGTHOF(expect_j2911),
789                      expectIndex_j2911,
790                      SRC_MIDDLE, EXPECT_MIDDLE,
791                      moves, UNORM_NFKC, "j2911");
792 
793     // try again from different "middle" indexes
794     TestPreviousNext(src, UPRV_LENGTHOF(src),
795                      expect, UPRV_LENGTHOF(expect),
796                      expectIndex,
797                      SRC_MIDDLE_2, EXPECT_MIDDLE_2,
798                      moves, UNORM_NFD, "basic_2");
799 
800     TestPreviousNext(src_j2911, UPRV_LENGTHOF(src_j2911),
801                      expect_j2911, UPRV_LENGTHOF(expect_j2911),
802                      expectIndex_j2911,
803                      SRC_MIDDLE_2, EXPECT_MIDDLE_2,
804                      moves, UNORM_NFKC, "j2911_2");
805 }
806 
TestConcatenate()807 void BasicNormalizerTest::TestConcatenate() {
808     static const char *const
809     cases[][4]={
810         /* mode, left, right, result */
811         {
812             "C",
813             "re",
814             "\\u0301sum\\u00e9",
815             "r\\u00e9sum\\u00e9"
816         },
817         {
818             "C",
819             "a\\u1100",
820             "\\u1161bcdefghijk",
821             "a\\uac00bcdefghijk"
822         },
823         /* ### TODO: add more interesting cases */
824         {
825             "D",
826             "\\u03B1\\u0345",
827             "\\u0C4D\\U000110BA\\U0001D169",
828             "\\u03B1\\U0001D169\\U000110BA\\u0C4D\\u0345"
829         }
830     };
831 
832     UnicodeString left, right, expect, result, r;
833     UErrorCode errorCode;
834     UNormalizationMode mode;
835     int32_t i;
836 
837     /* test concatenation */
838     for(i=0; i<UPRV_LENGTHOF(cases); ++i) {
839         switch(*cases[i][0]) {
840         case 'C': mode=UNORM_NFC; break;
841         case 'D': mode=UNORM_NFD; break;
842         case 'c': mode=UNORM_NFKC; break;
843         case 'd': mode=UNORM_NFKD; break;
844         default: mode=UNORM_NONE; break;
845         }
846 
847         left=UnicodeString(cases[i][1], "").unescape();
848         right=UnicodeString(cases[i][2], "").unescape();
849         expect=UnicodeString(cases[i][3], "").unescape();
850 
851         //result=r=UnicodeString();
852         errorCode=U_ZERO_ERROR;
853 
854         r=Normalizer::concatenate(left, right, result, mode, 0, errorCode);
855         if(U_FAILURE(errorCode) || /*result!=r ||*/ result!=expect) {
856             dataerrln("error in Normalizer::concatenate(), cases[] fails with "+
857                 UnicodeString(u_errorName(errorCode))+", result==expect: expected: "+
858                 hex(expect)+" =========> got: " + hex(result));
859         }
860     }
861 
862     /* test error cases */
863 
864     /* left.getBuffer()==result.getBuffer() */
865     result=r=expect=UnicodeString("zz", "");
866     errorCode=U_UNEXPECTED_TOKEN;
867     r=Normalizer::concatenate(left, right, result, mode, 0, errorCode);
868     if(errorCode!=U_UNEXPECTED_TOKEN || result!=r || !result.isBogus()) {
869         errln("error in Normalizer::concatenate(), violates UErrorCode protocol");
870     }
871 
872     left.setToBogus();
873     errorCode=U_ZERO_ERROR;
874     r=Normalizer::concatenate(left, right, result, mode, 0, errorCode);
875     if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR || result!=r || !result.isBogus()) {
876         errln("error in Normalizer::concatenate(), does not detect left.isBogus()");
877     }
878 }
879 
880 // reference implementation of Normalizer::compare
881 static int32_t
ref_norm_compare(const UnicodeString & s1,const UnicodeString & s2,uint32_t options,UErrorCode & errorCode)882 ref_norm_compare(const UnicodeString &s1, const UnicodeString &s2, uint32_t options, UErrorCode &errorCode) {
883     UnicodeString r1, r2, t1, t2;
884     int32_t normOptions = static_cast<int32_t>(options >> UNORM_COMPARE_NORM_OPTIONS_SHIFT);
885 
886     if(options&U_COMPARE_IGNORE_CASE) {
887         Normalizer::decompose(s1, false, normOptions, r1, errorCode);
888         Normalizer::decompose(s2, false, normOptions, r2, errorCode);
889 
890         r1.foldCase(options);
891         r2.foldCase(options);
892     } else {
893         r1=s1;
894         r2=s2;
895     }
896 
897     Normalizer::decompose(r1, false, normOptions, t1, errorCode);
898     Normalizer::decompose(r2, false, normOptions, t2, errorCode);
899 
900     if(options&U_COMPARE_CODE_POINT_ORDER) {
901         return t1.compareCodePointOrder(t2);
902     } else {
903         return t1.compare(t2);
904     }
905 }
906 
907 // test wrapper for Normalizer::compare, sets UNORM_INPUT_IS_FCD appropriately
908 static int32_t
_norm_compare(const UnicodeString & s1,const UnicodeString & s2,uint32_t options,UErrorCode & errorCode)909 _norm_compare(const UnicodeString &s1, const UnicodeString &s2, uint32_t options, UErrorCode &errorCode) {
910     int32_t normOptions = static_cast<int32_t>(options >> UNORM_COMPARE_NORM_OPTIONS_SHIFT);
911 
912     if( UNORM_YES==Normalizer::quickCheck(s1, UNORM_FCD, normOptions, errorCode) &&
913         UNORM_YES==Normalizer::quickCheck(s2, UNORM_FCD, normOptions, errorCode)) {
914         options|=UNORM_INPUT_IS_FCD;
915     }
916 
917     return Normalizer::compare(s1, s2, options, errorCode);
918 }
919 
920 // reference implementation of UnicodeString::caseCompare
921 static int32_t
ref_case_compare(const UnicodeString & s1,const UnicodeString & s2,uint32_t options)922 ref_case_compare(const UnicodeString &s1, const UnicodeString &s2, uint32_t options) {
923     UnicodeString t1, t2;
924 
925     t1=s1;
926     t2=s2;
927 
928     t1.foldCase(options);
929     t2.foldCase(options);
930 
931     if(options&U_COMPARE_CODE_POINT_ORDER) {
932         return t1.compareCodePointOrder(t2);
933     } else {
934         return t1.compare(t2);
935     }
936 }
937 
938 // reduce an integer to -1/0/1
939 static inline int32_t
_sign(int32_t value)940 _sign(int32_t value) {
941     if(value==0) {
942         return 0;
943     } else {
944         return (value>>31)|1;
945     }
946 }
947 
948 static const char *
_signString(int32_t value)949 _signString(int32_t value) {
950     if(value<0) {
951         return "<0";
952     } else if(value==0) {
953         return "=0";
954     } else /* value>0 */ {
955         return ">0";
956     }
957 }
958 
959 void
TestCompare()960 BasicNormalizerTest::TestCompare() {
961     // test Normalizer::compare and unorm_compare (thinly wrapped by the former)
962     // by comparing it with its semantic equivalent
963     // since we trust the pieces, this is sufficient
964 
965     // test each string with itself and each other
966     // each time with all options
967     static const char *const
968     strings[]={
969         // some cases from NormalizationTest.txt
970         // 0..3
971         "D\\u031B\\u0307\\u0323",
972         "\\u1E0C\\u031B\\u0307",
973         "D\\u031B\\u0323\\u0307",
974         "d\\u031B\\u0323\\u0307",
975 
976         // 4..6
977         "\\u00E4",
978         "a\\u0308",
979         "A\\u0308",
980 
981         // Angstrom sign = A ring
982         // 7..10
983         "\\u212B",
984         "\\u00C5",
985         "A\\u030A",
986         "a\\u030A",
987 
988         // 11.14
989         "a\\u059A\\u0316\\u302A\\u032Fb",
990         "a\\u302A\\u0316\\u032F\\u059Ab",
991         "a\\u302A\\u0316\\u032F\\u059Ab",
992         "A\\u059A\\u0316\\u302A\\u032Fb",
993 
994         // from ICU case folding tests
995         // 15..20
996         "A\\u00df\\u00b5\\ufb03\\U0001040c\\u0131",
997         "ass\\u03bcffi\\U00010434i",
998         "\\u0061\\u0042\\u0131\\u03a3\\u00df\\ufb03\\ud93f\\udfff",
999         "\\u0041\\u0062\\u0069\\u03c3\\u0073\\u0053\\u0046\\u0066\\u0049\\ud93f\\udfff",
1000         "\\u0041\\u0062\\u0131\\u03c3\\u0053\\u0073\\u0066\\u0046\\u0069\\ud93f\\udfff",
1001         "\\u0041\\u0062\\u0069\\u03c3\\u0073\\u0053\\u0046\\u0066\\u0049\\ud93f\\udffd",
1002 
1003         //     U+d800 U+10001   see implementation comment in unorm_cmpEquivFold
1004         // vs. U+10000          at bottom - code point order
1005         // 21..22
1006         "\\ud800\\ud800\\udc01",
1007         "\\ud800\\udc00",
1008 
1009         // other code point order tests from ustrtest.cpp
1010         // 23..31
1011         "\\u20ac\\ud801",
1012         "\\u20ac\\ud800\\udc00",
1013         "\\ud800",
1014         "\\ud800\\uff61",
1015         "\\udfff",
1016         "\\uff61\\udfff",
1017         "\\uff61\\ud800\\udc02",
1018         "\\ud800\\udc02",
1019         "\\ud84d\\udc56",
1020 
1021         // long strings, see cnormtst.c/TestNormCoverage()
1022         // equivalent if case-insensitive
1023         // 32..33
1024         "\\uAD8B\\uAD8B\\uAD8B\\uAD8B"
1025         "\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1026         "\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1027         "\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1028         "\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1029         "\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1030         "aaaaaaaaaaaaaaaaaazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
1031         "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
1032         "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
1033         "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
1034         "\\uAD8B\\uAD8B\\uAD8B\\uAD8B"
1035         "d\\u031B\\u0307\\u0323",
1036 
1037         "\\u1100\\u116f\\u11aa\\uAD8B\\uAD8B\\u1100\\u116f\\u11aa"
1038         "\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1039         "\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1040         "\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1041         "\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1042         "\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e\\U0001d15e\\U0001d15e\\U0001d15e"
1043         "aaaaaaaaaaAAAAAAAAZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
1044         "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
1045         "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
1046         "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
1047         "\\u1100\\u116f\\u11aa\\uAD8B\\uAD8B\\u1100\\u116f\\u11aa"
1048         "\\u1E0C\\u031B\\u0307",
1049 
1050         // some strings that may make a difference whether the compare function
1051         // case-folds or decomposes first
1052         // 34..41
1053         "\\u0360\\u0345\\u0334",
1054         "\\u0360\\u03b9\\u0334",
1055 
1056         "\\u0360\\u1f80\\u0334",
1057         "\\u0360\\u03b1\\u0313\\u03b9\\u0334",
1058 
1059         "\\u0360\\u1ffc\\u0334",
1060         "\\u0360\\u03c9\\u03b9\\u0334",
1061 
1062         "a\\u0360\\u0345\\u0360\\u0345b",
1063         "a\\u0345\\u0360\\u0345\\u0360b",
1064 
1065         // interesting cases for canonical caseless match with turkic i handling
1066         // 42..43
1067         "\\u00cc",
1068         "\\u0069\\u0300",
1069 
1070         // strings with post-Unicode 3.2 normalization or normalization corrections
1071         // 44..45
1072         "\\u00e4\\u193b\\U0002f868",
1073         "\\u0061\\u193b\\u0308\\u36fc",
1074 
1075         // empty string
1076         // 46
1077         ""
1078     };
1079 
1080     UnicodeString s[100]; // at least as many items as in strings[] !
1081 
1082     // all combinations of options
1083     // UNORM_INPUT_IS_FCD is set automatically if both input strings fulfill FCD conditions
1084     // set UNORM_UNICODE_3_2 in one additional combination
1085     static const struct {
1086         uint32_t options;
1087         const char *name;
1088     } opt[]={
1089         { 0, "default" },
1090         { U_COMPARE_CODE_POINT_ORDER, "c.p. order" },
1091         { U_COMPARE_IGNORE_CASE, "ignore case" },
1092         { U_COMPARE_CODE_POINT_ORDER|U_COMPARE_IGNORE_CASE, "c.p. order & ignore case" },
1093         { U_COMPARE_IGNORE_CASE|U_FOLD_CASE_EXCLUDE_SPECIAL_I, "ignore case & special i" },
1094         { U_COMPARE_CODE_POINT_ORDER|U_COMPARE_IGNORE_CASE|U_FOLD_CASE_EXCLUDE_SPECIAL_I, "c.p. order & ignore case & special i" },
1095         { UNORM_UNICODE_3_2<<UNORM_COMPARE_NORM_OPTIONS_SHIFT, "Unicode 3.2" }
1096     };
1097 
1098     int32_t i, j, k, count=UPRV_LENGTHOF(strings);
1099     int32_t result, refResult;
1100 
1101     UErrorCode errorCode;
1102 
1103     // create the UnicodeStrings
1104     for(i=0; i<count; ++i) {
1105         s[i]=UnicodeString(strings[i], "").unescape();
1106     }
1107 
1108     // test them each with each other
1109     for(i=0; i<count; ++i) {
1110         for(j=i; j<count; ++j) {
1111             for(k=0; k<UPRV_LENGTHOF(opt); ++k) {
1112                 // test Normalizer::compare
1113                 errorCode=U_ZERO_ERROR;
1114                 result=_norm_compare(s[i], s[j], opt[k].options, errorCode);
1115                 refResult=ref_norm_compare(s[i], s[j], opt[k].options, errorCode);
1116                 if(_sign(result)!=_sign(refResult)) {
1117                     errln("Normalizer::compare(%d, %d, %s)%s should be %s %s",
1118                         i, j, opt[k].name, _signString(result), _signString(refResult),
1119                         U_SUCCESS(errorCode) ? "" : u_errorName(errorCode));
1120                 }
1121 
1122                 // test UnicodeString::caseCompare - same internal implementation function
1123                 if(opt[k].options&U_COMPARE_IGNORE_CASE) {
1124                     errorCode=U_ZERO_ERROR;
1125                     result=s[i].caseCompare(s[j], opt[k].options);
1126                     refResult=ref_case_compare(s[i], s[j], opt[k].options);
1127                     if(_sign(result)!=_sign(refResult)) {
1128                         errln("UniStr::caseCompare(%d, %d, %s)%s should be %s %s",
1129                             i, j, opt[k].name, _signString(result), _signString(refResult),
1130                             U_SUCCESS(errorCode) ? "" : u_errorName(errorCode));
1131                     }
1132                 }
1133             }
1134         }
1135     }
1136 
1137     // test cases with i and I to make sure Turkic works
1138     static const char16_t iI[]={ 0x49, 0x69, 0x130, 0x131 };
1139     UnicodeSet iSet, set;
1140 
1141     UnicodeString s1, s2;
1142 
1143     const Normalizer2Impl *nfcImpl=Normalizer2Factory::getNFCImpl(errorCode);
1144     if(U_FAILURE(errorCode) || !nfcImpl->ensureCanonIterData(errorCode)) {
1145         dataerrln("Normalizer2Factory::getNFCImpl().ensureCanonIterData() failed: %s",
1146               u_errorName(errorCode));
1147         return;
1148     }
1149 
1150     // collect all sets into one for contiguous output
1151     for(i=0; i<UPRV_LENGTHOF(iI); ++i) {
1152         if(nfcImpl->getCanonStartSet(iI[i], iSet)) {
1153             set.addAll(iSet);
1154         }
1155     }
1156 
1157     // test all of these precomposed characters
1158     const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
1159     UnicodeSetIterator it(set);
1160     while(it.next() && !it.isString()) {
1161         UChar32 c=it.getCodepoint();
1162         if(!nfcNorm2->getDecomposition(c, s2)) {
1163             dataerrln("NFC.getDecomposition(i-composite U+%04lx) failed", static_cast<long>(c));
1164             return;
1165         }
1166 
1167         s1.setTo(c);
1168         for(k=0; k<UPRV_LENGTHOF(opt); ++k) {
1169             // test Normalizer::compare
1170             errorCode=U_ZERO_ERROR;
1171             result=_norm_compare(s1, s2, opt[k].options, errorCode);
1172             refResult=ref_norm_compare(s1, s2, opt[k].options, errorCode);
1173             if(_sign(result)!=_sign(refResult)) {
1174                 errln("Normalizer::compare(U+%04x with its NFD, %s)%s should be %s %s",
1175                     c, opt[k].name, _signString(result), _signString(refResult),
1176                     U_SUCCESS(errorCode) ? "" : u_errorName(errorCode));
1177             }
1178 
1179             // test UnicodeString::caseCompare - same internal implementation function
1180             if(opt[k].options&U_COMPARE_IGNORE_CASE) {
1181                 errorCode=U_ZERO_ERROR;
1182                 result=s1.caseCompare(s2, opt[k].options);
1183                 refResult=ref_case_compare(s1, s2, opt[k].options);
1184                 if(_sign(result)!=_sign(refResult)) {
1185                     errln("UniStr::caseCompare(U+%04x with its NFD, %s)%s should be %s %s",
1186                         c, opt[k].name, _signString(result), _signString(refResult),
1187                         U_SUCCESS(errorCode) ? "" : u_errorName(errorCode));
1188                 }
1189             }
1190         }
1191     }
1192 
1193     // test getDecomposition() for some characters that do not decompose
1194     if( nfcNorm2->getDecomposition(0x20, s2) ||
1195         nfcNorm2->getDecomposition(0x4e00, s2) ||
1196         nfcNorm2->getDecomposition(0x20002, s2)
1197     ) {
1198         errln("NFC.getDecomposition() returns true for characters which do not have decompositions");
1199     }
1200 
1201     // test getRawDecomposition() for some characters that do not decompose
1202     if( nfcNorm2->getRawDecomposition(0x20, s2) ||
1203         nfcNorm2->getRawDecomposition(0x4e00, s2) ||
1204         nfcNorm2->getRawDecomposition(0x20002, s2)
1205     ) {
1206         errln("NFC.getRawDecomposition() returns true for characters which do not have decompositions");
1207     }
1208 
1209     // test composePair() for some pairs of characters that do not compose
1210     if( nfcNorm2->composePair(0x20, 0x301)>=0 ||
1211         nfcNorm2->composePair(0x61, 0x305)>=0 ||
1212         nfcNorm2->composePair(0x1100, 0x1160)>=0 ||
1213         nfcNorm2->composePair(0xac00, 0x11a7)>=0 ||
1214         nfcNorm2->composePair(0x1100, 0x80000020)>= 0 ||  // ICU-22635
1215         nfcNorm2->composePair(0xac00, 0x80000020)>= 0     // ICU-22635
1216     ) {
1217         errln("NFC.composePair() incorrectly composes some pairs of characters");
1218     }
1219 
1220     // test FilteredNormalizer2::getDecomposition()
1221     UnicodeSet filter(UNICODE_STRING_SIMPLE("[^\\u00a0-\\u00ff]"), errorCode);
1222     FilteredNormalizer2 fn2(*nfcNorm2, filter);
1223     if( fn2.getDecomposition(0xe4, s1) || !fn2.getDecomposition(0x100, s2) ||
1224         s2.length()!=2 || s2[0]!=0x41 || s2[1]!=0x304
1225     ) {
1226         errln("FilteredNormalizer2(NFC, ^A0-FF).getDecomposition() failed");
1227     }
1228 
1229     // test FilteredNormalizer2::getRawDecomposition()
1230     if( fn2.getRawDecomposition(0xe4, s1) || !fn2.getRawDecomposition(0x100, s2) ||
1231         s2.length()!=2 || s2[0]!=0x41 || s2[1]!=0x304
1232     ) {
1233         errln("FilteredNormalizer2(NFC, ^A0-FF).getRawDecomposition() failed");
1234     }
1235 
1236     // test FilteredNormalizer2::composePair()
1237     if( 0x100!=fn2.composePair(0x41, 0x304) ||
1238         fn2.composePair(0xc7, 0x301)>=0 // unfiltered result: U+1E08
1239     ) {
1240         errln("FilteredNormalizer2(NFC, ^A0-FF).composePair() failed");
1241     }
1242 }
1243 
1244 // verify that case-folding does not un-FCD strings
1245 int32_t
countFoldFCDExceptions(uint32_t foldingOptions)1246 BasicNormalizerTest::countFoldFCDExceptions(uint32_t foldingOptions) {
1247     UnicodeString s, fold, d;
1248     UChar32 c;
1249     int32_t count;
1250     uint8_t cc, trailCC, foldCC, foldTrailCC;
1251     UNormalizationCheckResult qcResult;
1252     int8_t category;
1253     UBool isNFD;
1254     UErrorCode errorCode;
1255 
1256     logln("Test if case folding may un-FCD a string (folding options %04lx)", foldingOptions);
1257 
1258     count=0;
1259     for(c=0; c<=0x10ffff; ++c) {
1260         errorCode = U_ZERO_ERROR;
1261         category=u_charType(c);
1262         if(category==U_UNASSIGNED) {
1263             continue; // skip unassigned code points
1264         }
1265         if(c==0xac00) {
1266             c=0xd7a3; // skip Hangul - no case folding there
1267             continue;
1268         }
1269         // skip Han blocks - no case folding there either
1270         if(c==0x3400) {
1271             c=0x4db5;
1272             continue;
1273         }
1274         if(c==0x4e00) {
1275             c=0x9fa5;
1276             continue;
1277         }
1278         if(c==0x20000) {
1279             c=0x2a6d6;
1280             continue;
1281         }
1282 
1283         s.setTo(c);
1284 
1285         // get leading and trailing cc for c
1286         Normalizer::decompose(s, false, 0, d, errorCode);
1287         isNFD= s==d;
1288         cc=u_getCombiningClass(d.char32At(0));
1289         trailCC=u_getCombiningClass(d.char32At(d.length()-1));
1290 
1291         // get leading and trailing cc for the case-folding of c
1292         s.foldCase(foldingOptions);
1293         Normalizer::decompose(s, false, 0, d, errorCode);
1294         foldCC=u_getCombiningClass(d.char32At(0));
1295         foldTrailCC=u_getCombiningClass(d.char32At(d.length()-1));
1296 
1297         qcResult=Normalizer::quickCheck(s, UNORM_FCD, errorCode);
1298 
1299         if (U_FAILURE(errorCode)) {
1300             ++count;
1301             dataerrln("U+%04lx: Failed with error %s", u_errorName(errorCode));
1302         }
1303 
1304         // bad:
1305         // - character maps to empty string: adjacent characters may then need reordering
1306         // - folding has different leading/trailing cc's, and they don't become just 0
1307         // - folding itself is not FCD
1308         if( qcResult!=UNORM_YES ||
1309             s.isEmpty() ||
1310             (cc!=foldCC && foldCC!=0) || (trailCC!=foldTrailCC && foldTrailCC!=0)
1311         ) {
1312             ++count;
1313             dataerrln("U+%04lx: case-folding may un-FCD a string (folding options %04lx)", c, foldingOptions);
1314             dataerrln("  cc %02x trailCC %02x    foldCC(U+%04lx) %02x foldTrailCC(U+%04lx) %02x   quickCheck(folded)=%d", cc, trailCC, d.char32At(0), foldCC, d.char32At(d.length()-1), foldTrailCC, qcResult);
1315             continue;
1316         }
1317 
1318         // also bad:
1319         // if a code point is in NFD but its case folding is not, then
1320         // unorm_compare will also fail
1321         if(isNFD && UNORM_YES!=Normalizer::quickCheck(s, UNORM_NFD, errorCode)) {
1322             ++count;
1323             errln("U+%04lx: case-folding un-NFDs this character (folding options %04lx)", c, foldingOptions);
1324         }
1325     }
1326 
1327     logln("There are %ld code points for which case-folding may un-FCD a string (folding options %04lx)", count, foldingOptions);
1328     return count;
1329 }
1330 
1331 void
FindFoldFCDExceptions()1332 BasicNormalizerTest::FindFoldFCDExceptions() {
1333     int32_t count;
1334 
1335     count=countFoldFCDExceptions(0);
1336     count+=countFoldFCDExceptions(U_FOLD_CASE_EXCLUDE_SPECIAL_I);
1337     if(count>0) {
1338         /*
1339          * If case-folding un-FCDs any strings, then unorm_compare() must be
1340          * re-implemented.
1341          * It currently assumes that one can check for FCD then case-fold
1342          * and then still have FCD strings for raw decomposition without reordering.
1343          */
1344         dataerrln("error: There are %ld code points for which case-folding may un-FCD a string for all folding options.\n"
1345               "See comment in BasicNormalizerTest::FindFoldFCDExceptions()!", count);
1346     }
1347 }
1348 
1349 static void
initExpectedSkippables(UnicodeSet skipSets[UNORM_MODE_COUNT],UErrorCode & errorCode)1350 initExpectedSkippables(UnicodeSet skipSets[UNORM_MODE_COUNT], UErrorCode &errorCode) {
1351     skipSets[UNORM_NFD].applyPattern(
1352         UNICODE_STRING_SIMPLE("[[:NFD_QC=Yes:]&[:ccc=0:]]"), errorCode);
1353     skipSets[UNORM_NFC].applyPattern(
1354         UNICODE_STRING_SIMPLE("[[:NFC_QC=Yes:]&[:ccc=0:]-[:HST=LV:]]"), errorCode);
1355     skipSets[UNORM_NFKD].applyPattern(
1356         UNICODE_STRING_SIMPLE("[[:NFKD_QC=Yes:]&[:ccc=0:]]"), errorCode);
1357     skipSets[UNORM_NFKC].applyPattern(
1358         UNICODE_STRING_SIMPLE("[[:NFKC_QC=Yes:]&[:ccc=0:]-[:HST=LV:]]"), errorCode);
1359 
1360     // Remove from the NFC and NFKC sets all those characters that change
1361     // when a back-combining character is added.
1362     // First, get all of the back-combining characters and their combining classes.
1363     UnicodeSet combineBack("[:NFC_QC=Maybe:]", errorCode);
1364     int32_t numCombineBack=combineBack.size();
1365     int32_t *combineBackCharsAndCc=new int32_t[numCombineBack*2];
1366     UnicodeSetIterator iter(combineBack);
1367     for(int32_t i=0; i<numCombineBack; ++i) {
1368         iter.next();
1369         UChar32 c=iter.getCodepoint();
1370         combineBackCharsAndCc[2*i]=c;
1371         combineBackCharsAndCc[2*i+1]=u_getCombiningClass(c);
1372     }
1373 
1374     // We need not look at control codes, Han characters nor Hangul LVT syllables because they
1375     // do not combine forward. LV syllables are already removed.
1376     UnicodeSet notInteresting("[[:C:][:Unified_Ideograph:][:HST=LVT:]]", errorCode);
1377     LocalPointer<UnicodeSet> unsure(&(skipSets[UNORM_NFC].clone())->removeAll(notInteresting));
1378     // System.out.format("unsure.size()=%d\n", unsure.size());
1379 
1380     // For each character about which we are unsure, see if it changes when we add
1381     // one of the back-combining characters.
1382     const Normalizer2 *norm2=Normalizer2::getNFCInstance(errorCode);
1383     UnicodeString s;
1384     iter.reset(*unsure);
1385     while(iter.next()) {
1386         UChar32 c=iter.getCodepoint();
1387         s.setTo(c);
1388         int32_t cLength=s.length();
1389         int32_t tccc=u_getIntPropertyValue(c, UCHAR_TRAIL_CANONICAL_COMBINING_CLASS);
1390         for(int32_t i=0; i<numCombineBack; ++i) {
1391             // If c's decomposition ends with a character with non-zero combining class, then
1392             // c can only change if it combines with a character with a non-zero combining class.
1393             int32_t cc2=combineBackCharsAndCc[2*i+1];
1394             if(tccc==0 || cc2!=0) {
1395                 UChar32 c2=combineBackCharsAndCc[2*i];
1396                 s.append(c2);
1397                 if(!norm2->isNormalized(s, errorCode)) {
1398                     // System.out.format("remove U+%04x (tccc=%d) + U+%04x (cc=%d)\n", c, tccc, c2, cc2);
1399                     skipSets[UNORM_NFC].remove(c);
1400                     skipSets[UNORM_NFKC].remove(c);
1401                     break;
1402                 }
1403                 s.truncate(cLength);
1404             }
1405         }
1406     }
1407     delete [] combineBackCharsAndCc;
1408 }
1409 
1410 static const char *const kModeStrings[UNORM_MODE_COUNT] = {
1411     "?", "none", "D", "KD", "C", "KC", "FCD"
1412 };
1413 
1414 void
TestSkippable()1415 BasicNormalizerTest::TestSkippable() {
1416     UnicodeSet diff, skipSets[UNORM_MODE_COUNT], expectSets[UNORM_MODE_COUNT];
1417     UnicodeString s, pattern;
1418 
1419     /* build NF*Skippable sets from runtime data */
1420     IcuTestErrorCode errorCode(*this, "TestSkippable");
1421     skipSets[UNORM_NFD].applyPattern(UNICODE_STRING_SIMPLE("[:NFD_Inert:]"), errorCode);
1422     skipSets[UNORM_NFKD].applyPattern(UNICODE_STRING_SIMPLE("[:NFKD_Inert:]"), errorCode);
1423     skipSets[UNORM_NFC].applyPattern(UNICODE_STRING_SIMPLE("[:NFC_Inert:]"), errorCode);
1424     skipSets[UNORM_NFKC].applyPattern(UNICODE_STRING_SIMPLE("[:NFKC_Inert:]"), errorCode);
1425     if(errorCode.errDataIfFailureAndReset("UnicodeSet(NF..._Inert) failed")) {
1426         return;
1427     }
1428 
1429     /* get expected sets from hardcoded patterns */
1430     initExpectedSkippables(expectSets, errorCode);
1431     errorCode.assertSuccess();
1432 
1433     for(int32_t i=UNORM_NONE; i<UNORM_MODE_COUNT; ++i) {
1434         if(skipSets[i]!=expectSets[i]) {
1435             const char *ms=kModeStrings[i];
1436             errln("error: TestSkippable skipSets[%s]!=expectedSets[%s]\n", ms, ms);
1437             // Note: This used to depend on hardcoded UnicodeSet patterns generated by
1438             // Mark's unicodetools.com.ibm.text.UCD.NFSkippable, by
1439             // running com.ibm.text.UCD.Main with the option NFSkippable.
1440             // Since ICU 4.6/Unicode 6, we are generating the
1441             // expectSets ourselves in initSkippables().
1442 
1443             s=UNICODE_STRING_SIMPLE("skip-expect=");
1444             (diff=skipSets[i]).removeAll(expectSets[i]).toPattern(pattern, true);
1445             s.append(pattern);
1446 
1447             pattern.remove();
1448             s.append(UNICODE_STRING_SIMPLE("\n\nexpect-skip="));
1449             (diff=expectSets[i]).removeAll(skipSets[i]).toPattern(pattern, true);
1450             s.append(pattern);
1451             s.append(UNICODE_STRING_SIMPLE("\n\n"));
1452 
1453             errln(s);
1454         }
1455     }
1456 }
1457 
1458 struct StringPair { const char *input, *expected; };
1459 
1460 void
TestCustomComp()1461 BasicNormalizerTest::TestCustomComp() {
1462     static const StringPair pairs[]={
1463         // ICU 63 normalization with UCPTrie requires inert surrogate code points.
1464         // { "\\uD801\\uE000\\uDFFE", "" },
1465         // { "\\uD800\\uD801\\uE000\\uDFFE\\uDFFF", "\\uD7FF\\uFFFF" },
1466         // { "\\uD800\\uD801\\uDFFE\\uDFFF", "\\uD7FF\\U000107FE\\uFFFF" },
1467         { "\\uD801\\uE000\\uDFFE", "\\uD801\\uDFFE" },
1468         { "\\uD800\\uD801\\uE000\\uDFFE\\uDFFF", "\\uD800\\uD801\\uDFFE\\uDFFF" },
1469         { "\\uD800\\uD801\\uDFFE\\uDFFF", "\\uD800\\U000107FE\\uDFFF" },
1470 
1471         { "\\uE001\\U000110B9\\u0345\\u0308\\u0327", "\\uE002\\U000110B9\\u0327\\u0345" },
1472         { "\\uE010\\U000F0011\\uE012", "\\uE011\\uE012" },
1473         { "\\uE010\\U000F0011\\U000F0011\\uE012", "\\uE011\\U000F0010" },
1474         { "\\uE111\\u1161\\uE112\\u1162", "\\uAE4C\\u1102\\u0062\\u1162" },
1475         { "\\uFFF3\\uFFF7\\U00010036\\U00010077", "\\U00010037\\U00010037\\uFFF6\\U00010037" }
1476     };
1477     IcuTestErrorCode errorCode(*this, "BasicNormalizerTest/TestCustomComp");
1478     const Normalizer2 *customNorm2=
1479         Normalizer2::getInstance(loadTestData(errorCode), "testnorm",
1480                                  UNORM2_COMPOSE, errorCode);
1481     if(errorCode.errDataIfFailureAndReset("unable to load testdata/testnorm.nrm")) {
1482         return;
1483     }
1484     for(int32_t i=0; i<UPRV_LENGTHOF(pairs); ++i) {
1485         const StringPair &pair=pairs[i];
1486         UnicodeString input=UnicodeString(pair.input, -1, US_INV).unescape();
1487         UnicodeString expected=UnicodeString(pair.expected, -1, US_INV).unescape();
1488         UnicodeString result=customNorm2->normalize(input, errorCode);
1489         if(result!=expected) {
1490             errln("custom compose Normalizer2 did not normalize input %d as expected", i);
1491         }
1492     }
1493 }
1494 
1495 void
TestCustomFCC()1496 BasicNormalizerTest::TestCustomFCC() {
1497     static const StringPair pairs[]={
1498         // ICU 63 normalization with UCPTrie requires inert surrogate code points.
1499         // { "\\uD801\\uE000\\uDFFE", "" },
1500         // { "\\uD800\\uD801\\uE000\\uDFFE\\uDFFF", "\\uD7FF\\uFFFF" },
1501         // { "\\uD800\\uD801\\uDFFE\\uDFFF", "\\uD7FF\\U000107FE\\uFFFF" },
1502         { "\\uD801\\uE000\\uDFFE", "\\uD801\\uDFFE" },
1503         { "\\uD800\\uD801\\uE000\\uDFFE\\uDFFF", "\\uD800\\uD801\\uDFFE\\uDFFF" },
1504         { "\\uD800\\uD801\\uDFFE\\uDFFF", "\\uD800\\U000107FE\\uDFFF" },
1505 
1506         // The following expected result is different from CustomComp
1507         // because of only-contiguous composition.
1508         { "\\uE001\\U000110B9\\u0345\\u0308\\u0327", "\\uE001\\U000110B9\\u0327\\u0308\\u0345" },
1509         { "\\uE010\\U000F0011\\uE012", "\\uE011\\uE012" },
1510         { "\\uE010\\U000F0011\\U000F0011\\uE012", "\\uE011\\U000F0010" },
1511         { "\\uE111\\u1161\\uE112\\u1162", "\\uAE4C\\u1102\\u0062\\u1162" },
1512         { "\\uFFF3\\uFFF7\\U00010036\\U00010077", "\\U00010037\\U00010037\\uFFF6\\U00010037" }
1513     };
1514     IcuTestErrorCode errorCode(*this, "BasicNormalizerTest/TestCustomFCC");
1515     const Normalizer2 *customNorm2=
1516         Normalizer2::getInstance(loadTestData(errorCode), "testnorm",
1517                                  UNORM2_COMPOSE_CONTIGUOUS, errorCode);
1518     if(errorCode.errDataIfFailureAndReset("unable to load testdata/testnorm.nrm")) {
1519         return;
1520     }
1521     for(int32_t i=0; i<UPRV_LENGTHOF(pairs); ++i) {
1522         const StringPair &pair=pairs[i];
1523         UnicodeString input=UnicodeString(pair.input, -1, US_INV).unescape();
1524         UnicodeString expected=UnicodeString(pair.expected, -1, US_INV).unescape();
1525         UnicodeString result=customNorm2->normalize(input, errorCode);
1526         if(result!=expected) {
1527             errln("custom FCC Normalizer2 did not normalize input %d as expected", i);
1528         }
1529     }
1530 }
1531 
1532 /* Improve code coverage of Normalizer2 */
1533 void
TestFilteredNormalizer2Coverage()1534 BasicNormalizerTest::TestFilteredNormalizer2Coverage() {
1535     UErrorCode errorCode = U_ZERO_ERROR;
1536     const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
1537     if (U_FAILURE(errorCode)) {
1538         dataerrln("Normalizer2::getNFCInstance() call failed - %s", u_errorName(errorCode));
1539         return;
1540     }
1541     UnicodeSet filter(UNICODE_STRING_SIMPLE("[^\\u00a0-\\u00ff\\u0310-\\u031f]"), errorCode);
1542     FilteredNormalizer2 fn2(*nfcNorm2, filter);
1543 
1544     UChar32 char32 = 0x0054;
1545 
1546     if (fn2.isInert(char32)) {
1547         errln("FilteredNormalizer2.isInert() failed.");
1548     }
1549 
1550     if (fn2.hasBoundaryAfter(char32)) {
1551         errln("FilteredNormalizer2.hasBoundaryAfter() failed.");
1552     }
1553 
1554     UChar32 c;
1555     for(c=0; c<=0x3ff; ++c) {
1556         uint8_t expectedCC= filter.contains(c) ? nfcNorm2->getCombiningClass(c) : 0;
1557         uint8_t cc=fn2.getCombiningClass(c);
1558         if(cc!=expectedCC) {
1559             errln(
1560                 UnicodeString("FilteredNormalizer2(NFC, ^A0-FF,310-31F).getCombiningClass(U+")+
1561                 hex(c)+
1562                 ")==filtered NFC.getCC()");
1563         }
1564     }
1565 
1566     UnicodeString newString1 = UNICODE_STRING_SIMPLE("[^\\u0100-\\u01ff]");
1567     UnicodeString newString2 = UNICODE_STRING_SIMPLE("[^\\u0200-\\u02ff]");
1568     fn2.append(newString1, newString2, errorCode);
1569     if (U_FAILURE(errorCode)) {
1570         errln("FilteredNormalizer2.append() failed.");
1571     }
1572 }
1573 
1574 void
TestComposeUTF8WithEdits()1575 BasicNormalizerTest::TestComposeUTF8WithEdits() {
1576     IcuTestErrorCode errorCode(*this, "TestComposeUTF8WithEdits");
1577     const Normalizer2 *nfkc_cf=Normalizer2::getNFKCCasefoldInstance(errorCode);
1578     if(errorCode.errDataIfFailureAndReset("Normalizer2::getNFKCCasefoldInstance() call failed")) {
1579         return;
1580     }
1581     static const StringPiece src =
1582         u8"  AÄA\u0308A\u0308\u00ad\u0323Ä\u0323,\u00ad\u1100\u1161가\u11A8가\u3133  ";
1583     StringPiece expected = u8"  aääạ\u0308ạ\u0308,가각갃  ";
1584     std::string result;
1585     StringByteSink<std::string> sink(&result, static_cast<int32_t>(expected.length()));
1586     Edits edits;
1587     nfkc_cf->normalizeUTF8(0, src, sink, &edits, errorCode);
1588     assertSuccess("normalizeUTF8 with Edits", errorCode.get());
1589     assertEquals("normalizeUTF8 with Edits", expected.data(), result.c_str());
1590     static const EditChange expectedChanges[] = {
1591         { false, 2, 2 },  // 2 spaces
1592         { true, 1, 1 },  // A→a
1593         { true, 2, 2 },  // Ä→ä
1594         { true, 3, 2 },  // A\u0308→ä
1595         { true, 7, 5 },  // A\u0308\u00ad\u0323→ạ\u0308 removes the soft hyphen
1596         { true, 4, 5 },  // Ä\u0323→ạ\u0308
1597         { false, 1, 1 },  // comma
1598         { true, 2, 0 },  // U+00AD soft hyphen maps to empty
1599         { true, 6, 3 },  // \u1100\u1161→가
1600         { true, 6, 3 },  // 가\u11A8→각
1601         { true, 6, 3 },  // 가\u3133→갃
1602         { false, 2, 2 }  // 2 spaces
1603     };
1604     assertTrue("normalizeUTF8 with Edits hasChanges", edits.hasChanges());
1605     assertEquals("normalizeUTF8 with Edits numberOfChanges", 9, edits.numberOfChanges());
1606     TestUtility::checkEditsIter(*this, u"normalizeUTF8 with Edits",
1607             edits.getFineIterator(), edits.getFineIterator(),
1608             expectedChanges, UPRV_LENGTHOF(expectedChanges),
1609             true, errorCode);
1610 
1611     assertFalse("isNormalizedUTF8(source)", nfkc_cf->isNormalizedUTF8(src, errorCode));
1612     assertTrue("isNormalizedUTF8(normalized)", nfkc_cf->isNormalizedUTF8(result, errorCode));
1613 
1614     // Omit unchanged text.
1615     expected = u8"aääạ\u0308ạ\u0308가각갃";
1616     result.clear();
1617     edits.reset();
1618     nfkc_cf->normalizeUTF8(U_OMIT_UNCHANGED_TEXT, src, sink, &edits, errorCode);
1619     assertSuccess("normalizeUTF8 omit unchanged", errorCode.get());
1620     assertEquals("normalizeUTF8 omit unchanged", expected.data(), result.c_str());
1621     assertTrue("normalizeUTF8 omit unchanged hasChanges", edits.hasChanges());
1622     assertEquals("normalizeUTF8 omit unchanged numberOfChanges", 9, edits.numberOfChanges());
1623     TestUtility::checkEditsIter(*this, u"normalizeUTF8 omit unchanged",
1624             edits.getFineIterator(), edits.getFineIterator(),
1625             expectedChanges, UPRV_LENGTHOF(expectedChanges),
1626             true, errorCode);
1627 
1628     // With filter: The normalization code does not see the "A" substrings.
1629     UnicodeSet filter(u"[^A]", errorCode);
1630     FilteredNormalizer2 fn2(*nfkc_cf, filter);
1631     expected = u8"  AäA\u0308A\u0323\u0308ạ\u0308,가각갃  ";
1632     result.clear();
1633     edits.reset();
1634     fn2.normalizeUTF8(0, src, sink, &edits, errorCode);
1635     assertSuccess("filtered normalizeUTF8", errorCode.get());
1636     assertEquals("filtered normalizeUTF8", expected.data(), result.c_str());
1637     static const EditChange filteredChanges[] = {
1638         { false, 3, 3 },  // 2 spaces + A
1639         { true, 2, 2 },  // Ä→ä
1640         { false, 4, 4 },  // A\u0308A
1641         { true, 6, 4 },  // \u0308\u00ad\u0323→\u0323\u0308 removes the soft hyphen
1642         { true, 4, 5 },  // Ä\u0323→ạ\u0308
1643         { false, 1, 1 },  // comma
1644         { true, 2, 0 },  // U+00AD soft hyphen maps to empty
1645         { true, 6, 3 },  // \u1100\u1161→가
1646         { true, 6, 3 },  // 가\u11A8→각
1647         { true, 6, 3 },  // 가\u3133→갃
1648         { false, 2, 2 }  // 2 spaces
1649     };
1650     assertTrue("filtered normalizeUTF8 hasChanges", edits.hasChanges());
1651     assertEquals("filtered normalizeUTF8 numberOfChanges", 7, edits.numberOfChanges());
1652     TestUtility::checkEditsIter(*this, u"filtered normalizeUTF8",
1653             edits.getFineIterator(), edits.getFineIterator(),
1654             filteredChanges, UPRV_LENGTHOF(filteredChanges),
1655             true, errorCode);
1656 
1657     assertFalse("filtered isNormalizedUTF8(source)", fn2.isNormalizedUTF8(src, errorCode));
1658     assertTrue("filtered isNormalizedUTF8(normalized)", fn2.isNormalizedUTF8(result, errorCode));
1659 
1660     // Omit unchanged text.
1661     // Note that the result is not normalized because the inner normalizer
1662     // does not see text across filter spans.
1663     expected = u8"ä\u0323\u0308ạ\u0308가각갃";
1664     result.clear();
1665     edits.reset();
1666     fn2.normalizeUTF8(U_OMIT_UNCHANGED_TEXT, src, sink, &edits, errorCode);
1667     assertSuccess("filtered normalizeUTF8 omit unchanged", errorCode.get());
1668     assertEquals("filtered normalizeUTF8 omit unchanged", expected.data(), result.c_str());
1669     assertTrue("filtered normalizeUTF8 omit unchanged hasChanges", edits.hasChanges());
1670     assertEquals("filtered normalizeUTF8 omit unchanged numberOfChanges", 7, edits.numberOfChanges());
1671     TestUtility::checkEditsIter(*this, u"filtered normalizeUTF8 omit unchanged",
1672             edits.getFineIterator(), edits.getFineIterator(),
1673             filteredChanges, UPRV_LENGTHOF(filteredChanges),
1674             true, errorCode);
1675 }
1676 
1677 void
TestDecomposeUTF8WithEdits()1678 BasicNormalizerTest::TestDecomposeUTF8WithEdits() {
1679     IcuTestErrorCode errorCode(*this, "TestDecomposeUTF8WithEdits");
1680     const Normalizer2 *nfkd_cf =
1681         Normalizer2::getInstance(nullptr, "nfkc_cf", UNORM2_DECOMPOSE, errorCode);
1682     if(errorCode.errDataIfFailureAndReset("Normalizer2::getInstance(nfkc_cf/decompose) call failed")) {
1683         return;
1684     }
1685     static const StringPiece src =
1686         u8"  AÄA\u0308A\u0308\u00ad\u0323Ä\u0323,\u00ad\u1100\u1161가\u11A8가\u3133  ";
1687     StringPiece expected =
1688         u8"  aa\u0308a\u0308a\u0323\u0308a\u0323\u0308,"
1689         u8"\u1100\u1161\u1100\u1161\u11A8\u1100\u1161\u11AA  ";
1690     std::string result;
1691     StringByteSink<std::string> sink(&result, static_cast<int32_t>(expected.length()));
1692     Edits edits;
1693     nfkd_cf->normalizeUTF8(0, src, sink, &edits, errorCode);
1694     assertSuccess("normalizeUTF8 with Edits", errorCode.get());
1695     assertEquals("normalizeUTF8 with Edits", expected.data(), result.c_str());
1696     static const EditChange expectedChanges[] = {
1697         { false, 2, 2 },  // 2 spaces
1698         { true, 1, 1 },  // A→a
1699         { true, 2, 3 },  // Ä→a\u0308
1700         { true, 1, 1 },  // A→a
1701         { false, 2, 2 },  // \u0308→\u0308 unchanged
1702         { true, 1, 1 },  // A→a
1703         { true, 6, 4 },  // \u0308\u00ad\u0323→\u0323\u0308 removes the soft hyphen
1704         { true, 4, 5 },  // Ä\u0323→a\u0323\u0308
1705         { false, 1, 1 },  // comma
1706         { true, 2, 0 },  // U+00AD soft hyphen maps to empty
1707         { false, 6, 6 },  // \u1100\u1161 unchanged
1708         { true, 3, 6 },  // 가→\u1100\u1161
1709         { false, 3, 3 },  // \u11A8 unchanged
1710         { true, 3, 6 },  // 가→\u1100\u1161
1711         { true, 3, 3 },  // \u3133→\u11AA
1712         { false, 2, 2 }  // 2 spaces
1713     };
1714     assertTrue("normalizeUTF8 with Edits hasChanges", edits.hasChanges());
1715     assertEquals("normalizeUTF8 with Edits numberOfChanges", 10, edits.numberOfChanges());
1716     TestUtility::checkEditsIter(*this, u"normalizeUTF8 with Edits",
1717             edits.getFineIterator(), edits.getFineIterator(),
1718             expectedChanges, UPRV_LENGTHOF(expectedChanges),
1719             true, errorCode);
1720 
1721     assertFalse("isNormalizedUTF8(source)", nfkd_cf->isNormalizedUTF8(src, errorCode));
1722     assertTrue("isNormalizedUTF8(normalized)", nfkd_cf->isNormalizedUTF8(result, errorCode));
1723 
1724     // Omit unchanged text.
1725     expected = u8"aa\u0308aa\u0323\u0308a\u0323\u0308\u1100\u1161\u1100\u1161\u11AA";
1726     result.clear();
1727     edits.reset();
1728     nfkd_cf->normalizeUTF8(U_OMIT_UNCHANGED_TEXT, src, sink, &edits, errorCode);
1729     assertSuccess("normalizeUTF8 omit unchanged", errorCode.get());
1730     assertEquals("normalizeUTF8 omit unchanged", expected.data(), result.c_str());
1731     assertTrue("normalizeUTF8 omit unchanged hasChanges", edits.hasChanges());
1732     assertEquals("normalizeUTF8 omit unchanged numberOfChanges", 10, edits.numberOfChanges());
1733     TestUtility::checkEditsIter(*this, u"normalizeUTF8 omit unchanged",
1734             edits.getFineIterator(), edits.getFineIterator(),
1735             expectedChanges, UPRV_LENGTHOF(expectedChanges),
1736             true, errorCode);
1737 
1738     // Not testing FilteredNormalizer2:
1739     // The code there is the same for all normalization modes, and
1740     // TestComposeUTF8WithEdits() covers it well.
1741 }
1742 
1743 void
TestLowMappingToEmpty_D()1744 BasicNormalizerTest::TestLowMappingToEmpty_D() {
1745     IcuTestErrorCode errorCode(*this, "TestLowMappingToEmpty_D");
1746     const Normalizer2 *n2 = Normalizer2::getInstance(
1747         nullptr, "nfkc_cf", UNORM2_DECOMPOSE, errorCode);
1748     if (errorCode.errDataIfFailureAndReset("Normalizer2::getInstance() call failed")) {
1749         return;
1750     }
1751     checkLowMappingToEmpty(*n2);
1752 
1753     UnicodeString sh(u'\u00AD');
1754     assertFalse("soft hyphen is not normalized", n2->isNormalized(sh, errorCode));
1755     UnicodeString result = n2->normalize(sh, errorCode);
1756     assertTrue("soft hyphen normalizes to empty", result.isEmpty());
1757     assertEquals("soft hyphen QC=No", UNORM_NO, n2->quickCheck(sh, errorCode));
1758     assertEquals("soft hyphen spanQuickCheckYes", 0, n2->spanQuickCheckYes(sh, errorCode));
1759 
1760     UnicodeString s(u"\u00ADÄ\u00AD\u0323");
1761     result = n2->normalize(s, errorCode);
1762     assertEquals("normalize string with soft hyphens", u"a\u0323\u0308", result);
1763 }
1764 
1765 void
TestLowMappingToEmpty_FCD()1766 BasicNormalizerTest::TestLowMappingToEmpty_FCD() {
1767     IcuTestErrorCode errorCode(*this, "TestLowMappingToEmpty_FCD");
1768     const Normalizer2 *n2 = Normalizer2::getInstance(
1769         nullptr, "nfkc_cf", UNORM2_FCD, errorCode);
1770     if (errorCode.errDataIfFailureAndReset("Normalizer2::getInstance() call failed")) {
1771         return;
1772     }
1773     checkLowMappingToEmpty(*n2);
1774 
1775     UnicodeString sh(u'\u00AD');
1776     assertTrue("soft hyphen is FCD", n2->isNormalized(sh, errorCode));
1777 
1778     UnicodeString s(u"\u00ADÄ\u00AD\u0323");
1779     UnicodeString result = n2->normalize(s, errorCode);
1780     assertEquals("normalize string with soft hyphens", u"\u00ADa\u0323\u0308", result);
1781 }
1782 
1783 void
checkLowMappingToEmpty(const Normalizer2 & n2)1784 BasicNormalizerTest::checkLowMappingToEmpty(const Normalizer2 &n2) {
1785     UnicodeString mapping;
1786     assertTrue("getDecomposition(soft hyphen)", n2.getDecomposition(0xad, mapping));
1787     assertTrue("soft hyphen maps to empty", mapping.isEmpty());
1788     assertFalse("soft hyphen has no boundary before", n2.hasBoundaryBefore(0xad));
1789     assertFalse("soft hyphen has no boundary after", n2.hasBoundaryAfter(0xad));
1790     assertFalse("soft hyphen is not inert", n2.isInert(0xad));
1791 }
1792 
1793 void
TestNormalizeIllFormedText()1794 BasicNormalizerTest::TestNormalizeIllFormedText() {
1795     IcuTestErrorCode errorCode(*this, "TestNormalizeIllFormedText");
1796     const Normalizer2 *nfkc_cf = Normalizer2::getNFKCCasefoldInstance(errorCode);
1797     if(errorCode.errDataIfFailureAndReset("Normalizer2::getNFKCCasefoldInstance() call failed")) {
1798         return;
1799     }
1800     // Normalization behavior for ill-formed text is not defined.
1801     // ICU currently treats ill-formed sequences as normalization-inert
1802     // and copies them unchanged.
1803     UnicodeString src(u"  A");
1804     src.append(static_cast<char16_t>(0xD800)).append(u"ÄA\u0308").append(static_cast<char16_t>(0xD900)).
1805         append(u"A\u0308\u00ad\u0323").append(static_cast<char16_t>(0xDBFF)).
1806         append(u"Ä\u0323,\u00ad").append(static_cast<char16_t>(0xDC00)).
1807         append(u"\u1100\u1161가\u11A8가\u3133  ").append(static_cast<char16_t>(0xDFFF));
1808     UnicodeString expected(u"  a");
1809     expected.append(static_cast<char16_t>(0xD800)).append(u"ää").append(static_cast<char16_t>(0xD900)).
1810         append(u"ạ\u0308").append(static_cast<char16_t>(0xDBFF)).
1811         append(u"ạ\u0308,").append(static_cast<char16_t>(0xDC00)).
1812         append(u"가각갃  ").append(static_cast<char16_t>(0xDFFF));
1813     UnicodeString result = nfkc_cf->normalize(src, errorCode);
1814     assertSuccess("normalize", errorCode.get());
1815     assertEquals("normalize", expected, result);
1816 
1817     std::string src8(reinterpret_cast<const char*>(u8"  A"));
1818     src8.append("\x80").append(reinterpret_cast<const char*>(u8"ÄA\u0308")).append("\xC0\x80").
1819         append(reinterpret_cast<const char*>(u8"A\u0308\u00ad\u0323")).append("\xED\xA0\x80").
1820         append(reinterpret_cast<const char*>(u8"Ä\u0323,\u00ad")).append("\xF4\x90\x80\x80").
1821         append(reinterpret_cast<const char*>(u8"\u1100\u1161가\u11A8가\u3133  ")).append("\xF0");
1822     std::string expected8(reinterpret_cast<const char*>(u8"  a"));
1823     expected8.append("\x80").append(reinterpret_cast<const char*>(u8"ää")).append("\xC0\x80").
1824         append(reinterpret_cast<const char*>(u8"ạ\u0308")).append("\xED\xA0\x80").
1825         append(reinterpret_cast<const char*>(u8"ạ\u0308,")).append("\xF4\x90\x80\x80").
1826         append(reinterpret_cast<const char*>(u8"가각갃  ")).append("\xF0");
1827     std::string result8;
1828     StringByteSink<std::string> sink(&result8);
1829     nfkc_cf->normalizeUTF8(0, src8, sink, nullptr, errorCode);
1830     assertSuccess("normalizeUTF8", errorCode.get());
1831     assertEquals("normalizeUTF8", expected8.c_str(), result8.c_str());
1832 }
1833 
1834 void
TestComposeJamoTBase()1835 BasicNormalizerTest::TestComposeJamoTBase() {
1836     // Algorithmic composition of Hangul syllables must not combine with JAMO_T_BASE = U+11A7
1837     // which is not a conjoining Jamo Trailing consonant.
1838     IcuTestErrorCode errorCode(*this, "TestComposeJamoTBase");
1839     const Normalizer2 *nfkc = Normalizer2::getNFKCInstance(errorCode);
1840     if(errorCode.errDataIfFailureAndReset("Normalizer2::getNFKCInstance() call failed")) {
1841         return;
1842     }
1843     UnicodeString s(u"\u1100\u1161\u11A7\u1100\u314F\u11A7가\u11A7");
1844     UnicodeString expected(u"가\u11A7가\u11A7가\u11A7");
1845     UnicodeString result = nfkc->normalize(s, errorCode);
1846     assertSuccess("normalize(LV+11A7)", errorCode.get());
1847     assertEquals("normalize(LV+11A7)", expected, result);
1848     assertFalse("isNormalized(LV+11A7)", nfkc->isNormalized(s, errorCode));
1849     assertTrue("isNormalized(normalized)", nfkc->isNormalized(result, errorCode));
1850 
1851     StringPiece s8(u8"\u1100\u1161\u11A7\u1100\u314F\u11A7가\u11A7");
1852     StringPiece expected8(u8"가\u11A7가\u11A7가\u11A7");
1853     std::string result8;
1854     StringByteSink<std::string> sink(&result8, expected8.length());
1855     nfkc->normalizeUTF8(0, s8, sink, nullptr, errorCode);
1856     assertSuccess("normalizeUTF8(LV+11A7)", errorCode.get());
1857     assertEquals("normalizeUTF8(LV+11A7)", expected8.data(), result8.c_str());
1858     assertFalse("isNormalizedUTF8(LV+11A7)", nfkc->isNormalizedUTF8(s8, errorCode));
1859     assertTrue("isNormalizedUTF8(normalized)", nfkc->isNormalizedUTF8(result8, errorCode));
1860 }
1861 
1862 void
TestComposeBoundaryAfter()1863 BasicNormalizerTest::TestComposeBoundaryAfter() {
1864     IcuTestErrorCode errorCode(*this, "TestComposeBoundaryAfter");
1865     const Normalizer2 *nfkc = Normalizer2::getNFKCInstance(errorCode);
1866     if(errorCode.errDataIfFailureAndReset("Normalizer2::getNFKCInstance() call failed")) {
1867         return;
1868     }
1869     // U+02DA and U+FB2C do not have compose-boundaries-after.
1870     UnicodeString s(u"\u02DA\u0339 \uFB2C\u05B6");
1871     UnicodeString expected(u" \u0339\u030A \u05E9\u05B6\u05BC\u05C1");
1872     UnicodeString result = nfkc->normalize(s, errorCode);
1873     assertSuccess("nfkc", errorCode.get());
1874     assertEquals("nfkc", expected, result);
1875     assertFalse("U+02DA boundary-after", nfkc->hasBoundaryAfter(0x2DA));
1876     assertFalse("U+FB2C boundary-after", nfkc->hasBoundaryAfter(0xFB2C));
1877 }
1878 
1879 void
TestNFKC_SCF()1880 BasicNormalizerTest::TestNFKC_SCF() {
1881     IcuTestErrorCode errorCode(*this, "TestNFKC_SCF");
1882     const Normalizer2 *nfkc_scf = Normalizer2::getNFKCSimpleCasefoldInstance(errorCode);
1883     if(errorCode.errDataIfFailureAndReset(
1884             "Normalizer2::getNFKCSimpleCasefoldInstance() call failed")) {
1885         return;
1886     }
1887     // Uses only Simple_Casefolding mappings.
1888     UnicodeString s(u"aA\u0308 ßẞ \u1F80\u1F88");
1889     UnicodeString expected(u"aä ßß \u1F80\u1F80");
1890     UnicodeString result = nfkc_scf->normalize(s, errorCode);
1891     assertSuccess("nfkc_scf", errorCode.get());
1892     assertEquals("nfkc_scf", expected, result);
1893 }
1894 
1895 #endif /* #if !UCONFIG_NO_NORMALIZATION */
1896