ICU-2729 Fix a memory leak

X-SVN-Rev: 12378
This commit is contained in:
George Rhoten 2003-06-09 16:35:12 +00:00
parent 75930a0808
commit c750a537a8
2 changed files with 161 additions and 159 deletions

View File

@ -459,30 +459,31 @@ void IntlCalendarTest::TestJapaneseFormat() {
void IntlCalendarTest::simpleTest(const Locale& loc, const UnicodeString& expect, UDate expectDate, UErrorCode& status)
{
UnicodeString tmp;
UDate d;
DateFormat *fmt0 = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull);
UnicodeString tmp;
UDate d;
DateFormat *fmt0 = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull);
logln("Try format/parse of " + (UnicodeString)loc.getName());
DateFormat *fmt2 = DateFormat::createDateInstance(DateFormat::kFull, loc);
if(fmt2) {
fmt2->format(expectDate, tmp);
logln(escape(tmp) + " ( in locale " + loc.getName() + ")");
if(tmp != expect) {
errln(UnicodeString("Failed to format " ) + loc.getName() + " expected " + escape(expect) + " got " + escape(tmp) );
logln("Try format/parse of " + (UnicodeString)loc.getName());
DateFormat *fmt2 = DateFormat::createDateInstance(DateFormat::kFull, loc);
if(fmt2) {
fmt2->format(expectDate, tmp);
logln(escape(tmp) + " ( in locale " + loc.getName() + ")");
if(tmp != expect) {
errln(UnicodeString("Failed to format " ) + loc.getName() + " expected " + escape(expect) + " got " + escape(tmp) );
}
d = fmt2->parse(expect,status);
CHECK(status, "Error occured parsing " + UnicodeString(loc.getName()));
if(d != expectDate) {
fmt2->format(d,tmp);
errln(UnicodeString("Failed to parse " ) + escape(expect) + ", " + loc.getName() + " expect " + (double)expectDate + " got " + (double)d + " " + escape(tmp));
logln( "wanted " + escape(fmt0->format(expectDate,tmp.remove())) + " but got " + escape(fmt0->format(d,tmp.remove())));
}
delete fmt2;
} else {
errln((UnicodeString)"Can't create " + loc.getName() + " date instance");
}
d = fmt2->parse(expect,status);
CHECK(status, "Error occured parsing " + UnicodeString(loc.getName()));
if(d != expectDate) {
fmt2->format(d,tmp);
errln(UnicodeString("Failed to parse " ) + escape(expect) + ", " + loc.getName() + " expect " + (double)expectDate + " got " + (double)d + " " + escape(tmp));
logln( "wanted " + escape(fmt0->format(expectDate,tmp.remove())) + " but got " + escape(fmt0->format(d,tmp.remove())));
}
delete fmt2;
} else {
errln((UnicodeString)"Can't create " + loc.getName() + " date instance");
}
delete fmt0;
}
#undef CHECK

View File

@ -20,147 +20,148 @@
void CollationServiceTest::TestRegister()
{
// register a singleton
const Locale& FR = Locale::getFrance();
const Locale& US = Locale::getUS();
const Locale US_FOO("en", "US", "FOO");
UErrorCode status = U_ZERO_ERROR;
Collator* frcol = Collator::createInstance(FR, status);
Collator* uscol = Collator::createInstance(US, status);
if(U_FAILURE(status)) {
errln("Failed to create collators with %s", u_errorName(status));
delete frcol;
delete uscol;
return;
}
{ // try override en_US collator
URegistryKey key = Collator::registerInstance(frcol, US, status);
Collator* ncol = Collator::createInstance(US_FOO, status);
if (*frcol != *ncol) {
errln("register of french collator for en_US failed on request for en_US_FOO");
}
// ensure original collator's params not touched
Locale loc = frcol->getLocale(ULOC_REQUESTED_LOCALE, status);
if (loc != FR) {
errln(UnicodeString("fr collator's requested locale changed to ") + loc.getName());
}
loc = frcol->getLocale(ULOC_VALID_LOCALE, status);
if (loc != FR) {
errln(UnicodeString("fr collator's valid locale changed to ") + loc.getName());
}
loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status);
if (loc != US_FOO) {
errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO but ") + loc.getName());
}
loc = ncol->getLocale(ULOC_VALID_LOCALE, status);
if (loc != US) {
errln(UnicodeString("valid locale for en_US_FOO is not en_US but ") + loc.getName());
}
loc = ncol->getLocale(ULOC_ACTUAL_LOCALE, status);
if (loc != US) {
errln(UnicodeString("actual locale for en_US_FOO is not en_US but ") + loc.getName());
}
delete ncol; ncol = NULL;
if (!Collator::unregister(key, status)) {
errln("failed to unregister french collator");
}
// !!! frcol pointer is now invalid !!!
ncol = Collator::createInstance(US, status);
if (*uscol != *ncol) {
errln("collator after unregister does not match original");
}
delete ncol; ncol = NULL;
}
// recreate frcol
frcol = Collator::createInstance(FR, status);
UCollator* frFR = ucol_open("fr_FR", &status);
{ // try create collator for new locale
Locale fu_FU_FOO("fu", "FU", "FOO");
Locale fu_FU("fu", "FU", "");
Collator* fucol = Collator::createInstance(fu_FU, status);
URegistryKey key = Collator::registerInstance(frcol, fu_FU, status);
Collator* ncol = Collator::createInstance(fu_FU_FOO, status);
if (*frcol != *ncol) {
errln("register of fr collator for fu_FU failed");
}
UnicodeString locName = fu_FU.getName();
StringEnumeration* localeEnum = Collator::getAvailableLocales();
UBool found = FALSE;
const UnicodeString* locStr;
for (locStr = localeEnum->snext(status);
!found && locStr != NULL;
locStr = localeEnum->snext(status)) {
//
if (locName == *locStr) {
found = TRUE;
}
}
delete localeEnum;
const Locale& FR = Locale::getFrance();
const Locale& US = Locale::getUS();
const Locale US_FOO("en", "US", "FOO");
if (!found) {
errln("new locale fu_FU not reported as supported locale");
UErrorCode status = U_ZERO_ERROR;
Collator* frcol = Collator::createInstance(FR, status);
Collator* uscol = Collator::createInstance(US, status);
if(U_FAILURE(status)) {
errln("Failed to create collators with %s", u_errorName(status));
delete frcol;
delete uscol;
return;
}
UnicodeString displayName;
Collator::getDisplayName(fu_FU, displayName);
if (displayName != "fu (FU)") {
errln(UnicodeString("found ") + displayName + " for fu_FU");
{ // try override en_US collator
URegistryKey key = Collator::registerInstance(frcol, US, status);
Collator* ncol = Collator::createInstance(US_FOO, status);
if (*frcol != *ncol) {
errln("register of french collator for en_US failed on request for en_US_FOO");
}
// ensure original collator's params not touched
Locale loc = frcol->getLocale(ULOC_REQUESTED_LOCALE, status);
if (loc != FR) {
errln(UnicodeString("fr collator's requested locale changed to ") + loc.getName());
}
loc = frcol->getLocale(ULOC_VALID_LOCALE, status);
if (loc != FR) {
errln(UnicodeString("fr collator's valid locale changed to ") + loc.getName());
}
loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status);
if (loc != US_FOO) {
errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO but ") + loc.getName());
}
loc = ncol->getLocale(ULOC_VALID_LOCALE, status);
if (loc != US) {
errln(UnicodeString("valid locale for en_US_FOO is not en_US but ") + loc.getName());
}
loc = ncol->getLocale(ULOC_ACTUAL_LOCALE, status);
if (loc != US) {
errln(UnicodeString("actual locale for en_US_FOO is not en_US but ") + loc.getName());
}
delete ncol; ncol = NULL;
if (!Collator::unregister(key, status)) {
errln("failed to unregister french collator");
}
// !!! frcol pointer is now invalid !!!
ncol = Collator::createInstance(US, status);
if (*uscol != *ncol) {
errln("collator after unregister does not match original");
}
delete ncol; ncol = NULL;
}
Collator::getDisplayName(fu_FU, fu_FU, displayName);
if (displayName != "fu (FU)") {
errln(UnicodeString("found ") + displayName + " for fu_FU");
// recreate frcol
frcol = Collator::createInstance(FR, status);
UCollator* frFR = ucol_open("fr_FR", &status);
{ // try create collator for new locale
Locale fu_FU_FOO("fu", "FU", "FOO");
Locale fu_FU("fu", "FU", "");
Collator* fucol = Collator::createInstance(fu_FU, status);
URegistryKey key = Collator::registerInstance(frcol, fu_FU, status);
Collator* ncol = Collator::createInstance(fu_FU_FOO, status);
if (*frcol != *ncol) {
errln("register of fr collator for fu_FU failed");
}
UnicodeString locName = fu_FU.getName();
StringEnumeration* localeEnum = Collator::getAvailableLocales();
UBool found = FALSE;
const UnicodeString* locStr;
for (locStr = localeEnum->snext(status);
!found && locStr != NULL;
locStr = localeEnum->snext(status)) {
//
if (locName == *locStr) {
found = TRUE;
}
}
delete localeEnum;
if (!found) {
errln("new locale fu_FU not reported as supported locale");
}
UnicodeString displayName;
Collator::getDisplayName(fu_FU, displayName);
if (displayName != "fu (FU)") {
errln(UnicodeString("found ") + displayName + " for fu_FU");
}
Collator::getDisplayName(fu_FU, fu_FU, displayName);
if (displayName != "fu (FU)") {
errln(UnicodeString("found ") + displayName + " for fu_FU");
}
// test ucol_open
UCollator* fufu = ucol_open("fu_FU_FOO", &status);
if (!fufu) {
errln("could not open fu_FU_FOO with ucol_open");
} else {
if (!ucol_equals(fufu, frFR)) {
errln("collator fufu != collator frFR");
}
}
if (!Collator::unregister(key, status)) {
errln("failed to unregister french collator");
}
// !!! note frcoll invalid again, but we're no longer using it
// other collators should still work ok
Locale nloc = ncol->getLocale(ULOC_VALID_LOCALE, status);
if (nloc != fu_FU) {
errln(UnicodeString("asked for nloc valid locale after close and got") + nloc.getName());
}
delete ncol; ncol = NULL;
if (fufu) {
const char* nlocstr = ucol_getLocale(fufu, 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) {
errln("collator after unregister does not match original fu_FU");
}
delete uscol; uscol = NULL;
delete ncol; ncol = NULL;
delete fucol; fucol = NULL;
}
// test ucol_open
UCollator* fufu = ucol_open("fu_FU_FOO", &status);
if (!fufu) {
errln("could not open fu_FU_FOO with ucol_open");
} else {
if (!ucol_equals(fufu, frFR)) {
errln("collator fufu != collator frFR");
}
}
if (!Collator::unregister(key, status)) {
errln("failed to unregister french collator");
}
// !!! note frcoll invalid again, but we're no longer using it
// other collators should still work ok
Locale nloc = ncol->getLocale(ULOC_VALID_LOCALE, status);
if (nloc != fu_FU) {
errln(UnicodeString("asked for nloc valid locale after close and got") + nloc.getName());
}
delete ncol; ncol = NULL;
if (fufu) {
const char* nlocstr = ucol_getLocale(fufu, 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);
}
ncol = Collator::createInstance(fu_FU, status);
if (*fucol != *ncol) {
errln("collator after unregister does not match original fu_FU");
}
delete uscol; uscol = NULL;
delete ncol; ncol = NULL;
delete fucol; fucol = NULL;
}
}
// ------------------