use of android.icu.text.RuleBasedNumberFormat in project j2objc by google.
the class NumberFormatTest method TestFormatAbstractImplCoverage.
/*
* Coverage tests for the implementation of abstract format methods not being called otherwise
*/
public void TestFormatAbstractImplCoverage() {
NumberFormat df = DecimalFormat.getInstance(Locale.ENGLISH);
NumberFormat cdf = CompactDecimalFormat.getInstance(Locale.ENGLISH, CompactDecimalFormat.CompactStyle.SHORT);
NumberFormat rbf = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
/*
* Test NumberFormat.format(BigDecimal,StringBuffer,FieldPosition)
*/
StringBuffer sb = new StringBuffer();
String result = df.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
if (!"2,000.43".equals(result)) {
errln("DecimalFormat failed. Expected: 2,000.43 - Actual: " + result);
}
sb.delete(0, sb.length());
result = cdf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
if (!"2K".equals(result)) {
errln("DecimalFormat failed. Expected: 2K - Actual: " + result);
}
sb.delete(0, sb.length());
result = rbf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
if (!"two thousand point four three".equals(result)) {
errln("DecimalFormat failed. Expected: 'two thousand point four three' - Actual: '" + result + "'");
}
}
use of android.icu.text.RuleBasedNumberFormat in project j2objc by google.
the class RbnfLenientScannerTest method TestDefaultProvider.
/**
* Ensure that the default provider is instantiated and used if none is set
* and lenient parse is on.
*/
@Test
public void TestDefaultProvider() {
RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.SPELLOUT);
formatter.setLenientScannerProvider(null);
formatter.setLenientParseMode(true);
String[][] lpTestData = { { "2 thousand six HUNDRED fifty-7", "2,657" } };
doLenientParseTest(formatter, lpTestData);
}
use of android.icu.text.RuleBasedNumberFormat in project j2objc by google.
the class RbnfLenientScannerTest method TestFrenchSpellout.
/**
* Perform a simple spot check on the French spellout rules
*/
@Test
public void TestFrenchSpellout() {
RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.FRANCE, RuleBasedNumberFormat.SPELLOUT);
formatter.setLenientScannerProvider(provider);
formatter.setLenientParseMode(true);
String[][] lpTestData = { { "trente-et-un", "31" }, { "un cent quatre vingt dix huit", "198" } };
doLenientParseTest(formatter, lpTestData);
}
use of android.icu.text.RuleBasedNumberFormat in project j2objc by google.
the class RbnfLenientScannerTest method TestDurations.
/**
* Perform a simple spot check on the duration-formatting rules
*/
@Test
public void TestDurations() {
RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.DURATION);
formatter.setLenientScannerProvider(provider);
formatter.setLenientParseMode(true);
String[][] lpTestData = { { "2-51-33", "10,293" } };
doLenientParseTest(formatter, lpTestData);
}
use of android.icu.text.RuleBasedNumberFormat in project j2objc by google.
the class RbnfLenientScannerTest method TestAllLocales.
@Test
public void TestAllLocales() {
StringBuffer errors = null;
ULocale[] locales = ULocale.getAvailableLocales();
String[] names = { " (spellout) ", " (ordinal) ", " (duration) " };
double[] numbers = { 45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111 };
Random r = null;
// RBNF parse is extremely slow when lenient option is enabled.
// For non-exhaustive mode, we only test a few locales.
// "nl_NL", "be" had crash problem reported by #6534
String[] parseLocales = { "en_US", "nl_NL", "be" };
for (int i = 0; i < locales.length; ++i) {
ULocale loc = locales[i];
int count = numbers.length;
boolean testParse = true;
if (TestFmwk.getExhaustiveness() <= 5) {
testParse = false;
for (int k = 0; k < parseLocales.length; k++) {
if (loc.toString().equals(parseLocales[k])) {
testParse = true;
break;
}
}
} else {
// RBNF parse is too slow. Increase count only for debugging purpose for now.
// count = 100;
}
for (int j = 0; j < 3; ++j) {
RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(loc, j + 1);
for (int c = 0; c < count; c++) {
double n;
if (c < numbers.length) {
n = numbers[c];
} else {
if (r == null) {
r = createRandom();
}
n = ((int) (r.nextInt(10000) - 3000)) / 16d;
}
String s = fmt.format(n);
logln(loc.getName() + names[j] + "success format: " + n + " -> " + s);
if (testParse) {
// because there are cases which do not round trip by design.
try {
// non-lenient parse
fmt.setLenientParseMode(false);
Number num = fmt.parse(s);
logln(loc.getName() + names[j] + "success parse: " + s + " -> " + num);
// lenient parse
fmt.setLenientScannerProvider(provider);
fmt.setLenientParseMode(true);
num = fmt.parse(s);
logln(loc.getName() + names[j] + "success parse (lenient): " + s + " -> " + num);
} catch (ParseException pe) {
String msg = loc.getName() + names[j] + "ERROR:" + pe.getMessage();
logln(msg);
if (errors == null) {
errors = new StringBuffer();
}
errors.append("\n" + msg);
}
}
}
}
}
if (errors != null) {
// TODO: We need to fix parse problems - see #6895 / #6896
// errln(errors.toString());
logln(errors.toString());
}
}
Aggregations