ICU-10562 UText problem with shallow clone.

X-SVN-Rev: 34747
This commit is contained in:
Andy Heninger 2013-12-12 01:04:54 +00:00
parent be63df401e
commit 095a44d774
3 changed files with 88 additions and 54 deletions

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 2005-2012, International Business Machines
* Copyright (C) 2005-2013, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -806,6 +806,11 @@ shallowTextClone(UText * dest, const UText * src, UErrorCode * status) {
adjustPointer(dest, &dest->r, src);
adjustPointer(dest, (const void **)&dest->chunkContents, src);
// The newly shallow-cloned UText does _not_ own the underlying storage for the text.
// (The source for the clone may or may not have owned the text.)
dest->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT);
return dest;
}
@ -1575,7 +1580,7 @@ utf8TextClose(UText *ut) {
U_CDECL_END
static const struct UTextFuncs utf8Funcs =
static const struct UTextFuncs utf8Funcs =
{
sizeof(UTextFuncs),
0, 0, 0, // Reserved alignment padding
@ -1862,7 +1867,7 @@ repTextExtract(UText *ut,
UnicodeString buffer(dest, 0, destCapacity); // writable alias
rep->extractBetween(start32, limit32, buffer);
repTextAccess(ut, limit32, TRUE);
return u_terminateUChars(dest, destCapacity, length, status);
}
@ -1984,7 +1989,7 @@ repTextCopy(UText *ut,
repTextAccess(ut, nativeIterIndex, TRUE);
}
static const struct UTextFuncs repFuncs =
static const struct UTextFuncs repFuncs =
{
sizeof(UTextFuncs),
0, 0, 0, // Reserved alignment padding
@ -1992,8 +1997,8 @@ static const struct UTextFuncs repFuncs =
repTextLength,
repTextAccess,
repTextExtract,
repTextReplace,
repTextCopy,
repTextReplace,
repTextCopy,
NULL, // MapOffsetToNative,
NULL, // MapIndexToUTF16,
repTextClose,
@ -2235,7 +2240,7 @@ unistrTextCopy(UText *ut,
}
static const struct UTextFuncs unistrFuncs =
static const struct UTextFuncs unistrFuncs =
{
sizeof(UTextFuncs),
0, 0, 0, // Reserved alignment padding
@ -2243,8 +2248,8 @@ static const struct UTextFuncs unistrFuncs =
unistrTextLength,
unistrTextAccess,
unistrTextExtract,
unistrTextReplace,
unistrTextCopy,
unistrTextReplace,
unistrTextCopy,
NULL, // MapOffsetToNative,
NULL, // MapIndexToUTF16,
unistrTextClose,
@ -2548,7 +2553,7 @@ ucstrTextExtract(UText *ut,
return di;
}
static const struct UTextFuncs ucstrFuncs =
static const struct UTextFuncs ucstrFuncs =
{
sizeof(UTextFuncs),
0, 0, 0, // Reserved alignment padding
@ -2765,14 +2770,14 @@ charIterTextExtract(UText *ut,
}
srci += len;
}
charIterTextAccess(ut, copyLimit, TRUE);
u_terminateUChars(dest, destCapacity, desti, status);
return desti;
}
static const struct UTextFuncs charIterFuncs =
static const struct UTextFuncs charIterFuncs =
{
sizeof(UTextFuncs),
0, 0, 0, // Reserved alignment padding
@ -2832,6 +2837,3 @@ utext_openCharacterIterator(UText *ut, CharacterIterator *ci, UErrorCode *status
}
return ut;
}

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2005-2012, International Business Machines Corporation and
* COPYRIGHT:
* Copyright (c) 2005-2013, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/************************************************************************
@ -57,6 +57,8 @@ UTextTest::runIndexedTest(int32_t index, UBool exec,
if (exec) Ticket5560(); break;
case 4: name = "Ticket6847";
if (exec) Ticket6847(); break;
case 5: name = "Ticket10562";
if (exec) Ticket10562(); break;
default: name = ""; break;
}
}
@ -126,7 +128,7 @@ void UTextTest::TextTest() {
for (i=0; i<1000; i++) {
int len8 = m_rand()%4 + 1;
switch (len8) {
case 1:
case 1:
c1 = (c1+1)%0x80;
// don't put 0 into string (0 terminated strings for some tests)
// don't put '\', will cause unescape() to fail.
@ -178,7 +180,7 @@ void UTextTest::TestString(const UnicodeString &s) {
j++;
cpCount++;
}
cpMap[j].nativeIdx = i; // position following the last char in utf-16 string.
cpMap[j].nativeIdx = i; // position following the last char in utf-16 string.
// UChar * test, null terminated
@ -239,7 +241,7 @@ void UTextTest::TestString(const UnicodeString &s) {
TestAccess(sa, ut, cpCount, cpMap);
utext_close(ut);
delete ci;
// Fragmented UnicodeString (Chunk size of one)
//
@ -291,13 +293,13 @@ void UTextTest::TestString(const UnicodeString &s) {
//
// This function runs a whole series of opertions on each incoming UText.
// The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
//
//
void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nativeMap, m *u16Map) {
TEST_ASSERT(utext_isWritable(ut) == TRUE);
int srcLengthType; // Loop variables for selecting the postion and length
int srcPosType; // of the block to operate on within the source text.
int destPosType;
int destPosType;
int srcIndex = 0; // Code Point indexes of the block to operate on for
int srcLength = 0; // a specific test.
@ -330,7 +332,7 @@ void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nati
case 5: srcIndex = cpCount / 2; break;
}
if (srcIndex < 0 || srcIndex + srcLength > cpCount) {
// filter out bogus test cases -
// filter out bogus test cases -
// those with a source range that falls of an edge of the string.
continue;
}
@ -401,7 +403,7 @@ void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nati
//
void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
int32_t nativeStart, int32_t nativeLimit, int32_t nativeDest,
int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
{
UErrorCode status = U_ZERO_ERROR;
UText *targetUT = NULL;
@ -438,7 +440,7 @@ void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
// Compare the results of the two parallel tests
int32_t usi = 0; // UnicodeString postion, utf-16 index.
int64_t uti = 0; // UText position, native index.
int32_t cpi; // char32 position (code point index)
int32_t cpi; // char32 position (code point index)
UChar32 usc; // code point from Unicode String
UChar32 utc; // code point from UText
utext_setNativeIndex(targetUT, 0);
@ -467,15 +469,15 @@ void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
cleanupAndReturn:
utext_close(targetUT);
}
//
// TestReplace Test a single Replace operation.
//
void UTextTest::TestReplace(
const UnicodeString &us, // reference UnicodeString in which to do the replace
const UnicodeString &us, // reference UnicodeString in which to do the replace
UText *ut, // UnicodeText object under test.
int32_t nativeStart, // Range to be replaced, in UText native units.
int32_t nativeStart, // Range to be replaced, in UText native units.
int32_t nativeLimit,
int32_t u16Start, // Range to be replaced, in UTF-16 units
int32_t u16Limit, // for use in the reference UnicodeString.
@ -495,7 +497,7 @@ void UTextTest::TestReplace(
UnicodeString targetUS(us); // And copy the reference string.
//
// Do the replace operation in the Unicode String, to
// Do the replace operation in the Unicode String, to
// produce a reference result.
//
targetUS.replace(u16Start, u16Limit-u16Start, repStr);
@ -514,7 +516,7 @@ void UTextTest::TestReplace(
//
int32_t usi = 0; // UnicodeString postion, utf-16 index.
int64_t uti = 0; // UText position, native index.
int32_t cpi; // char32 position (code point index)
int32_t cpi; // char32 position (code point index)
UChar32 usc; // code point from Unicode String
UChar32 utc; // code point from UText
int64_t expectedNativeLength = 0;
@ -572,7 +574,7 @@ void UTextTest::TestAccess(const UnicodeString &us, UText *ut, int cpCount, m *c
}
utext_close(deepClone);
}
//
// TestAccessNoClone() Test the read only access functions on a UText.
@ -607,7 +609,7 @@ void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCoun
foundIndex = utext_getNativeIndex(ut);
TEST_ASSERT(expectedIndex == foundIndex);
expectedC = cpMap[i].cp;
foundC = utext_next32(ut);
foundC = utext_next32(ut);
TEST_ASSERT(expectedC == foundC);
foundIndex = utext_getPreviousNativeIndex(ut);
TEST_ASSERT(expectedIndex == foundIndex);
@ -617,7 +619,7 @@ void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCoun
}
foundC = utext_next32(ut);
TEST_ASSERT(foundC == U_SENTINEL);
// Repeat above, using macros
utext_setNativeIndex(ut, 0);
for (i=0; i<cpCount; i++) {
@ -625,7 +627,7 @@ void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCoun
foundIndex = UTEXT_GETNATIVEINDEX(ut);
TEST_ASSERT(expectedIndex == foundIndex);
expectedC = cpMap[i].cp;
foundC = UTEXT_NEXT32(ut);
foundC = UTEXT_NEXT32(ut);
TEST_ASSERT(expectedC == foundC);
if (gFailed) {
return;
@ -837,11 +839,11 @@ void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCoun
//
// ErrorTest() Check various error and edge cases.
//
void UTextTest::ErrorTest()
void UTextTest::ErrorTest()
{
// Close of an unitialized UText. Shouldn't blow up.
{
UText ut;
UText ut;
memset(&ut, 0, sizeof(UText));
utext_close(&ut);
utext_close(NULL);
@ -906,7 +908,7 @@ void UTextTest::ErrorTest()
{
UErrorCode status = U_ZERO_ERROR;
UText ut = UTEXT_INITIALIZER;
utext_openUChars(&ut, NULL, 5, &status);
TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
@ -930,7 +932,7 @@ void UTextTest::ErrorTest()
{
UErrorCode status = U_ZERO_ERROR;
UText *ut = NULL;
const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
UChar32 c;
ut = utext_openUTF8(NULL, badUTF8, -1, &status);
@ -993,8 +995,8 @@ void UTextTest::ErrorTest()
int32_t startMap[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
int32_t nextMap[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
int32_t prevMap[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
UChar32 c32Map[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
UChar32 pr32Map[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
UChar32 c32Map[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
UChar32 pr32Map[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
// extractLen is the size, in UChars, of what will be extracted between index and index+1.
// is zero when both index positions lie within the same code point.
@ -1031,7 +1033,7 @@ void UTextTest::ErrorTest()
int64_t cpIndex = utext_getNativeIndex(ut);
TEST_ASSERT(cpIndex == nextMap[i]);
}
// check utext_previous32From
for (i=0; i<startMapLimit; i++) {
gTestNum++;
@ -1064,13 +1066,13 @@ void UTextTest::ErrorTest()
{ // Similar test, with utf16 instead of utf8
// TODO: merge the common parts of these tests.
UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
int32_t startMap[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
int32_t nextMap[] = { 1, 3, 3, 4, 6, 6, 6, 6};
int32_t prevMap[] = { 0, 0, 0, 1, 3, 3, 4, 4};
UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
int32_t exLen[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
u16str = u16str.unescape();
@ -1101,7 +1103,7 @@ void UTextTest::ErrorTest()
int64_t cpIndex = utext_getNativeIndex(ut);
TEST_ASSERT(cpIndex == nextMap[i]);
}
// check utext_previous32From
for (i=0; i<startMapLimit; i++) {
UChar32 c32 = utext_previous32From(ut, i);
@ -1132,13 +1134,13 @@ void UTextTest::ErrorTest()
{ // Similar test, with UText over Replaceable
// TODO: merge the common parts of these tests.
UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
int32_t startMap[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
int32_t nextMap[] = { 1, 3, 3, 4, 6, 6, 6, 6};
int32_t prevMap[] = { 0, 0, 0, 1, 3, 3, 4, 4};
UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
int32_t exLen[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
u16str = u16str.unescape();
@ -1169,7 +1171,7 @@ void UTextTest::ErrorTest()
int64_t cpIndex = utext_getNativeIndex(ut);
TEST_ASSERT(cpIndex == nextMap[i]);
}
// check utext_previous32From
for (i=0; i<startMapLimit; i++) {
UChar32 c32 = utext_previous32From(ut, i);
@ -1205,7 +1207,7 @@ void UTextTest::FreezeTest() {
//
UnicodeString ustr("Hello, World.");
const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
const UChar u16str[] = {(UChar)0x31, (UChar)0x32, (UChar)0x44, 0};
UErrorCode status = U_ZERO_ERROR;
@ -1237,7 +1239,7 @@ void UTextTest::FreezeTest() {
TEST_ASSERT(writable == FALSE);
utext_copy(ut, 1, 2, 0, TRUE, &status);
TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
status = U_ZERO_ERROR;
ut = utext_openUnicodeString(ut, &ustr, &status);
TEST_SUCCESS(status);
@ -1320,7 +1322,7 @@ fragTextAccess(UText *ut, int64_t index, UBool forward) {
ut->chunkNativeStart = index-1;
ut->chunkNativeLimit = index;
return true;
}
}
ut->b = 0;
ut->chunkOffset = 0;
ut->chunkLength = 0;
@ -1387,7 +1389,7 @@ openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) {
// 1. Create an inital UText
// 2. Deep clone it. Contents should match original.
// 3. Reset original to something different.
// 4. Check that clone contents did not change.
// 4. Check that clone contents did not change.
//
void UTextTest::Ticket5560() {
/* The following two strings are in UTF-8 even on EBCDIC platforms. */
@ -1452,3 +1454,32 @@ void UTextTest::Ticket6847() {
utext_close(ut);
}
void UTextTest::Ticket10562() {
// Note: failures show as a heap error when the test is run under valgrind.
UErrorCode status = U_ZERO_ERROR;
const char *utf8_string = "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
UText *utf8Text = utext_openUTF8(NULL, utf8_string, -1, &status);
TEST_SUCCESS(status);
UText *deepClone = utext_clone(NULL, utf8Text, TRUE, FALSE, &status);
TEST_SUCCESS(status);
UText *shallowClone = utext_clone(NULL, deepClone, FALSE, FALSE, &status);
TEST_SUCCESS(status);
utext_close(shallowClone);
utext_close(deepClone);
utext_close(utf8Text);
status = U_ZERO_ERROR;
UnicodeString usString("Hello, World.");
UText *usText = utext_openUnicodeString(NULL, &usString, &status);
TEST_SUCCESS(status);
UText *usDeepClone = utext_clone(NULL, usText, TRUE, FALSE, &status);
TEST_SUCCESS(status);
UText *usShallowClone = utext_clone(NULL, usDeepClone, FALSE, FALSE, &status);
TEST_SUCCESS(status);
utext_close(usShallowClone);
utext_close(usDeepClone);
utext_close(usText);
}

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2005-2012, International Business Machines Corporation and
* Copyright (c) 2005-2013, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/************************************************************************
@ -33,6 +33,7 @@ public:
void FreezeTest();
void Ticket5560();
void Ticket6847();
void Ticket10562();
private:
struct m { // Map between native indices & code points.