]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dcfmapts.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2010, 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 default: name
= ""; break;
62 * This test checks various generic API methods in DecimalFormat to achieve 100%
65 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
67 UErrorCode status
= U_ZERO_ERROR
;
69 // ======= Test constructors
71 logln((UnicodeString
)"Testing DecimalFormat constructors");
73 DecimalFormat
def(status
);
74 if(U_FAILURE(status
)) {
75 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
79 status
= U_ZERO_ERROR
;
80 const UnicodeString
pattern("#,##0.# FF");
81 DecimalFormat
pat(pattern
, status
);
82 if(U_FAILURE(status
)) {
83 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern)");
87 status
= U_ZERO_ERROR
;
88 DecimalFormatSymbols
*symbols
= new DecimalFormatSymbols(Locale::getFrench(), status
);
89 if(U_FAILURE(status
)) {
90 errln((UnicodeString
)"ERROR: Could not create DecimalFormatSymbols (French)");
94 status
= U_ZERO_ERROR
;
95 DecimalFormat
cust1(pattern
, symbols
, status
);
96 if(U_FAILURE(status
)) {
97 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
100 status
= U_ZERO_ERROR
;
101 DecimalFormat
cust2(pattern
, *symbols
, status
);
102 if(U_FAILURE(status
)) {
103 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols)");
106 DecimalFormat
copy(pat
);
108 // ======= Test clone(), assignment, and equality
110 logln((UnicodeString
)"Testing clone(), assignment and equality operators");
112 if( ! (copy
== pat
) || copy
!= pat
) {
113 errln((UnicodeString
)"ERROR: Copy constructor or == failed");
118 errln((UnicodeString
)"ERROR: Assignment (or !=) failed");
121 Format
*clone
= def
.clone();
122 if( ! (*clone
== def
) ) {
123 errln((UnicodeString
)"ERROR: Clone() failed");
127 // ======= Test various format() methods
129 logln((UnicodeString
)"Testing various format() methods");
131 double d
= -10456.0037;
132 int32_t l
= 100000000;
136 UnicodeString res1
, res2
, res3
, res4
;
137 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
139 res1
= def
.format(d
, res1
, pos1
);
140 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
142 res2
= pat
.format(l
, res2
, pos2
);
143 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
145 status
= U_ZERO_ERROR
;
146 res3
= cust1
.format(fD
, res3
, pos3
, status
);
147 if(U_FAILURE(status
)) {
148 errln((UnicodeString
)"ERROR: format(Formattable [double]) failed");
150 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res3
);
152 status
= U_ZERO_ERROR
;
153 res4
= cust2
.format(fL
, res4
, pos4
, status
);
154 if(U_FAILURE(status
)) {
155 errln((UnicodeString
)"ERROR: format(Formattable [long]) failed");
157 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res4
);
159 // ======= Test parse()
161 logln((UnicodeString
)"Testing parse()");
163 UnicodeString
text("-10,456.0037");
164 Formattable result1
, result2
;
165 ParsePosition
pos(0);
166 UnicodeString
patt("#,##0.#");
167 status
= U_ZERO_ERROR
;
168 pat
.applyPattern(patt
, status
);
169 if(U_FAILURE(status
)) {
170 errln((UnicodeString
)"ERROR: applyPattern() failed");
172 pat
.parse(text
, result1
, pos
);
173 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
174 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
176 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
178 status
= U_ZERO_ERROR
;
179 pat
.parse(text
, result2
, status
);
180 if(U_FAILURE(status
)) {
181 errln((UnicodeString
)"ERROR: parse() failed");
183 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
184 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
186 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
188 // ======= Test getters and setters
190 logln((UnicodeString
)"Testing getters and setters");
192 const DecimalFormatSymbols
*syms
= pat
.getDecimalFormatSymbols();
193 DecimalFormatSymbols
*newSyms
= new DecimalFormatSymbols(*syms
);
194 def
.setDecimalFormatSymbols(*newSyms
);
195 def
.adoptDecimalFormatSymbols(newSyms
); // don't use newSyms after this
196 if( *(pat
.getDecimalFormatSymbols()) != *(def
.getDecimalFormatSymbols())) {
197 errln((UnicodeString
)"ERROR: adopt or set DecimalFormatSymbols() failed");
200 UnicodeString posPrefix
;
201 pat
.setPositivePrefix("+");
202 posPrefix
= pat
.getPositivePrefix(posPrefix
);
203 logln((UnicodeString
)"Positive prefix (should be +): " + posPrefix
);
204 if(posPrefix
!= "+") {
205 errln((UnicodeString
)"ERROR: setPositivePrefix() failed");
208 UnicodeString negPrefix
;
209 pat
.setNegativePrefix("-");
210 negPrefix
= pat
.getNegativePrefix(negPrefix
);
211 logln((UnicodeString
)"Negative prefix (should be -): " + negPrefix
);
212 if(negPrefix
!= "-") {
213 errln((UnicodeString
)"ERROR: setNegativePrefix() failed");
216 UnicodeString posSuffix
;
217 pat
.setPositiveSuffix("_");
218 posSuffix
= pat
.getPositiveSuffix(posSuffix
);
219 logln((UnicodeString
)"Positive suffix (should be _): " + posSuffix
);
220 if(posSuffix
!= "_") {
221 errln((UnicodeString
)"ERROR: setPositiveSuffix() failed");
224 UnicodeString negSuffix
;
225 pat
.setNegativeSuffix("~");
226 negSuffix
= pat
.getNegativeSuffix(negSuffix
);
227 logln((UnicodeString
)"Negative suffix (should be ~): " + negSuffix
);
228 if(negSuffix
!= "~") {
229 errln((UnicodeString
)"ERROR: setNegativeSuffix() failed");
232 int32_t multiplier
= 0;
233 pat
.setMultiplier(8);
234 multiplier
= pat
.getMultiplier();
235 logln((UnicodeString
)"Multiplier (should be 8): " + multiplier
);
236 if(multiplier
!= 8) {
237 errln((UnicodeString
)"ERROR: setMultiplier() failed");
240 int32_t groupingSize
= 0;
241 pat
.setGroupingSize(2);
242 groupingSize
= pat
.getGroupingSize();
243 logln((UnicodeString
)"Grouping size (should be 2): " + (int32_t) groupingSize
);
244 if(groupingSize
!= 2) {
245 errln((UnicodeString
)"ERROR: setGroupingSize() failed");
248 pat
.setDecimalSeparatorAlwaysShown(TRUE
);
249 UBool tf
= pat
.isDecimalSeparatorAlwaysShown();
250 logln((UnicodeString
)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString
) (tf
? "TRUE" : "FALSE"));
252 errln((UnicodeString
)"ERROR: setDecimalSeparatorAlwaysShown() failed");
254 // Added by Ken Liu testing set/isExponentSignAlwaysShown
255 pat
.setExponentSignAlwaysShown(TRUE
);
256 UBool esas
= pat
.isExponentSignAlwaysShown();
257 logln((UnicodeString
)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString
) (esas
? "TRUE" : "FALSE"));
259 errln((UnicodeString
)"ERROR: ExponentSignAlwaysShown() failed");
262 // Added by Ken Liu testing set/isScientificNotation
263 pat
.setScientificNotation(TRUE
);
264 UBool sn
= pat
.isScientificNotation();
265 logln((UnicodeString
)"isScientificNotation (should be TRUE) is " + (UnicodeString
) (sn
? "TRUE" : "FALSE"));
267 errln((UnicodeString
)"ERROR: setScientificNotation() failed");
270 // Added by Ken Liu testing set/getMinimumExponentDigits
271 int8_t MinimumExponentDigits
= 0;
272 pat
.setMinimumExponentDigits(2);
273 MinimumExponentDigits
= pat
.getMinimumExponentDigits();
274 logln((UnicodeString
)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits
);
275 if(MinimumExponentDigits
!= 2) {
276 errln((UnicodeString
)"ERROR: setMinimumExponentDigits() failed");
279 // Added by Ken Liu testing set/getRoundingIncrement
280 double RoundingIncrement
= 0.0;
281 pat
.setRoundingIncrement(2.0);
282 RoundingIncrement
= pat
.getRoundingIncrement();
283 logln((UnicodeString
)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement
);
284 if(RoundingIncrement
!= 2.0) {
285 errln((UnicodeString
)"ERROR: setRoundingIncrement() failed");
287 //end of Ken's Adding
289 UnicodeString funkyPat
;
290 funkyPat
= pat
.toPattern(funkyPat
);
291 logln((UnicodeString
)"Pattern is " + funkyPat
);
293 UnicodeString locPat
;
294 locPat
= pat
.toLocalizedPattern(locPat
);
295 logln((UnicodeString
)"Localized pattern is " + locPat
);
297 // ======= Test applyPattern()
299 logln((UnicodeString
)"Testing applyPattern()");
301 UnicodeString
p1("#,##0.0#;(#,##0.0#)");
302 logln((UnicodeString
)"Applying pattern " + p1
);
303 status
= U_ZERO_ERROR
;
304 pat
.applyPattern(p1
, status
);
305 if(U_FAILURE(status
)) {
306 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
309 s2
= pat
.toPattern(s2
);
310 logln((UnicodeString
)"Extracted pattern is " + s2
);
312 errln((UnicodeString
)"ERROR: toPattern() result did not match pattern applied");
315 if(pat
.getSecondaryGroupingSize() != 0) {
316 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat
.getSecondaryGroupingSize());
319 if(pat
.getGroupingSize() != 3) {
320 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
323 UnicodeString
p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
324 logln((UnicodeString
)"Applying pattern " + p2
);
325 status
= U_ZERO_ERROR
;
326 pat
.applyLocalizedPattern(p2
, status
);
327 if(U_FAILURE(status
)) {
328 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
331 s3
= pat
.toLocalizedPattern(s3
);
332 logln((UnicodeString
)"Extracted pattern is " + s3
);
334 errln((UnicodeString
)"ERROR: toLocalizedPattern() result did not match pattern applied");
337 status
= U_ZERO_ERROR
;
339 pat
.applyLocalizedPattern(p2
, pe
, status
);
340 if(U_FAILURE(status
)) {
341 errln((UnicodeString
)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status
);
344 s4
= pat
.toLocalizedPattern(s3
);
345 logln((UnicodeString
)"Extracted pattern is " + s4
);
347 errln((UnicodeString
)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
350 if(pat
.getSecondaryGroupingSize() != 2) {
351 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat
.getSecondaryGroupingSize());
354 if(pat
.getGroupingSize() != 3) {
355 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
358 // ======= Test getStaticClassID()
360 logln((UnicodeString
)"Testing getStaticClassID()");
362 status
= U_ZERO_ERROR
;
363 NumberFormat
*test
= new DecimalFormat(status
);
364 if(U_FAILURE(status
)) {
365 errln((UnicodeString
)"ERROR: Couldn't create a DecimalFormat");
368 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
369 errln((UnicodeString
)"ERROR: getDynamicClassID() didn't return the expected value");
375 void IntlTestDecimalFormatAPI::TestCurrencyPluralInfo(){
376 UErrorCode status
= U_ZERO_ERROR
;
378 CurrencyPluralInfo
*cpi
= new CurrencyPluralInfo(status
);
379 if(U_FAILURE(status
)) {
380 errln((UnicodeString
)"ERROR: CurrencyPluralInfo(UErrorCode) could not be created");
383 CurrencyPluralInfo cpi1
= *cpi
;
385 if(cpi
->getDynamicClassID() != CurrencyPluralInfo::getStaticClassID()){
386 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::getDynamicClassID() didn't return the expected value");
389 cpi
->setCurrencyPluralPattern("","",status
);
390 if(U_FAILURE(status
)) {
391 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setCurrencyPluralPattern");
394 cpi
->setLocale(Locale::getCanada(), status
);
395 if(U_FAILURE(status
)) {
396 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setLocale");
399 cpi
->setPluralRules("",status
);
400 if(U_FAILURE(status
)) {
401 errln((UnicodeString
)"ERROR: CurrencyPluralInfo::setPluralRules");
404 DecimalFormat
*df
= new DecimalFormat(status
);
405 if(U_FAILURE(status
)) {
406 errcheckln(status
, "ERROR: Could not create DecimalFormat - %s", u_errorName(status
));
409 df
->adoptCurrencyPluralInfo(cpi
);
411 df
->getCurrencyPluralInfo();
413 df
->setCurrencyPluralInfo(cpi1
);
418 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
420 UErrorCode status
= U_ZERO_ERROR
;
421 double Roundingnumber
= 2.55;
422 double Roundingnumber1
= -2.55;
423 //+2.55 results -2.55 results
424 double result
[]={ 3.0, -2.0, // kRoundCeiling 0,
425 2.0, -3.0, // kRoundFloor 1,
426 2.0, -2.0, // kRoundDown 2,
427 3.0, -3.0, // kRoundUp 3,
428 3.0, -3.0, // kRoundHalfEven 4,
429 3.0, -3.0, // kRoundHalfDown 5,
430 3.0, -3.0 // kRoundHalfUp 6
432 DecimalFormat
pat(status
);
433 if(U_FAILURE(status
)) {
434 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
439 UnicodeString message
;
440 UnicodeString resultStr
;
441 for(mode
=0;mode
< 7;mode
++){
442 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)mode
);
443 if(pat
.getRoundingMode() != (DecimalFormat::ERoundingMode
)mode
){
444 errln((UnicodeString
)"SetRoundingMode or GetRoundingMode failed for mode=" + mode
);
448 //for +2.55 with RoundingIncrement=1.0
449 pat
.setRoundingIncrement(1.0);
450 pat
.format(Roundingnumber
, resultStr
);
451 message
= (UnicodeString
)"round(" + (double)Roundingnumber
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
452 verify(message
, resultStr
, result
[i
++]);
456 //for -2.55 with RoundingIncrement=1.0
457 pat
.format(Roundingnumber1
, resultStr
);
458 message
= (UnicodeString
)"round(" + (double)Roundingnumber1
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
459 verify(message
, resultStr
, result
[i
++]);
465 void IntlTestDecimalFormatAPI::verify(const UnicodeString
& message
, const UnicodeString
& got
, double expected
){
466 logln((UnicodeString
)message
+ got
+ (UnicodeString
)" Expected : " + expected
);
467 UnicodeString
expectedStr("");
468 expectedStr
=expectedStr
+ expected
;
469 if(got
!= expectedStr
) {
470 errln((UnicodeString
)"ERROR: Round() failed: " + message
+ got
+ (UnicodeString
)" Expected : " + expectedStr
);
474 void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/)
476 UErrorCode status
= U_ZERO_ERROR
;
477 DecimalFormat
pat(UnicodeString("#,##0.00"),status
);
478 if(U_FAILURE(status
)) {
479 errcheckln(status
, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status
));
483 // get default rounding increment
484 double roundingInc
= pat
.getRoundingIncrement();
485 if (roundingInc
!= 0.0) {
486 errln((UnicodeString
)"ERROR: Rounding increment not zero");
490 // With rounding now being handled by decNumber, we no longer
491 // set a rounding increment to enable non-default mode rounding,
492 // checking of which was the original point of this test.
494 // set rounding mode with zero increment. Rounding
495 // increment should not be set by this operation
496 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)0);
497 roundingInc
= pat
.getRoundingIncrement();
498 if (roundingInc
!= 0.0) {
499 errln((UnicodeString
)"ERROR: Rounding increment not zero after setRoundingMode");
504 #endif /* #if !UCONFIG_NO_FORMATTING */