]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dcfmapts.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / intltest / dcfmapts.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-1999, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "dcfmapts.h"
12
13 #include "unicode/decimfmt.h"
14 #include "unicode/dcfmtsym.h"
15 #include "unicode/parseerr.h"
16
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.
20
21 void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
22 {
23 if (exec) logln((UnicodeString)"TestSuite DecimalFormatAPI");
24 switch (index) {
25 case 0: name = "DecimalFormat API test";
26 if (exec) {
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");
32 }
33 testAPI(/*par*/);
34 }
35 break;
36 case 1: name = "Rounding test";
37 if(exec) {
38 logln((UnicodeString)"DecimalFormat Rounding test---");
39 testRounding(/*par*/);
40 }
41 break;
42
43 default: name = ""; break;
44 }
45 }
46
47 /**
48 * This test checks various generic API methods in DecimalFormat to achieve 100%
49 * API coverage.
50 */
51 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
52 {
53 UErrorCode status = U_ZERO_ERROR;
54
55 // ======= Test constructors
56
57 logln((UnicodeString)"Testing DecimalFormat constructors");
58
59 DecimalFormat def(status);
60 if(U_FAILURE(status)) {
61 errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
62 return;
63 }
64
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)");
70 return;
71 }
72
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)");
77 return;
78 }
79
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*)");
84 }
85
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)");
90 }
91
92 DecimalFormat copy(pat);
93
94 // ======= Test clone(), assignment, and equality
95
96 logln((UnicodeString)"Testing clone(), assignment and equality operators");
97
98 if( ! (copy == pat) || copy != pat) {
99 errln((UnicodeString)"ERROR: Copy constructor or == failed");
100 }
101
102 copy = cust1;
103 if(copy != cust1) {
104 errln((UnicodeString)"ERROR: Assignment (or !=) failed");
105 }
106
107 Format *clone = def.clone();
108 if( ! (*clone == def) ) {
109 errln((UnicodeString)"ERROR: Clone() failed");
110 }
111 delete clone;
112
113 // ======= Test various format() methods
114
115 logln((UnicodeString)"Testing various format() methods");
116
117 double d = -10456.0037;
118 int32_t l = 100000000;
119 Formattable fD(d);
120 Formattable fL(l);
121
122 UnicodeString res1, res2, res3, res4;
123 FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
124
125 res1 = def.format(d, res1, pos1);
126 logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
127
128 res2 = pat.format(l, res2, pos2);
129 logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
130
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");
135 }
136 logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res3);
137
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");
142 }
143 logln((UnicodeString) "" + fL.getLong() + " formatted to " + res4);
144
145 // ======= Test parse()
146
147 logln((UnicodeString)"Testing parse()");
148
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");
157 }
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);
161 }
162 logln(text + " parsed into " + (int32_t) result1.getDouble());
163
164 status = U_ZERO_ERROR;
165 pat.parse(text, result2, status);
166 if(U_FAILURE(status)) {
167 errln((UnicodeString)"ERROR: parse() failed");
168 }
169 if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
170 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
171 }
172 logln(text + " parsed into " + (int32_t) result2.getDouble());
173
174 // ======= Test getters and setters
175
176 logln((UnicodeString)"Testing getters and setters");
177
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");
184 }
185
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");
192 }
193
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");
200 }
201
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");
208 }
209
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");
216 }
217
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");
224 }
225
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");
232 }
233
234 pat.setDecimalSeparatorAlwaysShown(TRUE);
235 UBool tf = pat.isDecimalSeparatorAlwaysShown();
236 logln((UnicodeString)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString) (tf ? "TRUE" : "FALSE"));
237 if(tf != TRUE) {
238 errln((UnicodeString)"ERROR: setDecimalSeparatorAlwaysShown() failed");
239 }
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"));
244 if(esas != TRUE) {
245 errln((UnicodeString)"ERROR: ExponentSignAlwaysShown() failed");
246 }
247
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"));
252 if(sn != TRUE) {
253 errln((UnicodeString)"ERROR: setScientificNotation() failed");
254 }
255
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");
263 }
264
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");
272 }
273 //end of Ken's Adding
274
275 UnicodeString funkyPat;
276 funkyPat = pat.toPattern(funkyPat);
277 logln((UnicodeString)"Pattern is " + funkyPat);
278
279 UnicodeString locPat;
280 locPat = pat.toLocalizedPattern(locPat);
281 logln((UnicodeString)"Localized pattern is " + locPat);
282
283 // ======= Test applyPattern()
284
285 logln((UnicodeString)"Testing applyPattern()");
286
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);
293 }
294 UnicodeString s2;
295 s2 = pat.toPattern(s2);
296 logln((UnicodeString)"Extracted pattern is " + s2);
297 if(s2 != p1) {
298 errln((UnicodeString)"ERROR: toPattern() result did not match pattern applied");
299 }
300
301 if(pat.getSecondaryGroupingSize() != 0) {
302 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat.getSecondaryGroupingSize());
303 }
304
305 if(pat.getGroupingSize() != 3) {
306 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
307 }
308
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);
315 }
316 UnicodeString s3;
317 s3 = pat.toLocalizedPattern(s3);
318 logln((UnicodeString)"Extracted pattern is " + s3);
319 if(s3 != p2) {
320 errln((UnicodeString)"ERROR: toLocalizedPattern() result did not match pattern applied");
321 }
322
323 status = U_ZERO_ERROR;
324 UParseError pe;
325 pat.applyLocalizedPattern(p2, pe, status);
326 if(U_FAILURE(status)) {
327 errln((UnicodeString)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status);
328 }
329 UnicodeString s4;
330 s4 = pat.toLocalizedPattern(s3);
331 logln((UnicodeString)"Extracted pattern is " + s4);
332 if(s4 != p2) {
333 errln((UnicodeString)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
334 }
335
336 if(pat.getSecondaryGroupingSize() != 2) {
337 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat.getSecondaryGroupingSize());
338 }
339
340 if(pat.getGroupingSize() != 3) {
341 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
342 }
343
344 // ======= Test getStaticClassID()
345
346 logln((UnicodeString)"Testing getStaticClassID()");
347
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");
352 }
353
354 if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
355 errln((UnicodeString)"ERROR: getDynamicClassID() didn't return the expected value");
356 }
357
358 delete test;
359 }
360
361 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
362 {
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
374 };
375 DecimalFormat pat(status);
376 if(U_FAILURE(status)) {
377 errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
378 return;
379 }
380 uint16_t mode;
381 uint16_t i=0;
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);
388 }
389
390
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++]);
396 message.remove();
397 resultStr.remove();
398
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++]);
403 message.remove();
404 resultStr.remove();
405 }
406
407 }
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);
414 }
415 }
416
417 #endif /* #if !UCONFIG_NO_FORMATTING */