/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2003, International Business Machines Corporation and
+ * Copyright (c) 1997-2013, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/decimfmt.h"
#include "unicode/dcfmtsym.h"
#include "unicode/parseerr.h"
+#include "unicode/currpinf.h"
// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
// try to test the full functionality. It just calls each function in the class and
if (exec) {
logln((UnicodeString)"DecimalFormat API test---"); logln((UnicodeString)"");
UErrorCode status = U_ZERO_ERROR;
+ Locale saveLocale;
Locale::setDefault(Locale::getEnglish(), status);
if(U_FAILURE(status)) {
errln((UnicodeString)"ERROR: Could not set default locale, test may not give correct results");
}
testAPI(/*par*/);
+ Locale::setDefault(saveLocale, status);
}
break;
case 1: name = "Rounding test";
testRounding(/*par*/);
}
break;
-
+ case 2: name = "Test6354";
+ if(exec) {
+ logln((UnicodeString)"DecimalFormat Rounding Increment test---");
+ testRoundingInc(/*par*/);
+ }
+ break;
+ case 3: name = "TestCurrencyPluralInfo";
+ if(exec) {
+ logln((UnicodeString)"CurrencyPluralInfo API test---");
+ TestCurrencyPluralInfo();
+ }
+ break;
+ case 4: name = "TestScale";
+ if(exec) {
+ logln((UnicodeString)"Scale test---");
+ TestScale();
+ }
+ break;
default: name = ""; break;
}
}
DecimalFormat def(status);
if(U_FAILURE(status)) {
- errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
+ errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
delete test;
}
+void IntlTestDecimalFormatAPI::TestCurrencyPluralInfo(){
+ UErrorCode status = U_ZERO_ERROR;
+
+ CurrencyPluralInfo *cpi = new CurrencyPluralInfo(status);
+ if(U_FAILURE(status)) {
+ errln((UnicodeString)"ERROR: CurrencyPluralInfo(UErrorCode) could not be created");
+ }
+
+ CurrencyPluralInfo cpi1 = *cpi;
+
+ if(cpi->getDynamicClassID() != CurrencyPluralInfo::getStaticClassID()){
+ errln((UnicodeString)"ERROR: CurrencyPluralInfo::getDynamicClassID() didn't return the expected value");
+ }
+
+ cpi->setCurrencyPluralPattern("","",status);
+ if(U_FAILURE(status)) {
+ errln((UnicodeString)"ERROR: CurrencyPluralInfo::setCurrencyPluralPattern");
+ }
+
+ cpi->setLocale(Locale::getCanada(), status);
+ if(U_FAILURE(status)) {
+ errln((UnicodeString)"ERROR: CurrencyPluralInfo::setLocale");
+ }
+
+ cpi->setPluralRules("",status);
+ if(U_FAILURE(status)) {
+ errln((UnicodeString)"ERROR: CurrencyPluralInfo::setPluralRules");
+ }
+
+ DecimalFormat *df = new DecimalFormat(status);
+ if(U_FAILURE(status)) {
+ errcheckln(status, "ERROR: Could not create DecimalFormat - %s", u_errorName(status));
+ }
+
+ df->adoptCurrencyPluralInfo(cpi);
+
+ df->getCurrencyPluralInfo();
+
+ df->setCurrencyPluralInfo(cpi1);
+
+ delete df;
+}
+
void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
{
UErrorCode status = U_ZERO_ERROR;
};
DecimalFormat pat(status);
if(U_FAILURE(status)) {
- errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
+ errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
uint16_t mode;
//for +2.55 with RoundingIncrement=1.0
pat.setRoundingIncrement(1.0);
pat.format(Roundingnumber, resultStr);
- message= (UnicodeString)"round(" + (double)Roundingnumber + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
+ message= (UnicodeString)"Round() failed: round(" + (double)Roundingnumber + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
verify(message, resultStr, result[i++]);
message.remove();
resultStr.remove();
//for -2.55 with RoundingIncrement=1.0
pat.format(Roundingnumber1, resultStr);
- message= (UnicodeString)"round(" + (double)Roundingnumber1 + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
+ message= (UnicodeString)"Round() failed: round(" + (double)Roundingnumber1 + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
verify(message, resultStr, result[i++]);
message.remove();
resultStr.remove();
UnicodeString expectedStr("");
expectedStr=expectedStr + expected;
if(got != expectedStr ) {
- errln((UnicodeString)"ERROR: Round() failed: " + message + got + (UnicodeString)" Expected : " + expectedStr);
+ errln((UnicodeString)"ERROR: " + message + got + (UnicodeString)" Expected : " + expectedStr);
}
}
+void IntlTestDecimalFormatAPI::verifyString(const UnicodeString& message, const UnicodeString& got, UnicodeString& expected){
+ logln((UnicodeString)message + got + (UnicodeString)" Expected : " + expected);
+ if(got != expected ) {
+ errln((UnicodeString)"ERROR: " + message + got + (UnicodeString)" Expected : " + expected);
+ }
+}
+
+void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/)
+{
+ UErrorCode status = U_ZERO_ERROR;
+ DecimalFormat pat(UnicodeString("#,##0.00"),status);
+ if(U_FAILURE(status)) {
+ errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
+ return;
+ }
+
+ // get default rounding increment
+ double roundingInc = pat.getRoundingIncrement();
+ if (roundingInc != 0.0) {
+ errln((UnicodeString)"ERROR: Rounding increment not zero");
+ return;
+ }
+
+ // With rounding now being handled by decNumber, we no longer
+ // set a rounding increment to enable non-default mode rounding,
+ // checking of which was the original point of this test.
+
+ // set rounding mode with zero increment. Rounding
+ // increment should not be set by this operation
+ pat.setRoundingMode((DecimalFormat::ERoundingMode)0);
+ roundingInc = pat.getRoundingIncrement();
+ if (roundingInc != 0.0) {
+ errln((UnicodeString)"ERROR: Rounding increment not zero after setRoundingMode");
+ return;
+ }
+}
+
+void IntlTestDecimalFormatAPI::TestScale()
+{
+ typedef struct TestData {
+ double inputValue;
+ int inputScale;
+ char *expectedOutput;
+ } TestData;
+
+ static TestData testData[] = {
+ { 100.0, 3, "100,000" },
+ { 10034.0, -2, "100.34" },
+ { 0.86, -3, "0.0009" },
+ { -0.000455, 1, "-0%" },
+ { -0.000555, 1, "-1%" },
+ { 0.000455, 1, "0%" },
+ { 0.000555, 1, "1%" },
+ };
+
+ UErrorCode status = U_ZERO_ERROR;
+ DecimalFormat pat(status);
+ if(U_FAILURE(status)) {
+ errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
+ return;
+ }
+
+ UnicodeString message;
+ UnicodeString resultStr;
+ UnicodeString exp;
+ UnicodeString percentPattern("#,##0%");
+ pat.setMaximumFractionDigits(4);
+
+ for(int32_t i=0;i < sizeof(testData)/sizeof(testData[0]);i++) {
+ if ( i > 2 ) {
+ pat.applyPattern(percentPattern,status);
+ }
+ pat.setAttribute(UNUM_SCALE,testData[i].inputScale,status);
+ pat.format(testData[i].inputValue, resultStr);
+ message = UnicodeString("Unexpected output for ") + testData[i].inputValue + UnicodeString(" and scale ") + testData[i].inputScale + UnicodeString(". Got: ");
+ exp = testData[i].expectedOutput;
+ verifyString(message, resultStr, exp);
+ message.remove();
+ resultStr.remove();
+ exp.remove();
+ }
+}
#endif /* #if !UCONFIG_NO_FORMATTING */