From 1cc5cd1a8b165b7c2d273bb12ba3536081129a20 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Wed, 16 Sep 2009 20:55:05 +0000 Subject: [PATCH] ICU-6991 Improve coverage for calendar, bms, and msgfmt. X-SVN-Rev: 26638 --- icu4c/source/i18n/calendar.cpp | 3 +- icu4c/source/i18n/gregocal.cpp | 4 +- icu4c/source/test/intltest/caltest.cpp | 17 +++++++ icu4c/source/test/intltest/ssearch.cpp | 15 ++++++ icu4c/source/test/intltest/tmsgfmt.cpp | 68 ++++++++++++++++++++++++++ icu4c/source/test/intltest/tmsgfmt.h | 5 +- 6 files changed, 108 insertions(+), 4 deletions(-) diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index e6138e8d775..cf8a0aa5101 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -947,7 +947,8 @@ Calendar::getAvailableLocales(int32_t& count) // ------------------------------------- -StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key, +StringEnumeration* U_EXPORT2 +Calendar::getKeywordValuesForLocale(const char* key, const Locale& locale, UBool commonlyUsed, UErrorCode& status) { // This is a wrapper over ucal_getKeywordValuesForLocale diff --git a/icu4c/source/i18n/gregocal.cpp b/icu4c/source/i18n/gregocal.cpp index 9c64950cb14..0791e6d2042 100644 --- a/icu4c/source/i18n/gregocal.cpp +++ b/icu4c/source/i18n/gregocal.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 1997-2008, International Business Machines Corporation and * +* Copyright (C) 1997-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* * @@ -150,7 +150,7 @@ static const UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILL // ------------------------------------- GregorianCalendar::GregorianCalendar(UErrorCode& status) -: Calendar(TimeZone::createDefault(), Locale::getDefault(), status), +: Calendar(status), fGregorianCutover(kPapalCutover), fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), fIsGregorian(TRUE), fInvertGregorian(FALSE) diff --git a/icu4c/source/test/intltest/caltest.cpp b/icu4c/source/test/intltest/caltest.cpp index 7769662ac41..028774284e6 100644 --- a/icu4c/source/test/intltest/caltest.cpp +++ b/icu4c/source/test/intltest/caltest.cpp @@ -475,6 +475,23 @@ CalendarTest::TestGenericAPI() if (gc2 != *gc || !(gc2 == *gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed"); delete gc; delete z; + + /* Code coverage for Calendar class. */ + cal = Calendar::createInstance(status); + if (cal != NULL && !U_FAILURE(status)) { + ((Calendar *)cal)->roll(UCAL_HOUR, 100, status); + ((Calendar *)cal)->clear(UCAL_HOUR); + URegistryKey key = cal->registerFactory(NULL, status); + cal->unregister(key, status); + } else { + errln("FAIL: Unable to create calendar instance."); + } + StringEnumeration *en = Calendar::getKeywordValuesForLocale(NULL, Locale::getDefault(),FALSE, status); + if (en == NULL || U_FAILURE(status)) { + errln("FAIL: getKeywordValuesForLocale for Calendar."); + } + delete en; + delete cal; } // ------------------------------------- diff --git a/icu4c/source/test/intltest/ssearch.cpp b/icu4c/source/test/intltest/ssearch.cpp index 168220d657b..9ffd9f7d9e6 100644 --- a/icu4c/source/test/intltest/ssearch.cpp +++ b/icu4c/source/test/intltest/ssearch.cpp @@ -1425,6 +1425,21 @@ void SSearchTest::bmsTest() } } + /* Add better coverage for bms code. */ + if(bms_empty(longPattern)) { + errln("FAIL: longgPattern is empty."); + } + + if (!bms_getData(longPattern)) { + errln("FAIL: bms_getData returned NULL."); + } + + if (!ucd_getCollator(data)) { + errln("FAIL: ucd_getCollator returned NULL."); + } + + ucd_freeCache(); + close_patterns: bms_close(shortPattern); bms_close(longPattern); diff --git a/icu4c/source/test/intltest/tmsgfmt.cpp b/icu4c/source/test/intltest/tmsgfmt.cpp index bbcb4cc51b2..320e0d9be70 100644 --- a/icu4c/source/test/intltest/tmsgfmt.cpp +++ b/icu4c/source/test/intltest/tmsgfmt.cpp @@ -53,6 +53,7 @@ TestMessageFormat::runIndexedTest(int32_t index, UBool exec, TESTCASE(19,TestTurkishCasing); TESTCASE(20,testAutoQuoteApostrophe); TESTCASE(21,testMsgFormatPlural); + TESTCASE(22,testCoverage); default: name = ""; break; } } @@ -1348,4 +1349,71 @@ void TestMessageFormat::testAutoQuoteApostrophe(void) { } } +void TestMessageFormat::testCoverage(void) { + UErrorCode status = U_ZERO_ERROR; + UnicodeString testformat("{argument, plural, one{C''est # fichier} other {Ce sont # fichiers}} dans la liste."); + MessageFormat *msgfmt = new MessageFormat(testformat, Locale("fr"), status); + if (msgfmt == NULL || U_FAILURE(status)) { + errln("FAIL: Unable to create MessageFormat."); + return; + } + if (!msgfmt->usesNamedArguments()) { + errln("FAIL: Unable to detect usage of named arguments."); + } + const double limit[] = {0.0, 1.0, 2.0}; + const UnicodeString formats[] = {"0.0<=Arg<1.0", + "1.0<=Arg<2.0", + "2.0<-Arg"}; + ChoiceFormat cf(limit, formats, 3); + + msgfmt->setFormat("set", cf, status); + + StringEnumeration *en = msgfmt->getFormatNames(status); + if (en == NULL || U_FAILURE(status)) { + errln("FAIL: Unable to get format names enumeration."); + } else { + int32_t count = 0; + en->reset(status); + count = en->count(status); + if (U_FAILURE(status)) { + errln("FAIL: Unable to get format name enumeration count."); + } else { + for (int32_t i = 0; i < count; i++) { + en->snext(status); + if (U_FAILURE(status)) { + errln("FAIL: Error enumerating through names."); + break; + } + } + } + } + + msgfmt->adoptFormat("adopt", &cf, status); + + delete en; + delete msgfmt; + + msgfmt = new MessageFormat("'", status); + if (msgfmt == NULL || U_FAILURE(status)) { + errln("FAIL: Unable to create MessageFormat."); + return; + } + if (msgfmt->usesNamedArguments()) { + errln("FAIL: Unable to detect usage of named arguments."); + } + + msgfmt->setFormat("formatName", cf, status); + if (!U_FAILURE(status)) { + errln("FAIL: Should fail to setFormat instead of passing."); + } + status = U_ZERO_ERROR; + en = msgfmt->getFormatNames(status); + if (!U_FAILURE(status)) { + errln("FAIL: Should fail to get format names enumeration instead of passing."); + } + + delete en; + delete msgfmt; +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/tmsgfmt.h b/icu4c/source/test/intltest/tmsgfmt.h index 65308060220..59c71a738a3 100644 --- a/icu4c/source/test/intltest/tmsgfmt.h +++ b/icu4c/source/test/intltest/tmsgfmt.h @@ -1,6 +1,6 @@ /*********************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2005, International Business Machines Corporation + * Copyright (c) 1997-2009, International Business Machines Corporation * and others. All Rights Reserved. ***********************************************************************/ @@ -92,6 +92,9 @@ public: void TestTurkishCasing(void); void testAutoQuoteApostrophe(void); + /* Provide better code coverage */ + void testCoverage(void); + private: };