1 /***********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/numfmt.h"
14 #include "unicode/decimfmt.h"
15 #include "unicode/locid.h"
16 #include "unicode/unum.h"
17 #include "unicode/strenum.h"
19 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
20 // try to test the full functionality. It just calls each function in the class and
21 // verifies that it works on a basic level.
23 void IntlTestNumberFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
25 if (exec
) logln("TestSuite NumberFormatAPI");
27 case 0: name
= "NumberFormat API test";
29 logln("NumberFormat API test---"); logln("");
30 UErrorCode status
= U_ZERO_ERROR
;
31 Locale::setDefault(Locale::getEnglish(), status
);
32 if(U_FAILURE(status
)) {
33 errln("ERROR: Could not set default locale, test may not give correct results");
38 case 1: name
= "NumberFormatRegistration";
40 logln("NumberFormat Registration test---"); logln("");
41 UErrorCode status
= U_ZERO_ERROR
;
42 Locale::setDefault(Locale::getEnglish(), status
);
43 if(U_FAILURE(status
)) {
44 errln("ERROR: Could not set default locale, test may not give correct results");
49 default: name
= ""; break;
54 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
57 void IntlTestNumberFormatAPI::testAPI(/* char* par */)
59 UErrorCode status
= U_ZERO_ERROR
;
61 // ======= Test constructors
63 logln("Testing NumberFormat constructors");
65 NumberFormat
*def
= NumberFormat::createInstance(status
);
66 if(U_FAILURE(status
)) {
67 errln("ERROR: Could not create NumberFormat (default)");
70 status
= U_ZERO_ERROR
;
71 NumberFormat
*fr
= NumberFormat::createInstance(Locale::getFrench(), status
);
72 if(U_FAILURE(status
)) {
73 errln("ERROR: Could not create NumberFormat (French)");
76 NumberFormat
*cur
= NumberFormat::createCurrencyInstance(status
);
77 if(U_FAILURE(status
)) {
78 errln("ERROR: Could not create NumberFormat (currency, default)");
81 status
= U_ZERO_ERROR
;
82 NumberFormat
*cur_fr
= NumberFormat::createCurrencyInstance(Locale::getFrench(), status
);
83 if(U_FAILURE(status
)) {
84 errln("ERROR: Could not create NumberFormat (currency, French)");
87 NumberFormat
*per
= NumberFormat::createPercentInstance(status
);
88 if(U_FAILURE(status
)) {
89 errln("ERROR: Could not create NumberFormat (percent, default)");
92 status
= U_ZERO_ERROR
;
93 NumberFormat
*per_fr
= NumberFormat::createPercentInstance(Locale::getFrench(), status
);
94 if(U_FAILURE(status
)) {
95 errln("ERROR: Could not create NumberFormat (percent, French)");
98 // ======= Test equality
100 logln("Testing equality operator");
102 if( *per_fr
== *cur_fr
|| ! ( *per_fr
!= *cur_fr
) ) {
103 errln("ERROR: == failed");
106 // ======= Test various format() methods
108 logln("Testing various format() methods");
110 double d
= -10456.0037;
111 int32_t l
= 100000000;
115 UnicodeString res1
, res2
, res3
, res4
, res5
, res6
;
116 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
118 res1
= cur_fr
->format(d
, res1
);
119 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
121 res2
= cur_fr
->format(l
, res2
);
122 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
124 res3
= cur_fr
->format(d
, res3
, pos1
);
125 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res3
);
127 res4
= cur_fr
->format(l
, res4
, pos2
);
128 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res4
);
130 status
= U_ZERO_ERROR
;
131 res5
= cur_fr
->format(fD
, res5
, pos3
, status
);
132 if(U_FAILURE(status
)) {
133 errln("ERROR: format(Formattable [double]) failed");
135 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res5
);
137 status
= U_ZERO_ERROR
;
138 res6
= cur_fr
->format(fL
, res6
, pos4
, status
);
139 if(U_FAILURE(status
)) {
140 errln("ERROR: format(Formattable [long]) failed");
142 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res6
);
145 // ======= Test parse()
147 logln("Testing parse()");
149 UnicodeString
text("-10,456.0037");
150 Formattable result1
, result2
, result3
;
151 ParsePosition
pos(0), pos01(0);
152 fr
->parseObject(text
, result1
, pos
);
153 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
154 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
156 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
158 fr
->parse(text
, result2
, pos01
);
159 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
160 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
162 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
164 status
= U_ZERO_ERROR
;
165 fr
->parse(text
, result3
, status
);
166 if(U_FAILURE(status
)) {
167 errln("ERROR: parse() failed");
169 if(result3
.getType() != Formattable::kDouble
&& result3
.getDouble() != d
) {
170 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
172 logln(text
+ " parsed into " + (int32_t) result3
.getDouble());
175 // ======= Test getters and setters
177 logln("Testing getters and setters");
180 const Locale
*locales
= NumberFormat::getAvailableLocales(count
);
181 logln((UnicodeString
) "Got " + count
+ " locales" );
182 for(int32_t i
= 0; i
< count
; i
++) {
183 UnicodeString
name(locales
[i
].getName(),"");
187 fr
->setParseIntegerOnly( def
->isParseIntegerOnly() );
188 if(fr
->isParseIntegerOnly() != def
->isParseIntegerOnly() ) {
189 errln("ERROR: setParseIntegerOnly() failed");
192 fr
->setGroupingUsed( def
->isGroupingUsed() );
193 if(fr
->isGroupingUsed() != def
->isGroupingUsed() ) {
194 errln("ERROR: setGroupingUsed() failed");
197 fr
->setMaximumIntegerDigits( def
->getMaximumIntegerDigits() );
198 if(fr
->getMaximumIntegerDigits() != def
->getMaximumIntegerDigits() ) {
199 errln("ERROR: setMaximumIntegerDigits() failed");
202 fr
->setMinimumIntegerDigits( def
->getMinimumIntegerDigits() );
203 if(fr
->getMinimumIntegerDigits() != def
->getMinimumIntegerDigits() ) {
204 errln("ERROR: setMinimumIntegerDigits() failed");
207 fr
->setMaximumFractionDigits( def
->getMaximumFractionDigits() );
208 if(fr
->getMaximumFractionDigits() != def
->getMaximumFractionDigits() ) {
209 errln("ERROR: setMaximumFractionDigits() failed");
212 fr
->setMinimumFractionDigits( def
->getMinimumFractionDigits() );
213 if(fr
->getMinimumFractionDigits() != def
->getMinimumFractionDigits() ) {
214 errln("ERROR: setMinimumFractionDigits() failed");
218 // ======= Test getStaticClassID()
220 logln("Testing getStaticClassID()");
222 status
= U_ZERO_ERROR
;
223 NumberFormat
*test
= new DecimalFormat(status
);
224 if(U_FAILURE(status
)) {
225 errln("ERROR: Couldn't create a NumberFormat");
228 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
229 errln("ERROR: getDynamicClassID() didn't return the expected value");
241 #if !UCONFIG_NO_SERVICE
243 #define SRC_LOC Locale::getFrance()
244 #define SWAP_LOC Locale::getUS()
246 class NFTestFactory
: public SimpleNumberFormatFactory
{
247 NumberFormat
* currencyStyle
;
251 : SimpleNumberFormatFactory(SRC_LOC
, TRUE
)
253 UErrorCode status
= U_ZERO_ERROR
;
254 currencyStyle
= NumberFormat::createInstance(SWAP_LOC
, status
);
257 virtual ~NFTestFactory()
259 delete currencyStyle
;
262 virtual NumberFormat
* createFormat(const Locale
& /* loc */, UNumberFormatStyle formatType
)
264 if (formatType
== UNUM_CURRENCY
) {
265 return (NumberFormat
*)currencyStyle
->clone();
270 virtual inline UClassID
getDynamicClassID() const
272 return (UClassID
)&gID
;
275 static inline UClassID
getStaticClassID()
277 return (UClassID
)&gID
;
284 char NFTestFactory::gID
= 0;
288 IntlTestNumberFormatAPI::testRegistration()
290 #if !UCONFIG_NO_SERVICE
291 UErrorCode status
= U_ZERO_ERROR
;
293 NumberFormat
* f0
= NumberFormat::createInstance(SWAP_LOC
, status
);
294 NumberFormat
* f1
= NumberFormat::createInstance(SRC_LOC
, status
);
295 NumberFormat
* f2
= NumberFormat::createCurrencyInstance(SRC_LOC
, status
);
296 URegistryKey key
= NumberFormat::registerFactory(new NFTestFactory(), status
);
297 NumberFormat
* f3
= NumberFormat::createCurrencyInstance(SRC_LOC
, status
);
298 NumberFormat
* f3a
= NumberFormat::createCurrencyInstance(SRC_LOC
, status
);
299 NumberFormat
* f4
= NumberFormat::createInstance(SRC_LOC
, status
);
301 StringEnumeration
* locs
= NumberFormat::getAvailableLocales();
303 UNumberFormat
* uf3
= unum_open(UNUM_CURRENCY
, NULL
, 0, SRC_LOC
.getName(),NULL
, &status
);
304 UNumberFormat
* uf4
= unum_open(UNUM_DEFAULT
, NULL
, 0, SRC_LOC
.getName(), NULL
, &status
);
306 const UnicodeString
* res
;
307 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
308 logln(*res
); // service is still in synch
311 NumberFormat::unregister(key
, status
); // restore for other tests
312 NumberFormat
* f5
= NumberFormat::createCurrencyInstance(SRC_LOC
, status
);
313 UNumberFormat
* uf5
= unum_open(UNUM_CURRENCY
, NULL
, 0, SRC_LOC
.getName(),NULL
, &status
);
316 UnicodeString res0
, res1
, res2
, res3
, res4
, res5
;
328 unum_formatDouble(uf3
, n
, ures3
, 50, NULL
, &status
);
329 unum_formatDouble(uf4
, n
, ures4
, 50, NULL
, &status
);
330 unum_formatDouble(uf5
, n
, ures5
, 50, NULL
, &status
);
332 logln((UnicodeString
)"f0 swap int: " + res0
);
333 logln((UnicodeString
)"f1 src int: " + res1
);
334 logln((UnicodeString
)"f2 src cur: " + res2
);
335 logln((UnicodeString
)"f3 reg cur: " + res3
);
336 logln((UnicodeString
)"f4 reg int: " + res4
);
337 logln((UnicodeString
)"f5 unreg cur: " + res5
);
338 log("uf3 reg cur: ");
340 log("uf4 reg int: ");
342 log("uf5 ureg cur: ");
346 errln("did not get new instance from service");
351 errln("registered service did not match");
354 errln("registered service did not inherit");
357 errln("unregistered service did not match original");
361 errln("registered service did not match / unum");
364 errln("registered service did not inherit / unum");
367 errln("unregistered service did not match original / unum");
380 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
381 errln(*res
); // service should be out of synch
384 locs
->reset(status
); // now in synch again, we hope
385 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
394 #endif /* #if !UCONFIG_NO_FORMATTING */