]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dcfmapts.cpp
1 /********************************************************************
3 * Copyright (c) 1997-1999, 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
;
29 Locale::setDefault(Locale::getEnglish(), status
);
30 if(U_FAILURE(status
)) {
31 errln((UnicodeString
)"ERROR: Could not set default locale, test may not give correct results");
36 case 1: name
= "Rounding test";
38 logln((UnicodeString
)"DecimalFormat Rounding test---");
39 testRounding(/*par*/);
43 default: name
= ""; break;
48 * This test checks various generic API methods in DecimalFormat to achieve 100%
51 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
53 UErrorCode status
= U_ZERO_ERROR
;
55 // ======= Test constructors
57 logln((UnicodeString
)"Testing DecimalFormat constructors");
59 DecimalFormat
def(status
);
60 if(U_FAILURE(status
)) {
61 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (default)");
65 status
= U_ZERO_ERROR
;
66 const UnicodeString
pattern("#,##0.# FF");
67 DecimalFormat
pat(pattern
, status
);
68 if(U_FAILURE(status
)) {
69 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern)");
73 status
= U_ZERO_ERROR
;
74 DecimalFormatSymbols
*symbols
= new DecimalFormatSymbols(Locale::getFrench(), status
);
75 if(U_FAILURE(status
)) {
76 errln((UnicodeString
)"ERROR: Could not create DecimalFormatSymbols (French)");
80 status
= U_ZERO_ERROR
;
81 DecimalFormat
cust1(pattern
, symbols
, status
);
82 if(U_FAILURE(status
)) {
83 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
86 status
= U_ZERO_ERROR
;
87 DecimalFormat
cust2(pattern
, *symbols
, status
);
88 if(U_FAILURE(status
)) {
89 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (pattern, symbols)");
92 DecimalFormat
copy(pat
);
94 // ======= Test clone(), assignment, and equality
96 logln((UnicodeString
)"Testing clone(), assignment and equality operators");
98 if( ! (copy
== pat
) || copy
!= pat
) {
99 errln((UnicodeString
)"ERROR: Copy constructor or == failed");
104 errln((UnicodeString
)"ERROR: Assignment (or !=) failed");
107 Format
*clone
= def
.clone();
108 if( ! (*clone
== def
) ) {
109 errln((UnicodeString
)"ERROR: Clone() failed");
113 // ======= Test various format() methods
115 logln((UnicodeString
)"Testing various format() methods");
117 double d
= -10456.0037;
118 int32_t l
= 100000000;
122 UnicodeString res1
, res2
, res3
, res4
;
123 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
125 res1
= def
.format(d
, res1
, pos1
);
126 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
128 res2
= pat
.format(l
, res2
, pos2
);
129 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
131 status
= U_ZERO_ERROR
;
132 res3
= cust1
.format(fD
, res3
, pos3
, status
);
133 if(U_FAILURE(status
)) {
134 errln((UnicodeString
)"ERROR: format(Formattable [double]) failed");
136 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res3
);
138 status
= U_ZERO_ERROR
;
139 res4
= cust2
.format(fL
, res4
, pos4
, status
);
140 if(U_FAILURE(status
)) {
141 errln((UnicodeString
)"ERROR: format(Formattable [long]) failed");
143 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res4
);
145 // ======= Test parse()
147 logln((UnicodeString
)"Testing parse()");
149 UnicodeString
text("-10,456.0037");
150 Formattable result1
, result2
;
151 ParsePosition
pos(0);
152 UnicodeString
patt("#,##0.#");
153 status
= U_ZERO_ERROR
;
154 pat
.applyPattern(patt
, status
);
155 if(U_FAILURE(status
)) {
156 errln((UnicodeString
)"ERROR: applyPattern() failed");
158 pat
.parse(text
, result1
, pos
);
159 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
160 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
162 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
164 status
= U_ZERO_ERROR
;
165 pat
.parse(text
, result2
, status
);
166 if(U_FAILURE(status
)) {
167 errln((UnicodeString
)"ERROR: parse() failed");
169 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
170 errln((UnicodeString
)"ERROR: Roundtrip failed (via parse()) for " + text
);
172 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
174 // ======= Test getters and setters
176 logln((UnicodeString
)"Testing getters and setters");
178 const DecimalFormatSymbols
*syms
= pat
.getDecimalFormatSymbols();
179 DecimalFormatSymbols
*newSyms
= new DecimalFormatSymbols(*syms
);
180 def
.setDecimalFormatSymbols(*newSyms
);
181 def
.adoptDecimalFormatSymbols(newSyms
); // don't use newSyms after this
182 if( *(pat
.getDecimalFormatSymbols()) != *(def
.getDecimalFormatSymbols())) {
183 errln((UnicodeString
)"ERROR: adopt or set DecimalFormatSymbols() failed");
186 UnicodeString posPrefix
;
187 pat
.setPositivePrefix("+");
188 posPrefix
= pat
.getPositivePrefix(posPrefix
);
189 logln((UnicodeString
)"Positive prefix (should be +): " + posPrefix
);
190 if(posPrefix
!= "+") {
191 errln((UnicodeString
)"ERROR: setPositivePrefix() failed");
194 UnicodeString negPrefix
;
195 pat
.setNegativePrefix("-");
196 negPrefix
= pat
.getNegativePrefix(negPrefix
);
197 logln((UnicodeString
)"Negative prefix (should be -): " + negPrefix
);
198 if(negPrefix
!= "-") {
199 errln((UnicodeString
)"ERROR: setNegativePrefix() failed");
202 UnicodeString posSuffix
;
203 pat
.setPositiveSuffix("_");
204 posSuffix
= pat
.getPositiveSuffix(posSuffix
);
205 logln((UnicodeString
)"Positive suffix (should be _): " + posSuffix
);
206 if(posSuffix
!= "_") {
207 errln((UnicodeString
)"ERROR: setPositiveSuffix() failed");
210 UnicodeString negSuffix
;
211 pat
.setNegativeSuffix("~");
212 negSuffix
= pat
.getNegativeSuffix(negSuffix
);
213 logln((UnicodeString
)"Negative suffix (should be ~): " + negSuffix
);
214 if(negSuffix
!= "~") {
215 errln((UnicodeString
)"ERROR: setNegativeSuffix() failed");
218 int32_t multiplier
= 0;
219 pat
.setMultiplier(8);
220 multiplier
= pat
.getMultiplier();
221 logln((UnicodeString
)"Multiplier (should be 8): " + multiplier
);
222 if(multiplier
!= 8) {
223 errln((UnicodeString
)"ERROR: setMultiplier() failed");
226 int32_t groupingSize
= 0;
227 pat
.setGroupingSize(2);
228 groupingSize
= pat
.getGroupingSize();
229 logln((UnicodeString
)"Grouping size (should be 2): " + (int32_t) groupingSize
);
230 if(groupingSize
!= 2) {
231 errln((UnicodeString
)"ERROR: setGroupingSize() failed");
234 pat
.setDecimalSeparatorAlwaysShown(TRUE
);
235 UBool tf
= pat
.isDecimalSeparatorAlwaysShown();
236 logln((UnicodeString
)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString
) (tf
? "TRUE" : "FALSE"));
238 errln((UnicodeString
)"ERROR: setDecimalSeparatorAlwaysShown() failed");
240 // Added by Ken Liu testing set/isExponentSignAlwaysShown
241 pat
.setExponentSignAlwaysShown(TRUE
);
242 UBool esas
= pat
.isExponentSignAlwaysShown();
243 logln((UnicodeString
)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString
) (esas
? "TRUE" : "FALSE"));
245 errln((UnicodeString
)"ERROR: ExponentSignAlwaysShown() failed");
248 // Added by Ken Liu testing set/isScientificNotation
249 pat
.setScientificNotation(TRUE
);
250 UBool sn
= pat
.isScientificNotation();
251 logln((UnicodeString
)"isScientificNotation (should be TRUE) is " + (UnicodeString
) (sn
? "TRUE" : "FALSE"));
253 errln((UnicodeString
)"ERROR: setScientificNotation() failed");
256 // Added by Ken Liu testing set/getMinimumExponentDigits
257 int8_t MinimumExponentDigits
= 0;
258 pat
.setMinimumExponentDigits(2);
259 MinimumExponentDigits
= pat
.getMinimumExponentDigits();
260 logln((UnicodeString
)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits
);
261 if(MinimumExponentDigits
!= 2) {
262 errln((UnicodeString
)"ERROR: setMinimumExponentDigits() failed");
265 // Added by Ken Liu testing set/getRoundingIncrement
266 double RoundingIncrement
= 0.0;
267 pat
.setRoundingIncrement(2.0);
268 RoundingIncrement
= pat
.getRoundingIncrement();
269 logln((UnicodeString
)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement
);
270 if(RoundingIncrement
!= 2.0) {
271 errln((UnicodeString
)"ERROR: setRoundingIncrement() failed");
273 //end of Ken's Adding
275 UnicodeString funkyPat
;
276 funkyPat
= pat
.toPattern(funkyPat
);
277 logln((UnicodeString
)"Pattern is " + funkyPat
);
279 UnicodeString locPat
;
280 locPat
= pat
.toLocalizedPattern(locPat
);
281 logln((UnicodeString
)"Localized pattern is " + locPat
);
283 // ======= Test applyPattern()
285 logln((UnicodeString
)"Testing applyPattern()");
287 UnicodeString
p1("#,##0.0#;(#,##0.0#)");
288 logln((UnicodeString
)"Applying pattern " + p1
);
289 status
= U_ZERO_ERROR
;
290 pat
.applyPattern(p1
, status
);
291 if(U_FAILURE(status
)) {
292 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
295 s2
= pat
.toPattern(s2
);
296 logln((UnicodeString
)"Extracted pattern is " + s2
);
298 errln((UnicodeString
)"ERROR: toPattern() result did not match pattern applied");
301 if(pat
.getSecondaryGroupingSize() != 0) {
302 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat
.getSecondaryGroupingSize());
305 if(pat
.getGroupingSize() != 3) {
306 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
309 UnicodeString
p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
310 logln((UnicodeString
)"Applying pattern " + p2
);
311 status
= U_ZERO_ERROR
;
312 pat
.applyLocalizedPattern(p2
, status
);
313 if(U_FAILURE(status
)) {
314 errln((UnicodeString
)"ERROR: applyPattern() failed with " + (int32_t) status
);
317 s3
= pat
.toLocalizedPattern(s3
);
318 logln((UnicodeString
)"Extracted pattern is " + s3
);
320 errln((UnicodeString
)"ERROR: toLocalizedPattern() result did not match pattern applied");
323 status
= U_ZERO_ERROR
;
325 pat
.applyLocalizedPattern(p2
, pe
, status
);
326 if(U_FAILURE(status
)) {
327 errln((UnicodeString
)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status
);
330 s4
= pat
.toLocalizedPattern(s3
);
331 logln((UnicodeString
)"Extracted pattern is " + s4
);
333 errln((UnicodeString
)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
336 if(pat
.getSecondaryGroupingSize() != 2) {
337 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat
.getSecondaryGroupingSize());
340 if(pat
.getGroupingSize() != 3) {
341 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat
.getGroupingSize());
344 // ======= Test getStaticClassID()
346 logln((UnicodeString
)"Testing getStaticClassID()");
348 status
= U_ZERO_ERROR
;
349 NumberFormat
*test
= new DecimalFormat(status
);
350 if(U_FAILURE(status
)) {
351 errln((UnicodeString
)"ERROR: Couldn't create a DecimalFormat");
354 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
355 errln((UnicodeString
)"ERROR: getDynamicClassID() didn't return the expected value");
361 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
363 UErrorCode status
= U_ZERO_ERROR
;
364 double Roundingnumber
= 2.55;
365 double Roundingnumber1
= -2.55;
366 //+2.55 results -2.55 results
367 double result
[]={ 3.0, -2.0, // kRoundCeiling 0,
368 2.0, -3.0, // kRoundFloor 1,
369 2.0, -2.0, // kRoundDown 2,
370 3.0, -3.0, // kRoundUp 3,
371 3.0, -3.0, // kRoundHalfEven 4,
372 3.0, -3.0, // kRoundHalfDown 5,
373 3.0, -3.0 // kRoundHalfUp 6
375 DecimalFormat
pat(status
);
376 if(U_FAILURE(status
)) {
377 errln((UnicodeString
)"ERROR: Could not create DecimalFormat (default)");
382 UnicodeString message
;
383 UnicodeString resultStr
;
384 for(mode
=0;mode
< 7;mode
++){
385 pat
.setRoundingMode((DecimalFormat::ERoundingMode
)mode
);
386 if(pat
.getRoundingMode() != (DecimalFormat::ERoundingMode
)mode
){
387 errln((UnicodeString
)"SetRoundingMode or GetRoundingMode failed for mode=" + mode
);
391 //for +2.55 with RoundingIncrement=1.0
392 pat
.setRoundingIncrement(1.0);
393 pat
.format(Roundingnumber
, resultStr
);
394 message
= (UnicodeString
)"round(" + (double)Roundingnumber
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
395 verify(message
, resultStr
, result
[i
++]);
399 //for -2.55 with RoundingIncrement=1.0
400 pat
.format(Roundingnumber1
, resultStr
);
401 message
= (UnicodeString
)"round(" + (double)Roundingnumber1
+ UnicodeString(",") + mode
+ UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
402 verify(message
, resultStr
, result
[i
++]);
408 void IntlTestDecimalFormatAPI::verify(const UnicodeString
& message
, const UnicodeString
& got
, double expected
){
409 logln((UnicodeString
)message
+ got
+ (UnicodeString
)" Expected : " + expected
);
410 UnicodeString
expectedStr("");
411 expectedStr
=expectedStr
+ expected
;
412 if(got
!= expectedStr
) {
413 errln((UnicodeString
)"ERROR: Round() failed: " + message
+ got
+ (UnicodeString
)" Expected : " + expectedStr
);
417 #endif /* #if !UCONFIG_NO_FORMATTING */