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