LEFT | RIGHT |
1 // © 2016 and later: Unicode, Inc. and others. | 1 // © 2016 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 /* | 3 /* |
4 ******************************************************************************* | 4 ******************************************************************************* |
5 * Copyright (C) 2001-2016, International Business Machines Corporation and | 5 * Copyright (C) 2001-2016, International Business Machines Corporation and |
6 * others. All Rights Reserved. | 6 * others. All Rights Reserved. |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 */ | 8 */ |
9 | 9 |
10 /** | 10 /** |
11 * Port From: ICU4C v1.8.1 : format : NumberFormatTest | 11 * Port From: ICU4C v1.8.1 : format : NumberFormatTest |
12 * Source File: $ICU4oot/source/test/intltest/numfmtst.cpp | 12 * Source File: $ICU4oot/source/test/intltest/numfmtst.cpp |
13 **/ | 13 **/ |
14 | 14 |
15 package com.ibm.icu.dev.test.format; | 15 package com.ibm.icu.dev.test.format; |
16 | 16 |
| 17 import java.io.ByteArrayInputStream; |
| 18 import java.io.ByteArrayOutputStream; |
17 import java.io.IOException; | 19 import java.io.IOException; |
| 20 import java.io.ObjectInputStream; |
| 21 import java.io.ObjectOutputStream; |
18 import java.math.BigInteger; | 22 import java.math.BigInteger; |
| 23 import java.math.RoundingMode; |
19 import java.text.AttributedCharacterIterator; | 24 import java.text.AttributedCharacterIterator; |
20 import java.text.FieldPosition; | 25 import java.text.FieldPosition; |
21 import java.text.Format; | 26 import java.text.Format; |
22 import java.text.ParseException; | 27 import java.text.ParseException; |
23 import java.text.ParsePosition; | 28 import java.text.ParsePosition; |
24 import java.util.ArrayList; | 29 import java.util.ArrayList; |
25 import java.util.Iterator; | 30 import java.util.Iterator; |
26 import java.util.List; | 31 import java.util.List; |
27 import java.util.Locale; | 32 import java.util.Locale; |
28 import java.util.Set; | 33 import java.util.Set; |
29 | 34 |
30 import org.junit.Test; | 35 import org.junit.Test; |
31 | 36 |
32 import com.ibm.icu.dev.test.TestFmwk; | 37 import com.ibm.icu.dev.test.TestFmwk; |
33 import com.ibm.icu.dev.test.TestUtil; | 38 import com.ibm.icu.dev.test.TestUtil; |
34 import com.ibm.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer; | 39 import com.ibm.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer; |
35 import com.ibm.icu.impl.ICUConfig; | 40 import com.ibm.icu.impl.ICUConfig; |
36 import com.ibm.icu.impl.LocaleUtility; | 41 import com.ibm.icu.impl.LocaleUtility; |
37 import com.ibm.icu.impl.data.ResourceReader; | 42 import com.ibm.icu.impl.data.ResourceReader; |
38 import com.ibm.icu.impl.data.TokenIterator; | 43 import com.ibm.icu.impl.data.TokenIterator; |
| 44 import com.ibm.icu.impl.number.rounders.SignificantDigitsRounder.SignificantDigi
tsMode; |
39 import com.ibm.icu.math.BigDecimal; | 45 import com.ibm.icu.math.BigDecimal; |
40 import com.ibm.icu.math.MathContext; | 46 import com.ibm.icu.math.MathContext; |
41 import com.ibm.icu.text.CompactDecimalFormat; | 47 import com.ibm.icu.text.CompactDecimalFormat; |
42 import com.ibm.icu.text.DecimalFormat; | 48 import com.ibm.icu.text.DecimalFormat; |
43 import com.ibm.icu.text.DecimalFormatSymbols; | 49 import com.ibm.icu.text.DecimalFormatSymbols; |
44 import com.ibm.icu.text.DisplayContext; | 50 import com.ibm.icu.text.DisplayContext; |
45 import com.ibm.icu.text.MeasureFormat; | 51 import com.ibm.icu.text.MeasureFormat; |
46 import com.ibm.icu.text.NumberFormat; | 52 import com.ibm.icu.text.NumberFormat; |
47 import com.ibm.icu.text.NumberFormat.NumberFormatFactory; | 53 import com.ibm.icu.text.NumberFormat.NumberFormatFactory; |
48 import com.ibm.icu.text.NumberFormat.SimpleNumberFormatFactory; | 54 import com.ibm.icu.text.NumberFormat.SimpleNumberFormatFactory; |
49 import com.ibm.icu.text.NumberingSystem; | 55 import com.ibm.icu.text.NumberingSystem; |
50 import com.ibm.icu.text.RuleBasedNumberFormat; | 56 import com.ibm.icu.text.RuleBasedNumberFormat; |
51 import com.ibm.icu.util.Currency; | 57 import com.ibm.icu.util.Currency; |
| 58 import com.ibm.icu.util.Currency.CurrencyUsage; |
52 import com.ibm.icu.util.CurrencyAmount; | 59 import com.ibm.icu.util.CurrencyAmount; |
53 import com.ibm.icu.util.ULocale; | 60 import com.ibm.icu.util.ULocale; |
54 | 61 |
55 public class NumberFormatTest extends TestFmwk { | 62 public class NumberFormatTest extends TestFmwk { |
56 | 63 |
57 @Test | 64 @Test |
58 public void TestRoundingScientific10542() { | 65 public void TestRoundingScientific10542() { |
59 DecimalFormat format = | 66 DecimalFormat format = |
60 new DecimalFormat("0.00E0"); | 67 new DecimalFormat("0.00E0"); |
61 | 68 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 213 } |
207 } | 214 } |
208 | 215 |
209 // Test various patterns | 216 // Test various patterns |
210 @Test | 217 @Test |
211 public void TestPatterns() { | 218 public void TestPatterns() { |
212 | 219 |
213 DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US); | 220 DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US); |
214 final String pat[] = { "#.#", "#.", ".#", "#" }; | 221 final String pat[] = { "#.#", "#.", ".#", "#" }; |
215 int pat_length = pat.length; | 222 int pat_length = pat.length; |
216 final String newpat[] = { "0.#", "0.", "#.#", "#" }; | 223 final String newpat[] = { "0.#", "0.", "#.0", "0" }; |
217 final String num[] = { "0", "0.", ".0", "0" }; | 224 final String num[] = { "0", "0.", ".0", "0" }; |
218 for (int i=0; i<pat_length; ++i) | 225 for (int i=0; i<pat_length; ++i) |
219 { | 226 { |
220 DecimalFormat fmt = new DecimalFormat(pat[i], sym); | 227 DecimalFormat fmt = new DecimalFormat(pat[i], sym); |
221 String newp = fmt.toPattern(); | 228 String newp = fmt.toPattern(); |
222 if (!newp.equals(newpat[i])) | 229 if (!newp.equals(newpat[i])) |
223 errln("FAIL: Pattern " + pat[i] + " should transmute to " + newp
at[i] + | 230 errln("FAIL: Pattern " + pat[i] + " should transmute to " + newp
at[i] + |
224 "; " + newp + " seen instead"); | 231 "; " + newp + " seen instead"); |
225 | 232 |
226 String s = ((NumberFormat)fmt).format(0); | 233 String s = ((NumberFormat)fmt).format(0); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 logln(" Format " + Double.toString(-1234.56) + " . " + s); | 413 logln(" Format " + Double.toString(-1234.56) + " . " + s); |
407 assertEquals("name, neg", "USD -1,234.56", s); | 414 assertEquals("name, neg", "USD -1,234.56", s); |
408 } | 415 } |
409 | 416 |
410 @Test | 417 @Test |
411 public void TestSpaceParsing() { | 418 public void TestSpaceParsing() { |
412 // the data are: | 419 // the data are: |
413 // the string to be parsed, parsed position, parsed error index | 420 // the string to be parsed, parsed position, parsed error index |
414 String[][] DATA = { | 421 String[][] DATA = { |
415 {"$124", "4", "-1"}, | 422 {"$124", "4", "-1"}, |
416 {"$124 $124", "5", "-1"}, | 423 {"$124 $124", "4", "-1"}, |
417 {"$124 ", "5", "-1"}, | 424 {"$124 ", "4", "-1"}, |
418 {"$ 124 ", "6", "-1"}, | 425 {"$124 ", "4", "-1"}, |
419 {"$\u00A0124 ", "6", "-1"}, | 426 {"$ 124 ", "5", "-1"}, |
420 {" $ 124 ", "7", "-1"}, | 427 {"$\u00A0124 ", "5", "-1"}, |
421 {"124$", "4", "-1"}, | 428 {" $ 124 ", "6", "-1"}, |
422 {"124 $", "5", "-1"}, | 429 {"124$", "3", "-1"}, |
| 430 {"124 $", "3", "-1"}, |
| 431 {"$124\u200D", "4", "-1"}, |
| 432 {"$\u200D124", "5", "-1"}, |
423 }; | 433 }; |
424 NumberFormat foo = NumberFormat.getCurrencyInstance(); | 434 NumberFormat foo = NumberFormat.getCurrencyInstance(); |
425 for (int i = 0; i < DATA.length; ++i) { | 435 for (int i = 0; i < DATA.length; ++i) { |
426 ParsePosition parsePosition = new ParsePosition(0); | 436 ParsePosition parsePosition = new ParsePosition(0); |
427 String stringToBeParsed = DATA[i][0]; | 437 String stringToBeParsed = DATA[i][0]; |
428 int parsedPosition = Integer.parseInt(DATA[i][1]); | 438 int parsedPosition = Integer.parseInt(DATA[i][1]); |
429 int errorIndex = Integer.parseInt(DATA[i][2]); | 439 int errorIndex = Integer.parseInt(DATA[i][2]); |
430 try { | 440 try { |
431 CurrencyAmount result = foo.parseCurrency(stringToBeParsed, pars
ePosition); | 441 Number result = foo.parse(stringToBeParsed, parsePosition); |
432 if (parsePosition.getIndex() != parsedPosition || | 442 if (parsePosition.getIndex() != parsedPosition || |
433 parsePosition.getErrorIndex() != errorIndex) { | 443 parsePosition.getErrorIndex() != errorIndex) { |
434 errln("FAILED parse " + stringToBeParsed + "; parse position
: " + parsePosition.getIndex() + "; error position: " + parsePosition.getErrorIn
dex()); | 444 errln("FAILED parse " + stringToBeParsed + "; parse position
: " + parsePosition.getIndex() + "; error position: " + parsePosition.getErrorIn
dex()); |
435 } | 445 } |
436 if (parsePosition.getErrorIndex() == -1 && | 446 if (parsePosition.getErrorIndex() == -1 && |
437 result.getNumber().doubleValue() != 124) { | 447 result.doubleValue() != 124) { |
438 errln("FAILED parse " + stringToBeParsed + "; value " + resu
lt.getNumber().doubleValue()); | 448 errln("FAILED parse " + stringToBeParsed + "; value " + resu
lt.doubleValue()); |
439 } | 449 } |
440 } catch (Exception e) { | 450 } catch (Exception e) { |
441 errln("FAILED " + e.toString()); | 451 errln("FAILED " + e.toString()); |
442 } | 452 } |
443 } | 453 } |
444 } | 454 } |
445 | 455 |
| 456 @Test |
| 457 public void TestSpaceParsingStrict() { |
| 458 // All trailing grouping separators should be ignored in strict mode, no
t just the first. |
| 459 Object[][] cases = { |
| 460 {"123 ", 3, -1}, |
| 461 {"123 ", 3, -1}, |
| 462 {"123 ,", 3, -1}, |
| 463 {"123,", 3, -1}, |
| 464 {"123, ", 3, -1}, |
| 465 {"123,,", 3, -1}, |
| 466 {"123,, ", 3, -1}, |
| 467 {"123 ,", 3, -1}, |
| 468 {"123, ", 3, -1}, |
| 469 {"123, 456", 3, -1}, |
| 470 {"123 456", 0, 8} // TODO: Does this behavior make sense? |
| 471 }; |
| 472 DecimalFormat df = new DecimalFormat("#,###"); |
| 473 df.setParseStrict(true); |
| 474 for (Object[] cas : cases) { |
| 475 String input = (String) cas[0]; |
| 476 int expectedIndex = (Integer) cas[1]; |
| 477 int expectedErrorIndex = (Integer) cas[2]; |
| 478 ParsePosition ppos = new ParsePosition(0); |
| 479 df.parse(input, ppos); |
| 480 assertEquals("Failed on index: '" + input + "'", expectedIndex, ppos
.getIndex()); |
| 481 assertEquals("Failed on error: '" + input + "'", expectedErrorIndex,
ppos.getErrorIndex()); |
| 482 } |
| 483 } |
446 | 484 |
447 @Test | 485 @Test |
448 public void TestMultiCurrencySign() { | 486 public void TestMultiCurrencySign() { |
449 String[][] DATA = { | 487 String[][] DATA = { |
450 // the fields in the following test are: | 488 // the fields in the following test are: |
451 // locale, | 489 // locale, |
452 // currency pattern (with negative pattern), | 490 // currency pattern (with negative pattern), |
453 // currency number to be formatted, | 491 // currency number to be formatted, |
454 // currency format using currency symbol name, such as "$" for U
SD, | 492 // currency format using currency symbol name, such as "$" for U
SD, |
455 // currency format using currency ISO name, such as "USD", | 493 // currency format using currency ISO name, such as "USD", |
(...skipping 30 matching lines...) Expand all Loading... |
486 // DATA[i][4] is the currency format result using | 524 // DATA[i][4] is the currency format result using |
487 // double currency sign. | 525 // double currency sign. |
488 // DATA[i][5] is the currency format result using | 526 // DATA[i][5] is the currency format result using |
489 // triple currency sign. | 527 // triple currency sign. |
490 // DATA[i][j+2] is the currency format result using | 528 // DATA[i][j+2] is the currency format result using |
491 // 'j' number of currency sign. | 529 // 'j' number of currency sign. |
492 String currencyFormatResult = DATA[i][2+j]; | 530 String currencyFormatResult = DATA[i][2+j]; |
493 if (!s.equals(currencyFormatResult)) { | 531 if (!s.equals(currencyFormatResult)) { |
494 errln("FAIL format: Expected " + currencyFormatResult); | 532 errln("FAIL format: Expected " + currencyFormatResult); |
495 } | 533 } |
496 | 534 try { |
497 // Same-style parsing | 535 // mix style parsing |
498 // The new parser only allows long names in currency mode. | 536 for (int k=3; k<=4; ++k) { |
499 if (j < 3) { | 537 // DATA[i][3] is the currency format result using a |
500 try { | 538 // single currency sign. |
501 Number number = fmt.parse(currencyFormatResult); | 539 // DATA[i][4] is the currency format result using |
502 if (number.doubleValue() != numberToBeFormat.doubleValue
()) { | 540 // double currency sign. |
503 errln("FAILED same style parse " + currencyFormatRes
ult); | 541 // DATA[i][5] is the currency format result using |
| 542 // triple currency sign. |
| 543 // ICU 59: long name parsing requires currency mode. |
| 544 String oneCurrencyFormat = DATA[i][k]; |
| 545 if (fmt.parse(oneCurrencyFormat).doubleValue() != |
| 546 numberToBeFormat.doubleValue()) { |
| 547 errln("FAILED parse " + oneCurrencyFormat); |
504 } | 548 } |
505 } catch(ParseException e) { | |
506 errln("FAILED same style with exception: " + e); | |
507 } | 549 } |
508 } | 550 } catch (ParseException e) { |
509 | 551 errln("FAILED, DecimalFormat parse currency: " + e.toString(
)); |
510 // Mixed-style parsing | |
511 // The new parser does not currently allow mixed styles in norma
l mode. | |
512 // It allows mixed styles in currency mode. | |
513 for (int k=3; k<=5; ++k) { | |
514 // DATA[i][3] is the currency format result using a | |
515 // single currency sign. | |
516 // DATA[i][4] is the currency format result using | |
517 // double currency sign. | |
518 // DATA[i][5] is the currency format result using | |
519 // triple currency sign. | |
520 String oneCurrencyFormat = DATA[i][k]; | |
521 ParsePosition ppos = new ParsePosition(0); | |
522 CurrencyAmount amount = fmt.parseCurrency(oneCurrencyFormat,
ppos); | |
523 if (amount.getNumber().doubleValue() != numberToBeFormat.dou
bleValue()) { | |
524 errln("FAILED mixed style parse " + oneCurrencyFormat); | |
525 } | |
526 } | 552 } |
527 } | 553 } |
528 } | 554 } |
529 } | 555 } |
530 | 556 |
531 @Test | 557 @Test |
532 public void TestCurrencyFormatForMixParsing() { | 558 public void TestCurrencyFormatForMixParsing() { |
533 MeasureFormat curFmt = MeasureFormat.getCurrencyFormat(new ULocale("en_U
S")); | 559 MeasureFormat curFmt = MeasureFormat.getCurrencyFormat(new ULocale("en_U
S")); |
534 String[] formats = { | 560 String[] formats = { |
535 "$1,234.56", // string to be parsed | 561 "$1,234.56", // string to be parsed |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 } | 736 } |
711 // test parsing, and test parsing for all currency formats. | 737 // test parsing, and test parsing for all currency formats. |
712 for (int j = 3; j < 6; ++j) { | 738 for (int j = 3; j < 6; ++j) { |
713 // DATA[i][3] is the currency format result using | 739 // DATA[i][3] is the currency format result using |
714 // CURRENCYSTYLE formatter. | 740 // CURRENCYSTYLE formatter. |
715 // DATA[i][4] is the currency format result using | 741 // DATA[i][4] is the currency format result using |
716 // ISOCURRENCYSTYLE formatter. | 742 // ISOCURRENCYSTYLE formatter. |
717 // DATA[i][5] is the currency format result using | 743 // DATA[i][5] is the currency format result using |
718 // PLURALCURRENCYSTYLE formatter. | 744 // PLURALCURRENCYSTYLE formatter. |
719 String oneCurrencyFormatResult = DATA[i][j]; | 745 String oneCurrencyFormatResult = DATA[i][j]; |
720 ParsePosition ppos = new ParsePosition(0); | 746 CurrencyAmount val = numFmt.parseCurrency(oneCurrencyFormatR
esult, null); |
721 CurrencyAmount val = numFmt.parseCurrency(oneCurrencyFormatR
esult, ppos); | 747 if (val.getNumber().doubleValue() != numberToBeFormat.double
Value()) { |
722 if (val == null || val.getNumber().doubleValue() != numberTo
BeFormat.doubleValue()) { | 748 errln("FAIL: getCurrencyFormat of locale " + localeStrin
g + " failed roundtripping the number. val=" + val + "; expected: " + numberToBe
Format); |
723 errln("FAIL: getCurrencyFormat of locale " + localeStrin
g + " failed roundtripping the number. val=" + val + "; expected: " + numberToBe
Format + "; string: " + oneCurrencyFormatResult); | |
724 } | 749 } |
725 } | 750 } |
726 } | 751 } |
727 } | 752 } |
728 } | 753 } |
729 | 754 |
730 | 755 |
731 @Test | 756 @Test |
732 public void TestMiscCurrencyParsing() { | 757 public void TestMiscCurrencyParsing() { |
733 String[][] DATA = { | 758 String[][] DATA = { |
734 // each has: string to be parsed, parsed position, error positio
n | 759 // each has: string to be parsed, parsed position, error positio
n |
735 {"1.00 ", "0", "5"}, | 760 {"1.00 ", "4", "-1", "0", "5"}, |
736 {"1.00 UAE dirha", "0", "14"}, | 761 {"1.00 UAE dirha", "4", "-1", "0", "14"}, |
737 {"1.00 us dollar", "14", "-1"}, | 762 {"1.00 us dollar", "4", "-1", "14", "-1"}, |
738 {"1.00 US DOLLAR", "14", "-1"}, | 763 {"1.00 US DOLLAR", "4", "-1", "14", "-1"}, |
739 {"1.00 usd", "8", "-1"}, | 764 {"1.00 usd", "4", "-1", "8", "-1"}, |
740 {"1.00 USD", "8", "-1"}, | 765 {"1.00 USD", "4", "-1", "8", "-1"}, |
741 }; | 766 }; |
742 ULocale locale = new ULocale("en_US"); | 767 ULocale locale = new ULocale("en_US"); |
743 for (int i=0; i<DATA.length; ++i) { | 768 for (int i=0; i<DATA.length; ++i) { |
744 String stringToBeParsed = DATA[i][0]; | 769 String stringToBeParsed = DATA[i][0]; |
745 int parsedPosition = Integer.parseInt(DATA[i][1]); | 770 int parsedPosition = Integer.parseInt(DATA[i][1]); |
746 int errorIndex = Integer.parseInt(DATA[i][2]); | 771 int errorIndex = Integer.parseInt(DATA[i][2]); |
| 772 int currParsedPosition = Integer.parseInt(DATA[i][3]); |
| 773 int currErrorIndex = Integer.parseInt(DATA[i][4]); |
747 NumberFormat numFmt = NumberFormat.getInstance(locale, NumberFormat.
CURRENCYSTYLE); | 774 NumberFormat numFmt = NumberFormat.getInstance(locale, NumberFormat.
CURRENCYSTYLE); |
748 ParsePosition parsePosition = new ParsePosition(0); | 775 ParsePosition parsePosition = new ParsePosition(0); |
749 // TODO: Mark/Andy, should the parser always use currency mode if it
was created with CURRENCYSTYLE? | 776 Number val = numFmt.parse(stringToBeParsed, parsePosition); |
750 CurrencyAmount val = numFmt.parseCurrency(stringToBeParsed, parsePos
ition); | |
751 if (parsePosition.getIndex() != parsedPosition || | 777 if (parsePosition.getIndex() != parsedPosition || |
752 parsePosition.getErrorIndex() != errorIndex) { | 778 parsePosition.getErrorIndex() != errorIndex) { |
753 errln("FAIL: parse failed on case " + i + ". expected error posi
tion: " + errorIndex + "; actual: " + parsePosition.getErrorIndex()); | 779 errln("FAIL: parse failed on case "+i+". expected position: " +
parsedPosition +"; actual: " + parsePosition.getIndex()); |
754 errln("FAIL: parse failed on case " + i + ". expected position:
" + parsedPosition +"; actual: " + parsePosition.getIndex()); | 780 errln("FAIL: parse failed on case "+i+". expected error position
: " + errorIndex + "; actual: " + parsePosition.getErrorIndex()); |
755 } | 781 } |
756 if (parsePosition.getErrorIndex() == -1 && | 782 if (parsePosition.getErrorIndex() == -1 && |
757 val.getNumber().doubleValue() != 1.00) { | 783 val.doubleValue() != 1.00) { |
758 errln("FAIL: parse failed. expected 1.00, actual:" + val); | 784 errln("FAIL: parse failed. expected 1.00, actual:" + val); |
| 785 } |
| 786 parsePosition = new ParsePosition(0); |
| 787 CurrencyAmount amt = numFmt.parseCurrency(stringToBeParsed, parsePos
ition); |
| 788 if (parsePosition.getIndex() != currParsedPosition || |
| 789 parsePosition.getErrorIndex() != currErrorIndex) { |
| 790 errln("FAIL: parseCurrency failed on case "+i+". expected error
position: " + currErrorIndex + "; actual: " + parsePosition.getErrorIndex()); |
| 791 errln("FAIL: parseCurrency failed on case "+i+". expected positi
on: " + currParsedPosition +"; actual: " + parsePosition.getIndex()); |
| 792 } |
| 793 if (parsePosition.getErrorIndex() == -1 && |
| 794 amt.getNumber().doubleValue() != 1.00) { |
| 795 errln("FAIL: parseCurrency failed. expected 1.00, actual:" + val
); |
759 } | 796 } |
760 } | 797 } |
761 } | 798 } |
762 | 799 |
763 @Test | 800 @Test |
764 public void TestParseCurrency() { | 801 public void TestParseCurrency() { |
765 class ParseCurrencyItem { | 802 class ParseCurrencyItem { |
766 private final String localeString; | 803 private final String localeString; |
767 private final String descrip; | 804 private final String descrip; |
768 private final String currStr; | 805 private final String currStr; |
(...skipping 20 matching lines...) Expand all Loading... |
789 public int getNumExpectVal() { return numExpectVal; } | 826 public int getNumExpectVal() { return numExpectVal; } |
790 public int getCurExpectPos() { return curExpectPos; } | 827 public int getCurExpectPos() { return curExpectPos; } |
791 public int getCurExpectVal() { return curExpectVal; } | 828 public int getCurExpectVal() { return curExpectVal; } |
792 public String getCurExpectCurr() { return curExpectCurr; } | 829 public String getCurExpectCurr() { return curExpectCurr; } |
793 } | 830 } |
794 // Note: In cases where the number occurs before the currency sign, non-
currency mode will parse the number | 831 // Note: In cases where the number occurs before the currency sign, non-
currency mode will parse the number |
795 // and stop when it reaches the currency symbol. | 832 // and stop when it reaches the currency symbol. |
796 final ParseCurrencyItem[] parseCurrencyItems = { | 833 final ParseCurrencyItem[] parseCurrencyItems = { |
797 new ParseCurrencyItem( "en_US", "dollars2", "$2.00",
5, 2, 5, 2, "USD" ), | 834 new ParseCurrencyItem( "en_US", "dollars2", "$2.00",
5, 2, 5, 2, "USD" ), |
798 new ParseCurrencyItem( "en_US", "dollars4", "$4",
2, 4, 2, 4, "USD" ), | 835 new ParseCurrencyItem( "en_US", "dollars4", "$4",
2, 4, 2, 4, "USD" ), |
799 new ParseCurrencyItem( "en_US", "dollars9", "9\u00A0$",
2, 9, 3, 9, "USD" ), | 836 new ParseCurrencyItem( "en_US", "dollars9", "9\u00A0$",
1, 9, 3, 9, "USD" ), |
800 new ParseCurrencyItem( "en_US", "pounds3", "\u00A33.00",
0, 0, 5, 3, "GBP" ), | 837 new ParseCurrencyItem( "en_US", "pounds3", "\u00A33.00",
0, 0, 5, 3, "GBP" ), |
801 new ParseCurrencyItem( "en_US", "pounds5", "\u00A35",
0, 0, 2, 5, "GBP" ), | 838 new ParseCurrencyItem( "en_US", "pounds5", "\u00A35",
0, 0, 2, 5, "GBP" ), |
802 new ParseCurrencyItem( "en_US", "pounds7", "7\u00A0\u00A3",
2, 7, 3, 7, "GBP" ), | 839 new ParseCurrencyItem( "en_US", "pounds7", "7\u00A0\u00A3",
1, 7, 3, 7, "GBP" ), |
803 new ParseCurrencyItem( "en_US", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), | 840 new ParseCurrencyItem( "en_US", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), |
804 | 841 |
805 new ParseCurrencyItem( "en_GB", "pounds3", "\u00A33.00",
5, 3, 5, 3, "GBP" ), | 842 new ParseCurrencyItem( "en_GB", "pounds3", "\u00A33.00",
5, 3, 5, 3, "GBP" ), |
806 new ParseCurrencyItem( "en_GB", "pounds5", "\u00A35",
2, 5, 2, 5, "GBP" ), | 843 new ParseCurrencyItem( "en_GB", "pounds5", "\u00A35",
2, 5, 2, 5, "GBP" ), |
807 new ParseCurrencyItem( "en_GB", "pounds7", "7\u00A0\u00A3",
2, 7, 3, 7, "GBP" ), | 844 new ParseCurrencyItem( "en_GB", "pounds7", "7\u00A0\u00A3",
1, 7, 3, 7, "GBP" ), |
808 new ParseCurrencyItem( "en_GB", "euros4", "4,00\u00A0\u20AC",
5,400, 6,400, "EUR" ), | 845 new ParseCurrencyItem( "en_GB", "euros4", "4,00\u00A0\u20AC",
4,400, 6,400, "EUR" ), |
809 new ParseCurrencyItem( "en_GB", "euros6", "6\u00A0\u20AC",
2, 6, 3, 6, "EUR" ), | 846 new ParseCurrencyItem( "en_GB", "euros6", "6\u00A0\u20AC",
1, 6, 3, 6, "EUR" ), |
810 new ParseCurrencyItem( "en_GB", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), | 847 new ParseCurrencyItem( "en_GB", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), |
811 new ParseCurrencyItem( "en_GB", "dollars4", "US$4",
0, 0, 4, 4, "USD" ), | 848 new ParseCurrencyItem( "en_GB", "dollars4", "US$4",
0, 0, 4, 4, "USD" ), |
812 | 849 |
813 new ParseCurrencyItem( "fr_FR", "euros4", "4,00\u00A0\u20AC",
6, 4, 6, 4, "EUR" ), | 850 new ParseCurrencyItem( "fr_FR", "euros4", "4,00\u00A0\u20AC",
6, 4, 6, 4, "EUR" ), |
814 new ParseCurrencyItem( "fr_FR", "euros6", "6\u00A0\u20AC",
3, 6, 3, 6, "EUR" ), | 851 new ParseCurrencyItem( "fr_FR", "euros6", "6\u00A0\u20AC",
3, 6, 3, 6, "EUR" ), |
815 new ParseCurrencyItem( "fr_FR", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), | 852 new ParseCurrencyItem( "fr_FR", "euros8", "\u20AC8",
0, 0, 2, 8, "EUR" ), |
816 new ParseCurrencyItem( "fr_FR", "dollars2", "$2.00",
0, 0, 0, 0, "" ), | 853 new ParseCurrencyItem( "fr_FR", "dollars2", "$2.00",
0, 0, 0, 0, "" ), |
817 new ParseCurrencyItem( "fr_FR", "dollars4", "$4",
0, 0, 0, 0, "" ), | 854 new ParseCurrencyItem( "fr_FR", "dollars4", "$4",
0, 0, 0, 0, "" ), |
818 }; | 855 }; |
819 for (ParseCurrencyItem item: parseCurrencyItems) { | 856 for (ParseCurrencyItem item: parseCurrencyItems) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 } else { | 890 } else { |
854 errln("NumberFormat.getCurrencyInstance parseCurrency " + lo
caleString + "/" + item.getDescrip() + | 891 errln("NumberFormat.getCurrencyInstance parseCurrency " + lo
caleString + "/" + item.getDescrip() + |
855 ", expect pos/val/curr " + item.getCurExpectPos() +
"/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + | 892 ", expect pos/val/curr " + item.getCurExpectPos() +
"/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + |
856 ", get " + parsePos.getIndex() + "/(NULL)" ); | 893 ", get " + parsePos.getIndex() + "/(NULL)" ); |
857 } | 894 } |
858 } | 895 } |
859 } | 896 } |
860 } | 897 } |
861 | 898 |
862 @Test | 899 @Test |
| 900 public void TestParseCurrencyWithWhitespace() { |
| 901 DecimalFormat df = new DecimalFormat("#,##0.00 ¤¤"); |
| 902 ParsePosition ppos = new ParsePosition(0); |
| 903 df.parseCurrency("1.00 us denmark", ppos); |
| 904 assertEquals("Expected to fail on 'us denmark' string", 9, ppos.getError
Index()); |
| 905 } |
| 906 |
| 907 @Test |
863 public void TestParseCurrPatternWithDecStyle() { | 908 public void TestParseCurrPatternWithDecStyle() { |
864 String currpat = "¤#,##0.00"; | 909 String currpat = "¤#,##0.00"; |
865 String parsetxt = "x0y$"; | 910 String parsetxt = "x0y$"; |
866 DecimalFormat decfmt = (DecimalFormat)NumberFormat.getInstance(new ULoca
le("en_US"), NumberFormat.NUMBERSTYLE); | 911 DecimalFormat decfmt = (DecimalFormat)NumberFormat.getInstance(new ULoca
le("en_US"), NumberFormat.NUMBERSTYLE); |
867 decfmt.applyPattern(currpat); | 912 decfmt.applyPattern(currpat); |
868 ParsePosition ppos = new ParsePosition(0); | 913 ParsePosition ppos = new ParsePosition(0); |
869 Number value = decfmt.parse(parsetxt, ppos); | 914 Number value = decfmt.parse(parsetxt, ppos); |
870 if (ppos.getIndex() != 0) { | 915 if (ppos.getIndex() != 0) { |
871 errln("DecimalFormat.parse expected to fail but got ppos " + ppos.ge
tIndex() + ", value " + value); | 916 errln("DecimalFormat.parse expected to fail but got ppos " + ppos.ge
tIndex() + ", value " + value); |
872 } | 917 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 } | 957 } |
913 | 958 |
914 @Test | 959 @Test |
915 public void TestCompatibleCurrencies() { | 960 public void TestCompatibleCurrencies() { |
916 NumberFormat fmt = | 961 NumberFormat fmt = |
917 NumberFormat.getCurrencyInstance(Locale.US); | 962 NumberFormat.getCurrencyInstance(Locale.US); |
918 expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\u00A51,23
5"); // Yen half-width | 963 expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\u00A51,23
5"); // Yen half-width |
919 expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\uFFE51,23
5"); // Yen full-wdith | 964 expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\uFFE51,23
5"); // Yen full-wdith |
920 } | 965 } |
921 | 966 |
922 /* | |
923 * This test doesn't work against the new formatter because | |
924 * getMinimumFractionDigits() and getMaximumFractionDigits() | |
925 * return the user-specified value, not the values actually | |
926 * used in formatting. | |
927 * | |
928 @Test | 967 @Test |
929 public void TestCurrencyPatterns() { | 968 public void TestCurrencyPatterns() { |
930 int i; | 969 int i; |
931 Locale[] locs = NumberFormat.getAvailableLocales(); | 970 Locale[] locs = NumberFormat.getAvailableLocales(); |
932 for (i=0; i<locs.length; ++i) { | 971 for (i=0; i<locs.length; ++i) { |
933 NumberFormat nf = NumberFormat.getCurrencyInstance(locs[i]); | 972 NumberFormat nf = NumberFormat.getCurrencyInstance(locs[i]); |
934 // Make sure currency formats do not have a variable number | 973 // Make sure currency formats do not have a variable number |
935 // of fraction digits | 974 // of fraction digits |
936 int min = nf.getMinimumFractionDigits(); | 975 int min = nf.getMinimumFractionDigits(); |
937 int max = nf.getMaximumFractionDigits(); | 976 int max = nf.getMaximumFractionDigits(); |
(...skipping 14 matching lines...) Expand all Loading... |
952 String a = nf.format(1.0); | 991 String a = nf.format(1.0); |
953 errln("FAIL: " + locs[i] + | 992 errln("FAIL: " + locs[i] + |
954 " is a EURO format but it does not have 2 fracti
on digits; "+ | 993 " is a EURO format but it does not have 2 fracti
on digits; "+ |
955 "x 1.0 => " + | 994 "x 1.0 => " + |
956 a); | 995 a); |
957 } | 996 } |
958 } | 997 } |
959 } | 998 } |
960 } | 999 } |
961 } | 1000 } |
962 */ | |
963 | 1001 |
964 /** | 1002 /** |
965 * Do rudimentary testing of parsing. | 1003 * Do rudimentary testing of parsing. |
966 */ | 1004 */ |
967 @Test | 1005 @Test |
968 public void TestParse() { | 1006 public void TestParse() { |
969 String arg = "0.0"; | 1007 String arg = "0.0"; |
970 DecimalFormat format = new DecimalFormat("00"); | 1008 DecimalFormat format = new DecimalFormat("00"); |
971 double aNumber = 0l; | 1009 double aNumber = 0l; |
972 try { | 1010 try { |
(...skipping 24 matching lines...) Expand all Loading... |
997 /** | 1035 /** |
998 * Test the functioning of the secondary grouping value. | 1036 * Test the functioning of the secondary grouping value. |
999 */ | 1037 */ |
1000 @Test | 1038 @Test |
1001 public void TestSecondaryGrouping() { | 1039 public void TestSecondaryGrouping() { |
1002 | 1040 |
1003 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); | 1041 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); |
1004 DecimalFormat f = new DecimalFormat("#,##,###", US); | 1042 DecimalFormat f = new DecimalFormat("#,##,###", US); |
1005 | 1043 |
1006 expect(f, 123456789L, "12,34,56,789"); | 1044 expect(f, 123456789L, "12,34,56,789"); |
1007 expectPat(f, "#,##,###"); | 1045 expectPat(f, "#,##,##0"); |
1008 f.applyPattern("#,###"); | 1046 f.applyPattern("#,###"); |
1009 | 1047 |
1010 f.setSecondaryGroupingSize(4); | 1048 f.setSecondaryGroupingSize(4); |
1011 expect(f, 123456789L, "12,3456,789"); | 1049 expect(f, 123456789L, "12,3456,789"); |
1012 expectPat(f, "#,####,###"); | 1050 expectPat(f, "#,####,##0"); |
1013 NumberFormat g = NumberFormat.getInstance(new Locale("hi", "IN")); | 1051 NumberFormat g = NumberFormat.getInstance(new Locale("hi", "IN")); |
1014 | 1052 |
1015 String out = ""; | 1053 String out = ""; |
1016 long l = 1876543210L; | 1054 long l = 1876543210L; |
1017 out = g.format(l); | 1055 out = g.format(l); |
1018 | 1056 |
1019 // expect "1,87,65,43,210", but with Hindi digits | 1057 // expect "1,87,65,43,210", but with Hindi digits |
1020 // 01234567890123 | 1058 // 01234567890123 |
1021 boolean ok = true; | 1059 boolean ok = true; |
1022 if (out.length() != 14) { | 1060 if (out.length() != 14) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 for (int i = 0; i < PAT_length; ++i) { | 1135 for (int i = 0; i < PAT_length; ++i) { |
1098 String pat = PAT[i]; | 1136 String pat = PAT[i]; |
1099 DecimalFormat df = new DecimalFormat(pat, US); | 1137 DecimalFormat df = new DecimalFormat(pat, US); |
1100 String pat2 = df.toPattern(); | 1138 String pat2 = df.toPattern(); |
1101 if (pat.equals(pat2)) { | 1139 if (pat.equals(pat2)) { |
1102 logln("Ok Pattern rt \"" + pat + "\" . \"" + pat2 + "\""); | 1140 logln("Ok Pattern rt \"" + pat + "\" . \"" + pat2 + "\""); |
1103 } else { | 1141 } else { |
1104 errln("FAIL Pattern rt \"" + pat + "\" . \"" + pat2 + "\""); | 1142 errln("FAIL Pattern rt \"" + pat + "\" . \"" + pat2 + "\""); |
1105 } | 1143 } |
1106 // Make sure digit counts match what we expect | 1144 // Make sure digit counts match what we expect |
| 1145 if (i == 0) continue; // outputs to 1,1,0,0 since at least one min d
igit is required. |
1107 if (df.getMinimumIntegerDigits() != DIGITS[4 * i] | 1146 if (df.getMinimumIntegerDigits() != DIGITS[4 * i] |
1108 || df.getMaximumIntegerDigits() != DIGITS[4 * i + 1] | 1147 || df.getMaximumIntegerDigits() != DIGITS[4 * i + 1] |
1109 || df.getMinimumFractionDigits() != DIGITS[4 * i + 2
] | 1148 || df.getMinimumFractionDigits() != DIGITS[4 * i + 2
] |
1110 || df.getMaximumFractionDigits() != DIGITS[4
* i + 3]) { | 1149 || df.getMaximumFractionDigits() != DIGITS[4
* i + 3]) { |
1111 errln("FAIL \""+ pat+ "\" min/max int; min/max frac = " | 1150 errln("FAIL \""+ pat+ "\" min/max int; min/max frac = " |
1112 + df.getMinimumIntegerDigits() + "/" | 1151 + df.getMinimumIntegerDigits() + "/" |
1113 + df.getMaximumIntegerDigits() + ";" | 1152 + df.getMaximumIntegerDigits() + ";" |
1114 + df.getMinimumFractionDigits() + "/" | 1153 + df.getMinimumFractionDigits() + "/" |
1115 + df.getMaximumFractionDigits() + ", expect " | 1154 + df.getMaximumFractionDigits() + ", expect " |
1116 + DIGITS[4 * i] + "/" | 1155 + DIGITS[4 * i] + "/" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1284 /* fmt.setPadCharacter((UnicodeString)"^^^"); | 1323 /* fmt.setPadCharacter((UnicodeString)"^^^"); |
1285 expectPad(fmt, "*^^^#", DecimalFormat.kPadBeforePrefix, 3, (UnicodeStr
ing)"^^^"); | 1324 expectPad(fmt, "*^^^#", DecimalFormat.kPadBeforePrefix, 3, (UnicodeStr
ing)"^^^"); |
1286 padString.remove(); | 1325 padString.remove(); |
1287 padString.append((UChar)0x0061); | 1326 padString.append((UChar)0x0061); |
1288 padString.append((UChar)0x0302); | 1327 padString.append((UChar)0x0302); |
1289 fmt.setPadCharacter(padString); | 1328 fmt.setPadCharacter(padString); |
1290 UChar patternChars[]={0x002a, 0x0061, 0x0302, 0x0061, 0x0302, 0x0023,
0x0000}; | 1329 UChar patternChars[]={0x002a, 0x0061, 0x0302, 0x0061, 0x0302, 0x0023,
0x0000}; |
1291 UnicodeString pattern(patternChars); | 1330 UnicodeString pattern(patternChars); |
1292 expectPad(fmt, pattern , DecimalFormat.kPadBeforePrefix, 4, padString)
; | 1331 expectPad(fmt, pattern , DecimalFormat.kPadBeforePrefix, 4, padString)
; |
1293 */ | 1332 */ |
| 1333 |
| 1334 // Test multi-char padding sequence specified via pattern |
| 1335 expect2(new DecimalFormat("*'😃'####.00", US), 1.1, "😃😃😃1.10"); |
1294 } | 1336 } |
1295 | 1337 |
1296 /** | 1338 /** |
1297 * Upgrade to alphaWorks | 1339 * Upgrade to alphaWorks |
1298 */ | 1340 */ |
1299 @Test | 1341 @Test |
1300 public void TestPatterns2() { | 1342 public void TestPatterns2() { |
1301 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); | 1343 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); |
1302 DecimalFormat fmt = new DecimalFormat("#", US); | 1344 DecimalFormat fmt = new DecimalFormat("#", US); |
1303 | 1345 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1544 ULocale[] allULocales = DecimalFormatSymbols.getAvailableULocales(); | 1586 ULocale[] allULocales = DecimalFormatSymbols.getAvailableULocales(); |
1545 if (allULocales.length == 0) { | 1587 if (allULocales.length == 0) { |
1546 errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableL
ocales"); | 1588 errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableL
ocales"); |
1547 } else { | 1589 } else { |
1548 logln("PASS: " + allULocales.length + | 1590 logln("PASS: " + allULocales.length + |
1549 " available locales returned by DecimalFormatSymbols.getAvai
lableULocales"); | 1591 " available locales returned by DecimalFormatSymbols.getAvai
lableULocales"); |
1550 } | 1592 } |
1551 } | 1593 } |
1552 | 1594 |
1553 @Test | 1595 @Test |
| 1596 public void TestParseNull() throws ParseException { |
| 1597 DecimalFormat df = new DecimalFormat(); |
| 1598 try { |
| 1599 df.parse(null); |
| 1600 fail("df.parse(null) didn't throw an exception"); |
| 1601 } catch (IllegalArgumentException e){} |
| 1602 try { |
| 1603 df.parse(null, null); |
| 1604 fail("df.parse(null) didn't throw an exception"); |
| 1605 } catch (IllegalArgumentException e){} |
| 1606 try { |
| 1607 df.parseCurrency(null, null); |
| 1608 fail("df.parse(null) didn't throw an exception"); |
| 1609 } catch (IllegalArgumentException e){} |
| 1610 } |
| 1611 |
| 1612 @Test |
1554 public void TestWhiteSpaceParsing() { | 1613 public void TestWhiteSpaceParsing() { |
1555 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); | 1614 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); |
1556 DecimalFormat fmt = new DecimalFormat("a b#0c ", US); | 1615 DecimalFormat fmt = new DecimalFormat("a b#0c ", US); |
1557 int n = 1234; | 1616 int n = 1234; |
1558 expect(fmt, "a b1234c ", n); | 1617 expect(fmt, "a b1234c ", n); |
1559 expect(fmt, "a b1234c ", n); | 1618 expect(fmt, "a b1234c ", n); |
1560 expect(fmt, "ab1234", n); | 1619 expect(fmt, "ab1234", n); |
1561 | 1620 |
1562 fmt.applyPattern("a b #"); | 1621 fmt.applyPattern("a b #"); |
1563 expect(fmt, "ab1234", n); | 1622 expect(fmt, "ab1234", n); |
1564 expect(fmt, "ab 1234", n); | 1623 expect(fmt, "ab 1234", n); |
1565 | |
1566 fmt.applyPattern("#"); | |
1567 fmt.setPositivePrefix(" a b "); | |
1568 expect(fmt, "a b1234", n); | 1624 expect(fmt, "a b1234", n); |
1569 expect(fmt, "a b1234", n); | 1625 expect(fmt, "a b1234", n); |
1570 expect(fmt, "ab1234", n); | |
1571 expect(fmt, "ab 1234", n); | |
1572 } | 1626 } |
1573 | 1627 |
1574 /** | 1628 /** |
1575 * Test currencies whose display name is a ChoiceFormat. | 1629 * Test currencies whose display name is a ChoiceFormat. |
1576 */ | 1630 */ |
1577 @Test | 1631 @Test |
1578 public void TestComplexCurrency() { | 1632 public void TestComplexCurrency() { |
1579 // CLDR No Longer uses complex currency symbols. | 1633 // CLDR No Longer uses complex currency symbols. |
1580 // Skipping this test. | 1634 // Skipping this test. |
1581 // Locale loc = new Locale("kn", "IN", ""); | 1635 // Locale loc = new Locale("kn", "IN", ""); |
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2365 char ch; | 2419 char ch; |
2366 while (len1 > 0 && ((ch = formatted.charAt(len1-1)) == '0' || ch == '.')
) --len1; | 2420 while (len1 > 0 && ((ch = formatted.charAt(len1-1)) == '0' || ch == '.')
) --len1; |
2367 if (len1==1 && ((ch = formatted.charAt(len1-1)) == '-')) --len1; | 2421 if (len1==1 && ((ch = formatted.charAt(len1-1)) == '-')) --len1; |
2368 return formatted.substring(0,len1); | 2422 return formatted.substring(0,len1); |
2369 } | 2423 } |
2370 | 2424 |
2371 //------------------------------------------------------------------ | 2425 //------------------------------------------------------------------ |
2372 // Support methods | 2426 // Support methods |
2373 //------------------------------------------------------------------ | 2427 //------------------------------------------------------------------ |
2374 | 2428 |
2375 // Format-Parse test | 2429 /** Format-Parse test */ |
2376 public void expect2(NumberFormat fmt, Number n, String exp) { | 2430 public void expect2(NumberFormat fmt, Number n, String exp) { |
2377 // Don't round-trip format test, since we explicitly do it | 2431 // Don't round-trip format test, since we explicitly do it |
2378 expect(fmt, n, exp, false); | 2432 expect(fmt, n, exp, false); |
2379 expect(fmt, exp, n); | 2433 expect(fmt, exp, n); |
2380 } | 2434 } |
2381 // Format-Parse test | 2435 /** Format-Parse test */ |
2382 public void expect3(NumberFormat fmt, Number n, String exp) { | 2436 public void expect3(NumberFormat fmt, Number n, String exp) { |
2383 // Don't round-trip format test, since we explicitly do it | 2437 // Don't round-trip format test, since we explicitly do it |
2384 expect_rbnf(fmt, n, exp, false); | 2438 expect_rbnf(fmt, n, exp, false); |
2385 expect_rbnf(fmt, exp, n); | 2439 expect_rbnf(fmt, exp, n); |
2386 } | 2440 } |
2387 | 2441 |
2388 // Format-Parse test (convenience) | 2442 /** Format-Parse test (convenience) */ |
2389 public void expect2(NumberFormat fmt, double n, String exp) { | 2443 public void expect2(NumberFormat fmt, double n, String exp) { |
2390 expect2(fmt, new Double(n), exp); | 2444 expect2(fmt, new Double(n), exp); |
2391 } | 2445 } |
2392 // Format-Parse test (convenience) | 2446 /** RBNF Format-Parse test (convenience) */ |
2393 public void expect3(NumberFormat fmt, double n, String exp) { | 2447 public void expect3(NumberFormat fmt, double n, String exp) { |
2394 expect3(fmt, new Double(n), exp); | 2448 expect3(fmt, new Double(n), exp); |
2395 } | 2449 } |
2396 | 2450 |
2397 // Format-Parse test (convenience) | 2451 /** Format-Parse test (convenience) */ |
2398 public void expect2(NumberFormat fmt, long n, String exp) { | 2452 public void expect2(NumberFormat fmt, long n, String exp) { |
2399 expect2(fmt, new Long(n), exp); | 2453 expect2(fmt, new Long(n), exp); |
2400 } | 2454 } |
2401 // Format-Parse test (convenience) | 2455 /** RBNF Format-Parse test (convenience) */ |
2402 public void expect3(NumberFormat fmt, long n, String exp) { | 2456 public void expect3(NumberFormat fmt, long n, String exp) { |
2403 expect3(fmt, new Long(n), exp); | 2457 expect3(fmt, new Long(n), exp); |
2404 } | 2458 } |
2405 | 2459 |
2406 // Format test | 2460 /** Format test */ |
2407 public void expect(NumberFormat fmt, Number n, String exp, boolean rt) { | 2461 public void expect(NumberFormat fmt, Number n, String exp, boolean rt) { |
2408 StringBuffer saw = new StringBuffer(); | 2462 StringBuffer saw = new StringBuffer(); |
2409 FieldPosition pos = new FieldPosition(0); | 2463 FieldPosition pos = new FieldPosition(0); |
2410 fmt.format(n, saw, pos); | 2464 fmt.format(n, saw, pos); |
2411 String pat = ((DecimalFormat)fmt).toPattern(); | 2465 String pat = ((DecimalFormat)fmt).toPattern(); |
2412 if (saw.toString().equals(exp)) { | 2466 if (saw.toString().equals(exp)) { |
2413 logln("Ok " + n + " x " + | 2467 logln("Ok " + n + " x " + |
2414 pat + " = \"" + | 2468 pat + " = \"" + |
2415 saw + "\""); | 2469 saw + "\""); |
2416 // We should be able to round-trip the formatted string => | 2470 // We should be able to round-trip the formatted string => |
(...skipping 12 matching lines...) Expand all Loading... |
2429 errln("expect() format test rt, locale " + fmt.getLocale(ULo
cale.VALID_LOCALE) + | 2483 errln("expect() format test rt, locale " + fmt.getLocale(ULo
cale.VALID_LOCALE) + |
2430 ", " + e.getMessage()); | 2484 ", " + e.getMessage()); |
2431 return; | 2485 return; |
2432 } | 2486 } |
2433 } | 2487 } |
2434 } else { | 2488 } else { |
2435 errln("expect() format test, locale " + fmt.getLocale(ULocale.VALID_
LOCALE) + | 2489 errln("expect() format test, locale " + fmt.getLocale(ULocale.VALID_
LOCALE) + |
2436 ", FAIL " + n + " x " + pat + " = \"" + saw + "\", expected
\"" + exp + "\""); | 2490 ", FAIL " + n + " x " + pat + " = \"" + saw + "\", expected
\"" + exp + "\""); |
2437 } | 2491 } |
2438 } | 2492 } |
2439 // Format test | 2493 /** RBNF format test */ |
2440 public void expect_rbnf(NumberFormat fmt, Number n, String exp, boolean rt)
{ | 2494 public void expect_rbnf(NumberFormat fmt, Number n, String exp, boolean rt)
{ |
2441 StringBuffer saw = new StringBuffer(); | 2495 StringBuffer saw = new StringBuffer(); |
2442 FieldPosition pos = new FieldPosition(0); | 2496 FieldPosition pos = new FieldPosition(0); |
2443 fmt.format(n, saw, pos); | 2497 fmt.format(n, saw, pos); |
2444 if (saw.toString().equals(exp)) { | 2498 if (saw.toString().equals(exp)) { |
2445 logln("Ok " + n + " = \"" + | 2499 logln("Ok " + n + " = \"" + |
2446 saw + "\""); | 2500 saw + "\""); |
2447 // We should be able to round-trip the formatted string => | 2501 // We should be able to round-trip the formatted string => |
2448 // number => string (but not the other way around: number | 2502 // number => string (but not the other way around: number |
2449 // => string => number2, might have number2 != number): | 2503 // => string => number2, might have number2 != number): |
(...skipping 11 matching lines...) Expand all Loading... |
2461 ", " + e.getMessage()); | 2515 ", " + e.getMessage()); |
2462 return; | 2516 return; |
2463 } | 2517 } |
2464 } | 2518 } |
2465 } else { | 2519 } else { |
2466 errln("expect_rbnf() format test, locale " + fmt.getLocale(ULocale.V
ALID_LOCALE) + | 2520 errln("expect_rbnf() format test, locale " + fmt.getLocale(ULocale.V
ALID_LOCALE) + |
2467 ", FAIL " + n + " = \"" + saw + "\", expected \"" + exp + "\
""); | 2521 ", FAIL " + n + " = \"" + saw + "\", expected \"" + exp + "\
""); |
2468 } | 2522 } |
2469 } | 2523 } |
2470 | 2524 |
2471 // Format test (convenience) | 2525 /** Format test (convenience) */ |
2472 public void expect(NumberFormat fmt, Number n, String exp) { | 2526 public void expect(NumberFormat fmt, Number n, String exp) { |
2473 expect(fmt, n, exp, true); | 2527 expect(fmt, n, exp, true); |
2474 } | 2528 } |
2475 | 2529 |
2476 // Format test (convenience) | 2530 /** Format test (convenience) */ |
2477 public void expect(NumberFormat fmt, double n, String exp) { | 2531 public void expect(NumberFormat fmt, double n, String exp) { |
2478 expect(fmt, new Double(n), exp); | 2532 expect(fmt, new Double(n), exp); |
2479 } | 2533 } |
2480 | 2534 |
2481 // Format test (convenience) | 2535 /** Format test (convenience) */ |
2482 public void expect(NumberFormat fmt, long n, String exp) { | 2536 public void expect(NumberFormat fmt, long n, String exp) { |
2483 expect(fmt, new Long(n), exp); | 2537 expect(fmt, new Long(n), exp); |
2484 } | 2538 } |
2485 | 2539 |
2486 // Parse test | 2540 /** Parse test */ |
2487 public void expect(NumberFormat fmt, String str, Number n) { | 2541 public void expect(NumberFormat fmt, String str, Number n) { |
2488 Number num = null; | 2542 Number num = null; |
2489 try { | 2543 try { |
2490 num = fmt.parse(str); | 2544 num = fmt.parse(str); |
2491 } catch (ParseException e) { | 2545 } catch (ParseException e) { |
2492 errln(e.getMessage()); | 2546 errln(e.getMessage()); |
2493 return; | 2547 return; |
2494 } | 2548 } |
2495 String pat = ((DecimalFormat)fmt).toPattern(); | 2549 String pat = ((DecimalFormat)fmt).toPattern(); |
2496 // A little tricky here -- make sure Double(12345.0) and | 2550 // A little tricky here -- make sure Double(12345.0) and |
2497 // Long(12345) match. | 2551 // Long(12345) match. |
2498 if (num.equals(n) || num.doubleValue() == n.doubleValue()) { | 2552 if (num.equals(n) || num.doubleValue() == n.doubleValue()) { |
2499 logln("Ok \"" + str + "\" x " + | 2553 logln("Ok \"" + str + "\" x " + |
2500 pat + " = " + | 2554 pat + " = " + |
2501 num); | 2555 num); |
2502 } else { | 2556 } else { |
2503 errln("expect() parse test, locale " + fmt.getLocale(ULocale.VALID_L
OCALE) + | 2557 errln("expect() parse test, locale " + fmt.getLocale(ULocale.VALID_L
OCALE) + |
2504 ", FAIL \"" + str + "\" x " + pat + " = " + num + ", expecte
d " + n); | 2558 ", FAIL \"" + str + "\" x " + pat + " = " + num + ", expecte
d " + n); |
2505 } | 2559 } |
2506 } | 2560 } |
2507 | 2561 |
2508 // Parse test | 2562 /** RBNF Parse test */ |
2509 public void expect_rbnf(NumberFormat fmt, String str, Number n) { | 2563 public void expect_rbnf(NumberFormat fmt, String str, Number n) { |
2510 Number num = null; | 2564 Number num = null; |
2511 try { | 2565 try { |
2512 num = fmt.parse(str); | 2566 num = fmt.parse(str); |
2513 } catch (ParseException e) { | 2567 } catch (ParseException e) { |
2514 errln(e.getMessage()); | 2568 errln(e.getMessage()); |
2515 return; | 2569 return; |
2516 } | 2570 } |
2517 // A little tricky here -- make sure Double(12345.0) and | 2571 // A little tricky here -- make sure Double(12345.0) and |
2518 // Long(12345) match. | 2572 // Long(12345) match. |
2519 if (num.equals(n) || num.doubleValue() == n.doubleValue()) { | 2573 if (num.equals(n) || num.doubleValue() == n.doubleValue()) { |
2520 logln("Ok \"" + str + " = " + | 2574 logln("Ok \"" + str + " = " + |
2521 num); | 2575 num); |
2522 } else { | 2576 } else { |
2523 errln("expect_rbnf() parse test, locale " + fmt.getLocale(ULocale.VA
LID_LOCALE) + | 2577 errln("expect_rbnf() parse test, locale " + fmt.getLocale(ULocale.VA
LID_LOCALE) + |
2524 ", FAIL \"" + str + " = " + num + ", expected " + n); | 2578 ", FAIL \"" + str + " = " + num + ", expected " + n); |
2525 } | 2579 } |
2526 } | 2580 } |
2527 | 2581 |
2528 // Parse test (convenience) | 2582 /** Parse test (convenience) */ |
2529 public void expect(NumberFormat fmt, String str, double n) { | 2583 public void expect(NumberFormat fmt, String str, double n) { |
2530 expect(fmt, str, new Double(n)); | 2584 expect(fmt, str, new Double(n)); |
2531 } | 2585 } |
2532 | 2586 |
2533 // Parse test (convenience) | 2587 /** Parse test (convenience) */ |
2534 public void expect(NumberFormat fmt, String str, long n) { | 2588 public void expect(NumberFormat fmt, String str, long n) { |
2535 expect(fmt, str, new Long(n)); | 2589 expect(fmt, str, new Long(n)); |
2536 } | 2590 } |
2537 | 2591 |
2538 private void expectCurrency(NumberFormat nf, Currency curr, | 2592 private void expectCurrency(NumberFormat nf, Currency curr, |
2539 double value, String string) { | 2593 double value, String string) { |
2540 DecimalFormat fmt = (DecimalFormat) nf; | 2594 DecimalFormat fmt = (DecimalFormat) nf; |
2541 if (curr != null) { | 2595 if (curr != null) { |
2542 fmt.setCurrency(curr); | 2596 fmt.setCurrency(curr); |
2543 } | 2597 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2631 "0.100,5", // comma stops parse of decimal (no grouping) | 2685 "0.100,5", // comma stops parse of decimal (no grouping) |
2632 ".00", // leading decimal is ok, even with zeros | 2686 ".00", // leading decimal is ok, even with zeros |
2633 "1234567", // group separators are not required | 2687 "1234567", // group separators are not required |
2634 "12345, ", // comma not followed by digit is not a group sep
arator, but end of number | 2688 "12345, ", // comma not followed by digit is not a group sep
arator, but end of number |
2635 "1,234, ", // if group separator is present, group sizes mus
t be appropriate | 2689 "1,234, ", // if group separator is present, group sizes mus
t be appropriate |
2636 "1,234,567", // ...secondary too | 2690 "1,234,567", // ...secondary too |
2637 "0E", // an exponent not followed by zero or digits is
not an exponent | 2691 "0E", // an exponent not followed by zero or digits is
not an exponent |
2638 "00", // leading zero before zero - used to be error -
see ticket #7913 | 2692 "00", // leading zero before zero - used to be error -
see ticket #7913 |
2639 "012", // leading zero before digit - used to be error -
see ticket #7913 | 2693 "012", // leading zero before digit - used to be error -
see ticket #7913 |
2640 "0,456", // leading zero before group separator - used to
be error - see ticket #7913 | 2694 "0,456", // leading zero before group separator - used to
be error - see ticket #7913 |
| 2695 "999,999", // see ticket #6863 |
| 2696 "-99,999", // see ticket #6863 |
| 2697 "-999,999", // see ticket #6863 |
| 2698 "-9,999,999", // see ticket #6863 |
2641 }; | 2699 }; |
2642 String[] fail = { | 2700 String[] fail = { |
2643 "1,2", // wrong number of digits after group separator | 2701 "1,2", // wrong number of digits after group separator |
2644 ",0", // leading group separator before zero | 2702 ",0", // leading group separator before zero |
2645 ",1", // leading group separator before digit | 2703 ",1", // leading group separator before digit |
2646 ",.02", // leading group separator before decimal | 2704 ",.02", // leading group separator before decimal |
2647 "1,.02", // group separator before decimal | 2705 "1,.02", // group separator before decimal |
2648 "1,,200", // multiple group separators | 2706 "1,,200", // multiple group separators |
2649 "1,45", // wrong number of digits in primary group | 2707 "1,45", // wrong number of digits in primary group |
2650 "1,45 that", // wrong number of digits in primary group | 2708 "1,45 that", // wrong number of digits in primary group |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3225 } | 3283 } |
3226 } | 3284 } |
3227 | 3285 |
3228 /* | 3286 /* |
3229 * Tests the method public void setMinimumIntegerDigits(int newValue) | 3287 * Tests the method public void setMinimumIntegerDigits(int newValue) |
3230 */ | 3288 */ |
3231 @Test | 3289 @Test |
3232 public void TestSetMinimumIntegerDigits() { | 3290 public void TestSetMinimumIntegerDigits() { |
3233 NumberFormat nf = NumberFormat.getInstance(); | 3291 NumberFormat nf = NumberFormat.getInstance(); |
3234 // For valid array, it is displayed as {min value, max value} | 3292 // For valid array, it is displayed as {min value, max value} |
3235 // Since the property bag does pre-process values, the expression | 3293 // Tests when "if (minimumIntegerDigits > maximumIntegerDigits)" is true |
3236 // "if (getMinimumIntegerDigits() > getMaximumIntegerDigits())" might no
t always be true. | 3294 int[][] cases = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 1
0, 0 } }; |
3237 // int[][] cases = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 }, { 2, 1 },
{ 10, 0 } }; | 3295 int[] expectedMax = { 1, 1, 1, 2, 2, 10 }; |
3238 int[] expectedMax = { 0, 1, 1, 2, 2, 10 }; | 3296 if (cases.length != expectedMax.length) { |
3239 for (int i = 0; i < expectedMax.length; i++) { | 3297 errln("Can't continue test case method TestSetMinimumIntegerDigits " |
3240 nf.setMaximumIntegerDigits(expectedMax[i]); | 3298 + "since the test case arrays are unequal."); |
3241 // nf.setMinimumIntegerDigits(cases[i][0]); | 3299 } else { |
3242 if (nf.getMaximumIntegerDigits() != expectedMax[i]) { | 3300 for (int i = 0; i < cases.length; i++) { |
3243 errln("NumberFormat.setMinimumIntegerDigits(int newValue " | 3301 nf.setMinimumIntegerDigits(cases[i][0]); |
3244 + "did not return an expected result for parameter ; exp
ected " | 3302 nf.setMaximumIntegerDigits(cases[i][1]); |
3245 + expectedMax[i] + " but got " + nf.getMaximumIntegerDig
its()); | 3303 if (nf.getMaximumIntegerDigits() != expectedMax[i]) { |
| 3304 errln("NumberFormat.setMinimumIntegerDigits(int newValue " |
| 3305 + "did not return an expected result for parameter "
+ cases[i][0] + " and " + cases[i][1] |
| 3306 + " and expected " + expectedMax[i] + " but
got " + nf.getMaximumIntegerDigits()); |
| 3307 } |
3246 } | 3308 } |
3247 } | 3309 } |
3248 } | 3310 } |
3249 | 3311 |
3250 /* | 3312 /* |
3251 * Tests the method public int getRoundingMode() public void setRoundingMode
(int roundingMode) | 3313 * Tests the method public int getRoundingMode() public void setRoundingMode
(int roundingMode) |
3252 */ | 3314 */ |
3253 @Test | 3315 @Test |
3254 public void TestRoundingMode() { | 3316 public void TestRoundingMode() { |
3255 @SuppressWarnings("serial") | 3317 @SuppressWarnings("serial") |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3406 } | 3468 } |
3407 | 3469 |
3408 } | 3470 } |
3409 | 3471 |
3410 @Test | 3472 @Test |
3411 public void TestCurrencyAmountCoverage() { | 3473 public void TestCurrencyAmountCoverage() { |
3412 CurrencyAmount ca, cb; | 3474 CurrencyAmount ca, cb; |
3413 | 3475 |
3414 try { | 3476 try { |
3415 ca = new CurrencyAmount(null, null); | 3477 ca = new CurrencyAmount(null, null); |
3416 errln("IllegalArgumentException should have been thrown."); | 3478 errln("NullPointerException should have been thrown."); |
3417 } catch (IllegalArgumentException ex) { | 3479 } catch (NullPointerException ex) { |
3418 } | 3480 } |
3419 try { | 3481 try { |
3420 ca = new CurrencyAmount(new Integer(0), null); | 3482 ca = new CurrencyAmount(new Integer(0), null); |
3421 errln("IllegalArgumentException should have been thrown."); | 3483 errln("NullPointerException should have been thrown."); |
3422 } catch (IllegalArgumentException ex) { | 3484 } catch (NullPointerException ex) { |
3423 } | 3485 } |
3424 | 3486 |
3425 ca = new CurrencyAmount(new Integer(0), Currency.getInstance(new ULocale
("ja_JP"))); | 3487 ca = new CurrencyAmount(new Integer(0), Currency.getInstance(new ULocale
("ja_JP"))); |
3426 cb = new CurrencyAmount(new Integer(1), Currency.getInstance(new ULocale
("ja_JP"))); | 3488 cb = new CurrencyAmount(new Integer(1), Currency.getInstance(new ULocale
("ja_JP"))); |
3427 if (ca.equals(null)) { | 3489 if (ca.equals(null)) { |
3428 errln("Comparison should return false."); | 3490 errln("Comparison should return false."); |
3429 } | 3491 } |
3430 if (!ca.equals(ca)) { | 3492 if (!ca.equals(ca)) { |
3431 errln("Comparision should return true."); | 3493 errln("Comparision should return true."); |
3432 } | 3494 } |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4033 | 4095 |
4034 fmt2.setCurrency(Currency.getInstance("PKR")); | 4096 fmt2.setCurrency(Currency.getInstance("PKR")); |
4035 String PKR_changed = fmt2.format(123.567); | 4097 String PKR_changed = fmt2.format(123.567); |
4036 String PKR_changed_expected = "PKR124"; | 4098 String PKR_changed_expected = "PKR124"; |
4037 assertEquals("Test Currency Context", PKR_changed_expected, PKR_chan
ged); | 4099 assertEquals("Test Currency Context", PKR_changed_expected, PKR_chan
ged); |
4038 } | 4100 } |
4039 } | 4101 } |
4040 | 4102 |
4041 @Test | 4103 @Test |
4042 public void TestCurrencyWithMinMaxFractionDigits() { | 4104 public void TestCurrencyWithMinMaxFractionDigits() { |
4043 // FIXME: Figure out how to make this test case pass. | |
4044 DecimalFormat df = new DecimalFormat(); | 4105 DecimalFormat df = new DecimalFormat(); |
4045 df.applyPattern("¤#,##0.00"); | 4106 df.applyPattern("¤#,##0.00"); |
4046 df.setCurrency(Currency.getInstance("USD")); | 4107 df.setCurrency(Currency.getInstance("USD")); |
4047 assertEquals("Basic currency format fails", "$1.23", df.format(1.234)); | 4108 assertEquals("Basic currency format fails", "$1.23", df.format(1.234)); |
4048 df.setMaximumFractionDigits(4); | 4109 df.setMaximumFractionDigits(4); |
4049 assertEquals("Currency with max fraction == 4", "$1.234", df.format(1.23
4)); | 4110 assertEquals("Currency with max fraction == 4", "$1.234", df.format(1.23
4)); |
4050 df.setMinimumFractionDigits(4); | 4111 df.setMinimumFractionDigits(4); |
4051 assertEquals("Currency with min fraction == 4", "$1.2340", df.format(1.2
34)); | 4112 assertEquals("Currency with min fraction == 4", "$1.2340", df.format(1.2
34)); |
4052 } | 4113 } |
4053 | 4114 |
4054 @Test | 4115 @Test |
4055 public void TestParseRequiredDecimalPoint() { | 4116 public void TestParseRequiredDecimalPoint() { |
4056 | 4117 |
4057 String[] testPattern = { "00.####", "00.0", "00" }; | 4118 String[] testPattern = { "00.####", "00.0", "00" }; |
4058 | 4119 |
4059 String value2Parse = "99"; | 4120 String value2Parse = "99"; |
| 4121 String value2ParseWithDecimal = "99.9"; |
4060 double parseValue = 99; | 4122 double parseValue = 99; |
| 4123 double parseValueWithDecimal = 99.9; |
4061 DecimalFormat parser = new DecimalFormat(); | 4124 DecimalFormat parser = new DecimalFormat(); |
4062 double result; | 4125 double result; |
4063 boolean hasDecimalPoint; | 4126 boolean hasDecimalPoint; |
4064 for (int i = 0; i < testPattern.length; i++) { | 4127 for (int i = 0; i < testPattern.length; i++) { |
4065 parser.applyPattern(testPattern[i]); | 4128 parser.applyPattern(testPattern[i]); |
4066 hasDecimalPoint = testPattern[i].contains("."); | 4129 hasDecimalPoint = testPattern[i].contains("."); |
4067 | 4130 |
4068 parser.setDecimalPatternMatchRequired(false); | 4131 parser.setDecimalPatternMatchRequired(false); |
4069 try { | 4132 try { |
4070 result = parser.parse(value2Parse).doubleValue(); | 4133 result = parser.parse(value2Parse).doubleValue(); |
4071 assertEquals("wrong parsed value", parseValue, result); | 4134 assertEquals("wrong parsed value", parseValue, result); |
4072 } catch (ParseException e) { | 4135 } catch (ParseException e) { |
4073 TestFmwk.errln("Parsing " + value2Parse + " should have succeede
d with " + testPattern[i] + | 4136 TestFmwk.errln("Parsing " + value2Parse + " should have succeede
d with " + testPattern[i] + |
| 4137 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); |
| 4138 } |
| 4139 try { |
| 4140 result = parser.parse(value2ParseWithDecimal).doubleValue(); |
| 4141 assertEquals("wrong parsed value", parseValueWithDecimal, result
); |
| 4142 } catch (ParseException e) { |
| 4143 TestFmwk.errln("Parsing " + value2ParseWithDecimal + " should ha
ve succeeded with " + testPattern[i] + |
4074 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); | 4144 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); |
4075 } | 4145 } |
4076 | 4146 |
4077 parser.setDecimalPatternMatchRequired(true); | 4147 parser.setDecimalPatternMatchRequired(true); |
4078 try { | 4148 try { |
4079 result = parser.parse(value2Parse).doubleValue(); | 4149 result = parser.parse(value2Parse).doubleValue(); |
4080 if(hasDecimalPoint){ | 4150 if(hasDecimalPoint){ |
4081 TestFmwk.errln("Parsing " + value2Parse + " should NOT have
succeeded with " + testPattern[i] + | 4151 TestFmwk.errln("Parsing " + value2Parse + " should NOT have
succeeded with " + testPattern[i] + |
4082 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); | 4152 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); |
4083 } | 4153 } |
4084 } catch (ParseException e) { | 4154 } catch (ParseException e) { |
4085 // OK, should fail | 4155 // OK, should fail |
4086 } | 4156 } |
| 4157 try { |
| 4158 result = parser.parse(value2ParseWithDecimal).doubleValue(); |
| 4159 if(!hasDecimalPoint){ |
| 4160 TestFmwk.errln("Parsing " + value2ParseWithDecimal + " shoul
d NOT have succeeded with " + testPattern[i] + |
| 4161 " and isDecimalPointMatchRequired set to: " + parser
.isDecimalPatternMatchRequired()); |
| 4162 } |
| 4163 } catch (ParseException e) { |
| 4164 // OK, should fail |
| 4165 } |
4087 } | 4166 } |
4088 } | 4167 } |
4089 | 4168 |
4090 @Test | 4169 @Test |
4091 public void TestCurrFmtNegSameAsPositive() { | 4170 public void TestCurrFmtNegSameAsPositive() { |
4092 DecimalFormatSymbols decfmtsym = DecimalFormatSymbols.getInstance(Locale
.US); | 4171 DecimalFormatSymbols decfmtsym = DecimalFormatSymbols.getInstance(Locale
.US); |
4093 decfmtsym.setMinusSign('\u200B'); // ZERO WIDTH SPACE, in ICU4J cannot s
et to empty string | 4172 decfmtsym.setMinusSign('\u200B'); // ZERO WIDTH SPACE, in ICU4J cannot s
et to empty string |
4094 DecimalFormat decfmt = new DecimalFormat("\u00A4#,##0.00;-\u00A4#,##0.00
", decfmtsym); | 4173 DecimalFormat decfmt = new DecimalFormat("\u00A4#,##0.00;-\u00A4#,##0.00
", decfmtsym); |
4095 String currFmtResult = decfmt.format(-100.0); | 4174 String currFmtResult = decfmt.format(-100.0); |
4096 if (!currFmtResult.equals("\u200B$100.00")) { | 4175 if (!currFmtResult.equals("\u200B$100.00")) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4210 public void TestNPEIssue11735() { | 4289 public void TestNPEIssue11735() { |
4211 DecimalFormat fmt = new DecimalFormat("0", new DecimalFormatSymbols(new
ULocale("en"))); | 4290 DecimalFormat fmt = new DecimalFormat("0", new DecimalFormatSymbols(new
ULocale("en"))); |
4212 ParsePosition ppos = new ParsePosition(0); | 4291 ParsePosition ppos = new ParsePosition(0); |
4213 assertEquals("Currency symbol missing in parse. Expect null result.", | 4292 assertEquals("Currency symbol missing in parse. Expect null result.", |
4214 fmt.parseCurrency("53.45", ppos), null); | 4293 fmt.parseCurrency("53.45", ppos), null); |
4215 } | 4294 } |
4216 | 4295 |
4217 private void CompareAttributedCharacterFormatOutput(AttributedCharacterItera
tor iterator, | 4296 private void CompareAttributedCharacterFormatOutput(AttributedCharacterItera
tor iterator, |
4218 List<FieldContainer> expected, String formattedOutput) { | 4297 List<FieldContainer> expected, String formattedOutput) { |
4219 | 4298 |
4220 List<FieldContainer> result = new ArrayList<>(); | 4299 List<FieldContainer> result = new ArrayList<FieldContainer>(); |
4221 while (iterator.getIndex() != iterator.getEndIndex()) { | 4300 while (iterator.getIndex() != iterator.getEndIndex()) { |
4222 int start = iterator.getRunStart(); | 4301 int start = iterator.getRunStart(); |
4223 int end = iterator.getRunLimit(); | 4302 int end = iterator.getRunLimit(); |
4224 Iterator it = iterator.getAttributes().keySet().iterator(); | 4303 Iterator it = iterator.getAttributes().keySet().iterator(); |
4225 AttributedCharacterIterator.Attribute attribute = (AttributedCharact
erIterator.Attribute) it.next(); | 4304 AttributedCharacterIterator.Attribute attribute = (AttributedCharact
erIterator.Attribute) it.next(); |
4226 Object value = iterator.getAttribute(attribute); | 4305 Object value = iterator.getAttribute(attribute); |
4227 result.add(new FieldContainer(start, end, attribute, value)); | 4306 result.add(new FieldContainer(start, end, attribute, value)); |
4228 iterator.setIndex(end); | 4307 iterator.setIndex(end); |
4229 } | 4308 } |
4230 assertEquals("Comparing vector length for " + formattedOutput, | 4309 assertEquals("Comparing vector length for " + formattedOutput, |
4231 expected.size(), result.size()); | 4310 expected.size(), result.size()); |
4232 | 4311 |
4233 if (!expected.containsAll(result)) { | 4312 if (!expected.containsAll(result)) { |
4234 // Print information on the differences. | 4313 // Print information on the differences. |
4235 for (int i = 0; i < expected.size(); i++) { | 4314 for (int i = 0; i < expected.size(); i++) { |
4236 System.out.println(" expected[" + i + "] =" + | 4315 System.out.println(" expected[" + i + "] =" + |
4237 expected.get(i).start + " " + | 4316 expected.get(i).start + " " + |
4238 expected.get(i).end + " " + | 4317 expected.get(i).end + " " + |
4239 expected.get(i).attribute + " " + | 4318 expected.get(i).attribute + " " + |
4240 expected.get(i).value); | 4319 expected.get(i).value); |
4241 System.out.println(" result[" + i + "] =" + | 4320 System.out.println(" result[" + i + "] =" + |
4242 result.get(i).start + " " + | 4321 result.get(i).start + " " + |
4243 result.get(i).end + " " + | 4322 result.get(i).end + " " + |
4244 result.get(i).attribute + " " + | 4323 result.get(i).attribute + " " + |
4245 result.get(i).value); | 4324 result.get(i).value); |
4246 } | 4325 } |
4247 } | 4326 } |
4248 // TODO: restore when #11914 is fixed. | 4327 assertTrue("Comparing vector results for " + formattedOutput, expected.c
ontainsAll(result)); |
4249 // assertTrue("Comparing vector results for " + formattedOutput, | |
4250 // expected.containsAll(result)); | |
4251 } | 4328 } |
4252 | 4329 |
4253 // Testing for Issue 11914, missing FieldPositions for some field types. | 4330 // Testing for Issue 11914, missing FieldPositions for some field types. |
4254 @Test | 4331 @Test |
4255 public void TestNPEIssue11914() { | 4332 public void TestNPEIssue11914() { |
4256 // First test: Double value with grouping separators. | 4333 // First test: Double value with grouping separators. |
4257 List<FieldContainer> v1 = new ArrayList<>(7); | 4334 List<FieldContainer> v1 = new ArrayList<FieldContainer>(7); |
4258 v1.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER)); | 4335 v1.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER)); |
4259 v1.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR)); | 4336 v1.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR)); |
4260 v1.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER)); | 4337 v1.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER)); |
4261 v1.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR)); | 4338 v1.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR)); |
4262 v1.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER)); | 4339 v1.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER)); |
4263 v1.add(new FieldContainer(11, 12, NumberFormat.Field.DECIMAL_SEPARATOR))
; | 4340 v1.add(new FieldContainer(11, 12, NumberFormat.Field.DECIMAL_SEPARATOR))
; |
4264 v1.add(new FieldContainer(12, 15, NumberFormat.Field.FRACTION)); | 4341 v1.add(new FieldContainer(12, 15, NumberFormat.Field.FRACTION)); |
4265 | 4342 |
4266 Number number = new Double(123456789.9753); | 4343 Number number = new Double(123456789.9753); |
4267 ULocale usLoc = new ULocale("en-US"); | 4344 ULocale usLoc = new ULocale("en-US"); |
4268 DecimalFormatSymbols US = new DecimalFormatSymbols(usLoc); | 4345 DecimalFormatSymbols US = new DecimalFormatSymbols(usLoc); |
4269 | 4346 |
4270 NumberFormat outFmt = NumberFormat.getNumberInstance(usLoc); | 4347 NumberFormat outFmt = NumberFormat.getNumberInstance(usLoc); |
4271 String numFmtted = outFmt.format(number); | 4348 String numFmtted = outFmt.format(number); |
4272 AttributedCharacterIterator iterator = | 4349 AttributedCharacterIterator iterator = |
4273 outFmt.formatToCharacterIterator(number); | 4350 outFmt.formatToCharacterIterator(number); |
4274 CompareAttributedCharacterFormatOutput(iterator, v1, numFmtted); | 4351 CompareAttributedCharacterFormatOutput(iterator, v1, numFmtted); |
4275 | 4352 |
4276 // Second test: Double with scientific notation formatting. | 4353 // Second test: Double with scientific notation formatting. |
4277 List<FieldContainer> v2 = new ArrayList<>(7); | 4354 List<FieldContainer> v2 = new ArrayList<FieldContainer>(7); |
4278 v2.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER)); | 4355 v2.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER)); |
4279 v2.add(new FieldContainer(1, 2, NumberFormat.Field.DECIMAL_SEPARATOR)); | 4356 v2.add(new FieldContainer(1, 2, NumberFormat.Field.DECIMAL_SEPARATOR)); |
4280 v2.add(new FieldContainer(2, 5, NumberFormat.Field.FRACTION)); | 4357 v2.add(new FieldContainer(2, 5, NumberFormat.Field.FRACTION)); |
4281 v2.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL)); | 4358 v2.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL)); |
4282 v2.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SIGN)); | 4359 v2.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SIGN)); |
4283 v2.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT)); | 4360 v2.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT)); |
4284 DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US); | 4361 DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US); |
4285 | 4362 |
4286 numFmtted = fmt2.format(number); | 4363 numFmtted = fmt2.format(number); |
4287 iterator = fmt2.formatToCharacterIterator(number); | 4364 iterator = fmt2.formatToCharacterIterator(number); |
4288 CompareAttributedCharacterFormatOutput(iterator, v2, numFmtted); | 4365 CompareAttributedCharacterFormatOutput(iterator, v2, numFmtted); |
4289 | 4366 |
4290 // Third test. BigInteger with grouping separators. | 4367 // Third test. BigInteger with grouping separators. |
4291 List<FieldContainer> v3 = new ArrayList<>(7); | 4368 List<FieldContainer> v3 = new ArrayList<FieldContainer>(7); |
4292 v3.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN)); | 4369 v3.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN)); |
4293 v3.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER)); | 4370 v3.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER)); |
4294 v3.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR)); | 4371 v3.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR)); |
4295 v3.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER)); | 4372 v3.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER)); |
4296 v3.add(new FieldContainer(6, 7, NumberFormat.Field.GROUPING_SEPARATOR)); | 4373 v3.add(new FieldContainer(6, 7, NumberFormat.Field.GROUPING_SEPARATOR)); |
4297 v3.add(new FieldContainer(7, 10, NumberFormat.Field.INTEGER)); | 4374 v3.add(new FieldContainer(7, 10, NumberFormat.Field.INTEGER)); |
4298 v3.add(new FieldContainer(10, 11, NumberFormat.Field.GROUPING_SEPARATOR)
); | 4375 v3.add(new FieldContainer(10, 11, NumberFormat.Field.GROUPING_SEPARATOR)
); |
4299 v3.add(new FieldContainer(11, 14, NumberFormat.Field.INTEGER)); | 4376 v3.add(new FieldContainer(11, 14, NumberFormat.Field.INTEGER)); |
4300 v3.add(new FieldContainer(14, 15, NumberFormat.Field.GROUPING_SEPARATOR)
); | 4377 v3.add(new FieldContainer(14, 15, NumberFormat.Field.GROUPING_SEPARATOR)
); |
4301 v3.add(new FieldContainer(15, 18, NumberFormat.Field.INTEGER)); | 4378 v3.add(new FieldContainer(15, 18, NumberFormat.Field.INTEGER)); |
4302 v3.add(new FieldContainer(18, 19, NumberFormat.Field.GROUPING_SEPARATOR)
); | 4379 v3.add(new FieldContainer(18, 19, NumberFormat.Field.GROUPING_SEPARATOR)
); |
4303 v3.add(new FieldContainer(19, 22, NumberFormat.Field.INTEGER)); | 4380 v3.add(new FieldContainer(19, 22, NumberFormat.Field.INTEGER)); |
4304 v3.add(new FieldContainer(22, 23, NumberFormat.Field.GROUPING_SEPARATOR)
); | 4381 v3.add(new FieldContainer(22, 23, NumberFormat.Field.GROUPING_SEPARATOR)
); |
4305 v3.add(new FieldContainer(23, 26, NumberFormat.Field.INTEGER)); | 4382 v3.add(new FieldContainer(23, 26, NumberFormat.Field.INTEGER)); |
4306 BigInteger bigNumberInt = new BigInteger("-1234567890246813579"); | 4383 BigInteger bigNumberInt = new BigInteger("-1234567890246813579"); |
4307 String fmtNumberBigInt = outFmt.format(bigNumberInt); | 4384 String fmtNumberBigInt = outFmt.format(bigNumberInt); |
4308 | 4385 |
4309 iterator = outFmt.formatToCharacterIterator(bigNumberInt); | 4386 iterator = outFmt.formatToCharacterIterator(bigNumberInt); |
4310 CompareAttributedCharacterFormatOutput(iterator, v3, fmtNumberBigInt); | 4387 CompareAttributedCharacterFormatOutput(iterator, v3, fmtNumberBigInt); |
4311 | 4388 |
4312 // Fourth test: BigDecimal with exponential formatting. | 4389 // Fourth test: BigDecimal with exponential formatting. |
4313 List<FieldContainer> v4 = new ArrayList<>(7); | 4390 List<FieldContainer> v4 = new ArrayList<FieldContainer>(7); |
4314 v4.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN)); | 4391 v4.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN)); |
4315 v4.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER)); | 4392 v4.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER)); |
4316 v4.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR)); | 4393 v4.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR)); |
4317 v4.add(new FieldContainer(3, 6, NumberFormat.Field.FRACTION)); | 4394 v4.add(new FieldContainer(3, 6, NumberFormat.Field.FRACTION)); |
4318 v4.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL)); | 4395 v4.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL)); |
4319 v4.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN)); | 4396 v4.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN)); |
4320 v4.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT)); | 4397 v4.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT)); |
4321 | 4398 |
4322 java.math.BigDecimal numberBigD = new java.math.BigDecimal(-123456789); | 4399 java.math.BigDecimal numberBigD = new java.math.BigDecimal(-123456789); |
4323 String fmtNumberBigDExp = fmt2.format(numberBigD); | 4400 String fmtNumberBigDExp = fmt2.format(numberBigD); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4584 if (currSpacingPatn==null || currSpacingPatn.length() == 0) { | 4661 if (currSpacingPatn==null || currSpacingPatn.length() == 0) { |
4585 errln("locale ar, getPatternForCurrencySpacing returns null or 0-len
gth string"); | 4662 errln("locale ar, getPatternForCurrencySpacing returns null or 0-len
gth string"); |
4586 } | 4663 } |
4587 | 4664 |
4588 DecimalFormat currAcctFormat = (DecimalFormat)NumberFormat.getInstance(a
rLocale, NumberFormat.ACCOUNTINGCURRENCYSTYLE); | 4665 DecimalFormat currAcctFormat = (DecimalFormat)NumberFormat.getInstance(a
rLocale, NumberFormat.ACCOUNTINGCURRENCYSTYLE); |
4589 String currAcctPatn = currAcctFormat.toPattern(); | 4666 String currAcctPatn = currAcctFormat.toPattern(); |
4590 if (currAcctPatn==null || currAcctPatn.length() == 0) { | 4667 if (currAcctPatn==null || currAcctPatn.length() == 0) { |
4591 errln("locale ar, toPattern for ACCOUNTINGCURRENCYSTYLE returns null
or 0-length string"); | 4668 errln("locale ar, toPattern for ACCOUNTINGCURRENCYSTYLE returns null
or 0-length string"); |
4592 } | 4669 } |
4593 } | 4670 } |
| 4671 |
| 4672 @Test |
| 4673 public void Test10436() { |
| 4674 DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLI
SH); |
| 4675 df.setRoundingMode(MathContext.ROUND_CEILING); |
| 4676 df.setMinimumFractionDigits(0); |
| 4677 df.setMaximumFractionDigits(0); |
| 4678 assertEquals("-.99 should round toward infinity", "-0", df.format(-0.99)
); |
| 4679 } |
| 4680 |
| 4681 @Test |
| 4682 public void Test10765() { |
| 4683 NumberFormat fmt = NumberFormat.getInstance(new ULocale("en")); |
| 4684 fmt.setMinimumIntegerDigits(10); |
| 4685 FieldPosition pos = new FieldPosition(NumberFormat.Field.GROUPING_SEPARA
TOR); |
| 4686 fmt.format(1234, new StringBuffer(), pos); |
| 4687 assertEquals("FieldPosition should report the first occurence", 1, pos.g
etBeginIndex()); |
| 4688 assertEquals("FieldPosition should report the first occurence", 2, pos.g
etEndIndex()); |
| 4689 } |
| 4690 |
| 4691 @Test |
| 4692 public void Test10997() { |
| 4693 NumberFormat fmt = NumberFormat.getCurrencyInstance(new ULocale("en-US")
); |
| 4694 fmt.setMinimumFractionDigits(4); |
| 4695 fmt.setMaximumFractionDigits(4); |
| 4696 String str1 = fmt.format(new CurrencyAmount(123.45, Currency.getInstance
("USD"))); |
| 4697 String str2 = fmt.format(new CurrencyAmount(123.45, Currency.getInstance
("EUR"))); |
| 4698 assertEquals("minFrac 4 should be respected in default currency", "$123.
4500", str1); |
| 4699 assertEquals("minFrac 4 should be respected in different currency", "€12
3.4500", str2); |
| 4700 } |
| 4701 |
| 4702 @Test |
| 4703 public void Test11020() { |
| 4704 DecimalFormatSymbols sym = new DecimalFormatSymbols(ULocale.FRANCE); |
| 4705 DecimalFormat fmt = new DecimalFormat("0.05E0", sym); |
| 4706 String result = fmt.format(12301.2).replace('\u00a0', ' '); |
| 4707 assertEquals("Rounding increment should be applied after magnitude scali
ng", "1,25E4", result); |
| 4708 } |
| 4709 |
| 4710 @Test |
| 4711 public void Test11025() { |
| 4712 String pattern = "¤¤ **####0.00"; |
| 4713 DecimalFormatSymbols sym = new DecimalFormatSymbols(ULocale.FRANCE); |
| 4714 DecimalFormat fmt = new DecimalFormat(pattern, sym); |
| 4715 String result = fmt.format(433.0); |
| 4716 assertEquals("Number should be padded to 11 characters", "EUR *433,00",
result); |
| 4717 } |
| 4718 |
| 4719 @Test |
| 4720 public void Test11640() { |
| 4721 DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(); |
| 4722 df.applyPattern("¤¤¤ 0"); |
| 4723 String result = df.getPositivePrefix(); |
| 4724 assertEquals("Triple-currency should give long name on getPositivePrefix
", "US dollars ", result); |
| 4725 } |
| 4726 |
| 4727 @Test |
| 4728 public void Test11645() { |
| 4729 String pattern = "#,##0.0#"; |
| 4730 DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(); |
| 4731 fmt.applyPattern(pattern); |
| 4732 DecimalFormat fmtCopy; |
| 4733 |
| 4734 final int newMultiplier = 37; |
| 4735 fmtCopy = (DecimalFormat) fmt.clone(); |
| 4736 assertNotEquals("Value before setter", fmtCopy.getMultiplier(), newMulti
plier); |
| 4737 fmtCopy.setMultiplier(newMultiplier); |
| 4738 assertEquals("Value after setter", fmtCopy.getMultiplier(), newMultiplie
r); |
| 4739 fmtCopy.applyPattern(pattern); |
| 4740 assertEquals("Value after applyPattern", fmtCopy.getMultiplier(), newMul
tiplier); |
| 4741 assertFalse("multiplier", fmt.equals(fmtCopy)); |
| 4742 |
| 4743 final int newRoundingMode = RoundingMode.CEILING.ordinal(); |
| 4744 fmtCopy = (DecimalFormat) fmt.clone(); |
| 4745 assertNotEquals("Value before setter", fmtCopy.getRoundingMode(), newRou
ndingMode); |
| 4746 fmtCopy.setRoundingMode(newRoundingMode); |
| 4747 assertEquals("Value after setter", fmtCopy.getRoundingMode(), newRoundin
gMode); |
| 4748 fmtCopy.applyPattern(pattern); |
| 4749 assertEquals("Value after applyPattern", fmtCopy.getRoundingMode(), newR
oundingMode); |
| 4750 assertFalse("roundingMode", fmt.equals(fmtCopy)); |
| 4751 |
| 4752 final Currency newCurrency = Currency.getInstance("EAT"); |
| 4753 fmtCopy = (DecimalFormat) fmt.clone(); |
| 4754 assertNotEquals("Value before setter", fmtCopy.getCurrency(), newCurrenc
y); |
| 4755 fmtCopy.setCurrency(newCurrency); |
| 4756 assertEquals("Value after setter", fmtCopy.getCurrency(), newCurrency); |
| 4757 fmtCopy.applyPattern(pattern); |
| 4758 assertEquals("Value after applyPattern", fmtCopy.getCurrency(), newCurre
ncy); |
| 4759 assertFalse("currency", fmt.equals(fmtCopy)); |
| 4760 |
| 4761 final CurrencyUsage newCurrencyUsage = CurrencyUsage.CASH; |
| 4762 fmtCopy = (DecimalFormat) fmt.clone(); |
| 4763 assertNotEquals("Value before setter", fmtCopy.getCurrencyUsage(), newCu
rrencyUsage); |
| 4764 fmtCopy.setCurrencyUsage(CurrencyUsage.CASH); |
| 4765 assertEquals("Value after setter", fmtCopy.getCurrencyUsage(), newCurren
cyUsage); |
| 4766 fmtCopy.applyPattern(pattern); |
| 4767 assertEquals("Value after applyPattern", fmtCopy.getCurrencyUsage(), new
CurrencyUsage); |
| 4768 assertFalse("currencyUsage", fmt.equals(fmtCopy)); |
| 4769 } |
| 4770 |
| 4771 @Test |
| 4772 public void Test11646() { |
| 4773 DecimalFormatSymbols symbols = new DecimalFormatSymbols(new ULocale("en_
US")); |
| 4774 String pattern = "\u00a4\u00a4\u00a4 0.00 %\u00a4\u00a4"; |
| 4775 DecimalFormat fmt = new DecimalFormat(pattern, symbols); |
| 4776 |
| 4777 // Test equality with affixes. set affix methods can't capture special |
| 4778 // characters which is why equality should fail. |
| 4779 { |
| 4780 DecimalFormat fmtCopy = (DecimalFormat) fmt.clone(); |
| 4781 assertEquals("", fmt, fmtCopy); |
| 4782 fmtCopy.setPositivePrefix(fmtCopy.getPositivePrefix()); |
| 4783 assertNotEquals("", fmt, fmtCopy); |
| 4784 } |
| 4785 { |
| 4786 DecimalFormat fmtCopy = (DecimalFormat) fmt.clone(); |
| 4787 assertEquals("", fmt, fmtCopy); |
| 4788 fmtCopy.setPositiveSuffix(fmtCopy.getPositiveSuffix()); |
| 4789 assertNotEquals("", fmt, fmtCopy); |
| 4790 } |
| 4791 { |
| 4792 DecimalFormat fmtCopy = (DecimalFormat) fmt.clone(); |
| 4793 assertEquals("", fmt, fmtCopy); |
| 4794 fmtCopy.setNegativePrefix(fmtCopy.getNegativePrefix()); |
| 4795 assertNotEquals("", fmt, fmtCopy); |
| 4796 } |
| 4797 { |
| 4798 DecimalFormat fmtCopy = (DecimalFormat) fmt.clone(); |
| 4799 assertEquals("", fmt, fmtCopy); |
| 4800 fmtCopy.setNegativeSuffix(fmtCopy.getNegativeSuffix()); |
| 4801 assertNotEquals("", fmt, fmtCopy); |
| 4802 } |
| 4803 } |
| 4804 |
| 4805 @Test |
| 4806 public void Test11648() { |
| 4807 DecimalFormat df = new DecimalFormat("0.00"); |
| 4808 df.setScientificNotation(true); |
| 4809 String pat = df.toPattern(); |
| 4810 assertEquals("A valid scientific notation pattern should be produced", "
0.00E0", pat); |
| 4811 } |
| 4812 |
| 4813 @Test |
| 4814 public void Test11649() { |
| 4815 String pattern = "\u00a4\u00a4\u00a4 0.00"; |
| 4816 DecimalFormat fmt = new DecimalFormat(pattern); |
| 4817 fmt.setCurrency(Currency.getInstance("USD")); |
| 4818 assertEquals("Triple currency sign should format long name", "US dollars
12.34", fmt.format(12.34)); |
| 4819 |
| 4820 String newPattern = fmt.toPattern(); |
| 4821 assertEquals("Should produce a valid pattern", pattern, newPattern); |
| 4822 |
| 4823 DecimalFormat fmt2 = new DecimalFormat(newPattern); |
| 4824 fmt2.setCurrency(Currency.getInstance("USD")); |
| 4825 assertEquals("Triple currency sign pattern should round-trip", "US dolla
rs 12.34", fmt2.format(12.34)); |
| 4826 } |
| 4827 |
| 4828 @Test |
| 4829 public void Test11686() { |
| 4830 DecimalFormat df = new DecimalFormat(); |
| 4831 df.setPositiveSuffix("0K"); |
| 4832 df.setNegativeSuffix("0N"); |
| 4833 expect2(df, 123, "1230K"); |
| 4834 expect2(df, -123, "1230N"); |
| 4835 } |
| 4836 |
| 4837 @Test |
| 4838 public void Test11839() { |
| 4839 DecimalFormatSymbols dfs = new DecimalFormatSymbols(ULocale.ENGLISH); |
| 4840 dfs.setMinusSign('∸'); |
| 4841 dfs.setPlusSign('∔'); // ∔ U+2214 DOT PLUS |
| 4842 DecimalFormat df = new DecimalFormat("0.00+;0.00-", dfs); |
| 4843 String result = df.format(-1.234); |
| 4844 assertEquals("Locale-specific minus sign should be used", "1.23∸", resul
t); |
| 4845 result = df.format(1.234); |
| 4846 assertEquals("Locale-specific plus sign should be used", "1.23∔", result
); |
| 4847 } |
| 4848 |
| 4849 @Test |
| 4850 public void Test12753() { |
| 4851 ULocale locale = new ULocale("en-US"); |
| 4852 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); |
| 4853 symbols.setDecimalSeparator('*'); |
| 4854 DecimalFormat df = new DecimalFormat("0.00", symbols); |
| 4855 df.setDecimalPatternMatchRequired(true); |
| 4856 try { |
| 4857 df.parse("123"); |
| 4858 fail("Parsing integer succeeded even though setDecimalPatternMatchRe
quired was set"); |
| 4859 } catch (ParseException e) { |
| 4860 // Parse failed (expected) |
| 4861 } |
| 4862 } |
| 4863 |
| 4864 @Test |
| 4865 public void Test12962() { |
| 4866 String pat = "**0.00"; |
| 4867 DecimalFormat df = new DecimalFormat(pat); |
| 4868 String newPat = df.toPattern(); |
| 4869 assertEquals("Format width changed upon calling applyPattern", pat.lengt
h(), newPat.length()); |
| 4870 } |
| 4871 |
| 4872 @Test |
| 4873 public void Test10354() { |
| 4874 DecimalFormatSymbols dfs = new DecimalFormatSymbols(); |
| 4875 dfs.setNaN(""); |
| 4876 DecimalFormat df = new DecimalFormat(); |
| 4877 df.setDecimalFormatSymbols(dfs); |
| 4878 try { |
| 4879 df.formatToCharacterIterator(Double.NaN); |
| 4880 // pass |
| 4881 } catch (IllegalArgumentException e) { |
| 4882 throw new AssertionError(e); |
| 4883 } |
| 4884 } |
| 4885 |
| 4886 @Test |
| 4887 public void Test11913() { |
| 4888 NumberFormat df = DecimalFormat.getInstance(); |
| 4889 String result = df.format(new BigDecimal("1.23456789E400")); |
| 4890 assertEquals("Should format more than 309 digits", "12,345,678", result.
substring(0, 10)); |
| 4891 assertEquals("Should format more than 309 digits", 534, result.length())
; |
| 4892 } |
| 4893 |
| 4894 @Test |
| 4895 public void Test12045() { |
| 4896 if (logKnownIssue("12045", "XSU is missing from fr")) { return; } |
| 4897 |
| 4898 NumberFormat nf = NumberFormat.getInstance(new ULocale("fr"), NumberForm
at.PLURALCURRENCYSTYLE); |
| 4899 ParsePosition ppos = new ParsePosition(0); |
| 4900 try { |
| 4901 CurrencyAmount result = nf.parseCurrency("2,34 XSU", ppos); |
| 4902 assertEquals("Parsing should succeed on XSU", |
| 4903 new CurrencyAmount(2.34, Currency.getInstance("XSU")),
result); |
| 4904 // pass |
| 4905 } catch (Exception e) { |
| 4906 throw new AssertionError("Should have been able to parse XSU", e); |
| 4907 } |
| 4908 } |
| 4909 |
| 4910 @Test |
| 4911 public void Test11739() { |
| 4912 NumberFormat nf = NumberFormat.getCurrencyInstance(new ULocale("sr_BA"))
; |
| 4913 ((DecimalFormat) nf).applyPattern("0.0 ¤¤¤"); |
| 4914 ParsePosition ppos = new ParsePosition(0); |
| 4915 CurrencyAmount result = nf.parseCurrency("1.500 амерички долар", ppos); |
| 4916 assertEquals("Should parse to 1500 USD", new CurrencyAmount(1500, Curren
cy.getInstance("USD")), result); |
| 4917 } |
| 4918 |
| 4919 @Test |
| 4920 public void Test11647() { |
| 4921 DecimalFormat df = new DecimalFormat(); |
| 4922 df.applyPattern("¤¤¤¤#"); |
| 4923 String actual = df.format(123); |
| 4924 assertEquals("Should replace 4 currency signs with U+FFFD", "\uFFFD123",
actual); |
| 4925 } |
| 4926 |
| 4927 @Test |
| 4928 public void Test12567() { |
| 4929 DecimalFormat df1 = (DecimalFormat) NumberFormat.getInstance(NumberForma
t.PLURALCURRENCYSTYLE); |
| 4930 DecimalFormat df2 = (DecimalFormat) NumberFormat.getInstance(NumberForma
t.NUMBERSTYLE); |
| 4931 df2.setCurrency(df1.getCurrency()); |
| 4932 df2.setCurrencyPluralInfo(df1.getCurrencyPluralInfo()); |
| 4933 df1.applyPattern("0.00"); |
| 4934 df2.applyPattern("0.00"); |
| 4935 assertEquals("df1 == df2", df1, df2); |
| 4936 assertEquals("df2 == df1", df2, df1); |
| 4937 df2.setPositivePrefix("abc"); |
| 4938 assertNotEquals("df1 != df2", df1, df2); |
| 4939 assertNotEquals("df2 != df1", df2, df1); |
| 4940 } |
| 4941 |
| 4942 @Test |
| 4943 public void testPercentZero() { |
| 4944 DecimalFormat df = (DecimalFormat) NumberFormat.getPercentInstance(); |
| 4945 String actual = df.format(0); |
| 4946 assertEquals("Should have one zero digit", "0%", actual); |
| 4947 } |
| 4948 |
| 4949 @Test |
| 4950 public void testCurrencyZeroRounding() { |
| 4951 DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(); |
| 4952 df.setMaximumFractionDigits(0); |
| 4953 String actual = df.format(0); |
| 4954 assertEquals("Should have zero fraction digits", "$0", actual); |
| 4955 } |
| 4956 |
| 4957 @Test |
| 4958 public void testCustomCurrencySymbol() { |
| 4959 DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(); |
| 4960 df.setCurrency(Currency.getInstance("USD")); |
| 4961 DecimalFormatSymbols symbols = df.getDecimalFormatSymbols(); |
| 4962 symbols.setCurrencySymbol("#"); |
| 4963 df.setDecimalFormatSymbols(symbols); |
| 4964 String actual = df.format(123); |
| 4965 assertEquals("Should use '#' instad of '$'", "#123.00", actual); |
| 4966 } |
| 4967 |
| 4968 @Test |
| 4969 public void TestBasicSerializationRoundTrip() throws IOException, ClassNotFo
undException { |
| 4970 DecimalFormat df0 = new DecimalFormat("A-**#####,#00.00b¤"); |
| 4971 |
| 4972 // Write to byte stream |
| 4973 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 4974 ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 4975 oos.writeObject(df0); |
| 4976 oos.flush(); |
| 4977 baos.close(); |
| 4978 byte[] bytes = baos.toByteArray(); |
| 4979 |
| 4980 // Read from byte stream |
| 4981 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b
ytes)); |
| 4982 Object obj = ois.readObject(); |
| 4983 ois.close(); |
| 4984 DecimalFormat df1 = (DecimalFormat) obj; |
| 4985 |
| 4986 // Test equality |
| 4987 assertEquals("Did not round-trip through serialization", df0, df1); |
| 4988 |
| 4989 // Test basic functionality |
| 4990 String str0 = df0.format(12345.67); |
| 4991 String str1 = df1.format(12345.67); |
| 4992 assertEquals("Serialized formatter does not produce same output", str0,
str1); |
| 4993 } |
| 4994 |
| 4995 @Test |
| 4996 public void testGetSetCurrency() { |
| 4997 DecimalFormat df = new DecimalFormat("¤#"); |
| 4998 assertEquals("Currency should start out null", null, df.getCurrency()); |
| 4999 Currency curr = Currency.getInstance("EUR"); |
| 5000 df.setCurrency(curr); |
| 5001 assertEquals("Currency should equal EUR after set", curr, df.getCurrency
()); |
| 5002 String result = df.format(123); |
| 5003 assertEquals("Currency should format as expected in EUR", "€123.00", res
ult); |
| 5004 } |
| 5005 |
| 5006 @Test |
| 5007 public void testRoundingModeSetters() { |
| 5008 DecimalFormat df1 = new DecimalFormat(); |
| 5009 DecimalFormat df2 = new DecimalFormat(); |
| 5010 |
| 5011 df1.setRoundingMode(java.math.BigDecimal.ROUND_CEILING); |
| 5012 assertNotEquals("Rounding mode was set to a non-default", df1, df2); |
| 5013 df2.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_CEILING); |
| 5014 assertEquals("Rounding mode from icu.math and java.math should be the sa
me", df1, df2); |
| 5015 df2.setRoundingMode(java.math.RoundingMode.CEILING.ordinal()); |
| 5016 assertEquals("Rounding mode ordinal from java.math.RoundingMode should b
e the same", df1, df2); |
| 5017 } |
| 5018 |
| 5019 @Test |
| 5020 public void testSignificantDigitsMode() { |
| 5021 String[][] allExpected = { |
| 5022 {"12340.0", "12340.0", "12340.0"}, |
| 5023 {"1234.0", "1234.0", "1234.0"}, |
| 5024 {"123.4", "123.4", "123.4"}, |
| 5025 {"12.34", "12.34", "12.34"}, |
| 5026 {"1.234", "1.23", "1.23"}, |
| 5027 {"0.1234", "0.12", "0.123"}, |
| 5028 {"0.01234", "0.01", "0.0123"}, |
| 5029 {"0.001234", "0.00", "0.00123"} |
| 5030 }; |
| 5031 |
| 5032 DecimalFormat df = new DecimalFormat(); |
| 5033 df.setMinimumFractionDigits(1); |
| 5034 df.setMaximumFractionDigits(2); |
| 5035 df.setMinimumSignificantDigits(3); |
| 5036 df.setMaximumSignificantDigits(4); |
| 5037 df.setGroupingUsed(false); |
| 5038 |
| 5039 SignificantDigitsMode[] modes = new SignificantDigitsMode[] { |
| 5040 SignificantDigitsMode.OVERRIDE_MAXIMUM_FRACTION, |
| 5041 SignificantDigitsMode.RESPECT_MAXIMUM_FRACTION, |
| 5042 SignificantDigitsMode.ENSURE_MINIMUM_SIGNIFICANT |
| 5043 }; |
| 5044 |
| 5045 for (double d = 12340.0, i=0; d > 0.001; d /= 10, i++) { |
| 5046 for (int j=0; j<modes.length; j++) { |
| 5047 SignificantDigitsMode mode = modes[j]; |
| 5048 df.setSignificantDigitsMode(mode); |
| 5049 String expected = allExpected[(int)i][j]; |
| 5050 String actual = df.format(d); |
| 5051 assertEquals("Significant digits mode getter is broken", |
| 5052 mode, df.getSignificantDigitsMode()); |
| 5053 assertEquals("Significant digits output differs for "+i+", "+j, |
| 5054 expected, actual); |
| 5055 } |
| 5056 } |
| 5057 } |
| 5058 |
| 5059 @Test |
| 5060 public void testParseNoExponent() throws ParseException { |
| 5061 DecimalFormat df = new DecimalFormat(); |
| 5062 assertEquals("Parse no exponent has wrong default", false, df.getParseNo
Exponent()); |
| 5063 Number result1 = df.parse("123E4"); |
| 5064 df.setParseNoExponent(true); |
| 5065 assertEquals("Parse no exponent getter is broken", true, df.getParseNoEx
ponent()); |
| 5066 Number result2 = df.parse("123E4"); |
| 5067 assertEquals("Exponent did not parse before setParseNoExponent", result1
, new Long(1230000)); |
| 5068 assertEquals("Exponent parsed after setParseNoExponent", result2, new Lo
ng(123)); |
| 5069 } |
| 5070 |
| 5071 @Test |
| 5072 public void testMinimumGroupingDigits() { |
| 5073 String[][] allExpected = { |
| 5074 {"123", "123"}, |
| 5075 {"1,230", "1230"}, |
| 5076 {"12,300", "12,300"}, |
| 5077 {"1,23,000", "1,23,000"} |
| 5078 }; |
| 5079 |
| 5080 DecimalFormat df = new DecimalFormat("#,##,##0"); |
| 5081 assertEquals("Minimum grouping digits has wrong default", 1, df.getMinim
umGroupingDigits()); |
| 5082 |
| 5083 for (int l = 123, i=0; l <= 123000; l *= 10, i++) { |
| 5084 df.setMinimumGroupingDigits(1); |
| 5085 assertEquals("Minimum grouping digits getter is broken", 1, df.getMi
nimumGroupingDigits()); |
| 5086 String actual = df.format(l); |
| 5087 assertEquals("Output is wrong for 1, "+i, allExpected[i][0], actual)
; |
| 5088 df.setMinimumGroupingDigits(2); |
| 5089 assertEquals("Minimum grouping digits getter is broken", 2, df.getMi
nimumGroupingDigits()); |
| 5090 actual = df.format(l); |
| 5091 assertEquals("Output is wrong for 2, "+i, allExpected[i][1], actual)
; |
| 5092 } |
| 5093 } |
| 5094 |
| 5095 @Test |
| 5096 public void testParseCaseSensitive() { |
| 5097 String[] patterns = {"a#b", "A#B"}; |
| 5098 String[] inputs = {"a500b", "A500b", "a500B", "a500e10b", "a500E10b"}; |
| 5099 int[][] expectedParsePositions = { |
| 5100 {5, 5, 5, 8, 8}, // case insensitive, pattern 0 |
| 5101 {5, 0, 4, 4, 8}, // case sensitive, pattern 0 |
| 5102 {5, 5, 5, 8, 8}, // case insensitive, pattern 1 |
| 5103 {0, 4, 0, 0, 0}, // case sensitive, pattern 1 |
| 5104 }; |
| 5105 |
| 5106 for (int p = 0; p < patterns.length; p++) { |
| 5107 String pat = patterns[p]; |
| 5108 DecimalFormat df = new DecimalFormat(pat); |
| 5109 assertEquals("parseCaseSensitive default is wrong", false, df.getPar
seCaseSensitive()); |
| 5110 for (int i = 0; i < inputs.length; i++) { |
| 5111 String inp = inputs[i]; |
| 5112 df.setParseCaseSensitive(false); |
| 5113 assertEquals("parseCaseSensitive getter is broken", false, df.ge
tParseCaseSensitive()); |
| 5114 ParsePosition actualInsensitive = new ParsePosition(0); |
| 5115 df.parse(inp, actualInsensitive); |
| 5116 assertEquals("Insensitive, pattern "+p+", input "+i, |
| 5117 expectedParsePositions[p*2][i], actualInsensitive.getInd
ex()); |
| 5118 df.setParseCaseSensitive(true); |
| 5119 assertEquals("parseCaseSensitive getter is broken", true, df.get
ParseCaseSensitive()); |
| 5120 ParsePosition actualSensitive = new ParsePosition(0); |
| 5121 df.parse(inp, actualSensitive); |
| 5122 assertEquals("Sensitive, pattern "+p+", input "+i, |
| 5123 expectedParsePositions[p*2+1][i], actualSensitive.getInd
ex()); |
| 5124 } |
| 5125 } |
| 5126 } |
4594 } | 5127 } |
LEFT | RIGHT |