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

« back to all changes in this revision

Viewing changes to source/test/intltest/numrgts.cpp

  • 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
 
#include "numrgts.h"
8
 
 
9
 
#include <float.h> // DBL_MIN, DBL_MAX
10
 
 
11
 
#include "unicode/dcfmtsym.h"
12
 
#include "unicode/decimfmt.h"
13
 
#include "unicode/locid.h"
14
 
#include "unicode/resbund.h"
15
 
#include "unicode/calendar.h"
16
 
#include "unicode/datefmt.h"
17
 
 
18
 
// *****************************************************************************
19
 
// class NumberFormatRegressionTest
20
 
// *****************************************************************************
21
 
 
22
 
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
23
 
 
24
 
void 
25
 
NumberFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
26
 
{
27
 
    // if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
28
 
    switch (index) {
29
 
        CASE(0,Test4075713);
30
 
        CASE(1,Test4074620);
31
 
        CASE(2,Test4088161);
32
 
        CASE(3,Test4087245);
33
 
        CASE(4,Test4087535);
34
 
        CASE(5,Test4088503);
35
 
        CASE(6,Test4066646);
36
 
        CASE(7,Test4059870);
37
 
        CASE(8,Test4083018);
38
 
        CASE(9,Test4071492);
39
 
        CASE(10,Test4086575);
40
 
        CASE(11,Test4068693);
41
 
        CASE(12,Test4069754);
42
 
        CASE(13,Test4087251);
43
 
        CASE(14,Test4090489);
44
 
        CASE(15,Test4090504);
45
 
        CASE(16,Test4095713);
46
 
        CASE(17,Test4092561);
47
 
        CASE(18,Test4092480);
48
 
        CASE(19,Test4087244);
49
 
        CASE(20,Test4070798);
50
 
        CASE(21,Test4071005);
51
 
        CASE(22,Test4071014);
52
 
        CASE(23,Test4071859);
53
 
        CASE(24,Test4093610);
54
 
        CASE(25,Test4098741);
55
 
        CASE(26,Test4074454);
56
 
        CASE(27,Test4099404);
57
 
        CASE(28,Test4101481);
58
 
        CASE(29,Test4052223);
59
 
        CASE(30,Test4061302);
60
 
        CASE(31,Test4062486);
61
 
        CASE(32,Test4108738);
62
 
        CASE(33,Test4106658);
63
 
        CASE(34,Test4106662);
64
 
        CASE(35,Test4114639);
65
 
        CASE(36,Test4106664);
66
 
        CASE(37,Test4106667);
67
 
        CASE(38,Test4110936);
68
 
        CASE(39,Test4122840);
69
 
        CASE(40,Test4125885);
70
 
        CASE(41,Test4134034);
71
 
        CASE(42,Test4134300);
72
 
        CASE(43,Test4140009);
73
 
        CASE(44,Test4141750);
74
 
        CASE(45,Test4145457);
75
 
        CASE(46,Test4147295);
76
 
        CASE(47,Test4147706);
77
 
        CASE(48,Test4162198);
78
 
        CASE(49,Test4162852);
79
 
        CASE(50,Test4167494);
80
 
        CASE(51,Test4170798);
81
 
        CASE(52,Test4176114);
82
 
        CASE(53,Test4179818);
83
 
        CASE(54,Test4212072);
84
 
        CASE(55,Test4216742);
85
 
        CASE(56,Test4217661);
86
 
        CASE(57,Test4161100);
87
 
        CASE(58,Test4243011);
88
 
        CASE(59,Test4243108);
89
 
        CASE(60,TestJ691);
90
 
 
91
 
        default: name = ""; break;
92
 
    }
93
 
}
94
 
 
95
 
UBool 
96
 
NumberFormatRegressionTest::failure(UErrorCode status, const UnicodeString& msg)
97
 
{
98
 
    if(U_FAILURE(status)) {
99
 
        errln(UnicodeString("FAIL: ", "") + msg
100
 
            + UnicodeString(" failed, error ", "") + UnicodeString(u_errorName(status), ""));
101
 
        return TRUE;
102
 
    }
103
 
 
104
 
    return FALSE;
105
 
}
106
 
 
107
 
/**
108
 
 * Convert Java-style strings with \u Unicode escapes into UnicodeString objects
109
 
 */
110
 
inline UnicodeString str(const char *input)
111
 
{
112
 
  return CharsToUnicodeString(input);
113
 
}
114
 
 
115
 
/* @bug 4075713
116
 
 * NumberFormat.equals comparing with null should always return false.
117
 
 */
118
 
// {sfb} kind of silly in C++, just checking for new success
119
 
void NumberFormatRegressionTest::Test4075713(void)
120
 
{
121
 
    //try {
122
 
        MyNumberFormatTest *tmp = new MyNumberFormatTest();
123
 
        if(tmp != NULL)
124
 
            logln("NumberFormat.equals passed");
125
 
    /*} catch (NullPointerException e) {
126
 
        errln("(new MyNumberFormatTest()).equals(null) throws unexpected exception");
127
 
    }*/
128
 
    
129
 
    delete tmp;
130
 
}
131
 
 
132
 
/* @bug 4074620
133
 
 * NumberFormat.equals comparing two obj equal even the setGroupingUsed
134
 
 * flag is different.
135
 
 */
136
 
void NumberFormatRegressionTest::Test4074620(void) 
137
 
{
138
 
 
139
 
    MyNumberFormatTest *nf1 = new MyNumberFormatTest();
140
 
    MyNumberFormatTest *nf2 = new MyNumberFormatTest();
141
 
 
142
 
    nf1->setGroupingUsed(FALSE);
143
 
    nf2->setGroupingUsed(TRUE);
144
 
 
145
 
    if(nf1 == nf2) 
146
 
        errln("Test for bug 4074620 failed");
147
 
    else 
148
 
        logln("Test for bug 4074620 passed.");
149
 
    
150
 
    delete nf1;
151
 
    delete nf2;
152
 
}
153
 
 
154
 
 
155
 
/* @bug 4088161
156
 
 * DecimalFormat.format() incorrectly uses maxFractionDigits setting.
157
 
 */
158
 
 
159
 
void NumberFormatRegressionTest::Test4088161 (void)
160
 
{
161
 
    UErrorCode status = U_ZERO_ERROR;
162
 
    DecimalFormat *df = new DecimalFormat(status);
163
 
    failure(status, "new DecimalFormat");
164
 
    double d = 100;
165
 
    df->setMinimumFractionDigits(0);
166
 
    df->setMaximumFractionDigits(16);
167
 
    UnicodeString sBuf1;
168
 
    FieldPosition fp1(0);
169
 
    logln(UnicodeString("d = ") + d);
170
 
    logln("maxFractionDigits = " + df->getMaximumFractionDigits());
171
 
    
172
 
    logln(" format(d) = '" + df->format(d, sBuf1, fp1) + "'");
173
 
    df->setMaximumFractionDigits(17);
174
 
    UnicodeString sBuf2;
175
 
    FieldPosition fp2(0);
176
 
    logln("maxFractionDigits = " + df->getMaximumFractionDigits());
177
 
    sBuf2 = df->format(d, sBuf2, fp2);
178
 
    if(sBuf2 != "100")
179
 
        errln(" format(d) = '" + sBuf2 + "'");
180
 
 
181
 
    delete df;
182
 
}
183
 
 
184
 
/* @bug 4087245
185
 
 * DecimalFormatSymbols should be cloned in the ctor DecimalFormat.
186
 
 * DecimalFormat(String, DecimalFormatSymbols).
187
 
 */
188
 
void NumberFormatRegressionTest::Test4087245 (void)
189
 
{
190
 
    UErrorCode status = U_ZERO_ERROR;
191
 
    DecimalFormatSymbols *symbols = new DecimalFormatSymbols(status);
192
 
    failure(status, "new DecimalFormatSymbols");
193
 
    // {sfb} One note about this test: if you pass in a pointer
194
 
    // to the symbols, they are adopted and this test will fail,
195
 
    // even though that is the correct behavior.  To test the cloning
196
 
    // of the symbols, it is necessary to pass in a reference to the symbols
197
 
    DecimalFormat *df = new DecimalFormat("#,##0.0", *symbols, status);
198
 
    failure(status, "new DecimalFormat with symbols");
199
 
    int32_t n = 123;
200
 
    UnicodeString buf1;
201
 
    UnicodeString buf2;
202
 
    FieldPosition pos(FieldPosition::DONT_CARE);
203
 
    logln(UnicodeString("format(") + n + ") = " + 
204
 
        df->format(n, buf1, pos));
205
 
    symbols->setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, UnicodeString((UChar)0x70)); // change value of field
206
 
    logln(UnicodeString("format(") + n + ") = " +
207
 
        df->format(n, buf2, pos));
208
 
    if(buf1 != buf2)
209
 
        errln("Test for bug 4087245 failed");
210
 
 
211
 
    delete df;
212
 
    delete symbols;
213
 
}
214
 
 
215
 
/* @bug 4087535
216
 
 * DecimalFormat.format() incorrectly formats 0.0
217
 
 */
218
 
void NumberFormatRegressionTest::Test4087535 (void)
219
 
{
220
 
    UErrorCode status = U_ZERO_ERROR;
221
 
    DecimalFormat *df = new DecimalFormat(status);
222
 
    failure(status, "new DecimalFormat");
223
 
    df->setMinimumIntegerDigits(0);
224
 
 
225
 
    double n = 0;
226
 
    UnicodeString buffer;
227
 
    FieldPosition pos(FieldPosition::DONT_CARE);
228
 
    buffer = df->format(n, buffer, pos);
229
 
    if (buffer.length() == 0)
230
 
        errln(/*n + */": '" + buffer + "'");
231
 
    n = 0.1;
232
 
    buffer = df->format(n, buffer, pos);
233
 
    if (buffer.length() == 0)
234
 
        errln(/*n + */": '" + buffer + "'");
235
 
 
236
 
    delete df;
237
 
}
238
 
 
239
 
/* @bug 4088503
240
 
 * DecimalFormat.format fails when groupingSize is set to 0.
241
 
 */
242
 
// {sfb} how do I tell if this worked? --> FieldPosition doesn't change ??
243
 
void NumberFormatRegressionTest::Test4088503 (void)
244
 
{
245
 
    UErrorCode status = U_ZERO_ERROR;
246
 
    DecimalFormat *df = new DecimalFormat(status);
247
 
    failure(status, "new DecimalFormat");
248
 
    df->setGroupingSize(0);
249
 
    UnicodeString sBuf;
250
 
    FieldPosition fp(FieldPosition::DONT_CARE);
251
 
    //try {
252
 
        logln(df->format((int32_t)123, sBuf, fp));
253
 
        //if(fp == FieldPosition(0))
254
 
        //    errln("Test for bug 4088503 failed.");
255
 
    /*} catch (Exception foo) {
256
 
        errln("Test for bug 4088503 failed.");
257
 
    }*/
258
 
    delete df;
259
 
 
260
 
}
261
 
/* @bug 4066646
262
 
 * NumberFormat.getCurrencyInstance is wrong.
263
 
 */
264
 
void NumberFormatRegressionTest::Test4066646 (void) 
265
 
{
266
 
    assignFloatValue(2.04f);
267
 
    assignFloatValue(2.03f);
268
 
    assignFloatValue(2.02f);
269
 
    assignFloatValue(0.0f);
270
 
}
271
 
 
272
 
float 
273
 
NumberFormatRegressionTest::assignFloatValue(float returnfloat)
274
 
{
275
 
    logln(UnicodeString(" VALUE ") + returnfloat);
276
 
    UErrorCode status = U_ZERO_ERROR;
277
 
    NumberFormat *nfcommon =  NumberFormat::createCurrencyInstance(Locale::US, status);
278
 
    failure(status, "NumberFormat::createCurrencyInstance");
279
 
    nfcommon->setGroupingUsed(FALSE);
280
 
 
281
 
    UnicodeString stringValue;
282
 
    stringValue = nfcommon->format(returnfloat, stringValue);
283
 
    logln(" DISPLAYVALUE " + stringValue);
284
 
    Formattable result;
285
 
    nfcommon->parse(stringValue, result, status);
286
 
    failure(status, "nfcommon->parse");
287
 
    float floatResult = (float) (result.getType() == Formattable::kDouble 
288
 
                                        ? result.getDouble() : result.getLong());
289
 
    if( uprv_fabs(floatResult - returnfloat) > 0.0001)
290
 
    //String stringValue = nfcommon.format(returnfloat).substring(1);
291
 
    //if (Float.valueOf(stringValue).floatValue() != returnfloat)
292
 
        errln(UnicodeString("FAIL: expected ") + returnfloat + ", got " + floatResult + " (" + stringValue+")");
293
 
    
294
 
    delete nfcommon;
295
 
    return returnfloat;
296
 
} // End Of assignFloatValue()
297
 
 
298
 
/* @bug 4059870
299
 
 * DecimalFormat throws exception when parsing "0"
300
 
 */
301
 
void NumberFormatRegressionTest::Test4059870(void) 
302
 
{
303
 
    UErrorCode status = U_ZERO_ERROR;
304
 
    DecimalFormat *format = new DecimalFormat("00", status);
305
 
    failure(status, "new Decimalformat");
306
 
    //try {
307
 
        Formattable result;
308
 
        UnicodeString str;
309
 
        format->parse(UnicodeString("0"), result, status);
310
 
        failure(status, "format->parse");
311
 
        
312
 
    /*} 
313
 
    catch (Exception e) { 
314
 
        errln("Test for bug 4059870 failed : " + e); 
315
 
    }*/
316
 
 
317
 
    delete format;
318
 
}
319
 
/* @bug 4083018
320
 
 * DecimalFormatSymbol.equals should always return false when
321
 
 * comparing with null.
322
 
 */
323
 
// {sfb} this is silly in C++
324
 
void NumberFormatRegressionTest::Test4083018 (void)
325
 
{
326
 
    UErrorCode status = U_ZERO_ERROR;
327
 
    DecimalFormatSymbols *dfs = new DecimalFormatSymbols(status);
328
 
    failure(status, "new DecimalFormatSymbols");
329
 
    //try {
330
 
        if (dfs != NULL)
331
 
            logln("Test Passed!");
332
 
        else
333
 
            errln("Test for bug 4083018 failed");
334
 
    /*} catch (Exception foo) {
335
 
        errln("Test for bug 4083018 failed => Message : " + foo.getMessage());
336
 
    }*/
337
 
 
338
 
    delete dfs;
339
 
}
340
 
 
341
 
/* @bug 4071492
342
 
 * DecimalFormat does not round up correctly.
343
 
 */
344
 
void NumberFormatRegressionTest::Test4071492 (void)
345
 
{
346
 
    double x = 0.00159999;
347
 
    UErrorCode status = U_ZERO_ERROR;
348
 
    NumberFormat *nf = NumberFormat::createInstance(status);
349
 
    failure(status, "NumberFormat::createInstance");
350
 
    nf->setMaximumFractionDigits(4);
351
 
    UnicodeString out;
352
 
    FieldPosition pos(FieldPosition::DONT_CARE);
353
 
    out = nf->format(x, out, pos);
354
 
    logln("0.00159999 formats with 4 fractional digits to " + out);
355
 
    UnicodeString expected("0.0016");
356
 
    if (out != expected)
357
 
        errln("FAIL: Expected " + expected);
358
 
 
359
 
    delete nf;
360
 
}
361
 
 
362
 
/* @bug 4086575
363
 
 * A space as a group separator for localized pattern causes
364
 
 * wrong format.  WorkAround : use non-breaking space.
365
 
 */
366
 
void NumberFormatRegressionTest::Test4086575(void) 
367
 
{
368
 
    UErrorCode status = U_ZERO_ERROR;
369
 
    NumberFormat *nf1 = NumberFormat::createInstance(Locale::getFrance(), status);
370
 
    failure(status, "NumberFormat::createInstance");
371
 
    
372
 
    // C++ workaround to make sure cast works
373
 
    // Wouldn't dynamic_cast<DecimalFormat*> be great?
374
 
    if(nf1->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
375
 
        errln("NumberFormat::createInstance returned incorrect type.");
376
 
        return;
377
 
    }
378
 
 
379
 
    DecimalFormat *nf = (DecimalFormat*) nf1;
380
 
    UnicodeString temp;
381
 
    logln("nf toPattern1: " + nf->toPattern(temp));
382
 
    logln("nf toLocPattern1: " + nf->toLocalizedPattern(temp));
383
 
    
384
 
    // No group separator
385
 
    logln("...applyLocalizedPattern ###,00;(###,00) ");
386
 
    nf->applyLocalizedPattern(UnicodeString("###,00;(###,00)"), status);
387
 
    failure(status, "nf->applyLocalizedPattern");
388
 
    logln("nf toPattern2: " + nf->toPattern(temp));
389
 
    logln("nf toLocPattern2: " + nf->toLocalizedPattern(temp));
390
 
 
391
 
    FieldPosition pos(FieldPosition::DONT_CARE);
392
 
    logln("nf: " + nf->format((int32_t)1234, temp, pos)); // 1234,00
393
 
    logln("nf: " + nf->format((int32_t)-1234, temp, pos)); // (1234,00)
394
 
 
395
 
    // Space as group separator
396
 
 
397
 
    logln("...applyLocalizedPattern # ###,00;(# ###,00) ");
398
 
    // nbsp = \u00a0
399
 
    //nf->applyLocalizedPattern("#\u00a0###,00;(#\u00a0###,00)");
400
 
    UChar patChars[] = {
401
 
             0x23, 0x00a0, 0x23, 0x23, 0x23, 0x2c, 0x30, 0x30, 0x3b, 
402
 
        0x28, 0x23, 0x00a0, 0x23, 0x23, 0x23, 0x2c, 0x30, 0x30, 0x29
403
 
    };
404
 
    UnicodeString pat(patChars, 19, 19);
405
 
    nf->applyLocalizedPattern(pat, status);
406
 
    failure(status, "nf->applyLocalizedPattern");
407
 
    logln("nf toPattern2: " + nf->toPattern(temp));
408
 
    logln("nf toLocPattern2: " + nf->toLocalizedPattern(temp));
409
 
    UnicodeString buffer;
410
 
    buffer = nf->format((int32_t)1234, buffer, pos);
411
 
    //if (buffer != UnicodeString("1\u00a0234,00"))
412
 
    UChar c[] = {
413
 
        0x31, 0x00a0, 0x32, 0x33, 0x34, 0x2c, 0x30, 0x30
414
 
    };
415
 
    UnicodeString cc(c, 8, 8);
416
 
    if (buffer != cc)
417
 
        errln("nf : " + buffer); // Expect 1 234,00
418
 
    
419
 
    buffer.remove();
420
 
    buffer = nf->format((int32_t)-1234, buffer, pos);
421
 
    UChar c1[] = {
422
 
        0x28, 0x31, 0x00a0, 0x32, 0x33, 0x34, 0x2c, 0x30, 0x30, 0x29
423
 
    };
424
 
    UnicodeString cc1(c1, 10, 10);
425
 
    if (buffer != cc1)
426
 
        errln("nf : " + buffer); // Expect (1 234,00)
427
 
 
428
 
    // Erroneously prints:
429
 
    // 1234,00 ,
430
 
    // (1234,00 ,)
431
 
 
432
 
    delete nf1;
433
 
}
434
 
/* @bug 4068693
435
 
 * DecimalFormat.parse returns wrong value
436
 
 */
437
 
// {sfb} slightly converted into a round-trip test, since in C++
438
 
// there is no Double.toString()
439
 
void NumberFormatRegressionTest::Test4068693(void)
440
 
{
441
 
    logln("----- Test Application -----");
442
 
    ParsePosition pos(0);
443
 
    UErrorCode status = U_ZERO_ERROR;
444
 
    DecimalFormat *df = new DecimalFormat(status);
445
 
    failure(status, "new DecimalFormat");
446
 
    Formattable d;
447
 
    //Double d = (Double)df.parse("123.55456", pos=new ParsePosition(0));
448
 
    df->parse(UnicodeString("123.55456"), d, pos);
449
 
    //if (!d.toString().equals("123.55456")) {
450
 
    UnicodeString dstr;
451
 
    df->setMaximumFractionDigits(999);
452
 
    df->setMaximumIntegerDigits(999);
453
 
    FieldPosition fp(FieldPosition::DONT_CARE);
454
 
    dstr = df->format(d.getDouble(), dstr, fp);
455
 
    if (dstr != UnicodeString("123.55456")) {
456
 
        errln(UnicodeString("Result -> ") + d.getDouble());
457
 
    }
458
 
 
459
 
    delete df;
460
 
}
461
 
 
462
 
/* @bug 4069754, 4067878
463
 
 * null pointer thrown when accessing a deserialized DecimalFormat
464
 
 * object.
465
 
 */
466
 
// {sfb} doesn't apply in C++
467
 
void NumberFormatRegressionTest::Test4069754(void)
468
 
{
469
 
/*    try {
470
 
        myformat it = new myformat();
471
 
        logln(it.Now());
472
 
        FileOutputStream ostream = new FileOutputStream("t.tmp");
473
 
        ObjectOutputStream p = new ObjectOutputStream(ostream);
474
 
        p.writeObject(it);
475
 
        ostream.close();
476
 
        logln("Saved ok.");
477
 
 
478
 
        FileInputStream istream = new FileInputStream("t.tmp");
479
 
        ObjectInputStream p2 = new ObjectInputStream(istream);
480
 
        myformat it2 = (myformat)p2.readObject();
481
 
        logln(it2.Now());
482
 
        istream.close();
483
 
        logln("Loaded ok.");
484
 
    } catch (Exception foo) {
485
 
        errln("Test for bug 4069754 or 4057878 failed => Exception: " + foo.getMessage());
486
 
    }
487
 
*/}
488
 
 
489
 
/* @bug 4087251
490
 
 * DecimalFormat.applyPattern(String) allows illegal patterns
491
 
 */
492
 
void NumberFormatRegressionTest::Test4087251 (void)
493
 
{
494
 
    UErrorCode status = U_ZERO_ERROR;
495
 
    DecimalFormat *df = new DecimalFormat(status);
496
 
    failure(status, "new DecimalFormat");
497
 
    //try {
498
 
        df->applyPattern(UnicodeString("#.#.#"), status);
499
 
        if( ! U_FAILURE(status))
500
 
            errln("df->applyPattern with illegal pattern didn't fail");
501
 
        UnicodeString temp;
502
 
        logln("toPattern() returns \"" + df->toPattern(temp) + "\"");
503
 
        //errln("applyPattern(\"#.#.#\") doesn't throw IllegalArgumentException");
504
 
    /*} catch (IllegalArgumentException e) {
505
 
        logln("Caught Illegal Argument Error !");
506
 
    }*/
507
 
    // Second test; added 5/11/98 when reported to fail on 1.2b3
508
 
    //try {
509
 
        df->applyPattern("#0.0#0#0", status);
510
 
        if( ! U_FAILURE(status))
511
 
            errln("df->applyPattern with illegal pattern didn't fail");
512
 
        logln("toPattern() returns \"" + df->toPattern(temp) + "\"");
513
 
        //errln("applyPattern(\"#0.0#0#0\") doesn't throw IllegalArgumentException");
514
 
    /*} catch (IllegalArgumentException e) {
515
 
        logln("Ok - IllegalArgumentException for #0.0#0#0");
516
 
    }*/
517
 
 
518
 
    delete df;
519
 
}
520
 
 
521
 
/* @bug 4090489
522
 
 * DecimalFormat.format() loses precision
523
 
 */
524
 
void NumberFormatRegressionTest::Test4090489 (void)
525
 
{
526
 
// {sfb} sprintf doesn't correctly handle the double, so there is nothing
527
 
// that NumberFormat can do.  For some reason, it does not format the last 1.
528
 
 
529
 
/*    UErrorCode status = U_ZERO_ERROR;
530
 
    DecimalFormat *df = new DecimalFormat(status);
531
 
    failure(status, "new DecimalFormat");
532
 
    df->setMinimumFractionDigits(10);
533
 
    df->setMaximumFractionDigits(999);
534
 
    df->setGroupingUsed(FALSE);
535
 
    double d = 1.000000000000001E7;
536
 
    //BigDecimal bd = new BigDecimal(d);
537
 
    UnicodeString sb;
538
 
    FieldPosition fp(0);
539
 
    logln(UnicodeString("d = ") + d);
540
 
    //logln("BigDecimal.toString():  " + bd.toString());
541
 
    df->format(d, sb, fp);
542
 
    if (sb != "10000000.0000000100") {
543
 
        errln("DecimalFormat.format(): " + sb);
544
 
    }
545
 
*/
546
 
}
547
 
 
548
 
/* @bug 4090504
549
 
 * DecimalFormat.format() loses precision
550
 
 */
551
 
void NumberFormatRegressionTest::Test4090504 (void)
552
 
{
553
 
    double d = 1;
554
 
    logln(UnicodeString("d = ") + d);
555
 
    UErrorCode status = U_ZERO_ERROR;
556
 
    DecimalFormat *df = new DecimalFormat(status);
557
 
    failure(status, "new DecimalFormat");
558
 
    UnicodeString sb;
559
 
    FieldPosition fp(FieldPosition::DONT_CARE);
560
 
    //try {
561
 
        for (int i = 17; i <= 20; i++) {
562
 
            df->setMaximumFractionDigits(i);
563
 
            //sb = new StringBuffer("");
564
 
            fp.setField(0);
565
 
            logln("  getMaximumFractionDigits() = " + i);
566
 
            logln("  formated: " + df->format(d, sb, fp));
567
 
        }
568
 
    /*} catch (Exception foo) {
569
 
        errln("Bug 4090504 regression test failed. Message : " + foo.getMessage());
570
 
    }*/
571
 
 
572
 
    delete df;
573
 
}
574
 
/* @bug 4095713
575
 
 * DecimalFormat.parse(String str, ParsePosition pp) loses precision
576
 
 */
577
 
void NumberFormatRegressionTest::Test4095713 (void)
578
 
{
579
 
    UErrorCode status = U_ZERO_ERROR;
580
 
    DecimalFormat *df = new DecimalFormat(status);
581
 
    failure(status, "new DecimalFOrmat");
582
 
    UnicodeString str("0.1234");
583
 
    double d1 = 0.1234;
584
 
    //Double d1 = new Double(str);
585
 
    //Double d2 = (Double) df.parse(str, new ParsePosition(0));
586
 
    Formattable d2;
587
 
    ParsePosition pp(0);
588
 
    df->parse(str, d2, pp);
589
 
    logln(UnicodeString("") + d1);
590
 
    if (d2.getDouble() != d1)
591
 
        errln(UnicodeString("Bug 4095713 test failed, new double value : ") + d2.getDouble());
592
 
    delete df;
593
 
}
594
 
 
595
 
/* @bug 4092561
596
 
 * DecimalFormat.parse() fails when multiplier is not set to 1
597
 
 */
598
 
// {sfb} not sure what to do with this one
599
 
void NumberFormatRegressionTest::Test4092561 (void)
600
 
{
601
 
    UErrorCode status = U_ZERO_ERROR;
602
 
    DecimalFormat *df = new DecimalFormat(status);
603
 
    failure(status, "new DecimalFormat");
604
 
 
605
 
    // {sfb} going to cheat here and use sprintf ??
606
 
 
607
 
    /*UnicodeString str = Long.toString(Long.MIN_VALUE);
608
 
    logln("Long.MIN_VALUE : " + df.parse(str, new ParsePosition(0)).toString());
609
 
    df.setMultiplier(100);
610
 
    Number num = df.parse(str, new ParsePosition(0));
611
 
    if (num.doubleValue() != -9.223372036854776E16)
612
 
        errln("Bug 4092561 test failed when multiplier is set to not 1.");
613
 
*/
614
 
    delete df;
615
 
}
616
 
 
617
 
/* @bug 4092480
618
 
 * DecimalFormat: Negative format ignored.
619
 
 */
620
 
void NumberFormatRegressionTest::Test4092480 (void)
621
 
{
622
 
    UErrorCode status = U_ZERO_ERROR;
623
 
    DecimalFormat *dfFoo = new DecimalFormat(UnicodeString("000"), status);
624
 
    failure(status, "new DecimalFormat");
625
 
 
626
 
    //try {
627
 
        dfFoo->applyPattern("0000;-000", status);
628
 
        failure(status, "dfFoo->applyPattern");
629
 
        UnicodeString temp;
630
 
        if (dfFoo->toPattern(temp) != UnicodeString("#0000"))
631
 
            errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
632
 
        FieldPosition pos(FieldPosition::DONT_CARE);
633
 
        logln(dfFoo->format((int32_t)42, temp, pos));
634
 
        logln(dfFoo->format((int32_t)-42, temp, pos));
635
 
        dfFoo->applyPattern("000;-000", status);
636
 
        failure(status, "dfFoo->applyPattern");
637
 
        if (dfFoo->toPattern(temp) != UnicodeString("#000"))
638
 
            errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
639
 
        logln(dfFoo->format((int32_t)42,temp, pos));
640
 
        logln(dfFoo->format((int32_t)-42, temp, pos));
641
 
 
642
 
        dfFoo->applyPattern("000;-0000", status);
643
 
        failure(status, "dfFoo->applyPattern");
644
 
        if (dfFoo->toPattern(temp) != UnicodeString("#000"))
645
 
            errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
646
 
        logln(dfFoo->format((int32_t)42, temp, pos));
647
 
        logln(dfFoo->format((int32_t)-42, temp, pos));
648
 
 
649
 
        dfFoo->applyPattern("0000;-000", status);
650
 
        failure(status, "dfFoo->applyPattern");
651
 
        if (dfFoo->toPattern(temp) != UnicodeString("#0000"))
652
 
            errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
653
 
        logln(dfFoo->format((int32_t)42, temp, pos));
654
 
        logln(dfFoo->format((int32_t)-42, temp, pos));
655
 
    /*} catch (Exception foo) {
656
 
        errln("Message " + foo.getMessage());
657
 
    }*/
658
 
 
659
 
    delete dfFoo;
660
 
}
661
 
/* @bug 4087244
662
 
 * NumberFormat.getCurrencyInstance() produces format that uses
663
 
 * decimal separator instead of monetary decimal separator.
664
 
 *
665
 
 * Rewrote this test not to depend on the actual pattern.  Pattern should
666
 
 * never contain the monetary separator!  Decimal separator in pattern is
667
 
 * interpreted as monetary separator if currency symbol is seen!
668
 
 */
669
 
void NumberFormatRegressionTest::Test4087244 (void) {
670
 
    Locale *de = new Locale("pt","PT_PREEURO");
671
 
    UErrorCode status = U_ZERO_ERROR;
672
 
    NumberFormat *nf = NumberFormat::createCurrencyInstance(*de, status);
673
 
    if (nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
674
 
        errln("expected DecimalFormat!");
675
 
        return;
676
 
    }
677
 
    DecimalFormat *df = (DecimalFormat*) nf;
678
 
    const DecimalFormatSymbols *sym = df->getDecimalFormatSymbols();
679
 
    UnicodeString decSep = sym->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
680
 
    UnicodeString monSep = sym->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
681
 
    if (decSep == monSep) {
682
 
        errln("ERROR in test: want decimal sep != monetary sep");
683
 
        return;
684
 
    }
685
 
    df->setMinimumIntegerDigits(1);
686
 
    df->setMinimumFractionDigits(2);
687
 
    UnicodeString str;
688
 
    FieldPosition pos;
689
 
    df->format(1.23, str, pos);
690
 
    UnicodeString monStr("1x23");
691
 
    monStr.replace((int32_t)1, 1, monSep);
692
 
    UnicodeString decStr("1x23");
693
 
    decStr.replace((int32_t)1, 1, decSep);
694
 
    if (str.indexOf(monStr) >= 0 && str.indexOf(decStr) < 0) {
695
 
        logln(UnicodeString("OK: 1.23 -> \"") + str + "\" contains \"" +
696
 
              monStr + "\" and not \"" + decStr + '"');
697
 
    } else {
698
 
        errln(UnicodeString("FAIL: 1.23 -> \"") + str + "\", should contain \"" +
699
 
              monStr +
700
 
              "\" and not \"" + decStr + '"');
701
 
    }
702
 
    delete de;
703
 
    delete nf;
704
 
}
705
 
/* @bug 4070798
706
 
 * Number format data rounding errors for locale FR
707
 
 */
708
 
void NumberFormatRegressionTest::Test4070798 (void) 
709
 
{
710
 
    NumberFormat *formatter;
711
 
    UnicodeString tempString;
712
 
    
713
 
    /* User error :
714
 
    String expectedDefault = "-5\u00a0789,987";
715
 
    String expectedCurrency = "5\u00a0789,98 F";
716
 
    String expectedPercent = "-578\u00a0998%";
717
 
    */
718
 
    UChar chars1 [] = {
719
 
        0x2d, 0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x38, 0x38
720
 
    };
721
 
    UChar chars2 [] = {
722
 
        0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x39, 0x20, 0x46
723
 
    };
724
 
    UChar chars3 [] = {
725
 
        0x2d, 0x35, 0x37, 0x38, 0x00a0, 0x39, 0x39, 0x39, 0x25
726
 
    };
727
 
    UnicodeString expectedDefault(chars1, 10, 10);
728
 
    UnicodeString expectedCurrency(chars2, 10, 10);
729
 
    UnicodeString expectedPercent(chars3, 9, 9);
730
 
 
731
 
    UErrorCode status = U_ZERO_ERROR;
732
 
    formatter = NumberFormat::createInstance(Locale("fr_FR_PREEURO"), status);
733
 
    failure(status, "NumberFormat::createNumberInstance");
734
 
    tempString = formatter->format (-5789.9876, tempString);
735
 
 
736
 
    if (tempString == expectedDefault) {
737
 
        logln ("Bug 4070798 default test passed.");
738
 
    } else {
739
 
        errln(UnicodeString("Failed:") +
740
 
        " Expected " + expectedDefault +
741
 
        " Received " + tempString );
742
 
    }
743
 
    delete formatter;
744
 
 
745
 
    formatter = NumberFormat::createCurrencyInstance(Locale("fr_FR_PREEURO"), status);
746
 
    failure(status, "NumberFormat::createCurrencyInstance");
747
 
    tempString.remove();
748
 
    tempString = formatter->format( 5789.9876, tempString );
749
 
 
750
 
    if (tempString == expectedCurrency) {
751
 
        logln ("Bug 4070798 currency test passed.");
752
 
    } else {
753
 
        errln(UnicodeString("Failed:") +
754
 
        " Expected " + expectedCurrency +
755
 
        " Received " + tempString );
756
 
    }
757
 
    delete formatter;
758
 
 
759
 
    formatter = NumberFormat::createPercentInstance(Locale("fr_FR_PREEURO"), status);
760
 
    failure(status, "NumberFormat::createPercentInstance");
761
 
    tempString.remove();
762
 
    tempString = formatter->format (-5789.9876, tempString);
763
 
 
764
 
    if (tempString == expectedPercent) {
765
 
        logln ("Bug 4070798 percentage test passed.");
766
 
    } else {
767
 
        errln(UnicodeString("Failed:") +
768
 
        " Expected " + expectedPercent +
769
 
        " Received " + tempString );
770
 
    }
771
 
 
772
 
    delete formatter;
773
 
}
774
 
/* @bug 4071005
775
 
 * Data rounding errors for French (Canada) locale
776
 
 */
777
 
void NumberFormatRegressionTest::Test4071005 (void) 
778
 
{
779
 
    NumberFormat *formatter;
780
 
    UnicodeString tempString;
781
 
    /* User error :
782
 
    String expectedDefault = "-5\u00a0789,987";
783
 
    String expectedCurrency = "5\u00a0789,98 $";
784
 
    String expectedPercent = "-578\u00a0998%";
785
 
    */
786
 
    UChar chars1 [] = {
787
 
        0x2d, 0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x38, 0x38
788
 
    };
789
 
    UChar chars2 [] = {
790
 
        0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x39, 0x20, 0x24
791
 
    };
792
 
    UChar chars3 [] = {
793
 
        0x2d, 0x35, 0x37, 0x38, 0x00a0, 0x39, 0x39, 0x39, 0x25
794
 
    };
795
 
    UnicodeString expectedDefault(chars1, 10, 10);
796
 
    UnicodeString expectedCurrency(chars2, 10, 10);
797
 
    UnicodeString expectedPercent(chars3, 9, 9);
798
 
 
799
 
    UErrorCode status = U_ZERO_ERROR;
800
 
    formatter = NumberFormat::createInstance(Locale::getCanadaFrench(), status);
801
 
    failure(status, "NumberFormat::createNumberInstance");
802
 
    tempString = formatter->format (-5789.9876, tempString);
803
 
 
804
 
    if (tempString == expectedDefault) {
805
 
        logln ("Bug 4071005 default test passed.");
806
 
    } else {
807
 
        errln(UnicodeString("Failed:") +
808
 
        " Expected " + expectedDefault +
809
 
        " Received " + tempString );
810
 
    }
811
 
    delete formatter;
812
 
 
813
 
    formatter = NumberFormat::createCurrencyInstance(Locale::getCanadaFrench(), status);
814
 
    failure(status, "NumberFormat::createCurrencyInstance");
815
 
    tempString.remove();
816
 
    tempString = formatter->format( 5789.9876, tempString );
817
 
 
818
 
    if (tempString == expectedCurrency) {
819
 
        logln ("Bug 4071005 currency test assed.");
820
 
    } else {
821
 
        errln(UnicodeString("Failed:") +
822
 
        " Expected " + expectedCurrency +
823
 
        " Received " + tempString );
824
 
    }
825
 
    delete formatter;
826
 
 
827
 
    formatter = NumberFormat::createPercentInstance(Locale::getCanadaFrench(), status);
828
 
    failure(status, "NumberFormat::createPercentInstance");
829
 
    tempString.remove();
830
 
    tempString = formatter->format (-5789.9876, tempString);
831
 
 
832
 
    if (tempString == expectedPercent) {
833
 
        logln ("Bug 4071005 percentage test passed.");
834
 
    } else {
835
 
        errln(UnicodeString("Failed:") +
836
 
        " Expected " + expectedPercent +
837
 
        " Received " + tempString );
838
 
    }
839
 
 
840
 
    delete formatter;
841
 
}
842
 
 
843
 
/* @bug 4071014
844
 
 * Data rounding errors for German (Germany) locale
845
 
 */
846
 
void NumberFormatRegressionTest::Test4071014 (void) 
847
 
{
848
 
    NumberFormat *formatter;
849
 
    UnicodeString tempString;
850
 
    /* user error :
851
 
    String expectedDefault = "-5.789,987";
852
 
    String expectedCurrency = "5.789,98 DM";
853
 
    String expectedPercent = "-578.998%";
854
 
    */
855
 
    UnicodeString expectedDefault("-5.789,988");
856
 
    UnicodeString expectedCurrency("5.789,99 DM");
857
 
    UnicodeString expectedPercent("-578.999%");
858
 
 
859
 
    UErrorCode status = U_ZERO_ERROR;
860
 
    formatter = NumberFormat::createInstance(Locale("de_DE_PREEURO"), status);
861
 
    failure(status, "NumberFormat::createNumberInstance");
862
 
    tempString.remove();
863
 
    tempString = formatter->format (-5789.9876, tempString);
864
 
 
865
 
    if (tempString == expectedDefault) {
866
 
        logln ("Bug 4071014 default test passed.");
867
 
    } else {
868
 
        errln(UnicodeString("Failed:") +
869
 
        " Expected " + expectedDefault +
870
 
        " Received " + tempString );
871
 
    }
872
 
    delete formatter;
873
 
 
874
 
    formatter = NumberFormat::createCurrencyInstance(Locale("de_DE_PREEURO"), status);
875
 
    failure(status, "NumberFormat::createCurrencyInstance");
876
 
    tempString.remove();
877
 
    tempString = formatter->format( 5789.9876, tempString );
878
 
 
879
 
    if (tempString == expectedCurrency) {
880
 
        logln ("Bug 4071014 currency test assed.");
881
 
    } else {
882
 
        errln(UnicodeString("Failed:") +
883
 
        " Expected " + expectedCurrency +
884
 
        " Received " + tempString );
885
 
    }
886
 
    delete formatter;
887
 
 
888
 
    formatter = NumberFormat::createPercentInstance(Locale::getGermany(), status);
889
 
    failure(status, "NumberFormat::createPercentInstance");
890
 
    tempString.remove();
891
 
    tempString = formatter->format (-5789.9876, tempString);
892
 
 
893
 
    if (tempString == expectedPercent) {
894
 
        logln ("Bug 4071014 percentage test passed.");
895
 
    } else {
896
 
        errln(UnicodeString("Failed:") +
897
 
        " Expected " + expectedPercent +
898
 
        " Received " + tempString );
899
 
    }
900
 
 
901
 
    delete formatter;
902
 
}
903
 
/* @bug 4071859
904
 
 * Data rounding errors for Italian locale number formats
905
 
 */
906
 
void NumberFormatRegressionTest::Test4071859 (void) 
907
 
{
908
 
    NumberFormat *formatter;
909
 
    UnicodeString tempString;
910
 
    /* user error :
911
 
    String expectedDefault = "-5.789,987";
912
 
    String expectedCurrency = "-L. 5.789,98";
913
 
    String expectedPercent = "-578.998%";
914
 
    */
915
 
    UnicodeString expectedDefault("-5.789,988");
916
 
    UnicodeString expectedCurrency("-L. 5.790");
917
 
    UnicodeString expectedPercent("-578.999%");
918
 
 
919
 
    UErrorCode status = U_ZERO_ERROR;
920
 
    formatter = NumberFormat::createInstance(Locale("it_IT_PREEURO"), status);
921
 
    failure(status, "NumberFormat::createNumberInstance");
922
 
    tempString = formatter->format (-5789.9876, tempString);
923
 
 
924
 
    if (tempString == expectedDefault) {
925
 
        logln ("Bug 4071859 default test passed.");
926
 
    } else {
927
 
        errln(UnicodeString("Failed:") +
928
 
        " Expected " + expectedDefault +
929
 
        " Received " + tempString );
930
 
    }
931
 
    delete formatter;
932
 
 
933
 
    formatter = NumberFormat::createCurrencyInstance(Locale("it_IT_PREEURO"), status);
934
 
    failure(status, "NumberFormat::createCurrencyInstance");
935
 
    tempString.remove();
936
 
    tempString = formatter->format( -5789.9876, tempString );
937
 
 
938
 
    if (tempString == expectedCurrency) {
939
 
        logln ("Bug 4071859 currency test assed.");
940
 
    } else {
941
 
        errln(UnicodeString("Failed:") +
942
 
        " Expected " + expectedCurrency +
943
 
        " Received " + tempString );
944
 
    }
945
 
    delete formatter;
946
 
 
947
 
    formatter = NumberFormat::createPercentInstance(Locale("it_IT_PREEURO"), status);
948
 
    failure(status, "NumberFormat::createPercentInstance");
949
 
    tempString.remove();
950
 
    tempString = formatter->format (-5789.9876, tempString);
951
 
 
952
 
    if (tempString == expectedPercent) {
953
 
        logln ("Bug 4071859 percentage test passed.");
954
 
    } else {
955
 
        errln(UnicodeString("Failed:") +
956
 
        " Expected " + expectedPercent +
957
 
        " Received " + tempString );
958
 
    }
959
 
 
960
 
    delete formatter;
961
 
}
962
 
/* @bug 4071859
963
 
 * Test rounding for nearest even.
964
 
 */
965
 
void NumberFormatRegressionTest::Test4093610(void)
966
 
{
967
 
    UErrorCode status = U_ZERO_ERROR;
968
 
    DecimalFormat *df = new DecimalFormat("#0.#", status);
969
 
    failure(status, "new DecimalFormat");
970
 
    UnicodeString s("12.4");
971
 
    roundingTest(df, 12.35, s);
972
 
    roundingTest(df, 12.45, s);
973
 
    s = "12.5";
974
 
    roundingTest(df, 12.452,s);
975
 
    s = "12.6";
976
 
    roundingTest(df, 12.55, s);
977
 
    roundingTest(df, 12.65, s);
978
 
    s = "12.7";
979
 
    roundingTest(df, 12.652,s);
980
 
    s = "12.8";
981
 
    roundingTest(df, 12.75, s);
982
 
    roundingTest(df, 12.752,s);
983
 
    roundingTest(df, 12.85, s);
984
 
    s = "12.9";
985
 
    roundingTest(df, 12.852,s);
986
 
    s = "13";
987
 
    roundingTest(df, 12.95, s);
988
 
    roundingTest(df, 12.952,s);
989
 
 
990
 
    delete df;
991
 
}
992
 
 
993
 
void NumberFormatRegressionTest::roundingTest(DecimalFormat *df, double x, UnicodeString& expected)
994
 
{
995
 
    UnicodeString out;
996
 
    FieldPosition pos(FieldPosition::DONT_CARE);
997
 
    out = df->format(x, out, pos);
998
 
    logln(UnicodeString("") + x + " formats with 1 fractional digits to " + out);
999
 
    if (out != expected) 
1000
 
        errln("FAIL: Expected " + expected);
1001
 
}
1002
 
/* @bug 4098741
1003
 
 * Tests the setMaximumFractionDigits limit.
1004
 
 */
1005
 
void NumberFormatRegressionTest::Test4098741(void)
1006
 
{
1007
 
    //try {
1008
 
    UErrorCode status = U_ZERO_ERROR;
1009
 
    NumberFormat *fmt = NumberFormat::createPercentInstance(status);
1010
 
        fmt->setMaximumFractionDigits(20);
1011
 
        UnicodeString temp;
1012
 
        logln(fmt->format(.001, temp));
1013
 
    /*} catch (Exception foo) {
1014
 
        errln("Bug 4098471 failed with exception thrown : " + foo.getMessage());
1015
 
    }*/
1016
 
    delete fmt;
1017
 
}
1018
 
/* @bug 4074454
1019
 
 * Tests illegal pattern exception.
1020
 
 * Fix comment : HShih A31 Part1 will not be fixed and javadoc needs to be updated.
1021
 
 * Part2 has been fixed.
1022
 
 */
1023
 
void NumberFormatRegressionTest::Test4074454(void)
1024
 
{
1025
 
    //try {
1026
 
    UErrorCode status = U_ZERO_ERROR;  
1027
 
    DecimalFormat *fmt = new DecimalFormat("#,#00.00;-#.#", status);
1028
 
    failure(status, "new DecimalFormat");
1029
 
      logln("Inconsistent negative pattern is fine.");
1030
 
        DecimalFormat *newFmt = new DecimalFormat("#,#00.00 p''ieces;-#,#00.00 p''ieces", status);
1031
 
        failure(status, "new DecimalFormat");
1032
 
        UnicodeString tempString;
1033
 
        FieldPosition pos(FieldPosition::DONT_CARE);
1034
 
        tempString = newFmt->format(3456.78, tempString, pos);
1035
 
        if (tempString != UnicodeString("3,456.78 p'ieces"))
1036
 
            errln("Failed!  3456.78 p'ieces expected, but got : " + tempString);
1037
 
    /*} catch (Exception foo) {
1038
 
        errln("An exception was thrown for any inconsistent negative pattern.");
1039
 
    }*/
1040
 
 
1041
 
    delete fmt;
1042
 
    delete newFmt;
1043
 
}
1044
 
/* @bug 4099404
1045
 
 * Tests all different comments.
1046
 
 * Response to some comments :
1047
 
 * [1] DecimalFormat.parse API documentation is more than just one line.
1048
 
 * This is not a reproducable doc error in 116 source code.
1049
 
 * [2] See updated javadoc.
1050
 
 * [3] Fixed.
1051
 
 * [4] NumberFormat.parse(String, ParsePosition) : If parsing fails,
1052
 
 * a null object will be returned.  The unchanged parse position also
1053
 
 * reflects an error.
1054
 
 * NumberFormat.parse(String) : If parsing fails, an ParseException
1055
 
 * will be thrown.
1056
 
 * See updated javadoc for more details.
1057
 
 * [5] See updated javadoc.
1058
 
 * [6] See updated javadoc.
1059
 
 * [7] This is a correct behavior if the DateFormat object is linient.
1060
 
 * Otherwise, an IllegalArgumentException will be thrown when formatting
1061
 
 * "January 35".  See GregorianCalendar class javadoc for more details.
1062
 
 */
1063
 
void NumberFormatRegressionTest::Test4099404(void)
1064
 
{
1065
 
    //try {
1066
 
        UErrorCode status = U_ZERO_ERROR;
1067
 
        DecimalFormat *fmt = new DecimalFormat(UnicodeString("000.0#0"), status);
1068
 
        if(! U_FAILURE(status))
1069
 
            errln(UnicodeString("Bug 4099404 failed applying illegal pattern \"000.0#0\""));
1070
 
    /*} catch (Exception foo) {
1071
 
        logln("Bug 4099404 pattern \"000.0#0\" passed");
1072
 
    }*/
1073
 
    delete fmt;
1074
 
    fmt = 0;
1075
 
        //try {
1076
 
        fmt = new DecimalFormat(UnicodeString("0#0.000"), status);
1077
 
        if( !U_FAILURE(status))
1078
 
           errln("Bug 4099404 failed applying illegal pattern \"0#0.000\"");
1079
 
    /*} catch (Exception foo) {
1080
 
        logln("Bug 4099404 pattern \"0#0.000\" passed");
1081
 
    }*/
1082
 
 
1083
 
    delete fmt;
1084
 
}
1085
 
/* @bug 4101481
1086
 
 * DecimalFormat.applyPattern doesn't set minimum integer digits
1087
 
 */
1088
 
void NumberFormatRegressionTest::Test4101481(void)
1089
 
{
1090
 
    UErrorCode status = U_ZERO_ERROR;
1091
 
    DecimalFormat *sdf = new DecimalFormat(UnicodeString("#,##0"), status);
1092
 
    failure(status, "new DecimalFormat");
1093
 
    if (sdf->getMinimumIntegerDigits() != 1)
1094
 
        errln("Minimum integer digits : " + sdf->getMinimumIntegerDigits());
1095
 
    delete sdf;
1096
 
}
1097
 
/* @bug 4052223 (API addition request A27)
1098
 
 * Tests ParsePosition.setErrorPosition() and ParsePosition.getErrorPosition().
1099
 
 */
1100
 
void NumberFormatRegressionTest::Test4052223(void)
1101
 
{
1102
 
    //try {
1103
 
    UErrorCode status = U_ZERO_ERROR;
1104
 
        DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,#00.00"), status);
1105
 
        failure(status, "new DecimalFormat");
1106
 
        Formattable num;
1107
 
        fmt->parse(UnicodeString("abc3"), num, status);
1108
 
        if(! U_FAILURE(status))
1109
 
            errln(UnicodeString("Bug 4052223 failed : can't parse string \"a\".  Got ") /*+ num*/);
1110
 
    /*} catch (ParseException foo) {
1111
 
        logln("Caught expected ParseException : " + foo.getMessage() + " at index : " + foo.getErrorOffset());
1112
 
    }*/
1113
 
    delete fmt;
1114
 
}
1115
 
/* @bug 4061302
1116
 
 * API tests for API addition request A9.
1117
 
 */
1118
 
void NumberFormatRegressionTest::Test4061302(void)
1119
 
{
1120
 
    UErrorCode status = U_ZERO_ERROR;
1121
 
    DecimalFormatSymbols *fmt = new DecimalFormatSymbols(status);
1122
 
    failure(status, "new DecimalFormatSymbols");
1123
 
    UnicodeString currency(fmt->getSymbol(DecimalFormatSymbols::kCurrencySymbol));
1124
 
    UnicodeString intlCurrency(fmt->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol));
1125
 
    UnicodeString monDecSeparator(fmt->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol));
1126
 
    if (currency == UnicodeString("") ||
1127
 
        intlCurrency == UnicodeString("") ||
1128
 
        monDecSeparator == UnicodeString(""))
1129
 
    {
1130
 
        errln("getCurrencySymbols failed, got empty string.");
1131
 
    }
1132
 
    UnicodeString monDecSeparatorStr;
1133
 
    monDecSeparatorStr.append(monDecSeparator);
1134
 
    logln((UnicodeString)"Before set ==> Currency : " + currency +(UnicodeString)" Intl Currency : " + intlCurrency + (UnicodeString)" Monetary Decimal Separator : " + monDecSeparatorStr);
1135
 
    fmt->setSymbol(DecimalFormatSymbols::kCurrencySymbol, UnicodeString("XYZ"));
1136
 
    fmt->setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, UnicodeString("ABC"));
1137
 
    fmt->setSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol, UnicodeString((UChar)0x002A/*'*'*/));
1138
 
    currency = fmt->getSymbol(DecimalFormatSymbols::kCurrencySymbol);
1139
 
    intlCurrency = fmt->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
1140
 
    monDecSeparator = fmt->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
1141
 
    if (currency != UnicodeString("XYZ") ||
1142
 
        intlCurrency != UnicodeString("ABC") ||
1143
 
        monDecSeparator != UnicodeString((UChar)0x002A/*'*'*/)) {
1144
 
        errln("setCurrencySymbols failed.");
1145
 
    }
1146
 
    monDecSeparatorStr.remove();
1147
 
    monDecSeparatorStr.append(monDecSeparator);
1148
 
    logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparatorStr);
1149
 
 
1150
 
    delete fmt;
1151
 
}
1152
 
/* @bug 4062486
1153
 
 * API tests for API addition request A23. FieldPosition.getBeginIndex and
1154
 
 * FieldPosition.getEndIndex.
1155
 
 */
1156
 
void NumberFormatRegressionTest::Test4062486(void)
1157
 
{
1158
 
    UErrorCode status = U_ZERO_ERROR;
1159
 
    DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,##0.00"), status);
1160
 
    failure(status, "new DecimalFormat");
1161
 
    UnicodeString formatted;
1162
 
    FieldPosition field(0);
1163
 
    double num = 1234.5;
1164
 
    fmt->format(num, formatted, field);
1165
 
    if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
1166
 
        errln(UnicodeString("Format 1234.5 failed. Begin index: ") /*+ field.getBeginIndex() + " End index: " + field.getEndIndex()*/);
1167
 
    field.setBeginIndex(7);
1168
 
    field.setEndIndex(4);
1169
 
    if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
1170
 
        errln("Set begin/end field indexes failed. Begin index: " /*+ field.getBeginIndex() + " End index: " + field.getEndIndex()*/);
1171
 
 
1172
 
    delete fmt;
1173
 
}
1174
 
 
1175
 
/* @bug 4108738
1176
 
 * DecimalFormat.parse incorrectly works with a group separator.
1177
 
 */
1178
 
void NumberFormatRegressionTest::Test4108738(void)
1179
 
{
1180
 
    UErrorCode status = U_ZERO_ERROR;
1181
 
    DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::US, status);
1182
 
    failure(status, "new DecimalFormatSymbols");
1183
 
    DecimalFormat *df = new DecimalFormat("#,##0.###", syms, status);
1184
 
    failure(status, "new DecimalFormat");
1185
 
    UnicodeString text("1.222,111");
1186
 
    Formattable num;
1187
 
    ParsePosition pp(0);
1188
 
    df->parse(text, num, pp);
1189
 
    
1190
 
    // {sfb} how to do this (again) ?
1191
 
    // shouldn't just be another round-trip test, should it?
1192
 
    UnicodeString temp;
1193
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1194
 
    temp = df->format(num.getDouble(), temp, pos);
1195
 
    //if (!num.toString().equals("1.222"))
1196
 
    if (temp != UnicodeString("1.222"))
1197
 
        //errln("\"" + text + "\"  is parsed as " + num);
1198
 
        errln("\"" + text + "\"  is parsed as " + temp);
1199
 
    text = UnicodeString("1.222x111");
1200
 
    pp = ParsePosition(0);
1201
 
    df->parse(text, num, pp);
1202
 
    temp.remove();
1203
 
    temp = df->format(num.getDouble(), temp, pos);
1204
 
    //if (!num.toString().equals("1.222"))
1205
 
    if (temp != UnicodeString("1.222"))
1206
 
        errln("\"" + text + "\"  is parsed as " + temp);
1207
 
 
1208
 
    delete df;
1209
 
}
1210
 
 
1211
 
/* @bug 4106658
1212
 
 * DecimalFormat.format() incorrectly formats negative doubles.
1213
 
 */
1214
 
void NumberFormatRegressionTest::Test4106658(void)
1215
 
{
1216
 
    UErrorCode status = U_ZERO_ERROR;
1217
 
    DecimalFormat *df = new DecimalFormat(status); // Corrected; see 4147706
1218
 
    failure(status, "new DecimalFormat");
1219
 
    volatile double d1 = 0.0;   // volatile to prevent code optimization
1220
 
    double d2 = -0.0001;
1221
 
    UnicodeString buffer;
1222
 
    UnicodeString temp;
1223
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1224
 
 
1225
 
#if defined(HPUX)
1226
 
    d1 = 0.0 * -1.0;    // old HPUX compiler ignores volatile keyword
1227
 
#else
1228
 
    d1 *= -1.0; // Some compilers have a problem with defining -0.0
1229
 
#endif
1230
 
    logln("pattern: \"" + df->toPattern(temp) + "\"");
1231
 
    df->format(d1, buffer, pos);
1232
 
    if (buffer != UnicodeString("-0")) // Corrected; see 4147706
1233
 
        errln(UnicodeString("") + d1 + "      is formatted as " + buffer);
1234
 
    buffer.remove();
1235
 
    df->format(d2, buffer, pos);
1236
 
    if (buffer != UnicodeString("-0")) // Corrected; see 4147706
1237
 
        errln(UnicodeString("") + d2 + "      is formatted as " + buffer);
1238
 
 
1239
 
    delete df;
1240
 
}
1241
 
 
1242
 
/* @bug 4106662
1243
 
 * DecimalFormat.parse returns 0 if string parameter is incorrect.
1244
 
 */
1245
 
void NumberFormatRegressionTest::Test4106662(void)
1246
 
{
1247
 
    UErrorCode status = U_ZERO_ERROR;
1248
 
    DecimalFormat *df = new DecimalFormat(status);
1249
 
    failure(status, "new DecimalFormat");
1250
 
    UnicodeString text("x");
1251
 
    ParsePosition pos1(0), pos2(0);
1252
 
 
1253
 
    UnicodeString temp;
1254
 
    logln("pattern: \"" + df->toPattern(temp) + "\"");
1255
 
    Formattable num;
1256
 
    df->parse(text, num, pos1);
1257
 
    if (pos1 == ParsePosition(0)/*num != null*/) {
1258
 
        errln(UnicodeString("Test Failed: \"") + text + "\" is parsed as " /*+ num*/);
1259
 
    }
1260
 
    delete df;
1261
 
    df = new DecimalFormat(UnicodeString("$###.00"), status);
1262
 
    failure(status, "new DecimalFormat");
1263
 
    df->parse(UnicodeString("$"), num, pos2);
1264
 
    if (pos2 == ParsePosition(0) /*num != null*/){
1265
 
        errln(UnicodeString("Test Failed: \"$\" is parsed as ") /*+ num*/);
1266
 
    }
1267
 
 
1268
 
    delete df;
1269
 
}
1270
 
 
1271
 
/* @bug 4114639 (duplicate of 4106662)
1272
 
 * NumberFormat.parse doesn't return null
1273
 
 */
1274
 
void NumberFormatRegressionTest::Test4114639(void)
1275
 
{
1276
 
    UErrorCode status = U_ZERO_ERROR;
1277
 
    NumberFormat *format = NumberFormat::createInstance(status);
1278
 
    failure(status, "NumberFormat::createInstance");
1279
 
    UnicodeString text("time 10:x");
1280
 
    ParsePosition pos(8);
1281
 
    Formattable result;
1282
 
    format->parse(text, result, pos);
1283
 
    if (/*result != null*/pos.getErrorIndex() != 8) 
1284
 
        errln(UnicodeString("Should return null but got : ") /*+ result*/); // Should be null; it isn't
1285
 
 
1286
 
    delete format;
1287
 
}
1288
 
 
1289
 
/* @bug 4106664
1290
 
 * TODO: this test does not work because we need to use a 64 bit number and a
1291
 
 * a double only MAY only have 52 bits of precision.
1292
 
 * DecimalFormat.format(long n) fails if n * multiplier > MAX_LONG.
1293
 
 */
1294
 
void NumberFormatRegressionTest::Test4106664(void)
1295
 
{
1296
 
    UErrorCode status = U_ZERO_ERROR;
1297
 
    DecimalFormat *df = new DecimalFormat(status);
1298
 
    failure(status, "new DecimalFormat");
1299
 
    // {sfb} long in java is 64 bits
1300
 
    /*long*/double n = 1234567890123456.0;
1301
 
    /*int*/int32_t m = 12345678;
1302
 
    // {sfb} will this work?
1303
 
    //BigInteger bigN = BigInteger.valueOf(n);
1304
 
    //bigN = bigN.multiply(BigInteger.valueOf(m));
1305
 
    double bigN = n * m;
1306
 
    df->setMultiplier(m);
1307
 
    df->setGroupingUsed(FALSE);
1308
 
    UnicodeString temp;
1309
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1310
 
    logln("formated: " +
1311
 
        df->format(n, temp, pos));
1312
 
    
1313
 
    char buf [128];
1314
 
    sprintf(buf, "%g", bigN);
1315
 
    //logln("expected: " + bigN.toString());
1316
 
    logln(UnicodeString("expected: ") + buf);
1317
 
 
1318
 
    delete df;
1319
 
}
1320
 
/* @bug 4106667 (duplicate of 4106658)
1321
 
 * DecimalFormat.format incorrectly formats -0.0.
1322
 
 */
1323
 
void NumberFormatRegressionTest::Test4106667(void)
1324
 
{
1325
 
    UErrorCode status = U_ZERO_ERROR;
1326
 
    DecimalFormat *df = new DecimalFormat(status);
1327
 
    failure(status, "new DecimalFormat");
1328
 
    UChar foo [] = { 0x002B };
1329
 
    UnicodeString bar(foo, 1, 1);
1330
 
    volatile double d = 0.0;   // volatile to prevent code optimization
1331
 
    UnicodeString temp;
1332
 
    UnicodeString buffer;
1333
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1334
 
 
1335
 
    logln("pattern: \"" + df->toPattern(temp) + "\"");
1336
 
#if defined(HPUX)
1337
 
    d = 0.0 * -1.0;    // old HPUX compiler ignores volatile keyword
1338
 
#else
1339
 
    d *= -1.0; // Some compilers have a problem with defining -0.0
1340
 
#endif
1341
 
    df->setPositivePrefix(/*"+"*/bar);
1342
 
    df->format(d, buffer, pos);
1343
 
    if (buffer != UnicodeString("-0")) // Corrected; see 4147706
1344
 
        errln(/*d + */UnicodeString("  is formatted as ") + buffer);
1345
 
 
1346
 
    delete df;
1347
 
}
1348
 
 
1349
 
/* @bug 4110936
1350
 
 * DecimalFormat.setMaximumIntegerDigits() works incorrectly.
1351
 
 */
1352
 
#ifdef OS390
1353
 
#   define MAX_INT_DIGITS 70
1354
 
#else
1355
 
#   define MAX_INT_DIGITS 128
1356
 
#endif
1357
 
 
1358
 
void NumberFormatRegressionTest::Test4110936(void)
1359
 
{
1360
 
    UErrorCode status = U_ZERO_ERROR;
1361
 
    NumberFormat *nf = NumberFormat::createInstance(status);
1362
 
    failure(status, "NumberFormat::createInstance");
1363
 
    nf->setMaximumIntegerDigits(MAX_INT_DIGITS);
1364
 
    logln("setMaximumIntegerDigits(MAX_INT_DIGITS)");
1365
 
    if (nf->getMaximumIntegerDigits() != MAX_INT_DIGITS)
1366
 
        errln("getMaximumIntegerDigits() returns " +
1367
 
            nf->getMaximumIntegerDigits());
1368
 
 
1369
 
    delete nf;
1370
 
}
1371
 
 
1372
 
/* @bug 4122840
1373
 
 * Locale data should use generic currency symbol
1374
 
 *
1375
 
 * 1) Make sure that all currency formats use the generic currency symbol.
1376
 
 * 2) Make sure we get the same results using the generic symbol or a
1377
 
 *    hard-coded one.
1378
 
 */
1379
 
void NumberFormatRegressionTest::Test4122840(void)
1380
 
{
1381
 
    int32_t count = 0;
1382
 
    const Locale *locales = Locale::getAvailableLocales(count);
1383
 
    
1384
 
    for (int i = 0; i < count; i++) {
1385
 
        UErrorCode status = U_ZERO_ERROR;
1386
 
        ResourceBundle *rb = new ResourceBundle(
1387
 
            u_getDataDirectory()/*"java.text.resources.LocaleElements"*/, 
1388
 
            locales[i], status);
1389
 
        failure(status, "new ResourceBundle");
1390
 
        ResourceBundle numPat = rb->get("NumberPatterns", status);
1391
 
        failure(status, "new ResourceBundle(NumberPatterns)");
1392
 
        //
1393
 
        // Get the currency pattern for this locale.  We have to fish it
1394
 
        // out of the ResourceBundle directly, since DecimalFormat.toPattern
1395
 
        // will return the localized symbol, not \00a4
1396
 
        //
1397
 
        UnicodeString pattern = numPat.getStringEx(1, status);
1398
 
        failure(status, "rb->getStringArray");
1399
 
 
1400
 
        UChar fo[] = { 0x00A4 };
1401
 
        UnicodeString foo(fo, 1, 1);
1402
 
 
1403
 
        //if (pattern.indexOf("\u00A4") == -1 ) {
1404
 
        if (pattern.indexOf(foo) == -1 ) {
1405
 
            errln(UnicodeString("Currency format for ") + UnicodeString(locales[i].getName()) +
1406
 
                    " does not contain generic currency symbol:" +
1407
 
                    pattern );
1408
 
        }
1409
 
 
1410
 
        // Create a DecimalFormat using the pattern we got and format a number
1411
 
        DecimalFormatSymbols *symbols = new DecimalFormatSymbols(locales[i], status);
1412
 
        failure(status, "new DecimalFormatSymbols");
1413
 
        DecimalFormat *fmt1 = new DecimalFormat(pattern, *symbols, status);
1414
 
        failure(status, "new DecimalFormat");
1415
 
        
1416
 
        UnicodeString result1;
1417
 
        FieldPosition pos(FieldPosition::DONT_CARE);
1418
 
        result1 = fmt1->format(1.111, result1, pos);
1419
 
        
1420
 
        //
1421
 
        // Now substitute in the locale's currency symbol and create another
1422
 
        // pattern.  We have to skip locales where the currency symbol
1423
 
        // contains decimal separators, because that confuses things
1424
 
        //
1425
 
        UChar ba[] = { 0x002E/*'.'*/ };
1426
 
        UnicodeString bar(ba, 1, 1);
1427
 
 
1428
 
        if (symbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol).indexOf(bar) == -1) {
1429
 
            // {sfb} Also, switch the decimal separator to the monetary decimal
1430
 
            // separator to mimic the behavior of a currency format
1431
 
            symbols->setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol,
1432
 
                symbols->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol));
1433
 
            
1434
 
            UnicodeString buf(pattern);
1435
 
            for (int j = 0; j < buf.length(); j++) {
1436
 
                if (buf[j] == 0x00a4 ) {
1437
 
                    if(buf[j + 1] == 0x00a4) {
1438
 
                        // {sfb} added to support double currency marker (intl currency sign)
1439
 
                        buf.replace(j, /*j+*/2, symbols->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol));
1440
 
                        j += symbols->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol).length();
1441
 
                    }
1442
 
                    else {
1443
 
                        buf.replace(j, /*j+*/1, symbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol)); 
1444
 
                        j += symbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol).length() - 1;
1445
 
                    }                    
1446
 
                }
1447
 
            }
1448
 
 
1449
 
            DecimalFormat *fmt2 = new DecimalFormat(buf, *symbols, status);
1450
 
            failure(status, "new DecimalFormat");
1451
 
            
1452
 
            UnicodeString result2;
1453
 
            fmt2->format(1.111, result2, pos);
1454
 
            
1455
 
            if (result1 != result2) {
1456
 
                errln("Results for " + (UnicodeString)(locales[i].getName()) + " differ: " +
1457
 
                        result1 + " vs " + result2);
1458
 
            }
1459
 
        
1460
 
            delete fmt2;
1461
 
        }
1462
 
    
1463
 
        delete rb;
1464
 
        delete fmt1;
1465
 
        delete symbols;
1466
 
    }
1467
 
}
1468
 
 
1469
 
/* @bug 4125885
1470
 
 * DecimalFormat.format() delivers wrong string.
1471
 
 */
1472
 
void NumberFormatRegressionTest::Test4125885(void)
1473
 
{
1474
 
    UErrorCode status = U_ZERO_ERROR;
1475
 
    double rate = 12.34;
1476
 
    DecimalFormat *formatDec = new DecimalFormat ("000.00", status);
1477
 
    failure(status, "new DecimalFormat");
1478
 
    UnicodeString temp;
1479
 
    logln("toPattern: " + formatDec->toPattern(temp));
1480
 
    UnicodeString rateString;
1481
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1482
 
    rateString = formatDec->format(rate, rateString, pos);
1483
 
    if (rateString != UnicodeString("012.34"))
1484
 
        errln("result : " + rateString + " expected : 012.34");
1485
 
    rate = 0.1234;
1486
 
    delete formatDec;// = null;
1487
 
    formatDec = new DecimalFormat ("+000.00%;-000.00%", status);
1488
 
    failure(status, "new DecimalFormat");
1489
 
    logln("toPattern: " + formatDec->toPattern(temp));
1490
 
    rateString.remove();
1491
 
    rateString = formatDec->format(rate, rateString, pos);
1492
 
    if (rateString != UnicodeString("+012.34%"))
1493
 
        errln("result : " + rateString + " expected : +012.34%");
1494
 
 
1495
 
    delete formatDec;
1496
 
}
1497
 
 
1498
 
/**
1499
 
 * @bug 4134034
1500
 
 * DecimalFormat produces extra zeros when formatting numbers.
1501
 
 */
1502
 
void NumberFormatRegressionTest::Test4134034(void) 
1503
 
{
1504
 
    UErrorCode status = U_ZERO_ERROR;
1505
 
    DecimalFormat *nf = new DecimalFormat("##,###,###.00", status);
1506
 
    failure(status, "new DecimalFormat");
1507
 
    
1508
 
    UnicodeString f;
1509
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1510
 
    f = nf->format(9.02, f, pos);
1511
 
    if (f == UnicodeString("9.02")) 
1512
 
        logln(f + " ok"); 
1513
 
    else 
1514
 
        errln("9.02 -> " + f + "; want 9.02");
1515
 
 
1516
 
    f.remove();
1517
 
    f = nf->format((int32_t)0, f, pos);
1518
 
    if (f == UnicodeString(".00")) 
1519
 
        logln(f + " ok"); 
1520
 
    else 
1521
 
        errln("0 -> " + f + "; want .00");
1522
 
 
1523
 
    delete nf;
1524
 
}
1525
 
 
1526
 
/**
1527
 
 * @bug 4134300
1528
 
 * CANNOT REPRODUCE - This bug could not be reproduced.  It may be
1529
 
 * a duplicate of 4134034.
1530
 
 *
1531
 
 * JDK 1.1.6 Bug, did NOT occur in 1.1.5
1532
 
 * Possibly related to bug 4125885.
1533
 
 * 
1534
 
 * This class demonstrates a regression in version 1.1.6
1535
 
 * of DecimalFormat class.
1536
 
 * 
1537
 
 * 1.1.6 Results
1538
 
 * Value 1.2 Format #.00 Result '01.20' !!!wrong
1539
 
 * Value 1.2 Format 0.00 Result '001.20' !!!wrong
1540
 
 * Value 1.2 Format 00.00 Result '0001.20' !!!wrong
1541
 
 * Value 1.2 Format #0.0# Result '1.2'
1542
 
 * Value 1.2 Format #0.00 Result '001.20' !!!wrong
1543
 
 * 
1544
 
 * 1.1.5 Results
1545
 
 * Value 1.2 Format #.00 Result '1.20'
1546
 
 * Value 1.2 Format 0.00 Result '1.20'
1547
 
 * Value 1.2 Format 00.00 Result '01.20'
1548
 
 * Value 1.2 Format #0.0# Result '1.2'
1549
 
 * Value 1.2 Format #0.00 Result '1.20'
1550
 
 */
1551
 
void NumberFormatRegressionTest::Test4134300(void) {
1552
 
    UnicodeString DATA [] = {
1553
 
     // Pattern      Expected string
1554
 
        UnicodeString("#.00"),      UnicodeString("1.20"),
1555
 
        UnicodeString("0.00"),      UnicodeString("1.20"),
1556
 
        UnicodeString("00.00"),     UnicodeString("01.20"),
1557
 
        UnicodeString("#0.0#"),     UnicodeString("1.2"),
1558
 
        UnicodeString("#0.00"),     UnicodeString("1.20")
1559
 
    };
1560
 
 
1561
 
    for (int i=0; i< 10; i+=2) {
1562
 
        UnicodeString result;
1563
 
        UErrorCode status = U_ZERO_ERROR;
1564
 
        DecimalFormat *df = new DecimalFormat(DATA[i], status);
1565
 
        failure(status, "new DecimalFormat");
1566
 
        FieldPosition pos(FieldPosition::DONT_CARE);
1567
 
        result = df->format(1.2, result, pos);
1568
 
        if (result != DATA[i+1]) {
1569
 
            errln("Fail: 1.2 x " + DATA[i] + " = " + result +
1570
 
                  "; want " + DATA[i+1]);
1571
 
        }
1572
 
        else {
1573
 
            logln("Ok: 1.2 x " + DATA[i] + " = " + result);
1574
 
        }
1575
 
    
1576
 
        delete df;
1577
 
    }
1578
 
}
1579
 
 
1580
 
/**
1581
 
 * @bug 4140009
1582
 
 * Empty pattern produces double negative prefix.
1583
 
 */
1584
 
void NumberFormatRegressionTest::Test4140009(void) 
1585
 
{
1586
 
    UErrorCode status = U_ZERO_ERROR;
1587
 
    DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::ENGLISH, status);
1588
 
    failure(status, "new DecimalFormatSymbols");
1589
 
    DecimalFormat *f = new DecimalFormat(UnicodeString(""), syms, status);
1590
 
    failure(status, "new DecimalFormat");
1591
 
    UnicodeString s;
1592
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1593
 
    s = f->format(123.456, s, pos);
1594
 
    if (s != UnicodeString("123.456"))
1595
 
        errln("Fail: Format empty pattern x 123.456 => " + s);
1596
 
    s.remove();
1597
 
    s = f->format(-123.456, s, pos);
1598
 
    if (s != UnicodeString("-123.456"))
1599
 
        errln("Fail: Format empty pattern x -123.456 => " + s);
1600
 
    delete f;
1601
 
}
1602
 
 
1603
 
/**
1604
 
 * @bug 4141750
1605
 
 * BigDecimal numbers get their fractions truncated by NumberFormat.
1606
 
 */
1607
 
// {sfb} not pertinent in C++ ??
1608
 
void NumberFormatRegressionTest::Test4141750(void) {
1609
 
    /*try {
1610
 
        UnicodeString str("12345.67");
1611
 
        BigDecimal bd = new BigDecimal(str);
1612
 
        String sd = NumberFormat.getInstance(Locale.US).format(bd);
1613
 
        if (!sd.endsWith("67")) errln("Fail: " + str + " x format -> " + sd);
1614
 
    }
1615
 
    catch (Exception e) {
1616
 
        errln(e.toString());
1617
 
        e.printStackTrace();
1618
 
    }*/
1619
 
}
1620
 
 
1621
 
/**
1622
 
 * @bug 4145457
1623
 
 * DecimalFormat toPattern() doesn't quote special characters or handle
1624
 
 * single quotes.
1625
 
 */
1626
 
void NumberFormatRegressionTest::Test4145457() {
1627
 
    //try {
1628
 
    UErrorCode status = U_ZERO_ERROR;
1629
 
    NumberFormat *nff = NumberFormat::createInstance(status);
1630
 
    failure(status, "NumberFormat::createInstance");
1631
 
    if(nff->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
1632
 
        errln("DecimalFormat needed to continue");
1633
 
        return;
1634
 
    }
1635
 
 
1636
 
    DecimalFormat *nf = (DecimalFormat*)nff;
1637
 
    DecimalFormatSymbols *sym = (DecimalFormatSymbols*) nf->getDecimalFormatSymbols();
1638
 
    sym->setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, (UChar)/*'\''*/0x0027);
1639
 
    nf->setDecimalFormatSymbols(*sym);
1640
 
    double pi = 3.14159;
1641
 
    
1642
 
    UnicodeString PATS [] = { 
1643
 
        UnicodeString("#.00 'num''ber'"), UnicodeString("''#.00''")
1644
 
    };
1645
 
 
1646
 
    for (int32_t i=0; i<2; ++i) {
1647
 
        nf->applyPattern(PATS[i], status);
1648
 
        failure(status, "nf->applyPattern");
1649
 
        UnicodeString out;
1650
 
        FieldPosition pos(FieldPosition::DONT_CARE);
1651
 
        out = nf->format(pi, out, pos);
1652
 
        UnicodeString pat;
1653
 
        pat = nf->toPattern(pat);
1654
 
        Formattable num;
1655
 
        ParsePosition pp(0);
1656
 
        nf->parse(out, num, pp);
1657
 
        double val = num.getDouble();
1658
 
        
1659
 
        nf->applyPattern(pat, status);
1660
 
        failure(status, "nf->applyPattern");
1661
 
        UnicodeString out2;
1662
 
        out2 = nf->format(pi, out2, pos);
1663
 
        UnicodeString pat2;
1664
 
        pat2 = nf->toPattern(pat2);
1665
 
        nf->parse(out2, num, pp);
1666
 
        double val2 = num.getDouble();
1667
 
        
1668
 
        if (pat != pat2)
1669
 
            errln("Fail with \"" + PATS[i] + "\": Patterns should concur, \"" +
1670
 
                pat + "\" vs. \"" + pat2 + "\"");
1671
 
        else
1672
 
            logln("Ok \"" + PATS[i] + "\" toPattern() -> \"" + pat + '"');
1673
 
        
1674
 
        if (val == val2 && out == out2) {
1675
 
            logln(UnicodeString("Ok ") + pi + " x \"" + PATS[i] + "\" -> \"" +
1676
 
                out + "\" -> " + val + " -> \"" +
1677
 
                out2 + "\" -> " + val2);
1678
 
        }
1679
 
        else {
1680
 
            errln(UnicodeString("Fail ") + pi + " x \"" + PATS[i] + "\" -> \"" +
1681
 
                out + "\" -> " + val + " -> \"" +
1682
 
                out2 + "\" -> " + val2);
1683
 
        }
1684
 
    }
1685
 
    /*}
1686
 
    catch (ParseException e) {
1687
 
        errln("Fail: " + e);
1688
 
        e.printStackTrace();
1689
 
    }*/
1690
 
 
1691
 
    delete nff;
1692
 
}
1693
 
 
1694
 
/**
1695
 
 * @bug 4147295
1696
 
 * DecimalFormat.applyPattern() sets minimum integer digits incorrectly.
1697
 
 * CANNOT REPRODUCE
1698
 
 * This bug is a duplicate of 4139344, which is a duplicate of 4134300
1699
 
 */
1700
 
void NumberFormatRegressionTest::Test4147295(void) 
1701
 
{
1702
 
    UErrorCode status = U_ZERO_ERROR;
1703
 
    DecimalFormat *sdf = new DecimalFormat(status);
1704
 
    UnicodeString pattern("#,###");
1705
 
    logln("Applying pattern \"" + pattern + "\"");
1706
 
    sdf->applyPattern(pattern, status);
1707
 
    failure(status, "sdf->applyPattern");
1708
 
    int minIntDig = sdf->getMinimumIntegerDigits();
1709
 
    if (minIntDig != 0) {
1710
 
        errln("Test failed");
1711
 
        errln(" Minimum integer digits : " + minIntDig);
1712
 
        UnicodeString temp;
1713
 
        errln(" new pattern: " + sdf->toPattern(temp));
1714
 
    } else {
1715
 
        logln("Test passed");
1716
 
        logln(" Minimum integer digits : " + minIntDig);
1717
 
    }
1718
 
    delete sdf;
1719
 
}
1720
 
 
1721
 
/**
1722
 
 * @bug 4147706
1723
 
 * DecimalFormat formats -0.0 as +0.0
1724
 
 * See also older related bug 4106658, 4106667
1725
 
 */
1726
 
void NumberFormatRegressionTest::Test4147706(void) 
1727
 
{
1728
 
    UErrorCode status = U_ZERO_ERROR;
1729
 
    DecimalFormat *df = new DecimalFormat("#,##0.0##", status);
1730
 
    failure(status, "new DecimalFormat");
1731
 
    DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::ENGLISH, status);
1732
 
    failure(status, "new DecimalFormatSymbols");
1733
 
    UnicodeString f1;
1734
 
    UnicodeString f2, temp;
1735
 
    FieldPosition pos(FieldPosition::DONT_CARE);
1736
 
    volatile double d1 = 0.0;   // volatile to prevent code optimization
1737
 
    double d2 = -0.0001;
1738
 
 
1739
 
#if defined(HPUX)
1740
 
    d1 = 0.0 * -1.0;    // old HPUX compiler ignores volatile keyword
1741
 
#else
1742
 
    d1 *= -1.0; // Some compilers have a problem with defining -0.0
1743
 
#endif
1744
 
    df->adoptDecimalFormatSymbols(syms);
1745
 
    f1 = df->format(d1, f1, pos);
1746
 
    f2 = df->format(d2, f2, pos);
1747
 
    if (f1 != UnicodeString("-0.0")) {
1748
 
        errln(UnicodeString("") + d1 + UnicodeString(" x \"") + df->toPattern(temp) + "\" is formatted as \"" + f1 + '"');
1749
 
    }
1750
 
    if (f2 != UnicodeString("-0.0")) {
1751
 
        errln(UnicodeString("") + d2 + UnicodeString(" x \"") + df->toPattern(temp) + "\" is formatted as \"" + f2 + '"');
1752
 
    }
1753
 
 
1754
 
    delete df;
1755
 
}
1756
 
 
1757
 
 
1758
 
// Not applicable, since no serialization in C++
1759
 
/*class myformat implements Serializable
1760
 
{
1761
 
DateFormat _dateFormat = DateFormat.getDateInstance();
1762
 
 
1763
 
public String Now()
1764
 
{
1765
 
    GregorianCalendar calendar = new GregorianCalendar();
1766
 
    Date t = calendar.getTime();
1767
 
    String nowStr = _dateFormat.format(t);
1768
 
    return nowStr;
1769
 
}
1770
 
}*/
1771
 
 
1772
 
/**
1773
 
 * @bug 4162198
1774
 
 * NumberFormat cannot format Double.MAX_VALUE
1775
 
 */
1776
 
// TODO: make this test actually test something
1777
 
void 
1778
 
NumberFormatRegressionTest::Test4162198(void) 
1779
 
{
1780
 
    // for some reason, DBL_MAX will not round trip. (bug in sprintf/atof)
1781
 
    double dbl = INT32_MAX * 1000.0;
1782
 
    UErrorCode status = U_ZERO_ERROR;
1783
 
    NumberFormat *f = NumberFormat::createInstance(status);
1784
 
    if(U_FAILURE(status)) {
1785
 
        errln("Couldn't create number format");
1786
 
        return;
1787
 
    }
1788
 
    f->setMaximumFractionDigits(INT32_MAX);
1789
 
    f->setMaximumIntegerDigits(INT32_MAX);
1790
 
    UnicodeString s;
1791
 
    f->format(dbl,s);
1792
 
    logln(UnicodeString("The number ") + dbl + " formatted to " + s);
1793
 
    Formattable n;
1794
 
    //try {
1795
 
    f->parse(s, n, status);
1796
 
    if(U_FAILURE(status))
1797
 
        errln("Couldn't parse!");
1798
 
    //} catch (java.text.ParseException e) {
1799
 
    //    errln("Caught a ParseException:");
1800
 
    //    e.printStackTrace();
1801
 
    //}
1802
 
    
1803
 
    //logln("The string " + s + " parsed as " + n);
1804
 
    
1805
 
    if(n.getDouble() != dbl) {
1806
 
        errln("Round trip failure");
1807
 
    }
1808
 
    delete f;
1809
 
}
1810
 
 
1811
 
/**
1812
 
 * @bug 4162852
1813
 
 * NumberFormat does not parse negative zero.
1814
 
 */
1815
 
void 
1816
 
NumberFormatRegressionTest::Test4162852(void) 
1817
 
{
1818
 
    UErrorCode status = U_ZERO_ERROR;
1819
 
    for(int32_t i=0; i < 2; ++i) {
1820
 
        NumberFormat *f = (i == 0) ? NumberFormat::createInstance(status)
1821
 
            : NumberFormat::createPercentInstance(status);
1822
 
        if(U_FAILURE(status)) {
1823
 
            errln("Couldn't create number format");
1824
 
            return;
1825
 
        }
1826
 
        double d = 0.0;
1827
 
        d *= -1.0;
1828
 
        UnicodeString s;
1829
 
        f->format(d, s);
1830
 
        Formattable n;
1831
 
        f->parse(s, n, status);
1832
 
        if(U_FAILURE(status))
1833
 
            errln("Couldn't parse!");
1834
 
        double e = n.getDouble();
1835
 
        logln(UnicodeString("") +
1836
 
              d + " -> " +
1837
 
              '"' + s + '"' + " -> " + e);
1838
 
#if (defined(OS390) && !defined(IEEE_754)) || defined(OS400)
1839
 
        if (e != 0.0) {
1840
 
#else
1841
 
        if (e != 0.0 || 1.0/e > 0.0) {
1842
 
#endif
1843
 
            logln("Failed to parse negative zero");
1844
 
        }
1845
 
        delete f;
1846
 
    }
1847
 
}
1848
 
 
1849
 
static double _u_abs(double a) { return a<0?-a:a; }
1850
 
 
1851
 
/**
1852
 
 * May 17 1999 sync up - liu
1853
 
 * @bug 4167494
1854
 
 * NumberFormat truncates data
1855
 
 */
1856
 
void NumberFormatRegressionTest::Test4167494(void) {
1857
 
    UErrorCode status = U_ZERO_ERROR;
1858
 
    NumberFormat *fmt = NumberFormat::createInstance(Locale::US, status);
1859
 
    failure(status, "NumberFormat::createInstance");
1860
 
 
1861
 
    double a = DBL_MAX * 0.99; // DBL_MAX itself overflows to +Inf
1862
 
    UnicodeString s;
1863
 
    fmt->format(a, s);
1864
 
    Formattable num;
1865
 
    fmt->parse(s, num, status);
1866
 
    failure(status, "Parse");
1867
 
    if (num.getType() == Formattable::kDouble &&
1868
 
        _u_abs(num.getDouble() - a) / a < 0.01) { // RT within 1%
1869
 
        logln(UnicodeString("") + a + " -> \"" + s + "\" -> " +
1870
 
              toString(num) + " ok");
1871
 
    } else {
1872
 
        errln(UnicodeString("") + a + " -> \"" + s + "\" -> " +
1873
 
              toString(num) + " FAIL");
1874
 
    }
1875
 
 
1876
 
    // We don't test Double.MIN_VALUE because the locale data for the US
1877
 
    // currently doesn't specify enough digits to display Double.MIN_VALUE.
1878
 
    // This is correct for now; however, we leave this here as a reminder
1879
 
    // in case we want to address this later.
1880
 
 
1881
 
    delete fmt;
1882
 
}
1883
 
 
1884
 
/**
1885
 
 * May 17 1999 sync up - liu
1886
 
 * @bug 4170798
1887
 
 * DecimalFormat.parse() fails when ParseIntegerOnly set to true
1888
 
 */
1889
 
void NumberFormatRegressionTest::Test4170798(void) {
1890
 
    UErrorCode status = U_ZERO_ERROR;
1891
 
    NumberFormat *nf = NumberFormat::createInstance(Locale::US, status);
1892
 
    failure(status, "NumberFormat::createInstance");
1893
 
    if(nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
1894
 
        errln("DecimalFormat needed to continue");
1895
 
        return;
1896
 
    }
1897
 
    DecimalFormat *df = (DecimalFormat*) nf;
1898
 
    df->setParseIntegerOnly(TRUE);
1899
 
    Formattable n;
1900
 
    ParsePosition pos(0);
1901
 
    df->parse("-0.0", n, pos);
1902
 
    if (n.getType() != Formattable::kLong
1903
 
        || n.getLong() != 0) {
1904
 
        errln(UnicodeString("FAIL: parse(\"-0.0\") returns ") + toString(n));
1905
 
    }
1906
 
    delete nf;
1907
 
}
1908
 
 
1909
 
/**
1910
 
 * May 17 1999 sync up - liu
1911
 
 * toPattern only puts the first grouping separator in.
1912
 
 */
1913
 
void NumberFormatRegressionTest::Test4176114(void) {
1914
 
    const char* DATA[] = {
1915
 
        "00", "#00",
1916
 
        "000", "#000", // No grouping
1917
 
        "#000", "#000", // No grouping
1918
 
        "#,##0", "#,##0",
1919
 
        "#,000", "#,000",
1920
 
        "0,000", "#0,000",
1921
 
        "00,000", "#00,000",
1922
 
        "000,000", "#,000,000",
1923
 
        "0,000,000,000,000.0000", "#0,000,000,000,000.0000", // Reported
1924
 
    };
1925
 
    int DATA_length = (int)(sizeof(DATA) / sizeof(DATA[0]));
1926
 
    UErrorCode status = U_ZERO_ERROR;
1927
 
    UnicodeString s;
1928
 
    for (int i=0; i<DATA_length; i+=2) {
1929
 
        DecimalFormat df(DATA[i], status);
1930
 
        failure(status, "DecimalFormat constructor");
1931
 
        df.toPattern(s);
1932
 
        UnicodeString exp(DATA[i+1]);
1933
 
        if (s != exp) {
1934
 
            errln(UnicodeString("FAIL: ") + DATA[i] + " -> " +
1935
 
                  s + ", want " + exp);
1936
 
        }
1937
 
    }
1938
 
}
1939
 
 
1940
 
/**
1941
 
 * May 17 1999 sync up - liu
1942
 
 * @bug 4179818
1943
 
 * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
1944
 
 */
1945
 
void NumberFormatRegressionTest::Test4179818(void) {
1946
 
    const char* DATA[] = {
1947
 
        // Input  Pattern  Expected output
1948
 
        "1.2511", "#.#",   "1.3",
1949
 
        "1.2501", "#.#",   "1.3",
1950
 
        "0.9999", "#",     "1",
1951
 
    };
1952
 
    int DATA_length = (int)(sizeof(DATA) / sizeof(DATA[0])); 
1953
 
    double DOUBLE[] = {
1954
 
        1.2511,
1955
 
        1.2501,
1956
 
        0.9999,
1957
 
    };
1958
 
    UErrorCode status = U_ZERO_ERROR;
1959
 
    DecimalFormatSymbols sym(Locale::US, status);
1960
 
    failure(status, "Construct DecimalFormatSymbols");
1961
 
    DecimalFormat fmt("#", sym, status);
1962
 
    failure(status, "Construct DecimalFormat");
1963
 
    for (int i=0; i<DATA_length; i+=3) {
1964
 
        double in = DOUBLE[i/3];
1965
 
        UnicodeString pat(DATA[i+1]);
1966
 
        UnicodeString exp(DATA[i+2]);
1967
 
        fmt.applyPattern(pat, status);
1968
 
        failure(status, "applyPattern");
1969
 
        UnicodeString out;
1970
 
        FieldPosition pos;
1971
 
        fmt.format(in, out, pos);
1972
 
        if (out == exp) {
1973
 
            logln(UnicodeString("Ok: ") + in + " x " + pat + " = " + out);
1974
 
        } else {
1975
 
            errln(UnicodeString("FAIL: ") + in + " x  " + pat + " = " + out +
1976
 
                  ", expected " + exp);
1977
 
        }
1978
 
    }
1979
 
}
1980
 
 
1981
 
/**
1982
 
 * May 17 1999 sync up - liu
1983
 
 * Some DecimalFormatSymbols changes are not picked up by DecimalFormat.
1984
 
 * This includes the minus sign, currency symbol, international currency
1985
 
 * symbol, percent, and permille.  This is filed as bugs 4212072 and
1986
 
 * 4212073.
1987
 
 */
1988
 
void NumberFormatRegressionTest::Test4212072(void) {
1989
 
    UErrorCode status = U_ZERO_ERROR;
1990
 
    DecimalFormatSymbols sym(Locale::US, status);
1991
 
/*
1992
 
    failure(status, "DecimalFormatSymbols ct");
1993
 
    DecimalFormat fmt(UnicodeString("#"), sym, status);
1994
 
    failure(status, "DecimalFormat ct");
1995
 
 
1996
 
    UnicodeString s;
1997
 
    FieldPosition pos;
1998
 
 
1999
 
    sym.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, (UChar)0x5e);
2000
 
    fmt.setDecimalFormatSymbols(sym);
2001
 
    s.remove();
2002
 
    if (fmt.format((int32_t)-1, s, pos) != UNICODE_STRING("^1", 2)) {
2003
 
        errln(UnicodeString("FAIL: -1 x (minus=^) -> ") + s +
2004
 
              ", exp ^1");
2005
 
    }
2006
 
    s.remove();
2007
 
    if (fmt.getNegativePrefix(s) != UnicodeString((UChar)0x5e)) {
2008
 
        errln(UnicodeString("FAIL: (minus=^).getNegativePrefix -> ") +
2009
 
              s + ", exp ^");
2010
 
    }
2011
 
    sym.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, (UChar)0x2d);
2012
 
 
2013
 
    fmt.applyPattern(UnicodeString("#%"), status);
2014
 
    failure(status, "applyPattern percent");
2015
 
    sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UChar)0x5e);
2016
 
    fmt.setDecimalFormatSymbols(sym);
2017
 
    s.remove();
2018
 
    if (fmt.format(0.25, s, pos) != UNICODE_STRING("25^", 3)) {
2019
 
        errln(UnicodeString("FAIL: 0.25 x (percent=^) -> ") + s +
2020
 
              ", exp 25^");
2021
 
    }
2022
 
    s.remove();
2023
 
    if (fmt.getPositiveSuffix(s) != UnicodeString((UChar)0x5e)) {
2024
 
        errln(UnicodeString("FAIL: (percent=^).getPositiveSuffix -> ") +
2025
 
              s + ", exp ^");
2026
 
    }
2027
 
    sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UChar)0x25);
2028
 
 
2029
 
    fmt.applyPattern(str("#\\u2030"), status);
2030
 
    failure(status, "applyPattern permill");
2031
 
    sym.setSymbol(DecimalFormatSymbols::kPerMillSymbol, (UChar)0x5e);
2032
 
    fmt.setDecimalFormatSymbols(sym);
2033
 
    s.remove();
2034
 
    if (fmt.format(0.25, s, pos) != UNICODE_STRING("250^", 4)) {
2035
 
        errln(UnicodeString("FAIL: 0.25 x (permill=^) -> ") + s +
2036
 
              ", exp 250^");
2037
 
    }
2038
 
    s.remove();
2039
 
    if (fmt.getPositiveSuffix(s) != UnicodeString((UChar)0x5e)) {
2040
 
        errln(UnicodeString("FAIL: (permill=^).getPositiveSuffix -> ") +
2041
 
              s + ", exp ^");
2042
 
    }
2043
 
    sym.setSymbol(DecimalFormatSymbols::kPerMillSymbol, (UChar)0x2030);
2044
 
 
2045
 
    fmt.applyPattern(str("\\u00A4#.00"), status);
2046
 
    failure(status, "applyPattern currency");
2047
 
    sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, "usd");
2048
 
    fmt.setDecimalFormatSymbols(sym);
2049
 
    s.remove();
2050
 
    if (fmt.format(12.5, s, pos) != UnicodeString("usd12.50")) {
2051
 
        errln(UnicodeString("FAIL: 12.5 x (currency=usd) -> ") + s +
2052
 
              ", exp usd12.50");
2053
 
    }
2054
 
    s.remove();
2055
 
    if (fmt.getPositivePrefix(s) != UnicodeString("usd")) {
2056
 
        errln(UnicodeString("FAIL: (currency=usd).getPositivePrefix -> ") +
2057
 
              s + ", exp usd");
2058
 
    }
2059
 
    sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, "$");
2060
 
 
2061
 
    fmt.applyPattern(str("\\u00A4\\u00A4#.00"), status);
2062
 
    failure(status, "applyPattern intl currency");
2063
 
    sym.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, "DOL");
2064
 
    fmt.setDecimalFormatSymbols(sym);
2065
 
    s.remove();
2066
 
    if (fmt.format(12.5, s, pos) != UnicodeString("DOL12.50")) {
2067
 
        errln(UnicodeString("FAIL: 12.5 x (intlcurrency=DOL) -> ") + s +
2068
 
              ", exp DOL12.50");
2069
 
    }
2070
 
    s.remove();
2071
 
    if (fmt.getPositivePrefix(s) != UnicodeString("DOL")) {
2072
 
        errln(UnicodeString("FAIL: (intlcurrency=DOL).getPositivePrefix -> ") +
2073
 
              s + ", exp DOL");
2074
 
    }
2075
 
    sym.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, "USD");
2076
 
*/
2077
 
    // Since the pattern logic has changed, make sure that patterns round
2078
 
    // trip properly.  Test stream in/out integrity too.
2079
 
    int32_t n;
2080
 
    const Locale* avail = NumberFormat::getAvailableLocales(n);
2081
 
    static const char* type[] = {
2082
 
        "",
2083
 
        "$ ",
2084
 
        "% ",
2085
 
    };
2086
 
    for (int i=0; i<n; ++i) {
2087
 
        for (int j=0; j<3; ++j) {
2088
 
            status = U_ZERO_ERROR;
2089
 
            NumberFormat *nf;
2090
 
            switch (j) {
2091
 
            case 0:
2092
 
                nf = NumberFormat::createInstance(avail[i], status);
2093
 
                failure(status, "createInstance");
2094
 
                break;
2095
 
            case 1:
2096
 
                nf = NumberFormat::createCurrencyInstance(avail[i], status);
2097
 
                failure(status, "createCurrencyInstance");
2098
 
                break;
2099
 
            default:
2100
 
                nf = NumberFormat::createPercentInstance(avail[i], status);
2101
 
                failure(status, "createPercentInstance");
2102
 
                break;
2103
 
            }
2104
 
            if (U_FAILURE(status)) {
2105
 
                continue;
2106
 
            }
2107
 
            DecimalFormat *df = (DecimalFormat*) nf;
2108
 
 
2109
 
            // Test toPattern/applyPattern round trip
2110
 
            UnicodeString pat;
2111
 
            df->toPattern(pat);
2112
 
            DecimalFormatSymbols symb(avail[i], status);
2113
 
            failure(status, "Construct DecimalFormatSymbols");
2114
 
            DecimalFormat f2(pat, symb, status);
2115
 
            if (failure(status,
2116
 
                        UnicodeString("Construct DecimalFormat(") + pat + ")")) {
2117
 
                continue;
2118
 
            }
2119
 
            if (*df != f2) {
2120
 
                UnicodeString l, p;
2121
 
                errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
2122
 
                      " -> \"" + pat +
2123
 
                      "\" -> \"" + f2.toPattern(p) + "\"");
2124
 
            }
2125
 
 
2126
 
            // Test toLocalizedPattern/applyLocalizedPattern round trip
2127
 
            df->toLocalizedPattern(pat);
2128
 
            f2.applyLocalizedPattern(pat, status);
2129
 
            failure(status,
2130
 
                    UnicodeString("applyLocalizedPattern(") + pat + ")");
2131
 
            if (U_FAILURE(status)) {
2132
 
                continue;
2133
 
            }
2134
 
            if (*df != f2) {
2135
 
                UnicodeString l, p;
2136
 
                errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
2137
 
                      " -> localized \"" + pat +
2138
 
                      "\" -> \"" + f2.toPattern(p) + "\"");
2139
 
            }
2140
 
 
2141
 
            delete nf;
2142
 
 
2143
 
            // Test writeObject/readObject round trip
2144
 
            // NOT ON ICU -- Java only
2145
 
        }
2146
 
    }
2147
 
}
2148
 
 
2149
 
/**
2150
 
 * May 17 1999 sync up - liu
2151
 
 * DecimalFormat.parse() fails for mulipliers 2^n.
2152
 
 */
2153
 
void NumberFormatRegressionTest::Test4216742(void) {
2154
 
    UErrorCode status = U_ZERO_ERROR;
2155
 
    DecimalFormat *fmt = (DecimalFormat*) NumberFormat::createInstance(Locale::US, status);
2156
 
    failure(status, "createInstance");
2157
 
    int32_t DATA[] = { INT32_MIN, INT32_MAX, -100000000, 100000000 };
2158
 
    int DATA_length = (int)(sizeof(DATA) / sizeof(DATA[0]));
2159
 
    for (int i=0; i<DATA_length; ++i) {
2160
 
        UnicodeString str((UnicodeString)"" + DATA[i]);
2161
 
        for (int m = 1; m <= 100; m++) {
2162
 
            fmt->setMultiplier(m);
2163
 
            Formattable num;
2164
 
            fmt->parse(str, num, status);
2165
 
            failure(status, "parse");
2166
 
            if (num.getType() != Formattable::kLong &&
2167
 
                num.getType() != Formattable::kDouble) {
2168
 
                errln(UnicodeString("FAIL: Wanted number, got ") +
2169
 
                      toString(num));
2170
 
            } else {
2171
 
                double d = num.getType() == Formattable::kDouble ?
2172
 
                    num.getDouble() : (double) num.getLong();
2173
 
                if (d > 0 != DATA[i] > 0) {
2174
 
                    errln(UnicodeString("\"") + str + "\" parse(x " +
2175
 
                          fmt->getMultiplier() +
2176
 
                          ") => " + toString(num));
2177
 
                }
2178
 
            }
2179
 
        }
2180
 
    }
2181
 
    delete fmt;
2182
 
}
2183
 
 
2184
 
/**
2185
 
 * May 17 1999 sync up - liu
2186
 
 * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
2187
 
 * digits.
2188
 
 */
2189
 
void NumberFormatRegressionTest::Test4217661(void) {
2190
 
    const double D[] = {  0.001, 1.001, 0.006,  1.006 };
2191
 
    const char*  S[] = { "0",   "1",   "0.01", "1.01" };
2192
 
    int D_length = (int)(sizeof(D) / sizeof(D[0]));
2193
 
    UErrorCode status = U_ZERO_ERROR;
2194
 
    NumberFormat *fmt = NumberFormat::createInstance(Locale::US, status);
2195
 
    failure(status, "createInstance");
2196
 
    fmt->setMaximumFractionDigits(2);
2197
 
    for (int i=0; i<D_length; i++) {
2198
 
        UnicodeString s;
2199
 
        fmt->format(D[i], s);
2200
 
        if (s != UnicodeString(S[i])) {
2201
 
            errln(UnicodeString("FAIL: Got ") + s + ", exp " + S[i]); 
2202
 
        }
2203
 
    }
2204
 
    delete fmt;
2205
 
}
2206
 
 
2207
 
/**
2208
 
 * alphaWorks upgrade
2209
 
 */
2210
 
void NumberFormatRegressionTest::Test4161100(void) {
2211
 
    UErrorCode status = U_ZERO_ERROR;
2212
 
    NumberFormat *nf = NumberFormat::createInstance(Locale::US, status);
2213
 
    failure(status, "createInstance");
2214
 
    nf->setMinimumFractionDigits(1);
2215
 
    nf->setMaximumFractionDigits(1);
2216
 
    double a = -0.09;
2217
 
    UnicodeString s;
2218
 
    nf->format(a, s);
2219
 
    UnicodeString pat;
2220
 
    logln(UnicodeString() + a + " x " +
2221
 
          ((DecimalFormat*) nf)->toPattern(pat) + " = " + s);
2222
 
    if (s != UnicodeString("-0.1")) {
2223
 
        errln("FAIL");
2224
 
    }
2225
 
    delete nf;
2226
 
}
2227
 
 
2228
 
/**
2229
 
 * June 16 1999 sync up - liu
2230
 
 * Formatting .5 rounds to "1" instead of "0". (Regression in 1.2.2 RC1)
2231
 
 */
2232
 
void NumberFormatRegressionTest::Test4243011(void) {
2233
 
    UErrorCode status = U_ZERO_ERROR;
2234
 
    DecimalFormatSymbols sym(Locale::US, status);
2235
 
    failure(status, "DecimalFormatSymbols ct");
2236
 
    DecimalFormat fmt(UnicodeString("0."), sym, status);
2237
 
    failure(status, "DecimalFormat ct");
2238
 
 
2239
 
    const double NUM[] = {  -2.5,  -1.5,  -0.5,  0.5,  1.5,  2.5,  3.5,  4.5 };
2240
 
    const char*  STR[] = { "-2.", "-2.", "-0.", "0.", "2.", "2.", "4.", "4." };
2241
 
    int32_t N = (int32_t)(sizeof(NUM) / sizeof(NUM[0]));
2242
 
 
2243
 
    for (int32_t i=0; i<N; ++i) {
2244
 
        UnicodeString str;
2245
 
        UnicodeString exp(STR[i]);
2246
 
        FieldPosition pos;
2247
 
        fmt.format(NUM[i], str, pos);
2248
 
        if (str == exp) {
2249
 
            logln(UnicodeString("Ok   ") + NUM[i] + " x 0. = " + str);
2250
 
        } else {
2251
 
            errln(UnicodeString("FAIL ") + NUM[i] + " x 0. = " + str +
2252
 
                  ", want " + exp);
2253
 
        }
2254
 
    }
2255
 
}
2256
 
 
2257
 
/**
2258
 
 * June 16 1999 sync up - liu
2259
 
 * format(0.0) gives "0.1" if preceded by parse("99.99").
2260
 
 * (Regression in 1.2.2 RC1)
2261
 
 */
2262
 
void NumberFormatRegressionTest::Test4243108(void) {
2263
 
    UErrorCode status = U_ZERO_ERROR;
2264
 
    DecimalFormatSymbols sym(Locale::US, status);
2265
 
    failure(status, "DecimalFormatSymbols ct");
2266
 
    DecimalFormat fmt(UnicodeString("#.#"), sym, status);
2267
 
    failure(status, "DecimalFormat ct");
2268
 
 
2269
 
    UnicodeString str;
2270
 
    FieldPosition pos;
2271
 
 
2272
 
    fmt.format(0.0, str, pos);
2273
 
    UnicodeString exp("0");
2274
 
    if (str == exp) {
2275
 
        logln(UnicodeString("Ok   0.0 x #.# = ") + str);
2276
 
    } else {
2277
 
        errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
2278
 
              ", want " + exp);
2279
 
    }
2280
 
 
2281
 
    str = "99.99";
2282
 
    Formattable val;
2283
 
    fmt.parse(str, val, status);
2284
 
    failure(status, "DecimalFormat.parse(99.99)");
2285
 
    if (val.getType() == Formattable::kDouble &&
2286
 
        val.getDouble() == 99.99) {
2287
 
        logln(UnicodeString("Ok   99.99 / #.# = ") + toString(val));
2288
 
    } else {
2289
 
        errln(UnicodeString("FAIL 99.99 / #.# = ") + toString(val) +
2290
 
              ", want " + 99.99);
2291
 
    }
2292
 
 
2293
 
    str.remove();
2294
 
    fmt.format(0.0, str, pos);
2295
 
    if (str == exp) {
2296
 
        logln(UnicodeString("Ok   0.0 x #.# = ") + str);
2297
 
    } else {
2298
 
        errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
2299
 
              ", want " + exp);
2300
 
    }
2301
 
}
2302
 
 
2303
 
 
2304
 
/**
2305
 
 * DateFormat should call setIntegerParseOnly(TRUE) on adopted
2306
 
 * NumberFormat objects.
2307
 
 */
2308
 
void NumberFormatRegressionTest::TestJ691(void) {
2309
 
    UErrorCode status = U_ZERO_ERROR;
2310
 
    Locale loc("fr", "CH");
2311
 
 
2312
 
    // set up the input date string & expected output
2313
 
    UnicodeString udt("11.10.2000", "");
2314
 
    UnicodeString exp("11.10.00", "");
2315
 
 
2316
 
    // create a Calendar for this locale
2317
 
    Calendar *cal = Calendar::createInstance(loc, status);
2318
 
    if (U_FAILURE(status)) {
2319
 
        errln("FAIL: Calendar::createInstance() returned " + (UnicodeString)u_errorName(status));
2320
 
        return;
2321
 
    }
2322
 
 
2323
 
    // create a NumberFormat for this locale
2324
 
    NumberFormat *nf = NumberFormat::createInstance(loc, status);
2325
 
    if (U_FAILURE(status)) {
2326
 
        errln("FAIL: NumberFormat::createInstance() returned " + (UnicodeString)u_errorName(status));
2327
 
        return;
2328
 
    }
2329
 
 
2330
 
    // *** Here's the key: We don't want to have to do THIS:
2331
 
    // nf->setParseIntegerOnly(TRUE);
2332
 
 
2333
 
    // create the DateFormat
2334
 
    DateFormat *df = DateFormat::createDateInstance(DateFormat::kShort, loc);
2335
 
    if (U_FAILURE(status)) {
2336
 
        errln("FAIL: DateFormat::createInstance() returned " + (UnicodeString)u_errorName(status));
2337
 
        return;
2338
 
    }
2339
 
 
2340
 
    df->adoptCalendar(cal);
2341
 
    df->adoptNumberFormat(nf);
2342
 
 
2343
 
    // set parsing to lenient & parse
2344
 
    df->setLenient(TRUE);
2345
 
    UDate ulocdat = df->parse(udt, status);
2346
 
 
2347
 
    // format back to a string
2348
 
    UnicodeString outString;
2349
 
    df->format(ulocdat, outString);
2350
 
 
2351
 
    if (outString != exp) {
2352
 
        errln("FAIL: " + udt + " => " + outString);
2353
 
    }
2354
 
 
2355
 
    delete df;
2356
 
}
2357