X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..2be6500137966bba13903b24204a44804f9e133a:/icuSources/test/intltest/ustrtest.cpp?ds=sidebyside diff --git a/icuSources/test/intltest/ustrtest.cpp b/icuSources/test/intltest/ustrtest.cpp index 17c7b2b7..6a27ea3c 100644 --- a/icuSources/test/intltest/ustrtest.cpp +++ b/icuSources/test/intltest/ustrtest.cpp @@ -1,27 +1,27 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2004, International Business Machines Corporation and + * Copyright (c) 1997-2012, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ #include "ustrtest.h" +#include "unicode/appendable.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 "unicode/utf16.h" #include "cmemory.h" #include "charstr.h" #if 0 #include "unicode/ustream.h" -#if U_IOSTREAM_SOURCE >= 199711 #include using namespace std; -#elif U_IOSTREAM_SOURCE >= 198506 -#include -#endif #endif @@ -56,7 +56,14 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* & 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 = "TestCharString"; if (exec) TestCharString(); 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; + case 20: name = "TestAppendable"; if (exec) TestAppendable(); break; + case 21: name = "TestUnicodeStringImplementsAppendable"; if (exec) TestUnicodeStringImplementsAppendable(); break; + case 22: name = "TestSizeofUnicodeString"; if (exec) TestSizeofUnicodeString(); break; + case 23: name = "TestStartsWithAndEndsWithNulTerminated"; if (exec) TestStartsWithAndEndsWithNulTerminated(); break; default: name = ""; break; //needed to end loop } @@ -106,9 +113,9 @@ UnicodeStringTest::TestBasicManipulation() errln("operator+=() failed: expected \"" + expectedValue + "\"\n,got \"" + test2 + "\""); if (test1.length() != 70) - errln("length() failed: expected 70, got " + test1.length()); + errln(UnicodeString("length() failed: expected 70, got ") + test1.length()); if (test2.length() != 30) - errln("length() failed: expected 30, got " + test2.length()); + errln(UnicodeString("length() failed: expected 30, got ") + test2.length()); UnicodeString test3; test3.append((UChar32)0x20402); @@ -116,7 +123,7 @@ UnicodeStringTest::TestBasicManipulation() errln((UnicodeString)"append failed for UChar32, expected \"\\\\ud841\\\\udc02\", got " + prettify(test3)); } if(test3.length() != 2){ - errln("append or length failed for UChar32, expected 2, got " + test3.length()); + errln(UnicodeString("append or length failed for UChar32, expected 2, got ") + test3.length()); } test3.append((UChar32)0x0074); if(test3 != CharsToUnicodeString("\\uD841\\uDC02t")){ @@ -224,10 +231,33 @@ UnicodeStringTest::TestBasicManipulation() cnv=ucnv_open("ISO-8859-1", &errorCode); UnicodeString v(cs, -1, cnv, errorCode); ucnv_close(cnv); - if(v!=UnicodeString("a\\xe4\\x85").unescape()) { + 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 @@ -691,14 +721,15 @@ UnicodeStringTest::TestSearching() (startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0) ; if (occurrences != 6) - errln("indexOf failed: expected to find 6 occurrences, found " + occurrences); - + errln(UnicodeString("indexOf failed: expected to find 6 occurrences, found ") + occurrences); + for ( occurrences = 0, startPos = 10; startPos != -1 && startPos < test1.length(); (startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0) ; if (occurrences != 4) - errln("indexOf with starting offset failed: expected to find 4 occurrences, found " + occurrences); + errln(UnicodeString("indexOf with starting offset failed: " + "expected to find 4 occurrences, found ") + occurrences); int32_t endPos = 28; for ( occurrences = 0, startPos = 5; @@ -706,7 +737,8 @@ UnicodeStringTest::TestSearching() (startPos = test1.indexOf(test2, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 4) : 0) ; if (occurrences != 4) - errln("indexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences); + errln(UnicodeString("indexOf with starting and ending offsets failed: " + "expected to find 4 occurrences, found ") + occurrences); //using UChar32 string for ( startPos=0, occurrences=0; @@ -721,7 +753,7 @@ UnicodeStringTest::TestSearching() (startPos = test3.indexOf(test4, startPos)) != -1 ? (++occurrences, startPos += 2) : 0) ; if (occurrences != 2) - errln("indexOf failed: expected to find 2 occurrences, found " + occurrences); + errln(UnicodeString("indexOf failed: expected to find 2 occurrences, found ") + occurrences); //--- for ( occurrences = 0, startPos = 0; @@ -729,21 +761,24 @@ UnicodeStringTest::TestSearching() (startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0) ; if (occurrences != 16) - errln("indexOf with character failed: expected to find 16 occurrences, found " + occurrences); + errln(UnicodeString("indexOf with character failed: " + "expected to find 16 occurrences, found ") + occurrences); for ( occurrences = 0, startPos = 10; startPos != -1 && startPos < test1.length(); (startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0) ; if (occurrences != 12) - errln("indexOf with character & start offset failed: expected to find 12 occurrences, found " + occurrences); + errln(UnicodeString("indexOf with character & start offset failed: " + "expected to find 12 occurrences, found ") + occurrences); for ( occurrences = 0, startPos = 5, endPos = 28; startPos != -1 && startPos < test1.length(); (startPos = test1.indexOf(testChar, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 1) : 0) ; if (occurrences != 10) - errln("indexOf with character & start & end offsets failed: expected to find 10 occurrences, found " + occurrences); + errln(UnicodeString("indexOf with character & start & end offsets failed: " + "expected to find 10 occurrences, found ") + occurrences); //testing for UChar32 UnicodeString subString; @@ -786,14 +821,16 @@ UnicodeStringTest::TestSearching() (startPos = test1.lastIndexOf(test2, 5, startPos - 5)) != -1 ? ++occurrences : 0) ; if (occurrences != 4) - errln("lastIndexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences); + errln(UnicodeString("lastIndexOf with starting and ending offsets failed: " + "expected to find 4 occurrences, found ") + occurrences); for ( occurrences = 0, startPos = 32; startPos != -1; (startPos = test1.lastIndexOf(testChar, 5, startPos - 5)) != -1 ? ++occurrences : 0) ; if (occurrences != 11) - errln("lastIndexOf with character & start & end offsets failed: expected to find 11 occurrences, found " + occurrences); + errln(UnicodeString("lastIndexOf with character & start & end offsets failed: " + "expected to find 11 occurrences, found ") + occurrences); //testing UChar32 startPos=test3.length(); @@ -915,27 +952,38 @@ 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 +UnicodeStringTest::TestStartsWithAndEndsWithNulTerminated() { + UnicodeString test("abcde"); + const UChar ab[] = { 0x61, 0x62, 0 }; + const UChar de[] = { 0x64, 0x65, 0 }; + assertTrue("abcde.startsWith(ab, -1)", test.startsWith(ab, -1)); + assertTrue("abcde.startsWith(ab, 0, -1)", test.startsWith(ab, 0, -1)); + assertTrue("abcde.endsWith(de, -1)", test.endsWith(de, -1)); + assertTrue("abcde.endsWith(de, 0, -1)", test.endsWith(de, 0, -1)); } void @@ -978,6 +1026,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 @@ -1092,6 +1150,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()