]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2001, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | /******************************************************************************** | |
7 | * | |
8 | * File CNUMTST.C | |
9 | * | |
10 | * Madhu Katragadda Creation | |
11 | * | |
12 | * Modification History: | |
13 | * | |
14 | * Date Name Description | |
15 | * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes | |
16 | * 07/15/99 helena Ported to HPUX 10/11 CC. | |
17 | ********************************************************************************* | |
18 | */ | |
19 | ||
20 | /* C API TEST FOR NUMBER FORMAT */ | |
21 | ||
22 | #include "unicode/utypes.h" | |
23 | ||
24 | #if !UCONFIG_NO_FORMATTING | |
25 | ||
26 | #include "unicode/uloc.h" | |
27 | #include "unicode/unum.h" | |
28 | #include "unicode/ustring.h" | |
29 | #include "cintltst.h" | |
30 | #include "cnumtst.h" | |
31 | #include "cmemory.h" | |
32 | ||
33 | #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) | |
34 | ||
35 | void addNumForTest(TestNode** root); | |
36 | ||
37 | void addNumForTest(TestNode** root) | |
38 | { | |
39 | addTest(root, &TestNumberFormat, "tsformat/cnumtst/TestNumberFormat"); | |
40 | addTest(root, &TestNumberFormatPadding, "tsformat/cnumtst/TestNumberFormatPadding"); | |
41 | } | |
42 | ||
43 | /** copy src to dst with unicode-escapes for values < 0x20 and > 0x7e, null terminate if possible */ | |
44 | static int32_t ustrToAstr(const UChar* src, int32_t srcLength, char* dst, int32_t dstLength) { | |
45 | static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
46 | ||
47 | char *p = dst; | |
48 | const char *e = p + dstLength; | |
49 | if (srcLength < 0) { | |
50 | const UChar* s = src; | |
51 | while (*s) { | |
52 | ++s; | |
53 | } | |
54 | srcLength = (int32_t)(s - src); | |
55 | } | |
56 | while (p < e && --srcLength >= 0) { | |
57 | UChar c = *src++; | |
58 | if (c == 0xd || c == 0xa || c == 0x9 || (c>= 0x20 && c <= 0x7e)) { | |
59 | *p++ = (char) c & 0x7f; | |
60 | } else if (e - p >= 6) { | |
61 | *p++ = '\\'; | |
62 | *p++ = 'u'; | |
63 | *p++ = hex[(c >> 12) & 0xf]; | |
64 | *p++ = hex[(c >> 8) & 0xf]; | |
65 | *p++ = hex[(c >> 4) & 0xf]; | |
66 | *p++ = hex[c & 0xf]; | |
67 | } else { | |
68 | break; | |
69 | } | |
70 | } | |
71 | if (p < e) { | |
72 | *p = 0; | |
73 | } | |
74 | return (int32_t)(p - dst); | |
75 | } | |
76 | ||
77 | /* test Number Format API */ | |
78 | static void TestNumberFormat() | |
79 | { | |
80 | UChar *result=NULL; | |
81 | UChar temp1[512]; | |
82 | UChar temp2[512]; | |
83 | ||
84 | UChar temp[5]; | |
85 | ||
86 | UChar prefix[5]; | |
87 | UChar suffix[5]; | |
88 | UChar symbol[20]; | |
89 | int32_t resultlength; | |
90 | int32_t resultlengthneeded; | |
91 | int32_t parsepos; | |
92 | double d1; | |
93 | int32_t l1; | |
94 | double d = -10456.37; | |
95 | int32_t l = 100000000; | |
96 | UFieldPosition pos1; | |
97 | UFieldPosition pos2; | |
98 | int32_t numlocales; | |
99 | int32_t i; | |
100 | ||
101 | UNumberFormatAttribute attr; | |
102 | UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; | |
103 | int32_t newvalue; | |
104 | UErrorCode status=U_ZERO_ERROR; | |
105 | UNumberFormatStyle style= UNUM_DEFAULT; | |
106 | UNumberFormat *pattern; | |
107 | UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, | |
108 | *cur_frpattern, *myclone, *spellout_def; | |
109 | ||
110 | /* Testing unum_open() with various Numberformat styles and locales*/ | |
111 | status = U_ZERO_ERROR; | |
112 | log_verbose("Testing unum_open() with default style and locale\n"); | |
113 | def=unum_open(style, NULL,0,NULL, NULL,&status); | |
114 | if(U_FAILURE(status)) | |
115 | log_err("Error in creating NumberFormat default using unum_open(): %s\n", myErrorName(status)); | |
116 | ||
117 | log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n"); | |
118 | fr=unum_open(style,NULL,0, "fr_FR",NULL, &status); | |
119 | if(U_FAILURE(status)) | |
120 | log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status)); | |
121 | ||
122 | log_verbose("\nTesting unum_open(currency,NULL,status)\n"); | |
123 | style=UNUM_CURRENCY; | |
124 | /* Can't hardcode the result to assume the default locale is "en_US". */ | |
125 | cur_def=unum_open(style, NULL,0,"en_US", NULL, &status); | |
126 | if(U_FAILURE(status)) | |
127 | log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n", | |
128 | myErrorName(status) ); | |
129 | ||
130 | log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n"); | |
131 | cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status); | |
132 | if(U_FAILURE(status)) | |
133 | log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n", | |
134 | myErrorName(status)); | |
135 | ||
136 | log_verbose("\nTesting unum_open(percent, NULL, status)\n"); | |
137 | style=UNUM_PERCENT; | |
138 | per_def=unum_open(style,NULL,0, NULL,NULL, &status); | |
139 | if(U_FAILURE(status)) | |
140 | log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status)); | |
141 | ||
142 | log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n"); | |
143 | per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status); | |
144 | if(U_FAILURE(status)) | |
145 | log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status)); | |
146 | ||
147 | log_verbose("\nTesting unum_open(spellout, NULL, status)"); | |
148 | style=UNUM_SPELLOUT; | |
149 | spellout_def=unum_open(style, NULL, 0, "en_US", NULL, &status); | |
150 | if(U_FAILURE(status)) | |
151 | log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status)); | |
152 | ||
153 | /* Testing unum_clone(..) */ | |
154 | log_verbose("\nTesting unum_clone(fmt, status)"); | |
155 | status = U_ZERO_ERROR; | |
156 | myclone = unum_clone(def,&status); | |
157 | if(U_FAILURE(status)) | |
158 | log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status)); | |
159 | else | |
160 | { | |
161 | log_verbose("unum_clone() successful\n"); | |
162 | } | |
163 | ||
164 | /*Testing unum_getAvailable() and unum_countAvailable()*/ | |
165 | log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); | |
166 | numlocales=unum_countAvailable(); | |
167 | if(numlocales < 0) | |
168 | log_err("error in countAvailable"); | |
169 | else{ | |
170 | log_verbose("unum_countAvialable() successful\n"); | |
171 | log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales); | |
172 | } | |
173 | for(i=0;i<numlocales;i++) | |
174 | { | |
175 | log_verbose("%s\n", unum_getAvailable(i)); | |
176 | if (unum_getAvailable(i) == 0) | |
177 | log_err("No locale for which number formatting patterns are applicable\n"); | |
178 | else | |
179 | log_verbose("A locale %s for which number formatting patterns are applicable\n",unum_getAvailable(i)); | |
180 | } | |
181 | ||
182 | ||
183 | /*Testing unum_format() and unum_formatdouble()*/ | |
184 | u_uastrcpy(temp1, "$100,000,000.00"); | |
185 | ||
186 | log_verbose("\nTesting unum_format() \n"); | |
187 | resultlength=0; | |
188 | pos1.field = 0; /* Integer Section */ | |
189 | resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status); | |
190 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
191 | { | |
192 | status=U_ZERO_ERROR; | |
193 | resultlength=resultlengthneeded+1; | |
194 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
195 | /* for (i = 0; i < 100000; i++) */ | |
196 | { | |
197 | unum_format(cur_def, l, result, resultlength, &pos1, &status); | |
198 | } | |
199 | } | |
200 | ||
201 | if(U_FAILURE(status)) | |
202 | { | |
203 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); | |
204 | } | |
205 | if(u_strcmp(result, temp1)==0) | |
206 | log_verbose("Pass: Number formatting using unum_format() successful\n"); | |
207 | else | |
208 | log_err("Fail: Error in number Formatting using unum_format()\n"); | |
209 | if(pos1.beginIndex == 1 && pos1.endIndex == 12) | |
210 | log_verbose("Pass: Complete number formatting using unum_format() successful\n"); | |
211 | else | |
212 | log_err("Fail: Error in complete number Formatting using unum_format()\nGot: b=%d end=%d\nExpected: b=1 end=12\n", | |
213 | pos1.beginIndex, pos1.endIndex); | |
214 | ||
215 | free(result); | |
216 | result = 0; | |
217 | ||
218 | log_verbose("\nTesting unum_formatDouble()\n"); | |
219 | u_uastrcpy(temp1, "($10,456.37)"); | |
220 | resultlength=0; | |
221 | pos2.field = 1; /* Fractional Section */ | |
222 | resultlengthneeded=unum_formatDouble(cur_def, d, NULL, resultlength, &pos2, &status); | |
223 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
224 | { | |
225 | status=U_ZERO_ERROR; | |
226 | resultlength=resultlengthneeded+1; | |
227 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
228 | /* for (i = 0; i < 100000; i++) */ | |
229 | { | |
230 | unum_formatDouble(cur_def, d, result, resultlength, &pos2, &status); | |
231 | } | |
232 | } | |
233 | if(U_FAILURE(status)) | |
234 | { | |
235 | log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status)); | |
236 | } | |
237 | if(u_strcmp(result, temp1)==0) | |
238 | log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n"); | |
239 | else | |
240 | log_err("FAIL: Error in number formatting using unum_formatDouble()\n"); | |
241 | if(pos2.beginIndex == 9 && pos2.endIndex == 11) | |
242 | log_verbose("Pass: Complete number formatting using unum_format() successful\n"); | |
243 | else | |
244 | log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=9 end=11", | |
245 | pos1.beginIndex, pos1.endIndex); | |
246 | ||
247 | ||
248 | /* Testing unum_parse() and unum_parseDouble() */ | |
249 | log_verbose("\nTesting unum_parseDouble()\n"); | |
250 | /* for (i = 0; i < 100000; i++)*/ | |
251 | { | |
252 | parsepos=0; | |
253 | d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status); | |
254 | } | |
255 | if(U_FAILURE(status)) | |
256 | { | |
257 | log_err("parse failed. The error is : %s\n", myErrorName(status)); | |
258 | } | |
259 | ||
260 | if(d1!=d) | |
261 | log_err("Fail: Error in parsing\n"); | |
262 | else | |
263 | log_verbose("Pass: parsing successful\n"); | |
264 | ||
265 | /* performance testing */ | |
266 | u_uastrcpy(temp1, "$462.12345"); | |
267 | resultlength=u_strlen(temp1); | |
268 | /* for (i = 0; i < 100000; i++) */ | |
269 | { | |
270 | parsepos=0; | |
271 | d1=unum_parseDouble(cur_def, temp1, resultlength, &parsepos, &status); | |
272 | } | |
273 | if(U_FAILURE(status)) | |
274 | { | |
275 | log_err("parse failed. The error is : %s\n", myErrorName(status)); | |
276 | } | |
277 | ||
278 | if(d1!=462.12345) | |
279 | log_err("Fail: Error in parsing\n"); | |
280 | else | |
281 | log_verbose("Pass: parsing successful\n"); | |
282 | ||
283 | free(result); | |
284 | ||
285 | u_uastrcpy(temp1, "($10,456.3E1])"); | |
286 | parsepos=0; | |
287 | d1=unum_parseDouble(cur_def, temp1, u_strlen(temp1), &parsepos, &status); | |
288 | if(U_SUCCESS(status)) | |
289 | { | |
290 | log_err("Error in unum_parseDouble(..., %s, ...): %s\n", temp1, myErrorName(status)); | |
291 | } | |
292 | ||
293 | ||
294 | log_verbose("\nTesting unum_format()\n"); | |
295 | status=U_ZERO_ERROR; | |
296 | resultlength=0; | |
297 | parsepos=0; | |
298 | resultlengthneeded=unum_format(per_fr, l, NULL, resultlength, &pos1, &status); | |
299 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
300 | { | |
301 | status=U_ZERO_ERROR; | |
302 | resultlength=resultlengthneeded+1; | |
303 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
304 | /* for (i = 0; i < 100000; i++)*/ | |
305 | { | |
306 | unum_format(per_fr, l, result, resultlength, &pos1, &status); | |
307 | } | |
308 | } | |
309 | if(U_FAILURE(status)) | |
310 | { | |
311 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
312 | } | |
313 | ||
314 | ||
315 | log_verbose("\nTesting unum_parse()\n"); | |
316 | /* for (i = 0; i < 100000; i++) */ | |
317 | { | |
318 | parsepos=0; | |
319 | l1=unum_parse(per_fr, result, u_strlen(result), &parsepos, &status); | |
320 | } | |
321 | if(U_FAILURE(status)) | |
322 | { | |
323 | log_err("parse failed. The error is : %s\n", myErrorName(status)); | |
324 | } | |
325 | ||
326 | if(l1!=l) | |
327 | log_err("Fail: Error in parsing\n"); | |
328 | else | |
329 | log_verbose("Pass: parsing successful\n"); | |
330 | ||
331 | free(result); | |
332 | ||
333 | /* create a number format using unum_openPattern(....)*/ | |
334 | log_verbose("\nTesting unum_openPattern()\n"); | |
335 | u_uastrcpy(temp1, "#,##0.0#;(#,##0.0#)"); | |
336 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status); | |
337 | if(U_FAILURE(status)) | |
338 | { | |
339 | log_err("error in unum_openPattern(): %s\n", myErrorName(status) );; | |
340 | } | |
341 | else | |
342 | log_verbose("Pass: unum_openPattern() works fine\n"); | |
343 | ||
344 | /*test for unum_toPattern()*/ | |
345 | log_verbose("\nTesting unum_toPattern()\n"); | |
346 | resultlength=0; | |
347 | resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status); | |
348 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
349 | { | |
350 | status=U_ZERO_ERROR; | |
351 | resultlength=resultlengthneeded+1; | |
352 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
353 | unum_toPattern(pattern, FALSE, result, resultlength, &status); | |
354 | } | |
355 | if(U_FAILURE(status)) | |
356 | { | |
357 | log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status)); | |
358 | } | |
359 | else | |
360 | { | |
361 | if(u_strcmp(result, temp1)!=0) | |
362 | log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n"); | |
363 | else | |
364 | log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n"); | |
365 | free(result); | |
366 | } | |
367 | ||
368 | /*Testing unum_getSymbols() and unum_setSymbols()*/ | |
369 | log_verbose("\nTesting unum_getSymbols and unum_setSymbols()\n"); | |
370 | /*when we try to change the symbols of french to default we need to apply the pattern as well to fetch correct results */ | |
371 | resultlength=0; | |
372 | resultlengthneeded=unum_toPattern(cur_def, FALSE, NULL, resultlength, &status); | |
373 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
374 | { | |
375 | status=U_ZERO_ERROR; | |
376 | resultlength=resultlengthneeded+1; | |
377 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
378 | unum_toPattern(cur_def, FALSE, result, resultlength, &status); | |
379 | } | |
380 | if(U_FAILURE(status)) | |
381 | { | |
382 | log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status)); | |
383 | } | |
384 | ||
385 | status=U_ZERO_ERROR; | |
386 | cur_frpattern=unum_open(UNUM_IGNORE,result, u_strlen(result), "fr_FR",NULL, &status); | |
387 | if(U_FAILURE(status)) | |
388 | { | |
389 | log_err("error in unum_openPattern(): %s\n", myErrorName(status)); | |
390 | } | |
391 | ||
392 | free(result); | |
393 | ||
394 | /*getting the symbols of cur_def */ | |
395 | /*set the symbols of cur_frpattern to cur_def */ | |
396 | for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) { | |
397 | status=U_ZERO_ERROR; | |
398 | unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status); | |
399 | unum_setSymbol(cur_frpattern, symType, temp1, -1, &status); | |
400 | if(U_FAILURE(status)) | |
401 | { | |
402 | log_err("Error in get/set symbols: %s\n", myErrorName(status)); | |
403 | } | |
404 | } | |
405 | ||
406 | /*format to check the result */ | |
407 | resultlength=0; | |
408 | resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status); | |
409 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
410 | { | |
411 | status=U_ZERO_ERROR; | |
412 | resultlength=resultlengthneeded+1; | |
413 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
414 | unum_format(cur_def, l, result, resultlength, &pos1, &status); | |
415 | } | |
416 | if(U_FAILURE(status)) | |
417 | { | |
418 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
419 | } | |
420 | ||
421 | if(U_FAILURE(status)){ | |
422 | log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status)); | |
423 | } | |
424 | unum_applyPattern(cur_frpattern, FALSE, result, u_strlen(result),NULL,NULL); | |
425 | ||
426 | for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) { | |
427 | status=U_ZERO_ERROR; | |
428 | unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status); | |
429 | unum_getSymbol(cur_frpattern, symType, temp2, sizeof(temp2), &status); | |
430 | if(U_FAILURE(status) || u_strcmp(temp1, temp2) != 0) | |
431 | { | |
432 | log_err("Fail: error in getting symbols\n"); | |
433 | } | |
434 | else | |
435 | log_verbose("Pass: get and set symbols successful\n"); | |
436 | } | |
437 | ||
438 | /*format and check with the previous result */ | |
439 | ||
440 | resultlength=0; | |
441 | resultlengthneeded=unum_format(cur_frpattern, l, NULL, resultlength, &pos1, &status); | |
442 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
443 | { | |
444 | status=U_ZERO_ERROR; | |
445 | resultlength=resultlengthneeded+1; | |
446 | unum_format(cur_frpattern, l, temp1, resultlength, &pos1, &status); | |
447 | } | |
448 | if(U_FAILURE(status)) | |
449 | { | |
450 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
451 | } | |
452 | /* TODO: | |
453 | * This test fails because we have not called unum_applyPattern(). | |
454 | * Currently, such an applyPattern() does not exist on the C API, and | |
455 | * we have jitterbug 411 for it. | |
456 | * Since it is close to the 1.5 release, I (markus) am disabling this test just | |
457 | * for this release (I added the test itself only last week). | |
458 | * For the next release, we need to fix this. | |
459 | * Then, remove the uprv_strcmp("1.5", ...) and this comment, and the include "cstring.h" at the beginning of this file. | |
460 | */ | |
461 | if(u_strcmp(result, temp1) != 0) { | |
462 | log_err("Formatting failed after setting symbols. result=%s temp1=%s\n", result, temp1); | |
463 | } | |
464 | ||
465 | ||
466 | /*----------- */ | |
467 | ||
468 | free(result); | |
469 | ||
470 | /* Testing unum_get/setSymbol() */ | |
471 | for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) { | |
472 | symbol[0] = (UChar)(0x41 + i); | |
473 | symbol[1] = (UChar)(0x61 + i); | |
474 | unum_setSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, 2, &status); | |
475 | if(U_FAILURE(status)) { | |
476 | log_err("Error from unum_setSymbol(%d): %s\n", i, myErrorName(status)); | |
477 | return; | |
478 | } | |
479 | } | |
480 | for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) { | |
481 | resultlength = unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, sizeof(symbol)/U_SIZEOF_UCHAR, &status); | |
482 | if(U_FAILURE(status)) { | |
483 | log_err("Error from unum_getSymbol(%d): %s\n", i, myErrorName(status)); | |
484 | return; | |
485 | } | |
486 | if(resultlength != 2 || symbol[0] != 0x41 + i || symbol[1] != 0x61 + i) { | |
487 | log_err("Failure in unum_getSymbol(%d): got unexpected symbol\n", i); | |
488 | } | |
489 | } | |
490 | /*try getting from a bogus symbol*/ | |
491 | unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, sizeof(symbol)/U_SIZEOF_UCHAR, &status); | |
492 | if(U_SUCCESS(status)){ | |
493 | log_err("Error : Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol"); | |
494 | } | |
495 | if(U_FAILURE(status)){ | |
496 | if(status != U_ILLEGAL_ARGUMENT_ERROR){ | |
497 | log_err("Error: Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol, Got %s\n", myErrorName(status)); | |
498 | } | |
499 | } | |
500 | status=U_ZERO_ERROR; | |
501 | ||
502 | /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/ | |
503 | log_verbose("\nTesting getting and setting text attributes\n"); | |
504 | resultlength=5; | |
505 | unum_getTextAttribute(cur_fr, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status); | |
506 | if(U_FAILURE(status)) | |
507 | { | |
508 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
509 | } | |
510 | unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status); | |
511 | if(U_FAILURE(status)) | |
512 | { | |
513 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
514 | } | |
515 | unum_getTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, suffix, resultlength, &status); | |
516 | if(U_FAILURE(status)) | |
517 | { | |
518 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
519 | } | |
520 | if(u_strcmp(suffix,temp)!=0) | |
521 | log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n"); | |
522 | else | |
523 | log_verbose("Pass: setting and getting suffix works fine\n"); | |
524 | /*set it back to normal */ | |
525 | u_uastrcpy(temp,"$"); | |
526 | unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status); | |
527 | ||
528 | /*checking some more text setter conditions */ | |
529 | u_uastrcpy(prefix, "+"); | |
530 | unum_setTextAttribute(def, UNUM_POSITIVE_PREFIX, prefix, u_strlen(prefix) , &status); | |
531 | if(U_FAILURE(status)) | |
532 | { | |
533 | log_err("error in setting the text attributes : %s\n", myErrorName(status)); | |
534 | } | |
535 | unum_getTextAttribute(def, UNUM_POSITIVE_PREFIX, temp, resultlength, &status); | |
536 | if(U_FAILURE(status)) | |
537 | { | |
538 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
539 | } | |
540 | ||
541 | if(u_strcmp(prefix, temp)!=0) | |
542 | log_err("ERROR: get and setTextAttributes with positive prefix failed\n"); | |
543 | else | |
544 | log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n"); | |
545 | ||
546 | u_uastrcpy(prefix, "+"); | |
547 | unum_setTextAttribute(def, UNUM_NEGATIVE_PREFIX, prefix, u_strlen(prefix), &status); | |
548 | if(U_FAILURE(status)) | |
549 | { | |
550 | log_err("error in setting the text attributes : %s\n", myErrorName(status)); | |
551 | } | |
552 | unum_getTextAttribute(def, UNUM_NEGATIVE_PREFIX, temp, resultlength, &status); | |
553 | if(U_FAILURE(status)) | |
554 | { | |
555 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
556 | } | |
557 | if(u_strcmp(prefix, temp)!=0) | |
558 | log_err("ERROR: get and setTextAttributes with negative prefix failed\n"); | |
559 | else | |
560 | log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n"); | |
561 | ||
562 | u_uastrcpy(suffix, "+"); | |
563 | unum_setTextAttribute(def, UNUM_NEGATIVE_SUFFIX, suffix, u_strlen(suffix) , &status); | |
564 | if(U_FAILURE(status)) | |
565 | { | |
566 | log_err("error in setting the text attributes: %s\n", myErrorName(status)); | |
567 | } | |
568 | ||
569 | unum_getTextAttribute(def, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status); | |
570 | if(U_FAILURE(status)) | |
571 | { | |
572 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
573 | } | |
574 | if(u_strcmp(suffix, temp)!=0) | |
575 | log_err("ERROR: get and setTextAttributes with negative suffix failed\n"); | |
576 | else | |
577 | log_verbose("Pass: get and settextAttributes with negative suffix works fine\n"); | |
578 | ||
579 | u_uastrcpy(suffix, "++"); | |
580 | unum_setTextAttribute(def, UNUM_POSITIVE_SUFFIX, suffix, u_strlen(suffix) , &status); | |
581 | if(U_FAILURE(status)) | |
582 | { | |
583 | log_err("error in setting the text attributes: %s\n", myErrorName(status)); | |
584 | } | |
585 | ||
586 | unum_getTextAttribute(def, UNUM_POSITIVE_SUFFIX, temp, resultlength, &status); | |
587 | if(U_FAILURE(status)) | |
588 | { | |
589 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
590 | } | |
591 | if(u_strcmp(suffix, temp)!=0) | |
592 | log_err("ERROR: get and setTextAttributes with negative suffix failed\n"); | |
593 | else | |
594 | log_verbose("Pass: get and settextAttributes with negative suffix works fine\n"); | |
595 | ||
596 | ||
597 | ||
598 | ||
599 | /*Testing unum_getAttribute and unum_setAttribute() */ | |
600 | log_verbose("\nTesting get and set Attributes\n"); | |
601 | attr=UNUM_GROUPING_SIZE; | |
602 | newvalue=unum_getAttribute(def, attr); | |
603 | newvalue=2; | |
604 | unum_setAttribute(def, attr, newvalue); | |
605 | if(unum_getAttribute(def,attr)!=2) | |
606 | log_err("Fail: error in setting and getting attributes for UNUM_GROUPING_SIZE\n"); | |
607 | else | |
608 | log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n"); | |
609 | ||
610 | attr=UNUM_MULTIPLIER; | |
611 | newvalue=unum_getAttribute(def, attr); | |
612 | newvalue=8; | |
613 | unum_setAttribute(def, attr, newvalue); | |
614 | if(unum_getAttribute(def,attr) != 8) | |
615 | log_err("error in setting and getting attributes for UNUM_MULTIPLIER\n"); | |
616 | else | |
617 | log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n"); | |
618 | ||
619 | /*testing set and get Attributes extensively */ | |
620 | log_verbose("\nTesting get and set attributes extensively\n"); | |
621 | for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_PADDING_POSITION; attr=(UNumberFormatAttribute)((int32_t)attr + 1) ) | |
622 | { | |
623 | newvalue=unum_getAttribute(fr, attr); | |
624 | unum_setAttribute(def, attr, newvalue); | |
625 | if(unum_getAttribute(def,attr)!=unum_getAttribute(fr, attr)) | |
626 | log_err("error in setting and getting attributes\n"); | |
627 | else | |
628 | log_verbose("Pass: attributes set and retrieved successfully\n"); | |
629 | } | |
630 | ||
631 | /*testing spellout format to make sure we can use it successfully.*/ | |
632 | log_verbose("\nTesting spellout format\n"); | |
633 | if (spellout_def) | |
634 | { | |
635 | static const int32_t values[] = { 0, -5, 105, 1005, 105050 }; | |
636 | for (i = 0; i < LENGTH(values); ++i) { | |
637 | UChar buffer[128]; | |
638 | int32_t len; | |
639 | int32_t value = values[i]; | |
640 | status = U_ZERO_ERROR; | |
641 | len = unum_format(spellout_def, value, buffer, LENGTH(buffer), NULL, &status); | |
642 | if(U_FAILURE(status)) { | |
643 | log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); | |
644 | } else { | |
645 | int32_t pp = 0; | |
646 | int32_t parseResult; | |
647 | char logbuf[256]; | |
648 | ustrToAstr(buffer, len, logbuf, LENGTH(logbuf)); | |
649 | log_verbose("formatted %d as '%s', length: %d\n", value, logbuf, len); | |
650 | ||
651 | parseResult = unum_parse(spellout_def, buffer, len, &pp, &status); | |
652 | if (U_FAILURE(status)) { | |
653 | log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); | |
654 | } else if (parseResult != value) { | |
655 | log_err("unum_format result %d != value %d\n", parseResult, value); | |
656 | } | |
657 | } | |
658 | } | |
659 | } | |
660 | else { | |
661 | log_err("Spellout format is unavailable\n"); | |
662 | } | |
663 | ||
664 | /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/ | |
665 | unum_close(def); | |
666 | unum_close(fr); | |
667 | unum_close(cur_def); | |
668 | unum_close(cur_fr); | |
669 | unum_close(per_def); | |
670 | unum_close(per_fr); | |
671 | unum_close(spellout_def); | |
672 | unum_close(pattern); | |
673 | unum_close(cur_frpattern); | |
674 | unum_close(myclone); | |
675 | ||
676 | } | |
677 | ||
678 | static void TestNumberFormatPadding() | |
679 | { | |
680 | UChar *result=NULL; | |
681 | UChar temp1[512]; | |
682 | ||
683 | UErrorCode status=U_ZERO_ERROR; | |
684 | int32_t resultlength; | |
685 | int32_t resultlengthneeded; | |
686 | UNumberFormat *pattern; | |
687 | double d1; | |
688 | double d = -10456.37; | |
689 | UFieldPosition pos1; | |
690 | int32_t parsepos; | |
691 | ||
692 | /* create a number format using unum_openPattern(....)*/ | |
693 | log_verbose("\nTesting unum_openPattern() with padding\n"); | |
694 | u_uastrcpy(temp1, "*#,##0.0#*;(#,##0.0#)"); | |
695 | status=U_ZERO_ERROR; | |
696 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status); | |
697 | if(U_SUCCESS(status)) | |
698 | { | |
699 | log_err("error in unum_openPattern(%s): %s\n", temp1, myErrorName(status) );; | |
700 | } | |
701 | else | |
702 | { | |
703 | unum_close(pattern); | |
704 | } | |
705 | ||
706 | /* u_uastrcpy(temp1, "*x#,###,###,##0.0#;(*x#,###,###,##0.0#)"); */ | |
707 | u_uastrcpy(temp1, "*x#,###,###,##0.0#;*x(###,###,##0.0#)"); | |
708 | status=U_ZERO_ERROR; | |
709 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), "en_US",NULL, &status); | |
710 | if(U_FAILURE(status)) | |
711 | { | |
712 | log_err("error in padding unum_openPattern(%s): %s\n", temp1, myErrorName(status) );; | |
713 | } | |
714 | else { | |
715 | log_verbose("Pass: padding unum_openPattern() works fine\n"); | |
716 | ||
717 | /*test for unum_toPattern()*/ | |
718 | log_verbose("\nTesting padding unum_toPattern()\n"); | |
719 | resultlength=0; | |
720 | resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status); | |
721 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
722 | { | |
723 | status=U_ZERO_ERROR; | |
724 | resultlength=resultlengthneeded+1; | |
725 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
726 | unum_toPattern(pattern, FALSE, result, resultlength, &status); | |
727 | } | |
728 | if(U_FAILURE(status)) | |
729 | { | |
730 | log_err("error in extracting the padding pattern from UNumberFormat: %s\n", myErrorName(status)); | |
731 | } | |
732 | else | |
733 | { | |
734 | if(u_strcmp(result, temp1)!=0) | |
735 | log_err("FAIL: Error in extracting the padding pattern using unum_toPattern()\n"); | |
736 | else | |
737 | log_verbose("Pass: extracted the padding pattern correctly using unum_toPattern()\n"); | |
738 | free(result); | |
739 | } | |
740 | /* u_uastrcpy(temp1, "(xxxxxxx10,456.37)"); */ | |
741 | u_uastrcpy(temp1, "xxxxx(10,456.37)"); | |
742 | resultlength=0; | |
743 | pos1.field = 1; /* Fraction field */ | |
744 | resultlengthneeded=unum_formatDouble(pattern, d, NULL, resultlength, &pos1, &status); | |
745 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
746 | { | |
747 | status=U_ZERO_ERROR; | |
748 | resultlength=resultlengthneeded+1; | |
749 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
750 | unum_formatDouble(pattern, d, result, resultlength, NULL, &status); | |
751 | } | |
752 | if(U_FAILURE(status)) | |
753 | { | |
754 | log_err("Error in formatting using unum_formatDouble(.....) with padding : %s\n", myErrorName(status)); | |
755 | } | |
756 | else | |
757 | { | |
758 | if(u_strcmp(result, temp1)==0) | |
759 | log_verbose("Pass: Number Formatting using unum_formatDouble() padding Successful\n"); | |
760 | else | |
761 | log_err("FAIL: Error in number formatting using unum_formatDouble() with padding\n"); | |
762 | if(pos1.beginIndex == 13 && pos1.endIndex == 15) | |
763 | log_verbose("Pass: Complete number formatting using unum_formatDouble() successful\n"); | |
764 | else | |
765 | log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=13 end=15\n", | |
766 | pos1.beginIndex, pos1.endIndex); | |
767 | ||
768 | ||
769 | /* Testing unum_parse() and unum_parseDouble() */ | |
770 | log_verbose("\nTesting padding unum_parseDouble()\n"); | |
771 | parsepos=0; | |
772 | d1=unum_parseDouble(pattern, result, u_strlen(result), &parsepos, &status); | |
773 | if(U_FAILURE(status)) | |
774 | { | |
775 | log_err("padding parse failed. The error is : %s\n", myErrorName(status)); | |
776 | } | |
777 | ||
778 | if(d1!=d) | |
779 | log_err("Fail: Error in padding parsing\n"); | |
780 | else | |
781 | log_verbose("Pass: padding parsing successful\n"); | |
782 | free(result); | |
783 | } | |
784 | } | |
785 | ||
786 | unum_close(pattern); | |
787 | } | |
788 | ||
789 | #endif /* #if !UCONFIG_NO_FORMATTING */ |