]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/dcfmapts.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / intltest / dcfmapts.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2003, International Business Machines Corporation and
b75a7d8f
A
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
21void 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;
73c04bcf 29 Locale saveLocale;
b75a7d8f
A
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");
33 }
34 testAPI(/*par*/);
73c04bcf 35 Locale::setDefault(saveLocale, status);
b75a7d8f
A
36 }
37 break;
38 case 1: name = "Rounding test";
39 if(exec) {
40 logln((UnicodeString)"DecimalFormat Rounding test---");
41 testRounding(/*par*/);
42 }
43 break;
44
45 default: name = ""; break;
46 }
47}
48
49/**
50 * This test checks various generic API methods in DecimalFormat to achieve 100%
51 * API coverage.
52 */
53void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
54{
55 UErrorCode status = U_ZERO_ERROR;
56
57// ======= Test constructors
58
59 logln((UnicodeString)"Testing DecimalFormat constructors");
60
61 DecimalFormat def(status);
62 if(U_FAILURE(status)) {
63 errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
64 return;
65 }
66
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)");
72 return;
73 }
74
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)");
79 return;
80 }
81
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*)");
86 }
87
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)");
92 }
93
94 DecimalFormat copy(pat);
95
96// ======= Test clone(), assignment, and equality
97
98 logln((UnicodeString)"Testing clone(), assignment and equality operators");
99
100 if( ! (copy == pat) || copy != pat) {
101 errln((UnicodeString)"ERROR: Copy constructor or == failed");
102 }
103
104 copy = cust1;
105 if(copy != cust1) {
106 errln((UnicodeString)"ERROR: Assignment (or !=) failed");
107 }
108
109 Format *clone = def.clone();
110 if( ! (*clone == def) ) {
111 errln((UnicodeString)"ERROR: Clone() failed");
112 }
113 delete clone;
114
115// ======= Test various format() methods
116
117 logln((UnicodeString)"Testing various format() methods");
118
119 double d = -10456.0037;
120 int32_t l = 100000000;
121 Formattable fD(d);
122 Formattable fL(l);
123
124 UnicodeString res1, res2, res3, res4;
125 FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
126
127 res1 = def.format(d, res1, pos1);
128 logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
129
130 res2 = pat.format(l, res2, pos2);
131 logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
132
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");
137 }
138 logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res3);
139
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");
144 }
145 logln((UnicodeString) "" + fL.getLong() + " formatted to " + res4);
146
147// ======= Test parse()
148
149 logln((UnicodeString)"Testing parse()");
150
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");
159 }
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);
163 }
164 logln(text + " parsed into " + (int32_t) result1.getDouble());
165
166 status = U_ZERO_ERROR;
167 pat.parse(text, result2, status);
168 if(U_FAILURE(status)) {
169 errln((UnicodeString)"ERROR: parse() failed");
170 }
171 if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
172 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
173 }
174 logln(text + " parsed into " + (int32_t) result2.getDouble());
175
176// ======= Test getters and setters
177
178 logln((UnicodeString)"Testing getters and setters");
179
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");
186 }
187
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");
194 }
195
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");
202 }
203
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");
210 }
211
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");
218 }
219
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");
226 }
227
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");
234 }
235
236 pat.setDecimalSeparatorAlwaysShown(TRUE);
237 UBool tf = pat.isDecimalSeparatorAlwaysShown();
238 logln((UnicodeString)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString) (tf ? "TRUE" : "FALSE"));
239 if(tf != TRUE) {
240 errln((UnicodeString)"ERROR: setDecimalSeparatorAlwaysShown() failed");
241 }
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"));
246 if(esas != TRUE) {
247 errln((UnicodeString)"ERROR: ExponentSignAlwaysShown() failed");
248 }
249
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"));
254 if(sn != TRUE) {
255 errln((UnicodeString)"ERROR: setScientificNotation() failed");
256 }
257
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");
265 }
266
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");
274 }
275 //end of Ken's Adding
276
277 UnicodeString funkyPat;
278 funkyPat = pat.toPattern(funkyPat);
279 logln((UnicodeString)"Pattern is " + funkyPat);
280
281 UnicodeString locPat;
282 locPat = pat.toLocalizedPattern(locPat);
283 logln((UnicodeString)"Localized pattern is " + locPat);
284
285// ======= Test applyPattern()
286
287 logln((UnicodeString)"Testing applyPattern()");
288
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);
295 }
296 UnicodeString s2;
297 s2 = pat.toPattern(s2);
298 logln((UnicodeString)"Extracted pattern is " + s2);
299 if(s2 != p1) {
300 errln((UnicodeString)"ERROR: toPattern() result did not match pattern applied");
301 }
302
303 if(pat.getSecondaryGroupingSize() != 0) {
304 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat.getSecondaryGroupingSize());
305 }
306
307 if(pat.getGroupingSize() != 3) {
308 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
309 }
310
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);
317 }
318 UnicodeString s3;
319 s3 = pat.toLocalizedPattern(s3);
320 logln((UnicodeString)"Extracted pattern is " + s3);
321 if(s3 != p2) {
322 errln((UnicodeString)"ERROR: toLocalizedPattern() result did not match pattern applied");
323 }
324
325 status = U_ZERO_ERROR;
326 UParseError pe;
327 pat.applyLocalizedPattern(p2, pe, status);
328 if(U_FAILURE(status)) {
329 errln((UnicodeString)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status);
330 }
331 UnicodeString s4;
332 s4 = pat.toLocalizedPattern(s3);
333 logln((UnicodeString)"Extracted pattern is " + s4);
334 if(s4 != p2) {
335 errln((UnicodeString)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
336 }
337
338 if(pat.getSecondaryGroupingSize() != 2) {
339 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat.getSecondaryGroupingSize());
340 }
341
342 if(pat.getGroupingSize() != 3) {
343 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
344 }
345
346// ======= Test getStaticClassID()
347
348 logln((UnicodeString)"Testing getStaticClassID()");
349
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");
354 }
355
356 if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
357 errln((UnicodeString)"ERROR: getDynamicClassID() didn't return the expected value");
358 }
359
360 delete test;
361}
362
363void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
364{
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
376 };
377 DecimalFormat pat(status);
378 if(U_FAILURE(status)) {
379 errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
380 return;
381 }
382 uint16_t mode;
383 uint16_t i=0;
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);
390 }
391
392
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++]);
398 message.remove();
399 resultStr.remove();
400
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++]);
405 message.remove();
406 resultStr.remove();
407 }
408
409}
410void 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);
416 }
417}
418
419#endif /* #if !UCONFIG_NO_FORMATTING */