ICU-7247 use almost every one of the LocalXyzPointer classes at least once

X-SVN-Rev: 26955
This commit is contained in:
Markus Scherer 2009-11-20 06:28:25 +00:00
parent 16168ea78a
commit 98fd35991b
18 changed files with 319 additions and 331 deletions

View File

@ -126,6 +126,27 @@ public:
uprv_free(LocalPointerBase<T>::ptr);
LocalPointerBase<T>::ptr=p;
}
/**
* Deletes the array it owns, allocates a new one and reset its bytes to 0.
* Returns the new array pointer.
* If the allocation fails, then the current array is unchanged and
* this method returns NULL.
* @param newCapacity must be >0
* @return the allocated array pointer, or NULL if the allocation failed
*/
inline T *allocateInsteadAndReset(int32_t newCapacity=1);
/**
* Deletes the array it owns and allocates a new one, copying length T items.
* Returns the new array pointer.
* If the allocation fails, then the current array is unchanged and
* this method returns NULL.
* @param newCapacity must be >0
* @param length number of T items to be copied from the old array to the new one;
* must be no more than the capacity of the old array,
* which the caller must track because the LocalMemory does not track it
* @return the allocated array pointer, or NULL if the allocation failed
*/
inline T *allocateInsteadAndCopy(int32_t newCapacity=1, int32_t length=0);
/**
* Array item access (writable).
* No index bounds check.
@ -205,29 +226,7 @@ public:
* @param length number of T items to be copied from the old array to the new one
* @return the allocated array pointer, or NULL if the allocation failed
*/
T *resize(int32_t newCapacity, int32_t length=0) {
if(newCapacity>0) {
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
if(p!=NULL) {
if(length>0) {
if(length>capacity) {
length=capacity;
}
if(length>newCapacity) {
length=newCapacity;
}
uprv_memcpy(p, ptr, length*sizeof(T));
}
releaseArray();
ptr=p;
capacity=newCapacity;
needToRelease=TRUE;
}
return p;
} else {
return NULL;
}
}
inline T *resize(int32_t newCapacity, int32_t length=0);
/**
* Gives up ownership of the array if owned, or else clones it,
* copying length T items; resets itself to the internal stack array.
@ -239,28 +238,7 @@ public:
* caller becomes responsible for deleting the array
* @draft ICU 4.4
*/
T *orphanOrClone(int32_t length, int32_t &resultCapacity) {
T *p;
if(needToRelease) {
p=ptr;
} else if(length<=0) {
return NULL;
} else {
if(length>capacity) {
length=capacity;
}
p=(T *)uprv_malloc(length*sizeof(T));
if(p==NULL) {
return NULL;
}
uprv_memcpy(p, ptr, length*sizeof(T));
}
resultCapacity=length;
ptr=stackArray;
capacity=stackCapacity;
needToRelease=FALSE;
return p;
}
inline T *orphanOrClone(int32_t length, int32_t &resultCapacity);
private:
T *ptr;
int32_t capacity;
@ -285,6 +263,91 @@ private:
#endif
};
template<typename T>
inline T *LocalMemory<T>::allocateInsteadAndReset(int32_t newCapacity) {
if(newCapacity>0) {
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
if(p!=NULL) {
uprv_memset(p, 0, newCapacity*sizeof(T));
uprv_free(LocalPointerBase<T>::ptr);
LocalPointerBase<T>::ptr=p;
}
return p;
} else {
return NULL;
}
}
template<typename T>
inline T *LocalMemory<T>::allocateInsteadAndCopy(int32_t newCapacity, int32_t length) {
if(newCapacity>0) {
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
if(p!=NULL) {
if(length>0) {
if(length>newCapacity) {
length=newCapacity;
}
uprv_memcpy(p, LocalPointerBase<T>::ptr, length*sizeof(T));
}
uprv_free(LocalPointerBase<T>::ptr);
LocalPointerBase<T>::ptr=p;
}
return p;
} else {
return NULL;
}
}
template<typename T, int32_t stackCapacity>
inline T *MaybeStackArray<T, stackCapacity>::resize(int32_t newCapacity, int32_t length) {
if(newCapacity>0) {
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
if(p!=NULL) {
if(length>0) {
if(length>capacity) {
length=capacity;
}
if(length>newCapacity) {
length=newCapacity;
}
uprv_memcpy(p, ptr, length*sizeof(T));
}
releaseArray();
ptr=p;
capacity=newCapacity;
needToRelease=TRUE;
}
return p;
} else {
return NULL;
}
}
template<typename T, int32_t stackCapacity>
inline T *MaybeStackArray<T, stackCapacity>::orphanOrClone(int32_t length, int32_t &resultCapacity) {
T *p;
if(needToRelease) {
p=ptr;
} else if(length<=0) {
return NULL;
} else {
if(length>capacity) {
length=capacity;
}
p=(T *)uprv_malloc(length*sizeof(T));
if(p==NULL) {
return NULL;
}
uprv_memcpy(p, ptr, length*sizeof(T));
}
resultCapacity=length;
ptr=stackArray;
capacity=stackCapacity;
needToRelease=FALSE;
return p;
}
U_NAMESPACE_END
#endif /* XP_CPLUSPLUS */

View File

@ -157,13 +157,13 @@ ucnvsel_open(const char* const* converterList, int32_t converterListSize,
}
// allocate a new converter
UConverterSelector* newSelector =
(UConverterSelector*)uprv_malloc(sizeof(UConverterSelector));
if (!newSelector) {
LocalUConverterSelectorPointer newSelector(
(UConverterSelector*)uprv_malloc(sizeof(UConverterSelector)));
if (newSelector.isNull()) {
*status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
uprv_memset(newSelector, 0, sizeof(UConverterSelector));
uprv_memset(newSelector.getAlias(), 0, sizeof(UConverterSelector));
if (converterListSize == 0) {
converterList = NULL;
@ -173,7 +173,6 @@ ucnvsel_open(const char* const* converterList, int32_t converterListSize,
(char**)uprv_malloc(converterListSize * sizeof(char*));
if (!newSelector->encodings) {
*status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(newSelector);
return NULL;
}
newSelector->encodings[0] = NULL; // now we can call ucnvsel_close()
@ -194,7 +193,6 @@ ucnvsel_open(const char* const* converterList, int32_t converterListSize,
char* allStrings = (char*) uprv_malloc(totalSize);
if (!allStrings) {
*status = U_MEMORY_ALLOCATION_ERROR;
ucnvsel_close(newSelector);
return NULL;
}
@ -212,15 +210,14 @@ ucnvsel_open(const char* const* converterList, int32_t converterListSize,
newSelector->ownEncodingStrings = TRUE;
newSelector->encodingsCount = converterListSize;
UPropsVectors *upvec = upvec_open((converterListSize+31)/32, status);
generateSelectorData(newSelector, upvec, excludedCodePoints, whichSet, status);
generateSelectorData(newSelector.getAlias(), upvec, excludedCodePoints, whichSet, status);
upvec_close(upvec);
if (U_FAILURE(*status)) {
ucnvsel_close(newSelector);
return NULL;
}
return newSelector;
return newSelector.orphan();
}
/* close opened selector */

View File

@ -2077,31 +2077,30 @@ void CalendarTest::Test6703()
void CalendarTest::Test3785()
{
UErrorCode status = U_ZERO_ERROR;
UChar uzone[] = {'E', 'u', 'r', 'o', 'p', 'e', '/', 'P', 'a', 'r', 'i', 's', 0};
UnicodeString uzone = UNICODE_STRING_SIMPLE("Europe/Paris");
UDateFormat * df = udat_open(UDAT_NONE, UDAT_NONE, "en@calendar=islamic", uzone,
u_strlen(uzone), NULL, 0, &status);
if (NULL == df || U_FAILURE(status)) return;
LocalUDateFormatPointer df(udat_open(UDAT_NONE, UDAT_NONE, "en@calendar=islamic", uzone.getTerminatedBuffer(),
uzone.length(), NULL, 0, &status));
if (df.isNull() || U_FAILURE(status)) return;
UChar upattern[64];
u_uastrcpy(upattern, "EEE d MMMM y G, HH:mm:ss");
udat_applyPattern(df, FALSE, upattern, u_strlen(upattern));
udat_applyPattern(df.getAlias(), FALSE, upattern, u_strlen(upattern));
UChar ubuffer[1024];
UDate ud0 = 1337557623000.0;
status = U_ZERO_ERROR;
udat_format(df, ud0, ubuffer, 1024, NULL, &status);
udat_format(df.getAlias(), ud0, ubuffer, 1024, NULL, &status);
if (U_FAILURE(status)) return;
//printf("formatted: '%s'\n", mkcstr(ubuffer));
ud0 += 1000.0; // add one second
status = U_ZERO_ERROR;
udat_format(df, ud0, ubuffer, 1024, NULL, &status);
udat_format(df.getAlias(), ud0, ubuffer, 1024, NULL, &status);
if (U_FAILURE(status)) return;
//printf("formatted: '%s'\n", mkcstr(ubuffer));
udat_close(df);
return;
}

View File

@ -359,7 +359,7 @@ ConversionTest::TestGetUnicodeSet() {
ParsePosition pos;
UnicodeSet cnvSet, mapSet, mapnotSet, diffSet;
UnicodeSet *cnvSetPtr = &cnvSet;
UConverter *cnv;
LocalUConverterPointer cnv;
TestDataModule *dataModule;
TestData *testData;
@ -421,7 +421,7 @@ ConversionTest::TestGetUnicodeSet() {
logln("TestGetUnicodeSet[%d] %s", i, charset);
cnv=cnv_open(charset, errorCode);
cnv.adoptInstead(cnv_open(charset, errorCode));
if(U_FAILURE(errorCode)) {
errcheckln(errorCode, "error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
charset, i, u_errorName(errorCode));
@ -429,8 +429,7 @@ ConversionTest::TestGetUnicodeSet() {
continue;
}
ucnv_getUnicodeSet(cnv, (USet *)cnvSetPtr, (UConverterUnicodeSet)which, &errorCode);
ucnv_close(cnv);
ucnv_getUnicodeSet(cnv.getAlias(), cnvSetPtr->toUSet(), (UConverterUnicodeSet)which, &errorCode);
if(U_FAILURE(errorCode)) {
errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
@ -551,26 +550,26 @@ ConversionTest::TestGetUnicodeSet2() {
"ISO-2022-CN-EXT",
"LMBCS"
};
LocalUConverterPointer cnv;
char buffer[1024];
int32_t i;
for(i=0; i<LENGTHOF(cnvNames); ++i) {
UErrorCode errorCode=U_ZERO_ERROR;
UConverter *cnv=cnv_open(cnvNames[i], errorCode);
cnv.adoptInstead(cnv_open(cnvNames[i], errorCode));
if(U_FAILURE(errorCode)) {
errcheckln(errorCode, "failed to open converter %s - %s", cnvNames[i], u_errorName(errorCode));
continue;
}
UnicodeSet expected;
ucnv_setFromUCallBack(cnv, getUnicodeSetCallback, &expected, NULL, NULL, &errorCode);
ucnv_setFromUCallBack(cnv.getAlias(), getUnicodeSetCallback, &expected, NULL, NULL, &errorCode);
if(U_FAILURE(errorCode)) {
errln("failed to set the callback on converter %s - %s", cnvNames[i], u_errorName(errorCode));
ucnv_close(cnv);
continue;
}
UConverterUnicodeSet which;
for(which=UCNV_ROUNDTRIP_SET; which<UCNV_SET_COUNT; which=(UConverterUnicodeSet)((int)which+1)) {
if(which==UCNV_ROUNDTRIP_AND_FALLBACK_SET) {
ucnv_setFallback(cnv, TRUE);
ucnv_setFallback(cnv.getAlias(), TRUE);
}
expected.add(0, cpLimit-1);
s=s0;
@ -578,7 +577,7 @@ ConversionTest::TestGetUnicodeSet2() {
do {
char *t=buffer;
flush=(UBool)(s==s0+s0Length);
ucnv_fromUnicode(cnv, &t, buffer+sizeof(buffer), (const UChar **)&s, s0+s0Length, NULL, flush, &errorCode);
ucnv_fromUnicode(cnv.getAlias(), &t, buffer+sizeof(buffer), (const UChar **)&s, s0+s0Length, NULL, flush, &errorCode);
if(U_FAILURE(errorCode)) {
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
errorCode=U_ZERO_ERROR;
@ -589,7 +588,7 @@ ConversionTest::TestGetUnicodeSet2() {
}
} while(!flush);
UnicodeSet set;
ucnv_getUnicodeSet(cnv, (USet *)&set, which, &errorCode);
ucnv_getUnicodeSet(cnv.getAlias(), set.toUSet(), which, &errorCode);
if(cpLimit<0x110000) {
set.remove(cpLimit, 0x10ffff);
}
@ -644,7 +643,6 @@ ConversionTest::TestGetUnicodeSet2() {
}
}
}
ucnv_close(cnv);
}
delete [] s0;
@ -944,25 +942,22 @@ stepToUnicode(ConversionCase &cc, UConverter *cnv,
UBool
ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback, const char *option) {
UConverter *cnv;
UErrorCode errorCode;
// open the converter
errorCode=U_ZERO_ERROR;
cnv=cnv_open(cc.charset, errorCode);
if(U_FAILURE(errorCode)) {
IcuTestErrorCode errorCode(*this, "ToUnicodeCase");
LocalUConverterPointer cnv(cnv_open(cc.charset, errorCode));
if(errorCode.isFailure()) {
errcheckln(errorCode, "toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, errorCode.errorName());
errorCode.reset();
return FALSE;
}
// set the callback
if(callback!=NULL) {
ucnv_setToUCallBack(cnv, callback, option, NULL, NULL, &errorCode);
ucnv_setToUCallBack(cnv.getAlias(), callback, option, NULL, NULL, errorCode);
if(U_FAILURE(errorCode)) {
errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setToUCallBack() failed - %s",
cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
ucnv_close(cnv);
return FALSE;
}
}
@ -1006,20 +1001,20 @@ ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback
memset(resultOffsets, -1, LENGTHOF(resultOffsets));
}
memset(result, -1, LENGTHOF(result));
errorCode=U_ZERO_ERROR;
resultLength=stepToUnicode(cc, cnv,
errorCode.reset();
resultLength=stepToUnicode(cc, cnv.getAlias(),
result, LENGTHOF(result),
step==0 ? resultOffsets : NULL,
step, &errorCode);
step, errorCode);
ok=checkToUnicode(
cc, cnv, steps[i].name,
cc, cnv.getAlias(), steps[i].name,
result, resultLength,
cc.offsets!=NULL ? resultOffsets : NULL,
errorCode);
if(U_FAILURE(errorCode) || !cc.finalFlush) {
if(errorCode.isFailure() || !cc.finalFlush) {
// reset if an error occurred or we did not flush
// otherwise do nothing to make sure that flushing resets
ucnv_resetToUnicode(cnv);
ucnv_resetToUnicode(cnv.getAlias());
}
if (cc.offsets != NULL && resultOffsets[resultLength] != -1) {
errln("toUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
@ -1036,13 +1031,13 @@ ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback
// test ucnv_toUChars()
memset(result, 0, sizeof(result));
errorCode=U_ZERO_ERROR;
resultLength=ucnv_toUChars(cnv,
errorCode.reset();
resultLength=ucnv_toUChars(cnv.getAlias(),
result, LENGTHOF(result),
(const char *)cc.bytes, cc.bytesLength,
&errorCode);
errorCode);
ok=checkToUnicode(
cc, cnv, "toUChars",
cc, cnv.getAlias(), "toUChars",
result, resultLength,
NULL,
errorCode);
@ -1052,23 +1047,23 @@ ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback
// test preflighting
// keep the correct result for simple checking
errorCode=U_ZERO_ERROR;
resultLength=ucnv_toUChars(cnv,
errorCode.reset();
resultLength=ucnv_toUChars(cnv.getAlias(),
NULL, 0,
(const char *)cc.bytes, cc.bytesLength,
&errorCode);
if(errorCode==U_STRING_NOT_TERMINATED_WARNING || errorCode==U_BUFFER_OVERFLOW_ERROR) {
errorCode=U_ZERO_ERROR;
errorCode);
if(errorCode.get()==U_STRING_NOT_TERMINATED_WARNING || errorCode.get()==U_BUFFER_OVERFLOW_ERROR) {
errorCode.reset();
}
ok=checkToUnicode(
cc, cnv, "preflight toUChars",
cc, cnv.getAlias(), "preflight toUChars",
result, resultLength,
NULL,
errorCode);
break;
}
ucnv_close(cnv);
errorCode.reset(); // all errors have already been reported
return ok;
}

View File

@ -154,7 +154,7 @@ void CharsetDetectionTest::checkEncoding(const UnicodeString &testString, const
u_UCharsToChars(eSplit[0].getBuffer(), codepage, cpLength);
codepage[cpLength] = '\0';
UCharsetDetector *csd = ucsdet_open(&status);
LocalUCharsetDetectorPointer csd(ucsdet_open(&status));
int32_t byteLength = 0;
char *bytes = extractBytes(testString, codepage, byteLength);
@ -166,10 +166,10 @@ void CharsetDetectionTest::checkEncoding(const UnicodeString &testString, const
return;
}
ucsdet_setText(csd, bytes, byteLength, &status);
ucsdet_setText(csd.getAlias(), bytes, byteLength, &status);
int32_t matchCount = 0;
const UCharsetMatch **matches = ucsdet_detectAll(csd, &matchCount, &status);
const UCharsetMatch **matches = ucsdet_detectAll(csd.getAlias(), &matchCount, &status);
UnicodeString name(ucsdet_getName(matches[0], &status));
@ -223,7 +223,6 @@ void CharsetDetectionTest::checkEncoding(const UnicodeString &testString, const
bail:
freeBytes(bytes);
ucsdet_close(csd);
delete[] eSplit;
}
@ -243,10 +242,10 @@ const char *CharsetDetectionTest::getPath(char buffer[2048], const char *filenam
void CharsetDetectionTest::ConstructionTest()
{
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector *csd = ucsdet_open(&status);
UEnumeration *e = ucsdet_getAllDetectableCharsets(csd, &status);
int32_t count = uenum_count(e, &status);
IcuTestErrorCode status(*this, "ConstructionTest");
LocalUCharsetDetectorPointer csd(ucsdet_open(status));
LocalUEnumerationPointer e(ucsdet_getAllDetectableCharsets(csd.getAlias(), status));
int32_t count = uenum_count(e.getAlias(), status);
#ifdef DEBUG_DETECT
printf("There are %d recognizers.\n", count);
@ -254,7 +253,7 @@ void CharsetDetectionTest::ConstructionTest()
for(int32_t i = 0; i < count; i += 1) {
int32_t length;
const char *name = uenum_next(e, &length, &status);
const char *name = uenum_next(e.getAlias(), &length, status);
if(name == NULL || length <= 0) {
errln("ucsdet_getAllDetectableCharsets() returned a null or empty name!");
@ -264,9 +263,6 @@ void CharsetDetectionTest::ConstructionTest()
printf("%s\n", name);
#endif
}
uenum_close(e);
ucsdet_close(csd);
}
void CharsetDetectionTest::UTF8Test()

View File

@ -179,22 +179,20 @@ IntlTestRBNF::TestAPI() {
// test rule constructor
{
logln("Testing rule constructor");
UResourceBundle *en = ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf", "en", &status);
LocalUResourceBundlePointer en(ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf", "en", &status));
if(U_FAILURE(status)) {
errln("Unable to access resource bundle with data!");
} else {
int32_t ruleLen = 0;
int32_t len = 0;
UResourceBundle *rbnfRules = ures_getByKey(en, "RBNFRules", NULL, &status);
UResourceBundle *ruleSets = ures_getByKey(rbnfRules, "SpelloutRules", NULL, &status);
LocalUResourceBundlePointer rbnfRules(ures_getByKey(en.getAlias(), "RBNFRules", NULL, &status));
LocalUResourceBundlePointer ruleSets(ures_getByKey(rbnfRules.getAlias(), "SpelloutRules", NULL, &status));
UnicodeString desc;
while (ures_hasNext(ruleSets)) {
const UChar* currentString = ures_getNextString(ruleSets,&len,NULL,&status);
while (ures_hasNext(ruleSets.getAlias())) {
const UChar* currentString = ures_getNextString(ruleSets.getAlias(), &len, NULL, &status);
ruleLen += len;
desc.append(currentString);
}
ures_close(ruleSets);
ures_close(rbnfRules);
const UChar *spelloutRules = desc.getTerminatedBuffer();
@ -213,7 +211,6 @@ IntlTestRBNF::TestAPI() {
errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
}
}
ures_close(en);
}
}

View File

@ -307,6 +307,8 @@ static void appendHexUChar(UnicodeString &dest, UChar32 c) {
dest.append((UChar)0x20);
}
U_DEFINE_LOCAL_OPEN_POINTER(LocalStdioFilePointer, FILE, fclose);
// testConfData - Check each data item from the Unicode confusables.txt file,
// verify that it transforms correctly in a skeleton.
//
@ -319,27 +321,24 @@ void IntlTestSpoof::testConfData() {
uprv_strcpy(buffer, testDataDir);
uprv_strcat(buffer, "confusables.txt");
FILE *f = NULL;
f = fopen(buffer, "rb");
if (f == 0) {
LocalStdioFilePointer f(fopen(buffer, "rb"));
if (f.isNull()) {
errln("Skipping test spoof/testConfData. File confusables.txt not accessible.");
return;
}
fseek(f, 0, SEEK_END);
int32_t fileSize = ftell(f);
char *fileBuf = new char[fileSize];
fseek(f, 0, SEEK_SET);
int32_t amt_read = fread(fileBuf, 1, fileSize, f);
fseek(f.getAlias(), 0, SEEK_END);
int32_t fileSize = ftell(f.getAlias());
LocalArray<char> fileBuf(new char[fileSize]);
fseek(f.getAlias(), 0, SEEK_SET);
int32_t amt_read = fread(fileBuf.getAlias(), 1, fileSize, f.getAlias());
TEST_ASSERT_EQ(amt_read, fileSize);
TEST_ASSERT(fileSize>0);
if (amt_read != fileSize || fileSize <=0) {
delete [] fileBuf;
return;
}
fclose(f);
UnicodeString confusablesTxt = UnicodeString::fromUTF8(StringPiece(fileBuf, fileSize));
UnicodeString confusablesTxt = UnicodeString::fromUTF8(StringPiece(fileBuf.getAlias(), fileSize));
USpoofChecker *sc = uspoof_open(&status);
LocalUSpoofCheckerPointer sc(uspoof_open(&status));
TEST_ASSERT_SUCCESS(status);
// Parse lines from the confusables.txt file. Example Line:
@ -377,7 +376,7 @@ void IntlTestSpoof::testConfData() {
}
UnicodeString actual;
uspoof_getSkeletonUnicodeString(sc, skeletonType, from, actual, &status);
uspoof_getSkeletonUnicodeString(sc.getAlias(), skeletonType, from, actual, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT(actual == expected);
if (actual != expected) {
@ -394,9 +393,6 @@ void IntlTestSpoof::testConfData() {
break;
}
}
uspoof_close(sc);
delete [] fileBuf;
}
#endif // UCONFIG_NO_REGULAR_EXPRESSIONS

View File

@ -300,18 +300,18 @@ IntlTestNumberFormatAPI::testRegistration()
#if !UCONFIG_NO_SERVICE
UErrorCode status = U_ZERO_ERROR;
NumberFormat* f0 = NumberFormat::createInstance(SWAP_LOC, status);
NumberFormat* f1 = NumberFormat::createInstance(SRC_LOC, status);
NumberFormat* f2 = NumberFormat::createCurrencyInstance(SRC_LOC, status);
LocalPointer<NumberFormat> f0(NumberFormat::createInstance(SWAP_LOC, status));
LocalPointer<NumberFormat> f1(NumberFormat::createInstance(SRC_LOC, status));
LocalPointer<NumberFormat> f2(NumberFormat::createCurrencyInstance(SRC_LOC, status));
URegistryKey key = NumberFormat::registerFactory(new NFTestFactory(), status);
NumberFormat* f3 = NumberFormat::createCurrencyInstance(SRC_LOC, status);
NumberFormat* f3a = NumberFormat::createCurrencyInstance(SRC_LOC, status);
NumberFormat* f4 = NumberFormat::createInstance(SRC_LOC, status);
LocalPointer<NumberFormat> f3(NumberFormat::createCurrencyInstance(SRC_LOC, status));
LocalPointer<NumberFormat> f3a(NumberFormat::createCurrencyInstance(SRC_LOC, status));
LocalPointer<NumberFormat> f4(NumberFormat::createInstance(SRC_LOC, status));
StringEnumeration* locs = NumberFormat::getAvailableLocales();
UNumberFormat* uf3 = unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(),NULL, &status);
UNumberFormat* uf4 = unum_open(UNUM_DEFAULT, NULL, 0, SRC_LOC.getName(), NULL, &status);
LocalUNumberFormatPointer uf3(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status));
LocalUNumberFormatPointer uf4(unum_open(UNUM_DEFAULT, NULL, 0, SRC_LOC.getName(), NULL, &status));
const UnicodeString* res;
for (res = locs->snext(status); res; res = locs->snext(status)) {
@ -319,8 +319,8 @@ IntlTestNumberFormatAPI::testRegistration()
}
NumberFormat::unregister(key, status); // restore for other tests
NumberFormat* f5 = NumberFormat::createCurrencyInstance(SRC_LOC, status);
UNumberFormat* uf5 = unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(),NULL, &status);
LocalPointer<NumberFormat> f5(NumberFormat::createCurrencyInstance(SRC_LOC, status));
LocalUNumberFormatPointer uf5(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status));
if (U_FAILURE(status)) {
dataerrln("Error creating instnaces.");
@ -339,9 +339,9 @@ IntlTestNumberFormatAPI::testRegistration()
f4->format(n, res4);
f5->format(n, res5);
unum_formatDouble(uf3, n, ures3, 50, NULL, &status);
unum_formatDouble(uf4, n, ures4, 50, NULL, &status);
unum_formatDouble(uf5, n, ures5, 50, NULL, &status);
unum_formatDouble(uf3.getAlias(), n, ures3, 50, NULL, &status);
unum_formatDouble(uf4.getAlias(), n, ures4, 50, NULL, &status);
unum_formatDouble(uf5.getAlias(), n, ures5, 50, NULL, &status);
logln((UnicodeString)"f0 swap int: " + res0);
logln((UnicodeString)"f1 src int: " + res1);
@ -356,10 +356,9 @@ IntlTestNumberFormatAPI::testRegistration()
log("uf5 ureg cur: ");
logln(ures5);
if (f3 == f3a) {
if (f3.getAlias() == f3a.getAlias()) {
errln("did not get new instance from service");
} else {
delete f3a;
f3a.orphan();
}
if (res3 != res0) {
errln("registered service did not match");
@ -382,16 +381,6 @@ IntlTestNumberFormatAPI::testRegistration()
}
}
unum_close(uf5);
delete f5;
unum_close(uf4);
unum_close(uf3);
delete f4;
delete f3;
delete f2;
delete f1;
delete f0;
for (res = locs->snext(status); res; res = locs->snext(status)) {
errln(*res); // service should be out of synch
}

View File

@ -242,13 +242,13 @@ void RBBIAPITest::TestHashCode()
void RBBIAPITest::TestGetSetAdoptText()
{
logln((UnicodeString)"Testing getText setText ");
UErrorCode status=U_ZERO_ERROR;
IcuTestErrorCode status(*this, "TestGetSetAdoptText");
UnicodeString str1="first string.";
UnicodeString str2="Second string.";
RuleBasedBreakIterator* charIter1 = (RuleBasedBreakIterator*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status);
RuleBasedBreakIterator* wordIter1 = (RuleBasedBreakIterator*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status);
if(U_FAILURE(status)){
errcheckln(status, "Fail : in construction - %s", u_errorName(status));
LocalPointer<RuleBasedBreakIterator> charIter1((RuleBasedBreakIterator*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status));
LocalPointer<RuleBasedBreakIterator> wordIter1((RuleBasedBreakIterator*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status));
if(status.isFailure()){
errcheckln(status, "Fail : in construction - %s", status.errorName());
return;
}
@ -283,7 +283,7 @@ void RBBIAPITest::TestGetSetAdoptText()
TEST_ASSERT(tstr == str1);
RuleBasedBreakIterator* rb=(RuleBasedBreakIterator*)wordIter1->clone();
LocalPointer<RuleBasedBreakIterator> rb((RuleBasedBreakIterator*)wordIter1->clone());
rb->adoptText(text1);
if(rb->getText() != *text1)
errln((UnicodeString)"ERROR:1 error in adoptText ");
@ -312,9 +312,9 @@ void RBBIAPITest::TestGetSetAdoptText()
const char *s2 = "\x73\x65\x65\x20\x79\x61"; /* "see ya" in UTF-8 */
// 012345678901
status = U_ZERO_ERROR;
UText *ut = utext_openUTF8(NULL, s1, -1, &status);
wordIter1->setText(ut, status);
status.reset();
LocalUTextPointer ut(utext_openUTF8(NULL, s1, -1, status));
wordIter1->setText(ut.getAlias(), status);
TEST_ASSERT_SUCCESS(status);
int32_t pos;
@ -329,10 +329,10 @@ void RBBIAPITest::TestGetSetAdoptText()
pos = wordIter1->next();
TEST_ASSERT(pos==UBRK_DONE);
status = U_ZERO_ERROR;
UText *ut2 = utext_openUTF8(NULL, s2, -1, &status);
status.reset();
LocalUTextPointer ut2(utext_openUTF8(NULL, s2, -1, status));
TEST_ASSERT_SUCCESS(status);
wordIter1->setText(ut2, status);
wordIter1->setText(ut2.getAlias(), status);
TEST_ASSERT_SUCCESS(status);
pos = wordIter1->first();
@ -353,21 +353,13 @@ void RBBIAPITest::TestGetSetAdoptText()
pos = wordIter1->previous();
TEST_ASSERT(pos==UBRK_DONE);
status = U_ZERO_ERROR;
status.reset();
UnicodeString sEmpty;
UText *gut2 = utext_openUnicodeString(NULL, &sEmpty, &status);
wordIter1->getUText(gut2, status);
LocalUTextPointer gut2(utext_openUnicodeString(NULL, &sEmpty, status));
wordIter1->getUText(gut2.getAlias(), status);
TEST_ASSERT_SUCCESS(status);
utext_close(gut2);
utext_close(ut);
utext_close(ut2);
delete wordIter1;
delete charIter1;
delete rb;
}
status.reset();
}
void RBBIAPITest::TestIteration()
@ -1018,7 +1010,7 @@ void RBBIAPITest::RoundtripRule(const char *dataFile) {
UParseError parseError;
parseError.line = 0;
parseError.offset = 0;
UDataMemory *data = udata_open(U_ICUDATA_BRKITR, "brk", dataFile, &status);
LocalUDataMemoryPointer data(udata_open(U_ICUDATA_BRKITR, "brk", dataFile, &status));
uint32_t length;
const UChar *builtSource;
const uint8_t *rbbiRules;
@ -1029,7 +1021,7 @@ void RBBIAPITest::RoundtripRule(const char *dataFile) {
return;
}
builtRules = (const uint8_t *)udata_getMemory(data);
builtRules = (const uint8_t *)udata_getMemory(data.getAlias());
builtSource = (const UChar *)(builtRules + ((RBBIDataHeader*)builtRules)->fRuleSource);
RuleBasedBreakIterator *brkItr = new RuleBasedBreakIterator(builtSource, parseError, status);
if (U_FAILURE(status)) {
@ -1044,7 +1036,6 @@ void RBBIAPITest::RoundtripRule(const char *dataFile) {
return;
}
delete brkItr;
udata_close(data);
}
void RBBIAPITest::TestRoundtripRules() {
@ -1067,9 +1058,9 @@ void RBBIAPITest::TestCreateFromRBBIData() {
// Get some handy RBBIData
const char *brkName = "word"; // or "sent", "line", "char", etc.
UErrorCode status = U_ZERO_ERROR;
UDataMemory * data = udata_open(U_ICUDATA_BRKITR, "brk", brkName, &status);
LocalUDataMemoryPointer data(udata_open(U_ICUDATA_BRKITR, "brk", brkName, &status));
if ( U_SUCCESS(status) ) {
const RBBIDataHeader * builtRules = (const RBBIDataHeader *)udata_getMemory(data);
const RBBIDataHeader * builtRules = (const RBBIDataHeader *)udata_getMemory(data.getAlias());
uint32_t length = builtRules->fLength;
RBBIWithProtectedFunctions * brkItr;
@ -1098,8 +1089,6 @@ void RBBIAPITest::TestCreateFromRBBIData() {
} else {
errln("create RuleBasedBreakIterator from RBBIData (non-adopted): ICU Error \"%s\"\n", u_errorName(status) );
}
udata_close(data);
}
}

View File

@ -155,9 +155,9 @@ void SSearchTest::searchTest()
return; /* Couldn't get path: error message already output. */
}
UXMLParser *parser = UXMLParser::createParser(status);
LocalPointer<UXMLParser> parser(UXMLParser::createParser(status));
TEST_ASSERT_SUCCESS(status);
UXMLElement *root = parser->parseFile(testFilePath, status);
LocalPointer<UXMLElement> root(parser->parseFile(testFilePath, status));
TEST_ASSERT_SUCCESS(status);
if (U_FAILURE(status)) {
return;
@ -289,20 +289,18 @@ void SSearchTest::searchTest()
// obtained from the XML.
//
status = U_ZERO_ERROR;
UCollator *collator = ucol_open(clocale, &status);
ucol_setStrength(collator, collatorStrength);
ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, normalize, &status);
ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, alternateHandling, &status);
UStringSearch *uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
target.getBuffer(), target.length(),
collator,
NULL, // the break iterator
&status);
LocalUCollatorPointer collator(ucol_open(clocale, &status));
ucol_setStrength(collator.getAlias(), collatorStrength);
ucol_setAttribute(collator.getAlias(), UCOL_NORMALIZATION_MODE, normalize, &status);
ucol_setAttribute(collator.getAlias(), UCOL_ALTERNATE_HANDLING, alternateHandling, &status);
LocalUStringSearchPointer uss(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
target.getBuffer(), target.length(),
collator.getAlias(),
NULL, // the break iterator
&status));
TEST_ASSERT_SUCCESS(status);
if (U_FAILURE(status)) {
usearch_close(uss);
ucol_close(collator);
continue;
}
@ -313,7 +311,7 @@ void SSearchTest::searchTest()
//
// Do the search, check the match result against the expected results.
//
foundMatch= usearch_search(uss, 0, &foundStart, &foundLimit, &status);
foundMatch= usearch_search(uss.getAlias(), 0, &foundStart, &foundLimit, &status);
TEST_ASSERT_SUCCESS(status);
if ((foundMatch && expectedMatchStart<0) ||
(foundStart != expectedMatchStart) ||
@ -330,21 +328,19 @@ void SSearchTest::searchTest()
expectedMatchStart = foundStart;
expectedMatchLimit = foundLimit;
foundMatch = usearch_search(uss, foundLimit, &foundStart, &foundLimit, &status);
foundMatch = usearch_search(uss.getAlias(), foundLimit, &foundStart, &foundLimit, &status);
}
usearch_close(uss);
uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
uss.adoptInstead(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
target.getBuffer(), target.length(),
collator,
collator.getAlias(),
NULL,
&status);
&status));
//
// Do the backwards search, check the match result against the expected results.
//
foundMatch= usearch_searchBackwards(uss, target.length(), &foundStart, &foundLimit, &status);
foundMatch= usearch_searchBackwards(uss.getAlias(), target.length(), &foundStart, &foundLimit, &status);
TEST_ASSERT_SUCCESS(status);
if ((foundMatch && expectedMatchStart<0) ||
(foundStart != expectedMatchStart) ||
@ -354,13 +350,7 @@ void SSearchTest::searchTest()
"Found, expected backwards match limit = %d, %d",
foundStart, expectedMatchStart, foundLimit, expectedMatchLimit);
}
usearch_close(uss);
ucol_close(collator);
}
delete root;
delete parser;
#endif
}
@ -1603,21 +1593,21 @@ const char *cPattern = "maketh houndes ete hem";
UErrorCode status = U_ZERO_ERROR;
UCollator *collator = ucol_open("en", &status);
CollData *data = CollData::open(collator, status);
if (U_FAILURE(status) || collator == NULL || data == NULL) {
LocalUCollatorPointer collator(ucol_open("en", &status));
CollData *data = CollData::open(collator.getAlias(), status);
if (U_FAILURE(status) || collator.isNull() || data == NULL) {
errcheckln(status, "Unable to open UCollator or CollData. - %s", u_errorName(status));
return;
}
//ucol_setStrength(collator, collatorStrength);
//ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, normalize, &status);
//ucol_setStrength(collator.getAlias(), collatorStrength);
//ucol_setAttribute(collator.getAlias(), UCOL_NORMALIZATION_MODE, normalize, &status);
UnicodeString uPattern = cPattern;
#ifndef TEST_BOYER_MOORE
UStringSearch *uss = usearch_openFromCollator(uPattern.getBuffer(), uPattern.length(),
target.getBuffer(), target.length(),
collator,
NULL, // the break iterator
&status);
LocalUStringSearchPointer uss(usearch_openFromCollator(uPattern.getBuffer(), uPattern.length(),
target.getBuffer(), target.length(),
collator.getAlias(),
NULL, // the break iterator
&status));
TEST_ASSERT_SUCCESS(status);
#else
BoyerMooreSearch bms(data, uPattern, &target, status);
@ -1635,7 +1625,7 @@ const char *cPattern = "maketh houndes ete hem";
int32_t icuMatchPos;
int32_t icuMatchEnd;
#ifndef TEST_BOYER_MOORE
usearch_search(uss, 0, &icuMatchPos, &icuMatchEnd, &status);
usearch_search(uss.getAlias(), 0, &icuMatchPos, &icuMatchEnd, &status);
TEST_ASSERT_SUCCESS(status);
#else
found = bms.search(0, icuMatchPos, icuMatchEnd);
@ -1649,15 +1639,15 @@ const char *cPattern = "maketh houndes ete hem";
// to get runtimes of at least several seconds.
for (i=0; i<10000; i++) {
#ifndef TEST_BOYER_MOORE
found = usearch_search(uss, 0, &icuMatchPos, &icuMatchEnd, &status);
found = usearch_search(uss.getAlias(), 0, &icuMatchPos, &icuMatchEnd, &status);
#else
found = bms.search(0, icuMatchPos, icuMatchEnd);
#endif
//TEST_ASSERT_SUCCESS(status);
//TEST_ASSERT(found);
// usearch_setOffset(uss, 0, &status);
// icuMatchPos = usearch_next(uss, &status);
// usearch_setOffset(uss.getAlias(), 0, &status);
// icuMatchPos = usearch_next(uss.getAlias(), &status);
// The i+j stuff is to confuse the optimizer and get it to actually leave the
// call to strstr in place.
@ -1666,12 +1656,9 @@ const char *cPattern = "maketh houndes ete hem";
}
printf("%ld, %d\n", pm-longishText, j);
#ifndef TEST_BOYER_MOORE
usearch_close(uss);
#else
#ifdef TEST_BOYER_MOORE
CollData::close(data);
#endif
ucol_close(collator);
}
#endif
@ -2086,16 +2073,16 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
//int32_t expectedStart = prefix.length(), expectedEnd = prefix.length() + altPattern.length();
int32_t expectedStart = -1, expectedEnd = -1;
int32_t notFoundCount = 0;
UStringSearch *uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
testCase.getBuffer(), testCase.length(),
coll,
NULL, // the break iterator
&status);
LocalUStringSearchPointer uss(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
testCase.getBuffer(), testCase.length(),
coll,
NULL, // the break iterator
&status));
// **** TODO: find *all* matches, not just first one ****
simpleSearch(coll, testCase, 0, pattern, expectedStart, expectedEnd);
usearch_search(uss, 0, &actualStart, &actualEnd, &status);
usearch_search(uss.getAlias(), 0, &actualStart, &actualEnd, &status);
if (expectedStart >= 0 && (actualStart != expectedStart || actualEnd != expectedEnd)) {
errln("Search for <pattern> in <%s> failed: expected [%d, %d], got [%d, %d]\n"
@ -2110,9 +2097,9 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
// **** TODO: find *all* matches, not just first one ****
simpleSearch(coll, testCase, 0, altPattern, expectedStart, expectedEnd);
usearch_setPattern(uss, altPattern.getBuffer(), altPattern.length(), &status);
usearch_setPattern(uss.getAlias(), altPattern.getBuffer(), altPattern.length(), &status);
usearch_search(uss, 0, &actualStart, &actualEnd, &status);
usearch_search(uss.getAlias(), 0, &actualStart, &actualEnd, &status);
if (expectedStart >= 0 && (actualStart != expectedStart || actualEnd != expectedEnd)) {
errln("Search for <alt_pattern> in <%s> failed: expected [%d, %d], got [%d, %d]\n"
@ -2124,8 +2111,6 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
notFoundCount += 1;
}
usearch_close(uss);
return notFoundCount;
}

View File

@ -395,47 +395,44 @@ StringCaseTest::TestCasingImpl(const UnicodeString &input,
int32_t utf8InLength, utf8OutLength, resultLength;
UChar *buffer;
UCaseMap *csm;
UErrorCode errorCode;
errorCode=U_ZERO_ERROR;
csm=ucasemap_open(localeID, options, &errorCode);
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, clone, &errorCode);
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);
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,
utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
utf8Out, (int32_t)sizeof(utf8Out),
utf8In, utf8InLength, &errorCode);
utf8In, utf8InLength, errorCode);
break;
case TEST_UPPER:
name="ucasemap_utf8ToUpper";
utf8OutLength=ucasemap_utf8ToUpper(csm,
utf8OutLength=ucasemap_utf8ToUpper(csm.getAlias(),
utf8Out, (int32_t)sizeof(utf8Out),
utf8In, utf8InLength, &errorCode);
utf8In, utf8InLength, errorCode);
break;
#if !UCONFIG_NO_BREAK_ITERATION
case TEST_TITLE:
name="ucasemap_utf8ToTitle";
utf8OutLength=ucasemap_utf8ToTitle(csm,
utf8OutLength=ucasemap_utf8ToTitle(csm.getAlias(),
utf8Out, (int32_t)sizeof(utf8Out),
utf8In, utf8InLength, &errorCode);
utf8In, utf8InLength, errorCode);
break;
#endif
case TEST_FOLD:
name="ucasemap_utf8FoldCase";
utf8OutLength=ucasemap_utf8FoldCase(csm,
utf8OutLength=ucasemap_utf8FoldCase(csm.getAlias(),
utf8Out, (int32_t)sizeof(utf8Out),
utf8In, utf8InLength, &errorCode);
utf8In, utf8InLength, errorCode);
break;
default:
name="";
@ -443,31 +440,29 @@ StringCaseTest::TestCasingImpl(const UnicodeString &input,
break; // won't happen
}
buffer=result.getBuffer(utf8OutLength);
u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, &errorCode);
result.releaseBuffer(U_SUCCESS(errorCode) ? resultLength : 0);
u_strFromUTF8(buffer, result.getCapacity(), &resultLength, utf8Out, utf8OutLength, errorCode);
result.releaseBuffer(errorCode.isSuccess() ? resultLength : 0);
if(U_FAILURE(errorCode)) {
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 + "\"" );
}
ucasemap_close(csm);
}
void
StringCaseTest::TestCasing() {
UErrorCode status = U_ZERO_ERROR;
#if UCONFIG_NO_BREAK_ITERATION
void *iter;
#else
UBreakIterator *iter;
#if !UCONFIG_NO_BREAK_ITERATION
LocalUBreakIteratorPointer iter;
#endif
char cLocaleID[100];
UnicodeString locale, input, output, optionsString, result;
uint32_t options;
int32_t whichCase, type;
TestDataModule *driver = TestDataModule::getTestDataModule("casing", *this, status);
LocalPointer<TestDataModule> driver(TestDataModule::getTestDataModule("casing", *this, status));
if(U_SUCCESS(status)) {
for(whichCase=0; whichCase<TEST_COUNT; ++whichCase) {
#if UCONFIG_NO_BREAK_ITERATION
@ -475,7 +470,7 @@ StringCaseTest::TestCasing() {
continue;
}
#endif
TestData *casingTest = driver->createTestData(dataNames[whichCase], status);
LocalPointer<TestData> casingTest(driver->createTestData(dataNames[whichCase], status));
if(U_FAILURE(status)) {
errln("TestCasing failed to createTestData(%s) - %s", dataNames[whichCase], u_errorName(status));
break;
@ -490,18 +485,17 @@ StringCaseTest::TestCasing() {
}
locale.extract(0, 0x7fffffff, cLocaleID, sizeof(cLocaleID), "");
iter=NULL;
#if !UCONFIG_NO_BREAK_ITERATION
if(whichCase==TEST_TITLE) {
type = myCase->getInt("Type", status);
if(type>=0) {
iter=ubrk_open((UBreakIteratorType)type, cLocaleID, NULL, 0, &status);
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=ubrk_openRules(rules, LENGTHOF(rules), NULL, 0, &parseError, &status);
iter.adoptInstead(ubrk_openRules(rules, LENGTHOF(rules), NULL, 0, &parseError, &status));
}
}
#endif
@ -523,19 +517,18 @@ StringCaseTest::TestCasing() {
dataerrln("error: TestCasing() setup failed for %s test case from casing.res: %s", dataNames[whichCase], u_errorName(status));
status = U_ZERO_ERROR;
} else {
TestCasingImpl(input, output, whichCase, iter, cLocaleID, options);
#if UCONFIG_NO_BREAK_ITERATION
LocalPointer<UMemory> iter;
#endif
TestCasingImpl(input, output, whichCase, iter.getAlias(), cLocaleID, options);
}
#if !UCONFIG_NO_BREAK_ITERATION
if(iter!=NULL) {
ubrk_close(iter);
}
iter.adoptInstead(NULL);
#endif
}
delete casingTest;
}
}
delete driver;
#if !UCONFIG_NO_BREAK_ITERATION
// more tests for API coverage

View File

@ -94,7 +94,7 @@ void CollationServiceTest::TestRegister()
// recreate frcol
frcol = Collator::createInstance(FR, status);
UCollator* frFR = ucol_open("fr_FR", &status);
LocalUCollatorPointer frFR(ucol_open("fr_FR", &status));
{ // try create collator for new locale
Locale fu_FU_FOO("fu", "FU", "FOO");
@ -162,11 +162,11 @@ void CollationServiceTest::TestRegister()
}
// test ucol_open
UCollator* fufu = ucol_open("fu_FU_FOO", &status);
if (!fufu) {
LocalUCollatorPointer fufu(ucol_open("fu_FU_FOO", &status));
if (fufu.isNull()) {
errln("could not open fu_FU_FOO with ucol_open");
} else {
if (!ucol_equals(fufu, frFR)) {
if (!ucol_equals(fufu.getAlias(), frFR.getAlias())) {
errln("collator fufu != collator frFR");
}
}
@ -183,14 +183,12 @@ void CollationServiceTest::TestRegister()
}
delete ncol; ncol = NULL;
if (fufu) {
const char* nlocstr = ucol_getLocaleByType(fufu, ULOC_VALID_LOCALE, &status);
if (fufu.isValid()) {
const char* nlocstr = ucol_getLocaleByType(fufu.getAlias(), ULOC_VALID_LOCALE, &status);
if (uprv_strcmp(nlocstr, "fu_FU") != 0) {
errln(UnicodeString("asked for uloc valid locale after close and got ") + nlocstr);
}
ucol_close(fufu);
}
ucol_close(frFR);
ncol = Collator::createInstance(fu_FU, status);
if (*fucol != *ncol) {

View File

@ -63,7 +63,6 @@ static TestIDNA* pTestIDNA =NULL;
static const char* fileNames[] = {
"rfc3491.txt"
};
static UStringPrepProfile *profile = NULL;
static const UTrie *idnTrie = NULL;
static const int32_t *indexes = NULL;
static const uint16_t *mappingData = NULL;
@ -80,7 +79,7 @@ testData(TestIDNA& test) {
UErrorCode errorCode=U_ZERO_ERROR;
char *saveBasename =NULL;
profile = usprep_openByType(USPREP_RFC3491_NAMEPREP, &errorCode);
LocalUStringPrepProfilePointer profile(usprep_openByType(USPREP_RFC3491_NAMEPREP, &errorCode));
if(U_FAILURE(errorCode)){
test.errcheckln(errorCode, "Failed to load IDNA data file. " + UnicodeString(u_errorName(errorCode)));
return errorCode;
@ -132,7 +131,6 @@ testData(TestIDNA& test) {
testAllCodepoints(test);
usprep_close(profile);
pTestIDNA = NULL;
free(filename);
return errorCode;

View File

@ -417,7 +417,7 @@ void TestIDNA::debug(const UChar* src, int32_t srcLength, int32_t options){
UErrorCode prepStatus = U_ZERO_ERROR;
NamePrepTransform* trans = NamePrepTransform::createInstance(parseError,transStatus);
int32_t prepOptions = (((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0);
UStringPrepProfile* prep = usprep_openByType(USPREP_RFC3491_NAMEPREP,&prepStatus);
LocalUStringPrepProfilePointer prep(usprep_openByType(USPREP_RFC3491_NAMEPREP,&prepStatus));
UChar *transOut=NULL, *prepOut=NULL;
int32_t transOutLength=0, prepOutLength=0;
@ -429,12 +429,12 @@ void TestIDNA::debug(const UChar* src, int32_t srcLength, int32_t options){
transOutLength = trans->process(src,srcLength,transOut, transOutLength, prepOptions>0, &parseError, transStatus);
}
prepOutLength = usprep_prepare(prep, src, srcLength, prepOut, 0, prepOptions, &parseError, &prepStatus);
prepOutLength = usprep_prepare(prep.getAlias(), src, srcLength, prepOut, 0, prepOptions, &parseError, &prepStatus);
if( prepStatus == U_BUFFER_OVERFLOW_ERROR){
prepStatus = U_ZERO_ERROR;
prepOut = (UChar*) malloc(U_SIZEOF_UCHAR * prepOutLength);
prepOutLength = usprep_prepare(prep, src, srcLength, prepOut, prepOutLength, prepOptions, &parseError, &prepStatus);
prepOutLength = usprep_prepare(prep.getAlias(), src, srcLength, prepOut, prepOutLength, prepOptions, &parseError, &prepStatus);
}
if(UnicodeString(transOut,transOutLength)!= UnicodeString(prepOut, prepOutLength)){
@ -444,7 +444,6 @@ void TestIDNA::debug(const UChar* src, int32_t srcLength, int32_t options){
free(transOut);
free(prepOut);
delete trans;
}
void TestIDNA::testAPI(const UChar* src, const UChar* expected, const char* testName,

View File

@ -1052,25 +1052,16 @@ static void writeStringInU8(FILE *out, const UnicodeString &s) {
void TransliteratorRoundTripTest::TestHan() {
UErrorCode status = U_ZERO_ERROR;
// TODO: getExemplars() exists only as a C API, taking a USet.
// The set API we need to use exists only on UnicodeSet, not on USet.
// Do a hacky cast, knowing that a USet is really a UnicodeSet in
// the implementation. Once USet gets the missing API, switch back
// to using that.
USet *USetExemplars = NULL;
ULocaleData *uld = ulocdata_open("zh",&status);
USetExemplars = uset_open(0, 0);
USetExemplars = ulocdata_getExemplarSet(uld, USetExemplars, 0, ULOCDATA_ES_STANDARD, &status);
LocalULocaleDataPointer uld(ulocdata_open("zh",&status));
LocalUSetPointer USetExemplars(ulocdata_getExemplarSet(uld.getAlias(), uset_openEmpty(), 0, ULOCDATA_ES_STANDARD, &status));
ASSERT_SUCCESS(status);
ulocdata_close(uld);
UnicodeString source;
UChar32 c;
int i;
for (i=0; ;i++) {
// Add all of the Chinese exemplar chars to the string "source".
c = uset_charAt(USetExemplars, i);
c = uset_charAt(USetExemplars.getAlias(), i);
if (c == (UChar32)-1) {
break;
}
@ -1131,7 +1122,6 @@ void TransliteratorRoundTripTest::TestHan() {
delete pn;
delete nfd;
delete np;
uset_close(USetExemplars);
}

View File

@ -216,7 +216,7 @@ static void U_CALLCONV DataDrivenPrintf(void)
const char *fileLocale = "en_US_POSIX";
int32_t uFileBufferLenReturned;
UFILE *testFile;
LocalUFILEPointer testFile;
errorCode=U_ZERO_ERROR;
dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode);
@ -230,8 +230,8 @@ static void U_CALLCONV DataDrivenPrintf(void)
errorCode=U_ZERO_ERROR;
continue;
}
testFile = u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8");
if (!testFile) {
testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8"));
if (testFile.isNull()) {
log_err("Can't open test file - %s\n",
STANDARD_TEST_FILE);
continue;
@ -256,36 +256,36 @@ static void U_CALLCONV DataDrivenPrintf(void)
case 0x64: // 'd' double
dbl = atof(u_austrcpy(cBuffer, argument));
uBufferLenReturned = u_sprintf_u(uBuffer, format, dbl);
uFileBufferLenReturned = u_fprintf_u(testFile, format, dbl);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, dbl);
break;
case 0x31: // '1' int8_t
i8 = (int8_t)uto64(argument);
uBufferLenReturned = u_sprintf_u(uBuffer, format, i8);
uFileBufferLenReturned = u_fprintf_u(testFile, format, i8);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i8);
break;
case 0x32: // '2' int16_t
i16 = (int16_t)uto64(argument);
uBufferLenReturned = u_sprintf_u(uBuffer, format, i16);
uFileBufferLenReturned = u_fprintf_u(testFile, format, i16);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i16);
break;
case 0x34: // '4' int32_t
i32 = (int32_t)uto64(argument);
uBufferLenReturned = u_sprintf_u(uBuffer, format, i32);
uFileBufferLenReturned = u_fprintf_u(testFile, format, i32);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i32);
break;
case 0x38: // '8' int64_t
i64 = uto64(argument);
uBufferLenReturned = u_sprintf_u(uBuffer, format, i64);
uFileBufferLenReturned = u_fprintf_u(testFile, format, i64);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i64);
break;
case 0x73: // 's' char *
u_austrncpy(cBuffer, argument, sizeof(cBuffer));
uBufferLenReturned = u_sprintf_u(uBuffer, format, cBuffer);
uFileBufferLenReturned = u_fprintf_u(testFile, format, cBuffer);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, cBuffer);
break;
case 0x53: // 'S' UChar *
uBufferLenReturned = u_sprintf_u(uBuffer, format, argument);
uFileBufferLenReturned = u_fprintf_u(testFile, format, argument);
uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, argument);
break;
default:
uBufferLenReturned = 0;
@ -314,14 +314,13 @@ static void U_CALLCONV DataDrivenPrintf(void)
log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
i, cBuffer, uBufferLenReturned);
}
u_fclose(testFile);
testFile = u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8");
if (!testFile) {
testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8"));
if (testFile.isNull()) {
log_err("Can't open test file - %s\n",
STANDARD_TEST_FILE);
}
uBuffer[0]=0;
u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile);
u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile.getAlias());
if (u_strcmp(uBuffer, expectedResult) != 0) {
u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
u_austrncpy(cFormat, format, sizeof(cFormat));
@ -344,7 +343,6 @@ static void U_CALLCONV DataDrivenPrintf(void)
errorCode=U_ZERO_ERROR;
continue;
}
u_fclose(testFile);
}
delete testData;
}

View File

@ -6,6 +6,8 @@
/* Created by weiv 05/09/2002 */
#include <stdarg.h>
#include "unicode/tstdtmod.h"
#include "cmemory.h"
@ -16,12 +18,16 @@ IcuTestErrorCode::~IcuTestErrorCode() {
if(isFailure()) { handleFailure(); }
}
UBool IcuTestErrorCode::logIfFailureAndReset(const char *s) {
UBool IcuTestErrorCode::logIfFailureAndReset(const char *fmt, ...) {
if(isFailure()) {
// testClass.errln("%s %s failure - %s", testName, s, errorName());
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
UnicodeString msg(testName, -1, US_INV);
msg.append((UChar)0x20).append(UnicodeString(s, -1, US_INV));
msg.append(UNICODE_STRING_SIMPLE(" failure - ")).append(UnicodeString(errorName(), -1, US_INV));
msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV));
msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV));
testClass.errln(msg);
reset();
return TRUE;

View File

@ -34,7 +34,7 @@ public:
testClass(callingTestClass), testName(callingTestName) {}
virtual ~IcuTestErrorCode();
// Returns TRUE if isFailure().
UBool logIfFailureAndReset(const char *s);
UBool logIfFailureAndReset(const char *fmt, ...);
protected:
virtual void handleFailure() const;
private: