]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dcfmapts.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/decimfmt.h"
14 #include "unicode/dcfmtsym.h"
15 #include "unicode/parseerr.h"
16 #include "unicode/currpinf.h"
18 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
19 // try to test the full functionality. It just calls each function in the class and
20 // verifies that it works on a basic level.
22 void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
24 if (exec
) logln((UnicodeString
)"TestSuite DecimalFormatAPI");
26 case 0: name
= "DecimalFormat API test";
28 logln((UnicodeString
)"DecimalFormat API test---"); logln((UnicodeString
)"");
29 UErrorCode status
= U_ZERO_ERROR
;
31 Locale::setDefault(Locale::getEnglish(), status
);
32 if(U_FAILURE(status
)) {
33 errln((UnicodeString
)"ERROR: Could not set default locale, test may not give correct results");
36 Locale::setDefault(saveLocale
, status
);
39 case 1: name
= "Rounding test";
41 logln((UnicodeString
)"DecimalFormat Rounding test---");
42 testRounding(/*par*/);
45 case 2: name
= "Test6354";
47 logln((UnicodeString
)"DecimalFormat Rounding Increment test---");
48 testRoundingInc(/*par*/);
51 case 3: name
= "TestCurrencyPluralInfo";
53 logln((UnicodeString
)"CurrencyPluralInfo API test---");
54 TestCurrencyPluralInfo();
57 case 4: name
= "TestScale";
59 logln((UnicodeString
)"Scale test---");
63 default: name
= ""; break;
68 * This test checks various generic API methods in DecimalFormat to achieve 100%
71 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
73 UErrorCode status
= U_ZERO_ERROR
;
75 // ======= Test constructors
77 logln((UnicodeString
)"Testing DecimalFormat constructors");
79 DecimalFormat
def(status
);
80 if(U_FAILURE(status
)) {
81 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
85 status
= U_ZERO_ERROR
;
86 const UnicodeString
pattern("#,##0.# FF");
87 DecimalFormat
pat(pattern
, status
);
88 if(U_FAILURE(status
)) {
89 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern)");
93 status
= U_ZERO_ERROR
;
94 DecimalFormatSymbols
*symbols
= new DecimalFormatSymbols(Locale::getFrench(), status
);
95 if(U_FAILURE(status
)) {
96 errln((UnicodeString
)"ERROR: Could not create DecimalFormatSymbols (French)");
100 status
= U_ZERO_ERROR
;
101 DecimalFormat
cust1(pattern
, symbols
, status
);
102 if(U_FAILURE(status
)) {
103 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
106 status
= U_ZERO_ERROR
;
107 DecimalFormat
cust2(pattern
, *symbols
, status
);
108 if(U_FAILURE(status
)) {
109 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols)");
112 DecimalFormat
copy(pat
);
114 // ======= Test clone(), assignment, and equality
116 logln((UnicodeString
)"Testing clone(), assignment and equality operators");
118 if( ! (copy
== pat
) || copy
!= pat
) {
119 errln((UnicodeString
)"ERROR: Copy constructor or == failed");
124 errln((UnicodeString
)"ERROR: Assignment (or !=) failed");
127 Format
*clone
= def
.clone();
128 if( ! (*clone
== def
) ) {
129 errln((UnicodeString
)"ERROR: Clone() failed");
133 // ======= Test various format() methods
135 logln((UnicodeString
)"Testing various format() methods");
137 double d
= -10456.0037;
138 int32_t l
= 100000000;
142 UnicodeString res1
, res2
, res3
, res4
;
143 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
145 res1
= def
.format(d
, res1
, pos1
);
146 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
148 res2
= pat
.format(l
, res2
, pos2
);
149 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
151 status
= U_ZERO_ERROR
;
152 res3
= cust1
.format(fD
, res3
, pos3
, status
);
153 if(U_FAILURE(status
)) {
154 errln((UnicodeString
)"ERROR: format(Formattable [double]) failed");
156 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res3
);
158 status
= U_ZERO_ERROR
;
159 res4
= cust2
.format(fL
, res4
, pos4
, status
);
160 if(U_FAILURE(status
)) {
161 errln((UnicodeString
)"ERROR: format(Formattable [long]) failed");
163 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res4
);
165 // ======= Test parse()
167 logln((UnicodeString
)"Testing parse()");
169 UnicodeString
text("-10,456.0037");
170 Formattable result1
, result2
;
171 ParsePosition
pos(0);
172 UnicodeString
patt("#,##0.#");
173 status
= U_ZERO_ERROR
;
174 pat
.applyPattern(patt
, status
);
175 if(U_FAILURE(status
)) {
176 errln((UnicodeString
)"ERROR: applyPattern() failed");
178 pat
.parse(text
, result1
, pos
);
179 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
180 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
182 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
184 status
= U_ZERO_ERROR
;
185 pat
.parse(text
, result2
, status
);
186 if(U_FAILURE(status
)) {
187 errln((UnicodeString
)"ERROR: parse() failed");
189 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
190 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
192 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
194 // ======= Test getters and setters
196 logln((UnicodeString
)"Testing getters and setters");
198 const DecimalFormatSymbols
*syms
= pat
.getDecimalFormatSymbols();
199 DecimalFormatSymbols
*newSyms
= new DecimalFormatSymbols(*syms
);
200 def
.setDecimalFormatSymbols(*newSyms
);
201 def
.adoptDecimalFormatSymbols(newSyms
); // don't use newSyms after this
202 if( *(pat
.getDecimalFormatSymbols()) != *(def
.getDecimalFormatSymbols())) {
203 errln((UnicodeString
)"ERROR: adopt or set DecimalFormatSymbols() failed");
206 UnicodeString posPrefix
;
207 pat
.setPositivePrefix("+");
208 posPrefix
= pat
.getPositivePrefix(posPrefix
);
209 logln((UnicodeString
)"Positive prefix (should be +): " + posPrefix
);
210 if(posPrefix
!= "+") {
211 errln((UnicodeString
)"ERROR: setPositivePrefix() failed");
214 UnicodeString negPrefix
;
215 pat
.setNegativePrefix("-");
216 negPrefix
= pat
.getNegativePrefix(negPrefix
);
217 logln((UnicodeString
)"Negative prefix (should be -): " + negPrefix
);
218 if(negPrefix
!= "-") {
219 errln((UnicodeString
)"ERROR: setNegativePrefix() failed");
222 UnicodeString posSuffix
;
223 pat
.setPositiveSuffix("_");
224 posSuffix
= pat
.getPositiveSuffix(posSuffix
);
225 logln((UnicodeString
)"Positive suffix (should be _): " + posSuffix
);
226 if(posSuffix
!= "_") {
227 errln((UnicodeString
)"ERROR: setPositiveSuffix() failed");
230 UnicodeString negSuffix
;
231 pat
.setNegativeSuffix("~");
232 negSuffix
= pat
.getNegativeSuffix(negSuffix
);
233 logln((UnicodeString
)"Negative suffix (should be ~): " + negSuffix
);
234 if(negSuffix
!= "~") {
235 errln((UnicodeString
)"ERROR: setNegativeSuffix() failed");
238 int32_t multiplier
= 0;
239 pat
.setMultiplier(8);
240 multiplier
= pat
.getMultiplier();
241 logln((UnicodeString
)"Multiplier (should be 8): " + multiplier
);
242 if(multiplier
!= 8) {
243 errln((UnicodeString
)"ERROR: setMultiplier() failed");
246 int32_t groupingSize
= 0;
247 pat
.setGroupingSize(2);
248 groupingSize
= pat
.getGroupingSize();
249 logln((UnicodeString
)"Grouping size (should be 2): " + (int32_t) groupingSize
);
250 if(groupingSize
!= 2) {
251 errln((UnicodeString
)"ERROR: setGroupingSize() failed");
254 pat
.setDecimalSeparatorAlwaysShown(TRUE
);
255 UBool tf
= pat
.isDecimalSeparatorAlwaysShown();
256 logln((UnicodeString
)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString
) (tf
? "TRUE" : "FALSE"));
258 errln((UnicodeString
)"ERROR: setDecimalSeparatorAlwaysShown() failed");
260 // Added by Ken Liu testing set/isExponentSignAlwaysShown
261 pat
.setExponentSignAlwaysShown(TRUE
);
262 UBool esas
= pat
.isExponentSignAlwaysShown();
263 logln((UnicodeString
)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString
) (esas
? "TRUE" : "FALSE"));
265 errln((UnicodeString
)"ERROR: ExponentSignAlwaysShown() failed");
268 // Added by Ken Liu testing set/isScientificNotation
269 pat
.setScientificNotation(TRUE
);
270 UBool sn
= pat
.isScientificNotation();
271 logln((UnicodeString
)"isScientificNotation (should be TRUE) is " + (UnicodeString
) (sn
? "TRUE" : "FALSE"));
273 errln((UnicodeString
)"ERROR: setScientificNotation() failed");
276 // Added by Ken Liu testing set/getMinimumExponentDigits
277 int8_t MinimumExponentDigits
= 0;
278 pat
.setMinimumExponentDigits(2);
279 MinimumExponentDigits
= pat
.getMinimumExponentDigits();
280 logln((UnicodeString
)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits
);
281 if(MinimumExponentDigits
!= 2) {
282 errln((UnicodeString
)"ERROR: setMinimumExponentDigits() failed");
285 // Added by Ken Liu testing set/getRoundingIncrement
286 double RoundingIncrement
= 0.0;
287 pat
.setRoundingIncrement(2.0);
288 RoundingIncrement
= pat
.getRoundingIncrement();
289 logln((UnicodeString
)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement
);
290 if(RoundingIncrement
!= 2.0) {
291 errln((UnicodeString
)"ERROR: setRoundingIncrement() failed");
293 //end of Ken's Adding
295 UnicodeString funkyPat
;
296 funkyPat
= pat
.toPattern(funkyPat
);
297 logln((UnicodeString
)"Pattern is " + funkyPat
);
299 UnicodeString locPat
;
300 locPat
= pat
.toLocalizedPattern(locPat
);
301 logln((UnicodeString
)"Localized pattern is " + locPat
);
303 // ======= Test applyPattern()
305 logln((UnicodeString
)"Testing applyPattern()");
307 UnicodeString
p1("#,##0.0#;(#,##0.0#)");
308 logln((UnicodeString
)"Applying pattern " + p1
);
309 status
= U_ZERO_ERROR
;
310 pat
.applyPattern(p1
, status
);
311 if(U_FAILURE(status
)) {
312 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
315 s2
= pat
.toPattern(s2
);
316 logln((UnicodeString
)"Extracted pattern is " + s2
);
318 errln((UnicodeString
)"ERROR: toPattern() result did not match pattern applied");
321 if(pat
.getSecondaryGroupingSize() != 0) {
322 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat
.getSecondaryGroupingSize());
325 if(pat
.getGroupingSize() != 3) {
326 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
329 UnicodeString
p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
330 logln((UnicodeString
)"Applying pattern " + p2
);
331 status
= U_ZERO_ERROR
;
332 pat
.applyLocalizedPattern(p2
, status
);
333 if(U_FAILURE(status
)) {
334 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
337 s3
= pat
.toLocalizedPattern(s3
);
338 logln((UnicodeString
)"Extracted pattern is " + s3
);
340 errln((UnicodeString
)"ERROR: toLocalizedPattern() result did not match pattern applied");
343 status
= U_ZERO_ERROR
;
345 pat
.applyLocalizedPattern(p2
, pe
, status
);
346 if(U_FAILURE(status
)) {
347 errln((UnicodeString
)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status
);
350 s4
= pat
.toLocalizedPattern(s3
);
351 logln((UnicodeString
)"Extracted pattern is " + s4
);
353 errln((UnicodeString
)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
356 if(pat
.getSecondaryGroupingSize() != 2) {
357 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat
.getSecondaryGroupingSize());
360 if(pat
.getGroupingSize() != 3) {
361 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
364 // ======= Test getStaticClassID()
366 logln((UnicodeString
)"Testing getStaticClassID()");
368 status
= U_ZERO_ERROR
;
369 NumberFormat
*test
= new DecimalFormat(status
);
370 if(U_FAILURE(status
)) {
371 errln((UnicodeString
)"ERROR: Couldn't create a DecimalFormat");
374 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
375 errln((UnicodeString
)"ERROR: getDynamicClassID() didn't return the expected value");
381 void IntlTestDecimalFormatAPI::TestCurrencyPluralInfo(){
382 UErrorCode status
= U_ZERO_ERROR
;
384 CurrencyPluralInfo
*cpi
= new CurrencyPluralInfo(status
);
385 if(U_FAILURE(status
)) {
386 errln((UnicodeString
)"ERROR: CurrencyPluralInfo(UErrorCode) could not be created");
389 CurrencyPluralInfo cpi1
= *cpi
;
391 if(cpi
->getDynamicClassID() != CurrencyPluralInfo::getStaticClassID()){
392 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::getDynamicClassID() didn't return the expected value");
395 cpi
->setCurrencyPluralPattern("","",status
);
396 if(U_FAILURE(status
)) {
397 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setCurrencyPluralPattern");
400 cpi
->setLocale(Locale::getCanada(), status
);
401 if(U_FAILURE(status
)) {
402 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setLocale");
405 cpi
->setPluralRules("",status
);
406 if(U_FAILURE(status
)) {
407 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setPluralRules");
410 DecimalFormat
*df
= new DecimalFormat(status
);
411 if(U_FAILURE(status
)) {
412 errcheckln(status
, "ERROR: Could not create DecimalFormat - %s", u_errorName(status
));
415 df
->adoptCurrencyPluralInfo(cpi
);
417 df
->getCurrencyPluralInfo();
419 df
->setCurrencyPluralInfo(cpi1
);
424 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
426 UErrorCode status
= U_ZERO_ERROR
;
427 double Roundingnumber
= 2.55;
428 double Roundingnumber1
= -2.55;
429 //+2.55 results -2.55 results
430 double result
[]={ 3.0, -2.0, // kRoundCeiling 0,
431 2.0, -3.0, // kRoundFloor 1,
432 2.0, -2.0, // kRoundDown 2,
433 3.0, -3.0, // kRoundUp 3,
434 3.0, -3.0, // kRoundHalfEven 4,
435 3.0, -3.0, // kRoundHalfDown 5,
436 3.0, -3.0 // kRoundHalfUp 6
438 DecimalFormat
pat(status
);
439 if(U_FAILURE(status
)) {
440 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
445 UnicodeString message
;
446 UnicodeString resultStr
;
447 for(mode
=0;mode
< 7;mode
++){
448 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)mode
);
449 if(pat
.getRoundingMode() != (DecimalFormat::ERoundingMode
)mode
){
450 errln((UnicodeString
)"SetRoundingMode or GetRoundingMode failed for mode=" + mode
);
454 //for +2.55 with RoundingIncrement=1.0
455 pat
.setRoundingIncrement(1.0);
456 pat
.format(Roundingnumber
, resultStr
);
457 message
= (UnicodeString
)"Round() failed: round(" + (double)Roundingnumber
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
458 verify(message
, resultStr
, result
[i
++]);
462 //for -2.55 with RoundingIncrement=1.0
463 pat
.format(Roundingnumber1
, resultStr
);
464 message
= (UnicodeString
)"Round() failed: round(" + (double)Roundingnumber1
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
465 verify(message
, resultStr
, result
[i
++]);
471 void IntlTestDecimalFormatAPI::verify(const UnicodeString
& message
, const UnicodeString
& got
, double expected
){
472 logln((UnicodeString
)message
+ got
+ (UnicodeString
)" Expected : " + expected
);
473 UnicodeString
expectedStr("");
474 expectedStr
=expectedStr
+ expected
;
475 if(got
!= expectedStr
) {
476 errln((UnicodeString
)"ERROR: " + message
+ got
+ (UnicodeString
)" Expected : " + expectedStr
);
480 void IntlTestDecimalFormatAPI::verifyString(const UnicodeString
& message
, const UnicodeString
& got
, UnicodeString
& expected
){
481 logln((UnicodeString
)message
+ got
+ (UnicodeString
)" Expected : " + expected
);
482 if(got
!= expected
) {
483 errln((UnicodeString
)"ERROR: " + message
+ got
+ (UnicodeString
)" Expected : " + expected
);
487 void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/)
489 UErrorCode status
= U_ZERO_ERROR
;
490 DecimalFormat
pat(UnicodeString("#,##0.00"),status
);
491 if(U_FAILURE(status
)) {
492 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
496 // get default rounding increment
497 double roundingInc
= pat
.getRoundingIncrement();
498 if (roundingInc
!= 0.0) {
499 errln((UnicodeString
)"ERROR: Rounding increment not zero");
503 // With rounding now being handled by decNumber, we no longer
504 // set a rounding increment to enable non-default mode rounding,
505 // checking of which was the original point of this test.
507 // set rounding mode with zero increment. Rounding
508 // increment should not be set by this operation
509 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)0);
510 roundingInc
= pat
.getRoundingIncrement();
511 if (roundingInc
!= 0.0) {
512 errln((UnicodeString
)"ERROR: Rounding increment not zero after setRoundingMode");
517 void IntlTestDecimalFormatAPI::TestScale()
519 typedef struct TestData
{
522 char *expectedOutput
;
525 static TestData testData
[] = {
526 { 100.0, 3, "100,000" },
527 { 10034.0, -2, "100.34" },
528 { 0.86, -3, "0.0009" },
529 { -0.000455, 1, "-0%" },
530 { -0.000555, 1, "-1%" },
531 { 0.000455, 1, "0%" },
532 { 0.000555, 1, "1%" },
535 UErrorCode status
= U_ZERO_ERROR
;
536 DecimalFormat
pat(status
);
537 if(U_FAILURE(status
)) {
538 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
542 UnicodeString message
;
543 UnicodeString resultStr
;
545 UnicodeString
percentPattern("#,##0%");
546 pat
.setMaximumFractionDigits(4);
548 for(int32_t i
=0;i
< sizeof(testData
)/sizeof(testData
[0]);i
++) {
550 pat
.applyPattern(percentPattern
,status
);
552 pat
.setAttribute(UNUM_SCALE
,testData
[i
].inputScale
,status
);
553 pat
.format(testData
[i
].inputValue
, resultStr
);
554 message
= UnicodeString("Unexpected output for ") + testData
[i
].inputValue
+ UnicodeString(" and scale ") + testData
[i
].inputScale
+ UnicodeString(". Got: ");
555 exp
= testData
[i
].expectedOutput
;
556 verifyString(message
, resultStr
, exp
);
562 #endif /* #if !UCONFIG_NO_FORMATTING */