]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cnormtst.c
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cnormtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CNORMTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Ported for C API
13 * synwee added test for quick check
14 * synwee added test for checkFCD
15 *********************************************************************************/
16 /*tests for u_normalization*/
17 #include "unicode/utypes.h"
18 #include "unicode/unorm.h"
19 #include "unicode/utf16.h"
20 #include "cintltst.h"
21
22 #if UCONFIG_NO_NORMALIZATION
23
24 void addNormTest(TestNode** root) {
25 /* no normalization - nothing to do */
26 }
27
28 #else
29
30 #include <stdlib.h>
31 #include <time.h>
32 #include "unicode/uchar.h"
33 #include "unicode/ustring.h"
34 #include "unicode/unorm.h"
35 #include "cnormtst.h"
36
37 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof ((array)[0]))
38
39 static void
40 TestAPI(void);
41
42 static void
43 TestNormCoverage(void);
44
45 static void
46 TestConcatenate(void);
47
48 static void
49 TestNextPrevious(void);
50
51 static void TestIsNormalized(void);
52
53 static void
54 TestFCNFKCClosure(void);
55
56 static void
57 TestQuickCheckPerCP(void);
58
59 static void
60 TestComposition(void);
61
62 static void
63 TestFCD(void);
64
65 static void
66 TestGetDecomposition(void);
67
68 static void
69 TestGetRawDecomposition(void);
70
71 static void TestAppendRestoreMiddle(void);
72 static void TestGetEasyToUseInstance(void);
73
74 static const char* const canonTests[][3] = {
75 /* Input*/ /*Decomposed*/ /*Composed*/
76 { "cat", "cat", "cat" },
77 { "\\u00e0ardvark", "a\\u0300ardvark", "\\u00e0ardvark", },
78
79 { "\\u1e0a", "D\\u0307", "\\u1e0a" }, /* D-dot_above*/
80 { "D\\u0307", "D\\u0307", "\\u1e0a" }, /* D dot_above*/
81
82 { "\\u1e0c\\u0307", "D\\u0323\\u0307", "\\u1e0c\\u0307" }, /* D-dot_below dot_above*/
83 { "\\u1e0a\\u0323", "D\\u0323\\u0307", "\\u1e0c\\u0307" }, /* D-dot_above dot_below */
84 { "D\\u0307\\u0323", "D\\u0323\\u0307", "\\u1e0c\\u0307" }, /* D dot_below dot_above */
85
86 { "\\u1e10\\u0307\\u0323", "D\\u0327\\u0323\\u0307", "\\u1e10\\u0323\\u0307" }, /*D dot_below cedilla dot_above*/
87 { "D\\u0307\\u0328\\u0323", "D\\u0328\\u0323\\u0307", "\\u1e0c\\u0328\\u0307" }, /* D dot_above ogonek dot_below*/
88
89 { "\\u1E14", "E\\u0304\\u0300", "\\u1E14" }, /* E-macron-grave*/
90 { "\\u0112\\u0300", "E\\u0304\\u0300", "\\u1E14" }, /* E-macron + grave*/
91 { "\\u00c8\\u0304", "E\\u0300\\u0304", "\\u00c8\\u0304" }, /* E-grave + macron*/
92
93 { "\\u212b", "A\\u030a", "\\u00c5" }, /* angstrom_sign*/
94 { "\\u00c5", "A\\u030a", "\\u00c5" }, /* A-ring*/
95
96 { "\\u00C4ffin", "A\\u0308ffin", "\\u00C4ffin" },
97 { "\\u00C4\\uFB03n", "A\\u0308\\uFB03n", "\\u00C4\\uFB03n" },
98
99 { "Henry IV", "Henry IV", "Henry IV" },
100 { "Henry \\u2163", "Henry \\u2163", "Henry \\u2163" },
101
102 { "\\u30AC", "\\u30AB\\u3099", "\\u30AC" }, /* ga (Katakana)*/
103 { "\\u30AB\\u3099", "\\u30AB\\u3099", "\\u30AC" }, /*ka + ten*/
104 { "\\uFF76\\uFF9E", "\\uFF76\\uFF9E", "\\uFF76\\uFF9E" }, /* hw_ka + hw_ten*/
105 { "\\u30AB\\uFF9E", "\\u30AB\\uFF9E", "\\u30AB\\uFF9E" }, /* ka + hw_ten*/
106 { "\\uFF76\\u3099", "\\uFF76\\u3099", "\\uFF76\\u3099" }, /* hw_ka + ten*/
107 { "A\\u0300\\u0316", "A\\u0316\\u0300", "\\u00C0\\u0316" }, /* hw_ka + ten*/
108 { "", "", "" }
109 };
110
111 static const char* const compatTests[][3] = {
112 /* Input*/ /*Decomposed */ /*Composed*/
113 { "cat", "cat", "cat" },
114
115 { "\\uFB4f", "\\u05D0\\u05DC", "\\u05D0\\u05DC" }, /* Alef-Lamed vs. Alef, Lamed*/
116
117 { "\\u00C4ffin", "A\\u0308ffin", "\\u00C4ffin" },
118 { "\\u00C4\\uFB03n", "A\\u0308ffin", "\\u00C4ffin" }, /* ffi ligature -> f + f + i*/
119
120 { "Henry IV", "Henry IV", "Henry IV" },
121 { "Henry \\u2163", "Henry IV", "Henry IV" },
122
123 { "\\u30AC", "\\u30AB\\u3099", "\\u30AC" }, /* ga (Katakana)*/
124 { "\\u30AB\\u3099", "\\u30AB\\u3099", "\\u30AC" }, /*ka + ten*/
125
126 { "\\uFF76\\u3099", "\\u30AB\\u3099", "\\u30AC" }, /* hw_ka + ten*/
127
128 /*These two are broken in Unicode 2.1.2 but fixed in 2.1.5 and later*/
129 { "\\uFF76\\uFF9E", "\\u30AB\\u3099", "\\u30AC" }, /* hw_ka + hw_ten*/
130 { "\\u30AB\\uFF9E", "\\u30AB\\u3099", "\\u30AC" }, /* ka + hw_ten*/
131 { "", "", "" }
132 };
133
134 static const char* const fcdTests[][3] = {
135 /* Added for testing the below-U+0300 prefix of a NUL-terminated string. */
136 { "\\u010e\\u0327", "D\\u0327\\u030c", NULL }, /* D-caron + cedilla */
137 { "\\u010e", "\\u010e", NULL } /* D-caron */
138 };
139
140 void addNormTest(TestNode** root);
141
142 void addNormTest(TestNode** root)
143 {
144 addTest(root, &TestAPI, "tsnorm/cnormtst/TestAPI");
145 addTest(root, &TestDecomp, "tsnorm/cnormtst/TestDecomp");
146 addTest(root, &TestCompatDecomp, "tsnorm/cnormtst/TestCompatDecomp");
147 addTest(root, &TestCanonDecompCompose, "tsnorm/cnormtst/TestCanonDecompCompose");
148 addTest(root, &TestCompatDecompCompose, "tsnorm/cnormtst/TestCompatDecompCompose");
149 addTest(root, &TestFCD, "tsnorm/cnormtst/TestFCD");
150 addTest(root, &TestNull, "tsnorm/cnormtst/TestNull");
151 addTest(root, &TestQuickCheck, "tsnorm/cnormtst/TestQuickCheck");
152 addTest(root, &TestQuickCheckPerCP, "tsnorm/cnormtst/TestQuickCheckPerCP");
153 addTest(root, &TestIsNormalized, "tsnorm/cnormtst/TestIsNormalized");
154 addTest(root, &TestCheckFCD, "tsnorm/cnormtst/TestCheckFCD");
155 addTest(root, &TestNormCoverage, "tsnorm/cnormtst/TestNormCoverage");
156 addTest(root, &TestConcatenate, "tsnorm/cnormtst/TestConcatenate");
157 addTest(root, &TestNextPrevious, "tsnorm/cnormtst/TestNextPrevious");
158 addTest(root, &TestFCNFKCClosure, "tsnorm/cnormtst/TestFCNFKCClosure");
159 addTest(root, &TestComposition, "tsnorm/cnormtst/TestComposition");
160 addTest(root, &TestGetDecomposition, "tsnorm/cnormtst/TestGetDecomposition");
161 addTest(root, &TestGetRawDecomposition, "tsnorm/cnormtst/TestGetRawDecomposition");
162 addTest(root, &TestAppendRestoreMiddle, "tsnorm/cnormtst/TestAppendRestoreMiddle");
163 addTest(root, &TestGetEasyToUseInstance, "tsnorm/cnormtst/TestGetEasyToUseInstance");
164 }
165
166 static const char* const modeStrings[]={
167 "UNORM_NONE",
168 "UNORM_NFD",
169 "UNORM_NFKD",
170 "UNORM_NFC",
171 "UNORM_NFKC",
172 "UNORM_FCD",
173 "UNORM_MODE_COUNT"
174 };
175
176 static void TestNormCases(UNormalizationMode mode,
177 const char* const cases[][3], int32_t lengthOfCases) {
178 int32_t x, neededLen, length2;
179 int32_t expIndex= (mode==UNORM_NFC || mode==UNORM_NFKC) ? 2 : 1;
180 UChar *source=NULL;
181 UChar result[16];
182 log_verbose("Testing unorm_normalize(%s)\n", modeStrings[mode]);
183 for(x=0; x < lengthOfCases; x++)
184 {
185 UErrorCode status = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
186 source=CharsToUChars(cases[x][0]);
187 neededLen= unorm_normalize(source, u_strlen(source), mode, 0, NULL, 0, &status);
188 length2= unorm_normalize(source, -1, mode, 0, NULL, 0, &status2);
189 if(neededLen!=length2) {
190 log_err("ERROR in unorm_normalize(%s)[%d]: "
191 "preflight length/NUL %d!=%d preflight length/srcLength\n",
192 modeStrings[mode], (int)x, (int)neededLen, (int)length2);
193 }
194 if(status==U_BUFFER_OVERFLOW_ERROR)
195 {
196 status=U_ZERO_ERROR;
197 }
198 length2=unorm_normalize(source, u_strlen(source), mode, 0, result, LENGTHOF(result), &status);
199 if(U_FAILURE(status) || neededLen!=length2) {
200 log_data_err("ERROR in unorm_normalize(%s/NUL) at %s: %s - (Are you missing data?)\n",
201 modeStrings[mode], austrdup(source), myErrorName(status));
202 } else {
203 assertEqual(result, cases[x][expIndex], x);
204 }
205 length2=unorm_normalize(source, -1, mode, 0, result, LENGTHOF(result), &status);
206 if(U_FAILURE(status) || neededLen!=length2) {
207 log_data_err("ERROR in unorm_normalize(%s/srcLength) at %s: %s - (Are you missing data?)\n",
208 modeStrings[mode], austrdup(source), myErrorName(status));
209 } else {
210 assertEqual(result, cases[x][expIndex], x);
211 }
212 free(source);
213 }
214 }
215
216 void TestDecomp() {
217 TestNormCases(UNORM_NFD, canonTests, LENGTHOF(canonTests));
218 }
219
220 void TestCompatDecomp() {
221 TestNormCases(UNORM_NFKD, compatTests, LENGTHOF(compatTests));
222 }
223
224 void TestCanonDecompCompose() {
225 TestNormCases(UNORM_NFC, canonTests, LENGTHOF(canonTests));
226 }
227
228 void TestCompatDecompCompose() {
229 TestNormCases(UNORM_NFKC, compatTests, LENGTHOF(compatTests));
230 }
231
232 void TestFCD() {
233 TestNormCases(UNORM_FCD, fcdTests, LENGTHOF(fcdTests));
234 }
235
236 static void assertEqual(const UChar* result, const char* expected, int32_t index)
237 {
238 UChar *expectedUni = CharsToUChars(expected);
239 if(u_strcmp(result, expectedUni)!=0){
240 log_err("ERROR in decomposition at index = %d. EXPECTED: %s , GOT: %s\n", index, expected,
241 austrdup(result) );
242 }
243 free(expectedUni);
244 }
245
246 static void TestNull_check(UChar *src, int32_t srcLen,
247 UChar *exp, int32_t expLen,
248 UNormalizationMode mode,
249 const char *name)
250 {
251 UErrorCode status = U_ZERO_ERROR;
252 int32_t len, i;
253
254 UChar result[50];
255
256
257 status = U_ZERO_ERROR;
258
259 for(i=0;i<50;i++)
260 {
261 result[i] = 0xFFFD;
262 }
263
264 len = unorm_normalize(src, srcLen, mode, 0, result, 50, &status);
265
266 if(U_FAILURE(status)) {
267 log_data_err("unorm_normalize(%s) with 0x0000 failed: %s - (Are you missing data?)\n", name, u_errorName(status));
268 } else if (len != expLen) {
269 log_err("unorm_normalize(%s) with 0x0000 failed: Expected len %d, got %d\n", name, expLen, len);
270 }
271
272 {
273 for(i=0;i<len;i++){
274 if(exp[i] != result[i]) {
275 log_err("unorm_normalize(%s): @%d, expected \\u%04X got \\u%04X\n",
276 name,
277 i,
278 exp[i],
279 result[i]);
280 return;
281 }
282 log_verbose(" %d: \\u%04X\n", i, result[i]);
283 }
284 }
285
286 log_verbose("unorm_normalize(%s) with 0x0000: OK\n", name);
287 }
288
289 void TestNull()
290 {
291
292 UChar source_comp[] = { 0x0061, 0x0000, 0x0044, 0x0307 };
293 int32_t source_comp_len = 4;
294 UChar expect_comp[] = { 0x0061, 0x0000, 0x1e0a };
295 int32_t expect_comp_len = 3;
296
297 UChar source_dcmp[] = { 0x1e0A, 0x0000, 0x0929 };
298 int32_t source_dcmp_len = 3;
299 UChar expect_dcmp[] = { 0x0044, 0x0307, 0x0000, 0x0928, 0x093C };
300 int32_t expect_dcmp_len = 5;
301
302 TestNull_check(source_comp,
303 source_comp_len,
304 expect_comp,
305 expect_comp_len,
306 UNORM_NFC,
307 "UNORM_NFC");
308
309 TestNull_check(source_dcmp,
310 source_dcmp_len,
311 expect_dcmp,
312 expect_dcmp_len,
313 UNORM_NFD,
314 "UNORM_NFD");
315
316 TestNull_check(source_comp,
317 source_comp_len,
318 expect_comp,
319 expect_comp_len,
320 UNORM_NFKC,
321 "UNORM_NFKC");
322
323
324 }
325
326 static void TestQuickCheckResultNO()
327 {
328 const UChar CPNFD[] = {0x00C5, 0x0407, 0x1E00, 0x1F57, 0x220C,
329 0x30AE, 0xAC00, 0xD7A3, 0xFB36, 0xFB4E};
330 const UChar CPNFC[] = {0x0340, 0x0F93, 0x1F77, 0x1FBB, 0x1FEB,
331 0x2000, 0x232A, 0xF900, 0xFA1E, 0xFB4E};
332 const UChar CPNFKD[] = {0x00A0, 0x02E4, 0x1FDB, 0x24EA, 0x32FE,
333 0xAC00, 0xFB4E, 0xFA10, 0xFF3F, 0xFA2D};
334 const UChar CPNFKC[] = {0x00A0, 0x017F, 0x2000, 0x24EA, 0x32FE,
335 0x33FE, 0xFB4E, 0xFA10, 0xFF3F, 0xFA2D};
336
337
338 const int SIZE = 10;
339
340 int count = 0;
341 UErrorCode error = U_ZERO_ERROR;
342
343 for (; count < SIZE; count ++)
344 {
345 if (unorm_quickCheck(&(CPNFD[count]), 1, UNORM_NFD, &error) !=
346 UNORM_NO)
347 {
348 log_err("ERROR in NFD quick check at U+%04x\n", CPNFD[count]);
349 return;
350 }
351 if (unorm_quickCheck(&(CPNFC[count]), 1, UNORM_NFC, &error) !=
352 UNORM_NO)
353 {
354 log_err("ERROR in NFC quick check at U+%04x\n", CPNFC[count]);
355 return;
356 }
357 if (unorm_quickCheck(&(CPNFKD[count]), 1, UNORM_NFKD, &error) !=
358 UNORM_NO)
359 {
360 log_err("ERROR in NFKD quick check at U+%04x\n", CPNFKD[count]);
361 return;
362 }
363 if (unorm_quickCheck(&(CPNFKC[count]), 1, UNORM_NFKC, &error) !=
364 UNORM_NO)
365 {
366 log_err("ERROR in NFKC quick check at U+%04x\n", CPNFKC[count]);
367 return;
368 }
369 }
370 }
371
372
373 static void TestQuickCheckResultYES()
374 {
375 const UChar CPNFD[] = {0x00C6, 0x017F, 0x0F74, 0x1000, 0x1E9A,
376 0x2261, 0x3075, 0x4000, 0x5000, 0xF000};
377 const UChar CPNFC[] = {0x0400, 0x0540, 0x0901, 0x1000, 0x1500,
378 0x1E9A, 0x3000, 0x4000, 0x5000, 0xF000};
379 const UChar CPNFKD[] = {0x00AB, 0x02A0, 0x1000, 0x1027, 0x2FFB,
380 0x3FFF, 0x4FFF, 0xA000, 0xF000, 0xFA27};
381 const UChar CPNFKC[] = {0x00B0, 0x0100, 0x0200, 0x0A02, 0x1000,
382 0x2010, 0x3030, 0x4000, 0xA000, 0xFA0E};
383
384 const int SIZE = 10;
385 int count = 0;
386 UErrorCode error = U_ZERO_ERROR;
387
388 UChar cp = 0;
389 while (cp < 0xA0)
390 {
391 if (unorm_quickCheck(&cp, 1, UNORM_NFD, &error) != UNORM_YES)
392 {
393 log_data_err("ERROR in NFD quick check at U+%04x - (Are you missing data?)\n", cp);
394 return;
395 }
396 if (unorm_quickCheck(&cp, 1, UNORM_NFC, &error) !=
397 UNORM_YES)
398 {
399 log_err("ERROR in NFC quick check at U+%04x\n", cp);
400 return;
401 }
402 if (unorm_quickCheck(&cp, 1, UNORM_NFKD, &error) != UNORM_YES)
403 {
404 log_err("ERROR in NFKD quick check at U+%04x\n", cp);
405 return;
406 }
407 if (unorm_quickCheck(&cp, 1, UNORM_NFKC, &error) !=
408 UNORM_YES)
409 {
410 log_err("ERROR in NFKC quick check at U+%04x\n", cp);
411 return;
412 }
413 cp ++;
414 }
415
416 for (; count < SIZE; count ++)
417 {
418 if (unorm_quickCheck(&(CPNFD[count]), 1, UNORM_NFD, &error) !=
419 UNORM_YES)
420 {
421 log_err("ERROR in NFD quick check at U+%04x\n", CPNFD[count]);
422 return;
423 }
424 if (unorm_quickCheck(&(CPNFC[count]), 1, UNORM_NFC, &error)
425 != UNORM_YES)
426 {
427 log_err("ERROR in NFC quick check at U+%04x\n", CPNFC[count]);
428 return;
429 }
430 if (unorm_quickCheck(&(CPNFKD[count]), 1, UNORM_NFKD, &error) !=
431 UNORM_YES)
432 {
433 log_err("ERROR in NFKD quick check at U+%04x\n", CPNFKD[count]);
434 return;
435 }
436 if (unorm_quickCheck(&(CPNFKC[count]), 1, UNORM_NFKC, &error) !=
437 UNORM_YES)
438 {
439 log_err("ERROR in NFKC quick check at U+%04x\n", CPNFKC[count]);
440 return;
441 }
442 }
443 }
444
445 static void TestQuickCheckResultMAYBE()
446 {
447 const UChar CPNFC[] = {0x0306, 0x0654, 0x0BBE, 0x102E, 0x1161,
448 0x116A, 0x1173, 0x1175, 0x3099, 0x309A};
449 const UChar CPNFKC[] = {0x0300, 0x0654, 0x0655, 0x09D7, 0x0B3E,
450 0x0DCF, 0xDDF, 0x102E, 0x11A8, 0x3099};
451
452
453 const int SIZE = 10;
454
455 int count = 0;
456 UErrorCode error = U_ZERO_ERROR;
457
458 /* NFD and NFKD does not have any MAYBE codepoints */
459 for (; count < SIZE; count ++)
460 {
461 if (unorm_quickCheck(&(CPNFC[count]), 1, UNORM_NFC, &error) !=
462 UNORM_MAYBE)
463 {
464 log_data_err("ERROR in NFC quick check at U+%04x - (Are you missing data?)\n", CPNFC[count]);
465 return;
466 }
467 if (unorm_quickCheck(&(CPNFKC[count]), 1, UNORM_NFKC, &error) !=
468 UNORM_MAYBE)
469 {
470 log_err("ERROR in NFKC quick check at U+%04x\n", CPNFKC[count]);
471 return;
472 }
473 }
474 }
475
476 static void TestQuickCheckStringResult()
477 {
478 int count;
479 UChar *d = NULL;
480 UChar *c = NULL;
481 UErrorCode error = U_ZERO_ERROR;
482
483 for (count = 0; count < LENGTHOF(canonTests); count ++)
484 {
485 d = CharsToUChars(canonTests[count][1]);
486 c = CharsToUChars(canonTests[count][2]);
487 if (unorm_quickCheck(d, u_strlen(d), UNORM_NFD, &error) !=
488 UNORM_YES)
489 {
490 log_data_err("ERROR in NFD quick check for string at count %d - (Are you missing data?)\n", count);
491 return;
492 }
493
494 if (unorm_quickCheck(c, u_strlen(c), UNORM_NFC, &error) ==
495 UNORM_NO)
496 {
497 log_err("ERROR in NFC quick check for string at count %d\n", count);
498 return;
499 }
500
501 free(d);
502 free(c);
503 }
504
505 for (count = 0; count < LENGTHOF(compatTests); count ++)
506 {
507 d = CharsToUChars(compatTests[count][1]);
508 c = CharsToUChars(compatTests[count][2]);
509 if (unorm_quickCheck(d, u_strlen(d), UNORM_NFKD, &error) !=
510 UNORM_YES)
511 {
512 log_err("ERROR in NFKD quick check for string at count %d\n", count);
513 return;
514 }
515
516 if (unorm_quickCheck(c, u_strlen(c), UNORM_NFKC, &error) !=
517 UNORM_YES)
518 {
519 log_err("ERROR in NFKC quick check for string at count %d\n", count);
520 return;
521 }
522
523 free(d);
524 free(c);
525 }
526 }
527
528 void TestQuickCheck()
529 {
530 TestQuickCheckResultNO();
531 TestQuickCheckResultYES();
532 TestQuickCheckResultMAYBE();
533 TestQuickCheckStringResult();
534 }
535
536 /*
537 * The intltest/NormalizerConformanceTest tests a lot of strings that _are_
538 * normalized, and some that are not.
539 * Here we pick some specific cases and test the C API.
540 */
541 static void TestIsNormalized(void) {
542 static const UChar notNFC[][8]={ /* strings that are not in NFC */
543 { 0x62, 0x61, 0x300, 0x63, 0 }, /* 0061 0300 compose */
544 { 0xfb1d, 0 }, /* excluded from composition */
545 { 0x0627, 0x0653, 0 }, /* 0627 0653 compose */
546 { 0x3071, 0x306f, 0x309a, 0x3073, 0 } /* 306F 309A compose */
547 };
548 static const UChar notNFKC[][8]={ /* strings that are not in NFKC */
549 { 0x1100, 0x1161, 0 }, /* Jamo compose */
550 { 0x1100, 0x314f, 0 }, /* compatibility Jamo compose */
551 { 0x03b1, 0x1f00, 0x0345, 0x03b3, 0 } /* 1F00 0345 compose */
552 };
553
554 int32_t i;
555 UErrorCode errorCode;
556
557 /* API test */
558
559 /* normal case with length>=0 (length -1 used for special cases below) */
560 errorCode=U_ZERO_ERROR;
561 if(!unorm_isNormalized(notNFC[0]+2, 1, UNORM_NFC, &errorCode) || U_FAILURE(errorCode)) {
562 log_data_err("error: !isNormalized(<U+0300>, NFC) (%s) - (Are you missing data?)\n", u_errorName(errorCode));
563 }
564
565 /* incoming U_FAILURE */
566 errorCode=U_TRUNCATED_CHAR_FOUND;
567 (void)unorm_isNormalized(notNFC[0]+2, 1, UNORM_NFC, &errorCode);
568 if(errorCode!=U_TRUNCATED_CHAR_FOUND) {
569 log_err("error: isNormalized(U_TRUNCATED_CHAR_FOUND) changed the error code to %s\n", u_errorName(errorCode));
570 }
571
572 /* NULL source */
573 errorCode=U_ZERO_ERROR;
574 (void)unorm_isNormalized(NULL, 1, UNORM_NFC, &errorCode);
575 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
576 log_data_err("error: isNormalized(NULL) did not set U_ILLEGAL_ARGUMENT_ERROR but %s - (Are you missing data?)\n", u_errorName(errorCode));
577 }
578
579 /* bad length */
580 errorCode=U_ZERO_ERROR;
581 (void)unorm_isNormalized(notNFC[0]+2, -2, UNORM_NFC, &errorCode);
582 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
583 log_data_err("error: isNormalized([-2]) did not set U_ILLEGAL_ARGUMENT_ERROR but %s - (Are you missing data?)\n", u_errorName(errorCode));
584 }
585
586 /* specific cases */
587 for(i=0; i<LENGTHOF(notNFC); ++i) {
588 errorCode=U_ZERO_ERROR;
589 if(unorm_isNormalized(notNFC[i], -1, UNORM_NFC, &errorCode) || U_FAILURE(errorCode)) {
590 log_data_err("error: isNormalized(notNFC[%d], NFC) is wrong (%s) - (Are you missing data?)\n", i, u_errorName(errorCode));
591 }
592 errorCode=U_ZERO_ERROR;
593 if(unorm_isNormalized(notNFC[i], -1, UNORM_NFKC, &errorCode) || U_FAILURE(errorCode)) {
594 log_data_err("error: isNormalized(notNFC[%d], NFKC) is wrong (%s) - (Are you missing data?)\n", i, u_errorName(errorCode));
595 }
596 }
597 for(i=0; i<LENGTHOF(notNFKC); ++i) {
598 errorCode=U_ZERO_ERROR;
599 if(unorm_isNormalized(notNFKC[i], -1, UNORM_NFKC, &errorCode) || U_FAILURE(errorCode)) {
600 log_data_err("error: isNormalized(notNFKC[%d], NFKC) is wrong (%s) - (Are you missing data?)\n", i, u_errorName(errorCode));
601 }
602 }
603 }
604
605 void TestCheckFCD()
606 {
607 UErrorCode status = U_ZERO_ERROR;
608 static const UChar FAST_[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
609 0x0A};
610 static const UChar FALSE_[] = {0x0001, 0x0002, 0x02EA, 0x03EB, 0x0300, 0x0301,
611 0x02B9, 0x0314, 0x0315, 0x0316};
612 static const UChar TRUE_[] = {0x0030, 0x0040, 0x0440, 0x056D, 0x064F, 0x06E7,
613 0x0050, 0x0730, 0x09EE, 0x1E10};
614
615 static const UChar datastr[][5] =
616 { {0x0061, 0x030A, 0x1E05, 0x0302, 0},
617 {0x0061, 0x030A, 0x00E2, 0x0323, 0},
618 {0x0061, 0x0323, 0x00E2, 0x0323, 0},
619 {0x0061, 0x0323, 0x1E05, 0x0302, 0} };
620 static const UBool result[] = {UNORM_YES, UNORM_NO, UNORM_NO, UNORM_YES};
621
622 static const UChar datachar[] = {0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
623 0x6a,
624 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
625 0xea,
626 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306,
627 0x0307, 0x0308, 0x0309, 0x030a,
628 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326,
629 0x0327, 0x0328, 0x0329, 0x032a,
630 0x1e00, 0x1e01, 0x1e02, 0x1e03, 0x1e04, 0x1e05, 0x1e06,
631 0x1e07, 0x1e08, 0x1e09, 0x1e0a};
632
633 int count = 0;
634
635 if (unorm_quickCheck(FAST_, 10, UNORM_FCD, &status) != UNORM_YES)
636 log_data_err("unorm_quickCheck(FCD) failed: expected value for fast unorm_quickCheck is UNORM_YES - (Are you missing data?)\n");
637 if (unorm_quickCheck(FALSE_, 10, UNORM_FCD, &status) != UNORM_NO)
638 log_err("unorm_quickCheck(FCD) failed: expected value for error unorm_quickCheck is UNORM_NO\n");
639 if (unorm_quickCheck(TRUE_, 10, UNORM_FCD, &status) != UNORM_YES)
640 log_data_err("unorm_quickCheck(FCD) failed: expected value for correct unorm_quickCheck is UNORM_YES - (Are you missing data?)\n");
641
642 if (U_FAILURE(status))
643 log_data_err("unorm_quickCheck(FCD) failed: %s - (Are you missing data?)\n", u_errorName(status));
644
645 while (count < 4)
646 {
647 UBool fcdresult = unorm_quickCheck(datastr[count], 4, UNORM_FCD, &status);
648 if (U_FAILURE(status)) {
649 log_data_err("unorm_quickCheck(FCD) failed: exception occured at data set %d - (Are you missing data?)\n", count);
650 break;
651 }
652 else {
653 if (result[count] != fcdresult) {
654 log_err("unorm_quickCheck(FCD) failed: Data set %d expected value %d\n", count,
655 result[count]);
656 }
657 }
658 count ++;
659 }
660
661 /* random checks of long strings */
662 status = U_ZERO_ERROR;
663 srand((unsigned)time( NULL ));
664
665 for (count = 0; count < 50; count ++)
666 {
667 int size = 0;
668 UBool testresult = UNORM_YES;
669 UChar data[20];
670 UChar norm[100];
671 UChar nfd[100];
672 int normsize = 0;
673 int nfdsize = 0;
674
675 while (size != 19) {
676 data[size] = datachar[(rand() * 50) / RAND_MAX];
677 log_verbose("0x%x", data[size]);
678 normsize += unorm_normalize(data + size, 1, UNORM_NFD, 0,
679 norm + normsize, 100 - normsize, &status);
680 if (U_FAILURE(status)) {
681 log_data_err("unorm_quickCheck(FCD) failed: exception occured at data generation - (Are you missing data?)\n");
682 break;
683 }
684 size ++;
685 }
686 log_verbose("\n");
687
688 nfdsize = unorm_normalize(data, size, UNORM_NFD, 0,
689 nfd, 100, &status);
690 if (U_FAILURE(status)) {
691 log_data_err("unorm_quickCheck(FCD) failed: exception occured at normalized data generation - (Are you missing data?)\n");
692 }
693
694 if (nfdsize != normsize || u_memcmp(nfd, norm, nfdsize) != 0) {
695 testresult = UNORM_NO;
696 }
697 if (testresult == UNORM_YES) {
698 log_verbose("result UNORM_YES\n");
699 }
700 else {
701 log_verbose("result UNORM_NO\n");
702 }
703
704 if (unorm_quickCheck(data, size, UNORM_FCD, &status) != testresult || U_FAILURE(status)) {
705 log_data_err("unorm_quickCheck(FCD) failed: expected %d for random data - (Are you missing data?)\n", testresult);
706 }
707 }
708 }
709
710 static void
711 TestAPI() {
712 static const UChar in[]={ 0x68, 0xe4 };
713 UChar out[20]={ 0xffff, 0xffff, 0xffff, 0xffff };
714 UErrorCode errorCode;
715 int32_t length;
716
717 /* try preflighting */
718 errorCode=U_ZERO_ERROR;
719 length=unorm_normalize(in, 2, UNORM_NFD, 0, NULL, 0, &errorCode);
720 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=3) {
721 log_data_err("unorm_normalize(pure preflighting NFD)=%ld failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
722 return;
723 }
724
725 errorCode=U_ZERO_ERROR;
726 length=unorm_normalize(in, 2, UNORM_NFD, 0, out, 3, &errorCode);
727 if(U_FAILURE(errorCode)) {
728 log_err("unorm_normalize(NFD)=%ld failed with %s\n", length, u_errorName(errorCode));
729 return;
730 }
731 if(length!=3 || out[2]!=0x308 || out[3]!=0xffff) {
732 log_err("unorm_normalize(NFD ma<umlaut>)=%ld failed with out[]=U+%04x U+%04x U+%04x U+%04x\n", length, out[0], out[1], out[2], out[3]);
733 return;
734 }
735 length=unorm_normalize(NULL, 0, UNORM_NFC, 0, NULL, 0, &errorCode);
736 if(U_FAILURE(errorCode)) {
737 log_err("unorm_normalize(src NULL[0], NFC, dest NULL[0])=%ld failed with %s\n", (long)length, u_errorName(errorCode));
738 return;
739 }
740 length=unorm_normalize(NULL, 0, UNORM_NFC, 0, out, 20, &errorCode);
741 if(U_FAILURE(errorCode)) {
742 log_err("unorm_normalize(src NULL[0], NFC, dest out[20])=%ld failed with %s\n", (long)length, u_errorName(errorCode));
743 return;
744 }
745 }
746
747 /* test cases to improve test code coverage */
748 enum {
749 HANGUL_K_KIYEOK=0x3131, /* NFKD->Jamo L U+1100 */
750 HANGUL_K_WEO=0x315d, /* NFKD->Jamo V U+116f */
751 HANGUL_K_KIYEOK_SIOS=0x3133, /* NFKD->Jamo T U+11aa */
752
753 HANGUL_KIYEOK=0x1100, /* Jamo L U+1100 */
754 HANGUL_WEO=0x116f, /* Jamo V U+116f */
755 HANGUL_KIYEOK_SIOS=0x11aa, /* Jamo T U+11aa */
756
757 HANGUL_AC00=0xac00, /* Hangul syllable = Jamo LV U+ac00 */
758 HANGUL_SYLLABLE=0xac00+14*28+3, /* Hangul syllable = U+1100 * U+116f * U+11aa */
759
760 MUSICAL_VOID_NOTEHEAD=0x1d157,
761 MUSICAL_HALF_NOTE=0x1d15e, /* NFC/NFD->Notehead+Stem */
762 MUSICAL_STEM=0x1d165, /* cc=216 */
763 MUSICAL_STACCATO=0x1d17c /* cc=220 */
764 };
765
766 static void
767 TestNormCoverage() {
768 UChar input[1000], expect[1000], output[1000];
769 UErrorCode errorCode;
770 int32_t i, length, inLength, expectLength, hangulPrefixLength, preflightLength;
771
772 /* create a long and nasty string with NFKC-unsafe characters */
773 inLength=0;
774
775 /* 3 Jamos L/V/T, all 8 combinations normal/compatibility */
776 input[inLength++]=HANGUL_KIYEOK;
777 input[inLength++]=HANGUL_WEO;
778 input[inLength++]=HANGUL_KIYEOK_SIOS;
779
780 input[inLength++]=HANGUL_KIYEOK;
781 input[inLength++]=HANGUL_WEO;
782 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
783
784 input[inLength++]=HANGUL_KIYEOK;
785 input[inLength++]=HANGUL_K_WEO;
786 input[inLength++]=HANGUL_KIYEOK_SIOS;
787
788 input[inLength++]=HANGUL_KIYEOK;
789 input[inLength++]=HANGUL_K_WEO;
790 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
791
792 input[inLength++]=HANGUL_K_KIYEOK;
793 input[inLength++]=HANGUL_WEO;
794 input[inLength++]=HANGUL_KIYEOK_SIOS;
795
796 input[inLength++]=HANGUL_K_KIYEOK;
797 input[inLength++]=HANGUL_WEO;
798 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
799
800 input[inLength++]=HANGUL_K_KIYEOK;
801 input[inLength++]=HANGUL_K_WEO;
802 input[inLength++]=HANGUL_KIYEOK_SIOS;
803
804 input[inLength++]=HANGUL_K_KIYEOK;
805 input[inLength++]=HANGUL_K_WEO;
806 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
807
808 /* Hangul LV with normal/compatibility Jamo T */
809 input[inLength++]=HANGUL_AC00;
810 input[inLength++]=HANGUL_KIYEOK_SIOS;
811
812 input[inLength++]=HANGUL_AC00;
813 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
814
815 /* compatibility Jamo L, V */
816 input[inLength++]=HANGUL_K_KIYEOK;
817 input[inLength++]=HANGUL_K_WEO;
818
819 hangulPrefixLength=inLength;
820
821 input[inLength++]=U16_LEAD(MUSICAL_HALF_NOTE);
822 input[inLength++]=U16_TRAIL(MUSICAL_HALF_NOTE);
823 for(i=0; i<200; ++i) {
824 input[inLength++]=U16_LEAD(MUSICAL_STACCATO);
825 input[inLength++]=U16_TRAIL(MUSICAL_STACCATO);
826 input[inLength++]=U16_LEAD(MUSICAL_STEM);
827 input[inLength++]=U16_TRAIL(MUSICAL_STEM);
828 }
829
830 /* (compatibility) Jamo L, T do not compose */
831 input[inLength++]=HANGUL_K_KIYEOK;
832 input[inLength++]=HANGUL_K_KIYEOK_SIOS;
833
834 /* quick checks */
835 errorCode=U_ZERO_ERROR;
836 if(UNORM_NO!=unorm_quickCheck(input, inLength, UNORM_NFD, &errorCode) || U_FAILURE(errorCode)) {
837 log_data_err("error unorm_quickCheck(long input, UNORM_NFD)!=NO (%s) - (Are you missing data?)\n", u_errorName(errorCode));
838 }
839 errorCode=U_ZERO_ERROR;
840 if(UNORM_NO!=unorm_quickCheck(input, inLength, UNORM_NFKD, &errorCode) || U_FAILURE(errorCode)) {
841 log_data_err("error unorm_quickCheck(long input, UNORM_NFKD)!=NO (%s) - (Are you missing data?)\n", u_errorName(errorCode));
842 }
843 errorCode=U_ZERO_ERROR;
844 if(UNORM_NO!=unorm_quickCheck(input, inLength, UNORM_NFC, &errorCode) || U_FAILURE(errorCode)) {
845 log_data_err("error unorm_quickCheck(long input, UNORM_NFC)!=NO (%s) - (Are you missing data?)\n", u_errorName(errorCode));
846 }
847 errorCode=U_ZERO_ERROR;
848 if(UNORM_NO!=unorm_quickCheck(input, inLength, UNORM_NFKC, &errorCode) || U_FAILURE(errorCode)) {
849 log_data_err("error unorm_quickCheck(long input, UNORM_NFKC)!=NO (%s) - (Are you missing data?)\n", u_errorName(errorCode));
850 }
851 errorCode=U_ZERO_ERROR;
852 if(UNORM_NO!=unorm_quickCheck(input, inLength, UNORM_FCD, &errorCode) || U_FAILURE(errorCode)) {
853 log_data_err("error unorm_quickCheck(long input, UNORM_FCD)!=NO (%s) - (Are you missing data?)\n", u_errorName(errorCode));
854 }
855
856 /* NFKC */
857 expectLength=0;
858 expect[expectLength++]=HANGUL_SYLLABLE;
859
860 expect[expectLength++]=HANGUL_SYLLABLE;
861
862 expect[expectLength++]=HANGUL_SYLLABLE;
863
864 expect[expectLength++]=HANGUL_SYLLABLE;
865
866 expect[expectLength++]=HANGUL_SYLLABLE;
867
868 expect[expectLength++]=HANGUL_SYLLABLE;
869
870 expect[expectLength++]=HANGUL_SYLLABLE;
871
872 expect[expectLength++]=HANGUL_SYLLABLE;
873
874 expect[expectLength++]=HANGUL_AC00+3;
875
876 expect[expectLength++]=HANGUL_AC00+3;
877
878 expect[expectLength++]=HANGUL_AC00+14*28;
879
880 expect[expectLength++]=U16_LEAD(MUSICAL_VOID_NOTEHEAD);
881 expect[expectLength++]=U16_TRAIL(MUSICAL_VOID_NOTEHEAD);
882 expect[expectLength++]=U16_LEAD(MUSICAL_STEM);
883 expect[expectLength++]=U16_TRAIL(MUSICAL_STEM);
884 for(i=0; i<200; ++i) {
885 expect[expectLength++]=U16_LEAD(MUSICAL_STEM);
886 expect[expectLength++]=U16_TRAIL(MUSICAL_STEM);
887 }
888 for(i=0; i<200; ++i) {
889 expect[expectLength++]=U16_LEAD(MUSICAL_STACCATO);
890 expect[expectLength++]=U16_TRAIL(MUSICAL_STACCATO);
891 }
892
893 expect[expectLength++]=HANGUL_KIYEOK;
894 expect[expectLength++]=HANGUL_KIYEOK_SIOS;
895
896 /* try destination overflow first */
897 errorCode=U_ZERO_ERROR;
898 preflightLength=unorm_normalize(input, inLength,
899 UNORM_NFKC, 0,
900 output, 100, /* too short */
901 &errorCode);
902 if(errorCode!=U_BUFFER_OVERFLOW_ERROR) {
903 log_data_err("error unorm_normalize(long input, output too short, UNORM_NFKC) did not overflow but %s - (Are you missing data?)\n", u_errorName(errorCode));
904 }
905
906 /* real NFKC */
907 errorCode=U_ZERO_ERROR;
908 length=unorm_normalize(input, inLength,
909 UNORM_NFKC, 0,
910 output, sizeof(output)/U_SIZEOF_UCHAR,
911 &errorCode);
912 if(U_FAILURE(errorCode)) {
913 log_data_err("error unorm_normalize(long input, UNORM_NFKC) failed with %s - (Are you missing data?)\n", u_errorName(errorCode));
914 } else if(length!=expectLength || u_memcmp(output, expect, length)!=0) {
915 log_err("error unorm_normalize(long input, UNORM_NFKC) produced wrong result\n");
916 for(i=0; i<length; ++i) {
917 if(output[i]!=expect[i]) {
918 log_err(" NFKC[%d]==U+%04lx expected U+%04lx\n", i, output[i], expect[i]);
919 break;
920 }
921 }
922 }
923 if(length!=preflightLength) {
924 log_err("error unorm_normalize(long input, UNORM_NFKC)==%ld but preflightLength==%ld\n", length, preflightLength);
925 }
926
927 /* FCD */
928 u_memcpy(expect, input, hangulPrefixLength);
929 expectLength=hangulPrefixLength;
930
931 expect[expectLength++]=U16_LEAD(MUSICAL_VOID_NOTEHEAD);
932 expect[expectLength++]=U16_TRAIL(MUSICAL_VOID_NOTEHEAD);
933 expect[expectLength++]=U16_LEAD(MUSICAL_STEM);
934 expect[expectLength++]=U16_TRAIL(MUSICAL_STEM);
935 for(i=0; i<200; ++i) {
936 expect[expectLength++]=U16_LEAD(MUSICAL_STEM);
937 expect[expectLength++]=U16_TRAIL(MUSICAL_STEM);
938 }
939 for(i=0; i<200; ++i) {
940 expect[expectLength++]=U16_LEAD(MUSICAL_STACCATO);
941 expect[expectLength++]=U16_TRAIL(MUSICAL_STACCATO);
942 }
943
944 expect[expectLength++]=HANGUL_K_KIYEOK;
945 expect[expectLength++]=HANGUL_K_KIYEOK_SIOS;
946
947 errorCode=U_ZERO_ERROR;
948 length=unorm_normalize(input, inLength,
949 UNORM_FCD, 0,
950 output, sizeof(output)/U_SIZEOF_UCHAR,
951 &errorCode);
952 if(U_FAILURE(errorCode)) {
953 log_data_err("error unorm_normalize(long input, UNORM_FCD) failed with %s - (Are you missing data?)\n", u_errorName(errorCode));
954 } else if(length!=expectLength || u_memcmp(output, expect, length)!=0) {
955 log_err("error unorm_normalize(long input, UNORM_FCD) produced wrong result\n");
956 for(i=0; i<length; ++i) {
957 if(output[i]!=expect[i]) {
958 log_err(" FCD[%d]==U+%04lx expected U+%04lx\n", i, output[i], expect[i]);
959 break;
960 }
961 }
962 }
963 }
964
965 /* API test for unorm_concatenate() - for real test strings see intltest/tstnorm.cpp */
966 static void
967 TestConcatenate(void) {
968 /* "re + 'sume'" */
969 static const UChar
970 left[]={
971 0x72, 0x65, 0
972 },
973 right[]={
974 0x301, 0x73, 0x75, 0x6d, 0xe9, 0
975 },
976 expect[]={
977 0x72, 0xe9, 0x73, 0x75, 0x6d, 0xe9, 0
978 };
979
980 UChar buffer[100];
981 UErrorCode errorCode;
982 int32_t length;
983
984 /* left with length, right NUL-terminated */
985 errorCode=U_ZERO_ERROR;
986 length=unorm_concatenate(left, 2, right, -1, buffer, 100, UNORM_NFC, 0, &errorCode);
987 if(U_FAILURE(errorCode) || length!=6 || 0!=u_memcmp(buffer, expect, length)) {
988 log_data_err("error: unorm_concatenate()=%ld (expect 6) failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
989 }
990
991 /* preflighting */
992 errorCode=U_ZERO_ERROR;
993 length=unorm_concatenate(left, 2, right, -1, NULL, 0, UNORM_NFC, 0, &errorCode);
994 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=6) {
995 log_data_err("error: unorm_concatenate(preflighting)=%ld (expect 6) failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
996 }
997
998 buffer[2]=0x5555;
999 errorCode=U_ZERO_ERROR;
1000 length=unorm_concatenate(left, 2, right, -1, buffer, 1, UNORM_NFC, 0, &errorCode);
1001 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=6 || buffer[2]!=0x5555) {
1002 log_data_err("error: unorm_concatenate(preflighting 2)=%ld (expect 6) failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
1003 }
1004
1005 /* enter with U_FAILURE */
1006 buffer[2]=0xaaaa;
1007 errorCode=U_UNEXPECTED_TOKEN;
1008 length=unorm_concatenate(left, 2, right, -1, buffer, 100, UNORM_NFC, 0, &errorCode);
1009 if(errorCode!=U_UNEXPECTED_TOKEN || buffer[2]!=0xaaaa) {
1010 log_err("error: unorm_concatenate(failure)=%ld failed with %s\n", length, u_errorName(errorCode));
1011 }
1012
1013 /* illegal arguments */
1014 buffer[2]=0xaaaa;
1015 errorCode=U_ZERO_ERROR;
1016 length=unorm_concatenate(NULL, 2, right, -1, buffer, 100, UNORM_NFC, 0, &errorCode);
1017 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR || buffer[2]!=0xaaaa) {
1018 log_data_err("error: unorm_concatenate(left=NULL)=%ld failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
1019 }
1020
1021 errorCode=U_ZERO_ERROR;
1022 length=unorm_concatenate(left, 2, right, -1, NULL, 100, UNORM_NFC, 0, &errorCode);
1023 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1024 log_data_err("error: unorm_concatenate(buffer=NULL)=%ld failed with %s - (Are you missing data?)\n", length, u_errorName(errorCode));
1025 }
1026 }
1027
1028 enum {
1029 _PLUS=0x2b
1030 };
1031
1032 static const char *const _modeString[UNORM_MODE_COUNT]={
1033 "0", "NONE", "NFD", "NFKD", "NFC", "NFKC", "FCD"
1034 };
1035
1036 static void
1037 _testIter(const UChar *src, int32_t srcLength,
1038 UCharIterator *iter, UNormalizationMode mode, UBool forward,
1039 const UChar *out, int32_t outLength,
1040 const int32_t *srcIndexes, int32_t srcIndexesLength) {
1041 UChar buffer[4];
1042 const UChar *expect, *outLimit, *in;
1043 int32_t length, i, expectLength, expectIndex, prevIndex, index, inLength;
1044 UErrorCode errorCode;
1045 UBool neededToNormalize, expectNeeded;
1046
1047 errorCode=U_ZERO_ERROR;
1048 outLimit=out+outLength;
1049 if(forward) {
1050 expect=out;
1051 i=index=0;
1052 } else {
1053 expect=outLimit;
1054 i=srcIndexesLength-2;
1055 index=srcLength;
1056 }
1057
1058 for(;;) {
1059 prevIndex=index;
1060 if(forward) {
1061 if(!iter->hasNext(iter)) {
1062 return;
1063 }
1064 length=unorm_next(iter,
1065 buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1066 mode, 0,
1067 (UBool)(out!=NULL), &neededToNormalize,
1068 &errorCode);
1069 expectIndex=srcIndexes[i+1];
1070 in=src+prevIndex;
1071 inLength=expectIndex-prevIndex;
1072
1073 if(out!=NULL) {
1074 /* get output piece from between plus signs */
1075 expectLength=0;
1076 while((expect+expectLength)!=outLimit && expect[expectLength]!=_PLUS) {
1077 ++expectLength;
1078 }
1079 expectNeeded=(UBool)(0!=u_memcmp(buffer, in, inLength));
1080 } else {
1081 expect=in;
1082 expectLength=inLength;
1083 expectNeeded=FALSE;
1084 }
1085 } else {
1086 if(!iter->hasPrevious(iter)) {
1087 return;
1088 }
1089 length=unorm_previous(iter,
1090 buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1091 mode, 0,
1092 (UBool)(out!=NULL), &neededToNormalize,
1093 &errorCode);
1094 expectIndex=srcIndexes[i];
1095 in=src+expectIndex;
1096 inLength=prevIndex-expectIndex;
1097
1098 if(out!=NULL) {
1099 /* get output piece from between plus signs */
1100 expectLength=0;
1101 while(expect!=out && expect[-1]!=_PLUS) {
1102 ++expectLength;
1103 --expect;
1104 }
1105 expectNeeded=(UBool)(0!=u_memcmp(buffer, in, inLength));
1106 } else {
1107 expect=in;
1108 expectLength=inLength;
1109 expectNeeded=FALSE;
1110 }
1111 }
1112 index=iter->getIndex(iter, UITER_CURRENT);
1113
1114 if(U_FAILURE(errorCode)) {
1115 log_data_err("error unorm iteration (next/previous %d %s)[%d]: %s - (Are you missing data?)\n",
1116 forward, _modeString[mode], i, u_errorName(errorCode));
1117 return;
1118 }
1119 if(expectIndex!=index) {
1120 log_err("error unorm iteration (next/previous %d %s): index[%d] wrong, got %d expected %d\n",
1121 forward, _modeString[mode], i, index, expectIndex);
1122 return;
1123 }
1124 if(expectLength!=length) {
1125 log_err("error unorm iteration (next/previous %d %s): length[%d] wrong, got %d expected %d\n",
1126 forward, _modeString[mode], i, length, expectLength);
1127 return;
1128 }
1129 if(0!=u_memcmp(expect, buffer, length)) {
1130 log_err("error unorm iteration (next/previous %d %s): output string[%d] wrong\n",
1131 forward, _modeString[mode], i);
1132 return;
1133 }
1134 if(neededToNormalize!=expectNeeded) {
1135 }
1136
1137 if(forward) {
1138 expect+=expectLength+1; /* go after the + */
1139 ++i;
1140 } else {
1141 --expect; /* go before the + */
1142 --i;
1143 }
1144 }
1145 }
1146
1147 static void
1148 TestNextPrevious() {
1149 static const UChar
1150 src[]={ /* input string */
1151 0xa0, 0xe4, 0x63, 0x302, 0x327, 0xac00, 0x3133
1152 },
1153 nfd[]={ /* + separates expected output pieces */
1154 0xa0, _PLUS, 0x61, 0x308, _PLUS, 0x63, 0x327, 0x302, _PLUS, 0x1100, 0x1161, _PLUS, 0x3133
1155 },
1156 nfkd[]={
1157 0x20, _PLUS, 0x61, 0x308, _PLUS, 0x63, 0x327, 0x302, _PLUS, 0x1100, 0x1161, _PLUS, 0x11aa
1158 },
1159 nfc[]={
1160 0xa0, _PLUS, 0xe4, _PLUS, 0xe7, 0x302, _PLUS, 0xac00, _PLUS, 0x3133
1161 },
1162 nfkc[]={
1163 0x20, _PLUS, 0xe4, _PLUS, 0xe7, 0x302, _PLUS, 0xac03
1164 },
1165 fcd[]={
1166 0xa0, _PLUS, 0xe4, _PLUS, 0x63, 0x327, 0x302, _PLUS, 0xac00, _PLUS, 0x3133
1167 };
1168
1169 /* expected iterator indexes in the source string for each iteration piece */
1170 static const int32_t
1171 nfdIndexes[]={
1172 0, 1, 2, 5, 6, 7
1173 },
1174 nfkdIndexes[]={
1175 0, 1, 2, 5, 6, 7
1176 },
1177 nfcIndexes[]={
1178 0, 1, 2, 5, 6, 7
1179 },
1180 nfkcIndexes[]={
1181 0, 1, 2, 5, 7
1182 },
1183 fcdIndexes[]={
1184 0, 1, 2, 5, 6, 7
1185 };
1186
1187 UCharIterator iter;
1188
1189 UChar buffer[4];
1190 int32_t length;
1191
1192 UBool neededToNormalize;
1193 UErrorCode errorCode;
1194
1195 uiter_setString(&iter, src, sizeof(src)/U_SIZEOF_UCHAR);
1196
1197 /* test iteration with doNormalize */
1198 iter.index=0;
1199 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFD, TRUE, nfd, sizeof(nfd)/U_SIZEOF_UCHAR, nfdIndexes, sizeof(nfdIndexes)/4);
1200 iter.index=0;
1201 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKD, TRUE, nfkd, sizeof(nfkd)/U_SIZEOF_UCHAR, nfkdIndexes, sizeof(nfkdIndexes)/4);
1202 iter.index=0;
1203 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFC, TRUE, nfc, sizeof(nfc)/U_SIZEOF_UCHAR, nfcIndexes, sizeof(nfcIndexes)/4);
1204 iter.index=0;
1205 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKC, TRUE, nfkc, sizeof(nfkc)/U_SIZEOF_UCHAR, nfkcIndexes, sizeof(nfkcIndexes)/4);
1206 iter.index=0;
1207 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_FCD, TRUE, fcd, sizeof(fcd)/U_SIZEOF_UCHAR, fcdIndexes, sizeof(fcdIndexes)/4);
1208
1209 iter.index=iter.length;
1210 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFD, FALSE, nfd, sizeof(nfd)/U_SIZEOF_UCHAR, nfdIndexes, sizeof(nfdIndexes)/4);
1211 iter.index=iter.length;
1212 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKD, FALSE, nfkd, sizeof(nfkd)/U_SIZEOF_UCHAR, nfkdIndexes, sizeof(nfkdIndexes)/4);
1213 iter.index=iter.length;
1214 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFC, FALSE, nfc, sizeof(nfc)/U_SIZEOF_UCHAR, nfcIndexes, sizeof(nfcIndexes)/4);
1215 iter.index=iter.length;
1216 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKC, FALSE, nfkc, sizeof(nfkc)/U_SIZEOF_UCHAR, nfkcIndexes, sizeof(nfkcIndexes)/4);
1217 iter.index=iter.length;
1218 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_FCD, FALSE, fcd, sizeof(fcd)/U_SIZEOF_UCHAR, fcdIndexes, sizeof(fcdIndexes)/4);
1219
1220 /* test iteration without doNormalize */
1221 iter.index=0;
1222 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFD, TRUE, NULL, 0, nfdIndexes, sizeof(nfdIndexes)/4);
1223 iter.index=0;
1224 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKD, TRUE, NULL, 0, nfkdIndexes, sizeof(nfkdIndexes)/4);
1225 iter.index=0;
1226 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFC, TRUE, NULL, 0, nfcIndexes, sizeof(nfcIndexes)/4);
1227 iter.index=0;
1228 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKC, TRUE, NULL, 0, nfkcIndexes, sizeof(nfkcIndexes)/4);
1229 iter.index=0;
1230 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_FCD, TRUE, NULL, 0, fcdIndexes, sizeof(fcdIndexes)/4);
1231
1232 iter.index=iter.length;
1233 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFD, FALSE, NULL, 0, nfdIndexes, sizeof(nfdIndexes)/4);
1234 iter.index=iter.length;
1235 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKD, FALSE, NULL, 0, nfkdIndexes, sizeof(nfkdIndexes)/4);
1236 iter.index=iter.length;
1237 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFC, FALSE, NULL, 0, nfcIndexes, sizeof(nfcIndexes)/4);
1238 iter.index=iter.length;
1239 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_NFKC, FALSE, NULL, 0, nfkcIndexes, sizeof(nfkcIndexes)/4);
1240 iter.index=iter.length;
1241 _testIter(src, sizeof(src)/U_SIZEOF_UCHAR, &iter, UNORM_FCD, FALSE, NULL, 0, fcdIndexes, sizeof(fcdIndexes)/4);
1242
1243 /* try without neededToNormalize */
1244 errorCode=U_ZERO_ERROR;
1245 buffer[0]=5;
1246 iter.index=1;
1247 length=unorm_next(&iter, buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1248 UNORM_NFD, 0, TRUE, NULL,
1249 &errorCode);
1250 if(U_FAILURE(errorCode) || length!=2 || buffer[0]!=nfd[2] || buffer[1]!=nfd[3]) {
1251 log_data_err("error unorm_next(without needed) %s - (Are you missing data?)\n", u_errorName(errorCode));
1252 return;
1253 }
1254
1255 /* preflight */
1256 neededToNormalize=9;
1257 iter.index=1;
1258 length=unorm_next(&iter, NULL, 0,
1259 UNORM_NFD, 0, TRUE, &neededToNormalize,
1260 &errorCode);
1261 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || neededToNormalize!=FALSE || length!=2) {
1262 log_err("error unorm_next(pure preflighting) %s\n", u_errorName(errorCode));
1263 return;
1264 }
1265
1266 errorCode=U_ZERO_ERROR;
1267 buffer[0]=buffer[1]=5;
1268 neededToNormalize=9;
1269 iter.index=1;
1270 length=unorm_next(&iter, buffer, 1,
1271 UNORM_NFD, 0, TRUE, &neededToNormalize,
1272 &errorCode);
1273 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || neededToNormalize!=FALSE || length!=2 || buffer[1]!=5) {
1274 log_err("error unorm_next(preflighting) %s\n", u_errorName(errorCode));
1275 return;
1276 }
1277
1278 /* no iterator */
1279 errorCode=U_ZERO_ERROR;
1280 buffer[0]=buffer[1]=5;
1281 neededToNormalize=9;
1282 iter.index=1;
1283 length=unorm_next(NULL, buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1284 UNORM_NFD, 0, TRUE, &neededToNormalize,
1285 &errorCode);
1286 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1287 log_err("error unorm_next(no iterator) %s\n", u_errorName(errorCode));
1288 return;
1289 }
1290
1291 /* illegal mode */
1292 buffer[0]=buffer[1]=5;
1293 neededToNormalize=9;
1294 iter.index=1;
1295 length=unorm_next(&iter, buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1296 (UNormalizationMode)0, 0, TRUE, &neededToNormalize,
1297 &errorCode);
1298 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1299 log_err("error unorm_next(illegal mode) %s\n", u_errorName(errorCode));
1300 return;
1301 }
1302
1303 /* error coming in */
1304 errorCode=U_MISPLACED_QUANTIFIER;
1305 buffer[0]=5;
1306 iter.index=1;
1307 length=unorm_next(&iter, buffer, sizeof(buffer)/U_SIZEOF_UCHAR,
1308 UNORM_NFD, 0, TRUE, NULL,
1309 &errorCode);
1310 if(errorCode!=U_MISPLACED_QUANTIFIER) {
1311 log_err("error unorm_next(U_MISPLACED_QUANTIFIER) %s\n", u_errorName(errorCode));
1312 return;
1313 }
1314 }
1315
1316 static void
1317 TestFCNFKCClosure(void) {
1318 static const struct {
1319 UChar32 c;
1320 const UChar s[6];
1321 } tests[]={
1322 { 0x00C4, { 0 } },
1323 { 0x00E4, { 0 } },
1324 { 0x037A, { 0x0020, 0x03B9, 0 } },
1325 { 0x03D2, { 0x03C5, 0 } },
1326 { 0x20A8, { 0x0072, 0x0073, 0 } },
1327 { 0x210B, { 0x0068, 0 } },
1328 { 0x210C, { 0x0068, 0 } },
1329 { 0x2121, { 0x0074, 0x0065, 0x006C, 0 } },
1330 { 0x2122, { 0x0074, 0x006D, 0 } },
1331 { 0x2128, { 0x007A, 0 } },
1332 { 0x1D5DB, { 0x0068, 0 } },
1333 { 0x1D5ED, { 0x007A, 0 } },
1334 { 0x0061, { 0 } }
1335 };
1336
1337 UChar buffer[8];
1338 UErrorCode errorCode;
1339 int32_t i, length;
1340
1341 for(i=0; i<LENGTHOF(tests); ++i) {
1342 errorCode=U_ZERO_ERROR;
1343 length=u_getFC_NFKC_Closure(tests[i].c, buffer, LENGTHOF(buffer), &errorCode);
1344 if(U_FAILURE(errorCode) || length!=u_strlen(buffer) || 0!=u_strcmp(tests[i].s, buffer)) {
1345 log_data_err("u_getFC_NFKC_Closure(U+%04lx) is wrong (%s) - (Are you missing data?)\n", tests[i].c, u_errorName(errorCode));
1346 }
1347 }
1348
1349 /* error handling */
1350 errorCode=U_ZERO_ERROR;
1351 length=u_getFC_NFKC_Closure(0x5c, NULL, LENGTHOF(buffer), &errorCode);
1352 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1353 log_err("u_getFC_NFKC_Closure(dest=NULL) is wrong (%s)\n", u_errorName(errorCode));
1354 }
1355
1356 length=u_getFC_NFKC_Closure(0x5c, buffer, LENGTHOF(buffer), &errorCode);
1357 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1358 log_err("u_getFC_NFKC_Closure(U_FAILURE) is wrong (%s)\n", u_errorName(errorCode));
1359 }
1360 }
1361
1362 static void
1363 TestQuickCheckPerCP() {
1364 UErrorCode errorCode;
1365 UChar32 c, lead, trail;
1366 UChar s[U16_MAX_LENGTH], nfd[16];
1367 int32_t length, lccc1, lccc2, tccc1, tccc2;
1368 int32_t qc1, qc2;
1369
1370 if(
1371 u_getIntPropertyMaxValue(UCHAR_NFD_QUICK_CHECK)!=(int32_t)UNORM_YES ||
1372 u_getIntPropertyMaxValue(UCHAR_NFKD_QUICK_CHECK)!=(int32_t)UNORM_YES ||
1373 u_getIntPropertyMaxValue(UCHAR_NFC_QUICK_CHECK)!=(int32_t)UNORM_MAYBE ||
1374 u_getIntPropertyMaxValue(UCHAR_NFKC_QUICK_CHECK)!=(int32_t)UNORM_MAYBE ||
1375 u_getIntPropertyMaxValue(UCHAR_LEAD_CANONICAL_COMBINING_CLASS)!=u_getIntPropertyMaxValue(UCHAR_CANONICAL_COMBINING_CLASS) ||
1376 u_getIntPropertyMaxValue(UCHAR_TRAIL_CANONICAL_COMBINING_CLASS)!=u_getIntPropertyMaxValue(UCHAR_CANONICAL_COMBINING_CLASS)
1377 ) {
1378 log_err("wrong result from one of the u_getIntPropertyMaxValue(UCHAR_NF*_QUICK_CHECK) or UCHAR_*_CANONICAL_COMBINING_CLASS\n");
1379 }
1380
1381 /*
1382 * compare the quick check property values for some code points
1383 * to the quick check results for checking same-code point strings
1384 */
1385 errorCode=U_ZERO_ERROR;
1386 c=0;
1387 while(c<0x110000) {
1388 length=0;
1389 U16_APPEND_UNSAFE(s, length, c);
1390
1391 qc1=u_getIntPropertyValue(c, UCHAR_NFC_QUICK_CHECK);
1392 qc2=unorm_quickCheck(s, length, UNORM_NFC, &errorCode);
1393 if(qc1!=qc2) {
1394 log_data_err("u_getIntPropertyValue(NFC)=%d != %d=unorm_quickCheck(NFC) for U+%04x - (Are you missing data?)\n", qc1, qc2, c);
1395 }
1396
1397 qc1=u_getIntPropertyValue(c, UCHAR_NFD_QUICK_CHECK);
1398 qc2=unorm_quickCheck(s, length, UNORM_NFD, &errorCode);
1399 if(qc1!=qc2) {
1400 log_data_err("u_getIntPropertyValue(NFD)=%d != %d=unorm_quickCheck(NFD) for U+%04x - (Are you missing data?)\n", qc1, qc2, c);
1401 }
1402
1403 qc1=u_getIntPropertyValue(c, UCHAR_NFKC_QUICK_CHECK);
1404 qc2=unorm_quickCheck(s, length, UNORM_NFKC, &errorCode);
1405 if(qc1!=qc2) {
1406 log_data_err("u_getIntPropertyValue(NFKC)=%d != %d=unorm_quickCheck(NFKC) for U+%04x - (Are you missing data?)\n", qc1, qc2, c);
1407 }
1408
1409 qc1=u_getIntPropertyValue(c, UCHAR_NFKD_QUICK_CHECK);
1410 qc2=unorm_quickCheck(s, length, UNORM_NFKD, &errorCode);
1411 if(qc1!=qc2) {
1412 log_data_err("u_getIntPropertyValue(NFKD)=%d != %d=unorm_quickCheck(NFKD) for U+%04x - (Are you missing data?)\n", qc1, qc2, c);
1413 }
1414
1415 length=unorm_normalize(s, length, UNORM_NFD, 0, nfd, LENGTHOF(nfd), &errorCode);
1416 /* length-length == 0 is used to get around a compiler warning. */
1417 U16_GET(nfd, 0, length-length, length, lead);
1418 U16_GET(nfd, 0, length-1, length, trail);
1419
1420 lccc1=u_getIntPropertyValue(c, UCHAR_LEAD_CANONICAL_COMBINING_CLASS);
1421 lccc2=u_getCombiningClass(lead);
1422 tccc1=u_getIntPropertyValue(c, UCHAR_TRAIL_CANONICAL_COMBINING_CLASS);
1423 tccc2=u_getCombiningClass(trail);
1424
1425 if(lccc1!=lccc2) {
1426 log_err("u_getIntPropertyValue(lccc)=%d != %d=u_getCombiningClass(lead) for U+%04x\n",
1427 lccc1, lccc2, c);
1428 }
1429 if(tccc1!=tccc2) {
1430 log_err("u_getIntPropertyValue(tccc)=%d != %d=u_getCombiningClass(trail) for U+%04x\n",
1431 tccc1, tccc2, c);
1432 }
1433
1434 /* skip some code points */
1435 c=(20*c)/19+1;
1436 }
1437 }
1438
1439 static void
1440 TestComposition(void) {
1441 static const struct {
1442 UNormalizationMode mode;
1443 uint32_t options;
1444 UChar input[12];
1445 UChar expect[12];
1446 } cases[]={
1447 /*
1448 * special cases for UAX #15 bug
1449 * see Unicode Corrigendum #5: Normalization Idempotency
1450 * at http://unicode.org/versions/corrigendum5.html
1451 * (was Public Review Issue #29)
1452 */
1453 { UNORM_NFC, 0, { 0x1100, 0x0300, 0x1161, 0x0327 }, { 0x1100, 0x0300, 0x1161, 0x0327 } },
1454 { UNORM_NFC, 0, { 0x1100, 0x0300, 0x1161, 0x0327, 0x11a8 }, { 0x1100, 0x0300, 0x1161, 0x0327, 0x11a8 } },
1455 { UNORM_NFC, 0, { 0xac00, 0x0300, 0x0327, 0x11a8 }, { 0xac00, 0x0327, 0x0300, 0x11a8 } },
1456 { UNORM_NFC, 0, { 0x0b47, 0x0300, 0x0b3e }, { 0x0b47, 0x0300, 0x0b3e } },
1457
1458 /* TODO: add test cases for UNORM_FCC here (j2151) */
1459 };
1460
1461 UChar output[16];
1462 UErrorCode errorCode;
1463 int32_t i, length;
1464
1465 for(i=0; i<LENGTHOF(cases); ++i) {
1466 errorCode=U_ZERO_ERROR;
1467 length=unorm_normalize(
1468 cases[i].input, -1,
1469 cases[i].mode, cases[i].options,
1470 output, LENGTHOF(output),
1471 &errorCode);
1472 if( U_FAILURE(errorCode) ||
1473 length!=u_strlen(cases[i].expect) ||
1474 0!=u_memcmp(output, cases[i].expect, length)
1475 ) {
1476 log_data_err("unexpected result for case %d - (Are you missing data?)\n", i);
1477 }
1478 }
1479 }
1480
1481 static void
1482 TestGetDecomposition() {
1483 UChar decomp[32];
1484 int32_t length;
1485
1486 UErrorCode errorCode=U_ZERO_ERROR;
1487 const UNormalizer2 *n2=unorm2_getInstance(NULL, "nfc", UNORM2_COMPOSE_CONTIGUOUS, &errorCode);
1488 if(U_FAILURE(errorCode)) {
1489 log_err_status(errorCode, "unorm2_getInstance(nfc/FCC) failed: %s\n", u_errorName(errorCode));
1490 return;
1491 }
1492
1493 length=unorm2_getDecomposition(n2, 0x20, decomp, LENGTHOF(decomp), &errorCode);
1494 if(U_FAILURE(errorCode) || length>=0) {
1495 log_err("unorm2_getDecomposition(fcc, space) failed\n");
1496 }
1497 errorCode=U_ZERO_ERROR;
1498 length=unorm2_getDecomposition(n2, 0xe4, decomp, LENGTHOF(decomp), &errorCode);
1499 if(U_FAILURE(errorCode) || length!=2 || decomp[0]!=0x61 || decomp[1]!=0x308 || decomp[2]!=0) {
1500 log_err("unorm2_getDecomposition(fcc, a-umlaut) failed\n");
1501 }
1502 errorCode=U_ZERO_ERROR;
1503 length=unorm2_getDecomposition(n2, 0xac01, decomp, LENGTHOF(decomp), &errorCode);
1504 if(U_FAILURE(errorCode) || length!=3 || decomp[0]!=0x1100 || decomp[1]!=0x1161 || decomp[2]!=0x11a8 || decomp[3]!=0) {
1505 log_err("unorm2_getDecomposition(fcc, Hangul syllable U+AC01) failed\n");
1506 }
1507 errorCode=U_ZERO_ERROR;
1508 length=unorm2_getDecomposition(n2, 0xac01, NULL, 0, &errorCode);
1509 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=3) {
1510 log_err("unorm2_getDecomposition(fcc, Hangul syllable U+AC01) overflow failed\n");
1511 }
1512 errorCode=U_ZERO_ERROR;
1513 length=unorm2_getDecomposition(n2, 0xac01, decomp, -1, &errorCode);
1514 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1515 log_err("unorm2_getDecomposition(fcc, capacity<0) failed\n");
1516 }
1517 errorCode=U_ZERO_ERROR;
1518 length=unorm2_getDecomposition(n2, 0xac01, NULL, 4, &errorCode);
1519 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1520 log_err("unorm2_getDecomposition(fcc, decomposition=NULL) failed\n");
1521 }
1522 }
1523
1524 static void
1525 TestGetRawDecomposition() {
1526 UChar decomp[32];
1527 int32_t length;
1528
1529 UErrorCode errorCode=U_ZERO_ERROR;
1530 const UNormalizer2 *n2=unorm2_getNFKCInstance(&errorCode);
1531 if(U_FAILURE(errorCode)) {
1532 log_err_status(errorCode, "unorm2_getNFKCInstance() failed: %s\n", u_errorName(errorCode));
1533 return;
1534 }
1535 /*
1536 * Raw decompositions from NFKC data are the Unicode Decomposition_Mapping values,
1537 * without recursive decomposition.
1538 */
1539
1540 length=unorm2_getRawDecomposition(n2, 0x20, decomp, LENGTHOF(decomp), &errorCode);
1541 if(U_FAILURE(errorCode) || length>=0) {
1542 log_err("unorm2_getDecomposition(nfkc, space) failed\n");
1543 }
1544 errorCode=U_ZERO_ERROR;
1545 length=unorm2_getRawDecomposition(n2, 0xe4, decomp, LENGTHOF(decomp), &errorCode);
1546 if(U_FAILURE(errorCode) || length!=2 || decomp[0]!=0x61 || decomp[1]!=0x308 || decomp[2]!=0) {
1547 log_err("unorm2_getDecomposition(nfkc, a-umlaut) failed\n");
1548 }
1549 /* U+1E08 LATIN CAPITAL LETTER C WITH CEDILLA AND ACUTE */
1550 errorCode=U_ZERO_ERROR;
1551 length=unorm2_getRawDecomposition(n2, 0x1e08, decomp, LENGTHOF(decomp), &errorCode);
1552 if(U_FAILURE(errorCode) || length!=2 || decomp[0]!=0xc7 || decomp[1]!=0x301 || decomp[2]!=0) {
1553 log_err("unorm2_getDecomposition(nfkc, c-cedilla-acute) failed\n");
1554 }
1555 /* U+212B ANGSTROM SIGN */
1556 errorCode=U_ZERO_ERROR;
1557 length=unorm2_getRawDecomposition(n2, 0x212b, decomp, LENGTHOF(decomp), &errorCode);
1558 if(U_FAILURE(errorCode) || length!=1 || decomp[0]!=0xc5 || decomp[1]!=0) {
1559 log_err("unorm2_getDecomposition(nfkc, angstrom sign) failed\n");
1560 }
1561 errorCode=U_ZERO_ERROR;
1562 length=unorm2_getRawDecomposition(n2, 0xac00, decomp, LENGTHOF(decomp), &errorCode);
1563 if(U_FAILURE(errorCode) || length!=2 || decomp[0]!=0x1100 || decomp[1]!=0x1161 || decomp[2]!=0) {
1564 log_err("unorm2_getDecomposition(nfkc, Hangul syllable U+AC00) failed\n");
1565 }
1566 /* A Hangul LVT syllable has a raw decomposition of an LV syllable + T. */
1567 errorCode=U_ZERO_ERROR;
1568 length=unorm2_getRawDecomposition(n2, 0xac01, decomp, LENGTHOF(decomp), &errorCode);
1569 if(U_FAILURE(errorCode) || length!=2 || decomp[0]!=0xac00 || decomp[1]!=0x11a8 || decomp[2]!=0) {
1570 log_err("unorm2_getDecomposition(nfkc, Hangul syllable U+AC01) failed\n");
1571 }
1572 errorCode=U_ZERO_ERROR;
1573 length=unorm2_getRawDecomposition(n2, 0xac01, NULL, 0, &errorCode);
1574 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=2) {
1575 log_err("unorm2_getDecomposition(nfkc, Hangul syllable U+AC01) overflow failed\n");
1576 }
1577 errorCode=U_ZERO_ERROR;
1578 length=unorm2_getRawDecomposition(n2, 0xac01, decomp, -1, &errorCode);
1579 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1580 log_err("unorm2_getDecomposition(nfkc, capacity<0) failed\n");
1581 }
1582 errorCode=U_ZERO_ERROR;
1583 length=unorm2_getRawDecomposition(n2, 0xac01, NULL, 4, &errorCode);
1584 if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
1585 log_err("unorm2_getDecomposition(nfkc, decomposition=NULL) failed\n");
1586 }
1587 }
1588
1589 static void
1590 TestAppendRestoreMiddle() {
1591 UChar a[20]={ 0x61, 0x62, 0x63, 0x41, 0x327, 0 }; /* last chars are 'A' and 'cedilla' NFC */
1592 static const UChar b[]={ 0x30A, 0x64, 0x65, 0x66, 0 }; /* first char is 'ring above' NFC */
1593 /* NFC: C5 is 'A with ring above' */
1594 static const UChar expected[]={ 0x61, 0x62, 0x63, 0xC5, 0x327, 0x64, 0x65, 0x66 };
1595 int32_t length;
1596 UErrorCode errorCode=U_ZERO_ERROR;
1597 const UNormalizer2 *n2=unorm2_getNFCInstance(&errorCode);
1598 if(U_FAILURE(errorCode)) {
1599 log_err_status(errorCode, "unorm2_getNFCInstance() failed: %s\n", u_errorName(errorCode));
1600 return;
1601 }
1602 /*
1603 * Use length=-1 to fool the estimate of the ReorderingBuffer capacity.
1604 * Use a capacity of 6 or 7 so that the middle sequence <41 327 30A>
1605 * still fits into a[] but the full result still overflows this capacity.
1606 * (Let it modify the destination buffer before reallocating internally.)
1607 */
1608 length=unorm2_append(n2, a, -1, 6, b, -1, &errorCode);
1609 if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=LENGTHOF(expected)) {
1610 log_err("unorm2_append(preflight) returned wrong length of %d\n", (int)length);
1611 return;
1612 }
1613 /* Verify that the middle is unchanged or restored. (ICU ticket #7848) */
1614 if(a[0]!=0x61 || a[1]!=0x62 || a[2]!=0x63 || a[3]!=0x41 || a[4]!=0x327 || a[5]!=0) {
1615 log_err("unorm2_append(overflow) modified the first string\n");
1616 return;
1617 }
1618 errorCode=U_ZERO_ERROR;
1619 length=unorm2_append(n2, a, -1, LENGTHOF(a), b, -1, &errorCode);
1620 if(U_FAILURE(errorCode) || length!=LENGTHOF(expected) || 0!=u_memcmp(a, expected, length)) {
1621 log_err("unorm2_append(real) failed - %s, length %d\n", u_errorName(errorCode), (int)length);
1622 return;
1623 }
1624 }
1625
1626 static void
1627 TestGetEasyToUseInstance() {
1628 static const UChar in[]={
1629 0xA0, /* -> <noBreak> 0020 */
1630 0xC7, 0x301 /* = 1E08 = 0043 0327 0301 */
1631 };
1632 UChar out[32];
1633 int32_t length;
1634
1635 UErrorCode errorCode=U_ZERO_ERROR;
1636 const UNormalizer2 *n2=unorm2_getNFCInstance(&errorCode);
1637 if(U_FAILURE(errorCode)) {
1638 log_err_status(errorCode, "unorm2_getNFCInstance() failed: %s\n", u_errorName(errorCode));
1639 return;
1640 }
1641 length=unorm2_normalize(n2, in, LENGTHOF(in), out, LENGTHOF(out), &errorCode);
1642 if(U_FAILURE(errorCode) || length!=2 || out[0]!=0xa0 || out[1]!=0x1e08) {
1643 log_err("unorm2_getNFCInstance() did not return an NFC instance (normalized length=%d; %s)\n",
1644 (int)length, u_errorName(errorCode));
1645 }
1646
1647 errorCode=U_ZERO_ERROR;
1648 n2=unorm2_getNFDInstance(&errorCode);
1649 if(U_FAILURE(errorCode)) {
1650 log_err_status(errorCode, "unorm2_getNFDInstance() failed: %s\n", u_errorName(errorCode));
1651 return;
1652 }
1653 length=unorm2_normalize(n2, in, LENGTHOF(in), out, LENGTHOF(out), &errorCode);
1654 if(U_FAILURE(errorCode) || length!=4 || out[0]!=0xa0 || out[1]!=0x43 || out[2]!=0x327 || out[3]!=0x301) {
1655 log_err("unorm2_getNFDInstance() did not return an NFD instance (normalized length=%d; %s)\n",
1656 (int)length, u_errorName(errorCode));
1657 }
1658
1659 errorCode=U_ZERO_ERROR;
1660 n2=unorm2_getNFKCInstance(&errorCode);
1661 if(U_FAILURE(errorCode)) {
1662 log_err_status(errorCode, "unorm2_getNFKCInstance() failed: %s\n", u_errorName(errorCode));
1663 return;
1664 }
1665 length=unorm2_normalize(n2, in, LENGTHOF(in), out, LENGTHOF(out), &errorCode);
1666 if(U_FAILURE(errorCode) || length!=2 || out[0]!=0x20 || out[1]!=0x1e08) {
1667 log_err("unorm2_getNFKCInstance() did not return an NFKC instance (normalized length=%d; %s)\n",
1668 (int)length, u_errorName(errorCode));
1669 }
1670
1671 errorCode=U_ZERO_ERROR;
1672 n2=unorm2_getNFKDInstance(&errorCode);
1673 if(U_FAILURE(errorCode)) {
1674 log_err_status(errorCode, "unorm2_getNFKDInstance() failed: %s\n", u_errorName(errorCode));
1675 return;
1676 }
1677 length=unorm2_normalize(n2, in, LENGTHOF(in), out, LENGTHOF(out), &errorCode);
1678 if(U_FAILURE(errorCode) || length!=4 || out[0]!=0x20 || out[1]!=0x43 || out[2]!=0x327 || out[3]!=0x301) {
1679 log_err("unorm2_getNFKDInstance() did not return an NFKD instance (normalized length=%d; %s)\n",
1680 (int)length, u_errorName(errorCode));
1681 }
1682
1683 errorCode=U_ZERO_ERROR;
1684 n2=unorm2_getNFKCCasefoldInstance(&errorCode);
1685 if(U_FAILURE(errorCode)) {
1686 log_err_status(errorCode, "unorm2_getNFKCCasefoldInstance() failed: %s\n", u_errorName(errorCode));
1687 return;
1688 }
1689 length=unorm2_normalize(n2, in, LENGTHOF(in), out, LENGTHOF(out), &errorCode);
1690 if(U_FAILURE(errorCode) || length!=2 || out[0]!=0x20 || out[1]!=0x1e09) {
1691 log_err("unorm2_getNFKCCasefoldInstance() did not return an NFKC_Casefold instance (normalized length=%d; %s)\n",
1692 (int)length, u_errorName(errorCode));
1693 }
1694 }
1695
1696 #endif /* #if !UCONFIG_NO_NORMALIZATION */