]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/strcase.cpp
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / test / intltest / strcase.cpp
index 1a9d605e85a86c57eb2268abd100054abb73c742..75fc74047dafc77c3c201ec33cf29677383fb85a 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2002-2008, International Business Machines
+*   Copyright (C) 2002-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -23,6 +23,7 @@
 #include "unicode/ubrk.h"
 #include "unicode/unistr.h"
 #include "unicode/ucasemap.h"
+#include "ucase.h"
 #include "ustrtest.h"
 #include "unicode/tstdtmod.h"
 
@@ -32,18 +33,16 @@ StringCaseTest::~StringCaseTest() {}
 
 void
 StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
-    if (exec) logln("TestSuite StringCaseTest: ");
-    switch (index) {
-        case 0: name = "TestCaseConversion"; if (exec) TestCaseConversion(); break;
-        case 1:
-            name = "TestCasing";
-#if !UCONFIG_NO_BREAK_ITERATION
-            if(exec) TestCasing();
-#endif
-            break;
-
-        default: name = ""; break; //needed to end loop
+    if(exec) {
+        logln("TestSuite StringCaseTest: ");
     }
+    TESTCASE_AUTO_BEGIN;
+    TESTCASE_AUTO(TestCaseConversion);
+#if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
+    TESTCASE_AUTO(TestCasing);
+#endif
+    TESTCASE_AUTO(TestFullCaseFoldingIterator);
+    TESTCASE_AUTO_END;
 }
 
 void
@@ -376,14 +375,14 @@ StringCaseTest::TestCasingImpl(const UnicodeString &input,
         break; // won't happen
     }
     if(result!=output) {
-        errln("error: UnicodeString.%s() got a wrong result for a test case from casing.res", name);
+        dataerrln("error: UnicodeString.%s() got a wrong result for a test case from casing.res", name);
     }
 #if !UCONFIG_NO_BREAK_ITERATION
     if(whichCase==TEST_TITLE && options==0) {
         result=input;
         result.toTitle((BreakIterator *)iter, locale);
         if(result!=output) {
-            errln("error: UnicodeString.toTitle(options=0) got a wrong result for a test case from casing.res");
+            dataerrln("error: UnicodeString.toTitle(options=0) got a wrong result for a test case from casing.res");
         }
     }
 #endif
@@ -393,47 +392,44 @@ StringCaseTest::TestCasingImpl(const UnicodeString &input,
     int32_t utf8InLength, utf8OutLength, resultLength;
     UChar *buffer;
 
-    UCaseMap *csm;
-    UErrorCode errorCode;
-
-    errorCode=U_ZERO_ERROR;
-    csm=ucasemap_open(localeID, options, &errorCode);
+    IcuTestErrorCode errorCode(*this, "TestCasingImpl");
+    LocalUCaseMapPointer csm(ucasemap_open(localeID, options, errorCode));
 #if !UCONFIG_NO_BREAK_ITERATION
     if(iter!=NULL) {
         // Clone the break iterator so that the UCaseMap can safely adopt it.
         int32_t size=1;  // Not 0 because that only gives preflighting.
-        UBreakIterator *clone=ubrk_safeClone((UBreakIterator *)iter, NULL, &size, &errorCode);
-        ucasemap_setBreakIterator(csm, clone, &errorCode);
+        UBreakIterator *clone=ubrk_safeClone((UBreakIterator *)iter, NULL, &size, errorCode);
+        ucasemap_setBreakIterator(csm.getAlias(), clone, errorCode);
     }
 #endif
 
-    u_strToUTF8(utf8In, (int32_t)sizeof(utf8In), &utf8InLength, input.getBuffer(), input.length(), &errorCode);
+    u_strToUTF8(utf8In, (int32_t)sizeof(utf8In), &utf8InLength, input.getBuffer(), input.length(), errorCode);
     switch(whichCase) {
     case TEST_LOWER:
         name="ucasemap_utf8ToLower";
-        utf8OutLength=ucasemap_utf8ToLower(csm,
+        utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
                     utf8Out, (int32_t)sizeof(utf8Out),
-                    utf8In, utf8InLength, &errorCode);
+                    utf8In, utf8InLength, errorCode);
         break;
     case TEST_UPPER:
         name="ucasemap_utf8ToUpper";
-        utf8OutLength=ucasemap_utf8ToUpper(csm,
+        utf8OutLength=ucasemap_utf8ToUpper(csm.getAlias(),
                     utf8Out, (int32_t)sizeof(utf8Out),
-                    utf8In, utf8InLength, &errorCode);
+                    utf8In, utf8InLength, errorCode);
         break;
 #if !UCONFIG_NO_BREAK_ITERATION
     case TEST_TITLE:
         name="ucasemap_utf8ToTitle";
-        utf8OutLength=ucasemap_utf8ToTitle(csm,
+        utf8OutLength=ucasemap_utf8ToTitle(csm.getAlias(),
                     utf8Out, (int32_t)sizeof(utf8Out),
-                    utf8In, utf8InLength, &errorCode);
+                    utf8In, utf8InLength, errorCode);
         break;
 #endif
     case TEST_FOLD:
         name="ucasemap_utf8FoldCase";
-        utf8OutLength=ucasemap_utf8FoldCase(csm,
+        utf8OutLength=ucasemap_utf8FoldCase(csm.getAlias(),
                     utf8Out, (int32_t)sizeof(utf8Out),
-                    utf8In, utf8InLength, &errorCode);
+                    utf8In, utf8InLength, errorCode);
         break;
     default:
         name="";
@@ -441,27 +437,29 @@ StringCaseTest::TestCasingImpl(const UnicodeString &input,
         break; // won't happen
     }
     buffer=result.getBuffer(utf8OutLength);
-    u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, &errorCode);
-    result.releaseBuffer(U_SUCCESS(errorCode) ? resultLength : 0);
+    u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, errorCode);
+    result.releaseBuffer(errorCode.isSuccess() ? resultLength : 0);
 
-    if(U_FAILURE(errorCode)) {
-        errln("error: %s() got an error for a test case from casing.res - %s", name, u_errorName(errorCode));
+    if(errorCode.isFailure()) {
+        errcheckln(errorCode, "error: %s() got an error for a test case from casing.res - %s", name, u_errorName(errorCode));
+        errorCode.reset();
     } else if(result!=output) {
         errln("error: %s() got a wrong result for a test case from casing.res", name);
         errln("expected \"" + output + "\" got \"" + result + "\"" );
     }
-    ucasemap_close(csm);
 }
 
 void
 StringCaseTest::TestCasing() {
     UErrorCode status = U_ZERO_ERROR;
-    void *iter;
+#if !UCONFIG_NO_BREAK_ITERATION
+    LocalUBreakIteratorPointer iter;
+#endif
     char cLocaleID[100];
     UnicodeString locale, input, output, optionsString, result;
     uint32_t options;
     int32_t whichCase, type;
-    TestDataModule *driver = TestDataModule::getTestDataModule("casing", *this, status);
+    LocalPointer<TestDataModule> driver(TestDataModule::getTestDataModule("casing", *this, status));
     if(U_SUCCESS(status)) {
         for(whichCase=0; whichCase<TEST_COUNT; ++whichCase) {
 #if UCONFIG_NO_BREAK_ITERATION
@@ -469,7 +467,7 @@ StringCaseTest::TestCasing() {
                 continue;
             }
 #endif
-            TestData *casingTest = driver->createTestData(dataNames[whichCase], status);
+            LocalPointer<TestData> casingTest(driver->createTestData(dataNames[whichCase], status));
             if(U_FAILURE(status)) {
                 errln("TestCasing failed to createTestData(%s) - %s", dataNames[whichCase], u_errorName(status));
                 break;
@@ -484,18 +482,17 @@ StringCaseTest::TestCasing() {
                 }
                 locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
 
-                iter=NULL;
 #if !UCONFIG_NO_BREAK_ITERATION
                 if(whichCase==TEST_TITLE) {
                     type = myCase->getInt("Type", status);
                     if(type>=0) {
-                        iter=ubrk_open((UBreakIteratorType)type, cLocaleID, NULL, 0, &status);
+                        iter.adoptInstead(ubrk_open((UBreakIteratorType)type, cLocaleID, NULL, 0, &status));
                     } else if(type==-2) {
                         // Open a trivial break iterator that only delivers { 0, length }
                         // or even just { 0 } as boundaries.
                         static const UChar rules[] = { 0x2e, 0x2a, 0x3b };  // ".*;"
                         UParseError parseError;
-                        iter=ubrk_openRules(rules, LENGTHOF(rules), NULL, 0, &parseError, &status);
+                        iter.adoptInstead(ubrk_openRules(rules, LENGTHOF(rules), NULL, 0, &parseError, &status));
                     }
                 }
 #endif
@@ -514,22 +511,21 @@ StringCaseTest::TestCasing() {
                 }
 
                 if(U_FAILURE(status)) {
-                    errln("error: TestCasing() setup failed for %s test case from casing.res: %s", dataNames[whichCase],  u_errorName(status));
+                    dataerrln("error: TestCasing() setup failed for %s test case from casing.res: %s", dataNames[whichCase],  u_errorName(status));
                     status = U_ZERO_ERROR;
                 } else {
-                    TestCasingImpl(input, output, whichCase, iter, cLocaleID, options);
+#if UCONFIG_NO_BREAK_ITERATION
+                    LocalPointer<UMemory> iter;
+#endif
+                    TestCasingImpl(input, output, whichCase, iter.getAlias(), cLocaleID, options);
                 }
 
 #if !UCONFIG_NO_BREAK_ITERATION
-                if(iter!=NULL) {
-                    ubrk_close(iter);
-                }
+                iter.adoptInstead(NULL);
 #endif
             }
-            delete casingTest;
         }
     }
-    delete driver;
 
 #if !UCONFIG_NO_BREAK_ITERATION
     // more tests for API coverage
@@ -537,7 +533,43 @@ StringCaseTest::TestCasing() {
     input=UNICODE_STRING_SIMPLE("sTrA\\u00dfE").unescape();
     (result=input).toTitle(NULL);
     if(result!=UNICODE_STRING_SIMPLE("Stra\\u00dfe").unescape()) {
-        errln("UnicodeString::toTitle(NULL) failed");
+        dataerrln("UnicodeString::toTitle(NULL) failed.");
     }
 #endif
 }
+
+void
+StringCaseTest::TestFullCaseFoldingIterator() {
+    UnicodeString ffi=UNICODE_STRING_SIMPLE("ffi");
+    UnicodeString ss=UNICODE_STRING_SIMPLE("ss");
+    FullCaseFoldingIterator iter;
+    int32_t count=0;
+    int32_t countSpecific=0;
+    UChar32 c;
+    UnicodeString full;
+    while((c=iter.next(full))>=0) {
+        ++count;
+        // Check that the full Case_Folding has more than 1 code point.
+        if(!full.hasMoreChar32Than(0, 0x7fffffff, 1)) {
+            errln("error: FullCaseFoldingIterator.next()=U+%04lX full Case_Folding has at most 1 code point", (long)c);
+            continue;
+        }
+        // Check that full == Case_Folding(c).
+        UnicodeString cf(c);
+        cf.foldCase();
+        if(full!=cf) {
+            errln("error: FullCaseFoldingIterator.next()=U+%04lX full Case_Folding != cf(c)", (long)c);
+            continue;
+        }
+        // Spot-check a couple of specific cases.
+        if((full==ffi && c==0xfb03) || (full==ss && (c==0xdf || c==0x1e9e))) {
+            ++countSpecific;
+        }
+    }
+    if(countSpecific!=3) {
+        errln("error: FullCaseFoldingIterator did not yield exactly the expected specific cases");
+    }
+    if(count<70) {
+        errln("error: FullCaseFoldingIterator yielded only %d (cp, full) pairs", (int)count);
+    }
+}