2 *******************************************************************************
4 * Copyright (C) 2002-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: strcase.cpp
10 * tab size: 8 (not used)
13 * created on: 2002mar12
14 * created by: Markus W. Scherer
16 * Test file for string casing C++ API functions.
19 #include "unicode/uchar.h"
20 #include "unicode/ures.h"
21 #include "unicode/uloc.h"
22 #include "unicode/locid.h"
23 #include "unicode/ubrk.h"
24 #include "unicode/unistr.h"
25 #include "unicode/ucasemap.h"
28 #include "unicode/tstdtmod.h"
30 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
32 StringCaseTest::~StringCaseTest() {}
35 StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
37 logln("TestSuite StringCaseTest: ");
40 TESTCASE_AUTO(TestCaseConversion);
41 #if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
42 TESTCASE_AUTO(TestCasing);
44 TESTCASE_AUTO(TestFullCaseFoldingIterator);
49 StringCaseTest::TestCaseConversion()
51 static const UChar uppercaseGreek[] =
52 { 0x399, 0x395, 0x3a3, 0x3a5, 0x3a3, 0x20, 0x03a7, 0x3a1, 0x399, 0x3a3, 0x3a4,
56 static const UChar lowercaseGreek[] =
57 { 0x3b9, 0x3b5, 0x3c3, 0x3c5, 0x3c2, 0x20, 0x03c7, 0x3c1, 0x3b9, 0x3c3, 0x3c4,
61 static const UChar lowercaseTurkish[] =
62 { 0x69, 0x73, 0x74, 0x61, 0x6e, 0x62, 0x75, 0x6c, 0x2c, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x63, 0x6f,
63 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x0131, 0x6e, 0x6f, 0x70, 0x6c, 0x65, 0x21, 0 };
65 static const UChar uppercaseTurkish[] =
66 { 0x54, 0x4f, 0x50, 0x4b, 0x41, 0x50, 0x49, 0x20, 0x50, 0x41, 0x4c, 0x41, 0x43, 0x45, 0x2c, 0x20,
67 0x0130, 0x53, 0x54, 0x41, 0x4e, 0x42, 0x55, 0x4c, 0 };
69 UnicodeString expectedResult;
72 test3 += (UChar32)0x0130;
73 test3 += "STANBUL, NOT CONSTANTINOPLE!";
75 UnicodeString test4(test3);
76 test4.toLower(Locale(""));
77 expectedResult = UnicodeString("i\\u0307stanbul, not constantinople!", "").unescape();
78 if (test4 != expectedResult)
79 errln("1. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
82 test4.toLower(Locale("tr", "TR"));
83 expectedResult = lowercaseTurkish;
84 if (test4 != expectedResult)
85 errln("2. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
88 test3 += (UChar32)0x0131;
89 test3 += " palace, istanbul";
92 test4.toUpper(Locale(""));
93 expectedResult = "TOPKAPI PALACE, ISTANBUL";
94 if (test4 != expectedResult)
95 errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
98 test4.toUpper(Locale("tr", "TR"));
99 expectedResult = uppercaseTurkish;
100 if (test4 != expectedResult)
101 errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
103 test3 = CharsToUnicodeString("S\\u00FC\\u00DFmayrstra\\u00DFe");
105 test3.toUpper(Locale("de", "DE"));
106 expectedResult = CharsToUnicodeString("S\\u00DCSSMAYRSTRASSE");
107 if (test3 != expectedResult)
108 errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test3 + "\".");
110 test4.replace(0, test4.length(), uppercaseGreek);
112 test4.toLower(Locale("el", "GR"));
113 expectedResult = lowercaseGreek;
114 if (test4 != expectedResult)
115 errln("toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
117 test4.replace(0, test4.length(), lowercaseGreek);
120 expectedResult = uppercaseGreek;
121 if (test4 != expectedResult)
122 errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
124 // more string case mapping tests with the new implementation
128 beforeLower[]= { 0x61, 0x42, 0x49, 0x3a3, 0xdf, 0x3a3, 0x2f, 0xd93f, 0xdfff },
129 lowerRoot[]= { 0x61, 0x62, 0x69, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff },
130 lowerTurkish[]={ 0x61, 0x62, 0x131, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff },
132 beforeUpper[]= { 0x61, 0x42, 0x69, 0x3c2, 0xdf, 0x3c3, 0x2f, 0xfb03, 0xfb03, 0xfb03, 0xd93f, 0xdfff },
133 upperRoot[]= { 0x41, 0x42, 0x49, 0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0xd93f, 0xdfff },
134 upperTurkish[]={ 0x41, 0x42, 0x130, 0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0xd93f, 0xdfff },
136 beforeMiniUpper[]= { 0xdf, 0x61 },
137 miniUpper[]= { 0x53, 0x53, 0x41 };
141 /* lowercase with root locale */
142 s=UnicodeString(FALSE, beforeLower, (int32_t)(sizeof(beforeLower)/U_SIZEOF_UCHAR));
144 if( s.length()!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) ||
145 s!=UnicodeString(FALSE, lowerRoot, s.length())
147 errln("error in toLower(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, lowerRoot, (int32_t)(sizeof(lowerRoot)/U_SIZEOF_UCHAR)) + "\"");
150 /* lowercase with turkish locale */
151 s=UnicodeString(FALSE, beforeLower, (int32_t)(sizeof(beforeLower)/U_SIZEOF_UCHAR));
152 s.setCharAt(0, beforeLower[0]).toLower(Locale("tr"));
153 if( s.length()!=(sizeof(lowerTurkish)/U_SIZEOF_UCHAR) ||
154 s!=UnicodeString(FALSE, lowerTurkish, s.length())
156 errln("error in toLower(turkish locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, lowerTurkish, (int32_t)(sizeof(lowerTurkish)/U_SIZEOF_UCHAR)) + "\"");
159 /* uppercase with root locale */
160 s=UnicodeString(FALSE, beforeUpper, (int32_t)(sizeof(beforeUpper)/U_SIZEOF_UCHAR));
161 s.setCharAt(0, beforeUpper[0]).toUpper(Locale(""));
162 if( s.length()!=(sizeof(upperRoot)/U_SIZEOF_UCHAR) ||
163 s!=UnicodeString(FALSE, upperRoot, s.length())
165 errln("error in toUpper(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, upperRoot, (int32_t)(sizeof(upperRoot)/U_SIZEOF_UCHAR)) + "\"");
168 /* uppercase with turkish locale */
169 s=UnicodeString(FALSE, beforeUpper, (int32_t)(sizeof(beforeUpper)/U_SIZEOF_UCHAR));
170 s.toUpper(Locale("tr"));
171 if( s.length()!=(sizeof(upperTurkish)/U_SIZEOF_UCHAR) ||
172 s!=UnicodeString(FALSE, upperTurkish, s.length())
174 errln("error in toUpper(turkish locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, upperTurkish, (int32_t)(sizeof(upperTurkish)/U_SIZEOF_UCHAR)) + "\"");
177 /* uppercase a short string with root locale */
178 s=UnicodeString(FALSE, beforeMiniUpper, (int32_t)(sizeof(beforeMiniUpper)/U_SIZEOF_UCHAR));
179 s.setCharAt(0, beforeMiniUpper[0]).toUpper("");
180 if( s.length()!=(sizeof(miniUpper)/U_SIZEOF_UCHAR) ||
181 s!=UnicodeString(FALSE, miniUpper, s.length())
183 errln("error in toUpper(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, miniUpper, (int32_t)(sizeof(miniUpper)/U_SIZEOF_UCHAR)) + "\"");
187 // test some supplementary characters (>= Unicode 3.1)
192 deseretInput=UnicodeString("\\U0001043C\\U00010414", "").unescape(),
193 deseretLower=UnicodeString("\\U0001043C\\U0001043C", "").unescape(),
194 deseretUpper=UnicodeString("\\U00010414\\U00010414", "").unescape();
195 (t=deseretInput).toLower();
196 if(t!=deseretLower) {
197 errln("error lowercasing Deseret (plane 1) characters");
199 (t=deseretInput).toUpper();
200 if(t!=deseretUpper) {
201 errln("error uppercasing Deseret (plane 1) characters");
205 // test some more cases that looked like problems
210 ljInput=UnicodeString("ab'cD \\uFB00i\\u0131I\\u0130 \\u01C7\\u01C8\\u01C9 \\U0001043C\\U00010414", "").unescape(),
211 ljLower=UnicodeString("ab'cd \\uFB00i\\u0131ii\\u0307 \\u01C9\\u01C9\\u01C9 \\U0001043C\\U0001043C", "").unescape(),
212 ljUpper=UnicodeString("AB'CD FFIII\\u0130 \\u01C7\\u01C7\\u01C7 \\U00010414\\U00010414", "").unescape();
213 (t=ljInput).toLower("en");
215 errln("error lowercasing LJ characters");
217 (t=ljInput).toUpper("en");
219 errln("error uppercasing LJ characters");
223 #if !UCONFIG_NO_NORMALIZATION
224 // some context-sensitive casing depends on normalization data being present
226 // Unicode 3.1.1 SpecialCasing tests
230 // sigmas preceded and/or followed by cased letters
232 sigmas=UnicodeString("i\\u0307\\u03a3\\u0308j \\u0307\\u03a3\\u0308j i\\u00ad\\u03a3\\u0308 \\u0307\\u03a3\\u0308 ", "").unescape(),
233 sigmasLower=UnicodeString("i\\u0307\\u03c3\\u0308j \\u0307\\u03c3\\u0308j i\\u00ad\\u03c2\\u0308 \\u0307\\u03c3\\u0308 ", "").unescape(),
234 sigmasUpper=UnicodeString("I\\u0307\\u03a3\\u0308J \\u0307\\u03a3\\u0308J I\\u00ad\\u03a3\\u0308 \\u0307\\u03a3\\u0308 ", "").unescape();
236 (t=sigmas).toLower();
238 errln("error in sigmas.toLower()=\"" + t + "\" expected \"" + sigmasLower + "\"");
241 (t=sigmas).toUpper(Locale(""));
243 errln("error in sigmas.toUpper()=\"" + t + "\" expected \"" + sigmasUpper + "\"");
246 // turkish & azerbaijani dotless i & dotted I
247 // remove dot above if there was a capital I before and there are no more accents above
249 dots=UnicodeString("I \\u0130 I\\u0307 I\\u0327\\u0307 I\\u0301\\u0307 I\\u0327\\u0307\\u0301", "").unescape(),
250 dotsTurkish=UnicodeString("\\u0131 i i i\\u0327 \\u0131\\u0301\\u0307 i\\u0327\\u0301", "").unescape(),
251 dotsDefault=UnicodeString("i i\\u0307 i\\u0307 i\\u0327\\u0307 i\\u0301\\u0307 i\\u0327\\u0307\\u0301", "").unescape();
253 (t=dots).toLower("tr");
255 errln("error in dots.toLower(tr)=\"" + t + "\" expected \"" + dotsTurkish + "\"");
258 (t=dots).toLower("de");
260 errln("error in dots.toLower(de)=\"" + t + "\" expected \"" + dotsDefault + "\"");
264 // more Unicode 3.1.1 tests
268 // lithuanian dot above in uppercasing
270 dots=UnicodeString("a\\u0307 \\u0307 i\\u0307 j\\u0327\\u0307 j\\u0301\\u0307", "").unescape(),
271 dotsLithuanian=UnicodeString("A\\u0307 \\u0307 I J\\u0327 J\\u0301\\u0307", "").unescape(),
272 dotsDefault=UnicodeString("A\\u0307 \\u0307 I\\u0307 J\\u0327\\u0307 J\\u0301\\u0307", "").unescape();
274 (t=dots).toUpper("lt");
275 if(t!=dotsLithuanian) {
276 errln("error in dots.toUpper(lt)=\"" + t + "\" expected \"" + dotsLithuanian + "\"");
279 (t=dots).toUpper("de");
281 errln("error in dots.toUpper(de)=\"" + t + "\" expected \"" + dotsDefault + "\"");
284 // lithuanian adds dot above to i in lowercasing if there are more above accents
286 i=UnicodeString("I I\\u0301 J J\\u0301 \\u012e \\u012e\\u0301 \\u00cc\\u00cd\\u0128", "").unescape(),
287 iLithuanian=UnicodeString("i i\\u0307\\u0301 j j\\u0307\\u0301 \\u012f \\u012f\\u0307\\u0301 i\\u0307\\u0300i\\u0307\\u0301i\\u0307\\u0303", "").unescape(),
288 iDefault=UnicodeString("i i\\u0301 j j\\u0301 \\u012f \\u012f\\u0301 \\u00ec\\u00ed\\u0129", "").unescape();
292 errln("error in i.toLower(lt)=\"" + t + "\" expected \"" + iLithuanian + "\"");
297 errln("error in i.toLower(de)=\"" + t + "\" expected \"" + iDefault + "\"");
306 s=UnicodeString("A\\u00df\\u00b5\\ufb03\\U0001040c\\u0130\\u0131", "").unescape(),
307 f=UnicodeString("ass\\u03bcffi\\U00010434i\\u0307\\u0131", "").unescape(),
308 g=UnicodeString("ass\\u03bcffi\\U00010434i\\u0131", "").unescape(),
313 errln("error in foldCase(\"" + s + "\", default)=\"" + t + "\" but expected \"" + f + "\"");
316 // alternate handling for dotted I/dotless i (U+0130, U+0131)
317 (t=s).foldCase(U_FOLD_CASE_EXCLUDE_SPECIAL_I);
319 errln("error in foldCase(\"" + s + "\", U_FOLD_CASE_EXCLUDE_SPECIAL_I)=\"" + t + "\" but expected \"" + g + "\"");
324 // data-driven case mapping tests ------------------------------------------ ***
334 // names of TestData children in casing.txt
335 static const char *const dataNames[TEST_COUNT+1]={
344 StringCaseTest::TestCasingImpl(const UnicodeString &input,
345 const UnicodeString &output,
347 void *iter, const char *localeID, uint32_t options) {
349 UnicodeString result;
351 Locale locale(localeID);
357 result.toLower(locale);
361 result.toUpper(locale);
363 #if !UCONFIG_NO_BREAK_ITERATION
366 result.toTitle((BreakIterator *)iter, locale, options);
371 result.foldCase(options);
375 break; // won't happen
378 dataerrln("error: UnicodeString.%s() got a wrong result for a test case from casing.res", name);
380 #if !UCONFIG_NO_BREAK_ITERATION
381 if(whichCase==TEST_TITLE && options==0) {
383 result.toTitle((BreakIterator *)iter, locale);
385 dataerrln("error: UnicodeString.toTitle(options=0) got a wrong result for a test case from casing.res");
391 char utf8In[100], utf8Out[100];
392 int32_t utf8InLength, utf8OutLength, resultLength;
395 IcuTestErrorCode errorCode(*this, "TestCasingImpl");
396 LocalUCaseMapPointer csm(ucasemap_open(localeID, options, errorCode));
397 #if !UCONFIG_NO_BREAK_ITERATION
399 // Clone the break iterator so that the UCaseMap can safely adopt it.
400 UBreakIterator *clone=ubrk_safeClone((UBreakIterator *)iter, NULL, NULL, errorCode);
401 ucasemap_setBreakIterator(csm.getAlias(), clone, errorCode);
405 u_strToUTF8(utf8In, (int32_t)sizeof(utf8In), &utf8InLength, input.getBuffer(), input.length(), errorCode);
408 name="ucasemap_utf8ToLower";
409 utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
410 utf8Out, (int32_t)sizeof(utf8Out),
411 utf8In, utf8InLength, errorCode);
414 name="ucasemap_utf8ToUpper";
415 utf8OutLength=ucasemap_utf8ToUpper(csm.getAlias(),
416 utf8Out, (int32_t)sizeof(utf8Out),
417 utf8In, utf8InLength, errorCode);
419 #if !UCONFIG_NO_BREAK_ITERATION
421 name="ucasemap_utf8ToTitle";
422 utf8OutLength=ucasemap_utf8ToTitle(csm.getAlias(),
423 utf8Out, (int32_t)sizeof(utf8Out),
424 utf8In, utf8InLength, errorCode);
428 name="ucasemap_utf8FoldCase";
429 utf8OutLength=ucasemap_utf8FoldCase(csm.getAlias(),
430 utf8Out, (int32_t)sizeof(utf8Out),
431 utf8In, utf8InLength, errorCode);
436 break; // won't happen
438 buffer=result.getBuffer(utf8OutLength);
439 u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, errorCode);
440 result.releaseBuffer(errorCode.isSuccess() ? resultLength : 0);
442 if(errorCode.isFailure()) {
443 errcheckln(errorCode, "error: %s() got an error for a test case from casing.res - %s", name, u_errorName(errorCode));
445 } else if(result!=output) {
446 errln("error: %s() got a wrong result for a test case from casing.res", name);
447 errln("expected \"" + output + "\" got \"" + result + "\"" );
452 StringCaseTest::TestCasing() {
453 UErrorCode status = U_ZERO_ERROR;
454 #if !UCONFIG_NO_BREAK_ITERATION
455 LocalUBreakIteratorPointer iter;
458 UnicodeString locale, input, output, optionsString, result;
460 int32_t whichCase, type;
461 LocalPointer<TestDataModule> driver(TestDataModule::getTestDataModule("casing", *this, status));
462 if(U_SUCCESS(status)) {
463 for(whichCase=0; whichCase<TEST_COUNT; ++whichCase) {
464 #if UCONFIG_NO_BREAK_ITERATION
465 if(whichCase==TEST_TITLE) {
469 LocalPointer<TestData> casingTest(driver->createTestData(dataNames[whichCase], status));
470 if(U_FAILURE(status)) {
471 errln("TestCasing failed to createTestData(%s) - %s", dataNames[whichCase], u_errorName(status));
474 const DataMap *myCase = NULL;
475 while(casingTest->nextCase(myCase, status)) {
476 input = myCase->getString("Input", status);
477 output = myCase->getString("Output", status);
479 if(whichCase!=TEST_FOLD) {
480 locale = myCase->getString("Locale", status);
482 locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
484 #if !UCONFIG_NO_BREAK_ITERATION
485 if(whichCase==TEST_TITLE) {
486 type = myCase->getInt("Type", status);
488 iter.adoptInstead(ubrk_open((UBreakIteratorType)type, cLocaleID, NULL, 0, &status));
489 } else if(type==-2) {
490 // Open a trivial break iterator that only delivers { 0, length }
491 // or even just { 0 } as boundaries.
492 static const UChar rules[] = { 0x2e, 0x2a, 0x3b }; // ".*;"
493 UParseError parseError;
494 iter.adoptInstead(ubrk_openRules(rules, LENGTHOF(rules), NULL, 0, &parseError, &status));
499 if(whichCase==TEST_TITLE || whichCase==TEST_FOLD) {
500 optionsString = myCase->getString("Options", status);
501 if(optionsString.indexOf((UChar)0x54)>=0) { // T
502 options|=U_FOLD_CASE_EXCLUDE_SPECIAL_I;
504 if(optionsString.indexOf((UChar)0x4c)>=0) { // L
505 options|=U_TITLECASE_NO_LOWERCASE;
507 if(optionsString.indexOf((UChar)0x41)>=0) { // A
508 options|=U_TITLECASE_NO_BREAK_ADJUSTMENT;
512 if(U_FAILURE(status)) {
513 dataerrln("error: TestCasing() setup failed for %s test case from casing.res: %s", dataNames[whichCase], u_errorName(status));
514 status = U_ZERO_ERROR;
516 #if UCONFIG_NO_BREAK_ITERATION
517 LocalPointer<UMemory> iter;
519 TestCasingImpl(input, output, whichCase, iter.getAlias(), cLocaleID, options);
522 #if !UCONFIG_NO_BREAK_ITERATION
523 iter.adoptInstead(NULL);
529 #if !UCONFIG_NO_BREAK_ITERATION
530 // more tests for API coverage
532 input=UNICODE_STRING_SIMPLE("sTrA\\u00dfE").unescape();
533 (result=input).toTitle(NULL);
534 if(result!=UNICODE_STRING_SIMPLE("Stra\\u00dfe").unescape()) {
535 dataerrln("UnicodeString::toTitle(NULL) failed.");
541 StringCaseTest::TestFullCaseFoldingIterator() {
542 UnicodeString ffi=UNICODE_STRING_SIMPLE("ffi");
543 UnicodeString ss=UNICODE_STRING_SIMPLE("ss");
544 FullCaseFoldingIterator iter;
546 int32_t countSpecific=0;
549 while((c=iter.next(full))>=0) {
551 // Check that the full Case_Folding has more than 1 code point.
552 if(!full.hasMoreChar32Than(0, 0x7fffffff, 1)) {
553 errln("error: FullCaseFoldingIterator.next()=U+%04lX full Case_Folding has at most 1 code point", (long)c);
556 // Check that full == Case_Folding(c).
560 errln("error: FullCaseFoldingIterator.next()=U+%04lX full Case_Folding != cf(c)", (long)c);
563 // Spot-check a couple of specific cases.
564 if((full==ffi && c==0xfb03) || (full==ss && (c==0xdf || c==0x1e9e))) {
568 if(countSpecific!=3) {
569 errln("error: FullCaseFoldingIterator did not yield exactly the expected specific cases");
572 errln("error: FullCaseFoldingIterator yielded only %d (cp, full) pairs", (int)count);