~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/test/cintltst/cnumtst.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

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