/*
*******************************************************************************
*
-* Copyright (C) 2002-2004, International Business Machines
+* Copyright (C) 2002-2012, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
#include "unicode/uloc.h"
#include "unicode/locid.h"
#include "unicode/ubrk.h"
+#include "unicode/unistr.h"
+#include "unicode/ucasemap.h"
+#include "ucase.h"
#include "ustrtest.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*/) {
- if (exec) logln("TestSuite StringCaseTest: ");
- switch (index) {
- case 0: name = "TestCaseConversion"; if (exec) TestCaseConversion(); break;
- case 1:
- name = "TestTitleCasing";
-#if !UCONFIG_NO_BREAK_ITERATION
- if(exec) TestTitleCasing();
-#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
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 };
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 + "\".");
test3 += " palace, istanbul";
test4 = test3;
- test4.toUpper();
+ test4.toUpper(Locale(""));
expectedResult = "TOPKAPI PALACE, ISTANBUL";
if (test4 != expectedResult)
errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
/* 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())
) {
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 + "\"");
}
}
}
+// 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::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
+ }
+ if(result!=output) {
+ 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) {
+ dataerrln("error: UnicodeString.toTitle(options=0) got a wrong result for a test case from casing.res");
+ }
+ }
+#endif
+
+ // UTF-8
+ char utf8In[100], utf8Out[100];
+ int32_t utf8InLength, utf8OutLength, resultLength;
+ UChar *buffer;
+
+ 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.getAlias(), 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.getAlias(),
+ utf8Out, (int32_t)sizeof(utf8Out),
+ utf8In, utf8InLength, errorCode);
+ break;
+ case TEST_UPPER:
+ name="ucasemap_utf8ToUpper";
+ utf8OutLength=ucasemap_utf8ToUpper(csm.getAlias(),
+ utf8Out, (int32_t)sizeof(utf8Out),
+ utf8In, utf8InLength, errorCode);
+ break;
#if !UCONFIG_NO_BREAK_ITERATION
+ case TEST_TITLE:
+ name="ucasemap_utf8ToTitle";
+ utf8OutLength=ucasemap_utf8ToTitle(csm.getAlias(),
+ utf8Out, (int32_t)sizeof(utf8Out),
+ utf8In, utf8InLength, errorCode);
+ break;
+#endif
+ case TEST_FOLD:
+ name="ucasemap_utf8FoldCase";
+ utf8OutLength=ucasemap_utf8FoldCase(csm.getAlias(),
+ 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(errorCode.isSuccess() ? resultLength : 0);
+
+ 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 + "\"" );
+ }
+}
void
-StringCaseTest::TestTitleCasing() {
+StringCaseTest::TestCasing() {
UErrorCode status = U_ZERO_ERROR;
- UBreakIterator *iter;
+#if !UCONFIG_NO_BREAK_ITERATION
+ LocalUBreakIteratorPointer iter;
+#endif
char cLocaleID[100];
- UnicodeString locale, input, result;
- int32_t type;
- TestDataModule *driver = TestDataModule::getTestDataModule("casing", *this, status);
+ UnicodeString locale, input, output, optionsString, result;
+ uint32_t options;
+ int32_t whichCase, type;
+ LocalPointer<TestDataModule> driver(TestDataModule::getTestDataModule("casing", *this, 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, NULL, 0, &status);
+ for(whichCase=0; whichCase<TEST_COUNT; ++whichCase) {
+#if UCONFIG_NO_BREAK_ITERATION
+ if(whichCase==TEST_TITLE) {
+ continue;
}
-
+#endif
+ LocalPointer<TestData> casingTest(driver->createTestData(dataNames[whichCase], 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");
+ 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);
+
+ if(whichCase!=TEST_FOLD) {
+ locale = myCase->getString("Locale", status);
+ }
+ locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
+
+#if !UCONFIG_NO_BREAK_ITERATION
+ if(whichCase==TEST_TITLE) {
+ type = myCase->getInt("Type", status);
+ if(type>=0) {
+ 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.adoptInstead(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;
+ }
}
- ubrk_close(iter);
+
+ if(U_FAILURE(status)) {
+ dataerrln("error: TestCasing() setup failed for %s test case from casing.res: %s", dataNames[whichCase], u_errorName(status));
+ status = U_ZERO_ERROR;
+ } else {
+#if UCONFIG_NO_BREAK_ITERATION
+ LocalPointer<UMemory> iter;
+#endif
+ TestCasingImpl(input, output, whichCase, iter.getAlias(), cLocaleID, options);
+ }
+
+#if !UCONFIG_NO_BREAK_ITERATION
+ iter.adoptInstead(NULL);
+#endif
}
}
- delete casingTest;
}
- delete driver;
+#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");
- }
-
-#if 0
- char cLocaleID[100];
- UnicodeString in, expect, result, localeID;
- UResourceBundle *casing, *titlecasing, *test, *res;
- 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;
+ dataerrln("UnicodeString::toTitle(NULL) failed.");
}
+#endif
+}
- // 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
- }
-
- // get test case data
- in=ures_getUnicodeStringByIndex(test, 0, &errorCode);
- expect=ures_getUnicodeStringByIndex(test, 1, &errorCode);
- localeID=ures_getUnicodeStringByIndex(test, 2, &errorCode);
-
- res=ures_getByIndex(test, 3, 0, &errorCode);
- type=ures_getInt(res, &errorCode);
- ures_close(res);
-
- 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
- }
-
- // 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(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);
- }
- }
-
- // clean up
- ubrk_close(iter);
- ures_close(test);
+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;
}
- ures_close(titlecasing);
- logln("TestTitleCasing() processed %ld test cases", testIndex);
}
-
- ures_close(casing);
-#endif
+ 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);
+ }
}
-#endif