]>
Commit | Line | Data |
---|---|---|
374ca955 | 1 | /*********************************************************************** |
b75a7d8f | 2 | * COPYRIGHT: |
374ca955 A |
3 | * Copyright (c) 1997-2004, International Business Machines Corporation |
4 | * and others. All Rights Reserved. | |
5 | ***********************************************************************/ | |
b75a7d8f A |
6 | |
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
11 | #include "nmfmapts.h" | |
12 | ||
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" | |
18 | ||
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. | |
22 | ||
23 | void IntlTestNumberFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
24 | { | |
25 | if (exec) logln("TestSuite NumberFormatAPI"); | |
26 | switch (index) { | |
27 | case 0: name = "NumberFormat API test"; | |
28 | if (exec) { | |
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"); | |
34 | } | |
35 | testAPI(/* par */); | |
36 | } | |
37 | break; | |
38 | case 1: name = "NumberFormatRegistration"; | |
39 | if (exec) { | |
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"); | |
45 | } | |
46 | testRegistration(); | |
47 | } | |
48 | break; | |
49 | default: name = ""; break; | |
50 | } | |
51 | } | |
52 | ||
53 | /** | |
54 | * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of | |
55 | * NumberFormat. | |
56 | */ | |
57 | void IntlTestNumberFormatAPI::testAPI(/* char* par */) | |
58 | { | |
59 | UErrorCode status = U_ZERO_ERROR; | |
60 | ||
61 | // ======= Test constructors | |
62 | ||
63 | logln("Testing NumberFormat constructors"); | |
64 | ||
65 | NumberFormat *def = NumberFormat::createInstance(status); | |
66 | if(U_FAILURE(status)) { | |
67 | errln("ERROR: Could not create NumberFormat (default)"); | |
68 | } | |
69 | ||
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)"); | |
74 | } | |
75 | ||
76 | NumberFormat *cur = NumberFormat::createCurrencyInstance(status); | |
77 | if(U_FAILURE(status)) { | |
78 | errln("ERROR: Could not create NumberFormat (currency, default)"); | |
79 | } | |
80 | ||
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)"); | |
85 | } | |
86 | ||
87 | NumberFormat *per = NumberFormat::createPercentInstance(status); | |
88 | if(U_FAILURE(status)) { | |
89 | errln("ERROR: Could not create NumberFormat (percent, default)"); | |
90 | } | |
91 | ||
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)"); | |
96 | } | |
97 | ||
98 | // ======= Test equality | |
99 | ||
100 | logln("Testing equality operator"); | |
101 | ||
102 | if( *per_fr == *cur_fr || ! ( *per_fr != *cur_fr) ) { | |
103 | errln("ERROR: == failed"); | |
104 | } | |
105 | ||
106 | // ======= Test various format() methods | |
107 | ||
108 | logln("Testing various format() methods"); | |
109 | ||
110 | double d = -10456.0037; | |
111 | int32_t l = 100000000; | |
112 | Formattable fD(d); | |
113 | Formattable fL(l); | |
114 | ||
115 | UnicodeString res1, res2, res3, res4, res5, res6; | |
116 | FieldPosition pos1(0), pos2(0), pos3(0), pos4(0); | |
117 | ||
118 | res1 = cur_fr->format(d, res1); | |
119 | logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1); | |
120 | ||
121 | res2 = cur_fr->format(l, res2); | |
122 | logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2); | |
123 | ||
124 | res3 = cur_fr->format(d, res3, pos1); | |
125 | logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res3); | |
126 | ||
127 | res4 = cur_fr->format(l, res4, pos2); | |
128 | logln((UnicodeString) "" + (int32_t) l + " formatted to " + res4); | |
129 | ||
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"); | |
134 | } | |
135 | logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res5); | |
136 | ||
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"); | |
141 | } | |
142 | logln((UnicodeString) "" + fL.getLong() + " formatted to " + res6); | |
143 | ||
144 | ||
145 | // ======= Test parse() | |
146 | ||
147 | logln("Testing parse()"); | |
148 | ||
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); | |
155 | } | |
156 | logln(text + " parsed into " + (int32_t) result1.getDouble()); | |
157 | ||
158 | fr->parse(text, result2, pos01); | |
159 | if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) { | |
160 | errln("ERROR: Roundtrip failed (via parse()) for " + text); | |
161 | } | |
162 | logln(text + " parsed into " + (int32_t) result2.getDouble()); | |
163 | ||
164 | status = U_ZERO_ERROR; | |
165 | fr->parse(text, result3, status); | |
166 | if(U_FAILURE(status)) { | |
167 | errln("ERROR: parse() failed"); | |
168 | } | |
169 | if(result3.getType() != Formattable::kDouble && result3.getDouble() != d) { | |
170 | errln("ERROR: Roundtrip failed (via parse()) for " + text); | |
171 | } | |
172 | logln(text + " parsed into " + (int32_t) result3.getDouble()); | |
173 | ||
174 | ||
175 | // ======= Test getters and setters | |
176 | ||
177 | logln("Testing getters and setters"); | |
178 | ||
179 | int32_t count = 0; | |
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(),""); | |
184 | logln(name); | |
185 | } | |
186 | ||
187 | fr->setParseIntegerOnly( def->isParseIntegerOnly() ); | |
188 | if(fr->isParseIntegerOnly() != def->isParseIntegerOnly() ) { | |
189 | errln("ERROR: setParseIntegerOnly() failed"); | |
190 | } | |
191 | ||
192 | fr->setGroupingUsed( def->isGroupingUsed() ); | |
193 | if(fr->isGroupingUsed() != def->isGroupingUsed() ) { | |
194 | errln("ERROR: setGroupingUsed() failed"); | |
195 | } | |
196 | ||
197 | fr->setMaximumIntegerDigits( def->getMaximumIntegerDigits() ); | |
198 | if(fr->getMaximumIntegerDigits() != def->getMaximumIntegerDigits() ) { | |
199 | errln("ERROR: setMaximumIntegerDigits() failed"); | |
200 | } | |
201 | ||
202 | fr->setMinimumIntegerDigits( def->getMinimumIntegerDigits() ); | |
203 | if(fr->getMinimumIntegerDigits() != def->getMinimumIntegerDigits() ) { | |
204 | errln("ERROR: setMinimumIntegerDigits() failed"); | |
205 | } | |
206 | ||
207 | fr->setMaximumFractionDigits( def->getMaximumFractionDigits() ); | |
208 | if(fr->getMaximumFractionDigits() != def->getMaximumFractionDigits() ) { | |
209 | errln("ERROR: setMaximumFractionDigits() failed"); | |
210 | } | |
211 | ||
212 | fr->setMinimumFractionDigits( def->getMinimumFractionDigits() ); | |
213 | if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) { | |
214 | errln("ERROR: setMinimumFractionDigits() failed"); | |
215 | } | |
216 | ||
217 | ||
218 | // ======= Test getStaticClassID() | |
219 | ||
220 | logln("Testing getStaticClassID()"); | |
221 | ||
222 | status = U_ZERO_ERROR; | |
223 | NumberFormat *test = new DecimalFormat(status); | |
224 | if(U_FAILURE(status)) { | |
225 | errln("ERROR: Couldn't create a NumberFormat"); | |
226 | } | |
227 | ||
228 | if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) { | |
229 | errln("ERROR: getDynamicClassID() didn't return the expected value"); | |
230 | } | |
231 | ||
232 | delete test; | |
233 | delete def; | |
234 | delete fr; | |
235 | delete cur; | |
236 | delete cur_fr; | |
237 | delete per; | |
238 | delete per_fr; | |
239 | } | |
240 | ||
374ca955 A |
241 | #if !UCONFIG_NO_SERVICE |
242 | ||
b75a7d8f A |
243 | #define SRC_LOC Locale::getFrance() |
244 | #define SWAP_LOC Locale::getUS() | |
245 | ||
246 | class NFTestFactory : public SimpleNumberFormatFactory { | |
374ca955 | 247 | NumberFormat* currencyStyle; |
b75a7d8f A |
248 | |
249 | public: | |
374ca955 A |
250 | NFTestFactory() |
251 | : SimpleNumberFormatFactory(SRC_LOC, TRUE) | |
252 | { | |
253 | UErrorCode status = U_ZERO_ERROR; | |
254 | currencyStyle = NumberFormat::createInstance(SWAP_LOC, status); | |
255 | } | |
256 | ||
257 | virtual ~NFTestFactory() | |
258 | { | |
259 | delete currencyStyle; | |
260 | } | |
261 | ||
262 | virtual NumberFormat* createFormat(const Locale& /* loc */, UNumberFormatStyle formatType) | |
263 | { | |
264 | if (formatType == UNUM_CURRENCY) { | |
265 | return (NumberFormat*)currencyStyle->clone(); | |
266 | } | |
267 | return NULL; | |
268 | } | |
b75a7d8f A |
269 | |
270 | virtual inline UClassID getDynamicClassID() const | |
271 | { | |
374ca955 | 272 | return (UClassID)&gID; |
b75a7d8f A |
273 | } |
274 | ||
275 | static inline UClassID getStaticClassID() | |
276 | { | |
374ca955 | 277 | return (UClassID)&gID; |
b75a7d8f A |
278 | } |
279 | ||
280 | private: | |
281 | static char gID; | |
282 | }; | |
283 | ||
284 | char NFTestFactory::gID = 0; | |
374ca955 | 285 | #endif |
b75a7d8f A |
286 | |
287 | void | |
288 | IntlTestNumberFormatAPI::testRegistration() | |
289 | { | |
374ca955 A |
290 | #if !UCONFIG_NO_SERVICE |
291 | UErrorCode status = U_ZERO_ERROR; | |
292 | ||
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); | |
300 | ||
301 | StringEnumeration* locs = NumberFormat::getAvailableLocales(); | |
302 | ||
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); | |
b75a7d8f A |
305 | |
306 | const UnicodeString* res; | |
374ca955 A |
307 | for (res = locs->snext(status); res; res = locs->snext(status)) { |
308 | logln(*res); // service is still in synch | |
309 | } | |
310 | ||
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); | |
314 | ||
315 | float n = 1234.567f; | |
316 | UnicodeString res0, res1, res2, res3, res4, res5; | |
317 | UChar ures3[50]; | |
318 | UChar ures4[50]; | |
319 | UChar ures5[50]; | |
b75a7d8f A |
320 | |
321 | f0->format(n, res0); | |
322 | f1->format(n, res1); | |
323 | f2->format(n, res2); | |
324 | f3->format(n, res3); | |
325 | f4->format(n, res4); | |
326 | f5->format(n, res5); | |
327 | ||
374ca955 A |
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); | |
331 | ||
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: "); | |
339 | logln(ures3); | |
340 | log("uf4 reg int: "); | |
341 | logln(ures4); | |
342 | log("uf5 ureg cur: "); | |
343 | logln(ures5); | |
344 | ||
345 | if (f3 == f3a) { | |
346 | errln("did not get new instance from service"); | |
347 | } else { | |
348 | delete f3a; | |
349 | } | |
350 | if (res3 != res0) { | |
351 | errln("registered service did not match"); | |
352 | } | |
353 | if (res4 != res1) { | |
354 | errln("registered service did not inherit"); | |
355 | } | |
356 | if (res5 != res2) { | |
357 | errln("unregistered service did not match original"); | |
358 | } | |
359 | ||
360 | if (res0 != ures3) { | |
361 | errln("registered service did not match / unum"); | |
362 | } | |
363 | if (res1 != ures4) { | |
364 | errln("registered service did not inherit / unum"); | |
365 | } | |
366 | if (res2 != ures5) { | |
367 | errln("unregistered service did not match original / unum"); | |
368 | } | |
369 | ||
370 | unum_close(uf5); | |
371 | delete f5; | |
372 | unum_close(uf4); | |
373 | unum_close(uf3); | |
374 | delete f4; | |
375 | delete f3; | |
376 | delete f2; | |
377 | delete f1; | |
378 | delete f0; | |
379 | ||
380 | for (res = locs->snext(status); res; res = locs->snext(status)) { | |
381 | errln(*res); // service should be out of synch | |
382 | } | |
383 | ||
384 | locs->reset(status); // now in synch again, we hope | |
385 | for (res = locs->snext(status); res; res = locs->snext(status)) { | |
386 | logln(*res); | |
387 | } | |
388 | ||
389 | delete locs; | |
390 | #endif | |
b75a7d8f A |
391 | } |
392 | ||
393 | ||
394 | #endif /* #if !UCONFIG_NO_FORMATTING */ |