-/********************************************************************
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
+/***********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2001, International Business Machines Corporation and
- * others. All Rights Reserved.
- ********************************************************************/
+ * Copyright (c) 1997-2011, International Business Machines Corporation
+ * and others. All Rights Reserved.
+ ***********************************************************************/
#include "unicode/utypes.h"
if (exec) {
logln("DateFormat API test---"); logln("");
UErrorCode status = U_ZERO_ERROR;
+ Locale saveLocale;
Locale::setDefault(Locale::getEnglish(), status);
if(U_FAILURE(status)) {
errln("ERROR: Could not set default locale, test may not give correct results");
}
testAPI(/*par*/);
+ Locale::setDefault(saveLocale, status);
}
break;
}
break;
+ case 3: name = "TestCoverage";
+ if (exec) {
+ logln("TestCoverage---"); logln("");
+ TestCoverage();
+ }
+ break;
+
default: name = ""; break;
}
}
+/**
+ * Add better code coverage.
+ */
+void IntlTestDateFormatAPI::TestCoverage(void)
+{
+ const char *LOCALES[] = {
+ "zh_CN@calendar=chinese",
+ "cop_EG@calendar=coptic",
+ "hi_IN@calendar=indian",
+ "am_ET@calendar=ethiopic"
+ };
+ int32_t numOfLocales = 4;
+
+ for (int32_t i = 0; i < numOfLocales; i++) {
+ DateFormat *df = DateFormat::createDateTimeInstance(DateFormat::kMedium, DateFormat::kMedium, Locale(LOCALES[i]));
+ if (df == NULL){
+ dataerrln("Error creating DateFormat instances.");
+ return;
+ }
+ delete df;
+ }
+}
/**
* Test that the equals method works correctly.
*/
while (Calendar::getNow() == start) ; // Wait for time to change
DateFormat *b = DateFormat::createInstance();
+ if (a == NULL || b == NULL){
+ dataerrln("Error calling DateFormat::createInstance()");
+ delete a;
+ delete b;
+ return;
+ }
+
if (!(*a == *b))
errln("FAIL: DateFormat objects created at different times are unequal.");
- if (b->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
+ SimpleDateFormat *sdtfmt = dynamic_cast<SimpleDateFormat *>(b);
+ if (sdtfmt != NULL)
{
double ONE_YEAR = 365*24*60*60*1000.0;
- ((SimpleDateFormat*)b)->set2DigitYearStart(start + 50*ONE_YEAR, status);
+ sdtfmt->set2DigitYearStart(start + 50*ONE_YEAR, status);
if (U_FAILURE(status))
errln("FAIL: setTwoDigitStartDate failed.");
else if (*a == *b)
DateFormat *it = DateFormat::createDateInstance(DateFormat::MEDIUM, Locale::getItalian());
DateFormat *de = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG, Locale::getGerman());
-// ======= Test equality
+ if (def == NULL || fr == NULL || it == NULL || de == NULL){
+ dataerrln("Error creating DateFormat instances.");
+ }
+// ======= Test equality
+if (fr != NULL && def != NULL)
+{
logln("Testing equality operator");
if( *fr == *it ) {
errln("ERROR: == failed");
}
+}
// ======= Test various format() methods
-
+if (fr != NULL && it != NULL && de != NULL)
+{
logln("Testing various format() methods");
UDate d = 837039928046.0;
Formattable fD(d, Formattable::kIsDate);
UnicodeString res1, res2, res3;
- FieldPosition pos1(0), pos2(0);
+ FieldPosition pos1(FieldPosition::DONT_CARE), pos2(FieldPosition::DONT_CARE);
status = U_ZERO_ERROR;
res1 = fr->format(d, res1, pos1, status);
res3 = de->format(d, res3);
logln( (UnicodeString) "" + d + " formatted to " + res3);
+}
// ======= Test parse()
-
+if (def != NULL)
+{
logln("Testing parse()");
UnicodeString text("02/03/76 2:50 AM, CST");
result3 = def->parse(text, pos01);
logln(text + " parsed into " + result3);
-
+}
// ======= Test getters and setters
-
+if (fr != NULL && it != NULL && de != NULL)
+{
logln("Testing getters and setters");
int32_t count = 0;
if( de->getTimeZone() != it->getTimeZone()) {
errln("ERROR: adopt or set TimeZone() failed");
}
-
+}
// ======= Test getStaticClassID()
logln("Testing getStaticClassID()");
status = U_ZERO_ERROR;
DateFormat *test = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
- errln("ERROR: Couldn't create a DateFormat");
+ dataerrln("ERROR: Couldn't create a DateFormat - %s", u_errorName(status));
}
if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
dateFmt->format(dateObj, str, fpos, status);
delete dateFmt;
} else {
- errln("FAIL: Can't create DateFormat");
+ dataerrln("FAIL: Can't create DateFormat");
}
}
logln("SimpleDateFormat");
status = U_ZERO_ERROR;
SimpleDateFormat sdf(status);
- // Format API
- sdf.format(dateObj, str, status);
- sdf.format(dateObj, str, fpos, status);
- // DateFormat API
- sdf.format((UDate)0, str, fpos);
- sdf.format((UDate)0, str);
- sdf.parse(str, status);
- sdf.parse(str, ppos);
+ if (U_SUCCESS(status)) {
+ // Format API
+ sdf.format(dateObj, str, status);
+ sdf.format(dateObj, str, fpos, status);
+ // DateFormat API
+ sdf.format((UDate)0, str, fpos);
+ sdf.format((UDate)0, str);
+ sdf.parse(str, status);
+ sdf.parse(str, ppos);
+ sdf.getNumberFormat();
+ } else {
+ dataerrln("FAIL: Can't create SimpleDateFormat() - %s", u_errorName(status));
+ }
}
// NumberFormat calling Format API
fmt->format(numObj, str, fpos, status);
delete fmt;
} else {
- errln("FAIL: Can't create NumberFormat");
+ dataerrln("FAIL: Can't create NumberFormat()");
}
}
fmt.parse(str, obj, ppos);
fmt.parse(str, obj, status);
} else {
- errln("FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status));
+ errcheckln(status, "FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status));
}
}