]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/strcase.cpp
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / test / intltest / strcase.cpp
index b7c3d6f4dac215d898d3fa239871f814138927b9..1a9d605e85a86c57eb2268abd100054abb73c742 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2002-2003, International Business Machines
+*   Copyright (C) 2002-2008, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 *   Test file for string casing C++ API functions.
 */
 
-#include "unicode/utypes.h"
 #include "unicode/uchar.h"
-#include "unicode/unistr.h"
 #include "unicode/ures.h"
 #include "unicode/uloc.h"
 #include "unicode/locid.h"
 #include "unicode/ubrk.h"
+#include "unicode/unistr.h"
+#include "unicode/ucasemap.h"
 #include "ustrtest.h"
-#include "tstdtmod.h"
+#include "unicode/tstdtmod.h"
+
+#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
+
+StringCaseTest::~StringCaseTest() {}
 
 void
 StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
@@ -32,9 +36,9 @@ StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
     switch (index) {
         case 0: name = "TestCaseConversion"; if (exec) TestCaseConversion(); break;
         case 1:
-            name = "TestTitleCasing";
+            name = "TestCasing";
 #if !UCONFIG_NO_BREAK_ITERATION
-            if(exec) TestTitleCasing();
+            if(exec) TestCasing();
 #endif
             break;
 
@@ -45,21 +49,21 @@ StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
 void
 StringCaseTest::TestCaseConversion()
 {
-    UChar uppercaseGreek[] =
+    static const UChar uppercaseGreek[] =
         { 0x399, 0x395, 0x3a3, 0x3a5, 0x3a3, 0x20, 0x03a7, 0x3a1, 0x399, 0x3a3, 0x3a4,
         0x39f, 0x3a3, 0 };
         // "IESUS CHRISTOS"
 
-    UChar lowercaseGreek[] = 
+    static const UChar lowercaseGreek[] = 
         { 0x3b9, 0x3b5, 0x3c3, 0x3c5, 0x3c2, 0x20, 0x03c7, 0x3c1, 0x3b9, 0x3c3, 0x3c4,
         0x3bf, 0x3c2, 0 };
         // "iesus christos"
 
-    UChar lowercaseTurkish[] = 
+    static const UChar lowercaseTurkish[] = 
         { 0x69, 0x73, 0x74, 0x61, 0x6e, 0x62, 0x75, 0x6c, 0x2c, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x63, 0x6f, 
         0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x0131, 0x6e, 0x6f, 0x70, 0x6c, 0x65, 0x21, 0 };
 
-    UChar uppercaseTurkish[] = 
+    static const UChar uppercaseTurkish[] = 
         { 0x54, 0x4f, 0x50, 0x4b, 0x41, 0x50, 0x49, 0x20, 0x50, 0x41, 0x4c, 0x41, 0x43, 0x45, 0x2c, 0x20,
         0x0130, 0x53, 0x54, 0x41, 0x4e, 0x42, 0x55, 0x4c, 0 };
     
@@ -70,7 +74,7 @@ StringCaseTest::TestCaseConversion()
     test3 += "STANBUL, NOT CONSTANTINOPLE!";
 
     UnicodeString   test4(test3);
-    test4.toLower();
+    test4.toLower(Locale(""));
     expectedResult = UnicodeString("i\\u0307stanbul, not constantinople!", "").unescape();
     if (test4 != expectedResult)
         errln("1. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
@@ -86,7 +90,7 @@ StringCaseTest::TestCaseConversion()
     test3 += " palace, istanbul";
     test4 = test3;
 
-    test4.toUpper();
+    test4.toUpper(Locale(""));
     expectedResult = "TOPKAPI PALACE, ISTANBUL";
     if (test4 != expectedResult)
         errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
@@ -155,7 +159,7 @@ StringCaseTest::TestCaseConversion()
 
         /* uppercase with root locale */
         s=UnicodeString(FALSE, beforeUpper, (int32_t)(sizeof(beforeUpper)/U_SIZEOF_UCHAR));
-        s.setCharAt(0, beforeUpper[0]).toUpper();
+        s.setCharAt(0, beforeUpper[0]).toUpper(Locale(""));
         if( s.length()!=(sizeof(upperRoot)/U_SIZEOF_UCHAR) ||
             s!=UnicodeString(FALSE, upperRoot, s.length())
         ) {
@@ -235,7 +239,7 @@ StringCaseTest::TestCaseConversion()
             errln("error in sigmas.toLower()=\"" + t + "\" expected \"" + sigmasLower + "\"");
         }
 
-        (t=sigmas).toUpper();
+        (t=sigmas).toUpper(Locale(""));
         if(t!=sigmasUpper) {
             errln("error in sigmas.toUpper()=\"" + t + "\" expected \"" + sigmasUpper + "\"");
         }
@@ -318,127 +322,222 @@ StringCaseTest::TestCaseConversion()
     }
 }
 
-#if !UCONFIG_NO_BREAK_ITERATION
+// data-driven case mapping tests ------------------------------------------ ***
+
+enum {
+    TEST_LOWER,
+    TEST_UPPER,
+    TEST_TITLE,
+    TEST_FOLD,
+    TEST_COUNT
+};
+
+// names of TestData children in casing.txt
+static const char *const dataNames[TEST_COUNT+1]={
+    "lowercasing",
+    "uppercasing",
+    "titlecasing",
+    "casefolding",
+    ""
+};
 
 void
-StringCaseTest::TestTitleCasing() {
-  UErrorCode status = U_ZERO_ERROR;
-  UBreakIterator *iter;
-  char cLocaleID[100];
-  UnicodeString locale, input, result;
-  int32_t type;
-  TestLog myLog;
-  TestDataModule *driver = TestDataModule::getTestDataModule("casing", myLog, status);
-  if(U_SUCCESS(status)) {
-    TestData *casingTest = driver->createTestData("titlecasing", status);
-    const DataMap *myCase = NULL;
-    while(casingTest->nextCase(myCase, status)) {
-      locale = myCase->getString("Locale", status);
-      locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
-      type = myCase->getInt("Type", status);
-      
-
-      input = myCase->getString("Input", status);
-      if(type<0) {
-          iter=0;
-      } else {
-          iter=ubrk_open((UBreakIteratorType)type, cLocaleID, input.getBuffer(), input.length(), &status);
-      }
-
-      if(U_FAILURE(status)) {
-          errln("error: TestTitleCasing() ubrk_open(%d) failed for test case  from casing.res: %s", type,  u_errorName(status));
-          status = U_ZERO_ERROR;
-      } else {
-          result=input;
-          result.toTitle((BreakIterator *)iter, Locale(cLocaleID));
-          if(result!=myCase->getString("Output", status)) {
-              errln("error: TestTitleCasing() got a wrong result for test case from casing.res");
-          }
-      }
-      ubrk_close(iter);
+StringCaseTest::TestCasingImpl(const UnicodeString &input,
+                               const UnicodeString &output,
+                               int32_t whichCase,
+                               void *iter, const char *localeID, uint32_t options) {
+    // UnicodeString
+    UnicodeString result;
+    const char *name;
+    Locale locale(localeID);
+
+    result=input;
+    switch(whichCase) {
+    case TEST_LOWER:
+        name="toLower";
+        result.toLower(locale);
+        break;
+    case TEST_UPPER:
+        name="toUpper";
+        result.toUpper(locale);
+        break;
+#if !UCONFIG_NO_BREAK_ITERATION
+    case TEST_TITLE:
+        name="toTitle";
+        result.toTitle((BreakIterator *)iter, locale, options);
+        break;
+#endif
+    case TEST_FOLD:
+        name="foldCase";
+        result.foldCase(options);
+        break;
+    default:
+        name="";
+        break; // won't happen
     }
-    delete casingTest;
-  }
-  delete driver;
-
-    // more tests for API coverage
-    status=U_ZERO_ERROR;
-    input=UNICODE_STRING_SIMPLE("sTrA\\u00dfE").unescape();
-    (result=input).toTitle(NULL);
-    if(result!=UNICODE_STRING_SIMPLE("Stra\\u00dfe").unescape()) {
-        errln("UnicodeString::toTitle(BreakIterator *) failed");
+    if(result!=output) {
+        errln("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");
+        }
+    }
+#endif
 
-#if 0
-    char cLocaleID[100];
-    UnicodeString in, expect, result, localeID;
-    UResourceBundle *casing, *titlecasing, *test, *res;
+    // UTF-8
+    char utf8In[100], utf8Out[100];
+    int32_t utf8InLength, utf8OutLength, resultLength;
+    UChar *buffer;
+
+    UCaseMap *csm;
     UErrorCode errorCode;
-    int32_t testIndex, type;
 
     errorCode=U_ZERO_ERROR;
-    loadTestData(errorCode);
-    casing=ures_openDirect("testdata", "casing", &errorCode);
-    if(U_FAILURE(errorCode)) {
-        errln("error: TestTitleCasing() is unable to open casing.res: %s", u_errorName(errorCode));
-        return;
+    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);
+    }
+#endif
+
+    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,
+                    utf8Out, (int32_t)sizeof(utf8Out),
+                    utf8In, utf8InLength, &errorCode);
+        break;
+    case TEST_UPPER:
+        name="ucasemap_utf8ToUpper";
+        utf8OutLength=ucasemap_utf8ToUpper(csm,
+                    utf8Out, (int32_t)sizeof(utf8Out),
+                    utf8In, utf8InLength, &errorCode);
+        break;
+#if !UCONFIG_NO_BREAK_ITERATION
+    case TEST_TITLE:
+        name="ucasemap_utf8ToTitle";
+        utf8OutLength=ucasemap_utf8ToTitle(csm,
+                    utf8Out, (int32_t)sizeof(utf8Out),
+                    utf8In, utf8InLength, &errorCode);
+        break;
+#endif
+    case TEST_FOLD:
+        name="ucasemap_utf8FoldCase";
+        utf8OutLength=ucasemap_utf8FoldCase(csm,
+                    utf8Out, (int32_t)sizeof(utf8Out),
+                    utf8In, utf8InLength, &errorCode);
+        break;
+    default:
+        name="";
+        utf8OutLength=0;
+        break; // won't happen
     }
+    buffer=result.getBuffer(utf8OutLength);
+    u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, &errorCode);
+    result.releaseBuffer(U_SUCCESS(errorCode) ? resultLength : 0);
 
-    // titlecasing tests
-    titlecasing=ures_getByKey(casing, "titlecasing", 0, &errorCode);
     if(U_FAILURE(errorCode)) {
-        logln("TestTitleCasing() is unable to open get casing.res/titlecasing: %s", u_errorName(errorCode));
-    } else {
-        UBreakIterator *iter;
-
-        for(testIndex=0;; ++testIndex) {
-            // get test case
-            test=ures_getByIndex(titlecasing, testIndex, 0, &errorCode);
-            if(U_FAILURE(errorCode)) {
-                break; // done
-            }
+        errln("error: %s() got an error for a test case from casing.res - %s", name, u_errorName(errorCode));
+    } 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);
+}
 
-            // get test case data
-            in=ures_getUnicodeStringByIndex(test, 0, &errorCode);
-            expect=ures_getUnicodeStringByIndex(test, 1, &errorCode);
-            localeID=ures_getUnicodeStringByIndex(test, 2, &errorCode);
+void
+StringCaseTest::TestCasing() {
+    UErrorCode status = U_ZERO_ERROR;
+    void *iter;
+    char cLocaleID[100];
+    UnicodeString locale, input, output, optionsString, result;
+    uint32_t options;
+    int32_t whichCase, type;
+    TestDataModule *driver = TestDataModule::getTestDataModule("casing", *this, status);
+    if(U_SUCCESS(status)) {
+        for(whichCase=0; whichCase<TEST_COUNT; ++whichCase) {
+#if UCONFIG_NO_BREAK_ITERATION
+            if(whichCase==TEST_TITLE) {
+                continue;
+            }
+#endif
+            TestData *casingTest = driver->createTestData(dataNames[whichCase], status);
+            if(U_FAILURE(status)) {
+                errln("TestCasing failed to createTestData(%s) - %s", dataNames[whichCase], u_errorName(status));
+                break;
+            }
+            const DataMap *myCase = NULL;
+            while(casingTest->nextCase(myCase, status)) {
+                input = myCase->getString("Input", status);
+                output = myCase->getString("Output", status);
 
-            res=ures_getByIndex(test, 3, 0, &errorCode);
-            type=ures_getInt(res, &errorCode);
-            ures_close(res);
+                if(whichCase!=TEST_FOLD) {
+                    locale = myCase->getString("Locale", status);
+                }
+                locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
 
-            if(U_FAILURE(errorCode)) {
-                errln("error: TestTitleCasing() is unable to get data for test case %ld from casing.res: %s", testIndex, u_errorName(errorCode));
-                continue; // skip this test case
-            }
+                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);
+                    } 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);
+                    }
+                }
+#endif
+                options = 0;
+                if(whichCase==TEST_TITLE || whichCase==TEST_FOLD) {
+                    optionsString = myCase->getString("Options", status);
+                    if(optionsString.indexOf((UChar)0x54)>=0) {  // T
+                        options|=U_FOLD_CASE_EXCLUDE_SPECIAL_I;
+                    }
+                    if(optionsString.indexOf((UChar)0x4c)>=0) {  // L
+                        options|=U_TITLECASE_NO_LOWERCASE;
+                    }
+                    if(optionsString.indexOf((UChar)0x41)>=0) {  // A
+                        options|=U_TITLECASE_NO_BREAK_ADJUSTMENT;
+                    }
+                }
 
-            // run this test case
-            localeID.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
-            if(type<0) {
-                iter=0;
-            } else {
-                iter=ubrk_open((UBreakIteratorType)type, cLocaleID, in.getBuffer(), in.length(), &errorCode);
-            }
+                if(U_FAILURE(status)) {
+                    errln("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(U_FAILURE(errorCode)) {
-                errln("error: TestTitleCasing() ubrk_open(%d) failed for test case %d from casing.res: %s", type, testIndex, u_errorName(errorCode));
-            } else {
-                result=in;
-                result.toTitle((BreakIterator *)iter, Locale(cLocaleID));
-                if(result!=expect) {
-                    errln("error: TestTitleCasing() got a wrong result for test case %ld from casing.res", testIndex);
+#if !UCONFIG_NO_BREAK_ITERATION
+                if(iter!=NULL) {
+                    ubrk_close(iter);
                 }
+#endif
             }
-
-            // clean up
-            ubrk_close(iter);
-            ures_close(test);
+            delete casingTest;
         }
-        ures_close(titlecasing);
-        logln("TestTitleCasing() processed %ld test cases", testIndex);
     }
+    delete driver;
 
-    ures_close(casing);
+#if !UCONFIG_NO_BREAK_ITERATION
+    // more tests for API coverage
+    status=U_ZERO_ERROR;
+    input=UNICODE_STRING_SIMPLE("sTrA\\u00dfE").unescape();
+    (result=input).toTitle(NULL);
+    if(result!=UNICODE_STRING_SIMPLE("Stra\\u00dfe").unescape()) {
+        errln("UnicodeString::toTitle(NULL) failed");
+    }
 #endif
 }
-#endif