: NumberFormatterApiTest(globalNumberFormatterApiTestStatus) {
}
-NumberFormatterApiTest::NumberFormatterApiTest(UErrorCode &status)
- : USD(u"USD", status), GBP(u"GBP", status),
- CZK(u"CZK", status), CAD(u"CAD", status),
- ESP(u"ESP", status), PTE(u"PTE", status),
- FRENCH_SYMBOLS(Locale::getFrench(), status),
- SWISS_SYMBOLS(Locale("de-CH"), status),
- MYANMAR_SYMBOLS(Locale("my"), status) {
+NumberFormatterApiTest::NumberFormatterApiTest(UErrorCode& status)
+ : USD(u"USD", status),
+ GBP(u"GBP", status),
+ CZK(u"CZK", status),
+ CAD(u"CAD", status),
+ ESP(u"ESP", status),
+ PTE(u"PTE", status),
+ FRENCH_SYMBOLS(Locale::getFrench(), status),
+ SWISS_SYMBOLS(Locale("de-CH"), status),
+ MYANMAR_SYMBOLS(Locale("my"), status) {
// Check for error on the first MeasureUnit in case there is no data
LocalPointer<MeasureUnit> unit(MeasureUnit::createMeter(status));
LATN = *LocalPointer<NumberingSystem>(NumberingSystem::createInstanceByName("latn", status));
}
-void NumberFormatterApiTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *) {
+void NumberFormatterApiTest::runIndexedTest(int32_t index, UBool exec, const char*& name, char*) {
if (exec) {
logln("TestSuite NumberFormatterApiTest: ");
}
void NumberFormatterApiTest::notationSimple() {
assertFormatDescending(
u"Basic",
+ u"",
NumberFormatter::with(),
Locale::getEnglish(),
u"87,650",
u"0.008765",
u"0");
+ assertFormatDescendingBig(
+ u"Big Simple",
+ u"notation-simple",
+ NumberFormatter::with().notation(Notation::simple()),
+ Locale::getEnglish(),
+ u"87,650,000",
+ u"8,765,000",
+ u"876,500",
+ u"87,650",
+ u"8,765",
+ u"876.5",
+ u"87.65",
+ u"8.765",
+ u"0");
+
assertFormatSingle(
u"Basic with Negative Sign",
+ u"",
NumberFormatter::with(),
Locale::getEnglish(),
-9876543.21,
void NumberFormatterApiTest::notationScientific() {
assertFormatDescending(
u"Scientific",
+ u"scientific",
NumberFormatter::with().notation(Notation::scientific()),
Locale::getEnglish(),
u"8.765E4",
assertFormatDescending(
u"Engineering",
+ u"engineering",
NumberFormatter::with().notation(Notation::engineering()),
Locale::getEnglish(),
u"87.65E3",
assertFormatDescending(
u"Scientific sign always shown",
+ u"scientific/sign-always",
NumberFormatter::with().notation(
Notation::scientific().withExponentSignDisplay(UNumberSignDisplay::UNUM_SIGN_ALWAYS)),
Locale::getEnglish(),
assertFormatDescending(
u"Scientific min exponent digits",
+ u"scientific/+ee",
NumberFormatter::with().notation(Notation::scientific().withMinExponentDigits(2)),
Locale::getEnglish(),
u"8.765E04",
assertFormatSingle(
u"Scientific Negative",
+ u"scientific",
NumberFormatter::with().notation(Notation::scientific()),
Locale::getEnglish(),
-1000000,
void NumberFormatterApiTest::notationCompact() {
assertFormatDescending(
u"Compact Short",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
u"88K",
assertFormatDescending(
u"Compact Long",
+ u"compact-long",
NumberFormatter::with().notation(Notation::compactLong()),
Locale::getEnglish(),
u"88 thousand",
assertFormatDescending(
u"Compact Short Currency",
+ u"compact-short currency/USD",
NumberFormatter::with().notation(Notation::compactShort()).unit(USD),
Locale::getEnglish(),
u"$88K",
assertFormatDescending(
u"Compact Short with ISO Currency",
+ u"compact-short currency/USD unit-width-iso-code",
NumberFormatter::with().notation(Notation::compactShort())
.unit(USD)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_ISO_CODE),
assertFormatDescending(
u"Compact Short with Long Name Currency",
+ u"compact-short currency/USD unit-width-full-name",
NumberFormatter::with().notation(Notation::compactShort())
.unit(USD)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
// This test case should be fixed when proper compact long currency patterns are added.
assertFormatDescending(
u"Compact Long Currency",
+ u"compact-long currency/USD",
NumberFormatter::with().notation(Notation::compactLong()).unit(USD),
Locale::getEnglish(),
u"$88K", // should be something like "$88 thousand"
// This test case should be fixed when proper compact long currency patterns are added.
assertFormatDescending(
u"Compact Long with ISO Currency",
+ u"compact-long currency/USD unit-width-iso-code",
NumberFormatter::with().notation(Notation::compactLong())
.unit(USD)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_ISO_CODE),
// TODO: This behavior could be improved and should be revisited.
assertFormatDescending(
u"Compact Long with Long Name Currency",
+ u"compact-long currency/USD unit-width-full-name",
NumberFormatter::with().notation(Notation::compactLong())
.unit(USD)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
assertFormatSingle(
u"Compact Plural One",
+ u"compact-long",
NumberFormatter::with().notation(Notation::compactLong()),
Locale::createFromName("es"),
1000000,
assertFormatSingle(
u"Compact Plural Other",
+ u"compact-long",
NumberFormatter::with().notation(Notation::compactLong()),
Locale::createFromName("es"),
2000000,
assertFormatSingle(
u"Compact with Negative Sign",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
-9876543.21,
assertFormatSingle(
u"Compact Rounding",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
990000,
assertFormatSingle(
u"Compact Rounding",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
999000,
assertFormatSingle(
u"Compact Rounding",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
999900,
assertFormatSingle(
u"Compact Rounding",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
9900000,
assertFormatSingle(
u"Compact Rounding",
+ u"compact-short",
NumberFormatter::with().notation(Notation::compactShort()),
Locale::getEnglish(),
9990000,
void NumberFormatterApiTest::unitMeasure() {
assertFormatDescending(
u"Meters Short and unit() method",
+ u"measure-unit/length-meter",
NumberFormatter::with().unit(METER),
Locale::getEnglish(),
u"87,650 m",
assertFormatDescending(
u"Meters Long and adoptUnit() method",
+ u"measure-unit/length-meter unit-width-full-name",
NumberFormatter::with().adoptUnit(new MeasureUnit(METER))
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
Locale::getEnglish(),
assertFormatDescending(
u"Compact Meters Long",
+ u"compact-long measure-unit/length-meter unit-width-full-name",
NumberFormatter::with().notation(Notation::compactLong())
.unit(METER)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
assertFormatSingle(
u"Meters with Negative Sign",
+ u"measure-unit/length-meter",
NumberFormatter::with().unit(METER),
Locale::getEnglish(),
-9876543.21,
// The locale string "सान" appears only in brx.txt:
assertFormatSingle(
u"Interesting Data Fallback 1",
- NumberFormatter::with().unit(DAY)
- .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
+ u"measure-unit/duration-day unit-width-full-name",
+ NumberFormatter::with().unit(DAY).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
Locale::createFromName("brx"),
5.43,
u"5.43 सान");
// Requires following the alias from unitsNarrow to unitsShort:
assertFormatSingle(
u"Interesting Data Fallback 2",
- NumberFormatter::with().unit(DAY)
- .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW),
+ u"measure-unit/duration-day unit-width-narrow",
+ NumberFormatter::with().unit(DAY).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW),
Locale::createFromName("brx"),
5.43,
u"5.43 d");
// requiring fallback to the root.
assertFormatSingle(
u"Interesting Data Fallback 3",
- NumberFormatter::with().unit(SQUARE_METER)
- .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW),
+ u"measure-unit/area-square-meter unit-width-narrow",
+ NumberFormatter::with().unit(SQUARE_METER).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW),
Locale::createFromName("en-GB"),
5.43,
u"5.43 m²");
// NOTE: This example is in the documentation.
assertFormatSingle(
u"Difference between Narrow and Short (Narrow Version)",
- NumberFormatter::with().unit(FAHRENHEIT)
- .unitWidth(UNUM_UNIT_WIDTH_NARROW),
+ u"measure-unit/temperature-fahrenheit unit-width-narrow",
+ NumberFormatter::with().unit(FAHRENHEIT).unitWidth(UNUM_UNIT_WIDTH_NARROW),
Locale("es-US"),
5.43,
u"5.43°");
assertFormatSingle(
u"Difference between Narrow and Short (Short Version)",
- NumberFormatter::with().unit(FAHRENHEIT)
- .unitWidth(UNUM_UNIT_WIDTH_SHORT),
+ u"measure-unit/temperature-fahrenheit unit-width-short",
+ NumberFormatter::with().unit(FAHRENHEIT).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("es-US"),
5.43,
u"5.43 °F");
assertFormatSingle(
u"MeasureUnit form without {0} in CLDR pattern",
- NumberFormatter::with()
- .unit(KELVIN)
- .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
+ u"measure-unit/temperature-kelvin unit-width-full-name",
+ NumberFormatter::with().unit(KELVIN).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
Locale("es-MX"),
1,
u"kelvin");
assertFormatSingle(
u"MeasureUnit form without {0} in CLDR pattern and wide base form",
- NumberFormatter::with()
- .rounding(Rounder::fixedFraction(20))
+ u"measure-unit/temperature-kelvin .0000000000 unit-width-full-name",
+ NumberFormatter::with().rounding(Rounder::fixedFraction(20))
.unit(KELVIN)
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
Locale("es-MX"),
void NumberFormatterApiTest::unitCompoundMeasure() {
assertFormatDescending(
u"Meters Per Second Short (unit that simplifies) and perUnit method",
+ u"measure-unit/length-meter per-measure-unit/duration-second",
NumberFormatter::with().unit(METER).perUnit(SECOND),
Locale::getEnglish(),
u"87,650 m/s",
assertFormatDescending(
u"Pounds Per Square Mile Short (secondary unit has per-format) and adoptPerUnit method",
+ u"measure-unit/mass-pound per-measure-unit/area-square-mile",
NumberFormatter::with().unit(POUND).adoptPerUnit(new MeasureUnit(SQUARE_MILE)),
Locale::getEnglish(),
u"87,650 lb/mi²",
assertFormatDescending(
u"Joules Per Furlong Short (unit with no simplifications or special patterns)",
+ u"measure-unit/energy-joule per-measure-unit/length-furlong",
NumberFormatter::with().unit(JOULE).perUnit(FURLONG),
Locale::getEnglish(),
u"87,650 J/fur",
void NumberFormatterApiTest::unitCurrency() {
assertFormatDescending(
u"Currency",
+ u"currency/GBP",
NumberFormatter::with().unit(GBP),
Locale::getEnglish(),
u"£87,650.00",
assertFormatDescending(
u"Currency ISO",
+ u"currency/GBP unit-width-iso-code",
NumberFormatter::with().unit(GBP).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_ISO_CODE),
Locale::getEnglish(),
u"GBP 87,650.00",
assertFormatDescending(
u"Currency Long Name",
+ u"currency/GBP unit-width-full-name",
NumberFormatter::with().unit(GBP).unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME),
Locale::getEnglish(),
u"87,650.00 British pounds",
assertFormatDescending(
u"Currency Hidden",
+ u"currency/GBP unit-width-hidden",
NumberFormatter::with().unit(GBP).unitWidth(UNUM_UNIT_WIDTH_HIDDEN),
Locale::getEnglish(),
u"87,650.00",
assertFormatSingle(
u"Currency with Negative Sign",
+ u"currency/GBP",
NumberFormatter::with().unit(GBP),
Locale::getEnglish(),
-9876543.21,
// NOTE: This example is in the documentation.
assertFormatSingle(
u"Currency Difference between Narrow and Short (Narrow Version)",
+ u"currency/USD unit-width-narrow",
NumberFormatter::with().unit(USD).unitWidth(UNUM_UNIT_WIDTH_NARROW),
Locale("en-CA"),
5.43,
assertFormatSingle(
u"Currency Difference between Narrow and Short (Short Version)",
+ u"currency/USD unit-width-short",
NumberFormatter::with().unit(USD).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("en-CA"),
5.43,
assertFormatSingle(
u"Currency-dependent format (Control)",
+ u"currency/USD unit-width-short",
NumberFormatter::with().unit(USD).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("ca"),
444444.55,
assertFormatSingle(
u"Currency-dependent format (Test)",
+ u"currency/ESP unit-width-short",
NumberFormatter::with().unit(ESP).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("ca"),
444444.55,
assertFormatSingle(
u"Currency-dependent symbols (Control)",
+ u"currency/USD unit-width-short",
NumberFormatter::with().unit(USD).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("pt-PT"),
444444.55,
// width space), and they set the decimal separator to the $ symbol.
assertFormatSingle(
u"Currency-dependent symbols (Test Short)",
+ u"currency/PTE unit-width-short",
NumberFormatter::with().unit(PTE).unitWidth(UNUM_UNIT_WIDTH_SHORT),
Locale("pt-PT"),
444444.55,
assertFormatSingle(
u"Currency-dependent symbols (Test Narrow)",
+ u"currency/PTE unit-width-narrow",
NumberFormatter::with().unit(PTE).unitWidth(UNUM_UNIT_WIDTH_NARROW),
Locale("pt-PT"),
444444.55,
assertFormatSingle(
u"Currency-dependent symbols (Test ISO Code)",
+ u"currency/PTE unit-width-iso-code",
NumberFormatter::with().unit(PTE).unitWidth(UNUM_UNIT_WIDTH_ISO_CODE),
Locale("pt-PT"),
444444.55,
void NumberFormatterApiTest::unitPercent() {
assertFormatDescending(
u"Percent",
+ u"percent",
NumberFormatter::with().unit(NoUnit::percent()),
Locale::getEnglish(),
u"87,650%",
assertFormatDescending(
u"Permille",
+ u"permille",
NumberFormatter::with().unit(NoUnit::permille()),
Locale::getEnglish(),
u"87,650‰",
assertFormatSingle(
u"NoUnit Base",
+ u"base-unit",
NumberFormatter::with().unit(NoUnit::base()),
Locale::getEnglish(),
51423,
assertFormatSingle(
u"Percent with Negative Sign",
+ u"percent",
NumberFormatter::with().unit(NoUnit::percent()),
Locale::getEnglish(),
-98.7654321,
void NumberFormatterApiTest::roundingFraction() {
assertFormatDescending(
u"Integer",
+ u"round-integer",
NumberFormatter::with().rounding(Rounder::integer()),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Fixed Fraction",
+ u".000",
NumberFormatter::with().rounding(Rounder::fixedFraction(3)),
Locale::getEnglish(),
u"87,650.000",
assertFormatDescending(
u"Min Fraction",
+ u".0+",
NumberFormatter::with().rounding(Rounder::minFraction(1)),
Locale::getEnglish(),
u"87,650.0",
assertFormatDescending(
u"Max Fraction",
+ u".#",
NumberFormatter::with().rounding(Rounder::maxFraction(1)),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Min/Max Fraction",
+ u".0##",
NumberFormatter::with().rounding(Rounder::minMaxFraction(1, 3)),
Locale::getEnglish(),
u"87,650.0",
void NumberFormatterApiTest::roundingFigures() {
assertFormatSingle(
u"Fixed Significant",
+ u"@@@",
NumberFormatter::with().rounding(Rounder::fixedDigits(3)),
Locale::getEnglish(),
-98,
assertFormatSingle(
u"Fixed Significant Rounding",
+ u"@@@",
NumberFormatter::with().rounding(Rounder::fixedDigits(3)),
Locale::getEnglish(),
-98.7654321,
assertFormatSingle(
u"Fixed Significant Zero",
+ u"@@@",
NumberFormatter::with().rounding(Rounder::fixedDigits(3)),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Min Significant",
+ u"@@+",
NumberFormatter::with().rounding(Rounder::minDigits(2)),
Locale::getEnglish(),
-9,
assertFormatSingle(
u"Max Significant",
+ u"@###",
NumberFormatter::with().rounding(Rounder::maxDigits(4)),
Locale::getEnglish(),
98.7654321,
assertFormatSingle(
u"Min/Max Significant",
+ u"@@@#",
NumberFormatter::with().rounding(Rounder::minMaxDigits(3, 4)),
Locale::getEnglish(),
9.99999,
void NumberFormatterApiTest::roundingFractionFigures() {
assertFormatDescending(
u"Basic Significant", // for comparison
+ u"@#",
NumberFormatter::with().rounding(Rounder::maxDigits(2)),
Locale::getEnglish(),
u"88,000",
assertFormatDescending(
u"FracSig minMaxFrac minSig",
+ u".0#/@@@+",
NumberFormatter::with().rounding(Rounder::minMaxFraction(1, 2).withMinDigits(3)),
Locale::getEnglish(),
u"87,650.0",
assertFormatDescending(
u"FracSig minMaxFrac maxSig A",
+ u".0##/@#",
NumberFormatter::with().rounding(Rounder::minMaxFraction(1, 3).withMaxDigits(2)),
Locale::getEnglish(),
u"88,000.0", // maxSig beats maxFrac
assertFormatDescending(
u"FracSig minMaxFrac maxSig B",
+ u".00/@#",
NumberFormatter::with().rounding(Rounder::fixedFraction(2).withMaxDigits(2)),
Locale::getEnglish(),
u"88,000.00", // maxSig beats maxFrac
u"0.00");
assertFormatSingle(
- "FracSig with trailing zeros A",
+ u"FracSig with trailing zeros A",
+ u".00/@@@+",
NumberFormatter::with().rounding(Rounder::fixedFraction(2).withMinDigits(3)),
Locale::getEnglish(),
0.1,
u"0.10");
assertFormatSingle(
- "FracSig with trailing zeros B",
+ u"FracSig with trailing zeros B",
+ u".00/@@@+",
NumberFormatter::with().rounding(Rounder::fixedFraction(2).withMinDigits(3)),
Locale::getEnglish(),
0.0999999,
void NumberFormatterApiTest::roundingOther() {
assertFormatDescending(
u"Rounding None",
+ u"round-unlimited",
NumberFormatter::with().rounding(Rounder::unlimited()),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Increment",
+ u"round-increment/0.5",
NumberFormatter::with().rounding(Rounder::increment(0.5).withMinFraction(1)),
Locale::getEnglish(),
u"87,650.0",
assertFormatDescending(
u"Increment with Min Fraction",
+ u"round-increment/0.50",
NumberFormatter::with().rounding(Rounder::increment(0.5).withMinFraction(2)),
Locale::getEnglish(),
u"87,650.00",
assertFormatDescending(
u"Currency Standard",
+ u"currency/CZK round-currency-standard",
NumberFormatter::with().rounding(Rounder::currency(UCurrencyUsage::UCURR_USAGE_STANDARD))
.unit(CZK),
Locale::getEnglish(),
assertFormatDescending(
u"Currency Cash",
+ u"currency/CZK round-currency-cash",
NumberFormatter::with().rounding(Rounder::currency(UCurrencyUsage::UCURR_USAGE_CASH))
.unit(CZK),
Locale::getEnglish(),
assertFormatDescending(
u"Currency Cash with Nickel Rounding",
+ u"currency/CAD round-currency-cash",
NumberFormatter::with().rounding(Rounder::currency(UCurrencyUsage::UCURR_USAGE_CASH))
.unit(CAD),
Locale::getEnglish(),
assertFormatDescending(
u"Currency not in top-level fluent chain",
+ u"round-integer", // calling .withCurrency() applies currency rounding rules immediately
NumberFormatter::with().rounding(
Rounder::currency(UCurrencyUsage::UCURR_USAGE_CASH).withCurrency(CZK)),
Locale::getEnglish(),
// NOTE: Other tests cover the behavior of the other rounding modes.
assertFormatDescending(
u"Rounding Mode CEILING",
+ u"round-integer/ceiling",
NumberFormatter::with().rounding(Rounder::integer().withMode(UNumberFormatRoundingMode::UNUM_ROUND_CEILING)),
Locale::getEnglish(),
u"87,650",
void NumberFormatterApiTest::grouping() {
assertFormatDescendingBig(
u"Western Grouping",
+ u"group-auto",
NumberFormatter::with().grouping(UNUM_GROUPING_AUTO),
Locale::getEnglish(),
u"87,650,000",
assertFormatDescendingBig(
u"Indic Grouping",
+ u"group-auto",
NumberFormatter::with().grouping(UNUM_GROUPING_AUTO),
Locale("en-IN"),
u"8,76,50,000",
u"0");
assertFormatDescendingBig(
- u"Western Grouping, Wide",
+ u"Western Grouping, Min 2",
+ u"group-min2",
NumberFormatter::with().grouping(UNUM_GROUPING_MIN2),
Locale::getEnglish(),
u"87,650,000",
u"0");
assertFormatDescendingBig(
- u"Indic Grouping, Wide",
+ u"Indic Grouping, Min 2",
+ u"group-min2",
NumberFormatter::with().grouping(UNUM_GROUPING_MIN2),
Locale("en-IN"),
u"8,76,50,000",
assertFormatDescendingBig(
u"No Grouping",
+ u"group-off",
NumberFormatter::with().grouping(UNUM_GROUPING_OFF),
Locale("en-IN"),
u"87650000",
assertFormatDescendingBig(
u"Indic locale with THOUSANDS grouping",
+ u"group-thousands",
NumberFormatter::with().grouping(UNUM_GROUPING_THOUSANDS),
Locale("en-IN"),
u"87,650,000",
// If this test breaks due to data changes, find another locale that has minimumGroupingDigits.
assertFormatDescendingBig(
u"Hungarian Grouping",
+ u"group-auto",
NumberFormatter::with().grouping(UNUM_GROUPING_AUTO),
Locale("hu"),
u"87 650 000",
assertFormatDescendingBig(
u"Hungarian Grouping, Min 2",
+ u"group-min2",
NumberFormatter::with().grouping(UNUM_GROUPING_MIN2),
Locale("hu"),
u"87 650 000",
assertFormatDescendingBig(
u"Hungarian Grouping, Always",
+ u"group-on-aligned",
NumberFormatter::with().grouping(UNUM_GROUPING_ON_ALIGNED),
Locale("hu"),
u"87 650 000",
// If this test breaks due to data changes, find another locale that has no default grouping.
assertFormatDescendingBig(
u"Bulgarian Currency Grouping",
+ u"currency/USD group-auto",
NumberFormatter::with().grouping(UNUM_GROUPING_AUTO).unit(USD),
Locale("bg"),
u"87650000,00 щ.д.",
assertFormatDescendingBig(
u"Bulgarian Currency Grouping, Always",
+ u"currency/USD group-on-aligned",
NumberFormatter::with().grouping(UNUM_GROUPING_ON_ALIGNED).unit(USD),
Locale("bg"),
u"87 650 000,00 щ.д.",
u"8,76 щ.д.",
u"0,00 щ.д.");
- // TODO: Enable this test when macro-setter is available in C++
- // MacroProps macros;
- // macros.grouping = Grouper(4, 1, 3);
- // assertFormatDescendingBig(
- // u"Custom Grouping via Internal API",
- // NumberFormatter::with().macros(macros),
- // Locale::getEnglish(),
- // u"8,7,6,5,0000",
- // u"8,7,6,5000",
- // u"876500",
- // u"87650",
- // u"8765",
- // u"876.5",
- // u"87.65",
- // u"8.765",
- // u"0");
+ MacroProps macros;
+ macros.grouper = Grouper(4, 1, 3, UNUM_GROUPING_COUNT);
+ assertFormatDescendingBig(
+ u"Custom Grouping via Internal API",
+ nullptr,
+ NumberFormatter::with().macros(macros),
+ Locale::getEnglish(),
+ u"8,7,6,5,0000",
+ u"8,7,6,5000",
+ u"876500",
+ u"87650",
+ u"8765",
+ u"876.5",
+ u"87.65",
+ u"8.765",
+ u"0");
}
void NumberFormatterApiTest::padding() {
assertFormatDescending(
u"Padding",
+ nullptr,
NumberFormatter::with().padding(Padder::none()),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Padding",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_AFTER_PREFIX)),
assertFormatDescending(
u"Padding with code points",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
0x101E4, 8, PadPosition::UNUM_PAD_AFTER_PREFIX)),
assertFormatDescending(
u"Padding with wide digits",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_AFTER_PREFIX))
assertFormatDescending(
u"Padding with currency spacing",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 10, PadPosition::UNUM_PAD_AFTER_PREFIX))
assertFormatSingle(
u"Pad Before Prefix",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_BEFORE_PREFIX)),
assertFormatSingle(
u"Pad After Prefix",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_AFTER_PREFIX)),
assertFormatSingle(
u"Pad Before Suffix",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_BEFORE_SUFFIX)).unit(NoUnit::percent()),
assertFormatSingle(
u"Pad After Suffix",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'*', 8, PadPosition::UNUM_PAD_AFTER_SUFFIX)).unit(NoUnit::percent()),
assertFormatSingle(
u"Currency Spacing with Zero Digit Padding Broken",
+ nullptr,
NumberFormatter::with().padding(
Padder::codePoints(
'0', 12, PadPosition::UNUM_PAD_AFTER_PREFIX))
void NumberFormatterApiTest::integerWidth() {
assertFormatDescending(
u"Integer Width Default",
+ u"integer-width/+0",
NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(1)),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Integer Width Zero Fill 0",
+ u"integer-width/+",
NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(0)),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Integer Width Zero Fill 3",
+ u"integer-width/+000",
NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(3)),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Integer Width Max 3",
+ u"integer-width/##0",
NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(1).truncateAt(3)),
Locale::getEnglish(),
u"650",
assertFormatDescending(
u"Integer Width Fixed 2",
+ u"integer-width/00",
NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(2).truncateAt(2)),
Locale::getEnglish(),
u"50",
void NumberFormatterApiTest::symbols() {
assertFormatDescending(
u"French Symbols with Japanese Data 1",
+ nullptr,
NumberFormatter::with().symbols(FRENCH_SYMBOLS),
Locale::getJapan(),
u"87 650",
assertFormatSingle(
u"French Symbols with Japanese Data 2",
+ nullptr,
NumberFormatter::with().notation(Notation::compactShort()).symbols(FRENCH_SYMBOLS),
Locale::getJapan(),
12345,
assertFormatDescending(
u"Latin Numbering System with Arabic Data",
+ u"currency/USD latin",
NumberFormatter::with().adoptSymbols(new NumberingSystem(LATN)).unit(USD),
Locale("ar"),
u"US$ 87,650.00",
assertFormatDescending(
u"Math Numbering System with French Data",
+ u"numbering-system/mathsanb",
NumberFormatter::with().adoptSymbols(new NumberingSystem(MATHSANB)),
Locale::getFrench(),
u"𝟴𝟳 𝟲𝟱𝟬",
assertFormatSingle(
u"Swiss Symbols (used in documentation)",
+ nullptr,
NumberFormatter::with().symbols(SWISS_SYMBOLS),
Locale::getEnglish(),
12345.67,
assertFormatSingle(
u"Myanmar Symbols (used in documentation)",
+ nullptr,
NumberFormatter::with().symbols(MYANMAR_SYMBOLS),
Locale::getEnglish(),
12345.67,
assertFormatSingle(
u"Currency symbol should precede number in ar with NS latn",
+ u"currency/USD latin",
NumberFormatter::with().adoptSymbols(new NumberingSystem(LATN)).unit(USD),
Locale("ar"),
12345.67,
assertFormatSingle(
u"Currency symbol should precede number in ar@numbers=latn",
+ u"currency/USD",
NumberFormatter::with().unit(USD),
Locale("ar@numbers=latn"),
12345.67,
assertFormatSingle(
u"Currency symbol should follow number in ar-EG with NS arab",
+ u"currency/USD",
NumberFormatter::with().unit(USD),
Locale("ar-EG"),
12345.67,
assertFormatSingle(
u"Currency symbol should follow number in ar@numbers=arab",
+ u"currency/USD",
NumberFormatter::with().unit(USD),
Locale("ar@numbers=arab"),
12345.67,
assertFormatSingle(
u"NumberingSystem in API should win over @numbers keyword",
+ u"currency/USD latin",
NumberFormatter::with().adoptSymbols(new NumberingSystem(LATN)).unit(USD),
Locale("ar@numbers=arab"),
12345.67,
u"US$ 12,345.67");
UErrorCode status = U_ZERO_ERROR;
- assertEquals("NumberingSystem in API should win over @numbers keyword in reverse order",
+ assertEquals(
+ "NumberingSystem in API should win over @numbers keyword in reverse order",
u"US$ 12,345.67",
- NumberFormatter::withLocale(Locale("ar@numbers=arab"))
- .adoptSymbols(new NumberingSystem(LATN))
- .unit(USD)
- .formatDouble(12345.67, status)
- .toString());
+ NumberFormatter::withLocale(Locale("ar@numbers=arab")).adoptSymbols(new NumberingSystem(LATN))
+ .unit(USD)
+ .formatDouble(12345.67, status)
+ .toString());
DecimalFormatSymbols symbols = SWISS_SYMBOLS;
UnlocalizedNumberFormatter f = NumberFormatter::with().symbols(symbols);
symbols.setSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kGroupingSeparatorSymbol, u"!", status);
assertFormatSingle(
- u"Symbols object should be copied", f, Locale::getEnglish(), 12345.67, u"12’345.67");
+ u"Symbols object should be copied", nullptr, f, Locale::getEnglish(), 12345.67, u"12’345.67");
assertFormatSingle(
u"The last symbols setter wins",
+ u"latin",
NumberFormatter::with().symbols(symbols).adoptSymbols(new NumberingSystem(LATN)),
Locale::getEnglish(),
12345.67,
assertFormatSingle(
u"The last symbols setter wins",
+ nullptr,
NumberFormatter::with().adoptSymbols(new NumberingSystem(LATN)).symbols(symbols),
Locale::getEnglish(),
12345.67,
void NumberFormatterApiTest::sign() {
assertFormatSingle(
u"Sign Auto Positive",
+ u"sign-auto",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_AUTO),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Auto Negative",
+ u"sign-auto",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_AUTO),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Auto Zero",
+ u"sign-auto",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_AUTO),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Always Positive",
+ u"sign-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Always Negative",
+ u"sign-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Always Zero",
+ u"sign-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Never Positive",
+ u"sign-never",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_NEVER),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Never Negative",
+ u"sign-never",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_NEVER),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Never Zero",
+ u"sign-never",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_NEVER),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Accounting Positive",
+ u"currency/USD sign-accounting",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING).unit(USD),
Locale::getEnglish(),
444444,
u"$444,444.00");
assertFormatSingle(
+ u"currency/USD sign-accounting",
u"Sign Accounting Negative",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING).unit(USD),
Locale::getEnglish(),
assertFormatSingle(
u"Sign Accounting Zero",
+ u"currency/USD sign-accounting",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING).unit(USD),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Accounting-Always Positive",
+ u"currency/USD sign-accounting-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_ALWAYS).unit(USD),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Accounting-Always Negative",
+ u"currency/USD sign-accounting-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_ALWAYS).unit(USD),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Accounting-Always Zero",
+ u"currency/USD sign-accounting-always",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_ALWAYS).unit(USD),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Except-Zero Positive",
+ u"sign-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_EXCEPT_ZERO),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Except-Zero Negative",
+ u"sign-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_EXCEPT_ZERO),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Except-Zero Zero",
+ u"sign-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_EXCEPT_ZERO),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Accounting-Except-Zero Positive",
+ u"currency/USD sign-accounting-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO).unit(USD),
Locale::getEnglish(),
444444,
assertFormatSingle(
u"Sign Accounting-Except-Zero Negative",
+ u"currency/USD sign-accounting-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO).unit(USD),
Locale::getEnglish(),
-444444,
assertFormatSingle(
u"Sign Accounting-Except-Zero Zero",
+ u"currency/USD sign-accounting-except-zero",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO).unit(USD),
Locale::getEnglish(),
0,
assertFormatSingle(
u"Sign Accounting Negative Hidden",
+ u"currency/USD unit-width-hidden sign-accounting",
NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ACCOUNTING)
.unit(USD)
.unitWidth(UNUM_UNIT_WIDTH_HIDDEN),
void NumberFormatterApiTest::decimal() {
assertFormatDescending(
u"Decimal Default",
+ u"decimal-auto",
NumberFormatter::with().decimal(UNumberDecimalSeparatorDisplay::UNUM_DECIMAL_SEPARATOR_AUTO),
Locale::getEnglish(),
u"87,650",
assertFormatDescending(
u"Decimal Always Shown",
+ u"decimal-always",
NumberFormatter::with().decimal(UNumberDecimalSeparatorDisplay::UNUM_DECIMAL_SEPARATOR_ALWAYS),
Locale::getEnglish(),
u"87,650.",
UErrorCode status2 = U_ZERO_ERROR;
FormattedNumber fn = lnf.formatInt(1, status1);
assertEquals(
- "Should fail since rounder is not legal",
- U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
- status1);
+ "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status1);
FieldPosition fp;
fn.populateFieldPosition(fp, status2);
assertEquals(
- "Should fail on terminal method",
- U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
- status2);
+ "Should fail on terminal method", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status2);
}
{
UErrorCode status = U_ZERO_ERROR;
lnf.copyErrorTo(status);
assertEquals(
- "Should fail since rounder is not legal",
- U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
- status);
+ "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status);
}
}
}
-void NumberFormatterApiTest::assertFormatDescending(const UnicodeString &message,
- const UnlocalizedNumberFormatter &f,
- Locale locale, ...) {
+void NumberFormatterApiTest::assertFormatDescending(const char16_t* umessage, const char16_t* uskeleton,
+ const UnlocalizedNumberFormatter& f, Locale locale,
+ ...) {
va_list args;
va_start(args, locale);
+ UnicodeString message(TRUE, umessage, -1);
static double inputs[] = {87650, 8765, 876.5, 87.65, 8.765, 0.8765, 0.08765, 0.008765, 0};
const LocalizedNumberFormatter l1 = f.threshold(0).locale(locale); // no self-regulation
const LocalizedNumberFormatter l2 = f.threshold(1).locale(locale); // all self-regulation
- UErrorCode status = U_ZERO_ERROR;
+ IcuTestErrorCode status(*this, "assertFormatDescending");
+ status.setScope(message);
+ UnicodeString expecteds[10];
for (int16_t i = 0; i < 9; i++) {
char16_t caseNumber = u'0' + i;
double d = inputs[i];
UnicodeString expected = va_arg(args, const char16_t*);
+ expecteds[i] = expected;
UnicodeString actual1 = l1.formatDouble(d, status).toString();
assertSuccess(message + u": Unsafe Path: " + caseNumber, status);
assertEquals(message + u": Unsafe Path: " + caseNumber, expected, actual1);
assertSuccess(message + u": Safe Path: " + caseNumber, status);
assertEquals(message + u": Safe Path: " + caseNumber, expected, actual2);
}
+ if (uskeleton != nullptr) { // if null, skeleton is declared as undefined.
+ UnicodeString skeleton(TRUE, uskeleton, -1);
+ // Only compare normalized skeletons: the tests need not provide the normalized forms.
+ // Use the normalized form to construct the testing formatter to guarantee no loss of info.
+ UnicodeString normalized = NumberFormatter::fromSkeleton(skeleton, status).toSkeleton(status);
+ assertEquals(message + ": Skeleton:", normalized, f.toSkeleton(status));
+ LocalizedNumberFormatter l3 = NumberFormatter::fromSkeleton(normalized, status).locale(locale);
+ for (int32_t i = 0; i < 9; i++) {
+ double d = inputs[i];
+ UnicodeString actual3 = l3.formatDouble(d, status).toString();
+ assertEquals(message + ": Skeleton Path: " + d, expecteds[i], actual3);
+ }
+ } else {
+ assertUndefinedSkeleton(f);
+ }
}
-void NumberFormatterApiTest::assertFormatDescendingBig(const UnicodeString &message,
- const UnlocalizedNumberFormatter &f,
- Locale locale, ...) {
+void NumberFormatterApiTest::assertFormatDescendingBig(const char16_t* umessage, const char16_t* uskeleton,
+ const UnlocalizedNumberFormatter& f, Locale locale,
+ ...) {
va_list args;
va_start(args, locale);
+ UnicodeString message(TRUE, umessage, -1);
static double inputs[] = {87650000, 8765000, 876500, 87650, 8765, 876.5, 87.65, 8.765, 0};
const LocalizedNumberFormatter l1 = f.threshold(0).locale(locale); // no self-regulation
const LocalizedNumberFormatter l2 = f.threshold(1).locale(locale); // all self-regulation
- UErrorCode status = U_ZERO_ERROR;
+ IcuTestErrorCode status(*this, "assertFormatDescendingBig");
+ status.setScope(message);
+ UnicodeString expecteds[10];
for (int16_t i = 0; i < 9; i++) {
char16_t caseNumber = u'0' + i;
double d = inputs[i];
UnicodeString expected = va_arg(args, const char16_t*);
+ expecteds[i] = expected;
UnicodeString actual1 = l1.formatDouble(d, status).toString();
assertSuccess(message + u": Unsafe Path: " + caseNumber, status);
assertEquals(message + u": Unsafe Path: " + caseNumber, expected, actual1);
assertSuccess(message + u": Safe Path: " + caseNumber, status);
assertEquals(message + u": Safe Path: " + caseNumber, expected, actual2);
}
+ if (uskeleton != nullptr) { // if null, skeleton is declared as undefined.
+ UnicodeString skeleton(TRUE, uskeleton, -1);
+ // Only compare normalized skeletons: the tests need not provide the normalized forms.
+ // Use the normalized form to construct the testing formatter to guarantee no loss of info.
+ UnicodeString normalized = NumberFormatter::fromSkeleton(skeleton, status).toSkeleton(status);
+ assertEquals(message + ": Skeleton:", normalized, f.toSkeleton(status));
+ LocalizedNumberFormatter l3 = NumberFormatter::fromSkeleton(normalized, status).locale(locale);
+ for (int32_t i = 0; i < 9; i++) {
+ double d = inputs[i];
+ UnicodeString actual3 = l3.formatDouble(d, status).toString();
+ assertEquals(message + ": Skeleton Path: " + d, expecteds[i], actual3);
+ }
+ } else {
+ assertUndefinedSkeleton(f);
+ }
}
-void NumberFormatterApiTest::assertFormatSingle(const UnicodeString &message,
- const UnlocalizedNumberFormatter &f, Locale locale,
- double input, const UnicodeString &expected) {
+void NumberFormatterApiTest::assertFormatSingle(const char16_t* umessage, const char16_t* uskeleton,
+ const UnlocalizedNumberFormatter& f, Locale locale,
+ double input, const UnicodeString& expected) {
+ UnicodeString message(TRUE, umessage, -1);
const LocalizedNumberFormatter l1 = f.threshold(0).locale(locale); // no self-regulation
const LocalizedNumberFormatter l2 = f.threshold(1).locale(locale); // all self-regulation
- UErrorCode status = U_ZERO_ERROR;
+ IcuTestErrorCode status(*this, "assertFormatSingle");
+ status.setScope(message);
UnicodeString actual1 = l1.formatDouble(input, status).toString();
assertSuccess(message + u": Unsafe Path", status);
assertEquals(message + u": Unsafe Path", expected, actual1);
UnicodeString actual2 = l2.formatDouble(input, status).toString();
assertSuccess(message + u": Safe Path", status);
assertEquals(message + u": Safe Path", expected, actual2);
+ if (uskeleton != nullptr) { // if null, skeleton is declared as undefined.
+ UnicodeString skeleton(TRUE, uskeleton, -1);
+ // Only compare normalized skeletons: the tests need not provide the normalized forms.
+ // Use the normalized form to construct the testing formatter to ensure no loss of info.
+ UnicodeString normalized = NumberFormatter::fromSkeleton(skeleton, status).toSkeleton(status);
+ assertEquals(message + ": Skeleton:", normalized, f.toSkeleton(status));
+ LocalizedNumberFormatter l3 = NumberFormatter::fromSkeleton(normalized, status).locale(locale);
+ UnicodeString actual3 = l3.formatDouble(input, status).toString();
+ assertEquals(message + ": Skeleton Path: " + input, expected, actual3);
+ } else {
+ assertUndefinedSkeleton(f);
+ }
+}
+
+void NumberFormatterApiTest::assertUndefinedSkeleton(const UnlocalizedNumberFormatter& f) {
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString skeleton = f.toSkeleton(status);
+ assertEquals(
+ u"Expect toSkeleton to fail, but passed, producing: " + skeleton,
+ U_UNSUPPORTED_ERROR,
+ status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */