From 98fd35991b019716331556c0baa60be9c7e2f5f7 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Fri, 20 Nov 2009 06:28:25 +0000 Subject: [PATCH] ICU-7247 use almost every one of the LocalXyzPointer classes at least once X-SVN-Rev: 26955 --- icu4c/source/common/cmemory.h | 153 +++++++++++++------ icu4c/source/common/ucnvsel.cpp | 15 +- icu4c/source/test/intltest/caltest.cpp | 15 +- icu4c/source/test/intltest/convtest.cpp | 69 ++++----- icu4c/source/test/intltest/csdetest.cpp | 20 +-- icu4c/source/test/intltest/itrbnf.cpp | 13 +- icu4c/source/test/intltest/itspoof.cpp | 28 ++-- icu4c/source/test/intltest/nmfmapts.cpp | 41 ++--- icu4c/source/test/intltest/rbbiapts.cpp | 53 +++---- icu4c/source/test/intltest/ssearch.cpp | 95 +++++------- icu4c/source/test/intltest/strcase.cpp | 63 ++++---- icu4c/source/test/intltest/svccoll.cpp | 14 +- icu4c/source/test/intltest/testidn.cpp | 4 +- icu4c/source/test/intltest/testidna.cpp | 7 +- icu4c/source/test/intltest/transrt.cpp | 16 +- icu4c/source/test/iotest/iotest.cpp | 28 ++-- icu4c/source/tools/ctestfw/tstdtmod.cpp | 14 +- icu4c/source/tools/ctestfw/unicode/testlog.h | 2 +- 18 files changed, 319 insertions(+), 331 deletions(-) diff --git a/icu4c/source/common/cmemory.h b/icu4c/source/common/cmemory.h index 9fca0f75ed..440afec7da 100644 --- a/icu4c/source/common/cmemory.h +++ b/icu4c/source/common/cmemory.h @@ -126,6 +126,27 @@ public: uprv_free(LocalPointerBase::ptr); LocalPointerBase::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 +inline T *LocalMemory::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::ptr); + LocalPointerBase::ptr=p; + } + return p; + } else { + return NULL; + } +} + + +template +inline T *LocalMemory::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::ptr, length*sizeof(T)); + } + uprv_free(LocalPointerBase::ptr); + LocalPointerBase::ptr=p; + } + return p; + } else { + return NULL; + } +} + +template +inline T *MaybeStackArray::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 +inline T *MaybeStackArray::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 */ diff --git a/icu4c/source/common/ucnvsel.cpp b/icu4c/source/common/ucnvsel.cpp index bdd4e9a8fe..cc626eecd2 100644 --- a/icu4c/source/common/ucnvsel.cpp +++ b/icu4c/source/common/ucnvsel.cpp @@ -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 */ diff --git a/icu4c/source/test/intltest/caltest.cpp b/icu4c/source/test/intltest/caltest.cpp index 44ff1af9da..5f002308fc 100644 --- a/icu4c/source/test/intltest/caltest.cpp +++ b/icu4c/source/test/intltest/caltest.cpp @@ -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; } diff --git a/icu4c/source/test/intltest/convtest.cpp b/icu4c/source/test/intltest/convtest.cpp index 3f2adf3501..89b7299476 100644 --- a/icu4c/source/test/intltest/convtest.cpp +++ b/icu4c/source/test/intltest/convtest.cpp @@ -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 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 diff --git a/icu4c/source/test/intltest/nmfmapts.cpp b/icu4c/source/test/intltest/nmfmapts.cpp index fe9c0ff120..71c2b6fb22 100644 --- a/icu4c/source/test/intltest/nmfmapts.cpp +++ b/icu4c/source/test/intltest/nmfmapts.cpp @@ -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 f0(NumberFormat::createInstance(SWAP_LOC, status)); + LocalPointer f1(NumberFormat::createInstance(SRC_LOC, status)); + LocalPointer 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 f3(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + LocalPointer f3a(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + LocalPointer 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 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 } diff --git a/icu4c/source/test/intltest/rbbiapts.cpp b/icu4c/source/test/intltest/rbbiapts.cpp index 786641c9f3..4e4e3f0e85 100644 --- a/icu4c/source/test/intltest/rbbiapts.cpp +++ b/icu4c/source/test/intltest/rbbiapts.cpp @@ -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 charIter1((RuleBasedBreakIterator*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status)); + LocalPointer 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 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); } } diff --git a/icu4c/source/test/intltest/ssearch.cpp b/icu4c/source/test/intltest/ssearch.cpp index 86c86fa0ec..1be7739117 100644 --- a/icu4c/source/test/intltest/ssearch.cpp +++ b/icu4c/source/test/intltest/ssearch.cpp @@ -155,9 +155,9 @@ void SSearchTest::searchTest() return; /* Couldn't get path: error message already output. */ } - UXMLParser *parser = UXMLParser::createParser(status); + LocalPointer parser(UXMLParser::createParser(status)); TEST_ASSERT_SUCCESS(status); - UXMLElement *root = parser->parseFile(testFilePath, status); + LocalPointer 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 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 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; } diff --git a/icu4c/source/test/intltest/strcase.cpp b/icu4c/source/test/intltest/strcase.cpp index 5cfc7366ea..cdf8b2baa9 100644 --- a/icu4c/source/test/intltest/strcase.cpp +++ b/icu4c/source/test/intltest/strcase.cpp @@ -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 driver(TestDataModule::getTestDataModule("casing", *this, status)); if(U_SUCCESS(status)) { for(whichCase=0; whichCasecreateTestData(dataNames[whichCase], status); + LocalPointer 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 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 diff --git a/icu4c/source/test/intltest/svccoll.cpp b/icu4c/source/test/intltest/svccoll.cpp index 904355a294..c47da25c5b 100644 --- a/icu4c/source/test/intltest/svccoll.cpp +++ b/icu4c/source/test/intltest/svccoll.cpp @@ -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) { diff --git a/icu4c/source/test/intltest/testidn.cpp b/icu4c/source/test/intltest/testidn.cpp index ea451701ac..919acde776 100644 --- a/icu4c/source/test/intltest/testidn.cpp +++ b/icu4c/source/test/intltest/testidn.cpp @@ -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; diff --git a/icu4c/source/test/intltest/testidna.cpp b/icu4c/source/test/intltest/testidna.cpp index 4d39689ed2..0afe4d0dc3 100644 --- a/icu4c/source/test/intltest/testidna.cpp +++ b/icu4c/source/test/intltest/testidna.cpp @@ -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, diff --git a/icu4c/source/test/intltest/transrt.cpp b/icu4c/source/test/intltest/transrt.cpp index 3a43c384d1..d4b85852ce 100644 --- a/icu4c/source/test/intltest/transrt.cpp +++ b/icu4c/source/test/intltest/transrt.cpp @@ -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); } diff --git a/icu4c/source/test/iotest/iotest.cpp b/icu4c/source/test/iotest/iotest.cpp index a78fb3ad43..09ffdf67c4 100644 --- a/icu4c/source/test/iotest/iotest.cpp +++ b/icu4c/source/test/iotest/iotest.cpp @@ -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; } diff --git a/icu4c/source/tools/ctestfw/tstdtmod.cpp b/icu4c/source/tools/ctestfw/tstdtmod.cpp index dc078826f1..8f1c4528c0 100644 --- a/icu4c/source/tools/ctestfw/tstdtmod.cpp +++ b/icu4c/source/tools/ctestfw/tstdtmod.cpp @@ -6,6 +6,8 @@ /* Created by weiv 05/09/2002 */ +#include + #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; diff --git a/icu4c/source/tools/ctestfw/unicode/testlog.h b/icu4c/source/tools/ctestfw/unicode/testlog.h index 5f01b021ae..f31377ede1 100644 --- a/icu4c/source/tools/ctestfw/unicode/testlog.h +++ b/icu4c/source/tools/ctestfw/unicode/testlog.h @@ -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: