]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dcfmapts.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2003, 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"
17 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
18 // try to test the full functionality. It just calls each function in the class and
19 // verifies that it works on a basic level.
21 void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
23 if (exec
) logln((UnicodeString
)"TestSuite DecimalFormatAPI");
25 case 0: name
= "DecimalFormat API test";
27 logln((UnicodeString
)"DecimalFormat API test---"); logln((UnicodeString
)"");
28 UErrorCode status
= U_ZERO_ERROR
;
30 Locale::setDefault(Locale::getEnglish(), status
);
31 if(U_FAILURE(status
)) {
32 errln((UnicodeString
)"ERROR: Could not set default locale, test may not give correct results");
35 Locale::setDefault(saveLocale
, status
);
38 case 1: name
= "Rounding test";
40 logln((UnicodeString
)"DecimalFormat Rounding test---");
41 testRounding(/*par*/);
45 default: name
= ""; break;
50 * This test checks various generic API methods in DecimalFormat to achieve 100%
53 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
55 UErrorCode status
= U_ZERO_ERROR
;
57 // ======= Test constructors
59 logln((UnicodeString
)"Testing DecimalFormat constructors");
61 DecimalFormat
def(status
);
62 if(U_FAILURE(status
)) {
63 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (default)");
67 status
= U_ZERO_ERROR
;
68 const UnicodeString
pattern("#,##0.# FF");
69 DecimalFormat
pat(pattern
, status
);
70 if(U_FAILURE(status
)) {
71 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern)");
75 status
= U_ZERO_ERROR
;
76 DecimalFormatSymbols
*symbols
= new DecimalFormatSymbols(Locale::getFrench(), status
);
77 if(U_FAILURE(status
)) {
78 errln((UnicodeString
)"ERROR: Could not create DecimalFormatSymbols (French)");
82 status
= U_ZERO_ERROR
;
83 DecimalFormat
cust1(pattern
, symbols
, status
);
84 if(U_FAILURE(status
)) {
85 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
88 status
= U_ZERO_ERROR
;
89 DecimalFormat
cust2(pattern
, *symbols
, status
);
90 if(U_FAILURE(status
)) {
91 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols)");
94 DecimalFormat
copy(pat
);
96 // ======= Test clone(), assignment, and equality
98 logln((UnicodeString
)"Testing clone(), assignment and equality operators");
100 if( ! (copy
== pat
) || copy
!= pat
) {
101 errln((UnicodeString
)"ERROR: Copy constructor or == failed");
106 errln((UnicodeString
)"ERROR: Assignment (or !=) failed");
109 Format
*clone
= def
.clone();
110 if( ! (*clone
== def
) ) {
111 errln((UnicodeString
)"ERROR: Clone() failed");
115 // ======= Test various format() methods
117 logln((UnicodeString
)"Testing various format() methods");
119 double d
= -10456.0037;
120 int32_t l
= 100000000;
124 UnicodeString res1
, res2
, res3
, res4
;
125 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
127 res1
= def
.format(d
, res1
, pos1
);
128 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
130 res2
= pat
.format(l
, res2
, pos2
);
131 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
133 status
= U_ZERO_ERROR
;
134 res3
= cust1
.format(fD
, res3
, pos3
, status
);
135 if(U_FAILURE(status
)) {
136 errln((UnicodeString
)"ERROR: format(Formattable [double]) failed");
138 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res3
);
140 status
= U_ZERO_ERROR
;
141 res4
= cust2
.format(fL
, res4
, pos4
, status
);
142 if(U_FAILURE(status
)) {
143 errln((UnicodeString
)"ERROR: format(Formattable [long]) failed");
145 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res4
);
147 // ======= Test parse()
149 logln((UnicodeString
)"Testing parse()");
151 UnicodeString
text("-10,456.0037");
152 Formattable result1
, result2
;
153 ParsePosition
pos(0);
154 UnicodeString
patt("#,##0.#");
155 status
= U_ZERO_ERROR
;
156 pat
.applyPattern(patt
, status
);
157 if(U_FAILURE(status
)) {
158 errln((UnicodeString
)"ERROR: applyPattern() failed");
160 pat
.parse(text
, result1
, pos
);
161 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
162 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
164 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
166 status
= U_ZERO_ERROR
;
167 pat
.parse(text
, result2
, status
);
168 if(U_FAILURE(status
)) {
169 errln((UnicodeString
)"ERROR: parse() failed");
171 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
172 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
174 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
176 // ======= Test getters and setters
178 logln((UnicodeString
)"Testing getters and setters");
180 const DecimalFormatSymbols
*syms
= pat
.getDecimalFormatSymbols();
181 DecimalFormatSymbols
*newSyms
= new DecimalFormatSymbols(*syms
);
182 def
.setDecimalFormatSymbols(*newSyms
);
183 def
.adoptDecimalFormatSymbols(newSyms
); // don't use newSyms after this
184 if( *(pat
.getDecimalFormatSymbols()) != *(def
.getDecimalFormatSymbols())) {
185 errln((UnicodeString
)"ERROR: adopt or set DecimalFormatSymbols() failed");
188 UnicodeString posPrefix
;
189 pat
.setPositivePrefix("+");
190 posPrefix
= pat
.getPositivePrefix(posPrefix
);
191 logln((UnicodeString
)"Positive prefix (should be +): " + posPrefix
);
192 if(posPrefix
!= "+") {
193 errln((UnicodeString
)"ERROR: setPositivePrefix() failed");
196 UnicodeString negPrefix
;
197 pat
.setNegativePrefix("-");
198 negPrefix
= pat
.getNegativePrefix(negPrefix
);
199 logln((UnicodeString
)"Negative prefix (should be -): " + negPrefix
);
200 if(negPrefix
!= "-") {
201 errln((UnicodeString
)"ERROR: setNegativePrefix() failed");
204 UnicodeString posSuffix
;
205 pat
.setPositiveSuffix("_");
206 posSuffix
= pat
.getPositiveSuffix(posSuffix
);
207 logln((UnicodeString
)"Positive suffix (should be _): " + posSuffix
);
208 if(posSuffix
!= "_") {
209 errln((UnicodeString
)"ERROR: setPositiveSuffix() failed");
212 UnicodeString negSuffix
;
213 pat
.setNegativeSuffix("~");
214 negSuffix
= pat
.getNegativeSuffix(negSuffix
);
215 logln((UnicodeString
)"Negative suffix (should be ~): " + negSuffix
);
216 if(negSuffix
!= "~") {
217 errln((UnicodeString
)"ERROR: setNegativeSuffix() failed");
220 int32_t multiplier
= 0;
221 pat
.setMultiplier(8);
222 multiplier
= pat
.getMultiplier();
223 logln((UnicodeString
)"Multiplier (should be 8): " + multiplier
);
224 if(multiplier
!= 8) {
225 errln((UnicodeString
)"ERROR: setMultiplier() failed");
228 int32_t groupingSize
= 0;
229 pat
.setGroupingSize(2);
230 groupingSize
= pat
.getGroupingSize();
231 logln((UnicodeString
)"Grouping size (should be 2): " + (int32_t) groupingSize
);
232 if(groupingSize
!= 2) {
233 errln((UnicodeString
)"ERROR: setGroupingSize() failed");
236 pat
.setDecimalSeparatorAlwaysShown(TRUE
);
237 UBool tf
= pat
.isDecimalSeparatorAlwaysShown();
238 logln((UnicodeString
)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString
) (tf
? "TRUE" : "FALSE"));
240 errln((UnicodeString
)"ERROR: setDecimalSeparatorAlwaysShown() failed");
242 // Added by Ken Liu testing set/isExponentSignAlwaysShown
243 pat
.setExponentSignAlwaysShown(TRUE
);
244 UBool esas
= pat
.isExponentSignAlwaysShown();
245 logln((UnicodeString
)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString
) (esas
? "TRUE" : "FALSE"));
247 errln((UnicodeString
)"ERROR: ExponentSignAlwaysShown() failed");
250 // Added by Ken Liu testing set/isScientificNotation
251 pat
.setScientificNotation(TRUE
);
252 UBool sn
= pat
.isScientificNotation();
253 logln((UnicodeString
)"isScientificNotation (should be TRUE) is " + (UnicodeString
) (sn
? "TRUE" : "FALSE"));
255 errln((UnicodeString
)"ERROR: setScientificNotation() failed");
258 // Added by Ken Liu testing set/getMinimumExponentDigits
259 int8_t MinimumExponentDigits
= 0;
260 pat
.setMinimumExponentDigits(2);
261 MinimumExponentDigits
= pat
.getMinimumExponentDigits();
262 logln((UnicodeString
)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits
);
263 if(MinimumExponentDigits
!= 2) {
264 errln((UnicodeString
)"ERROR: setMinimumExponentDigits() failed");
267 // Added by Ken Liu testing set/getRoundingIncrement
268 double RoundingIncrement
= 0.0;
269 pat
.setRoundingIncrement(2.0);
270 RoundingIncrement
= pat
.getRoundingIncrement();
271 logln((UnicodeString
)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement
);
272 if(RoundingIncrement
!= 2.0) {
273 errln((UnicodeString
)"ERROR: setRoundingIncrement() failed");
275 //end of Ken's Adding
277 UnicodeString funkyPat
;
278 funkyPat
= pat
.toPattern(funkyPat
);
279 logln((UnicodeString
)"Pattern is " + funkyPat
);
281 UnicodeString locPat
;
282 locPat
= pat
.toLocalizedPattern(locPat
);
283 logln((UnicodeString
)"Localized pattern is " + locPat
);
285 // ======= Test applyPattern()
287 logln((UnicodeString
)"Testing applyPattern()");
289 UnicodeString
p1("#,##0.0#;(#,##0.0#)");
290 logln((UnicodeString
)"Applying pattern " + p1
);
291 status
= U_ZERO_ERROR
;
292 pat
.applyPattern(p1
, status
);
293 if(U_FAILURE(status
)) {
294 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
297 s2
= pat
.toPattern(s2
);
298 logln((UnicodeString
)"Extracted pattern is " + s2
);
300 errln((UnicodeString
)"ERROR: toPattern() result did not match pattern applied");
303 if(pat
.getSecondaryGroupingSize() != 0) {
304 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat
.getSecondaryGroupingSize());
307 if(pat
.getGroupingSize() != 3) {
308 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
311 UnicodeString
p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
312 logln((UnicodeString
)"Applying pattern " + p2
);
313 status
= U_ZERO_ERROR
;
314 pat
.applyLocalizedPattern(p2
, status
);
315 if(U_FAILURE(status
)) {
316 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
319 s3
= pat
.toLocalizedPattern(s3
);
320 logln((UnicodeString
)"Extracted pattern is " + s3
);
322 errln((UnicodeString
)"ERROR: toLocalizedPattern() result did not match pattern applied");
325 status
= U_ZERO_ERROR
;
327 pat
.applyLocalizedPattern(p2
, pe
, status
);
328 if(U_FAILURE(status
)) {
329 errln((UnicodeString
)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status
);
332 s4
= pat
.toLocalizedPattern(s3
);
333 logln((UnicodeString
)"Extracted pattern is " + s4
);
335 errln((UnicodeString
)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
338 if(pat
.getSecondaryGroupingSize() != 2) {
339 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat
.getSecondaryGroupingSize());
342 if(pat
.getGroupingSize() != 3) {
343 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
346 // ======= Test getStaticClassID()
348 logln((UnicodeString
)"Testing getStaticClassID()");
350 status
= U_ZERO_ERROR
;
351 NumberFormat
*test
= new DecimalFormat(status
);
352 if(U_FAILURE(status
)) {
353 errln((UnicodeString
)"ERROR: Couldn't create a DecimalFormat");
356 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
357 errln((UnicodeString
)"ERROR: getDynamicClassID() didn't return the expected value");
363 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
365 UErrorCode status
= U_ZERO_ERROR
;
366 double Roundingnumber
= 2.55;
367 double Roundingnumber1
= -2.55;
368 //+2.55 results -2.55 results
369 double result
[]={ 3.0, -2.0, // kRoundCeiling 0,
370 2.0, -3.0, // kRoundFloor 1,
371 2.0, -2.0, // kRoundDown 2,
372 3.0, -3.0, // kRoundUp 3,
373 3.0, -3.0, // kRoundHalfEven 4,
374 3.0, -3.0, // kRoundHalfDown 5,
375 3.0, -3.0 // kRoundHalfUp 6
377 DecimalFormat
pat(status
);
378 if(U_FAILURE(status
)) {
379 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (default)");
384 UnicodeString message
;
385 UnicodeString resultStr
;
386 for(mode
=0;mode
< 7;mode
++){
387 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)mode
);
388 if(pat
.getRoundingMode() != (DecimalFormat::ERoundingMode
)mode
){
389 errln((UnicodeString
)"SetRoundingMode or GetRoundingMode failed for mode=" + mode
);
393 //for +2.55 with RoundingIncrement=1.0
394 pat
.setRoundingIncrement(1.0);
395 pat
.format(Roundingnumber
, resultStr
);
396 message
= (UnicodeString
)"round(" + (double)Roundingnumber
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
397 verify(message
, resultStr
, result
[i
++]);
401 //for -2.55 with RoundingIncrement=1.0
402 pat
.format(Roundingnumber1
, resultStr
);
403 message
= (UnicodeString
)"round(" + (double)Roundingnumber1
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
404 verify(message
, resultStr
, result
[i
++]);
410 void IntlTestDecimalFormatAPI::verify(const UnicodeString
& message
, const UnicodeString
& got
, double expected
){
411 logln((UnicodeString
)message
+ got
+ (UnicodeString
)" Expected : " + expected
);
412 UnicodeString
expectedStr("");
413 expectedStr
=expectedStr
+ expected
;
414 if(got
!= expectedStr
) {
415 errln((UnicodeString
)"ERROR: Round() failed: " + message
+ got
+ (UnicodeString
)" Expected : " + expectedStr
);
419 #endif /* #if !UCONFIG_NO_FORMATTING */