1 /***********************************************************************
3 * Copyright (c) 1997-2010, 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
;
32 Locale::setDefault(Locale::getEnglish(), status
);
33 if(U_FAILURE(status
)) {
34 errln("ERROR: Could not set default locale, test may not give correct results");
37 Locale::setDefault(saveLocale
, status
);
40 case 1: name
= "NumberFormatRegistration";
42 logln("NumberFormat Registration test---"); logln("");
43 UErrorCode status
= U_ZERO_ERROR
;
45 Locale::setDefault(Locale::getEnglish(), status
);
46 if(U_FAILURE(status
)) {
47 errln("ERROR: Could not set default locale, test may not give correct results");
50 Locale::setDefault(saveLocale
, status
);
53 default: name
= ""; break;
58 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
61 void IntlTestNumberFormatAPI::testAPI(/* char* par */)
63 UErrorCode status
= U_ZERO_ERROR
;
65 // ======= Test constructors
67 logln("Testing NumberFormat constructors");
69 NumberFormat
*def
= NumberFormat::createInstance(status
);
70 if(U_FAILURE(status
)) {
71 dataerrln("ERROR: Could not create NumberFormat (default) - %s", u_errorName(status
));
74 status
= U_ZERO_ERROR
;
75 NumberFormat
*fr
= NumberFormat::createInstance(Locale::getFrench(), status
);
76 if(U_FAILURE(status
)) {
77 dataerrln("ERROR: Could not create NumberFormat (French) - %s", u_errorName(status
));
80 NumberFormat
*cur
= NumberFormat::createCurrencyInstance(status
);
81 if(U_FAILURE(status
)) {
82 dataerrln("ERROR: Could not create NumberFormat (currency, default) - %s", u_errorName(status
));
85 status
= U_ZERO_ERROR
;
86 NumberFormat
*cur_fr
= NumberFormat::createCurrencyInstance(Locale::getFrench(), status
);
87 if(U_FAILURE(status
)) {
88 dataerrln("ERROR: Could not create NumberFormat (currency, French) - %s", u_errorName(status
));
91 NumberFormat
*per
= NumberFormat::createPercentInstance(status
);
92 if(U_FAILURE(status
)) {
93 dataerrln("ERROR: Could not create NumberFormat (percent, default) - %s", u_errorName(status
));
96 status
= U_ZERO_ERROR
;
97 NumberFormat
*per_fr
= NumberFormat::createPercentInstance(Locale::getFrench(), status
);
98 if(U_FAILURE(status
)) {
99 dataerrln("ERROR: Could not create NumberFormat (percent, French) - %s", u_errorName(status
));
102 // ======= Test equality
103 if (per_fr
!= NULL
&& cur_fr
!= NULL
)
105 logln("Testing equality operator");
107 if( *per_fr
== *cur_fr
|| ! ( *per_fr
!= *cur_fr
) ) {
108 errln("ERROR: == failed");
112 // ======= Test various format() methods
115 logln("Testing various format() methods");
117 double d
= -10456.0037;
118 int32_t l
= 100000000;
122 UnicodeString res1
, res2
, res3
, res4
, res5
, res6
;
123 FieldPosition
pos1(0), pos2(0), pos3(0), pos4(0);
125 res1
= cur_fr
->format(d
, res1
);
126 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res1
);
128 res2
= cur_fr
->format(l
, res2
);
129 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res2
);
131 res3
= cur_fr
->format(d
, res3
, pos1
);
132 logln( (UnicodeString
) "" + (int32_t) d
+ " formatted to " + res3
);
134 res4
= cur_fr
->format(l
, res4
, pos2
);
135 logln((UnicodeString
) "" + (int32_t) l
+ " formatted to " + res4
);
137 status
= U_ZERO_ERROR
;
138 res5
= cur_fr
->format(fD
, res5
, pos3
, status
);
139 if(U_FAILURE(status
)) {
140 errln("ERROR: format(Formattable [double]) failed");
142 logln((UnicodeString
) "" + (int32_t) fD
.getDouble() + " formatted to " + res5
);
144 status
= U_ZERO_ERROR
;
145 res6
= cur_fr
->format(fL
, res6
, pos4
, status
);
146 if(U_FAILURE(status
)) {
147 errln("ERROR: format(Formattable [long]) failed");
149 logln((UnicodeString
) "" + fL
.getLong() + " formatted to " + res6
);
152 // ======= Test parse()
155 logln("Testing parse()");
157 double d
= -10456.0037;
158 UnicodeString
text("-10,456.0037");
159 Formattable result1
, result2
, result3
;
160 ParsePosition
pos(0), pos01(0);
161 fr
->parseObject(text
, result1
, pos
);
162 if(result1
.getType() != Formattable::kDouble
&& result1
.getDouble() != d
) {
163 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
165 logln(text
+ " parsed into " + (int32_t) result1
.getDouble());
167 fr
->parse(text
, result2
, pos01
);
168 if(result2
.getType() != Formattable::kDouble
&& result2
.getDouble() != d
) {
169 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
171 logln(text
+ " parsed into " + (int32_t) result2
.getDouble());
173 status
= U_ZERO_ERROR
;
174 fr
->parse(text
, result3
, status
);
175 if(U_FAILURE(status
)) {
176 errln("ERROR: parse() failed");
178 if(result3
.getType() != Formattable::kDouble
&& result3
.getDouble() != d
) {
179 errln("ERROR: Roundtrip failed (via parse()) for " + text
);
181 logln(text
+ " parsed into " + (int32_t) result3
.getDouble());
184 // ======= Test getters and setters
185 if (fr
!= NULL
&& def
!= NULL
)
187 logln("Testing getters and setters");
190 const Locale
*locales
= NumberFormat::getAvailableLocales(count
);
191 logln((UnicodeString
) "Got " + count
+ " locales" );
192 for(int32_t i
= 0; i
< count
; i
++) {
193 UnicodeString
name(locales
[i
].getName(),"");
197 fr
->setParseIntegerOnly( def
->isParseIntegerOnly() );
198 if(fr
->isParseIntegerOnly() != def
->isParseIntegerOnly() ) {
199 errln("ERROR: setParseIntegerOnly() failed");
202 fr
->setGroupingUsed( def
->isGroupingUsed() );
203 if(fr
->isGroupingUsed() != def
->isGroupingUsed() ) {
204 errln("ERROR: setGroupingUsed() failed");
207 fr
->setMaximumIntegerDigits( def
->getMaximumIntegerDigits() );
208 if(fr
->getMaximumIntegerDigits() != def
->getMaximumIntegerDigits() ) {
209 errln("ERROR: setMaximumIntegerDigits() failed");
212 fr
->setMinimumIntegerDigits( def
->getMinimumIntegerDigits() );
213 if(fr
->getMinimumIntegerDigits() != def
->getMinimumIntegerDigits() ) {
214 errln("ERROR: setMinimumIntegerDigits() failed");
217 fr
->setMaximumFractionDigits( def
->getMaximumFractionDigits() );
218 if(fr
->getMaximumFractionDigits() != def
->getMaximumFractionDigits() ) {
219 errln("ERROR: setMaximumFractionDigits() failed");
222 fr
->setMinimumFractionDigits( def
->getMinimumFractionDigits() );
223 if(fr
->getMinimumFractionDigits() != def
->getMinimumFractionDigits() ) {
224 errln("ERROR: setMinimumFractionDigits() failed");
228 // ======= Test getStaticClassID()
230 logln("Testing getStaticClassID()");
232 status
= U_ZERO_ERROR
;
233 NumberFormat
*test
= new DecimalFormat(status
);
234 if(U_FAILURE(status
)) {
235 errcheckln(status
, "ERROR: Couldn't create a NumberFormat - %s", u_errorName(status
));
238 if(test
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
239 errln("ERROR: getDynamicClassID() didn't return the expected value");
251 #if !UCONFIG_NO_SERVICE
253 #define SRC_LOC Locale::getFrance()
254 #define SWAP_LOC Locale::getUS()
256 class NFTestFactory
: public SimpleNumberFormatFactory
{
257 NumberFormat
* currencyStyle
;
261 : SimpleNumberFormatFactory(SRC_LOC
, TRUE
)
263 UErrorCode status
= U_ZERO_ERROR
;
264 currencyStyle
= NumberFormat::createInstance(SWAP_LOC
, status
);
267 virtual ~NFTestFactory()
269 delete currencyStyle
;
272 virtual NumberFormat
* createFormat(const Locale
& /* loc */, UNumberFormatStyle formatType
)
274 if (formatType
== UNUM_CURRENCY
) {
275 return (NumberFormat
*)currencyStyle
->clone();
280 virtual inline UClassID
getDynamicClassID() const
282 return (UClassID
)&gID
;
285 static inline UClassID
getStaticClassID()
287 return (UClassID
)&gID
;
294 char NFTestFactory::gID
= 0;
298 IntlTestNumberFormatAPI::testRegistration()
300 #if !UCONFIG_NO_SERVICE
301 UErrorCode status
= U_ZERO_ERROR
;
303 LocalPointer
<NumberFormat
> f0(NumberFormat::createInstance(SWAP_LOC
, status
));
304 LocalPointer
<NumberFormat
> f1(NumberFormat::createInstance(SRC_LOC
, status
));
305 LocalPointer
<NumberFormat
> f2(NumberFormat::createCurrencyInstance(SRC_LOC
, status
));
306 URegistryKey key
= NumberFormat::registerFactory(new NFTestFactory(), status
);
307 LocalPointer
<NumberFormat
> f3(NumberFormat::createCurrencyInstance(SRC_LOC
, status
));
308 LocalPointer
<NumberFormat
> f3a(NumberFormat::createCurrencyInstance(SRC_LOC
, status
));
309 LocalPointer
<NumberFormat
> f4(NumberFormat::createInstance(SRC_LOC
, status
));
311 StringEnumeration
* locs
= NumberFormat::getAvailableLocales();
313 LocalUNumberFormatPointer
uf3(unum_open(UNUM_CURRENCY
, NULL
, 0, SRC_LOC
.getName(), NULL
, &status
));
314 LocalUNumberFormatPointer
uf4(unum_open(UNUM_DEFAULT
, NULL
, 0, SRC_LOC
.getName(), NULL
, &status
));
316 const UnicodeString
* res
;
317 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
318 logln(*res
); // service is still in synch
321 NumberFormat::unregister(key
, status
); // restore for other tests
322 LocalPointer
<NumberFormat
> f5(NumberFormat::createCurrencyInstance(SRC_LOC
, status
));
323 LocalUNumberFormatPointer
uf5(unum_open(UNUM_CURRENCY
, NULL
, 0, SRC_LOC
.getName(), NULL
, &status
));
325 if (U_FAILURE(status
)) {
326 dataerrln("Error creating instnaces.");
330 UnicodeString res0
, res1
, res2
, res3
, res4
, res5
;
342 unum_formatDouble(uf3
.getAlias(), n
, ures3
, 50, NULL
, &status
);
343 unum_formatDouble(uf4
.getAlias(), n
, ures4
, 50, NULL
, &status
);
344 unum_formatDouble(uf5
.getAlias(), n
, ures5
, 50, NULL
, &status
);
346 logln((UnicodeString
)"f0 swap int: " + res0
);
347 logln((UnicodeString
)"f1 src int: " + res1
);
348 logln((UnicodeString
)"f2 src cur: " + res2
);
349 logln((UnicodeString
)"f3 reg cur: " + res3
);
350 logln((UnicodeString
)"f4 reg int: " + res4
);
351 logln((UnicodeString
)"f5 unreg cur: " + res5
);
352 log("uf3 reg cur: ");
354 log("uf4 reg int: ");
356 log("uf5 ureg cur: ");
359 if (f3
.getAlias() == f3a
.getAlias()) {
360 errln("did not get new instance from service");
364 errln("registered service did not match");
367 errln("registered service did not inherit");
370 errln("unregistered service did not match original");
374 errln("registered service did not match / unum");
377 errln("registered service did not inherit / unum");
380 errln("unregistered service did not match original / unum");
384 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
385 errln(*res
); // service should be out of synch
388 locs
->reset(status
); // now in synch again, we hope
389 for (res
= locs
->snext(status
); res
; res
= locs
->snext(status
)) {
398 #endif /* #if !UCONFIG_NO_FORMATTING */