1
/********************************************************************
3
* Copyright (c) 1997-2001, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
9
#include <float.h> // DBL_MIN, DBL_MAX
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"
18
// *****************************************************************************
19
// class NumberFormatRegressionTest
20
// *****************************************************************************
22
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
25
NumberFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
27
// if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
91
default: name = ""; break;
96
NumberFormatRegressionTest::failure(UErrorCode status, const UnicodeString& msg)
98
if(U_FAILURE(status)) {
99
errln(UnicodeString("FAIL: ", "") + msg
100
+ UnicodeString(" failed, error ", "") + UnicodeString(u_errorName(status), ""));
108
* Convert Java-style strings with \u Unicode escapes into UnicodeString objects
110
inline UnicodeString str(const char *input)
112
return CharsToUnicodeString(input);
116
* NumberFormat.equals comparing with null should always return false.
118
// {sfb} kind of silly in C++, just checking for new success
119
void NumberFormatRegressionTest::Test4075713(void)
122
MyNumberFormatTest *tmp = new MyNumberFormatTest();
124
logln("NumberFormat.equals passed");
125
/*} catch (NullPointerException e) {
126
errln("(new MyNumberFormatTest()).equals(null) throws unexpected exception");
133
* NumberFormat.equals comparing two obj equal even the setGroupingUsed
136
void NumberFormatRegressionTest::Test4074620(void)
139
MyNumberFormatTest *nf1 = new MyNumberFormatTest();
140
MyNumberFormatTest *nf2 = new MyNumberFormatTest();
142
nf1->setGroupingUsed(FALSE);
143
nf2->setGroupingUsed(TRUE);
146
errln("Test for bug 4074620 failed");
148
logln("Test for bug 4074620 passed.");
156
* DecimalFormat.format() incorrectly uses maxFractionDigits setting.
159
void NumberFormatRegressionTest::Test4088161 (void)
161
UErrorCode status = U_ZERO_ERROR;
162
DecimalFormat *df = new DecimalFormat(status);
163
failure(status, "new DecimalFormat");
165
df->setMinimumFractionDigits(0);
166
df->setMaximumFractionDigits(16);
168
FieldPosition fp1(0);
169
logln(UnicodeString("d = ") + d);
170
logln("maxFractionDigits = " + df->getMaximumFractionDigits());
172
logln(" format(d) = '" + df->format(d, sBuf1, fp1) + "'");
173
df->setMaximumFractionDigits(17);
175
FieldPosition fp2(0);
176
logln("maxFractionDigits = " + df->getMaximumFractionDigits());
177
sBuf2 = df->format(d, sBuf2, fp2);
179
errln(" format(d) = '" + sBuf2 + "'");
185
* DecimalFormatSymbols should be cloned in the ctor DecimalFormat.
186
* DecimalFormat(String, DecimalFormatSymbols).
188
void NumberFormatRegressionTest::Test4087245 (void)
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");
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));
209
errln("Test for bug 4087245 failed");
216
* DecimalFormat.format() incorrectly formats 0.0
218
void NumberFormatRegressionTest::Test4087535 (void)
220
UErrorCode status = U_ZERO_ERROR;
221
DecimalFormat *df = new DecimalFormat(status);
222
failure(status, "new DecimalFormat");
223
df->setMinimumIntegerDigits(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 + "'");
232
buffer = df->format(n, buffer, pos);
233
if (buffer.length() == 0)
234
errln(/*n + */": '" + buffer + "'");
240
* DecimalFormat.format fails when groupingSize is set to 0.
242
// {sfb} how do I tell if this worked? --> FieldPosition doesn't change ??
243
void NumberFormatRegressionTest::Test4088503 (void)
245
UErrorCode status = U_ZERO_ERROR;
246
DecimalFormat *df = new DecimalFormat(status);
247
failure(status, "new DecimalFormat");
248
df->setGroupingSize(0);
250
FieldPosition fp(FieldPosition::DONT_CARE);
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.");
262
* NumberFormat.getCurrencyInstance is wrong.
264
void NumberFormatRegressionTest::Test4066646 (void)
266
assignFloatValue(2.04f);
267
assignFloatValue(2.03f);
268
assignFloatValue(2.02f);
269
assignFloatValue(0.0f);
273
NumberFormatRegressionTest::assignFloatValue(float returnfloat)
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);
281
UnicodeString stringValue;
282
stringValue = nfcommon->format(returnfloat, stringValue);
283
logln(" DISPLAYVALUE " + stringValue);
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+")");
296
} // End Of assignFloatValue()
299
* DecimalFormat throws exception when parsing "0"
301
void NumberFormatRegressionTest::Test4059870(void)
303
UErrorCode status = U_ZERO_ERROR;
304
DecimalFormat *format = new DecimalFormat("00", status);
305
failure(status, "new Decimalformat");
309
format->parse(UnicodeString("0"), result, status);
310
failure(status, "format->parse");
313
catch (Exception e) {
314
errln("Test for bug 4059870 failed : " + e);
320
* DecimalFormatSymbol.equals should always return false when
321
* comparing with null.
323
// {sfb} this is silly in C++
324
void NumberFormatRegressionTest::Test4083018 (void)
326
UErrorCode status = U_ZERO_ERROR;
327
DecimalFormatSymbols *dfs = new DecimalFormatSymbols(status);
328
failure(status, "new DecimalFormatSymbols");
331
logln("Test Passed!");
333
errln("Test for bug 4083018 failed");
334
/*} catch (Exception foo) {
335
errln("Test for bug 4083018 failed => Message : " + foo.getMessage());
342
* DecimalFormat does not round up correctly.
344
void NumberFormatRegressionTest::Test4071492 (void)
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);
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");
357
errln("FAIL: Expected " + expected);
363
* A space as a group separator for localized pattern causes
364
* wrong format. WorkAround : use non-breaking space.
366
void NumberFormatRegressionTest::Test4086575(void)
368
UErrorCode status = U_ZERO_ERROR;
369
NumberFormat *nf1 = NumberFormat::createInstance(Locale::getFrance(), status);
370
failure(status, "NumberFormat::createInstance");
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.");
379
DecimalFormat *nf = (DecimalFormat*) nf1;
381
logln("nf toPattern1: " + nf->toPattern(temp));
382
logln("nf toLocPattern1: " + nf->toLocalizedPattern(temp));
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));
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)
395
// Space as group separator
397
logln("...applyLocalizedPattern # ###,00;(# ###,00) ");
399
//nf->applyLocalizedPattern("#\u00a0###,00;(#\u00a0###,00)");
401
0x23, 0x00a0, 0x23, 0x23, 0x23, 0x2c, 0x30, 0x30, 0x3b,
402
0x28, 0x23, 0x00a0, 0x23, 0x23, 0x23, 0x2c, 0x30, 0x30, 0x29
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"))
413
0x31, 0x00a0, 0x32, 0x33, 0x34, 0x2c, 0x30, 0x30
415
UnicodeString cc(c, 8, 8);
417
errln("nf : " + buffer); // Expect 1 234,00
420
buffer = nf->format((int32_t)-1234, buffer, pos);
422
0x28, 0x31, 0x00a0, 0x32, 0x33, 0x34, 0x2c, 0x30, 0x30, 0x29
424
UnicodeString cc1(c1, 10, 10);
426
errln("nf : " + buffer); // Expect (1 234,00)
428
// Erroneously prints:
435
* DecimalFormat.parse returns wrong value
437
// {sfb} slightly converted into a round-trip test, since in C++
438
// there is no Double.toString()
439
void NumberFormatRegressionTest::Test4068693(void)
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");
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")) {
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());
462
/* @bug 4069754, 4067878
463
* null pointer thrown when accessing a deserialized DecimalFormat
466
// {sfb} doesn't apply in C++
467
void NumberFormatRegressionTest::Test4069754(void)
470
myformat it = new myformat();
472
FileOutputStream ostream = new FileOutputStream("t.tmp");
473
ObjectOutputStream p = new ObjectOutputStream(ostream);
478
FileInputStream istream = new FileInputStream("t.tmp");
479
ObjectInputStream p2 = new ObjectInputStream(istream);
480
myformat it2 = (myformat)p2.readObject();
484
} catch (Exception foo) {
485
errln("Test for bug 4069754 or 4057878 failed => Exception: " + foo.getMessage());
490
* DecimalFormat.applyPattern(String) allows illegal patterns
492
void NumberFormatRegressionTest::Test4087251 (void)
494
UErrorCode status = U_ZERO_ERROR;
495
DecimalFormat *df = new DecimalFormat(status);
496
failure(status, "new DecimalFormat");
498
df->applyPattern(UnicodeString("#.#.#"), status);
499
if( ! U_FAILURE(status))
500
errln("df->applyPattern with illegal pattern didn't fail");
502
logln("toPattern() returns \"" + df->toPattern(temp) + "\"");
503
//errln("applyPattern(\"#.#.#\") doesn't throw IllegalArgumentException");
504
/*} catch (IllegalArgumentException e) {
505
logln("Caught Illegal Argument Error !");
507
// Second test; added 5/11/98 when reported to fail on 1.2b3
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");
522
* DecimalFormat.format() loses precision
524
void NumberFormatRegressionTest::Test4090489 (void)
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.
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);
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);
549
* DecimalFormat.format() loses precision
551
void NumberFormatRegressionTest::Test4090504 (void)
554
logln(UnicodeString("d = ") + d);
555
UErrorCode status = U_ZERO_ERROR;
556
DecimalFormat *df = new DecimalFormat(status);
557
failure(status, "new DecimalFormat");
559
FieldPosition fp(FieldPosition::DONT_CARE);
561
for (int i = 17; i <= 20; i++) {
562
df->setMaximumFractionDigits(i);
563
//sb = new StringBuffer("");
565
logln(" getMaximumFractionDigits() = " + i);
566
logln(" formated: " + df->format(d, sb, fp));
568
/*} catch (Exception foo) {
569
errln("Bug 4090504 regression test failed. Message : " + foo.getMessage());
575
* DecimalFormat.parse(String str, ParsePosition pp) loses precision
577
void NumberFormatRegressionTest::Test4095713 (void)
579
UErrorCode status = U_ZERO_ERROR;
580
DecimalFormat *df = new DecimalFormat(status);
581
failure(status, "new DecimalFOrmat");
582
UnicodeString str("0.1234");
584
//Double d1 = new Double(str);
585
//Double d2 = (Double) df.parse(str, new ParsePosition(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());
596
* DecimalFormat.parse() fails when multiplier is not set to 1
598
// {sfb} not sure what to do with this one
599
void NumberFormatRegressionTest::Test4092561 (void)
601
UErrorCode status = U_ZERO_ERROR;
602
DecimalFormat *df = new DecimalFormat(status);
603
failure(status, "new DecimalFormat");
605
// {sfb} going to cheat here and use sprintf ??
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.");
618
* DecimalFormat: Negative format ignored.
620
void NumberFormatRegressionTest::Test4092480 (void)
622
UErrorCode status = U_ZERO_ERROR;
623
DecimalFormat *dfFoo = new DecimalFormat(UnicodeString("000"), status);
624
failure(status, "new DecimalFormat");
627
dfFoo->applyPattern("0000;-000", status);
628
failure(status, "dfFoo->applyPattern");
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));
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));
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());
662
* NumberFormat.getCurrencyInstance() produces format that uses
663
* decimal separator instead of monetary decimal separator.
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!
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!");
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");
685
df->setMinimumIntegerDigits(1);
686
df->setMinimumFractionDigits(2);
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 + '"');
698
errln(UnicodeString("FAIL: 1.23 -> \"") + str + "\", should contain \"" +
700
"\" and not \"" + decStr + '"');
706
* Number format data rounding errors for locale FR
708
void NumberFormatRegressionTest::Test4070798 (void)
710
NumberFormat *formatter;
711
UnicodeString tempString;
714
String expectedDefault = "-5\u00a0789,987";
715
String expectedCurrency = "5\u00a0789,98 F";
716
String expectedPercent = "-578\u00a0998%";
719
0x2d, 0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x38, 0x38
722
0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x39, 0x20, 0x46
725
0x2d, 0x35, 0x37, 0x38, 0x00a0, 0x39, 0x39, 0x39, 0x25
727
UnicodeString expectedDefault(chars1, 10, 10);
728
UnicodeString expectedCurrency(chars2, 10, 10);
729
UnicodeString expectedPercent(chars3, 9, 9);
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);
736
if (tempString == expectedDefault) {
737
logln ("Bug 4070798 default test passed.");
739
errln(UnicodeString("Failed:") +
740
" Expected " + expectedDefault +
741
" Received " + tempString );
745
formatter = NumberFormat::createCurrencyInstance(Locale("fr_FR_PREEURO"), status);
746
failure(status, "NumberFormat::createCurrencyInstance");
748
tempString = formatter->format( 5789.9876, tempString );
750
if (tempString == expectedCurrency) {
751
logln ("Bug 4070798 currency test passed.");
753
errln(UnicodeString("Failed:") +
754
" Expected " + expectedCurrency +
755
" Received " + tempString );
759
formatter = NumberFormat::createPercentInstance(Locale("fr_FR_PREEURO"), status);
760
failure(status, "NumberFormat::createPercentInstance");
762
tempString = formatter->format (-5789.9876, tempString);
764
if (tempString == expectedPercent) {
765
logln ("Bug 4070798 percentage test passed.");
767
errln(UnicodeString("Failed:") +
768
" Expected " + expectedPercent +
769
" Received " + tempString );
775
* Data rounding errors for French (Canada) locale
777
void NumberFormatRegressionTest::Test4071005 (void)
779
NumberFormat *formatter;
780
UnicodeString tempString;
782
String expectedDefault = "-5\u00a0789,987";
783
String expectedCurrency = "5\u00a0789,98 $";
784
String expectedPercent = "-578\u00a0998%";
787
0x2d, 0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x38, 0x38
790
0x35, 0x00a0, 0x37, 0x38, 0x39, 0x2c, 0x39, 0x39, 0x20, 0x24
793
0x2d, 0x35, 0x37, 0x38, 0x00a0, 0x39, 0x39, 0x39, 0x25
795
UnicodeString expectedDefault(chars1, 10, 10);
796
UnicodeString expectedCurrency(chars2, 10, 10);
797
UnicodeString expectedPercent(chars3, 9, 9);
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);
804
if (tempString == expectedDefault) {
805
logln ("Bug 4071005 default test passed.");
807
errln(UnicodeString("Failed:") +
808
" Expected " + expectedDefault +
809
" Received " + tempString );
813
formatter = NumberFormat::createCurrencyInstance(Locale::getCanadaFrench(), status);
814
failure(status, "NumberFormat::createCurrencyInstance");
816
tempString = formatter->format( 5789.9876, tempString );
818
if (tempString == expectedCurrency) {
819
logln ("Bug 4071005 currency test assed.");
821
errln(UnicodeString("Failed:") +
822
" Expected " + expectedCurrency +
823
" Received " + tempString );
827
formatter = NumberFormat::createPercentInstance(Locale::getCanadaFrench(), status);
828
failure(status, "NumberFormat::createPercentInstance");
830
tempString = formatter->format (-5789.9876, tempString);
832
if (tempString == expectedPercent) {
833
logln ("Bug 4071005 percentage test passed.");
835
errln(UnicodeString("Failed:") +
836
" Expected " + expectedPercent +
837
" Received " + tempString );
844
* Data rounding errors for German (Germany) locale
846
void NumberFormatRegressionTest::Test4071014 (void)
848
NumberFormat *formatter;
849
UnicodeString tempString;
851
String expectedDefault = "-5.789,987";
852
String expectedCurrency = "5.789,98 DM";
853
String expectedPercent = "-578.998%";
855
UnicodeString expectedDefault("-5.789,988");
856
UnicodeString expectedCurrency("5.789,99 DM");
857
UnicodeString expectedPercent("-578.999%");
859
UErrorCode status = U_ZERO_ERROR;
860
formatter = NumberFormat::createInstance(Locale("de_DE_PREEURO"), status);
861
failure(status, "NumberFormat::createNumberInstance");
863
tempString = formatter->format (-5789.9876, tempString);
865
if (tempString == expectedDefault) {
866
logln ("Bug 4071014 default test passed.");
868
errln(UnicodeString("Failed:") +
869
" Expected " + expectedDefault +
870
" Received " + tempString );
874
formatter = NumberFormat::createCurrencyInstance(Locale("de_DE_PREEURO"), status);
875
failure(status, "NumberFormat::createCurrencyInstance");
877
tempString = formatter->format( 5789.9876, tempString );
879
if (tempString == expectedCurrency) {
880
logln ("Bug 4071014 currency test assed.");
882
errln(UnicodeString("Failed:") +
883
" Expected " + expectedCurrency +
884
" Received " + tempString );
888
formatter = NumberFormat::createPercentInstance(Locale::getGermany(), status);
889
failure(status, "NumberFormat::createPercentInstance");
891
tempString = formatter->format (-5789.9876, tempString);
893
if (tempString == expectedPercent) {
894
logln ("Bug 4071014 percentage test passed.");
896
errln(UnicodeString("Failed:") +
897
" Expected " + expectedPercent +
898
" Received " + tempString );
904
* Data rounding errors for Italian locale number formats
906
void NumberFormatRegressionTest::Test4071859 (void)
908
NumberFormat *formatter;
909
UnicodeString tempString;
911
String expectedDefault = "-5.789,987";
912
String expectedCurrency = "-L. 5.789,98";
913
String expectedPercent = "-578.998%";
915
UnicodeString expectedDefault("-5.789,988");
916
UnicodeString expectedCurrency("-L. 5.790");
917
UnicodeString expectedPercent("-578.999%");
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);
924
if (tempString == expectedDefault) {
925
logln ("Bug 4071859 default test passed.");
927
errln(UnicodeString("Failed:") +
928
" Expected " + expectedDefault +
929
" Received " + tempString );
933
formatter = NumberFormat::createCurrencyInstance(Locale("it_IT_PREEURO"), status);
934
failure(status, "NumberFormat::createCurrencyInstance");
936
tempString = formatter->format( -5789.9876, tempString );
938
if (tempString == expectedCurrency) {
939
logln ("Bug 4071859 currency test assed.");
941
errln(UnicodeString("Failed:") +
942
" Expected " + expectedCurrency +
943
" Received " + tempString );
947
formatter = NumberFormat::createPercentInstance(Locale("it_IT_PREEURO"), status);
948
failure(status, "NumberFormat::createPercentInstance");
950
tempString = formatter->format (-5789.9876, tempString);
952
if (tempString == expectedPercent) {
953
logln ("Bug 4071859 percentage test passed.");
955
errln(UnicodeString("Failed:") +
956
" Expected " + expectedPercent +
957
" Received " + tempString );
963
* Test rounding for nearest even.
965
void NumberFormatRegressionTest::Test4093610(void)
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);
974
roundingTest(df, 12.452,s);
976
roundingTest(df, 12.55, s);
977
roundingTest(df, 12.65, s);
979
roundingTest(df, 12.652,s);
981
roundingTest(df, 12.75, s);
982
roundingTest(df, 12.752,s);
983
roundingTest(df, 12.85, s);
985
roundingTest(df, 12.852,s);
987
roundingTest(df, 12.95, s);
988
roundingTest(df, 12.952,s);
993
void NumberFormatRegressionTest::roundingTest(DecimalFormat *df, double x, UnicodeString& expected)
996
FieldPosition pos(FieldPosition::DONT_CARE);
997
out = df->format(x, out, pos);
998
logln(UnicodeString("") + x + " formats with 1 fractional digits to " + out);
1000
errln("FAIL: Expected " + expected);
1003
* Tests the setMaximumFractionDigits limit.
1005
void NumberFormatRegressionTest::Test4098741(void)
1008
UErrorCode status = U_ZERO_ERROR;
1009
NumberFormat *fmt = NumberFormat::createPercentInstance(status);
1010
fmt->setMaximumFractionDigits(20);
1012
logln(fmt->format(.001, temp));
1013
/*} catch (Exception foo) {
1014
errln("Bug 4098471 failed with exception thrown : " + foo.getMessage());
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.
1023
void NumberFormatRegressionTest::Test4074454(void)
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.");
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.
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
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.
1063
void NumberFormatRegressionTest::Test4099404(void)
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");
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");
1086
* DecimalFormat.applyPattern doesn't set minimum integer digits
1088
void NumberFormatRegressionTest::Test4101481(void)
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());
1097
/* @bug 4052223 (API addition request A27)
1098
* Tests ParsePosition.setErrorPosition() and ParsePosition.getErrorPosition().
1100
void NumberFormatRegressionTest::Test4052223(void)
1103
UErrorCode status = U_ZERO_ERROR;
1104
DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,#00.00"), status);
1105
failure(status, "new DecimalFormat");
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());
1116
* API tests for API addition request A9.
1118
void NumberFormatRegressionTest::Test4061302(void)
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(""))
1130
errln("getCurrencySymbols failed, got empty string.");
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.");
1146
monDecSeparatorStr.remove();
1147
monDecSeparatorStr.append(monDecSeparator);
1148
logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparatorStr);
1153
* API tests for API addition request A23. FieldPosition.getBeginIndex and
1154
* FieldPosition.getEndIndex.
1156
void NumberFormatRegressionTest::Test4062486(void)
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()*/);
1176
* DecimalFormat.parse incorrectly works with a group separator.
1178
void NumberFormatRegressionTest::Test4108738(void)
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");
1187
ParsePosition pp(0);
1188
df->parse(text, num, pp);
1190
// {sfb} how to do this (again) ?
1191
// shouldn't just be another round-trip test, should it?
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);
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);
1212
* DecimalFormat.format() incorrectly formats negative doubles.
1214
void NumberFormatRegressionTest::Test4106658(void)
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;
1223
FieldPosition pos(FieldPosition::DONT_CARE);
1226
d1 = 0.0 * -1.0; // old HPUX compiler ignores volatile keyword
1228
d1 *= -1.0; // Some compilers have a problem with defining -0.0
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);
1235
df->format(d2, buffer, pos);
1236
if (buffer != UnicodeString("-0")) // Corrected; see 4147706
1237
errln(UnicodeString("") + d2 + " is formatted as " + buffer);
1243
* DecimalFormat.parse returns 0 if string parameter is incorrect.
1245
void NumberFormatRegressionTest::Test4106662(void)
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);
1254
logln("pattern: \"" + df->toPattern(temp) + "\"");
1256
df->parse(text, num, pos1);
1257
if (pos1 == ParsePosition(0)/*num != null*/) {
1258
errln(UnicodeString("Test Failed: \"") + text + "\" is parsed as " /*+ num*/);
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*/);
1271
/* @bug 4114639 (duplicate of 4106662)
1272
* NumberFormat.parse doesn't return null
1274
void NumberFormatRegressionTest::Test4114639(void)
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);
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
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.
1294
void NumberFormatRegressionTest::Test4106664(void)
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);
1309
FieldPosition pos(FieldPosition::DONT_CARE);
1310
logln("formated: " +
1311
df->format(n, temp, pos));
1314
sprintf(buf, "%g", bigN);
1315
//logln("expected: " + bigN.toString());
1316
logln(UnicodeString("expected: ") + buf);
1320
/* @bug 4106667 (duplicate of 4106658)
1321
* DecimalFormat.format incorrectly formats -0.0.
1323
void NumberFormatRegressionTest::Test4106667(void)
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
1332
UnicodeString buffer;
1333
FieldPosition pos(FieldPosition::DONT_CARE);
1335
logln("pattern: \"" + df->toPattern(temp) + "\"");
1337
d = 0.0 * -1.0; // old HPUX compiler ignores volatile keyword
1339
d *= -1.0; // Some compilers have a problem with defining -0.0
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);
1350
* DecimalFormat.setMaximumIntegerDigits() works incorrectly.
1353
# define MAX_INT_DIGITS 70
1355
# define MAX_INT_DIGITS 128
1358
void NumberFormatRegressionTest::Test4110936(void)
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());
1373
* Locale data should use generic currency symbol
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
1379
void NumberFormatRegressionTest::Test4122840(void)
1382
const Locale *locales = Locale::getAvailableLocales(count);
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)");
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
1397
UnicodeString pattern = numPat.getStringEx(1, status);
1398
failure(status, "rb->getStringArray");
1400
UChar fo[] = { 0x00A4 };
1401
UnicodeString foo(fo, 1, 1);
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:" +
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");
1416
UnicodeString result1;
1417
FieldPosition pos(FieldPosition::DONT_CARE);
1418
result1 = fmt1->format(1.111, result1, pos);
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
1425
UChar ba[] = { 0x002E/*'.'*/ };
1426
UnicodeString bar(ba, 1, 1);
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));
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();
1443
buf.replace(j, /*j+*/1, symbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol));
1444
j += symbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol).length() - 1;
1449
DecimalFormat *fmt2 = new DecimalFormat(buf, *symbols, status);
1450
failure(status, "new DecimalFormat");
1452
UnicodeString result2;
1453
fmt2->format(1.111, result2, pos);
1455
if (result1 != result2) {
1456
errln("Results for " + (UnicodeString)(locales[i].getName()) + " differ: " +
1457
result1 + " vs " + result2);
1470
* DecimalFormat.format() delivers wrong string.
1472
void NumberFormatRegressionTest::Test4125885(void)
1474
UErrorCode status = U_ZERO_ERROR;
1475
double rate = 12.34;
1476
DecimalFormat *formatDec = new DecimalFormat ("000.00", status);
1477
failure(status, "new DecimalFormat");
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");
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%");
1500
* DecimalFormat produces extra zeros when formatting numbers.
1502
void NumberFormatRegressionTest::Test4134034(void)
1504
UErrorCode status = U_ZERO_ERROR;
1505
DecimalFormat *nf = new DecimalFormat("##,###,###.00", status);
1506
failure(status, "new DecimalFormat");
1509
FieldPosition pos(FieldPosition::DONT_CARE);
1510
f = nf->format(9.02, f, pos);
1511
if (f == UnicodeString("9.02"))
1514
errln("9.02 -> " + f + "; want 9.02");
1517
f = nf->format((int32_t)0, f, pos);
1518
if (f == UnicodeString(".00"))
1521
errln("0 -> " + f + "; want .00");
1528
* CANNOT REPRODUCE - This bug could not be reproduced. It may be
1529
* a duplicate of 4134034.
1531
* JDK 1.1.6 Bug, did NOT occur in 1.1.5
1532
* Possibly related to bug 4125885.
1534
* This class demonstrates a regression in version 1.1.6
1535
* of DecimalFormat class.
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
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'
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")
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]);
1573
logln("Ok: 1.2 x " + DATA[i] + " = " + result);
1582
* Empty pattern produces double negative prefix.
1584
void NumberFormatRegressionTest::Test4140009(void)
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");
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);
1597
s = f->format(-123.456, s, pos);
1598
if (s != UnicodeString("-123.456"))
1599
errln("Fail: Format empty pattern x -123.456 => " + s);
1605
* BigDecimal numbers get their fractions truncated by NumberFormat.
1607
// {sfb} not pertinent in C++ ??
1608
void NumberFormatRegressionTest::Test4141750(void) {
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);
1615
catch (Exception e) {
1616
errln(e.toString());
1617
e.printStackTrace();
1623
* DecimalFormat toPattern() doesn't quote special characters or handle
1626
void NumberFormatRegressionTest::Test4145457() {
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");
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;
1642
UnicodeString PATS [] = {
1643
UnicodeString("#.00 'num''ber'"), UnicodeString("''#.00''")
1646
for (int32_t i=0; i<2; ++i) {
1647
nf->applyPattern(PATS[i], status);
1648
failure(status, "nf->applyPattern");
1650
FieldPosition pos(FieldPosition::DONT_CARE);
1651
out = nf->format(pi, out, pos);
1653
pat = nf->toPattern(pat);
1655
ParsePosition pp(0);
1656
nf->parse(out, num, pp);
1657
double val = num.getDouble();
1659
nf->applyPattern(pat, status);
1660
failure(status, "nf->applyPattern");
1662
out2 = nf->format(pi, out2, pos);
1664
pat2 = nf->toPattern(pat2);
1665
nf->parse(out2, num, pp);
1666
double val2 = num.getDouble();
1669
errln("Fail with \"" + PATS[i] + "\": Patterns should concur, \"" +
1670
pat + "\" vs. \"" + pat2 + "\"");
1672
logln("Ok \"" + PATS[i] + "\" toPattern() -> \"" + pat + '"');
1674
if (val == val2 && out == out2) {
1675
logln(UnicodeString("Ok ") + pi + " x \"" + PATS[i] + "\" -> \"" +
1676
out + "\" -> " + val + " -> \"" +
1677
out2 + "\" -> " + val2);
1680
errln(UnicodeString("Fail ") + pi + " x \"" + PATS[i] + "\" -> \"" +
1681
out + "\" -> " + val + " -> \"" +
1682
out2 + "\" -> " + val2);
1686
catch (ParseException e) {
1687
errln("Fail: " + e);
1688
e.printStackTrace();
1696
* DecimalFormat.applyPattern() sets minimum integer digits incorrectly.
1698
* This bug is a duplicate of 4139344, which is a duplicate of 4134300
1700
void NumberFormatRegressionTest::Test4147295(void)
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);
1713
errln(" new pattern: " + sdf->toPattern(temp));
1715
logln("Test passed");
1716
logln(" Minimum integer digits : " + minIntDig);
1723
* DecimalFormat formats -0.0 as +0.0
1724
* See also older related bug 4106658, 4106667
1726
void NumberFormatRegressionTest::Test4147706(void)
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");
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;
1740
d1 = 0.0 * -1.0; // old HPUX compiler ignores volatile keyword
1742
d1 *= -1.0; // Some compilers have a problem with defining -0.0
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 + '"');
1750
if (f2 != UnicodeString("-0.0")) {
1751
errln(UnicodeString("") + d2 + UnicodeString(" x \"") + df->toPattern(temp) + "\" is formatted as \"" + f2 + '"');
1758
// Not applicable, since no serialization in C++
1759
/*class myformat implements Serializable
1761
DateFormat _dateFormat = DateFormat.getDateInstance();
1765
GregorianCalendar calendar = new GregorianCalendar();
1766
Date t = calendar.getTime();
1767
String nowStr = _dateFormat.format(t);
1774
* NumberFormat cannot format Double.MAX_VALUE
1776
// TODO: make this test actually test something
1778
NumberFormatRegressionTest::Test4162198(void)
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");
1788
f->setMaximumFractionDigits(INT32_MAX);
1789
f->setMaximumIntegerDigits(INT32_MAX);
1792
logln(UnicodeString("The number ") + dbl + " formatted to " + s);
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();
1803
//logln("The string " + s + " parsed as " + n);
1805
if(n.getDouble() != dbl) {
1806
errln("Round trip failure");
1813
* NumberFormat does not parse negative zero.
1816
NumberFormatRegressionTest::Test4162852(void)
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");
1831
f->parse(s, n, status);
1832
if(U_FAILURE(status))
1833
errln("Couldn't parse!");
1834
double e = n.getDouble();
1835
logln(UnicodeString("") +
1837
'"' + s + '"' + " -> " + e);
1838
#if (defined(OS390) && !defined(IEEE_754)) || defined(OS400)
1841
if (e != 0.0 || 1.0/e > 0.0) {
1843
logln("Failed to parse negative zero");
1849
static double _u_abs(double a) { return a<0?-a:a; }
1852
* May 17 1999 sync up - liu
1854
* NumberFormat truncates data
1856
void NumberFormatRegressionTest::Test4167494(void) {
1857
UErrorCode status = U_ZERO_ERROR;
1858
NumberFormat *fmt = NumberFormat::createInstance(Locale::US, status);
1859
failure(status, "NumberFormat::createInstance");
1861
double a = DBL_MAX * 0.99; // DBL_MAX itself overflows to +Inf
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");
1872
errln(UnicodeString("") + a + " -> \"" + s + "\" -> " +
1873
toString(num) + " FAIL");
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.
1885
* May 17 1999 sync up - liu
1887
* DecimalFormat.parse() fails when ParseIntegerOnly set to true
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");
1897
DecimalFormat *df = (DecimalFormat*) nf;
1898
df->setParseIntegerOnly(TRUE);
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));
1910
* May 17 1999 sync up - liu
1911
* toPattern only puts the first grouping separator in.
1913
void NumberFormatRegressionTest::Test4176114(void) {
1914
const char* DATA[] = {
1916
"000", "#000", // No grouping
1917
"#000", "#000", // No grouping
1921
"00,000", "#00,000",
1922
"000,000", "#,000,000",
1923
"0,000,000,000,000.0000", "#0,000,000,000,000.0000", // Reported
1925
int DATA_length = (int)(sizeof(DATA) / sizeof(DATA[0]));
1926
UErrorCode status = U_ZERO_ERROR;
1928
for (int i=0; i<DATA_length; i+=2) {
1929
DecimalFormat df(DATA[i], status);
1930
failure(status, "DecimalFormat constructor");
1932
UnicodeString exp(DATA[i+1]);
1934
errln(UnicodeString("FAIL: ") + DATA[i] + " -> " +
1935
s + ", want " + exp);
1941
* May 17 1999 sync up - liu
1943
* DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
1945
void NumberFormatRegressionTest::Test4179818(void) {
1946
const char* DATA[] = {
1947
// Input Pattern Expected output
1948
"1.2511", "#.#", "1.3",
1949
"1.2501", "#.#", "1.3",
1952
int DATA_length = (int)(sizeof(DATA) / sizeof(DATA[0]));
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");
1971
fmt.format(in, out, pos);
1973
logln(UnicodeString("Ok: ") + in + " x " + pat + " = " + out);
1975
errln(UnicodeString("FAIL: ") + in + " x " + pat + " = " + out +
1976
", expected " + exp);
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
1988
void NumberFormatRegressionTest::Test4212072(void) {
1989
UErrorCode status = U_ZERO_ERROR;
1990
DecimalFormatSymbols sym(Locale::US, status);
1992
failure(status, "DecimalFormatSymbols ct");
1993
DecimalFormat fmt(UnicodeString("#"), sym, status);
1994
failure(status, "DecimalFormat ct");
1999
sym.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, (UChar)0x5e);
2000
fmt.setDecimalFormatSymbols(sym);
2002
if (fmt.format((int32_t)-1, s, pos) != UNICODE_STRING("^1", 2)) {
2003
errln(UnicodeString("FAIL: -1 x (minus=^) -> ") + s +
2007
if (fmt.getNegativePrefix(s) != UnicodeString((UChar)0x5e)) {
2008
errln(UnicodeString("FAIL: (minus=^).getNegativePrefix -> ") +
2011
sym.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, (UChar)0x2d);
2013
fmt.applyPattern(UnicodeString("#%"), status);
2014
failure(status, "applyPattern percent");
2015
sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UChar)0x5e);
2016
fmt.setDecimalFormatSymbols(sym);
2018
if (fmt.format(0.25, s, pos) != UNICODE_STRING("25^", 3)) {
2019
errln(UnicodeString("FAIL: 0.25 x (percent=^) -> ") + s +
2023
if (fmt.getPositiveSuffix(s) != UnicodeString((UChar)0x5e)) {
2024
errln(UnicodeString("FAIL: (percent=^).getPositiveSuffix -> ") +
2027
sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UChar)0x25);
2029
fmt.applyPattern(str("#\\u2030"), status);
2030
failure(status, "applyPattern permill");
2031
sym.setSymbol(DecimalFormatSymbols::kPerMillSymbol, (UChar)0x5e);
2032
fmt.setDecimalFormatSymbols(sym);
2034
if (fmt.format(0.25, s, pos) != UNICODE_STRING("250^", 4)) {
2035
errln(UnicodeString("FAIL: 0.25 x (permill=^) -> ") + s +
2039
if (fmt.getPositiveSuffix(s) != UnicodeString((UChar)0x5e)) {
2040
errln(UnicodeString("FAIL: (permill=^).getPositiveSuffix -> ") +
2043
sym.setSymbol(DecimalFormatSymbols::kPerMillSymbol, (UChar)0x2030);
2045
fmt.applyPattern(str("\\u00A4#.00"), status);
2046
failure(status, "applyPattern currency");
2047
sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, "usd");
2048
fmt.setDecimalFormatSymbols(sym);
2050
if (fmt.format(12.5, s, pos) != UnicodeString("usd12.50")) {
2051
errln(UnicodeString("FAIL: 12.5 x (currency=usd) -> ") + s +
2055
if (fmt.getPositivePrefix(s) != UnicodeString("usd")) {
2056
errln(UnicodeString("FAIL: (currency=usd).getPositivePrefix -> ") +
2059
sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, "$");
2061
fmt.applyPattern(str("\\u00A4\\u00A4#.00"), status);
2062
failure(status, "applyPattern intl currency");
2063
sym.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, "DOL");
2064
fmt.setDecimalFormatSymbols(sym);
2066
if (fmt.format(12.5, s, pos) != UnicodeString("DOL12.50")) {
2067
errln(UnicodeString("FAIL: 12.5 x (intlcurrency=DOL) -> ") + s +
2071
if (fmt.getPositivePrefix(s) != UnicodeString("DOL")) {
2072
errln(UnicodeString("FAIL: (intlcurrency=DOL).getPositivePrefix -> ") +
2075
sym.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, "USD");
2077
// Since the pattern logic has changed, make sure that patterns round
2078
// trip properly. Test stream in/out integrity too.
2080
const Locale* avail = NumberFormat::getAvailableLocales(n);
2081
static const char* type[] = {
2086
for (int i=0; i<n; ++i) {
2087
for (int j=0; j<3; ++j) {
2088
status = U_ZERO_ERROR;
2092
nf = NumberFormat::createInstance(avail[i], status);
2093
failure(status, "createInstance");
2096
nf = NumberFormat::createCurrencyInstance(avail[i], status);
2097
failure(status, "createCurrencyInstance");
2100
nf = NumberFormat::createPercentInstance(avail[i], status);
2101
failure(status, "createPercentInstance");
2104
if (U_FAILURE(status)) {
2107
DecimalFormat *df = (DecimalFormat*) nf;
2109
// Test toPattern/applyPattern round trip
2112
DecimalFormatSymbols symb(avail[i], status);
2113
failure(status, "Construct DecimalFormatSymbols");
2114
DecimalFormat f2(pat, symb, status);
2116
UnicodeString("Construct DecimalFormat(") + pat + ")")) {
2121
errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
2123
"\" -> \"" + f2.toPattern(p) + "\"");
2126
// Test toLocalizedPattern/applyLocalizedPattern round trip
2127
df->toLocalizedPattern(pat);
2128
f2.applyLocalizedPattern(pat, status);
2130
UnicodeString("applyLocalizedPattern(") + pat + ")");
2131
if (U_FAILURE(status)) {
2136
errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
2137
" -> localized \"" + pat +
2138
"\" -> \"" + f2.toPattern(p) + "\"");
2143
// Test writeObject/readObject round trip
2144
// NOT ON ICU -- Java only
2150
* May 17 1999 sync up - liu
2151
* DecimalFormat.parse() fails for mulipliers 2^n.
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);
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 ") +
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));
2185
* May 17 1999 sync up - liu
2186
* DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
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++) {
2199
fmt->format(D[i], s);
2200
if (s != UnicodeString(S[i])) {
2201
errln(UnicodeString("FAIL: Got ") + s + ", exp " + S[i]);
2208
* alphaWorks upgrade
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);
2220
logln(UnicodeString() + a + " x " +
2221
((DecimalFormat*) nf)->toPattern(pat) + " = " + s);
2222
if (s != UnicodeString("-0.1")) {
2229
* June 16 1999 sync up - liu
2230
* Formatting .5 rounds to "1" instead of "0". (Regression in 1.2.2 RC1)
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");
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]));
2243
for (int32_t i=0; i<N; ++i) {
2245
UnicodeString exp(STR[i]);
2247
fmt.format(NUM[i], str, pos);
2249
logln(UnicodeString("Ok ") + NUM[i] + " x 0. = " + str);
2251
errln(UnicodeString("FAIL ") + NUM[i] + " x 0. = " + str +
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)
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");
2272
fmt.format(0.0, str, pos);
2273
UnicodeString exp("0");
2275
logln(UnicodeString("Ok 0.0 x #.# = ") + str);
2277
errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
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));
2289
errln(UnicodeString("FAIL 99.99 / #.# = ") + toString(val) +
2294
fmt.format(0.0, str, pos);
2296
logln(UnicodeString("Ok 0.0 x #.# = ") + str);
2298
errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
2305
* DateFormat should call setIntegerParseOnly(TRUE) on adopted
2306
* NumberFormat objects.
2308
void NumberFormatRegressionTest::TestJ691(void) {
2309
UErrorCode status = U_ZERO_ERROR;
2310
Locale loc("fr", "CH");
2312
// set up the input date string & expected output
2313
UnicodeString udt("11.10.2000", "");
2314
UnicodeString exp("11.10.00", "");
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));
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));
2330
// *** Here's the key: We don't want to have to do THIS:
2331
// nf->setParseIntegerOnly(TRUE);
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));
2340
df->adoptCalendar(cal);
2341
df->adoptNumberFormat(nf);
2343
// set parsing to lenient & parse
2344
df->setLenient(TRUE);
2345
UDate ulocdat = df->parse(udt, status);
2347
// format back to a string
2348
UnicodeString outString;
2349
df->format(ulocdat, outString);
2351
if (outString != exp) {
2352
errln("FAIL: " + udt + " => " + outString);