diff options
Diffstat (limited to 'Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp')
-rw-r--r-- | Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp b/Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp index 3392a8fdfc2..7054b7f1e7a 100644 --- a/Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp +++ b/Build/source/libs/icu/icu-src/source/test/intltest/strcase.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * @@ -16,6 +18,7 @@ * Test file for string casing C++ API functions. */ +#include "unicode/std_string.h" #include "unicode/uchar.h" #include "unicode/ures.h" #include "unicode/uloc.h" @@ -28,6 +31,8 @@ #include "unicode/tstdtmod.h" #include "cmemory.h" +StringCaseTest::StringCaseTest() : GREEK_LOCALE_("el") {} + StringCaseTest::~StringCaseTest() {} void @@ -41,6 +46,10 @@ StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha TESTCASE_AUTO(TestCasing); #endif TESTCASE_AUTO(TestFullCaseFoldingIterator); + TESTCASE_AUTO(TestGreekUpper); + TESTCASE_AUTO(TestLongUpper); + TESTCASE_AUTO(TestMalformedUTF8); + TESTCASE_AUTO(TestBufferOverflow); TESTCASE_AUTO_END; } @@ -571,3 +580,271 @@ StringCaseTest::TestFullCaseFoldingIterator() { errln("error: FullCaseFoldingIterator yielded only %d (cp, full) pairs", (int)count); } } + +void +StringCaseTest::assertGreekUpper(const char *s, const char *expected) { + UnicodeString s16 = UnicodeString(s).unescape(); + UnicodeString expected16 = UnicodeString(expected).unescape(); + UnicodeString msg = UnicodeString("UnicodeString::toUpper/Greek(\"") + s16 + "\")"; + UnicodeString result16(s16); + result16.toUpper(GREEK_LOCALE_); + assertEquals(msg, expected16, result16); + + msg = UnicodeString("u_strToUpper/Greek(\"") + s16 + "\") cap="; + int32_t length = expected16.length(); + int32_t capacities[] = { + // Keep in sync with the UTF-8 capacities near the bottom of this function. + 0, length / 2, length - 1, length, length + 1 + }; + for (int32_t i = 0; i < UPRV_LENGTHOF(capacities); ++i) { + int32_t cap = capacities[i]; + UChar *dest16 = result16.getBuffer(expected16.length() + 1); + u_memset(dest16, 0x55AA, result16.getCapacity()); + UErrorCode errorCode = U_ZERO_ERROR; + length = u_strToUpper(dest16, cap, s16.getBuffer(), s16.length(), "el", &errorCode); + assertEquals(msg + cap, expected16.length(), length); + UErrorCode expectedErrorCode; + if (cap < expected16.length()) { + expectedErrorCode = U_BUFFER_OVERFLOW_ERROR; + } else if (cap == expected16.length()) { + expectedErrorCode = U_STRING_NOT_TERMINATED_WARNING; + } else { + expectedErrorCode = U_ZERO_ERROR; + assertEquals(msg + cap + " NUL", 0, dest16[length]); + } + assertEquals(msg + cap + " errorCode", expectedErrorCode, errorCode); + result16.releaseBuffer(length); + if (cap >= expected16.length()) { + assertEquals(msg + cap, expected16, result16); + } + } + +#if U_HAVE_STD_STRING + UErrorCode errorCode = U_ZERO_ERROR; + LocalUCaseMapPointer csm(ucasemap_open("el", 0, &errorCode)); + assertSuccess("ucasemap_open", errorCode); + std::string s8; + s16.toUTF8String(s8); + msg = UnicodeString("ucasemap_utf8ToUpper/Greek(\"") + s16 + "\")"; + char dest8[1000]; + length = ucasemap_utf8ToUpper(csm.getAlias(), dest8, UPRV_LENGTHOF(dest8), + s8.data(), s8.length(), &errorCode); + assertSuccess("ucasemap_utf8ToUpper", errorCode); + StringPiece result8(dest8, length); + UnicodeString result16From8 = UnicodeString::fromUTF8(result8); + assertEquals(msg, expected16, result16From8); + + msg += " cap="; + capacities[1] = length / 2; + capacities[2] = length - 1; + capacities[3] = length; + capacities[4] = length + 1; + char dest8b[1000]; + int32_t expected8Length = length; // Assuming the previous call worked. + for (int32_t i = 0; i < UPRV_LENGTHOF(capacities); ++i) { + int32_t cap = capacities[i]; + memset(dest8b, 0x5A, UPRV_LENGTHOF(dest8b)); + UErrorCode errorCode = U_ZERO_ERROR; + length = ucasemap_utf8ToUpper(csm.getAlias(), dest8b, cap, + s8.data(), s8.length(), &errorCode); + assertEquals(msg + cap, expected8Length, length); + UErrorCode expectedErrorCode; + if (cap < expected8Length) { + expectedErrorCode = U_BUFFER_OVERFLOW_ERROR; + } else if (cap == expected8Length) { + expectedErrorCode = U_STRING_NOT_TERMINATED_WARNING; + } else { + expectedErrorCode = U_ZERO_ERROR; + assertEquals(msg + cap + " NUL", 0, dest8b[length]); + } + assertEquals(msg + cap + " errorCode", expectedErrorCode, errorCode); + if (cap >= expected8Length) { + assertEquals(msg + cap + " (memcmp)", 0, memcmp(dest8, dest8b, expected8Length)); + } + } +#endif +} + +void +StringCaseTest::TestGreekUpper() { + // See UCharacterCaseTest.java for human-readable strings. + + // http://bugs.icu-project.org/trac/ticket/5456 + assertGreekUpper("\\u03AC\\u03B4\\u03B9\\u03BA\\u03BF\\u03C2, " + "\\u03BA\\u03B5\\u03AF\\u03BC\\u03B5\\u03BD\\u03BF, " + "\\u03AF\\u03C1\\u03B9\\u03B4\\u03B1", + "\\u0391\\u0394\\u0399\\u039A\\u039F\\u03A3, " + "\\u039A\\u0395\\u0399\\u039C\\u0395\\u039D\\u039F, " + "\\u0399\\u03A1\\u0399\\u0394\\u0391"); + // https://bugzilla.mozilla.org/show_bug.cgi?id=307039 + // https://bug307039.bmoattachments.org/attachment.cgi?id=194893 + assertGreekUpper("\\u03A0\\u03B1\\u03C4\\u03AC\\u03C4\\u03B1", + "\\u03A0\\u0391\\u03A4\\u0391\\u03A4\\u0391"); + assertGreekUpper("\\u0391\\u03AD\\u03C1\\u03B1\\u03C2, " + "\\u039C\\u03C5\\u03C3\\u03C4\\u03AE\\u03C1\\u03B9\\u03BF, " + "\\u03A9\\u03C1\\u03B1\\u03AF\\u03BF", + "\\u0391\\u0395\\u03A1\\u0391\\u03A3, " + "\\u039C\\u03A5\\u03A3\\u03A4\\u0397\\u03A1\\u0399\\u039F, " + "\\u03A9\\u03A1\\u0391\\u0399\\u039F"); + assertGreekUpper("\\u039C\\u03B1\\u0390\\u03BF\\u03C5, \\u03A0\\u03CC\\u03C1\\u03BF\\u03C2, " + "\\u03A1\\u03CD\\u03B8\\u03BC\\u03B9\\u03C3\\u03B7", + "\\u039C\\u0391\\u03AA\\u039F\\u03A5, \\u03A0\\u039F\\u03A1\\u039F\\u03A3, " + "\\u03A1\\u03A5\\u0398\\u039C\\u0399\\u03A3\\u0397"); + assertGreekUpper("\\u03B0, \\u03A4\\u03B7\\u03C1\\u03CE, \\u039C\\u03AC\\u03B9\\u03BF\\u03C2", + "\\u03AB, \\u03A4\\u0397\\u03A1\\u03A9, \\u039C\\u0391\\u03AA\\u039F\\u03A3"); + assertGreekUpper("\\u03AC\\u03C5\\u03BB\\u03BF\\u03C2", + "\\u0391\\u03AB\\u039B\\u039F\\u03A3"); + assertGreekUpper("\\u0391\\u03AB\\u039B\\u039F\\u03A3", + "\\u0391\\u03AB\\u039B\\u039F\\u03A3"); + assertGreekUpper("\\u0386\\u03BA\\u03BB\\u03B9\\u03C4\\u03B1 " + "\\u03C1\\u03AE\\u03BC\\u03B1\\u03C4\\u03B1 \\u03AE " + "\\u03AC\\u03BA\\u03BB\\u03B9\\u03C4\\u03B5\\u03C2 " + "\\u03BC\\u03B5\\u03C4\\u03BF\\u03C7\\u03AD\\u03C2", + "\\u0391\\u039A\\u039B\\u0399\\u03A4\\u0391 " + "\\u03A1\\u0397\\u039C\\u0391\\u03A4\\u0391 \\u0397\\u0301 " + "\\u0391\\u039A\\u039B\\u0399\\u03A4\\u0395\\u03A3 " + "\\u039C\\u0395\\u03A4\\u039F\\u03A7\\u0395\\u03A3"); + // http://www.unicode.org/udhr/d/udhr_ell_monotonic.html + assertGreekUpper("\\u0395\\u03C0\\u03B5\\u03B9\\u03B4\\u03AE \\u03B7 " + "\\u03B1\\u03BD\\u03B1\\u03B3\\u03BD\\u03CE\\u03C1\\u03B9\\u03C3\\u03B7 " + "\\u03C4\\u03B7\\u03C2 \\u03B1\\u03BE\\u03B9\\u03BF\\u03C0\\u03C1\\u03AD" + "\\u03C0\\u03B5\\u03B9\\u03B1\\u03C2", + "\\u0395\\u03A0\\u0395\\u0399\\u0394\\u0397 \\u0397 " + "\\u0391\\u039D\\u0391\\u0393\\u039D\\u03A9\\u03A1\\u0399\\u03A3\\u0397 " + "\\u03A4\\u0397\\u03A3 \\u0391\\u039E\\u0399\\u039F\\u03A0\\u03A1\\u0395" + "\\u03A0\\u0395\\u0399\\u0391\\u03A3"); + assertGreekUpper("\\u03BD\\u03BF\\u03BC\\u03B9\\u03BA\\u03BF\\u03CD \\u03AE " + "\\u03B4\\u03B9\\u03B5\\u03B8\\u03BD\\u03BF\\u03CD\\u03C2", + "\\u039D\\u039F\\u039C\\u0399\\u039A\\u039F\\u03A5 \\u0397\\u0301 " + "\\u0394\\u0399\\u0395\\u0398\\u039D\\u039F\\u03A5\\u03A3"); + // http://unicode.org/udhr/d/udhr_ell_polytonic.html + assertGreekUpper("\\u1F18\\u03C0\\u03B5\\u03B9\\u03B4\\u1F74 \\u1F21 " + "\\u1F00\\u03BD\\u03B1\\u03B3\\u03BD\\u1F7D\\u03C1\\u03B9\\u03C3\\u03B7", + "\\u0395\\u03A0\\u0395\\u0399\\u0394\\u0397 \\u0397 " + "\\u0391\\u039D\\u0391\\u0393\\u039D\\u03A9\\u03A1\\u0399\\u03A3\\u0397"); + assertGreekUpper("\\u03BD\\u03BF\\u03BC\\u03B9\\u03BA\\u03BF\\u1FE6 \\u1F22 " + "\\u03B4\\u03B9\\u03B5\\u03B8\\u03BD\\u03BF\\u1FE6\\u03C2", + "\\u039D\\u039F\\u039C\\u0399\\u039A\\u039F\\u03A5 \\u0397\\u0301 " + "\\u0394\\u0399\\u0395\\u0398\\u039D\\u039F\\u03A5\\u03A3"); + // From Google bug report + assertGreekUpper("\\u039D\\u03AD\\u03BF, " + "\\u0394\\u03B7\\u03BC\\u03B9\\u03BF\\u03C5\\u03C1\\u03B3\\u03AF\\u03B1", + "\\u039D\\u0395\\u039F, " + "\\u0394\\u0397\\u039C\\u0399\\u039F\\u03A5\\u03A1\\u0393\\u0399\\u0391"); + // http://crbug.com/234797 + assertGreekUpper("\\u0395\\u03BB\\u03AC\\u03C4\\u03B5 \\u03BD\\u03B1 \\u03C6\\u03AC\\u03C4\\u03B5 " + "\\u03C4\\u03B1 \\u03BA\\u03B1\\u03BB\\u03CD\\u03C4\\u03B5\\u03C1\\u03B1 " + "\\u03C0\\u03B1\\u03CA\\u03B4\\u03AC\\u03BA\\u03B9\\u03B1!", + "\\u0395\\u039B\\u0391\\u03A4\\u0395 \\u039D\\u0391 \\u03A6\\u0391\\u03A4\\u0395 " + "\\u03A4\\u0391 \\u039A\\u0391\\u039B\\u03A5\\u03A4\\u0395\\u03A1\\u0391 " + "\\u03A0\\u0391\\u03AA\\u0394\\u0391\\u039A\\u0399\\u0391!"); + assertGreekUpper("\\u039C\\u03B1\\u0390\\u03BF\\u03C5, \\u03C4\\u03C1\\u03CC\\u03BB\\u03B5\\u03CA", + "\\u039C\\u0391\\u03AA\\u039F\\u03A5, \\u03A4\\u03A1\\u039F\\u039B\\u0395\\u03AA"); + assertGreekUpper("\\u03A4\\u03BF \\u03AD\\u03BD\\u03B1 \\u03AE \\u03C4\\u03BF " + "\\u03AC\\u03BB\\u03BB\\u03BF.", + "\\u03A4\\u039F \\u0395\\u039D\\u0391 \\u0397\\u0301 \\u03A4\\u039F " + "\\u0391\\u039B\\u039B\\u039F."); + // http://multilingualtypesetting.co.uk/blog/greek-typesetting-tips/ + assertGreekUpper("\\u03C1\\u03C9\\u03BC\\u03AD\\u03B9\\u03BA\\u03B1", + "\\u03A1\\u03A9\\u039C\\u0395\\u03AA\\u039A\\u0391"); +} + +void +StringCaseTest::TestLongUpper() { + if (quick) { + logln("not exhaustive mode: skipping this test"); + return; + } + // Ticket #12663, crash with an extremely long string where + // U+0390 maps to 0399 0308 0301 so that the result is three times as long + // and overflows an int32_t. + int32_t length = 0x40000004; // more than 1G UChars + UnicodeString s(length, (UChar32)0x390, length); + UnicodeString result; + UChar *dest = result.getBuffer(length + 1); + if (s.isBogus() || dest == NULL) { + logln("Out of memory, unable to run this test on this machine."); + return; + } + IcuTestErrorCode errorCode(*this, "TestLongUpper"); + int32_t destLength = u_strToUpper(dest, result.getCapacity(), + s.getBuffer(), s.length(), "", errorCode); + result.releaseBuffer(destLength); + if (errorCode.reset() != U_INDEX_OUTOFBOUNDS_ERROR) { + errln("expected U_INDEX_OUTOFBOUNDS_ERROR, got %s (destLength is undefined, got %ld)", + errorCode.errorName(), (long)destLength); + } +} + +void StringCaseTest::TestMalformedUTF8() { + // ticket #12639 + IcuTestErrorCode errorCode(*this, "TestMalformedUTF8"); + LocalUCaseMapPointer csm(ucasemap_open("en", U_TITLECASE_NO_BREAK_ADJUSTMENT, errorCode)); + if (errorCode.isFailure()) { + errln("ucasemap_open(English) failed - %s", errorCode.errorName()); + return; + } + char src[1] = { (char)0x85 }; // malformed UTF-8 + char dest[3] = { 0, 0, 0 }; + int32_t destLength = ucasemap_utf8ToTitle(csm.getAlias(), dest, 3, src, 1, errorCode); + if (errorCode.isFailure() || destLength != 1 || dest[0] != src[0]) { + errln("ucasemap_utf8ToTitle(\\x85) failed: %s destLength=%d dest[0]=0x%02x", + errorCode.errorName(), (int)destLength, dest[0]); + } + + errorCode.reset(); + dest[0] = 0; + destLength = ucasemap_utf8ToLower(csm.getAlias(), dest, 3, src, 1, errorCode); + if (errorCode.isFailure() || destLength != 1 || dest[0] != src[0]) { + errln("ucasemap_utf8ToLower(\\x85) failed: %s destLength=%d dest[0]=0x%02x", + errorCode.errorName(), (int)destLength, dest[0]); + } + + errorCode.reset(); + dest[0] = 0; + destLength = ucasemap_utf8ToUpper(csm.getAlias(), dest, 3, src, 1, errorCode); + if (errorCode.isFailure() || destLength != 1 || dest[0] != src[0]) { + errln("ucasemap_utf8ToUpper(\\x85) failed: %s destLength=%d dest[0]=0x%02x", + errorCode.errorName(), (int)destLength, dest[0]); + } + + errorCode.reset(); + dest[0] = 0; + destLength = ucasemap_utf8FoldCase(csm.getAlias(), dest, 3, src, 1, errorCode); + if (errorCode.isFailure() || destLength != 1 || dest[0] != src[0]) { + errln("ucasemap_utf8FoldCase(\\x85) failed: %s destLength=%d dest[0]=0x%02x", + errorCode.errorName(), (int)destLength, dest[0]); + } +} + +void StringCaseTest::TestBufferOverflow() { + // Ticket #12849, incorrect result from Title Case preflight operation, + // when buffer overflow error is expected. + IcuTestErrorCode errorCode(*this, "TestBufferOverflow"); + LocalUCaseMapPointer csm(ucasemap_open("en", 0, errorCode)); + if (errorCode.isFailure()) { + errln("ucasemap_open(English) failed - %s", errorCode.errorName()); + return; + } + + UnicodeString data("hello world"); + int32_t result = ucasemap_toTitle(csm.getAlias(), NULL, 0, data.getBuffer(), data.length(), errorCode); + if (errorCode.get() != U_BUFFER_OVERFLOW_ERROR || result != data.length()) { + errln("%s:%d ucasemap_toTitle(\"hello world\") failed: " + "expected (U_BUFFER_OVERFLOW_ERROR, %d), got (%s, %d)", + __FILE__, __LINE__, data.length(), errorCode.errorName(), result); + } + errorCode.reset(); + +#if U_HAVE_STD_STRING + std::string data_utf8; + data.toUTF8String(data_utf8); + result = ucasemap_utf8ToTitle(csm.getAlias(), NULL, 0, data_utf8.c_str(), data_utf8.length(), errorCode); + if (errorCode.get() != U_BUFFER_OVERFLOW_ERROR || result != (int32_t)data_utf8.length()) { + errln("%s:%d ucasemap_toTitle(\"hello world\") failed: " + "expected (U_BUFFER_OVERFLOW_ERROR, %d), got (%s, %d)", + __FILE__, __LINE__, data_utf8.length(), errorCode.errorName(), result); + } + errorCode.reset(); +#endif // U_HAVE_STD_STRING +} |