X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..b25be06635768807f8f693286fa73bb2297bb06c:/icuSources/test/intltest/ustrtest.cpp?ds=sidebyside diff --git a/icuSources/test/intltest/ustrtest.cpp b/icuSources/test/intltest/ustrtest.cpp index 74c6a0e7..cd7e5f73 100644 --- a/icuSources/test/intltest/ustrtest.cpp +++ b/icuSources/test/intltest/ustrtest.cpp @@ -1,16 +1,19 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2003, International Business Machines Corporation and + * Copyright (c) 1997-2010, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ #include "ustrtest.h" +#include "unicode/std_string.h" #include "unicode/unistr.h" #include "unicode/uchar.h" #include "unicode/ustring.h" #include "unicode/locid.h" #include "unicode/ucnv.h" +#include "unicode/uenum.h" #include "cmemory.h" +#include "charstr.h" #if 0 #include "unicode/ustream.h" @@ -24,17 +27,15 @@ using namespace std; #endif -#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) +#define LENGTHOF(array) (int32_t)((sizeof(array)/sizeof((array)[0]))) + +UnicodeStringTest::~UnicodeStringTest() {} void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char *par) { if (exec) logln("TestSuite UnicodeStringTest: "); switch (index) { - case 0: name = "TestBasicManipulation"; if (exec) TestBasicManipulation(); break; - case 1: name = "TestCompare"; if (exec) TestCompare(); break; - case 2: name = "TestExtract"; if (exec) TestExtract(); break; - case 3: name = "TestRemoveReplace"; if (exec) TestRemoveReplace(); break; - case 4: + case 0: name = "StringCaseTest"; if (exec) { logln("StringCaseTest---"); logln(""); @@ -42,6 +43,10 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* & callTest(test, par); } break; + case 1: name = "TestBasicManipulation"; if (exec) TestBasicManipulation(); break; + case 2: name = "TestCompare"; if (exec) TestCompare(); break; + case 3: name = "TestExtract"; if (exec) TestExtract(); break; + case 4: name = "TestRemoveReplace"; if (exec) TestRemoveReplace(); break; case 5: name = "TestSearching"; if (exec) TestSearching(); break; case 6: name = "TestSpacePadding"; if (exec) TestSpacePadding(); break; case 7: name = "TestPrefixAndSuffix"; if (exec) TestPrefixAndSuffix(); break; @@ -52,6 +57,11 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* & case 12: name = "TestStackAllocation"; if (exec) TestStackAllocation(); break; case 13: name = "TestUnescape"; if (exec) TestUnescape(); break; case 14: name = "TestCountChar32"; if (exec) TestCountChar32(); break; + case 15: name = "TestStringEnumeration"; if (exec) TestStringEnumeration(); break; + case 16: name = "TestNameSpace"; if (exec) TestNameSpace(); break; + case 17: name = "TestUTF32"; if (exec) TestUTF32(); break; + case 18: name = "TestUTF8"; if (exec) TestUTF8(); break; + case 19: name = "TestReadOnlyAlias"; if (exec) TestReadOnlyAlias(); break; default: name = ""; break; //needed to end loop } @@ -154,21 +164,98 @@ UnicodeStringTest::TestBasicManipulation() } } - { - // test new 2.2 constructors and setTo function that parallel Java's substring function. - UnicodeString src("Hello folks how are you?"); - UnicodeString target1("how are you?"); - if (target1 != UnicodeString(src, 12)) { - errln("UnicodeString(const UnicodeString&, int32_t) failed"); - } - UnicodeString target2("folks"); - if (target2 != UnicodeString(src, 6, 5)) { - errln("UnicodeString(const UnicodeString&, int32_t, int32_t) failed"); - } - if (target1 != target2.setTo(src, 12)) { - errln("UnicodeString::setTo(const UnicodeString&, int32_t) failed"); - } - } + { + // test new 2.2 constructors and setTo function that parallel Java's substring function. + UnicodeString src("Hello folks how are you?"); + UnicodeString target1("how are you?"); + if (target1 != UnicodeString(src, 12)) { + errln("UnicodeString(const UnicodeString&, int32_t) failed"); + } + UnicodeString target2("folks"); + if (target2 != UnicodeString(src, 6, 5)) { + errln("UnicodeString(const UnicodeString&, int32_t, int32_t) failed"); + } + if (target1 != target2.setTo(src, 12)) { + errln("UnicodeString::setTo(const UnicodeString&, int32_t) failed"); + } + } + + { + // op+ is new in ICU 2.8 + UnicodeString s=UnicodeString("abc", "")+UnicodeString("def", "")+UnicodeString("ghi", ""); + if(s!=UnicodeString("abcdefghi", "")) { + errln("operator+(UniStr, UniStr) failed"); + } + } + + { + // tests for Jitterbug 2360 + // verify that APIs with source pointer + length accept length == -1 + // mostly test only where modified, only few functions did not already do this + if(UnicodeString("abc", -1, "")!=UnicodeString("abc", "")) { + errln("UnicodeString(codepageData, dataLength, codepage) does not work with dataLength==-1"); + } + + UChar buffer[10]={ 0x61, 0x62, 0x20ac, 0xd900, 0xdc05, 0, 0x62, 0xffff, 0xdbff, 0xdfff }; + UnicodeString s, t(buffer, -1, LENGTHOF(buffer)); + + if(s.setTo(buffer, -1, LENGTHOF(buffer)).length()!=u_strlen(buffer)) { + errln("UnicodeString.setTo(buffer, length, capacity) does not work with length==-1"); + } + if(t.length()!=u_strlen(buffer)) { + errln("UnicodeString(buffer, length, capacity) does not work with length==-1"); + } + + if(0!=s.caseCompare(buffer, -1, U_FOLD_CASE_DEFAULT)) { + errln("UnicodeString.caseCompare(const UChar *, length, options) does not work with length==-1"); + } + if(0!=s.caseCompare(0, s.length(), buffer, U_FOLD_CASE_DEFAULT)) { + errln("UnicodeString.caseCompare(start, _length, const UChar *, options) does not work"); + } + + buffer[u_strlen(buffer)]=0xe4; + UnicodeString u(buffer, -1, LENGTHOF(buffer)); + if(s.setTo(buffer, -1, LENGTHOF(buffer)).length()!=LENGTHOF(buffer)) { + errln("UnicodeString.setTo(buffer without NUL, length, capacity) does not work with length==-1"); + } + if(u.length()!=LENGTHOF(buffer)) { + errln("UnicodeString(buffer without NUL, length, capacity) does not work with length==-1"); + } + + static const char cs[]={ 0x61, (char)0xe4, (char)0x85, 0 }; + UConverter *cnv; + UErrorCode errorCode=U_ZERO_ERROR; + + cnv=ucnv_open("ISO-8859-1", &errorCode); + UnicodeString v(cs, -1, cnv, errorCode); + ucnv_close(cnv); + if(v!=CharsToUnicodeString("a\\xe4\\x85")) { + errln("UnicodeString(const char *, length, cnv, errorCode) does not work with length==-1"); + } + } + +#if U_CHARSET_IS_UTF8 + { + // Test the hardcoded-UTF-8 UnicodeString optimizations. + static const uint8_t utf8[]={ 0x61, 0xC3, 0xA4, 0xC3, 0x9F, 0xE4, 0xB8, 0x80, 0 }; + static const UChar utf16[]={ 0x61, 0xE4, 0xDF, 0x4E00 }; + UnicodeString from8a = UnicodeString((const char *)utf8); + UnicodeString from8b = UnicodeString((const char *)utf8, (int32_t)sizeof(utf8)-1); + UnicodeString from16(FALSE, utf16, LENGTHOF(utf16)); + if(from8a != from16 || from8b != from16) { + errln("UnicodeString(const char * U_CHARSET_IS_UTF8) failed"); + } + char buffer[16]; + int32_t length8=from16.extract(0, 0x7fffffff, buffer, (uint32_t)sizeof(buffer)); + if(length8!=((int32_t)sizeof(utf8)-1) || 0!=uprv_memcmp(buffer, utf8, sizeof(utf8))) { + errln("UnicodeString::extract(char * U_CHARSET_IS_UTF8) failed"); + } + length8=from16.extract(1, 2, buffer, (uint32_t)sizeof(buffer)); + if(length8!=4 || buffer[length8]!=0 || 0!=uprv_memcmp(buffer, utf8+1, length8)) { + errln("UnicodeString::extract(substring to char * U_CHARSET_IS_UTF8) failed"); + } + } +#endif } void @@ -386,6 +473,13 @@ UnicodeStringTest::TestExtract() if (test1.extract(11, 12, test4) != 12 || test4[12] != 0) { errln("UnicodeString.extract(char *) failed to return the correct size of destination buffer."); } + + // test proper pinning in extractBetween() + test1.extractBetween(-3, 7, test5); + if(test5!=UNICODE_STRING("Now is ", 7)) { + errln("UnicodeString.extractBetween(-3, 7) did not pin properly."); + } + test1.extractBetween(11, 23, test5); if (test1.extract(60, 71, test6) != 9) { errln("UnicodeString.extract() failed to return the correct size of destination buffer for end of buffer."); @@ -530,6 +624,15 @@ UnicodeStringTest::TestExtract() errln("UnicodeString::extract(UConverter) conversion failed (length=%ld, %s)", length, u_errorName(errorCode)); } + // Test again with just the converter name. + if( (length=s.extract(0, s.length(), buffer, sizeof(buffer), "UTF-8"))!=13 || + uprv_memcmp(buffer, expect, 13)!=0 || + buffer[13]!=0 || + U_FAILURE(errorCode) + ) { + errln("UnicodeString::extract(\"UTF-8\") conversion failed (length=%ld, %s)", + length, u_errorName(errorCode)); + } // try the constructor UnicodeString t(expect, sizeof(expect), cnv, errorCode); @@ -840,27 +943,27 @@ UnicodeStringTest::TestPrefixAndSuffix() errln("endsWith() failed: \"" + test2 + "\" shouldn't be a suffix of \"" + test1 + "\"."); } - if (!test1.endsWith(test3)) { - errln("endsWith(test3) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); - } - if (!test1.endsWith(test3, 0, INT32_MAX)) { - errln("endsWith(test3, 0, INT32_MAX) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); - } - - if(!test1.endsWith(test3.getBuffer(), test3.length())) { - errln("endsWith(test3.getBuffer(), test3.length()) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); - } - if(!test1.endsWith(test3.getTerminatedBuffer(), 0, -1)) { - errln("endsWith(test3.getTerminatedBuffer(), 0, -1) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); - } - - if (!test3.startsWith(test4)) { - errln("endsWith(test4) failed: \"" + test4 + "\" should be a prefix of \"" + test3 + "\"."); - } - - if (test4.startsWith(test3)) { - errln("startsWith(test3) failed: \"" + test3 + "\" shouldn't be a prefix of \"" + test4 + "\"."); - } + if (!test1.endsWith(test3)) { + errln("endsWith(test3) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); + } + if (!test1.endsWith(test3, 0, INT32_MAX)) { + errln("endsWith(test3, 0, INT32_MAX) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); + } + + if(!test1.endsWith(test3.getBuffer(), test3.length())) { + errln("endsWith(test3.getBuffer(), test3.length()) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); + } + if(!test1.endsWith(test3.getTerminatedBuffer(), 0, -1)) { + errln("endsWith(test3.getTerminatedBuffer(), 0, -1) failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\"."); + } + + if (!test3.startsWith(test4)) { + errln("endsWith(test4) failed: \"" + test4 + "\" should be a prefix of \"" + test3 + "\"."); + } + + if (test4.startsWith(test3)) { + errln("startsWith(test3) failed: \"" + test3 + "\" shouldn't be a prefix of \"" + test4 + "\"."); + } } void @@ -903,6 +1006,16 @@ UnicodeStringTest::TestReverse() if(test.char32At(0)!=0x1ed0 || test.char32At(1)!=0xc4 || test.char32At(2)!=0x1d15f || test.char32At(4)!=0x2f999) { errln("reverse() failed with supplementary characters"); } + + // Test case for ticket #8091: + // UnicodeString::reverse() failed to see a lead surrogate in the middle of + // an odd-length string that contains no other lead surrogates. + test=UNICODE_STRING_SIMPLE("ab\\U0001F4A9e").unescape(); + UnicodeString expected=UNICODE_STRING_SIMPLE("e\\U0001F4A9ba").unescape(); + test.reverse(); + if(test!=expected) { + errln("reverse() failed with only lead surrogate in the middle"); + } } void @@ -1017,6 +1130,30 @@ UnicodeStringTest::TestMiscellaneous() if(test1.hasMetaData() || UnicodeString().hasMetaData()) { errln("UnicodeString::hasMetaData() returns TRUE"); } + + // test getTerminatedBuffer() on a truncated, shared, heap-allocated string + test1=UNICODE_STRING_SIMPLE("abcdefghijklmnopqrstuvwxyz0123456789."); + test1.truncate(36); // ensure length()