]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 2002, International Business Machines | |
5 | * Corporation and others. All Rights Reserved. | |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: cstrcase.c | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2002feb21 | |
14 | * created by: Markus W. Scherer | |
15 | * | |
16 | * Test file for string casing C API functions. | |
17 | */ | |
18 | ||
19 | #include "unicode/utypes.h" | |
20 | #include "unicode/uchar.h" | |
21 | #include "unicode/ustring.h" | |
22 | #include "unicode/uloc.h" | |
23 | #include "unicode/ubrk.h" | |
24 | #include "cmemory.h" | |
25 | #include "cintltst.h" | |
26 | #include "cucdtst.h" | |
27 | ||
28 | /* test string case mapping functions --------------------------------------- */ | |
29 | ||
30 | U_CFUNC void | |
31 | TestCaseLower() { | |
32 | static const UChar | |
33 | ||
34 | beforeLower[]= { 0x61, 0x42, 0x49, 0x3a3, 0xdf, 0x3a3, 0x2f, 0xd93f, 0xdfff }, | |
35 | lowerRoot[]= { 0x61, 0x62, 0x69, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff }, | |
36 | lowerTurkish[]={ 0x61, 0x62, 0x131, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff }; | |
37 | ||
38 | UChar buffer[32]; | |
39 | int32_t length; | |
40 | UErrorCode errorCode; | |
41 | ||
42 | /* lowercase with root locale and separate buffers */ | |
43 | buffer[0]=0xabcd; | |
44 | errorCode=U_ZERO_ERROR; | |
45 | length=u_strToLower(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
46 | beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, | |
47 | "", | |
48 | &errorCode); | |
49 | if( U_FAILURE(errorCode) || | |
50 | length!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) || | |
51 | uprv_memcmp(lowerRoot, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
52 | buffer[length]!=0 | |
53 | ) { | |
54 | log_err("error in u_strToLower(root locale)=%ld error=%s string matches: %s\t\nlowerRoot=%s\t\nbuffer=%s\n", | |
55 | length, | |
56 | u_errorName(errorCode), | |
57 | uprv_memcmp(lowerRoot, buffer, length*U_SIZEOF_UCHAR)==0 && | |
58 | buffer[length]==0 ? "yes" : "no", | |
59 | aescstrdup(lowerRoot,-1), | |
60 | aescstrdup(buffer,-1)); | |
61 | } | |
62 | ||
63 | /* lowercase with turkish locale and in the same buffer */ | |
64 | uprv_memcpy(buffer, beforeLower, sizeof(beforeLower)); | |
65 | buffer[sizeof(beforeLower)/U_SIZEOF_UCHAR]=0; | |
66 | errorCode=U_ZERO_ERROR; | |
67 | length=u_strToLower(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
68 | buffer, -1, /* implicit srcLength */ | |
69 | "tr", | |
70 | &errorCode); | |
71 | if( U_FAILURE(errorCode) || | |
72 | length!=(sizeof(lowerTurkish)/U_SIZEOF_UCHAR) || | |
73 | uprv_memcmp(lowerTurkish, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
74 | buffer[length]!=0 | |
75 | ) { | |
76 | log_err("error in u_strToLower(turkish locale)=%ld error=%s string matches: %s\n", | |
77 | length, | |
78 | u_errorName(errorCode), | |
79 | uprv_memcmp(lowerTurkish, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
80 | } | |
81 | ||
82 | /* test preflighting */ | |
83 | buffer[0]=buffer[2]=0xabcd; | |
84 | errorCode=U_ZERO_ERROR; | |
85 | length=u_strToLower(buffer, 2, /* set destCapacity=2 */ | |
86 | beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, | |
87 | "", | |
88 | &errorCode); | |
89 | if( errorCode!=U_BUFFER_OVERFLOW_ERROR || | |
90 | length!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) || | |
91 | uprv_memcmp(lowerRoot, buffer, 2*U_SIZEOF_UCHAR)!=0 || | |
92 | buffer[2]!=0xabcd | |
93 | ) { | |
94 | log_err("error in u_strToLower(root locale preflighting)=%ld error=%s string matches: %s\n", | |
95 | length, | |
96 | u_errorName(errorCode), | |
97 | uprv_memcmp(lowerRoot, buffer, 2*U_SIZEOF_UCHAR)==0 && buffer[2]==0xabcd ? "yes" : "no"); | |
98 | } | |
99 | ||
100 | /* test error handling */ | |
101 | errorCode=U_ZERO_ERROR; | |
102 | length=u_strToLower(NULL, sizeof(buffer)/U_SIZEOF_UCHAR, | |
103 | beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, | |
104 | "", | |
105 | &errorCode); | |
106 | if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) { | |
107 | log_err("error in u_strToLower(root locale dest=NULL)=%ld error=%s\n", | |
108 | length, | |
109 | u_errorName(errorCode)); | |
110 | } | |
111 | ||
112 | buffer[0]=0xabcd; | |
113 | errorCode=U_ZERO_ERROR; | |
114 | length=u_strToLower(buffer, -1, | |
115 | beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, | |
116 | "", | |
117 | &errorCode); | |
118 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
119 | buffer[0]!=0xabcd | |
120 | ) { | |
121 | log_err("error in u_strToLower(root locale destCapacity=-1)=%ld error=%s buffer[0]==0x%lx\n", | |
122 | length, | |
123 | u_errorName(errorCode), | |
124 | buffer[0]); | |
125 | } | |
126 | } | |
127 | ||
128 | U_CFUNC void | |
129 | TestCaseUpper() { | |
130 | static const UChar | |
131 | ||
132 | beforeUpper[]= { 0x61, 0x42, 0x69, 0x3c2, 0xdf, 0x3c3, 0x2f, 0xfb03, 0xd93f, 0xdfff }, | |
133 | upperRoot[]= { 0x41, 0x42, 0x49, 0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0xd93f, 0xdfff }, | |
134 | upperTurkish[]={ 0x41, 0x42, 0x130, 0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0xd93f, 0xdfff }; | |
135 | ||
136 | UChar buffer[32]; | |
137 | int32_t length; | |
138 | UErrorCode errorCode; | |
139 | ||
140 | /* uppercase with root locale and in the same buffer */ | |
141 | uprv_memcpy(buffer, beforeUpper, sizeof(beforeUpper)); | |
142 | errorCode=U_ZERO_ERROR; | |
143 | length=u_strToUpper(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
144 | buffer, sizeof(beforeUpper)/U_SIZEOF_UCHAR, | |
145 | "", | |
146 | &errorCode); | |
147 | if( U_FAILURE(errorCode) || | |
148 | length!=(sizeof(upperRoot)/U_SIZEOF_UCHAR) || | |
149 | uprv_memcmp(upperRoot, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
150 | buffer[length]!=0 | |
151 | ) { | |
152 | log_err("error in u_strToUpper(root locale)=%ld error=%s string matches: %s\n", | |
153 | length, | |
154 | u_errorName(errorCode), | |
155 | uprv_memcmp(upperRoot, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
156 | } | |
157 | ||
158 | /* uppercase with turkish locale and separate buffers */ | |
159 | buffer[0]=0xabcd; | |
160 | errorCode=U_ZERO_ERROR; | |
161 | length=u_strToUpper(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
162 | beforeUpper, sizeof(beforeUpper)/U_SIZEOF_UCHAR, | |
163 | "tr", | |
164 | &errorCode); | |
165 | if( U_FAILURE(errorCode) || | |
166 | length!=(sizeof(upperTurkish)/U_SIZEOF_UCHAR) || | |
167 | uprv_memcmp(upperTurkish, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
168 | buffer[length]!=0 | |
169 | ) { | |
170 | log_err("error in u_strToUpper(turkish locale)=%ld error=%s string matches: %s\n", | |
171 | length, | |
172 | u_errorName(errorCode), | |
173 | uprv_memcmp(upperTurkish, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
174 | } | |
175 | ||
176 | /* test preflighting */ | |
177 | errorCode=U_ZERO_ERROR; | |
178 | length=u_strToUpper(NULL, 0, | |
179 | beforeUpper, sizeof(beforeUpper)/U_SIZEOF_UCHAR, | |
180 | "tr", | |
181 | &errorCode); | |
182 | if( errorCode!=U_BUFFER_OVERFLOW_ERROR || | |
183 | length!=(sizeof(upperTurkish)/U_SIZEOF_UCHAR) | |
184 | ) { | |
185 | log_err("error in u_strToUpper(turkish locale pure preflighting)=%ld error=%s\n", | |
186 | length, | |
187 | u_errorName(errorCode)); | |
188 | } | |
189 | ||
190 | /* test error handling */ | |
191 | buffer[0]=0xabcd; | |
192 | errorCode=U_ZERO_ERROR; | |
193 | length=u_strToUpper(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
194 | NULL, sizeof(beforeUpper)/U_SIZEOF_UCHAR, | |
195 | "tr", | |
196 | &errorCode); | |
197 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
198 | buffer[0]!=0xabcd | |
199 | ) { | |
200 | log_err("error in u_strToUpper(turkish locale src=NULL)=%ld error=%s buffer[0]==0x%lx\n", | |
201 | length, | |
202 | u_errorName(errorCode), | |
203 | buffer[0]); | |
204 | } | |
205 | ||
206 | buffer[0]=0xabcd; | |
207 | errorCode=U_ZERO_ERROR; | |
208 | length=u_strToUpper(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
209 | beforeUpper, -2, | |
210 | "tr", | |
211 | &errorCode); | |
212 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
213 | buffer[0]!=0xabcd | |
214 | ) { | |
215 | log_err("error in u_strToUpper(turkish locale srcLength=-2)=%ld error=%s buffer[0]==0x%lx\n", | |
216 | length, | |
217 | u_errorName(errorCode), | |
218 | buffer[0]); | |
219 | } | |
220 | } | |
221 | ||
222 | #if !UCONFIG_NO_BREAK_ITERATION | |
223 | ||
224 | U_CFUNC void | |
225 | TestCaseTitle() { | |
226 | static const UChar | |
227 | ||
228 | beforeTitle[]= { 0x61, 0x42, 0x20, 0x69, 0x3c2, 0x20, 0xdf, 0x3c3, 0x2f, 0xfb03, 0xd93f, 0xdfff }, | |
229 | titleWord[]= { 0x41, 0x62, 0x20, 0x49, 0x3c2, 0x20, 0x53, 0x73, 0x3c3, 0x2f, 0x46, 0x66, 0x69, 0xd93f, 0xdfff }, | |
230 | titleChar[]= { 0x41, 0x42, 0x20, 0x49, 0x3a3, 0x20, 0x53, 0x73, 0x3a3, 0x2f, 0x46, 0x66, 0x69, 0xd93f, 0xdfff }; | |
231 | ||
232 | UChar buffer[32]; | |
233 | UBreakIterator *titleIterChars; | |
234 | int32_t length; | |
235 | UErrorCode errorCode; | |
236 | ||
237 | errorCode=U_ZERO_ERROR; | |
238 | titleIterChars=ubrk_open(UBRK_CHARACTER, "", beforeTitle, sizeof(beforeTitle)/U_SIZEOF_UCHAR, &errorCode); | |
239 | if(U_FAILURE(errorCode)) { | |
240 | log_err("error: ubrk_open(UBRK_CHARACTER)->%s\n", u_errorName(errorCode)); | |
241 | return; | |
242 | } | |
243 | ||
244 | /* titlecase with standard break iterator and in the same buffer */ | |
245 | uprv_memcpy(buffer, beforeTitle, sizeof(beforeTitle)); | |
246 | errorCode=U_ZERO_ERROR; | |
247 | length=u_strToTitle(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
248 | buffer, sizeof(beforeTitle)/U_SIZEOF_UCHAR, | |
249 | NULL, "", | |
250 | &errorCode); | |
251 | if( U_FAILURE(errorCode) || | |
252 | length!=(sizeof(titleWord)/U_SIZEOF_UCHAR) || | |
253 | uprv_memcmp(titleWord, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
254 | buffer[length]!=0 | |
255 | ) { | |
256 | log_err("error in u_strToTitle(standard iterator)=%ld error=%s string matches: %s\n", | |
257 | length, | |
258 | u_errorName(errorCode), | |
259 | uprv_memcmp(titleWord, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
260 | } | |
261 | ||
262 | /* titlecase with UBRK_CHARACTERS and separate buffers */ | |
263 | buffer[0]=0xabcd; | |
264 | errorCode=U_ZERO_ERROR; | |
265 | length=u_strToTitle(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
266 | beforeTitle, sizeof(beforeTitle)/U_SIZEOF_UCHAR, | |
267 | titleIterChars, "", | |
268 | &errorCode); | |
269 | if( U_FAILURE(errorCode) || | |
270 | length!=(sizeof(titleChar)/U_SIZEOF_UCHAR) || | |
271 | uprv_memcmp(titleChar, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
272 | buffer[length]!=0 | |
273 | ) { | |
274 | log_err("error in u_strToTitle(UBRK_CHARACTERS)=%ld error=%s string matches: %s\n", | |
275 | length, | |
276 | u_errorName(errorCode), | |
277 | uprv_memcmp(titleChar, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
278 | } | |
279 | ||
280 | /* test preflighting */ | |
281 | errorCode=U_ZERO_ERROR; | |
282 | length=u_strToTitle(NULL, 0, | |
283 | beforeTitle, sizeof(beforeTitle)/U_SIZEOF_UCHAR, | |
284 | titleIterChars, "", | |
285 | &errorCode); | |
286 | if( errorCode!=U_BUFFER_OVERFLOW_ERROR || | |
287 | length!=(sizeof(titleChar)/U_SIZEOF_UCHAR) | |
288 | ) { | |
289 | log_err("error in u_strToTitle(UBRK_CHARACTERS pure preflighting)=%ld error=%s\n", | |
290 | length, | |
291 | u_errorName(errorCode)); | |
292 | } | |
293 | ||
294 | /* test error handling */ | |
295 | buffer[0]=0xabcd; | |
296 | errorCode=U_ZERO_ERROR; | |
297 | length=u_strToTitle(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
298 | NULL, sizeof(beforeTitle)/U_SIZEOF_UCHAR, | |
299 | titleIterChars, "", | |
300 | &errorCode); | |
301 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
302 | buffer[0]!=0xabcd | |
303 | ) { | |
304 | log_err("error in u_strToTitle(UBRK_CHARACTERS src=NULL)=%ld error=%s buffer[0]==0x%lx\n", | |
305 | length, | |
306 | u_errorName(errorCode), | |
307 | buffer[0]); | |
308 | } | |
309 | ||
310 | buffer[0]=0xabcd; | |
311 | errorCode=U_ZERO_ERROR; | |
312 | length=u_strToTitle(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
313 | beforeTitle, -2, | |
314 | titleIterChars, "", | |
315 | &errorCode); | |
316 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
317 | buffer[0]!=0xabcd | |
318 | ) { | |
319 | log_err("error in u_strToTitle(UBRK_CHARACTERS srcLength=-2)=%ld error=%s buffer[0]==0x%lx\n", | |
320 | length, | |
321 | u_errorName(errorCode), | |
322 | buffer[0]); | |
323 | } | |
324 | ||
325 | ubrk_close(titleIterChars); | |
326 | } | |
327 | ||
328 | #endif | |
329 | ||
330 | /* test case folding and case-insensitive string compare -------------------- */ | |
331 | ||
332 | U_CFUNC void | |
333 | TestCaseFolding() { | |
334 | static const UChar32 | |
335 | simple[]={ | |
336 | /* input, default, exclude special i */ | |
337 | 0x61, 0x61, 0x61, | |
338 | 0x49, 0x69, 0x131, | |
339 | 0x130, 0x69, 0x69, | |
340 | 0x131, 0x131, 0x131, | |
341 | 0xdf, 0xdf, 0xdf, | |
342 | 0xfb03, 0xfb03, 0xfb03, | |
343 | 0x1040e,0x10436,0x10436, | |
344 | 0x5ffff,0x5ffff,0x5ffff | |
345 | }; | |
346 | ||
347 | static const UChar | |
348 | mixed[]= { 0x61, 0x42, 0x130, 0x49, 0x131, 0x3d0, 0xdf, 0xfb03, 0xd93f, 0xdfff }, | |
349 | foldedDefault[]= { 0x61, 0x62, 0x69, 0x307, 0x69, 0x131, 0x3b2, 0x73, 0x73, 0x66, 0x66, 0x69, 0xd93f, 0xdfff }, | |
350 | foldedExcludeSpecialI[]={ 0x61, 0x62, 0x69, 0x131, 0x131, 0x3b2, 0x73, 0x73, 0x66, 0x66, 0x69, 0xd93f, 0xdfff }; | |
351 | ||
352 | UVersionInfo unicodeVersion={ 0, 0, 17, 89 }, unicode_3_1={ 3, 1, 0, 0 }; | |
353 | ||
354 | const UChar32 *p; | |
355 | int32_t i; | |
356 | ||
357 | UChar buffer[32]; | |
358 | int32_t length; | |
359 | UErrorCode errorCode; | |
360 | UBool isUnicode_3_1; | |
361 | ||
362 | /* if unicodeVersion()>=3.1 then test exclude-special-i cases as well */ | |
363 | u_getUnicodeVersion(unicodeVersion); | |
364 | isUnicode_3_1= uprv_memcmp(unicodeVersion, unicode_3_1, 4)>=0; | |
365 | ||
366 | /* test simple case folding */ | |
367 | p=simple; | |
368 | for(i=0; i<sizeof(simple)/12; p+=3, ++i) { | |
369 | if(u_foldCase(p[0], U_FOLD_CASE_DEFAULT)!=p[1]) { | |
370 | log_err("error: u_foldCase(0x%04lx, default)=0x%04lx instead of 0x%04lx\n", | |
371 | p[0], u_foldCase(p[0], U_FOLD_CASE_DEFAULT), p[1]); | |
372 | return; | |
373 | } | |
374 | ||
375 | if(isUnicode_3_1 && u_foldCase(p[0], U_FOLD_CASE_EXCLUDE_SPECIAL_I)!=p[2]) { | |
376 | log_err("error: u_foldCase(0x%04lx, exclude special i)=0x%04lx instead of 0x%04lx\n", | |
377 | p[0], u_foldCase(p[0], U_FOLD_CASE_EXCLUDE_SPECIAL_I), p[2]); | |
378 | return; | |
379 | } | |
380 | } | |
381 | ||
382 | /* test full string case folding with default option and separate buffers */ | |
383 | buffer[0]=0xabcd; | |
384 | errorCode=U_ZERO_ERROR; | |
385 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
386 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
387 | U_FOLD_CASE_DEFAULT, | |
388 | &errorCode); | |
389 | if( U_FAILURE(errorCode) || | |
390 | length!=(sizeof(foldedDefault)/U_SIZEOF_UCHAR) || | |
391 | uprv_memcmp(foldedDefault, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
392 | buffer[length]!=0 | |
393 | ) { | |
394 | log_err("error in u_strFoldCase(default)=%ld error=%s string matches: %s\n", | |
395 | length, | |
396 | u_errorName(errorCode), | |
397 | uprv_memcmp(foldedDefault, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
398 | } | |
399 | ||
400 | /* exclude special i */ | |
401 | if(isUnicode_3_1) { | |
402 | buffer[0]=0xabcd; | |
403 | errorCode=U_ZERO_ERROR; | |
404 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
405 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
406 | U_FOLD_CASE_EXCLUDE_SPECIAL_I, | |
407 | &errorCode); | |
408 | if( U_FAILURE(errorCode) || | |
409 | length!=(sizeof(foldedExcludeSpecialI)/U_SIZEOF_UCHAR) || | |
410 | uprv_memcmp(foldedExcludeSpecialI, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
411 | buffer[length]!=0 | |
412 | ) { | |
413 | log_err("error in u_strFoldCase(exclude special i)=%ld error=%s string matches: %s\n", | |
414 | length, | |
415 | u_errorName(errorCode), | |
416 | uprv_memcmp(foldedExcludeSpecialI, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
417 | } | |
418 | } | |
419 | ||
420 | /* test full string case folding with default option and in the same buffer */ | |
421 | uprv_memcpy(buffer, mixed, sizeof(mixed)); | |
422 | buffer[sizeof(mixed)/U_SIZEOF_UCHAR]=0; | |
423 | errorCode=U_ZERO_ERROR; | |
424 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
425 | buffer, -1, /* implicit srcLength */ | |
426 | U_FOLD_CASE_DEFAULT, | |
427 | &errorCode); | |
428 | if( U_FAILURE(errorCode) || | |
429 | length!=(sizeof(foldedDefault)/U_SIZEOF_UCHAR) || | |
430 | uprv_memcmp(foldedDefault, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
431 | buffer[length]!=0 | |
432 | ) { | |
433 | log_err("error in u_strFoldCase(default same buffer)=%ld error=%s string matches: %s\n", | |
434 | length, | |
435 | u_errorName(errorCode), | |
436 | uprv_memcmp(foldedDefault, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
437 | } | |
438 | ||
439 | /* test full string case folding, exclude special i, in the same buffer */ | |
440 | if(isUnicode_3_1) { | |
441 | uprv_memcpy(buffer, mixed, sizeof(mixed)); | |
442 | errorCode=U_ZERO_ERROR; | |
443 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
444 | buffer, sizeof(mixed)/U_SIZEOF_UCHAR, | |
445 | U_FOLD_CASE_EXCLUDE_SPECIAL_I, | |
446 | &errorCode); | |
447 | if( U_FAILURE(errorCode) || | |
448 | length!=(sizeof(foldedExcludeSpecialI)/U_SIZEOF_UCHAR) || | |
449 | uprv_memcmp(foldedExcludeSpecialI, buffer, length*U_SIZEOF_UCHAR)!=0 || | |
450 | buffer[length]!=0 | |
451 | ) { | |
452 | log_err("error in u_strFoldCase(exclude special i same buffer)=%ld error=%s string matches: %s\n", | |
453 | length, | |
454 | u_errorName(errorCode), | |
455 | uprv_memcmp(foldedExcludeSpecialI, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); | |
456 | } | |
457 | } | |
458 | ||
459 | /* test preflighting */ | |
460 | buffer[0]=buffer[2]=0xabcd; | |
461 | errorCode=U_ZERO_ERROR; | |
462 | length=u_strFoldCase(buffer, 2, /* set destCapacity=2 */ | |
463 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
464 | U_FOLD_CASE_DEFAULT, | |
465 | &errorCode); | |
466 | if( errorCode!=U_BUFFER_OVERFLOW_ERROR || | |
467 | length!=(sizeof(foldedDefault)/U_SIZEOF_UCHAR) || | |
468 | uprv_memcmp(foldedDefault, buffer, 2*U_SIZEOF_UCHAR)!=0 || | |
469 | buffer[2]!=0xabcd | |
470 | ) { | |
471 | log_err("error in u_strFoldCase(default preflighting)=%ld error=%s string matches: %s\n", | |
472 | length, | |
473 | u_errorName(errorCode), | |
474 | uprv_memcmp(foldedDefault, buffer, 2*U_SIZEOF_UCHAR)==0 && buffer[2]==0xabcd ? "yes" : "no"); | |
475 | } | |
476 | ||
477 | errorCode=U_ZERO_ERROR; | |
478 | length=u_strFoldCase(NULL, 0, | |
479 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
480 | U_FOLD_CASE_DEFAULT, | |
481 | &errorCode); | |
482 | if( errorCode!=U_BUFFER_OVERFLOW_ERROR || | |
483 | length!=(sizeof(foldedDefault)/U_SIZEOF_UCHAR) | |
484 | ) { | |
485 | log_err("error in u_strFoldCase(default pure preflighting)=%ld error=%s\n", | |
486 | length, | |
487 | u_errorName(errorCode)); | |
488 | } | |
489 | ||
490 | /* test error handling */ | |
491 | errorCode=U_ZERO_ERROR; | |
492 | length=u_strFoldCase(NULL, sizeof(buffer)/U_SIZEOF_UCHAR, | |
493 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
494 | U_FOLD_CASE_DEFAULT, | |
495 | &errorCode); | |
496 | if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) { | |
497 | log_err("error in u_strFoldCase(default dest=NULL)=%ld error=%s\n", | |
498 | length, | |
499 | u_errorName(errorCode)); | |
500 | } | |
501 | ||
502 | buffer[0]=0xabcd; | |
503 | errorCode=U_ZERO_ERROR; | |
504 | length=u_strFoldCase(buffer, -1, | |
505 | mixed, sizeof(mixed)/U_SIZEOF_UCHAR, | |
506 | U_FOLD_CASE_DEFAULT, | |
507 | &errorCode); | |
508 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
509 | buffer[0]!=0xabcd | |
510 | ) { | |
511 | log_err("error in u_strFoldCase(default destCapacity=-1)=%ld error=%s buffer[0]==0x%lx\n", | |
512 | length, | |
513 | u_errorName(errorCode), | |
514 | buffer[0]); | |
515 | } | |
516 | ||
517 | buffer[0]=0xabcd; | |
518 | errorCode=U_ZERO_ERROR; | |
519 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
520 | NULL, sizeof(mixed)/U_SIZEOF_UCHAR, | |
521 | U_FOLD_CASE_EXCLUDE_SPECIAL_I, | |
522 | &errorCode); | |
523 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
524 | buffer[0]!=0xabcd | |
525 | ) { | |
526 | log_err("error in u_strFoldCase(exclude special i src=NULL)=%ld error=%s buffer[0]==0x%lx\n", | |
527 | length, | |
528 | u_errorName(errorCode), | |
529 | buffer[0]); | |
530 | } | |
531 | ||
532 | buffer[0]=0xabcd; | |
533 | errorCode=U_ZERO_ERROR; | |
534 | length=u_strFoldCase(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, | |
535 | mixed, -2, | |
536 | U_FOLD_CASE_EXCLUDE_SPECIAL_I, | |
537 | &errorCode); | |
538 | if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || | |
539 | buffer[0]!=0xabcd | |
540 | ) { | |
541 | log_err("error in u_strFoldCase(exclude special i srcLength=-2)=%ld error=%s buffer[0]==0x%lx\n", | |
542 | length, | |
543 | u_errorName(errorCode), | |
544 | buffer[0]); | |
545 | } | |
546 | } | |
547 | ||
548 | U_CFUNC void | |
549 | TestCaseCompare() { | |
550 | static const UChar | |
551 | ||
552 | mixed[]= { 0x61, 0x42, 0x131, 0x3a3, 0xdf, 0xfb03, 0xd93f, 0xdfff, 0 }, | |
553 | otherDefault[]= { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x46, 0x66, 0x49, 0xd93f, 0xdfff, 0 }, | |
554 | otherExcludeSpecialI[]={ 0x41, 0x62, 0x131, 0x3c3, 0x53, 0x73, 0x66, 0x46, 0x69, 0xd93f, 0xdfff, 0 }, | |
555 | different[]= { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x46, 0x66, 0x49, 0xd93f, 0xdffd, 0 }; | |
556 | ||
557 | UVersionInfo unicodeVersion={ 0, 0, 17, 89 }, unicode_3_1={ 3, 1, 0, 0 }; | |
558 | ||
559 | int32_t result, lenMixed, lenOtherDefault, lenOtherExcludeSpecialI, lenDifferent; | |
560 | UErrorCode errorCode; | |
561 | UBool isUnicode_3_1; | |
562 | ||
563 | errorCode=U_ZERO_ERROR; | |
564 | ||
565 | lenMixed=u_strlen(mixed); | |
566 | lenOtherDefault=u_strlen(otherDefault); | |
567 | lenOtherExcludeSpecialI=u_strlen(otherExcludeSpecialI); | |
568 | lenDifferent=u_strlen(different); | |
569 | ||
570 | /* if unicodeVersion()>=3.1 then test exclude-special-i cases as well */ | |
571 | u_getUnicodeVersion(unicodeVersion); | |
572 | isUnicode_3_1= uprv_memcmp(unicodeVersion, unicode_3_1, 4)>=0; | |
573 | ||
574 | /* test u_strcasecmp() */ | |
575 | result=u_strcasecmp(mixed, otherDefault, U_FOLD_CASE_DEFAULT); | |
576 | if(result!=0) { | |
577 | log_err("error: u_strcasecmp(mixed, other, default)=%ld instead of 0\n", result); | |
578 | } | |
579 | result=u_strCaseCompare(mixed, -1, otherDefault, -1, U_FOLD_CASE_DEFAULT, &errorCode); | |
580 | if(result!=0) { | |
581 | log_err("error: u_strCaseCompare(mixed, other, default)=%ld instead of 0\n", result); | |
582 | } | |
583 | ||
584 | /* test u_strcasecmp() - exclude special i */ | |
585 | result=u_strcasecmp(mixed, otherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I); | |
586 | if(result!=0) { | |
587 | log_err("error: u_strcasecmp(mixed, other, exclude special i)=%ld instead of 0\n", result); | |
588 | } | |
589 | result=u_strCaseCompare(mixed, lenMixed, otherExcludeSpecialI, lenOtherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &errorCode); | |
590 | if(result!=0) { | |
591 | log_err("error: u_strCaseCompare(mixed, other, exclude special i)=%ld instead of 0\n", result); | |
592 | } | |
593 | ||
594 | /* test u_strcasecmp() */ | |
595 | result=u_strcasecmp(mixed, different, U_FOLD_CASE_DEFAULT); | |
596 | if(result<=0) { | |
597 | log_err("error: u_strcasecmp(mixed, different, default)=%ld instead of positive\n", result); | |
598 | } | |
599 | result=u_strCaseCompare(mixed, -1, different, lenDifferent, U_FOLD_CASE_DEFAULT, &errorCode); | |
600 | if(result<=0) { | |
601 | log_err("error: u_strCaseCompare(mixed, different, default)=%ld instead of positive\n", result); | |
602 | } | |
603 | ||
604 | /* test u_strncasecmp() - stop before the sharp s (U+00df) */ | |
605 | result=u_strncasecmp(mixed, different, 4, U_FOLD_CASE_DEFAULT); | |
606 | if(result!=0) { | |
607 | log_err("error: u_strncasecmp(mixed, different, 4, default)=%ld instead of 0\n", result); | |
608 | } | |
609 | result=u_strCaseCompare(mixed, 4, different, 4, U_FOLD_CASE_DEFAULT, &errorCode); | |
610 | if(result!=0) { | |
611 | log_err("error: u_strCaseCompare(mixed, 4, different, 4, default)=%ld instead of 0\n", result); | |
612 | } | |
613 | ||
614 | /* test u_strncasecmp() - stop in the middle of the sharp s (U+00df) */ | |
615 | result=u_strncasecmp(mixed, different, 5, U_FOLD_CASE_DEFAULT); | |
616 | if(result<=0) { | |
617 | log_err("error: u_strncasecmp(mixed, different, 5, default)=%ld instead of positive\n", result); | |
618 | } | |
619 | result=u_strCaseCompare(mixed, 5, different, 5, U_FOLD_CASE_DEFAULT, &errorCode); | |
620 | if(result<=0) { | |
621 | log_err("error: u_strCaseCompare(mixed, 5, different, 5, default)=%ld instead of positive\n", result); | |
622 | } | |
623 | ||
624 | /* test u_memcasecmp() - stop before the sharp s (U+00df) */ | |
625 | result=u_memcasecmp(mixed, different, 4, U_FOLD_CASE_DEFAULT); | |
626 | if(result!=0) { | |
627 | log_err("error: u_memcasecmp(mixed, different, 4, default)=%ld instead of 0\n", result); | |
628 | } | |
629 | ||
630 | /* test u_memcasecmp() - stop in the middle of the sharp s (U+00df) */ | |
631 | result=u_memcasecmp(mixed, different, 5, U_FOLD_CASE_DEFAULT); | |
632 | if(result<=0) { | |
633 | log_err("error: u_memcasecmp(mixed, different, 5, default)=%ld instead of positive\n", result); | |
634 | } | |
635 | } |